From 54b00e0991b7d1d96f2085e0a5b4bd74ef69a6a3 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 9 Aug 2021 15:07:35 -0230 Subject: [PATCH] Add `TypedDataUtils.sanitizeData` tests This function should now be comprehensively tested. --- src/index.test.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index fa2c56f9..649d6547 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -2984,6 +2984,82 @@ describe('TypedDataUtils.hashType', () => { }); }); +describe('TypedDataUtils.sanitizeData', function () { + it('should return correctly formatted data unchanged', function () { + const typedMessage = { + domain: {}, + message: {}, + primaryType: 'Person' as const, + types: { + EIP712Domain: [{ name: 'name', type: 'string' }], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + }, + }; + + const sanitizedTypedMessage = + sigUtil.TypedDataUtils.sanitizeData(typedMessage); + + expect(sanitizedTypedMessage).toStrictEqual(typedMessage); + }); + + it("should add `EIP712Domain` to `types` if it's missing", function () { + const typedMessage = { + domain: {}, + message: {}, + primaryType: 'Person' as const, + types: { + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + }, + }; + + const sanitizedTypedMessage = sigUtil.TypedDataUtils.sanitizeData( + typedMessage as any, + ); + + expect(sanitizedTypedMessage).toStrictEqual({ + ...typedMessage, + types: { ...typedMessage.types, EIP712Domain: [] }, + }); + }); + + it('should sanitize empty object', function () { + const typedMessage = {}; + + const sanitizedTypedMessage = sigUtil.TypedDataUtils.sanitizeData( + typedMessage as any, + ); + + expect(sanitizedTypedMessage).toStrictEqual({}); + }); + + it('should omit unrecognized properties', function () { + const expectedMessage = { + domain: {}, + message: {}, + primaryType: 'Person' as const, + types: { + EIP712Domain: [{ name: 'name', type: 'string' }], + Person: [ + { name: 'name', type: 'string' }, + { name: 'wallet', type: 'address' }, + ], + }, + }; + const typedMessage = { ...expectedMessage, extraStuff: 'Extra stuff' }; + + const sanitizedTypedMessage = + sigUtil.TypedDataUtils.sanitizeData(typedMessage); + + expect(sanitizedTypedMessage).toStrictEqual(expectedMessage); + }); +}); + it('normalize address lower cases', function () { const initial = '0xA06599BD35921CfB5B71B4BE3869740385b0B306'; const result = sigUtil.normalize(initial);