Skip to content

Commit a3813c9

Browse files
committed
fix: review
1 parent b414a83 commit a3813c9

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

__tests__/utils/typedData.test.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getMessageHash, getStructHash } from '../../src/utils/typedData';
1+
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';
22

33
const typedDataExample = {
44
types: {
@@ -19,7 +19,7 @@ const typedDataExample = {
1919
},
2020
primaryType: 'Mail',
2121
domain: {
22-
name: 'Ether Mail',
22+
name: 'StarkNet Mail',
2323
version: '1',
2424
chainId: 1,
2525
},
@@ -37,16 +37,36 @@ const typedDataExample = {
3737
};
3838

3939
describe('typedData', () => {
40+
test('should get right type encoding', () => {
41+
const typeEncoding = encodeType(typedDataExample, 'Mail');
42+
expect(typeEncoding).toMatchInlineSnapshot(
43+
`"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"`
44+
);
45+
});
46+
test('should get right type hash', () => {
47+
const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain');
48+
expect(typeHashDomain).toMatchInlineSnapshot(
49+
`"0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288"`
50+
);
51+
const typeHashPerson = getTypeHash(typedDataExample, 'Person');
52+
expect(typeHashPerson).toMatchInlineSnapshot(
53+
`"0x2896dbe4b96a67110f454c01e5336edc5bbc3635537efd690f122f4809cc855"`
54+
);
55+
const typeHashMail = getTypeHash(typedDataExample, 'Mail');
56+
expect(typeHashMail).toMatchInlineSnapshot(
57+
`"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"`
58+
);
59+
});
4060
test('should get right hash for StarkNetDomain', () => {
4161
const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
4262
expect(hash).toMatchInlineSnapshot(
43-
`"0x3a53775bb506be3f4f84619cd2d063a9408ba2b2e7fe134b82b04a62783eef9"`
63+
`"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"`
4464
);
4565
});
4666
test('should get right hash for entire message', () => {
4767
const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
4868
expect(hash).toMatchInlineSnapshot(
49-
`"0x214aad847084997443f3bace488411e46dfff96dce13f0356107d0fc12b1219"`
69+
`"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
5070
);
5171
});
5272
});

src/signer/interface.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export abstract class SignerInterface extends Provider {
1717
): Promise<AddTransactionResponse>;
1818

1919
/**
20-
* Sign an JSON object with the starknet private key and return the signature
20+
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
21+
* This adds a message prefix so it cant be interchanged with transactions
2122
*
2223
* @param json - JSON object to be signed
2324
* @returns the signature of the JSON object
@@ -27,6 +28,7 @@ export abstract class SignerInterface extends Provider {
2728

2829
/**
2930
* Hash a JSON object with pederson hash and return the hash
31+
* This adds a message prefix so it cant be interchanged with transactions
3032
*
3133
* @param json - JSON object to be hashed
3234
* @returns the hash of the JSON object

src/utils/typedData/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const encodeType = (typedData: TypedData, type: string): string => {
7272

7373
return types
7474
.map((dependency) => {
75-
return `${dependency}(${typedData.types[dependency].map((t) => `${t.type} ${t.name}`)})`;
75+
return `${dependency}(${typedData.types[dependency].map((t) => `${t.name}:${t.type}`)})`;
7676
})
7777
.join('');
7878
};

src/utils/typedData/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
union,
1313
} from 'superstruct';
1414

15-
export const STATIC_TYPES = ['felt', 'felt*'];
15+
export const ATOMIC_TYPES = ['felt', 'felt*'];
1616

1717
// Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
1818
// and modified to support starknet types
@@ -27,7 +27,7 @@ export const STATIC_TYPES = ['felt', 'felt*'];
2727
* @return {boolean}
2828
*/
2929
export const isValidType = (types: Record<string, unknown>, type: string): boolean => {
30-
if (STATIC_TYPES.includes(type as string)) {
30+
if (ATOMIC_TYPES.includes(type as string)) {
3131
return true;
3232
}
3333

0 commit comments

Comments
 (0)