classic-ui/lib/Permit.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-11-05 01:03:56 +03:00
import { concatSig } from 'eth-sig-util'
2022-04-22 06:05:56 +03:00
2022-11-05 01:03:56 +03:00
const PermitType = [
2022-04-22 06:05:56 +03:00
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
{ name: 'nonce', type: 'uint256' },
{ name: 'deadline', type: 'uint256' }
]
2022-11-05 01:03:56 +03: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-22 06:05:56 +03:00
constructor(_domain, _permitArgs) {
this.permitArgs = _permitArgs
2022-11-05 01:03:56 +03:00
this.domain = _domain
2022-04-22 06:05:56 +03:00
}
setPermitInfo(_permitArgs) {
this.permitArgs = _permitArgs
}
2022-11-05 01:03:56 +03:00
getReqPayload() {
return {
domain: this.domain,
primaryType: 'Permit',
types: {
Permit: PermitType,
EIP712Domain: EIP712DomainType
},
message: this.permitArgs
}
2022-04-22 06:05:56 +03:00
}
2022-11-05 01:03:56 +03: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-22 06:05:56 +03:00
return {
2022-11-05 01:03:56 +03:00
hex: concatSig(v, r, s),
2022-04-22 06:05:56 +03:00
v,
2022-11-05 01:03:56 +03:00
r,
s
2022-04-22 06:05:56 +03:00
}
}
}