Created
December 16, 2021 17:59
-
-
Save alisinabh/96bd50fd2f9bf827544585cee40e2f7c to your computer and use it in GitHub Desktop.
This file contains 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
const { ethers } = require("ethers"); | |
const sigUtil = require('@metamask/eth-sig-util'); | |
// The wallet to sign with | |
const hdWallet = ethers.utils.HDNode.fromMnemonic('all all all all all all all all all all all all').derivePath(ethers.utils.defaultPath) | |
const privateKey = Buffer.from(hdWallet.privateKey.substr(2), 'hex') | |
msgParams = { | |
domain: { | |
// Defining the chain aka Rinkeby testnet or Ethereum Main Net | |
chainId: 1, | |
// Give a user friendly name to the specific contract you are signing for. | |
name: 'Ether Mail', | |
// If name isn't enough add verifying contract to make sure you are establishing contracts with the proper entity | |
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', | |
// Just let's you know the latest version. Definitely make sure the field name is correct. | |
version: '1', | |
}, | |
// Defining the message signing data content. | |
message: { | |
/* | |
- Anything you want. Just a JSON Blob that encodes the data you want to send | |
- No required fields | |
- This is DApp Specific | |
- Be as explicit as possible when building out the message schema. | |
*/ | |
contents: 'Hello, Bob!', | |
attachedMoneyInEth: 4.2, | |
from: { | |
name: 'Cow', | |
wallets: [ | |
'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', | |
'0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF', | |
], | |
}, | |
to: [ | |
{ | |
name: 'Bob', | |
wallets: [ | |
'0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', | |
'0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', | |
'0xB0B0b0b0b0b0B000000000000000000000000000', | |
], | |
}, | |
], | |
}, | |
// Refers to the keys of the *types* object below. | |
primaryType: 'Mail', | |
types: { | |
// TODO: Clarify if EIP712Domain refers to the domain the contract is hosted on | |
EIP712Domain: [ | |
{ name: 'name', type: 'string' }, | |
{ name: 'version', type: 'string' }, | |
{ name: 'chainId', type: 'uint256' }, | |
{ name: 'verifyingContract', type: 'address' }, | |
], | |
// Not an EIP712Domain definition | |
Group: [ | |
{ name: 'name', type: 'string' }, | |
{ name: 'members', type: 'Person[]' }, | |
], | |
// Refer to PrimaryType | |
Mail: [ | |
{ name: 'from', type: 'Person' }, | |
{ name: 'to', type: 'Person[]' }, | |
{ name: 'contents', type: 'string' }, | |
], | |
// Not an EIP712Domain definition | |
Person: [ | |
{ name: 'name', type: 'string' }, | |
{ name: 'wallets', type: 'address[]' }, | |
], | |
}, | |
} | |
const version = sigUtil.SignTypedDataVersion.V4 | |
async function main() { | |
console.log("Signing with", hdWallet.address) | |
console.log("Signature", await sigUtil.signTypedData({ | |
data: msgParams, | |
version: version, | |
privateKey: privateKey | |
})) | |
const { domain, types, primaryType, message } = | |
sigUtil.TypedDataUtils.sanitizeData(msgParams) | |
const domainSeparatorHex = sigUtil.TypedDataUtils.hashStruct( | |
'EIP712Domain', | |
domain, | |
types, | |
version, | |
).toString('hex'); | |
const hashStructMessageHex = sigUtil.TypedDataUtils.hashStruct( | |
primaryType, | |
message, | |
types, | |
version | |
).toString('hex'); | |
console.log("domain separator hash:", domainSeparatorHex) | |
console.log("message hash:", hashStructMessageHex) | |
} | |
main() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment