classic-ui-dev/lib/Permit.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-11-04 18:03:56 -04:00
import { concatSig } from 'eth-sig-util'
2022-04-21 23:05:56 -04:00
2022-11-04 18:03:56 -04:00
const PermitType = [
2022-04-21 23:05:56 -04:00
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' }
]
2022-11-04 18:03:56 -04:00
const EIP712DomainType = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
]
export default class PermitSigner {
2022-04-21 23:05:56 -04:00
constructor(_domain, _permitArgs) {
this.permitArgs = _permitArgs
2022-11-04 18:03:56 -04:00
this.domain = _domain
2022-04-21 23:05:56 -04:00
}
setPermitInfo(_permitArgs) {
this.permitArgs = _permitArgs
}
2022-11-04 18:03:56 -04:00
getReqPayload() {
return {
domain: this.domain,
primaryType: 'Permit',
types: {
Permit: PermitType,
EIP712Domain: EIP712DomainType
},
message: this.permitArgs
}
2022-04-21 23:05:56 -04:00
}
2022-11-04 18:03:56 -04:00
getSignature(response) {
response = response.substring(2)
const r = '0x' + response.substring(0, 64)
const s = '0x' + response.substring(64, 128)
let v = parseInt(response.substring(128, 130), 16)
// fix ledger sign
if (v === 0 || v === 1) {
v = v + 27
}
2022-04-21 23:05:56 -04:00
return {
2022-11-04 18:03:56 -04:00
hex: concatSig(v, r, s),
2022-04-21 23:05:56 -04:00
v,
2022-11-04 18:03:56 -04:00
r,
s
2022-04-21 23:05:56 -04:00
}
}
}