@cowprotocol/permit-utils 0.6.1-RC.0 → 0.7.0-RC.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -1
- package/index.js +32 -32
- package/package.json +1 -1
package/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("@ethersproject/constants"),F=require("@ethersproject/wallet"),p=require("@1inch/permit-signed-approvals-utils"),E=require("@ethersproject/abi"),x=require("@ethersproject/bignumber"),G=require("@ethersproject/contracts"),A=require("@ethersproject/address"),q="
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("@ethersproject/constants"),F=require("@ethersproject/wallet"),p=require("@1inch/permit-signed-approvals-utils"),E=require("@ethersproject/abi"),x=require("@ethersproject/bignumber"),G=require("@ethersproject/contracts"),A=require("@ethersproject/address"),q="0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe",P=new F.Wallet(q),v=55e3,h="80000",S=O.MaxUint256.toString(),K=157788e6;async function V(n,t,e,a,r,i,{version:o,type:l,name:d}){const s=d||r;if(l==="unsupported")return!1;if(!s)throw new Error(`No token name for ${a}`);const m={chainId:t,tokenName:s,tokenAddress:a,callData:i,version:o};let u;if(i.startsWith(p.EIP_2612_PERMIT_SELECTOR)?u=e.recoverPermitOwnerFromCallData({...m,callData:i.replace(p.EIP_2612_PERMIT_SELECTOR,"0x")}):i.startsWith(p.DAI_PERMIT_SELECTOR)&&(u=e.recoverDaiLikePermitOwnerFromCallData({...m,callData:i.replace(p.DAI_PERMIT_SELECTOR,"0x")})),!!u)try{return(await u).toLowerCase()===n.toLowerCase()}catch(f){return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${n} with callData ${i}`,f),!1}}async function L({eip2612Utils:n,callDataParams:t}){const[e,a,r,...i]=t;return(await n.buildPermitCallData(e,a,r,...i)).replace("0x",p.EIP_2612_PERMIT_SELECTOR)}async function R({eip2612Utils:n,callDataParams:t}){return(await n.buildDaiLikePermitCallData(...t)).replace("0x",p.DAI_PERMIT_SELECTOR)}function w(){return Math.ceil((Date.now()+K)/1e3)}function $(n){return!!n&&n.type!=="unsupported"}const B="PERMIT_TOKEN",T={};async function j(n){const t=Y(n),e=T[t];if(e)return await e;const a=W(n).catch(r=>{console.debug("[generatePermitHook] cached request failed",r)}).finally(()=>{delete T[t]});return T[t]=a,a}async function W(n){const{inputToken:t,spender:e,chainId:a,permitInfo:r,provider:i,account:o,eip2612Utils:l,nonce:d}=n,s=t.address,m=r.name||t.name;if(!$(r))throw new Error(`Trying to generate permit hook for unsupported token: ${s}`);if(!m)throw new Error(`No token name for token: ${s}`);const u=o||P.address,f=d===void 0?await l.getTokenNonce(s,u):d,c=w(),g=S,b=r.type==="eip-2612"?await L({eip2612Utils:l,callDataParams:[{owner:u,spender:e,value:g,nonce:f,deadline:c},a,m,s,r.version]}):await R({eip2612Utils:l,callDataParams:[{holder:u,spender:e,allowed:!0,value:g,nonce:f,expiry:c},a,m,s,r.version]}),H=await Q(b,u,s,i,!!o);return{target:s,callData:b,gasLimit:H,dappId:B}}async function Q(n,t,e,a,r){try{const i=await a.estimateGas({data:n,from:t,to:e}),o=r?i:i.add(i.div(10));return o.gt(h)?o.toString():h}catch(i){return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default",i),h}}function Y(n){const{inputToken:t,chainId:e,account:a}=n;return`${t.address.toLowerCase()}-${e}${a?`-${a.toLowerCase()}`:""}`}function y(n,t,e){return new G.Contract(n,t,e)}class z{constructor(t,e){this.provider=t,this.walletSigner=e}contractEncodeABI(t,e,a,r){return y(e||"",t,this.provider).interface.encodeFunctionData(a,r)}signTypedData(t,e,a){const r=Object.keys(e.types).reduce((o,l)=>(l!=="EIP712Domain"&&(o[l]=e.types[l]),o),{});return(this.walletSigner||this.provider.getSigner())._signTypedData(e.domain,r,e.message)}ethCall(t,e){return this.provider.call({to:t,data:e})}decodeABIParameter(t,e){return E.defaultAbiCoder.decode([t],e)[0]}decodeABIParameters(t,e){const a=E.defaultAbiCoder.decode(t,e);if(a&&typeof a=="object"){const r={};return Object.keys(a).forEach(i=>{const o=a[i];x.BigNumber.isBigNumber(o)?r[i]=o.toHexString():r[i]=o}),r}return a}}const k=new Map,C=new Map;function M(n,t,e){const a=k.get(n);if(!e&&a)return a;const r=`${n}-${e}`,i=C.get(r);if(i)return i;const o=new z(t,e?void 0:P),l=new p.Eip2612PermitUtils(o);return e?(console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${n}-${e}`,l),C.set(r,l)):(console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${n}`,l),k.set(n,l)),l}const N=[{constant:!0,inputs:[],name:"name",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"},{inputs:[],name:"eip712Domain",outputs:[{internalType:"bytes1",name:"fields",type:"bytes1"},{internalType:"string",name:"name",type:"string"},{internalType:"string",name:"version",type:"string"},{internalType:"uint256",name:"chainId",type:"uint256"},{internalType:"address",name:"verifyingContract",type:"address"},{internalType:"bytes32",name:"salt",type:"bytes32"},{internalType:"uint256[]",name:"extensions",type:"uint256[]"}],stateMutability:"view",type:"function"}];async function J(n,t,e){const a=A.getAddress(n),i=await y(a,N,e).callStatic.eip712Domain();return X(i,t,n)}async function X(n,t,e){const{extensions:a,...r}=n;if(a.length>0)throw Error("Extensions not implemented");if(r.chainId&&t!==parseInt(r.chainId,16))throw Error(`ChainId mismatch. Received: '${r.chainId}', expected: '${t}'`);if(r.verifyingContract&&e.toLowerCase()!==r.verifyingContract.toLowerCase())throw Error(`Address mismatch. Received: '${r.verifyingContract}', expected: '${e}'`);return ee(r)}const Z=["name","version","chainId","verifyingContract","salt"];function ee(n){const t=parseInt(n.fields,16);return Z.reduce((e,a,r)=>(t&1<<r&&(e[a]=n[a]),e),{})}async function te(n,t,e){const a=A.getAddress(n);return y(a,N,e).callStatic.name()}const ne=[[{constant:!0,inputs:[],name:"version",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}],[{constant:!0,inputs:[],name:"ERC712_VERSION",outputs:[{name:"",type:"string"}],payable:!1,stateMutability:"view",type:"function"}]];async function U(n,t,e=0){const a=ne[e];if(!a)return"1";try{const i=y(n,a,t).interface.encodeFunctionData(a[0].name),o=await t.call({to:n,data:i});return o==="0x"||Number.isNaN(Number(o))?"1":o.startsWith("0x")?E.defaultAbiCoder.decode(["string"],o).toString():o}catch{return U(n,t,e+1)}}const re={value:S,nonce:0,deadline:w()},ae={allowed:!0,nonce:0,expiry:w()},_={},I={type:"unsupported"};async function ie(n){const{tokenAddress:t,chainId:e}=n,a=`${e}-${t.toLowerCase()}`,r=_[a];if(r)return r;const i=oe(n);return _[a]=i,i}async function oe(n){const{spender:t,tokenAddress:e,chainId:a,provider:r,minGasLimit:i}=n,o=M(a,r),l=P.address;let d;try{d=await J(e,a,r)}catch{console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`)}let s=d==null?void 0:d.name;try{s||(s=await te(e,a,r))}catch(c){if(/ETIMEDOUT/.test(c)&&!s)return{error:"Failed to fetch token name from contract. RPC connection error"};console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${e}, using provided '${s}'`,c)}if(!s)return{error:`Token name could not be determined for ${e}`};let m;try{m=await o.getTokenNonce(e,l)}catch(c){return c==="nonce not supported"||c.message==="nonce is NaN"?(console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${s}`,(c==null?void 0:c.message)||c),{...I,name:s}):(console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${s}`,c),{error:c.message||c.toString()})}let u=d==null?void 0:d.version;if(u===void 0)try{u=await U(e,r)}catch(c){console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${s}`,c)}const f={chainId:a,eip2612PermitUtils:o,nonce:m,spender:t,tokenAddress:e,tokenName:s,walletAddress:l,version:u,minGasLimit:i};try{return await D({...f,type:"eip-2612",provider:r})}catch(c){try{return await ce(e,o)?await D({...f,type:"dai-like",provider:r}):/invalid signature/.test(c)||(c==null?void 0:c.code)==="UNPREDICTABLE_GAS_LIMIT"?(console.debug(`[checkTokenIsPermittable] Token ${e} - ${s} might be permittable, but it's not supported for now. Reason:`,c==null?void 0:c.reason),{...I,name:s}):(console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${s}`,c),{error:c.message||c.toString()})}catch(g){return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${s}`,g),{error:g.message||g.toString()}}}}async function D(n){const{provider:t,walletAddress:e,tokenAddress:a,tokenName:r,type:i,version:o,minGasLimit:l=v}=n,s=await(i==="eip-2612"?se:le)(n);return s?(await t.estimateGas({data:s,from:e,to:a})).toNumber()>l?{type:i,version:o,name:r}:{...I,name:r}:{...I,name:r}}async function se(n){const{eip2612PermitUtils:t,walletAddress:e,spender:a,nonce:r,chainId:i,tokenName:o,tokenAddress:l,version:d}=n;return L({eip2612Utils:t,callDataParams:[{...re,owner:e,spender:a,nonce:r},+i,o,l,d]})}async function ce(n,t){return await t.getPermitTypeHash(n)===p.DAI_LIKE_PERMIT_TYPEHASH}async function le(n){const{eip2612PermitUtils:t,tokenAddress:e,walletAddress:a,spender:r,nonce:i,chainId:o,tokenName:l,version:d}=n;return await t.getPermitTypeHash(e)===p.DAI_LIKE_PERMIT_TYPEHASH?R({eip2612Utils:t,callDataParams:[{...ae,holder:a,spender:r,nonce:i},o,l,e,d]}):!1}exports.DEFAULT_MIN_GAS_LIMIT=v;exports.PERMIT_SIGNER=P;exports.checkIsCallDataAValidPermit=V;exports.generatePermitHook=j;exports.getPermitUtilsInstance=M;exports.getTokenPermitInfo=ie;exports.isSupportedPermitInfo=$;
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { MaxUint256 as H } from "@ethersproject/constants";
|
|
2
2
|
import { Wallet as F } from "@ethersproject/wallet";
|
|
3
|
-
import { EIP_2612_PERMIT_SELECTOR as
|
|
4
|
-
import { defaultAbiCoder as
|
|
3
|
+
import { EIP_2612_PERMIT_SELECTOR as I, DAI_PERMIT_SELECTOR as w, Eip2612PermitUtils as O, DAI_LIKE_PERMIT_TYPEHASH as A } from "@1inch/permit-signed-approvals-utils";
|
|
4
|
+
import { defaultAbiCoder as T } from "@ethersproject/abi";
|
|
5
5
|
import { BigNumber as x } from "@ethersproject/bignumber";
|
|
6
6
|
import { Contract as G } from "@ethersproject/contracts";
|
|
7
7
|
import { getAddress as L } from "@ethersproject/address";
|
|
8
|
-
const B = "
|
|
8
|
+
const B = "0xa50dc0f7fc051309434deb3b1c71e927dbb711759231d8ecbf630c85d94a42fe", k = new F(B), K = 55e3, h = "80000", $ = H.toString(), V = 157788e6;
|
|
9
9
|
async function he(n, t, e, a, r, o, {
|
|
10
10
|
version: i,
|
|
11
11
|
type: l,
|
|
@@ -24,16 +24,16 @@ async function he(n, t, e, a, r, o, {
|
|
|
24
24
|
version: i
|
|
25
25
|
};
|
|
26
26
|
let u;
|
|
27
|
-
if (o.startsWith(
|
|
27
|
+
if (o.startsWith(I) ? u = e.recoverPermitOwnerFromCallData({
|
|
28
28
|
...m,
|
|
29
29
|
// I don't know why this was removed, ok?
|
|
30
30
|
// We added it back on buildPermitCallData.ts
|
|
31
31
|
// But it looks like this is needed 🤷
|
|
32
32
|
// Check the test for this method https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.test.ts#L85-L106
|
|
33
|
-
callData: o.replace(w, "0x")
|
|
34
|
-
}) : o.startsWith(I) && (u = e.recoverDaiLikePermitOwnerFromCallData({
|
|
35
|
-
...m,
|
|
36
33
|
callData: o.replace(I, "0x")
|
|
34
|
+
}) : o.startsWith(w) && (u = e.recoverDaiLikePermitOwnerFromCallData({
|
|
35
|
+
...m,
|
|
36
|
+
callData: o.replace(w, "0x")
|
|
37
37
|
})), !!u)
|
|
38
38
|
try {
|
|
39
39
|
return (await u).toLowerCase() === n.toLowerCase();
|
|
@@ -46,21 +46,21 @@ async function S({
|
|
|
46
46
|
callDataParams: t
|
|
47
47
|
}) {
|
|
48
48
|
const [e, a, r, ...o] = t;
|
|
49
|
-
return (await n.buildPermitCallData(e, a, r, ...o)).replace("0x",
|
|
49
|
+
return (await n.buildPermitCallData(e, a, r, ...o)).replace("0x", I);
|
|
50
50
|
}
|
|
51
51
|
async function N({
|
|
52
52
|
eip2612Utils: n,
|
|
53
53
|
callDataParams: t
|
|
54
54
|
}) {
|
|
55
|
-
return (await n.buildDaiLikePermitCallData(...t)).replace("0x",
|
|
55
|
+
return (await n.buildDaiLikePermitCallData(...t)).replace("0x", w);
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
return Math.ceil((Date.now() +
|
|
57
|
+
function E() {
|
|
58
|
+
return Math.ceil((Date.now() + V) / 1e3);
|
|
59
59
|
}
|
|
60
60
|
function q(n) {
|
|
61
61
|
return !!n && n.type !== "unsupported";
|
|
62
62
|
}
|
|
63
|
-
const W = "
|
|
63
|
+
const W = "PERMIT_TOKEN", P = {};
|
|
64
64
|
async function Pe(n) {
|
|
65
65
|
const t = Y(n), e = P[t];
|
|
66
66
|
if (e)
|
|
@@ -87,7 +87,7 @@ async function j(n) {
|
|
|
87
87
|
throw new Error(`Trying to generate permit hook for unsupported token: ${s}`);
|
|
88
88
|
if (!m)
|
|
89
89
|
throw new Error(`No token name for token: ${s}`);
|
|
90
|
-
const u = i ||
|
|
90
|
+
const u = i || k.address, p = d === void 0 ? await l.getTokenNonce(s, u) : d, c = E(), f = $, b = r.type === "eip-2612" ? await S({
|
|
91
91
|
eip2612Utils: l,
|
|
92
92
|
callDataParams: [{
|
|
93
93
|
owner: u,
|
|
@@ -106,10 +106,10 @@ async function j(n) {
|
|
|
106
106
|
nonce: p,
|
|
107
107
|
expiry: c
|
|
108
108
|
}, a, m, s, r.version]
|
|
109
|
-
}), U = await Q(
|
|
109
|
+
}), U = await Q(b, u, s, o, !!i);
|
|
110
110
|
return {
|
|
111
111
|
target: s,
|
|
112
|
-
callData:
|
|
112
|
+
callData: b,
|
|
113
113
|
gasLimit: U,
|
|
114
114
|
dappId: W
|
|
115
115
|
};
|
|
@@ -155,10 +155,10 @@ class z {
|
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
157
|
decodeABIParameter(t, e) {
|
|
158
|
-
return
|
|
158
|
+
return T.decode([t], e)[0];
|
|
159
159
|
}
|
|
160
160
|
decodeABIParameters(t, e) {
|
|
161
|
-
const a =
|
|
161
|
+
const a = T.decode(t, e);
|
|
162
162
|
if (a && typeof a == "object") {
|
|
163
163
|
const r = {};
|
|
164
164
|
return Object.keys(a).forEach((o) => {
|
|
@@ -177,7 +177,7 @@ function J(n, t, e) {
|
|
|
177
177
|
const r = `${n}-${e}`, o = v.get(r);
|
|
178
178
|
if (o)
|
|
179
179
|
return o;
|
|
180
|
-
const i = new z(t, e ? void 0 :
|
|
180
|
+
const i = new z(t, e ? void 0 : k), l = new O(i);
|
|
181
181
|
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${n}-${e}`, l), v.set(r, l)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${n}`, l), C.set(n, l)), l;
|
|
182
182
|
}
|
|
183
183
|
const R = [
|
|
@@ -297,7 +297,7 @@ async function M(n, t, e = 0) {
|
|
|
297
297
|
to: n,
|
|
298
298
|
data: o
|
|
299
299
|
});
|
|
300
|
-
return i === "0x" || Number.isNaN(Number(i)) ? "1" : i.startsWith("0x") ?
|
|
300
|
+
return i === "0x" || Number.isNaN(Number(i)) ? "1" : i.startsWith("0x") ? T.decode(["string"], i).toString() : i;
|
|
301
301
|
} catch {
|
|
302
302
|
return M(n, t, e + 1);
|
|
303
303
|
}
|
|
@@ -305,15 +305,15 @@ async function M(n, t, e = 0) {
|
|
|
305
305
|
const ae = {
|
|
306
306
|
value: $,
|
|
307
307
|
nonce: 0,
|
|
308
|
-
deadline:
|
|
308
|
+
deadline: E()
|
|
309
309
|
}, oe = {
|
|
310
310
|
allowed: !0,
|
|
311
311
|
nonce: 0,
|
|
312
|
-
expiry:
|
|
312
|
+
expiry: E()
|
|
313
313
|
}, D = {}, g = {
|
|
314
314
|
type: "unsupported"
|
|
315
315
|
};
|
|
316
|
-
async function
|
|
316
|
+
async function Ie(n) {
|
|
317
317
|
const {
|
|
318
318
|
tokenAddress: t,
|
|
319
319
|
chainId: e
|
|
@@ -330,7 +330,7 @@ async function ie(n) {
|
|
|
330
330
|
chainId: a,
|
|
331
331
|
provider: r,
|
|
332
332
|
minGasLimit: o
|
|
333
|
-
} = n, i = J(a, r), l =
|
|
333
|
+
} = n, i = J(a, r), l = k.address;
|
|
334
334
|
let d;
|
|
335
335
|
try {
|
|
336
336
|
d = await X(e, a, r);
|
|
@@ -381,14 +381,14 @@ async function ie(n) {
|
|
|
381
381
|
minGasLimit: o
|
|
382
382
|
};
|
|
383
383
|
try {
|
|
384
|
-
return await
|
|
384
|
+
return await _({
|
|
385
385
|
...p,
|
|
386
386
|
type: "eip-2612",
|
|
387
387
|
provider: r
|
|
388
388
|
});
|
|
389
389
|
} catch (c) {
|
|
390
390
|
try {
|
|
391
|
-
return await ce(e, i) ? await
|
|
391
|
+
return await ce(e, i) ? await _({
|
|
392
392
|
...p,
|
|
393
393
|
type: "dai-like",
|
|
394
394
|
provider: r
|
|
@@ -405,7 +405,7 @@ async function ie(n) {
|
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
407
|
}
|
|
408
|
-
async function
|
|
408
|
+
async function _(n) {
|
|
409
409
|
const {
|
|
410
410
|
provider: t,
|
|
411
411
|
walletAddress: e,
|
|
@@ -413,7 +413,7 @@ async function A(n) {
|
|
|
413
413
|
tokenName: r,
|
|
414
414
|
type: o,
|
|
415
415
|
version: i,
|
|
416
|
-
minGasLimit: l =
|
|
416
|
+
minGasLimit: l = K
|
|
417
417
|
} = n, s = await (o === "eip-2612" ? se : le)(n);
|
|
418
418
|
return s ? (await t.estimateGas({
|
|
419
419
|
data: s,
|
|
@@ -453,7 +453,7 @@ async function se(n) {
|
|
|
453
453
|
});
|
|
454
454
|
}
|
|
455
455
|
async function ce(n, t) {
|
|
456
|
-
return await t.getPermitTypeHash(n) ===
|
|
456
|
+
return await t.getPermitTypeHash(n) === A;
|
|
457
457
|
}
|
|
458
458
|
async function le(n) {
|
|
459
459
|
const {
|
|
@@ -466,7 +466,7 @@ async function le(n) {
|
|
|
466
466
|
tokenName: l,
|
|
467
467
|
version: d
|
|
468
468
|
} = n;
|
|
469
|
-
return await t.getPermitTypeHash(e) ===
|
|
469
|
+
return await t.getPermitTypeHash(e) === A ? N({
|
|
470
470
|
eip2612Utils: t,
|
|
471
471
|
callDataParams: [{
|
|
472
472
|
...oe,
|
|
@@ -477,11 +477,11 @@ async function le(n) {
|
|
|
477
477
|
}) : !1;
|
|
478
478
|
}
|
|
479
479
|
export {
|
|
480
|
-
|
|
481
|
-
|
|
480
|
+
K as DEFAULT_MIN_GAS_LIMIT,
|
|
481
|
+
k as PERMIT_SIGNER,
|
|
482
482
|
he as checkIsCallDataAValidPermit,
|
|
483
483
|
Pe as generatePermitHook,
|
|
484
484
|
J as getPermitUtilsInstance,
|
|
485
|
-
|
|
485
|
+
Ie as getTokenPermitInfo,
|
|
486
486
|
q as isSupportedPermitInfo
|
|
487
487
|
};
|