-
-
Notifications
You must be signed in to change notification settings - Fork 366
Feat/add type signed data v4 with salt #400
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
Open
dawnseeker8
wants to merge
5
commits into
main
Choose a base branch
from
feat/add-type-signed-data-v4-with-salt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
95ee834
feat: add sign typed data v4 with salt tests
dawnseeker8 462dc6b
feat: modify the tests to make error occur for old version.
dawnseeker8 70bf261
feat: fix the verification of sign typed data v4 with salt.
dawnseeker8 f841a9c
fix: fix lint error.
dawnseeker8 324daba
Merge branch 'main' into feat/add-type-signed-data-v4-with-salt
dawnseeker8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletions
251
src/components/signatures/signTypedDataV4-sign-with-salt.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| import { recoverTypedSignature } from '@metamask/eth-sig-util'; | ||
| import { toChecksumAddress } from 'ethereumjs-util'; | ||
| import globalContext from '../..'; | ||
|
|
||
| export function signTypedDataV4WithSaltComponent(parentContainer) { | ||
| parentContainer.insertAdjacentHTML( | ||
| 'beforeend', | ||
| `<div class="col-xl-4 col-lg-6 col-md-12 col-sm-12 col-12 d-flex align-items-stretch"> | ||
| <div class="card full-width"> | ||
| <div class="card-body"> | ||
| <h4> | ||
| Sign Typed Data V4 with salt | ||
| </h4> | ||
|
|
||
| <button | ||
| class="btn btn-primary btn-lg btn-block mb-3" | ||
| id="signTypedDataV4WithSalt" | ||
| disabled | ||
| > | ||
| Sign | ||
| </button> | ||
|
|
||
| <p class="info-text alert alert-warning"> | ||
| Result: | ||
| <span id="signTypedDataV4WithSaltResult"></span> | ||
| </p> | ||
|
|
||
| <button | ||
| class="btn btn-primary btn-lg btn-block mb-3" | ||
| id="signTypedDataV4WithSaltVerify" | ||
| disabled | ||
| > | ||
| Verify | ||
| </button> | ||
|
|
||
| <p class="info-text alert alert-warning"> | ||
| Recovery result: | ||
| <span id="signTypedDataV4WithSaltVerifyResult"></span> | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </div>`, | ||
| ); | ||
|
|
||
| const signTypedDataV4WithSalt = document.getElementById( | ||
| 'signTypedDataV4WithSalt', | ||
| ); | ||
| const signTypedDataV4WithSaltResult = document.getElementById( | ||
| 'signTypedDataV4WithSaltResult', | ||
| ); | ||
| const signTypedDataV4WithSaltVerify = document.getElementById( | ||
| 'signTypedDataV4WithSaltVerify', | ||
| ); | ||
| const signTypedDataV4WithSaltVerifyResult = document.getElementById( | ||
| 'signTypedDataV4WithSaltVerifyResult', | ||
| ); | ||
|
|
||
| document.addEventListener('globalConnectionChange', function (e) { | ||
| if (e.detail.connected) { | ||
| // MetaMask is connected, enable the button | ||
| signTypedDataV4WithSalt.disabled = false; | ||
| } | ||
| }); | ||
|
|
||
| document.addEventListener('disableAndClear', function () { | ||
| signTypedDataV4WithSalt.disabled = true; | ||
| signTypedDataV4WithSaltVerify.disabled = true; | ||
| }); | ||
|
|
||
| /** | ||
| * Sign Typed Data V4 | ||
| */ | ||
| signTypedDataV4WithSalt.onclick = async () => { | ||
| const msgParams = { | ||
| domain: { | ||
| chainId: globalContext.chainIdInt.toString(), | ||
| name: 'Ether Mail', | ||
| verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', | ||
| version: '1', | ||
| salt: 'test', | ||
|
||
| }, | ||
| message: { | ||
| contents: 'Hello, Bob!', | ||
| from: { | ||
| name: 'Cow', | ||
| wallets: [ | ||
| '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', | ||
| '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF', | ||
| ], | ||
| }, | ||
| to: [ | ||
| { | ||
| name: 'Bob', | ||
| wallets: [ | ||
| '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', | ||
| '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', | ||
| '0xB0B0b0b0b0b0B000000000000000000000000000', | ||
| ], | ||
| }, | ||
| ], | ||
| attachment: '0x', | ||
| }, | ||
| primaryType: 'Mail', | ||
| types: { | ||
| EIP712Domain: [ | ||
| { | ||
| name: 'name', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'version', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'chainId', | ||
| type: 'uint256', | ||
| }, | ||
| { | ||
| name: 'verifyingContract', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'salt', | ||
| type: 'string', | ||
| }, | ||
| ], | ||
| Group: [ | ||
| { name: 'name', type: 'string' }, | ||
| { name: 'members', type: 'Person[]' }, | ||
| ], | ||
| Mail: [ | ||
| { name: 'from', type: 'Person' }, | ||
| { name: 'to', type: 'Person[]' }, | ||
| { name: 'contents', type: 'string' }, | ||
| { name: 'attachment', type: 'bytes' }, | ||
| ], | ||
| Person: [ | ||
| { name: 'name', type: 'string' }, | ||
| { name: 'wallets', type: 'address[]' }, | ||
| ], | ||
| }, | ||
| }; | ||
| try { | ||
| const from = globalContext.accounts[0]; | ||
| const sign = await globalContext.provider.request({ | ||
| method: 'eth_signTypedData_v4', | ||
| params: [from, JSON.stringify(msgParams)], | ||
| }); | ||
| signTypedDataV4WithSaltResult.innerHTML = sign; | ||
| signTypedDataV4WithSaltVerify.disabled = false; | ||
| } catch (err) { | ||
| console.error(err); | ||
| signTypedDataV4WithSaltResult.innerHTML = `Error: ${err.message}`; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Sign Typed Data V4 Verification | ||
| */ | ||
| signTypedDataV4WithSaltVerify.onclick = async () => { | ||
| const msgParams = { | ||
| domain: { | ||
| chainId: globalContext.chainIdInt, | ||
| name: 'Ether Mail', | ||
| verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', | ||
| version: '1', | ||
| salt: 'test', | ||
| }, | ||
| message: { | ||
| contents: 'Hello, Bob!', | ||
| from: { | ||
| name: 'Cow', | ||
| wallets: [ | ||
| '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', | ||
| '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF', | ||
| ], | ||
| }, | ||
| to: [ | ||
| { | ||
| name: 'Bob', | ||
| wallets: [ | ||
| '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', | ||
| '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', | ||
| '0xB0B0b0b0b0b0B000000000000000000000000000', | ||
| ], | ||
| }, | ||
| ], | ||
| attachment: '0x', | ||
| }, | ||
| primaryType: 'Mail', | ||
| types: { | ||
| EIP712Domain: [ | ||
| { | ||
| name: 'name', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'version', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'chainId', | ||
| type: 'uint256', | ||
| }, | ||
| { | ||
| name: 'verifyingContract', | ||
| type: 'string', | ||
| }, | ||
| { | ||
| name: 'salt', | ||
| type: 'string', | ||
| }, | ||
| ], | ||
| Group: [ | ||
| { name: 'name', type: 'string' }, | ||
| { name: 'members', type: 'Person[]' }, | ||
| ], | ||
| Mail: [ | ||
| { name: 'from', type: 'Person' }, | ||
| { name: 'to', type: 'Person[]' }, | ||
| { name: 'contents', type: 'string' }, | ||
| { name: 'attachment', type: 'bytes' }, | ||
| ], | ||
| Person: [ | ||
| { name: 'name', type: 'string' }, | ||
| { name: 'wallets', type: 'address[]' }, | ||
| ], | ||
| }, | ||
| }; | ||
| try { | ||
| const from = globalContext.accounts[0]; | ||
| const sign = signTypedDataV4WithSaltResult.innerHTML; | ||
| const recoveredAddr = recoverTypedSignature({ | ||
| data: msgParams, | ||
| signature: sign, | ||
| version: 'V4', | ||
| }); | ||
| if (toChecksumAddress(recoveredAddr) === toChecksumAddress(from)) { | ||
| console.log(`Successfully verified signer as ${recoveredAddr}`); | ||
| signTypedDataV4WithSaltVerifyResult.innerHTML = recoveredAddr; | ||
| } else { | ||
| console.log( | ||
| `Failed to verify signer when comparing ${recoveredAddr} to ${from}`, | ||
| ); | ||
| } | ||
| } catch (err) { | ||
| console.error(err); | ||
| signTypedDataV4WithSaltVerifyResult.innerHTML = `Error: ${err.message}`; | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The chainId is converted to a string during signing, but in verification it is used as a number (line 163). Consider using the same data type for chainId in both places to prevent potential verification issues.