Last active
October 22, 2020 10:33
-
-
Save Ramarti/50caafb9849af3414d6b712183896523 to your computer and use it in GitHub Desktop.
EIP712 for DAI style permit()
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
async preparePermitDataStruct (holder, allowed) { | |
const netId = await this.web3.eth.net.getId() | |
let nonce = await this.stableTokenContract.methods.nonces(holder).call({ | |
from: holder | |
}) | |
const expiration = Math.floor(Date.now() / 1000) + 900000 | |
const EIP712Domain = [ | |
{ | |
name: 'name', | |
type: 'string' | |
}, | |
{ | |
name: 'version', | |
type: 'string' | |
}, | |
{ | |
name: 'chainId', | |
type: 'uint256' | |
}, | |
{ | |
name: 'verifyingContract', | |
type: 'address' | |
} | |
] | |
const Permit = [ | |
{ | |
name: 'holder', | |
type: 'address' | |
}, | |
{ | |
name: 'spender', | |
type: 'address' | |
}, | |
{ | |
name: 'nonce', | |
type: 'uint256' | |
}, | |
{ | |
name: 'expiry', | |
type: 'uint256' | |
}, | |
{ | |
name: 'allowed', | |
type: 'bool' | |
} | |
] | |
const domain = { | |
name: process.env.VUE_APP_TOKEN_NAME, | |
version: '1', | |
chainId: parseInt(netId), | |
verifyingContract: verifyingContract | |
} | |
var message = { | |
holder: holder, | |
spender: depositManagerAddress, | |
nonce, | |
expiry: expiration, | |
allowed: allowed | |
} | |
const data = { | |
types: { | |
EIP712Domain, | |
Permit | |
}, | |
domain, | |
primaryType: 'Permit', | |
message | |
} | |
return Promise.resolve(data) | |
} | |
} |
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
// npm install eth-sig-util --save-dev | |
const sigUtil = require('eth-sig-util') | |
let px = '0x123123...' | |
let privateKey = Buffer.from(pk.slice(2), 'hex') | |
let msgParams = { | |
data: typedData | |
} | |
try { | |
const signature = sigUtil.signTypedData(privateKey, msgParams) | |
resolve(signature) | |
} catch (e) { | |
console.log(e) | |
reject(e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment