Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypedDataUtils.sanitizeData tests #172

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,82 @@ describe('TypedDataUtils.findTypeDependencies', () => {
});
});

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);
Expand Down