Skip to content

Commit

Permalink
fix: expand encoding type for preset types
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Mar 12, 2024
1 parent 6cd35aa commit b992446
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions __tests__/utils/typedData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('typedData', () => {
);
encoded = encodeType(examplePresetTypes.types, 'Example', TypedDataRevision.Active);
expect(encoded).toMatchInlineSnapshot(
`"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")"`
`"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")\\"NftId\\"(\\"collection_address\\":\\"ContractAddress\\",\\"token_id\\":\\"u256\\")\\"TokenAmount\\"(\\"token_address\\":\\"ContractAddress\\",\\"amount\\":\\"u256\\")\\"u256\\"(\\"low\\":\\"u128\\",\\"high\\":\\"u128\\")"`
);
encoded = encodeType(exampleEnum.types, 'Example', TypedDataRevision.Active);
expect(encoded).toMatchInlineSnapshot(
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('typedData', () => {
);
typeHash = getTypeHash(examplePresetTypes.types, 'Example', TypedDataRevision.Active);
expect(typeHash).toMatchInlineSnapshot(
`"0x155de33c6a0cc7f2b8926afc7a71fc2ac31ffc26726aee5da0570c5d517a763"`
`"0x1a25a8bb84b761090b1fadaebe762c4b679b0d8883d2bedda695ea340839a55"`
);
typeHash = getTypeHash(exampleEnum.types, 'Example', TypedDataRevision.Active);
expect(typeHash).toMatchInlineSnapshot(
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('typedData', () => {

messageHash = getMessageHash(examplePresetTypes, exampleAddress);
expect(messageHash).toMatchInlineSnapshot(
`"0x26e7b8cedfa63cdbed14e7e51b60ee53ac82bdf26724eb1e3f0710cb8987522"`
`"0x185b339d5c566a883561a88fb36da301051e2c0225deb325c91bb7aa2f3473a"`
);

messageHash = getMessageHash(exampleEnum, exampleAddress);
Expand Down
14 changes: 12 additions & 2 deletions src/utils/typedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,24 @@ export function encodeType(
type: string,
revision: Revision = Revision.Legacy
): string {
const [primary, ...dependencies] = getDependencies(types, type, undefined, undefined, revision);
const allTypes =
revision === Revision.Active
? { ...types, ...revisionConfiguration[revision].presetTypes }
: types;
const [primary, ...dependencies] = getDependencies(
allTypes,
type,
undefined,
undefined,
revision
);
const newTypes = !primary ? [] : [primary, ...dependencies.sort()];

const esc = revisionConfiguration[revision].escapeTypeString;

return newTypes
.map((dependency) => {
const dependencyElements = types[dependency].map((t) => {
const dependencyElements = allTypes[dependency].map((t) => {
const targetType =
t.type === 'enum' && revision === Revision.Active
? (t as StarkNetEnumType).contains
Expand Down

0 comments on commit b992446

Please sign in to comment.