Skip to content

Commit

Permalink
Bump @spruceid/siwe-parser to 2.1.0 (#4141)
Browse files Browse the repository at this point in the history
## Explanation
This pull request bumps the siwe-parser to the latest version which adds
additional support for the `scheme` parameter. I also refactor the tests
to explicitly call the siwe-parser library to act as a smoke test that
can catch any issues in the future. This will make breaking changes
easier to catch.

## References

MetaMask/MetaMask-planning#2278

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

None

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
NicholasEllul authored Apr 11, 2024
1 parent 48a661e commit 498e748
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@metamask/eth-query": "^4.0.0",
"@metamask/ethjs-unit": "^0.3.0",
"@metamask/utils": "^8.3.0",
"@spruceid/siwe-parser": "1.1.3",
"@spruceid/siwe-parser": "2.1.0",
"@types/bn.js": "^5.1.5",
"bn.js": "^5.2.1",
"eth-ens-namehash": "^2.0.8",
Expand Down
51 changes: 20 additions & 31 deletions packages/controller-utils/src/siwe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,39 @@ import { ParsedMessage } from '@spruceid/siwe-parser';

import { detectSIWE, isValidSIWEOrigin } from './siwe';

const mockedParsedMessage = {
domain: 'example.eth',
address: '0x0000000',
};

jest.mock('@spruceid/siwe-parser');
const siweMessage =
'example.com wants you to sign in with your Ethereum account:\n0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\n\n\nURI: https://example.com/login\nVersion: 1\nChain ID: 1\nNonce: 32891756\nIssued At: 2021-09-30T16:25:24Z';
const parsedMessage = new ParsedMessage(siweMessage);

describe('siwe', () => {
describe('detectSIWE', () => {
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parsedMessageMock = ParsedMessage as any;
const textAsHex = (string: string) => {
return Buffer.from(string, 'utf8').toString('hex');
};

it('returns an object with isSIWEMessage set to true and parsedMessage', () => {
parsedMessageMock.mockReturnValue(mockedParsedMessage);
const result = detectSIWE({ data: '0xVALIDDATA' });
const result = detectSIWE({ data: textAsHex(siweMessage) });
expect(result.isSIWEMessage).toBe(true);
expect(result.parsedMessage).toStrictEqual(parsedMessage);
});

it('returns an object with isSIWEMessage set to true and parsedMessage when scheme is provided', () => {
const messageWithScheme = `https://${siweMessage}`;
const parsedMessageWithScheme = new ParsedMessage(messageWithScheme);
const result = detectSIWE({ data: textAsHex(messageWithScheme) });

expect(result.isSIWEMessage).toBe(true);
expect(result.parsedMessage).toBe(mockedParsedMessage);
expect(result.parsedMessage).toStrictEqual(parsedMessageWithScheme);
});

it('returns an object with isSIWEMessage set to false and parsedMessage set to null', () => {
parsedMessageMock.mockImplementation(() => {
throw new Error('Invalid SIWE message');
});
const result = detectSIWE({ data: '0xINVALIDDATA' });
expect(result.isSIWEMessage).toBe(false);
expect(result.parsedMessage).toBeNull();
});
});

describe('isValidSIWEOrigin', () => {
const msg = {
domain: 'example.com',
address: '0x0',
statement: '',
uri: 'https://example.com',
version: '1',
chainId: 1,
nonce: '',
issuedAt: '',
expirationTime: null,
notBefore: null,
requestId: 'foo',
resources: [],
};
const checks = [
{
name: 'identical domain',
Expand Down Expand Up @@ -251,12 +240,12 @@ describe('siwe', () => {
origin,
})}`, () => {
const result = isValidSIWEOrigin({
from: '0x0',
from: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
origin,
siwe: {
isSIWEMessage: true,
parsedMessage: {
...msg,
...parsedMessage,
domain,
},
},
Expand Down
31 changes: 24 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1901,7 +1901,7 @@ __metadata:
"@metamask/eth-query": ^4.0.0
"@metamask/ethjs-unit": ^0.3.0
"@metamask/utils": ^8.3.0
"@spruceid/siwe-parser": 1.1.3
"@spruceid/siwe-parser": 2.1.0
"@types/bn.js": ^5.1.5
"@types/jest": ^27.4.1
bn.js: ^5.2.1
Expand Down Expand Up @@ -3167,13 +3167,20 @@ __metadata:
languageName: node
linkType: hard

"@noble/hashes@npm:1.3.3, @noble/hashes@npm:^1.0.0, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.3.2, @noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1, @noble/hashes@npm:~1.3.2":
"@noble/hashes@npm:1.3.3, @noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1, @noble/hashes@npm:~1.3.2":
version: 1.3.3
resolution: "@noble/hashes@npm:1.3.3"
checksum: 8a6496d1c0c64797339bc694ad06cdfaa0f9e56cd0c3f68ae3666cfb153a791a55deb0af9c653c7ed2db64d537aa3e3054629740d2f2338bb1dcb7ab60cd205b
languageName: node
linkType: hard

"@noble/hashes@npm:^1.0.0, @noble/hashes@npm:^1.1.2, @noble/hashes@npm:^1.3.1, @noble/hashes@npm:^1.3.2":
version: 1.4.0
resolution: "@noble/hashes@npm:1.4.0"
checksum: 8ba816ae26c90764b8c42493eea383716396096c5f7ba6bea559993194f49d80a73c081f315f4c367e51bd2d5891700bcdfa816b421d24ab45b41cb03e4f3342
languageName: node
linkType: hard

"@noble/secp256k1@npm:^1.5.5":
version: 1.7.1
resolution: "@noble/secp256k1@npm:1.7.1"
Expand Down Expand Up @@ -3482,12 +3489,15 @@ __metadata:
languageName: node
linkType: hard

"@spruceid/siwe-parser@npm:1.1.3":
version: 1.1.3
resolution: "@spruceid/siwe-parser@npm:1.1.3"
"@spruceid/siwe-parser@npm:2.1.0":
version: 2.1.0
resolution: "@spruceid/siwe-parser@npm:2.1.0"
dependencies:
"@noble/hashes": ^1.1.2
apg-js: ^4.1.1
checksum: 708786ba2f10987c45c1fd8a6243ba6572ee7f320531616d71ff66044828bc24af66f5537ce09c9272bdae93fcc35b566a7804fe7997284f2ee5445a36e6add2
uri-js: ^4.4.1
valid-url: ^1.0.9
checksum: 99365956bd5e35127568e7ee69246cfc79cc26d83f6fbc5e3a9ed6f0693f7da6f2ee67cf8b93b65761da3c3ce8cc156858bab85e24b2eadd49ec8ae07cb8826e
languageName: node
linkType: hard

Expand Down Expand Up @@ -11670,7 +11680,7 @@ __metadata:
languageName: node
linkType: hard

"uri-js@npm:^4.2.2":
"uri-js@npm:^4.2.2, uri-js@npm:^4.4.1":
version: 4.4.1
resolution: "uri-js@npm:4.4.1"
dependencies:
Expand Down Expand Up @@ -11752,6 +11762,13 @@ __metadata:
languageName: node
linkType: hard

"valid-url@npm:^1.0.9":
version: 1.0.9
resolution: "valid-url@npm:1.0.9"
checksum: 3ecb030559404441c2cf104cbabab8770efb0f36d117db03d1081052ef133015a68806148ce954bb4dd0b5c42c14b709a88783c93d66b0916cb67ba771c98702
languageName: node
linkType: hard

"validate-npm-package-license@npm:^3.0.4":
version: 3.0.4
resolution: "validate-npm-package-license@npm:3.0.4"
Expand Down

0 comments on commit 498e748

Please sign in to comment.