Skip to content

Commit

Permalink
feat: replace in verifyMessage : response by Error
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeR26 committed Jan 17, 2024
1 parent ae49d2c commit 1abf91f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 76 deletions.
26 changes: 5 additions & 21 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Contract,
DeclareDeployUDCResponse,
Provider,
SignatureVerifResult,
TransactionType,
cairo,
constants,
Expand Down Expand Up @@ -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', () => {
Expand Down
50 changes: 13 additions & 37 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
Nonce,
ProviderOptions,
Signature,
SignatureVerifResult,
SimulateTransactionDetails,
SimulateTransactionResponse,
TransactionType,
Expand Down Expand Up @@ -549,10 +548,7 @@ export class Account extends Provider implements AccountInterface {
return getMessageHash(typedData, this.address);
}

public async verifyMessageHash(
hash: BigNumberish,
signature: Signature
): Promise<SignatureVerifResult> {
public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean> {
try {
const resp = await this.callContract({
contractAddress: this.address,
Expand All @@ -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<SignatureVerifResult> {
public async verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean> {
const hash = await this.hashMessage(typedData);
return this.verifyMessageHash(hash, signature);
}
Expand Down
11 changes: 2 additions & 9 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
MultiDeployContractResponse,
Nonce,
Signature,
SignatureVerifResult,
SimulateTransactionDetails,
SimulateTransactionResponse,
TypedData,
Expand Down Expand Up @@ -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<SignatureVerifResult>;
public abstract verifyMessage(typedData: TypedData, signature: Signature): Promise<boolean>;

/**
* Verify a signature of a given hash
Expand All @@ -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<SignatureVerifResult>;
public abstract verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean>;

/**
* Gets the nonce of the account with respect to a specific block
Expand Down
5 changes: 0 additions & 5 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions www/docs/guides/signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```

0 comments on commit 1abf91f

Please sign in to comment.