Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Jan 24, 2022
1 parent bc9c4e9 commit e495d48
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
35 changes: 35 additions & 0 deletions __mocks__/typedDataExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"types": {
"StarkNetDomain": [
{ "name": "name", "type": "felt" },
{ "name": "version", "type": "felt" },
{ "name": "chainId", "type": "felt" }
],
"Person": [
{ "name": "name", "type": "felt" },
{ "name": "wallet", "type": "felt" }
],
"Mail": [
{ "name": "from", "type": "Person" },
{ "name": "to", "type": "Person" },
{ "name": "contents", "type": "felt" }
]
},
"primaryType": "Mail",
"domain": {
"name": "StarkNet Mail",
"version": "1",
"chainId": 1
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}
6 changes: 6 additions & 0 deletions __tests__/signer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';

import typedDataExample from '../__mocks__/typedDataExample.json';
import {
CompiledContract,
Contract,
Expand Down Expand Up @@ -116,4 +117,9 @@ describe('deploy and test Wallet', () => {
expect(code).toBe('TRANSACTION_RECEIVED');
await defaultProvider.waitForTx(transaction_hash);
});
test('sign and verify offchain message', async () => {
const signature = await signer.signMessage(typedDataExample);

expect(await signer.verifyMessage(typedDataExample, signature)).toBe(true);
});
});
37 changes: 1 addition & 36 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,6 @@
import typedDataExample from '../../__mocks__/typedDataExample.json';
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';

const typedDataExample = {
types: {
StarkNetDomain: [
{ name: 'name', type: 'felt' },
{ name: 'version', type: 'felt' },
{ name: 'chainId', type: 'felt' },
],
Person: [
{ name: 'name', type: 'felt' },
{ name: 'wallet', type: 'felt' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'felt' },
],
},
primaryType: 'Mail',
domain: {
name: 'StarkNet Mail',
version: '1',
chainId: 1,
},
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
};

describe('typedData', () => {
test('should get right type encoding', () => {
const typeEncoding = encodeType(typedDataExample, 'Mail');
Expand Down
24 changes: 13 additions & 11 deletions src/signer/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,19 @@ export class Signer extends Provider implements SignerInterface {
* @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());
try {
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()),
}),
});
return true;
} catch {
return false;
}
}

/**
Expand Down

0 comments on commit e495d48

Please sign in to comment.