Skip to content

Instantly share code, notes, and snippets.

@ptsayli
Created November 27, 2019 13:28
Show Gist options
  • Save ptsayli/49a9e02f97121adf3450c8da1d6d4301 to your computer and use it in GitHub Desktop.
Save ptsayli/49a9e02f97121adf3450c8da1d6d4301 to your computer and use it in GitHub Desktop.
Test eth_signTypedData
import signUtil from "eth-sig-util"
const getLoginTypedData = ({ address, timestamp, primaryType }) => ({
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'host', type: 'string' },
{ name: 'version', type: 'string' },
],
WebLogin: [
{ name: 'address', type: 'address' },
{ name: 'timestamp', type: 'uint256' },
],
},
domain: {
name: 'Wallet web',
host: 'https://wallet.matic.network',
version: '1',
},
primaryType,
message: {
address,
timestamp,
},
})
// Sign Message
const signedMsg = sigUtil.signTypedData(privKeybuffer, {
data: getLoginTypedData({ address, timestamp, primaryType: 'WebLogin' }),
})
// recover from signedMessage
const recovered = signUtil.recoverTypedSignature({
data: getLoginTypedData(address, timestamp),
sig: signedMessage
})
if (recovered && recovered === address) {
console.log("valid")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment