depreciate e712 w/ provider signing

This commit is contained in:
gozzy 2022-11-04 22:03:56 +00:00
parent 5da31361c6
commit 85a222d93a
4 changed files with 49 additions and 110 deletions

View File

@ -1,6 +1,6 @@
const { EIP712Signer } = require('@ticket721/e712')
import { concatSig } from 'eth-sig-util'
const Permit = [
const PermitType = [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
{ name: 'value', type: 'uint256' },
@ -8,36 +8,53 @@ const Permit = [
{ name: 'deadline', type: 'uint256' }
]
class PermitSigner extends EIP712Signer {
const EIP712DomainType = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' }
]
export default class PermitSigner {
constructor(_domain, _permitArgs) {
super(_domain, ['Permit', Permit])
this.permitArgs = _permitArgs
this.domain = _domain
}
// 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)
getReqPayload() {
return {
hex,
domain: this.domain,
primaryType: 'Permit',
types: {
Permit: PermitType,
EIP712Domain: EIP712DomainType
},
message: this.permitArgs
}
}
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
}
return {
hex: concatSig(v, r, s),
v,
r: '0x' + r,
s: '0x' + s
r,
s
}
}
getSignerAddress(permitArgs, signature) {
const originalPayload = this.generatePayload(permitArgs, 'Permit')
return this.verify(originalPayload, signature)
}
}
module.exports = { PermitSigner }

View File

@ -23,7 +23,6 @@
"@apollo/client": "^3.3.20",
"@metamask/onboarding": "^1.0.0",
"@nuxtjs/moment": "^1.6.0",
"@ticket721/e712": "^0.4.1",
"@walletconnect/web3-provider": "1.7.8",
"ajv": "^6.10.2",
"arraybuffer-loader": "^1.0.8",

View File

