Skip to content

Commit

Permalink
feat: implement verifyMessage and verifyMessageHash
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Jan 21, 2022
1 parent b61a28c commit bc9c4e9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/signer/default.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -98,4 +99,40 @@ export class Signer extends Provider implements SignerInterface {
public async hashMessage(typedData: TypedData): Promise<string> {
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<boolean> {
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<boolean> {
const hash = await this.hashMessage(typedData);
return this.verifyMessageHash(hash, signature);
}
}
22 changes: 22 additions & 0 deletions src/signer/interface.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<string>;

/**
* 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<boolean>;

/**
* 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<boolean>;
}

0 comments on commit bc9c4e9

Please sign in to comment.