From 9e30867927aba26d96e1838a0b08582f9771c097 Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Thu, 18 Jul 2024 18:07:50 +0300 Subject: [PATCH] refactor: Permit code (#351) * refactor: use 1 const EIP712Domain * refactor: permitMsgParamsDomain * refactor: add getPermitMsgParams * refactor: mv splitSig move above so no need to recreate fn each time + easier to reuse * refactor: mv Permit const --- src/index.js | 197 +++++++++++++++------------------------------------ 1 file changed, 57 insertions(+), 140 deletions(-) diff --git a/src/index.js b/src/index.js index cc745803..84fc4ea6 100644 --- a/src/index.js +++ b/src/index.js @@ -2312,12 +2312,7 @@ const initializeFormElements = () => { signTypedDataV3.onclick = async () => { const msgParams = { types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' }, @@ -2367,12 +2362,7 @@ const initializeFormElements = () => { signTypedDataV3Verify.onclick = async () => { const msgParams = { types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' }, @@ -2458,12 +2448,7 @@ const initializeFormElements = () => { }, primaryType: 'Mail', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Group: [ { name: 'name', type: 'string' }, { name: 'members', type: 'Person[]' }, @@ -2528,12 +2513,7 @@ const initializeFormElements = () => { }, primaryType: 'Mail', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Group: [ { name: 'name', type: 'string' }, { name: 'members', type: 'Person[]' }, @@ -2575,23 +2555,31 @@ const initializeFormElements = () => { /** * Sign Permit */ - signPermit.onclick = async () => { + const EIP712Domain = [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' }, + { name: 'chainId', type: 'uint256' }, + { name: 'verifyingContract', type: 'address' }, + ]; + + const Permit = [ + { name: 'owner', type: 'address' }, + { name: 'spender', type: 'address' }, + { name: 'value', type: 'uint256' }, + { name: 'nonce', type: 'uint256' }, + { name: 'deadline', type: 'uint256' }, + ]; + + const permitMsgParamsDomain = { + name: 'MyToken', + version: '1', + verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', + chainId: chainIdInt, + }; + + function getPermitMsgParams() { const from = accounts[0]; - const domain = { - name: 'MyToken', - version: '1', - verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', - chainId: chainIdInt, - }; - - const EIP712Domain = [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ]; - const permit = { owner: from, spender: '0x5B38Da6a701c568545dCfcB03FcB875f56beddC4', @@ -2600,41 +2588,38 @@ const initializeFormElements = () => { deadline: 50000000000, }; - const Permit = [ - { name: 'owner', type: 'address' }, - { name: 'spender', type: 'address' }, - { name: 'value', type: 'uint256' }, - { name: 'nonce', type: 'uint256' }, - { name: 'deadline', type: 'uint256' }, - ]; + return { + types: { + EIP712Domain, + Permit, + }, + primaryType: 'Permit', + domain: permitMsgParamsDomain, + message: permit, + }; + } - const splitSig = (sig) => { - const pureSig = sig.replace('0x', ''); + function splitSig(sig) { + const pureSig = sig.replace('0x', ''); - const _r = Buffer.from(pureSig.substring(0, 64), 'hex'); - const _s = Buffer.from(pureSig.substring(64, 128), 'hex'); - const _v = Buffer.from( - parseInt(pureSig.substring(128, 130), 16).toString(), - ); + const _r = Buffer.from(pureSig.substring(0, 64), 'hex'); + const _s = Buffer.from(pureSig.substring(64, 128), 'hex'); + const _v = Buffer.from( + parseInt(pureSig.substring(128, 130), 16).toString(), + ); - return { _r, _s, _v }; - }; + return { _r, _s, _v }; + } + + signPermit.onclick = async () => { + const from = accounts[0]; + const msgParams = getPermitMsgParams(); let sign; let r; let s; let v; - const msgParams = { - types: { - EIP712Domain, - Permit, - }, - primaryType: 'Permit', - domain, - message: permit, - }; - try { sign = await provider.request({ method: 'eth_signTypedData_v4', @@ -2661,46 +2646,8 @@ const initializeFormElements = () => { */ signPermitVerify.onclick = async () => { const from = accounts[0]; + const msgParams = getPermitMsgParams(); - const domain = { - name: 'MyToken', - version: '1', - verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', - chainId: chainIdInt, - }; - - const EIP712Domain = [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ]; - - const permit = { - owner: from, - spender: '0x5B38Da6a701c568545dCfcB03FcB875f56beddC4', - value: 3000, - nonce: 0, - deadline: 50000000000, - }; - - const Permit = [ - { name: 'owner', type: 'address' }, - { name: 'spender', type: 'address' }, - { name: 'value', type: 'uint256' }, - { name: 'nonce', type: 'uint256' }, - { name: 'deadline', type: 'uint256' }, - ]; - - const msgParams = { - types: { - EIP712Domain, - Permit, - }, - primaryType: 'Permit', - domain, - message: permit, - }; try { const sign = signPermitResult.innerHTML; const recoveredAddr = recoverTypedSignature({ @@ -2735,12 +2682,7 @@ const initializeFormElements = () => { verifyingContract: '0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC', }, types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, OrderComponents: [ { name: 'consideration', type: 'ConsiderationItem[+' }, ], @@ -2800,12 +2742,7 @@ const initializeFormElements = () => { }, primaryType: 'Mail', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Group: [ { name: 'name', type: 'string' }, { name: 'members', type: 'Person[]' }, @@ -2852,12 +2789,7 @@ const initializeFormElements = () => { }, primaryType: 'Wallet', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Wallet: [{ name: 'name', type: 'string' }], }, }; @@ -2890,12 +2822,7 @@ const initializeFormElements = () => { }, primaryType: 'Non-Existent', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Wallet: [{ name: 'name', type: 'string' }], }, }; @@ -2944,12 +2871,7 @@ const initializeFormElements = () => { ], }, types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Group: [ { name: 'name', type: 'string' }, { name: 'members', type: 'Person[]' }, @@ -2993,12 +2915,7 @@ const initializeFormElements = () => { }, primaryType: 'Wallet', types: { - EIP712Domain: [ - { name: 'name', type: 'string' }, - { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' }, - { name: 'verifyingContract', type: 'address' }, - ], + EIP712Domain, Wallet: [{ name: 'name', type: 'string' }], }, };