Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@cowprotocol/permit-utils

Package Overview
Dependencies
Maintainers
5
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cowprotocol/permit-utils - npm Package Compare versions

Comparing version 0.1.1-RC.1 to 0.1.2

2

const.d.ts
import { Wallet } from '@ethersproject/wallet';
export declare const PERMIT_SIGNER: Wallet;
export declare const PERMIT_GAS_LIMIT_MIN: Record<number, number>;
export declare const DEFAULT_MIN_GAS_LIMIT = 55000;
export declare const DEFAULT_PERMIT_GAS_LIMIT = "80000";

@@ -5,0 +5,0 @@ export declare const DEFAULT_PERMIT_VALUE: string;

@@ -1,2 +0,2 @@

export { PERMIT_SIGNER } from './const';
export { PERMIT_SIGNER, DEFAULT_MIN_GAS_LIMIT } from './const';
export { checkIsCallDataAValidPermit } from './lib/checkIsCallDataAValidPermit';

@@ -3,0 +3,0 @@ export { generatePermitHook } from './lib/generatePermitHook';

@@ -1,27 +0,22 @@

import { MaxUint256 as H } from "@ethersproject/constants";
import { Wallet as F } from "@ethersproject/wallet";
import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as I, Eip2612PermitUtils as x, DAI_LIKE_PERMIT_TYPEHASH as L } from "@1inch/permit-signed-approvals-utils";
import { defaultAbiCoder as b } from "@ethersproject/abi";
import { BigNumber as O } from "@ethersproject/bignumber";
import { Contract as G } from "@ethersproject/contracts";
import { getAddress as $ } from "@ethersproject/address";
const K = "0x4dae303b820e9878cafeb0f84edcc015e8a81b1bff510e824e4fc27544e458dd", T = new F(K), h = 55e3, B = {
1: h,
100: h,
11155111: h,
5: 36e3
}, y = "80000", S = H.toString(), V = 157788e6, q = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
async function he(r, t, e, a, n, o, {
import { MaxUint256 as M } from "@ethersproject/constants";
import { Wallet as H } from "@ethersproject/wallet";
import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as w, Eip2612PermitUtils as F, DAI_LIKE_PERMIT_TYPEHASH as _ } from "@1inch/permit-signed-approvals-utils";
import { defaultAbiCoder as E } from "@ethersproject/abi";
import { BigNumber as x } from "@ethersproject/bignumber";
import { Contract as O } from "@ethersproject/contracts";
import { getAddress as L } from "@ethersproject/address";
const G = "0x1b80501ea68b883241ac5b9f92e8635aa3df23c89b7bbb87e762be65b8c6eb75", T = new H(G), K = 55e3, y = "80000", $ = M.toString(), B = 157788e6, V = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
async function ge(r, n, e, a, t, o, {
version: c,
type: s,
name: l
type: l,
name: d
}) {
const d = l || n;
if (s === "unsupported")
const s = d || t;
if (l === "unsupported")
return !1;
if (!d)
if (!s)
throw new Error(`No token name for ${a}`);
const u = {
chainId: t,
tokenName: d,
chainId: n,
tokenName: s,
tokenAddress: a,

@@ -39,69 +34,69 @@ callData: o,

callData: o.replace(P, "0x")
}) : o.startsWith(I) && (m = e.recoverDaiLikePermitOwnerFromCallData({
}) : o.startsWith(w) && (m = e.recoverDaiLikePermitOwnerFromCallData({
...u,
callData: o.replace(I, "0x")
callData: o.replace(w, "0x")
})), !!m)
try {
return (await m).toLowerCase() === r.toLowerCase();
} catch (i) {
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`, i), !1;
} catch (p) {
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${r} with callData ${o}`, p), !1;
}
}
async function R({
async function S({
eip2162Utils: r,
callDataParams: t
callDataParams: n
}) {
const [e, a, n, ...o] = t;
return (await r.buildPermitCallData(e, a, n, ...o)).replace("0x", P);
const [e, a, t, ...o] = n;
return (await r.buildPermitCallData(e, a, t, ...o)).replace("0x", P);
}
async function N({
async function R({
eip2162Utils: r,
callDataParams: t
callDataParams: n
}) {
return (await r.buildDaiLikePermitCallData(...t)).replace("0x", I);
return (await r.buildDaiLikePermitCallData(...n)).replace("0x", w);
}
function w() {
return Math.ceil((Date.now() + V) / 1e3);
function I() {
return Math.ceil((Date.now() + B) / 1e3);
}
function W(r) {
function q(r) {
return !!r && r.type !== "unsupported";
}
const f = {};
async function ye(r) {
const t = Y(r), e = f[t];
const g = {};
async function he(r) {
const n = Q(r), e = g[n];
if (e)
try {
return await e;
} catch (n) {
console.debug("[generatePermitHookWith] cached request failed", n), delete f[t];
} catch (t) {
console.debug("[generatePermitHookWith] cached request failed", t), delete g[n];
}
const a = j(r).then((n) => (delete f[t], n));
return f[t] = a, a;
const a = W(r).then((t) => (delete g[n], t));
return g[n] = a, a;
}
async function j(r) {
async function W(r) {
const {
inputToken: t,
inputToken: n,
spender: e,
chainId: a,
permitInfo: n,
permitInfo: t,
provider: o,
account: c,
eip2162Utils: s,
nonce: l
} = r, d = t.address, u = n.name || t.name;
if (!W(n))
throw new Error(`Trying to generate permit hook for unsupported token: ${d}`);
eip2162Utils: l,
nonce: d
} = r, s = n.address, u = t.name || n.name;
if (!q(t))
throw new Error(`Trying to generate permit hook for unsupported token: ${s}`);
if (!u)
throw new Error(`No token name for token: ${d}`);
const m = c || T.address, i = l === void 0 ? await s.getTokenNonce(d, m) : l, p = w(), E = S, C = n.type === "eip-2612" ? await R({
eip2162Utils: s,
throw new Error(`No token name for token: ${s}`);
const m = c || T.address, p = d === void 0 ? await l.getTokenNonce(s, m) : d, i = I(), f = $, b = t.type === "eip-2612" ? await S({
eip2162Utils: l,
callDataParams: [{
owner: m,
spender: e,
value: E,
nonce: i,
deadline: p
}, a, u, d, n.version]
}) : await N({
eip2162Utils: s,
value: f,
nonce: p,
deadline: i
}, a, u, s, t.version]
}) : await R({
eip2162Utils: l,
callDataParams: [{

@@ -111,20 +106,20 @@ holder: m,

allowed: !0,
value: E,
nonce: i,
expiry: p
}, a, u, d, n.version]
}), U = await Q(C, m, d, o, !!c);
value: f,
nonce: p,
expiry: i
}, a, u, s, t.version]
}), U = await j(b, m, s, o, !!c);
return {
target: d,
callData: C,
target: s,
callData: b,
gasLimit: U
};
}
async function Q(r, t, e, a, n) {
async function j(r, n, e, a, t) {
try {
const o = await a.estimateGas({
data: r,
from: t,
from: n,
to: e
}), c = n ? o : o.add(o.div(10));
}), c = t ? o : o.add(o.div(10));
return c.gt(y) ? c.toString() : y;

@@ -135,41 +130,41 @@ } catch (o) {

}
function Y(r) {
function Q(r) {
const {
inputToken: t,
inputToken: n,
chainId: e,
account: a
} = r;
return `${t.address.toLowerCase()}-${e}${a ? `-${a.toLowerCase()}` : ""}`;
return `${n.address.toLowerCase()}-${e}${a ? `-${a.toLowerCase()}` : ""}`;
}
function k(r, t, e) {
return new G(r, t, e);
function k(r, n, e) {
return new O(r, n, e);
}
class z {
constructor(t, e) {
this.provider = t, this.walletSigner = e;
class Y {
constructor(n, e) {
this.provider = n, this.walletSigner = e;
}
contractEncodeABI(t, e, a, n) {
return k(e || "", t, this.provider).interface.encodeFunctionData(a, n);
contractEncodeABI(n, e, a, t) {
return k(e || "", n, this.provider).interface.encodeFunctionData(a, t);
}
signTypedData(t, e, a) {
const n = Object.keys(e.types).reduce((c, s) => (s !== "EIP712Domain" && (c[s] = e.types[s]), c), {});
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, n, e.message);
signTypedData(n, e, a) {
const t = Object.keys(e.types).reduce((c, l) => (l !== "EIP712Domain" && (c[l] = e.types[l]), c), {});
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, t, e.message);
}
ethCall(t, e) {
ethCall(n, e) {
return this.provider.call({
to: t,
to: n,
data: e
});
}
decodeABIParameter(t, e) {
return b.decode([t], e)[0];
decodeABIParameter(n, e) {
return E.decode([n], e)[0];
}
decodeABIParameters(t, e) {
const a = b.decode(t, e);
decodeABIParameters(n, e) {
const a = E.decode(n, e);
if (a && typeof a == "object") {
const n = {};
const t = {};
return Object.keys(a).forEach((o) => {
const c = a[o];
O.isBigNumber(c) ? n[o] = c.toHexString() : n[o] = c;
}), n;
x.isBigNumber(c) ? t[o] = c.toHexString() : t[o] = c;
}), t;
}

@@ -179,14 +174,14 @@ return a;

}
const v = /* @__PURE__ */ new Map(), D = /* @__PURE__ */ new Map();
function J(r, t, e) {
const a = v.get(r);
const C = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Map();
function z(r, n, e) {
const a = C.get(r);
if (!e && a)
return a;
const n = `${r}-${e}`, o = D.get(n);
const t = `${r}-${e}`, o = v.get(t);
if (o)
return o;
const c = new z(t, e ? void 0 : T), s = new x(c);
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`, s), D.set(n, s)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`, s), v.set(r, s)), s;
const c = new Y(n, e ? void 0 : T), l = new F(c);
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${r}-${e}`, l), v.set(t, l)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${r}`, l), C.set(r, l)), l;
}
const M = [
const N = [
{

@@ -250,125 +245,127 @@ constant: !0,

];
async function X(r, t, e) {
const a = $(r), o = await k(a, M, e).callStatic.eip712Domain();
return Z(o, t, r);
async function J(r, n, e) {
const a = L(r), o = await k(a, N, e).callStatic.eip712Domain();
return X(o, n, r);
}
async function Z(r, t, e) {
async function X(r, n, e) {
const {
extensions: a,
...n
...t
} = r;
if (a.length > 0)
throw Error("Extensions not implemented");
if (n.chainId && t !== parseInt(n.chainId, 16))
throw Error(`ChainId mismatch. Received: '${n.chainId}', expected: '${t}'`);
if (n.verifyingContract && e.toLowerCase() !== n.verifyingContract.toLowerCase())
throw Error(`Address mismatch. Received: '${n.verifyingContract}', expected: '${e}'`);
return te(n);
if (t.chainId && n !== parseInt(t.chainId, 16))
throw Error(`ChainId mismatch. Received: '${t.chainId}', expected: '${n}'`);
if (t.verifyingContract && e.toLowerCase() !== t.verifyingContract.toLowerCase())
throw Error(`Address mismatch. Received: '${t.verifyingContract}', expected: '${e}'`);
return ee(t);
}
const ee = ["name", "version", "chainId", "verifyingContract", "salt"];
function te(r) {
const t = parseInt(r.fields, 16);
return ee.reduce((e, a, n) => (t & 1 << n && (e[a] = r[a]), e), {});
const Z = ["name", "version", "chainId", "verifyingContract", "salt"];
function ee(r) {
const n = parseInt(r.fields, 16);
return Z.reduce((e, a, t) => (n & 1 << t && (e[a] = r[a]), e), {});
}
async function ne(r, t, e) {
const a = $(r);
return k(a, M, e).callStatic.name();
async function te(r, n, e) {
const a = L(r);
return k(a, N, e).callStatic.name();
}
const re = {
value: S,
const ne = {
value: $,
nonce: 0,
deadline: w()
}, ae = {
deadline: I()
}, re = {
allowed: !0,
nonce: 0,
expiry: w()
}, A = {}, g = {
expiry: I()
}, D = {}, h = {
type: "unsupported"
};
async function Pe(r) {
async function ye(r) {
const {
tokenAddress: t,
tokenAddress: n,
chainId: e
} = r, a = `${e}-${t.toLowerCase()}`, n = A[a];
if (n)
return n;
const o = oe(r);
return A[a] = o, o;
} = r, a = `${e}-${n.toLowerCase()}`, t = D[a];
if (t)
return t;
const o = ae(r);
return D[a] = o, o;
}
async function oe(r) {
async function ae(r) {
const {
spender: t,
spender: n,
tokenAddress: e,
chainId: a,
provider: n
} = r, o = J(a, n), c = T.address;
let s;
provider: t,
minGasLimit: o
} = r, c = z(a, t), l = T.address;
let d;
try {
s = await X(e, a, n);
} catch (i) {
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`, i);
d = await J(e, a, t);
} catch {
console.debug(`[checkTokenIsPermittable] Couldn't fetch eip712domain for token ${e}`);
}
let l = s == null ? void 0 : s.name;
let s = d == null ? void 0 : d.name;
try {
l || (l = await ne(e, a, n));
s || (s = await te(e, a, t));
} catch (i) {
if (/ETIMEDOUT/.test(i) && !l)
if (/ETIMEDOUT/.test(i) && !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 '${l}'`, i);
console.debug(`[checkTokenIsPermittable] Couldn't fetch token name from the contract for token ${e}, using provided '${s}'`, i);
}
if (!l)
if (!s)
return {
error: `Token name could not be determined for ${e}`
};
let d;
let u;
try {
d = await o.getTokenNonce(e, c);
u = await c.getTokenNonce(e, l);
} catch (i) {
return i === "nonce not supported" || i.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${l}`, (i == null ? void 0 : i.message) || i), {
...g,
name: l
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${l}`, i), {
return i === "nonce not supported" || i.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${s}`, (i == null ? void 0 : i.message) || i), {
...h,
name: s
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${s}`, i), {
error: i.message || i.toString()
});
}
let u = s == null ? void 0 : s.version;
if (!q.has(e) && u === void 0)
let m = d == null ? void 0 : d.version;
if (!V.has(e) && m === void 0)
try {
u = await o.getTokenVersion(e);
m = await c.getTokenVersion(e);
} catch (i) {
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${l}`, i);
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${s}`, i);
}
const m = {
const p = {
chainId: a,
eip2612PermitUtils: o,
nonce: d,
spender: t,
eip2612PermitUtils: c,
nonce: u,
spender: n,
tokenAddress: e,
tokenName: l,
walletAddress: c,
version: u
tokenName: s,
walletAddress: l,
version: m,
minGasLimit: o
};
try {
return await _({
...m,
return await A({
...p,
type: "eip-2612",
provider: n
provider: t
});
} catch (i) {
try {
return await se(e, o) ? await _({
...m,
return await ie(e, c) ? await A({
...p,
type: "dai-like",
provider: n
}) : /invalid signature/.test(i) || (i == null ? void 0 : i.code) === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${e} - ${l} might be permittable, but it's not supported for now. Reason:`, i == null ? void 0 : i.reason), {
...g,
name: l
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${l}`, i), {
provider: t
}) : /invalid signature/.test(i) || (i == null ? void 0 : i.code) === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${e} - ${s} might be permittable, but it's not supported for now. Reason:`, i == null ? void 0 : i.reason), {
...h,
name: s
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${s}`, i), {
error: i.message || i.toString()
});
} catch (p) {
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${l}`, p), {
error: p.message || p.toString()
} catch (f) {
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${s}`, f), {
error: f.message || f.toString()
};

@@ -378,80 +375,81 @@ }

}
async function _(r) {
async function A(r) {
const {
provider: t,
chainId: e,
walletAddress: a,
tokenAddress: n,
tokenName: o,
type: c,
version: s
} = r, d = await (c === "eip-2612" ? ie : ce)(r);
return d ? (await t.estimateGas({
data: d,
from: a,
to: n
})).toNumber() > B[e] ? {
type: c,
version: s,
name: o
provider: n,
walletAddress: e,
tokenAddress: a,
tokenName: t,
type: o,
version: c,
minGasLimit: l = K
} = r, s = await (o === "eip-2612" ? oe : se)(r);
return s ? (await n.estimateGas({
data: s,
from: e,
to: a
})).toNumber() > l ? {
type: o,
version: c,
name: t
} : {
...g,
name: o
...h,
name: t
} : {
...g,
name: o
...h,
name: t
};
}
async function ie(r) {
async function oe(r) {
const {
eip2612PermitUtils: t,
eip2612PermitUtils: n,
walletAddress: e,
spender: a,
nonce: n,
nonce: t,
chainId: o,
tokenName: c,
tokenAddress: s,
version: l
tokenAddress: l,
version: d
} = r;
return R({
eip2162Utils: t,
return S({
eip2162Utils: n,
callDataParams: [{
...re,
...ne,
owner: e,
spender: a,
nonce: n
}, +o, c, s, l]
nonce: t
}, +o, c, l, d]
});
}
async function se(r, t) {
return await t.getPermitTypeHash(r) === L;
async function ie(r, n) {
return await n.getPermitTypeHash(r) === _;
}
async function ce(r) {
async function se(r) {
const {
eip2612PermitUtils: t,
eip2612PermitUtils: n,
tokenAddress: e,
walletAddress: a,
spender: n,
spender: t,
nonce: o,
chainId: c,
tokenName: s,
version: l
tokenName: l,
version: d
} = r;
return await t.getPermitTypeHash(e) === L ? N({
eip2162Utils: t,
return await n.getPermitTypeHash(e) === _ ? R({
eip2162Utils: n,
callDataParams: [{
...ae,
...re,
holder: a,
spender: n,
spender: t,
nonce: o
}, c, s, e, l]
}, c, l, e, d]
}) : !1;
}
export {
K as DEFAULT_MIN_GAS_LIMIT,
T as PERMIT_SIGNER,
he as checkIsCallDataAValidPermit,
ye as generatePermitHook,
J as getPermitUtilsInstance,
Pe as getTokenPermitInfo,
W as isSupportedPermitInfo
ge as checkIsCallDataAValidPermit,
he as generatePermitHook,
z as getPermitUtilsInstance,
ye as getTokenPermitInfo,
q as isSupportedPermitInfo
};
{
"name": "@cowprotocol/permit-utils",
"version": "0.1.1-RC.1",
"version": "0.1.2",
"type": "module",

@@ -5,0 +5,0 @@ "dependencies": {

@@ -43,3 +43,4 @@ import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';

provider: JsonRpcProvider;
minGasLimit?: number | undefined;
};
export {};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc