From 1abf91f170c46d4f4cf5f493dc3d6717c6d3c1e7 Mon Sep 17 00:00:00 2001 From: PhilippeR26 Date: Wed, 17 Jan 2024 12:05:29 +0100 Subject: [PATCH] feat: replace in verifyMessage : response by Error --- __tests__/account.test.ts | 26 ++++--------------- src/account/default.ts | 50 ++++++++++-------------------------- src/account/interface.ts | 11 ++------ src/types/lib/index.ts | 5 ---- www/docs/guides/signature.md | 8 +++--- 5 files changed, 24 insertions(+), 76 deletions(-) diff --git a/__tests__/account.test.ts b/__tests__/account.test.ts index 3c8ab4cb3..b607ac8bb 100644 --- a/__tests__/account.test.ts +++ b/__tests__/account.test.ts @@ -4,7 +4,6 @@ import { Contract, DeclareDeployUDCResponse, Provider, - SignatureVerifResult, TransactionType, cairo, constants, @@ -411,32 +410,17 @@ describe('deploy and test Wallet', () => { if (!signature2) return; - const verifMessageResponse: SignatureVerifResult = await account.verifyMessage( - typedDataExample, - signature2 - ); - expect(verifMessageResponse.isVerificationProcessed).toBe(true); - expect(verifMessageResponse.isSignatureValid).toBe(false); + const verifMessageResponse: boolean = await account.verifyMessage(typedDataExample, signature2); + expect(verifMessageResponse).toBe(false); const wrongAccount = new Account(provider, '0x037891', '0x026789', undefined, TEST_TX_VERSION); // non existing account - const verifMessageResponse2: SignatureVerifResult = await wrongAccount.verifyMessage( - typedDataExample, - signature2 - ); - expect(verifMessageResponse2.isVerificationProcessed).toBe(false); - expect(verifMessageResponse2.error?.message).toContain( - 'Signature verification request is rejected by the network.' - ); + await expect(wrongAccount.verifyMessage(typedDataExample, signature2)).rejects.toThrow(); }); test('sign and verify message', async () => { const signature = await account.signMessage(typedDataExample); - const verifMessageResponse: SignatureVerifResult = await account.verifyMessage( - typedDataExample, - signature - ); - expect(verifMessageResponse.isVerificationProcessed).toBe(true); - expect(verifMessageResponse.isSignatureValid).toBe(true); + const verifMessageResponse: boolean = await account.verifyMessage(typedDataExample, signature); + expect(verifMessageResponse).toBe(true); }); describe('Contract interaction with Account', () => { diff --git a/src/account/default.ts b/src/account/default.ts index 96b3da3e0..47620bc72 100644 --- a/src/account/default.ts +++ b/src/account/default.ts @@ -31,7 +31,6 @@ import { Nonce, ProviderOptions, Signature, - SignatureVerifResult, SimulateTransactionDetails, SimulateTransactionResponse, TransactionType, @@ -549,10 +548,7 @@ export class Account extends Provider implements AccountInterface { return getMessageHash(typedData, this.address); } - public async verifyMessageHash( - hash: BigNumberish, - signature: Signature - ): Promise { + public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise { try { const resp = await this.callContract({ contractAddress: this.address, @@ -562,46 +558,26 @@ export class Account extends Provider implements AccountInterface { signature: formatSignature(signature), }), }); - // console.log('verifySign=', resp); - if (BigInt(resp.result[0]) === 0n) { + if (BigInt(resp[0]) === 0n) { // OpenZeppelin 0.8.0 invalid signature - return { - isVerificationProcessed: true, - isSignatureValid: false, - } as SignatureVerifResult; + return false; } // OpenZeppelin 0.8.0, ArgentX 0.3.0 & Braavos Cairo 0 valid signature - return { - isVerificationProcessed: true, - isSignatureValid: true, - } as SignatureVerifResult; + return true; } catch (err) { - // console.log('verifySign error=', err); - if ((err as Error).message.includes('argent/invalid-signature')) { - // ArgentX 0.3.0 invalid signature - return { - isVerificationProcessed: true, - isSignatureValid: false, - } as SignatureVerifResult; - } - if ((err as Error).message.includes('is invalid, with respect to the public key')) { - // Braavos Cairo 0 invalid signature - return { - isVerificationProcessed: true, - isSignatureValid: false, - } as SignatureVerifResult; + if ( + ['argent/invalid-signature', 'is invalid, with respect to the public key'].some( + (errMessage) => (err as Error).message.includes(errMessage) + ) + ) { + // ArgentX 0.3.0 invalid signature, Braavos Cairo 0 invalid signature + return false; } - return { - isVerificationProcessed: false, - error: new Error('Signature verification request is rejected by the network.'), - } as SignatureVerifResult; + throw Error(`Signature verification request is rejected by the network: ${err}`); } } - public async verifyMessage( - typedData: TypedData, - signature: Signature - ): Promise { + public async verifyMessage(typedData: TypedData, signature: Signature): Promise { const hash = await this.hashMessage(typedData); return this.verifyMessageHash(hash, signature); } diff --git a/src/account/interface.ts b/src/account/interface.ts index 72a20c161..3db5f9e62 100644 --- a/src/account/interface.ts +++ b/src/account/interface.ts @@ -25,7 +25,6 @@ import { MultiDeployContractResponse, Nonce, Signature, - SignatureVerifResult, SimulateTransactionDetails, SimulateTransactionResponse, TypedData, @@ -357,10 +356,7 @@ export abstract class AccountInterface extends ProviderInterface { * @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; + public abstract verifyMessage(typedData: TypedData, signature: Signature): Promise; /** * Verify a signature of a given hash @@ -371,10 +367,7 @@ export abstract class AccountInterface extends ProviderInterface { * @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; + public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise; /** * Gets the nonce of the account with respect to a specific block diff --git a/src/types/lib/index.ts b/src/types/lib/index.ts index 67a77256f..2b85b9a9a 100644 --- a/src/types/lib/index.ts +++ b/src/types/lib/index.ts @@ -7,11 +7,6 @@ import { CompiledContract, CompiledSierraCasm, ContractClass } from './contract' export type WeierstrassSignatureType = weierstrass.SignatureType; export type ArraySignatureType = string[]; export type Signature = ArraySignatureType | WeierstrassSignatureType; -export type SignatureVerifResult = { - isVerificationProcessed: boolean; - isSignatureValid?: boolean; - error?: Error; -}; export type BigNumberish = string | number | bigint; diff --git a/www/docs/guides/signature.md b/www/docs/guides/signature.md index 8312025e4..8ee836316 100644 --- a/www/docs/guides/signature.md +++ b/www/docs/guides/signature.md @@ -175,10 +175,10 @@ On the receiver side, you receive the JSON, the signature, and the account addre ```typescript const myAccount = new Account(provider, accountAddress, "0x0123"); // fake private key -const result = await myAccount.verifyMessage(typedMessage, signature); -if (result.isVerificationProcessed) { - console.log("Result (boolean) =", result.isSignatureValid); -} else { +try { + const result = await myAccount.verifyMessage(typedMessage, signature); + console.log("Result (boolean) =", result); +} catch { console.log("verification failed :", result.error); } ```