@ -2,11 +2,11 @@
import Web3 from 'web3'
import { toBN } from 'web3-utils'
import tornABI from '../abis/ERC20.abi.json'
import PermitSigner from '../lib/Permit'
import { isWalletRejection } from '@/utils'
const { toWei, toHex, numberToHex } = require('web3-utils')
const { PermitSigner } = require('../lib/Permit')
const state = () => {
return {
@ -168,11 +168,11 @@ const actions = {
}
const permitSigner = new PermitSigner(domain, args)
const message = permitSigner.getPayload()
const req = permitSigner.getReqPayload()
const callParams = {
method: 'eth_signTypedData_v4',
params: [ethAccount, JSON.stringify(message)]
params: [ethAccount, JSON.stringify(req)]
}
dispatch(
@ -186,18 +186,10 @@ const actions = {
{ root: true }
)
let signature = await this.$provider.sendRequest(callParams)
signature = signature.substring(2)
const r = '0x' + signature.substring(0, 64)
const s = '0x' + signature.substring(64, 128)
let v = parseInt(signature.substring(128, 130), 16)
const query = await this.$provider.sendRequest(callParams)
const { v, r, s, hex } = permitSigner.getSignature(query)
// fix ledger sign
if (v === 0 || v === 1) {
v = v + 27
}
console.log('signature', v, r, s, signature)
console.log('signature', v, r, s, hex)
// signature validation on contract
await getters.tokenContract.methods

View File

@ -2109,14 +2109,6 @@
dependencies:
defer-to-connect "^1.0.1"
"@ticket721/e712@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@ticket721/e712/-/e712-0.4.1.tgz#b9be3f5d9d01a8468e0a49eeed67fe4454c87582"
integrity sha512-JHqyb2HntsmLJ4PtlXQ2AoU+s42X1itRtxeCAlA1AxOMORXQudbhly9wvGhN/Hbcl8VYpfgLJKp4hK3AT6xDiw==
dependencies:
bn.js "5.1.1"
ethers "4.0.41"
"@types/anymatch@*":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@types/anymatch/-/anymatch-1.3.1.tgz#336badc1beecb9dacc38bea2cf32adf627a8421a"
@ -4060,11 +4052,6 @@ bn.js@4.11.8, bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
integrity sha512-ItfYfPLkWHUjckQCk8xC+LwxgK8NYcXywGigJgSwOP8Y2iyWT4f2vsZnoOXTTbo+o5yXmIUJ4gn5538SO5S3gA==
bn.js@5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.1.1.tgz#48efc4031a9c4041b9c99c6941d903463ab62eb5"
integrity sha512-IUTD/REb78Z2eodka1QZyyEk66pciRcP6Sroka0aI3tG/iwIdYLrBD62RsubR7vqdt3WyX8p4jxeatzmRSphtA==
bn.js@^4.11.0, bn.js@^4.11.6, bn.js@^4.11.8, bn.js@^4.11.9:
version "4.12.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
@ -6093,19 +6080,6 @@ electron-to-chromium@^1.4.84:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.86.tgz#90fe4a9787f48d6522957213408e08a8126b2ebc"
integrity sha512-EVTZ+igi8x63pK4bPuA95PXIs2b2Cowi3WQwI9f9qManLiZJOD1Lash1J3W4TvvcUCcIR4o/rgi9o8UicXSO+w==
elliptic@6.5.2:
version "6.5.2"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.2.tgz#05c5678d7173c049d8ca433552224a495d0e3762"
integrity sha512-f4x70okzZbIQl/NSRLkI/+tteV/9WqL98zx+SQ69KbXxmVrmjwsNUPn/gYJJ0sHvEak24cZgHIPegRePAtA/xw==
dependencies:
bn.js "^4.4.0"
brorand "^1.0.1"
hash.js "^1.0.0"
hmac-drbg "^1.0.0"
inherits "^2.0.1"
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.0"
elliptic@6.5.4, elliptic@^6.4.0, elliptic@^6.5.2, elliptic@^6.5.4:
version "6.5.4"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
@ -6987,21 +6961,6 @@ ethereumjs-vm@^2.3.4:
rustbn.js "~0.2.0"
safe-buffer "^5.1.1"
ethers@4.0.41:
version "4.0.41"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-4.0.41.tgz#a0cff526f08c2e08c525cf82ef4483f6333b8000"
integrity sha512-QpW2CPZajquwiA7rVDozMksOuvdUBKIruamAakt0++EKBB/VWtLB9zSRZDInLDpp9fZYgOT/0trPD38r6CzLIg==
dependencies:
aes-js "3.0.0"
bn.js "^4.4.0"
elliptic "6.5.2"
hash.js "1.1.3"
js-sha3 "0.5.7"
scrypt-js "2.0.4"
setimmediate "1.0.4"
uuid "2.0.1"
xmlhttprequest "1.8.0"
ethers@^5.5.1:
version "5.6.1"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.6.1.tgz#a56cd67f1595b745dc3dde0ccf2b5de53a41a6d0"
@ -8306,14 +8265,6 @@ hash-sum@^2.0.0:
resolved "https://registry.yarnpkg.com/hash-sum/-/hash-sum-2.0.0.tgz#81d01bb5de8ea4a214ad5d6ead1b523460b0b45a"
integrity sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==
hash.js@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.3.tgz#340dedbe6290187151c1ea1d777a3448935df846"
integrity sha512-/UETyP0W22QILqS+6HowevwhEFJ3MBJnwTf75Qob9Wz9t0DPuisL8kW8YZMK62dHAKE1c1p+gY1TtOLY+USEHA==
dependencies:
inherits "^2.0.3"
minimalistic-assert "^1.0.0"
hash.js@1.1.7, hash.js@^1.0.0, hash.js@^1.0.3, hash.js@^1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42"
@ -9733,16 +9684,16 @@ js-beautify@^1.6.14:
mkdirp "~0.5.1"
nopt "~4.0.1"
js-sha3@0.5.7, js-sha3@^0.5.7:
version "0.5.7"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.5.7.tgz#0d4ffd8002d5333aabaf4a23eed2f6374c9f28e7"
integrity sha1-DU/9gALVMzqrr0oj7tL2N0yfKOc=
js-sha3@0.8.0, js-sha3@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.8.0.tgz#b9b7a5da73afad7dedd0f8c463954cbde6818840"
integrity sha512-gF1cRrHhIzNfToc802P800N8PpXS+evLLXfsVpowqmAFR9uwbi89WvXg2QspOmXL8QL86J4T1EpFu+yUkwJY3Q==
js-sha3@^0.5.7:
version "0.5.7"
resolved "https://registry.yarnpkg.com/js-sha3/-/js-sha3-0.5.7.tgz#0d4ffd8002d5333aabaf4a23eed2f6374c9f28e7"
integrity sha1-DU/9gALVMzqrr0oj7tL2N0yfKOc=
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
@ -13826,11 +13777,6 @@ schema-utils@^3.0.0:
ajv "^6.12.5"
ajv-keywords "^3.5.2"
scrypt-js@2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-2.0.4.tgz#32f8c5149f0797672e551c07e230f834b6af5f16"
integrity sha512-4KsaGcPnuhtCZQCxFxN3GVYIhKFPTdLd8PLC552XwbMndtD0cjRFAhDuuydXQ0h08ZfPgzqe6EKHozpuH74iDw==
scrypt-js@3.0.1, scrypt-js@^3.0.0, scrypt-js@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-3.0.1.tgz#d314a57c2aef69d1ad98a138a21fe9eafa9ee312"
@ -14016,11 +13962,6 @@ set-value@^2.0.0, set-value@^2.0.1:
is-plain-object "^2.0.3"
split-string "^3.0.1"
setimmediate@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.4.tgz#20e81de622d4a02588ce0c8da8973cbcf1d3138f"
integrity sha1-IOgd5iLUoCWIzgyNqJc8vPHTE48=
setimmediate@^1.0.4, setimmediate@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
@ -15408,11 +15349,6 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
uuid@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.1.tgz#c2a30dedb3e535d72ccf82e343941a50ba8533ac"
integrity sha1-wqMN7bPlNdcsz4LjQ5QaULqFM6w=
uuid@3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131"
@ -16589,11 +16525,6 @@ xmldom@^0.1.22:
resolved "https://registry.yarnpkg.com/xmldom/-/xmldom-0.1.31.tgz#b76c9a1bd9f0a9737e5a72dc37231cf38375e2ff"
integrity sha512-yS2uJflVQs6n+CyjHoaBmVSqIDevTAWrzMmjG1Gc7h1qQ7uVozNhEPJAwZXWyGQ/Gafo3fCwrcaokezLPupVyQ==
xmlhttprequest@1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz#67fe075c5c24fef39f9d65f5f7b7fe75171968fc"
integrity sha1-Z/4HXFwk/vOfnWX197f+dRcZaPw=
xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.0, xtend@~4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"