Skip to content

Commit b414a83

Browse files
committed
feat: add tests
1 parent 039a360 commit b414a83

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

__tests__/utils/typedData.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getMessageHash, getStructHash } from '../../src/utils/typedData';
2+
3+
const typedDataExample = {
4+
types: {
5+
StarkNetDomain: [
6+
{ name: 'name', type: 'felt' },
7+
{ name: 'version', type: 'felt' },
8+
{ name: 'chainId', type: 'felt' },
9+
],
10+
Person: [
11+
{ name: 'name', type: 'felt' },
12+
{ name: 'wallet', type: 'felt' },
13+
],
14+
Mail: [
15+
{ name: 'from', type: 'Person' },
16+
{ name: 'to', type: 'Person' },
17+
{ name: 'contents', type: 'felt' },
18+
],
19+
},
20+
primaryType: 'Mail',
21+
domain: {
22+
name: 'Ether Mail',
23+
version: '1',
24+
chainId: 1,
25+
},
26+
message: {
27+
from: {
28+
name: 'Cow',
29+
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
30+
},
31+
to: {
32+
name: 'Bob',
33+
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
34+
},
35+
contents: 'Hello, Bob!',
36+
},
37+
};
38+
39+
describe('typedData', () => {
40+
test('should get right hash for StarkNetDomain', () => {
41+
const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
42+
expect(hash).toMatchInlineSnapshot(
43+
`"0x3a53775bb506be3f4f84619cd2d063a9408ba2b2e7fe134b82b04a62783eef9"`
44+
);
45+
});
46+
test('should get right hash for entire message', () => {
47+
const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
48+
expect(hash).toMatchInlineSnapshot(
49+
`"0x214aad847084997443f3bace488411e46dfff96dce13f0356107d0fc12b1219"`
50+
);
51+
});
52+
});

src/utils/typedData/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { computeHashOnElements } from '../hash';
2-
import { BigNumberish } from '../number';
2+
import { BigNumberish, toBN, toHex } from '../number';
33
import { encodeShortString } from '../shortString';
44
import { getSelectorFromName } from '../stark';
55
import { TypedData } from './types';
66
import { validateTypedData } from './utils';
77

88
export * from './types';
99

10+
function getHex(value: BigNumberish): string {
11+
try {
12+
return toHex(toBN(value));
13+
} catch (e) {
14+
if (typeof value === 'string') {
15+
return toHex(toBN(encodeShortString(value)));
16+
}
17+
throw new Error(`Invalid BigNumberish: ${value}`);
18+
}
19+
}
20+
1021
/**
1122
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
1223
* in the resulting array.
@@ -89,18 +100,14 @@ export const getTypeHash = (typedData: TypedData, type: string): string => {
89100
const encodeValue = (typedData: TypedData, type: string, data: unknown): [string, string] => {
90101
if (typedData.types[type]) {
91102
// eslint-disable-next-line @typescript-eslint/no-use-before-define
92-
return ['felt', getStructHash(typedData, type, data as Record<string, unknown>)];
93-
}
94-
95-
if (type === 'shortString') {
96-
return ['felt', encodeShortString(data as string)];
103+
return [type, getStructHash(typedData, type, data as Record<string, unknown>)];
97104
}
98105

99106
if (type === 'felt*') {
100-
return ['felt', computeHashOnElements(data as string[])];
107+
return ['felt*', computeHashOnElements(data as string[])];
101108
}
102109

103-
return [type, data as string];
110+
return [type, getHex(data as string)];
104111
};
105112

106113
/**

src/utils/typedData/types.ts

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

15-
export const STATIC_TYPES = ['felt', 'felt*', 'shortString'];
15+
export const STATIC_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
1919

2020
/**
2121
* Checks if a type is valid with the given `typedData`. The following types are valid:
2222
* - Atomic types: felt, felt*
23-
* - Dynamic types: shortString
2423
* - Reference types: struct type (e.g. SomeStruct)
2524
*
2625
* @param {Record<string, unknown>} types

0 commit comments

Comments
 (0)