From bc9c4e9574cc453af35705eb4488602ea33cc2cb Mon Sep 17 00:00:00 2001 From: Janek Rahrt Date: Fri, 21 Jan 2022 15:03:15 +0100 Subject: [PATCH] feat: implement verifyMessage and verifyMessageHash --- src/signer/default.ts | 39 ++++++++++++++++++++++++++++++++++++++- src/signer/interface.ts | 22 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/signer/default.ts b/src/signer/default.ts index 91e54bd30..cfb1e8d48 100644 --- a/src/signer/default.ts +++ b/src/signer/default.ts @@ -1,11 +1,12 @@ import assert from 'minimalistic-assert'; +import { compileCalldata } from '../contract'; import { Provider } from '../provider'; import { AddTransactionResponse, KeyPair, Signature, Transaction } from '../types'; import { sign } from '../utils/ellipticCurve'; import { addHexPrefix } from '../utils/encode'; import { hashMessage } from '../utils/hash'; -import { toBN } from '../utils/number'; +import { BigNumberish, toBN } from '../utils/number'; import { getSelectorFromName } from '../utils/stark'; import { TypedData, getMessageHash } from '../utils/typedData'; import { SignerInterface } from './interface'; @@ -98,4 +99,40 @@ export class Signer extends Provider implements SignerInterface { public async hashMessage(typedData: TypedData): Promise { return getMessageHash(typedData, this.address); } + + /** + * Verify a signature of a JSON object + * + * @param json - JSON object to be verified + * @param signature - signature of the JSON object + * @returns true if the signature is valid, false otherwise + * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature + */ + public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise { + const { result } = await this.callContract({ + contract_address: this.address, + entry_point_selector: getSelectorFromName('is_valid_signature'), + calldata: compileCalldata({ + hash: toBN(hash).toString(), + signature: signature.map((x) => toBN(x).toString()), + }), + }); + + // 0 is false, 1 is true + return Boolean(toBN(result[0]).toNumber()); + } + + /** + * Verify a signature of a given hash + * @warning This method is not recommended, use verifyMessage instead + * + * @param hash - hash to be verified + * @param signature - signature of the hash + * @returns true if the signature is valid, false otherwise + * @throws {Error} if the signature is not a valid signature + */ + public async verifyMessage(typedData: TypedData, signature: Signature): Promise { + const hash = await this.hashMessage(typedData); + return this.verifyMessageHash(hash, signature); + } } diff --git a/src/signer/interface.ts b/src/signer/interface.ts index 5ea9cdf66..7144febf1 100644 --- a/src/signer/interface.ts +++ b/src/signer/interface.ts @@ -1,5 +1,6 @@ import { Provider } from '../provider'; import { AddTransactionResponse, Signature, Transaction } from '../types'; +import { BigNumberish } from '../utils/number'; import { TypedData } from '../utils/typedData/types'; export abstract class SignerInterface extends Provider { @@ -35,4 +36,25 @@ export abstract class SignerInterface extends Provider { * @throws {Error} if the JSON object is not a valid JSON */ public abstract hashMessage(typedData: TypedData): Promise; + + /** + * Verify a signature of a JSON object + * + * @param json - JSON object to be verified + * @param signature - signature of the JSON object + * @returns true if the signature is valid, false otherwise + * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature + */ + public abstract verifyMessage(typedData: TypedData, signature: Signature): Promise; + + /** + * Verify a signature of a given hash + * @warning This method is not recommended, use verifyMessage instead + * + * @param hash - hash to be verified + * @param signature - signature of the hash + * @returns true if the signature is valid, false otherwise + * @throws {Error} if the signature is not a valid signature + */ + public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise; }