classic-ui/lib/Permit.js
Danil Kovtonyuk 44f31f8b9f
init
2022-04-22 13:14:19 +10:00

44 lines
1.1 KiB
JavaScript

const { EIP712Signer } = require('@ticket721/e712')
const Permit = [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' }
]
class PermitSigner extends EIP712Signer {
constructor(_domain, _permitArgs) {
super(_domain, ['Permit', Permit])
this.permitArgs = _permitArgs
}
// Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)
setPermitInfo(_permitArgs) {
this.permitArgs = _permitArgs
}
getPayload() {
return this.generatePayload(this.permitArgs, 'Permit')
}
async getSignature(privateKey) {
const payload = this.getPayload()
const { hex, v, r, s } = await this.sign(privateKey, payload)
return {
hex,
v,
r: '0x' + r,
s: '0x' + s
}
}
getSignerAddress(permitArgs, signature) {
const originalPayload = this.generatePayload(permitArgs, 'Permit')
return this.verify(originalPayload, signature)
}
}
module.exports = { PermitSigner }