Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 10, 2021
1 parent 039a360 commit b414a83
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
52 changes: 52 additions & 0 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getMessageHash, getStructHash } 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: 'Ether 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 hash for StarkNetDomain', () => {
const hash = getStructHash(typedDataExample, 'StarkNetDomain', typedDataExample.domain as any);
expect(hash).toMatchInlineSnapshot(
`"0x3a53775bb506be3f4f84619cd2d063a9408ba2b2e7fe134b82b04a62783eef9"`
);
});
test('should get right hash for entire message', () => {
const hash = getMessageHash(typedDataExample, '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826');
expect(hash).toMatchInlineSnapshot(
`"0x214aad847084997443f3bace488411e46dfff96dce13f0356107d0fc12b1219"`
);
});
});
23 changes: 15 additions & 8 deletions src/utils/typedData/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { computeHashOnElements } from '../hash';
import { BigNumberish } from '../number';
import { BigNumberish, toBN, toHex } from '../number';
import { encodeShortString } from '../shortString';
import { getSelectorFromName } from '../stark';
import { TypedData } from './types';
import { validateTypedData } from './utils';

export * from './types';

function getHex(value: BigNumberish): string {
try {
return toHex(toBN(value));
} catch (e) {
if (typeof value === 'string') {
return toHex(toBN(encodeShortString(value)));
}
throw new Error(`Invalid BigNumberish: ${value}`);
}
}

/**
* Get the dependencies of a struct type. If a struct has the same dependency multiple times, it's only included once
* in the resulting array.
Expand Down Expand Up @@ -89,18 +100,14 @@ export const getTypeHash = (typedData: TypedData, type: string): string => {
const encodeValue = (typedData: TypedData, type: string, data: unknown): [string, string] => {
if (typedData.types[type]) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return ['felt', getStructHash(typedData, type, data as Record<string, unknown>)];
}

if (type === 'shortString') {
return ['felt', encodeShortString(data as string)];
return [type, getStructHash(typedData, type, data as Record<string, unknown>)];
}

if (type === 'felt*') {
return ['felt', computeHashOnElements(data as string[])];
return ['felt*', computeHashOnElements(data as string[])];
}

return [type, data as string];
return [type, getHex(data as string)];
};

/**
Expand Down
3 changes: 1 addition & 2 deletions src/utils/typedData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import {
union,
} from 'superstruct';

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

// Source: https://github.com/Mrtenz/eip-712/blob/master/src/eip-712.ts
// and modified to support starknet types

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

0 comments on commit b414a83

Please sign in to comment.