@cowprotocol/permit-utils 0.4.0 → 0.6.1-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/README.md +3 -3
- package/index.cjs +1 -1
- package/index.js +7 -7
- package/lib/checkIsCallDataAValidPermit.d.ts +1 -1
- package/package.json +1 -1
- package/types.d.ts +3 -3
- package/utils/buildPermitCallData.d.ts +3 -3
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ const hookData = await generatePermitHook({
|
|
|
43
43
|
spender,
|
|
44
44
|
provider,
|
|
45
45
|
permitInfo,
|
|
46
|
-
|
|
46
|
+
eip2612Utils,
|
|
47
47
|
account,
|
|
48
48
|
nonce
|
|
49
49
|
})
|
|
@@ -57,7 +57,7 @@ import { checkIsCallDataAValidPermit } from "@cowprotocol/permit-utils"
|
|
|
57
57
|
const isCallDataAValidPermit = await checkIsCallDataAValidPermit(
|
|
58
58
|
account,
|
|
59
59
|
chainId,
|
|
60
|
-
eip2612Utils,
|
|
60
|
+
eip2612Utils,
|
|
61
61
|
tokenAddress,
|
|
62
62
|
tokenName,
|
|
63
63
|
callData,
|
|
@@ -97,7 +97,7 @@ if (!permitInfo) {
|
|
|
97
97
|
const eip2612Utils = getPermitUtilsInstance(chainId, provider, account)
|
|
98
98
|
|
|
99
99
|
// Need to know what the current permit nonce is
|
|
100
|
-
const nonce = await
|
|
100
|
+
const nonce = await eip2612Utils.getTokenNonce(inputToken.address, account)
|
|
101
101
|
|
|
102
102
|
// Calling this fn should trigger the signature in the user's wallet
|
|
103
103
|
const hookData = await generatePermitHook({
|
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="0x68012a4467ce455b6b278b1a6815db9b7224deaa6bced68c3848ec21e6380f8a",I=new F.Wallet(q),v=55e3,h="80000",S=O.MaxUint256.toString(),V=157788e6;async function B(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({
|
|
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="0x68012a4467ce455b6b278b1a6815db9b7224deaa6bced68c3848ec21e6380f8a",I=new F.Wallet(q),v=55e3,h="80000",S=O.MaxUint256.toString(),V=157788e6;async function B(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 b(){return Math.ceil((Date.now()+V)/1e3)}function $(n){return!!n&&n.type!=="unsupported"}const K="1db4bacb661a90fb6b475fd5b585acba9745bc373573c65ecc3e8f5bfd5dee1f",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||I.address,f=d===void 0?await l.getTokenNonce(s,u):d,c=b(),g=S,w=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(w,u,s,i,!!o);return{target:s,callData:w,gasLimit:H,dappId:K}}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 P(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 P(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:I),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 P(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 P(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=P(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:b()},ae={allowed:!0,nonce:0,expiry:b()},_={},y={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=I.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),{...y,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),{...y,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}:{...y,name:r}:{...y,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=I;exports.checkIsCallDataAValidPermit=B;exports.generatePermitHook=j;exports.getPermitUtilsInstance=M;exports.getTokenPermitInfo=ie;exports.isSupportedPermitInfo=$;
|
package/index.js
CHANGED
|
@@ -42,14 +42,14 @@ async function he(n, t, e, a, r, o, {
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
async function S({
|
|
45
|
-
|
|
45
|
+
eip2612Utils: n,
|
|
46
46
|
callDataParams: t
|
|
47
47
|
}) {
|
|
48
48
|
const [e, a, r, ...o] = t;
|
|
49
49
|
return (await n.buildPermitCallData(e, a, r, ...o)).replace("0x", w);
|
|
50
50
|
}
|
|
51
51
|
async function N({
|
|
52
|
-
|
|
52
|
+
eip2612Utils: n,
|
|
53
53
|
callDataParams: t
|
|
54
54
|
}) {
|
|
55
55
|
return (await n.buildDaiLikePermitCallData(...t)).replace("0x", I);
|
|
@@ -80,7 +80,7 @@ async function j(n) {
|
|
|
80
80
|
permitInfo: r,
|
|
81
81
|
provider: o,
|
|
82
82
|
account: i,
|
|
83
|
-
|
|
83
|
+
eip2612Utils: l,
|
|
84
84
|
nonce: d
|
|
85
85
|
} = n, s = t.address, m = r.name || t.name;
|
|
86
86
|
if (!q(r))
|
|
@@ -88,7 +88,7 @@ async function j(n) {
|
|
|
88
88
|
if (!m)
|
|
89
89
|
throw new Error(`No token name for token: ${s}`);
|
|
90
90
|
const u = i || T.address, p = d === void 0 ? await l.getTokenNonce(s, u) : d, c = k(), f = $, E = r.type === "eip-2612" ? await S({
|
|
91
|
-
|
|
91
|
+
eip2612Utils: l,
|
|
92
92
|
callDataParams: [{
|
|
93
93
|
owner: u,
|
|
94
94
|
spender: e,
|
|
@@ -97,7 +97,7 @@ async function j(n) {
|
|
|
97
97
|
deadline: c
|
|
98
98
|
}, a, m, s, r.version]
|
|
99
99
|
}) : await N({
|
|
100
|
-
|
|
100
|
+
eip2612Utils: l,
|
|
101
101
|
callDataParams: [{
|
|
102
102
|
holder: u,
|
|
103
103
|
spender: e,
|
|
@@ -443,7 +443,7 @@ async function se(n) {
|
|
|
443
443
|
version: d
|
|
444
444
|
} = n;
|
|
445
445
|
return S({
|
|
446
|
-
|
|
446
|
+
eip2612Utils: t,
|
|
447
447
|
callDataParams: [{
|
|
448
448
|
...ae,
|
|
449
449
|
owner: e,
|
|
@@ -467,7 +467,7 @@ async function le(n) {
|
|
|
467
467
|
version: d
|
|
468
468
|
} = n;
|
|
469
469
|
return await t.getPermitTypeHash(e) === _ ? N({
|
|
470
|
-
|
|
470
|
+
eip2612Utils: t,
|
|
471
471
|
callDataParams: [{
|
|
472
472
|
...oe,
|
|
473
473
|
holder: a,
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
|
|
2
2
|
import { PermitInfo } from '../types';
|
|
3
|
-
export declare function checkIsCallDataAValidPermit(owner: string, chainId: number,
|
|
3
|
+
export declare function checkIsCallDataAValidPermit(owner: string, chainId: number, eip2612Utils: Eip2612PermitUtils, tokenAddress: string, _tokenName: string | undefined, callData: string, { version, type, name }: PermitInfo): Promise<boolean | undefined>;
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type PermitHookParams = {
|
|
|
17
17
|
chainId: number;
|
|
18
18
|
permitInfo: PermitInfo;
|
|
19
19
|
provider: JsonRpcProvider;
|
|
20
|
-
|
|
20
|
+
eip2612Utils: Eip2612PermitUtils;
|
|
21
21
|
account?: string | undefined;
|
|
22
22
|
nonce?: number | undefined;
|
|
23
23
|
};
|
|
@@ -27,9 +27,9 @@ type FailedToIdentify = {
|
|
|
27
27
|
};
|
|
28
28
|
export type GetTokenPermitIntoResult = PermitInfo | FailedToIdentify;
|
|
29
29
|
type BasePermitCallDataParams = {
|
|
30
|
-
|
|
30
|
+
eip2612Utils: Eip2612PermitUtils;
|
|
31
31
|
};
|
|
32
|
-
export type
|
|
32
|
+
export type BuildEip2612PermitCallDataParams = BasePermitCallDataParams & {
|
|
33
33
|
callDataParams: Parameters<Eip2612PermitUtils['buildPermitCallData']>;
|
|
34
34
|
};
|
|
35
35
|
export type BuildDaiLikePermitCallDataParams = BasePermitCallDataParams & {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { BuildDaiLikePermitCallDataParams,
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function buildDaiLikePermitCallData({
|
|
1
|
+
import { BuildDaiLikePermitCallDataParams, BuildEip2612PermitCallDataParams } from '../types';
|
|
2
|
+
export declare function buildEip2612PermitCallData({ eip2612Utils, callDataParams, }: BuildEip2612PermitCallDataParams): Promise<string>;
|
|
3
|
+
export declare function buildDaiLikePermitCallData({ eip2612Utils, callDataParams, }: BuildDaiLikePermitCallDataParams): Promise<string>;
|