Skip to content

Commit 1c0f7d2

Browse files
authored
Merge pull request #895 from PhilippeR26/fix-verifyMessage
fix: solve wrong response for account.verifyMessage
2 parents 3159e4a + 1abf91f commit 1c0f7d2

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

__tests__/account.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ describe('deploy and test Wallet', () => {
399399
expect(toBigInt(response.number as string).toString()).toStrictEqual('57');
400400
});
401401

402-
test('sign and verify offchain message fail', async () => {
402+
test('sign and verify EIP712 message fail', async () => {
403403
const signature = await account.signMessage(typedDataExample);
404404
const [r, s] = stark.formatSignature(signature);
405405

@@ -410,12 +410,17 @@ describe('deploy and test Wallet', () => {
410410

411411
if (!signature2) return;
412412

413-
expect(await account.verifyMessage(typedDataExample, signature2)).toBe(false);
413+
const verifMessageResponse: boolean = await account.verifyMessage(typedDataExample, signature2);
414+
expect(verifMessageResponse).toBe(false);
415+
416+
const wrongAccount = new Account(provider, '0x037891', '0x026789', undefined, TEST_TX_VERSION); // non existing account
417+
await expect(wrongAccount.verifyMessage(typedDataExample, signature2)).rejects.toThrow();
414418
});
415419

416-
test('sign and verify offchain message', async () => {
420+
test('sign and verify message', async () => {
417421
const signature = await account.signMessage(typedDataExample);
418-
expect(await account.verifyMessage(typedDataExample, signature)).toBe(true);
422+
const verifMessageResponse: boolean = await account.verifyMessage(typedDataExample, signature);
423+
expect(verifMessageResponse).toBe(true);
419424
});
420425

421426
describe('Contract interaction with Account', () => {

src/account/default.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,17 +550,30 @@ export class Account extends Provider implements AccountInterface {
550550

551551
public async verifyMessageHash(hash: BigNumberish, signature: Signature): Promise<boolean> {
552552
try {
553-
await this.callContract({
553+
const resp = await this.callContract({
554554
contractAddress: this.address,
555555
entrypoint: 'isValidSignature',
556556
calldata: CallData.compile({
557557
hash: toBigInt(hash).toString(),
558558
signature: formatSignature(signature),
559559
}),
560560
});
561+
if (BigInt(resp[0]) === 0n) {
562+
// OpenZeppelin 0.8.0 invalid signature
563+
return false;
564+
}
565+
// OpenZeppelin 0.8.0, ArgentX 0.3.0 & Braavos Cairo 0 valid signature
561566
return true;
562-
} catch {
563-
return false;
567+
} catch (err) {
568+
if (
569+
['argent/invalid-signature', 'is invalid, with respect to the public key'].some(
570+
(errMessage) => (err as Error).message.includes(errMessage)
571+
)
572+
) {
573+
// ArgentX 0.3.0 invalid signature, Braavos Cairo 0 invalid signature
574+
return false;
575+
}
576+
throw Error(`Signature verification request is rejected by the network: ${err}`);
564577
}
565578
}
566579

www/docs/guides/signature.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,11 @@ const signature2 = await account.signMessage(typedDataValidate) as WeierstrassSi
174174
On the receiver side, you receive the JSON, the signature, and the account address. To verify the message:
175175

176176
```typescript
177-
const compiledAccount = json.parse(fs.readFileSync("./compiledContracts/Account_0_5_1.json").toString("ascii"));
178-
const contractAccount = new Contract(compiledAccount.abi, accountAddress, provider);
179-
180-
const msgHash5 = typedData.getMessageHash(typedDataValidate, accountAddress);
181-
// The call of isValidSignature will generate an error if not valid
182-
let result5: boolean;
177+
const myAccount = new Account(provider, accountAddress, "0x0123"); // fake private key
183178
try {
184-
await contractAccount.isValidSignature(msgHash5, [signature2.r, signature2.s]);
185-
result5 = true;
179+
const result = await myAccount.verifyMessage(typedMessage, signature);
180+
console.log("Result (boolean) =", result);
186181
} catch {
187-
result5 = false;
182+
console.log("verification failed :", result.error);
188183
}
189-
console.log("Result5 (boolean) =", result5);
190184
```

0 commit comments

Comments
 (0)