Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 10, 2021
1 parent b414a83 commit a3813c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
28 changes: 24 additions & 4 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMessageHash, getStructHash } from '../../src/utils/typedData';
import { encodeType, getMessageHash, getStructHash, getTypeHash } from '../../src/utils/typedData';

const typedDataExample = {
types: {
Expand All @@ -19,7 +19,7 @@ const typedDataExample = {
},
primaryType: 'Mail',
domain: {
name: 'Ether Mail',
name: 'StarkNet Mail',
version: '1',
chainId: 1,
},
Expand All @@ -37,16 +37,36 @@ const typedDataExample = {
};

describe('typedData', () => {
test('should get right type encoding', () => {
const typeEncoding = encodeType(typedDataExample, 'Mail');
expect(typeEncoding).toMatchInlineSnapshot(
`"Mail(from:Person,to:Person,contents:felt)Person(name:felt,wallet:felt)"`
);
});
test('should get right type hash', () => {
const typeHashDomain = getTypeHash(typedDataExample, 'StarkNetDomain');
expect(typeHashDomain).toMatchInlineSnapshot(
`"0x1bfc207425a47a5dfa1a50a4f5241203f50624ca5fdf5e18755765416b8e288"`
);
const typeHashPerson = getTypeHash(typedDataExample, 'Person');
expect(typeHashPerson).toMatchInlineSnapshot(
`"0x2896dbe4b96a67110f454c01e5336edc5bbc3635537efd690f122f4809cc855"`
);
const typeHashMail = getTypeHash(typedDataExample, 'Mail');
expect(typeHashMail).toMatchInlineSnapshot(
`"0x13d89452df9512bf750f539ba3001b945576243288137ddb6c788457d4b2f79"`
);
});
test('should get right hash for StarkNetDomain', () => {
const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
expect(hash).toMatchInlineSnapshot(
`"0x3a53775bb506be3f4f84619cd2d063a9408ba2b2e7fe134b82b04a62783eef9"`
`"0x54833b121883a3e3aebff48ec08a962f5742e5f7b973469c1f8f4f55d470b07"`
);
});
test('should get right hash for entire message', () => {
const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
expect(hash).toMatchInlineSnapshot(
`"0x214aad847084997443f3bace488411e46dfff96dce13f0356107d0fc12b1219"`
`"0x6fcff244f63e38b9d88b9e3378d44757710d1b244282b435cb472053c8d78d0"`
);
});
});
4 changes: 3 additions & 1 deletion src/signer/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export abstract class SignerInterface extends Provider {
): Promise<AddTransactionResponse>;

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

/**
* Hash a JSON object with pederson hash and return the hash
* This adds a message prefix so it cant be interchanged with transactions
*
* @param json - JSON object to be hashed
* @returns the hash of the JSON object
Expand Down
2 changes: 1 addition & 1 deletion src/utils/typedData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const encodeType = (typedData: TypedData, type: string): string => {

return types
.map((dependency) => {
return `${dependency}(${typedData.types[dependency].map((t) => `${t.type} ${t.name}`)})`;
return `${dependency}(${typedData.types[dependency].map((t) => `${t.name}:${t.type}`)})`;
})
.join('');
};
Expand Down
4 changes: 2 additions & 2 deletions src/utils/typedData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
union,
} from 'superstruct';

export const STATIC_TYPES = ['felt', 'felt*'];
export const ATOMIC_TYPES = ['felt', 'felt*'];

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

Expand Down

0 comments on commit a3813c9

Please sign in to comment.