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 verifyContract address normalization #309

Merged
merged 12 commits into from
Jun 26, 2024

Conversation

OGPoyraz
Copy link
Member

@OGPoyraz OGPoyraz commented Jun 7, 2024

Explanation

This PR aims to add address normalization for EIP712Domain.verifyContract

References

Please see https://github.com/MetaMask/MetaMask-planning/issues/2229

Blocked by

@OGPoyraz OGPoyraz requested a review from a team as a code owner June 7, 2024 13:20
Copy link

socket-security bot commented Jun 7, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/bn.js@5.1.5 None 0 13.8 kB types
npm/bn.js@5.2.1 None 0 99 kB fanatid

View full report↗︎

src/wallet.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.ts Outdated Show resolved Hide resolved
src/utils/normalizer.test.ts Outdated Show resolved Hide resolved
src/utils/normalizer.test.ts Outdated Show resolved Hide resolved
src/utils/normalizer.test.ts Outdated Show resolved Hide resolved
@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

digiwand
digiwand previously approved these changes Jun 14, 2024
src/wallet.ts Show resolved Hide resolved
try {
data = parseTypedMessage(
messageData,
) as unknown as SignTypedMessageDataV3V4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using a type assertion it's good to know why it's necessary. After looking at parseTypedMessage I can see it's because of the JSON.parse. What are your thoughts on putting this type assertion in that function instead so it's more associated with the JSON.parse visually?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That make sense @mcmire, fixed in 2ebe685

* @returns The data object for EIP712 normalization.
*/
function parseTypedMessage(data: string) {
if (typeof data !== 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in af2a8c2

Comment on lines 69 to 70
const addressHex = address as Hex;
if (isValidHexAddress(addressHex)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that we need to use a type assertion because isValidHexAddress takes a Hex. However, we should be able to avoid the type assertion by using isStrictHexString, which narrows its argument to a Hex if given a hex string:

Suggested change
const addressHex = address as Hex;
if (isValidHexAddress(addressHex)) {
if (isStrictHexString(address) && isValidHexAddress(address)) {

That means we shouldn't need to use addressHex below, or upcast it to a string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, done in a8e8e71

}

// Check if the address is in decimal format, convert to hexadecimal
const parsedAddress = parseInt(addressHex, 10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to be worried that this will be a large number? I see that you use BN below, but you seem to only use it for conversion (and then, only for decimal -> hex and not octal -> hex).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ba7ec73

try {
return JSON.parse(data);
} catch (e) {
throw new Error(`Invalid message data for normalization. data: ${data}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there value in throwing a custom error here since we catch it above anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 4baeb92

src/utils/normalize.ts Show resolved Hide resolved
signatureMethod: 'eth_signTypedData_v3',
});
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] could include tests for:

  • octal addresses
  • inputs that are not parsable

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above (#309 (comment)), these test cases are already covered by the normalizer unit tests. I added this single eth_signTypedData_v3 test because it was missing from the wallet tests. Is there any advantage to adding essentially the same tests here as well?

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this, looks good to me.

@OGPoyraz OGPoyraz merged commit 0a1d2ae into main Jun 26, 2024
19 checks passed
@OGPoyraz OGPoyraz deleted the fix/add-verifycontract-address-normalization branch June 26, 2024 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants