Socket
Socket
Sign inDemoInstall

@cowprotocol/permit-utils

Package Overview
Dependencies
Maintainers
5
Versions
9
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.0.1-RC.1 to 0.0.1

utils/getContract.d.ts

3

index.d.ts

@@ -5,2 +5,3 @@ export { checkIsCallDataAValidPermit } from './lib/checkIsCallDataAValidPermit';

export { getTokenPermitInfo } from './lib/getTokenPermitInfo';
export type { PermitHookData, PermitHookParams, PermitInfo, PermitType, SupportedPermitInfo, GetTokenPermitIntoResult, } from './types';
export { isSupportedPermitInfo } from './utils/isSupportedPermitInfo';
export type { PermitHookData, PermitHookParams, PermitInfo, PermitType, GetTokenPermitIntoResult } from './types';

@@ -1,23 +0,31 @@

import { EIP_2612_PERMIT_SELECTOR as P, DAI_PERMIT_SELECTOR as g, Eip2612PermitUtils as R, DAI_LIKE_PERMIT_TYPEHASH as U } from "@1inch/permit-signed-approvals-utils";
import { MaxUint256 as N } from "@ethersproject/constants";
import { Wallet as M } from "@ethersproject/wallet";
import { defaultAbiCoder as E } from "@ethersproject/abi";
import { BigNumber as $ } from "@ethersproject/bignumber";
import { Contract as H } from "@ethersproject/contracts";
function D(a) {
return a === "USD//C" ? "USD Coin" : a;
import { EIP_2612_PERMIT_SELECTOR as h, DAI_PERMIT_SELECTOR as k, Eip2612PermitUtils as U, DAI_LIKE_PERMIT_TYPEHASH as A } from "@1inch/permit-signed-approvals-utils";
import { MaxUint256 as M } from "@ethersproject/constants";
import { Wallet as H } from "@ethersproject/wallet";
import { defaultAbiCoder as y } from "@ethersproject/abi";
import { BigNumber as F } from "@ethersproject/bignumber";
import { Contract as O } from "@ethersproject/contracts";
import { getAddress as x } from "@ethersproject/address";
function v(t) {
return t === "USD//C" ? "USD Coin" : t;
}
async function ae(a, t, e, n, o, r, {
version: i
async function ue(t, n, e, o, a, r, {
version: i,
type: c,
name: l
}) {
const s = {
chainId: t,
tokenName: D(o),
tokenAddress: n,
const d = l || a;
if (c === "unsupported")
return !1;
if (!d)
throw new Error(`No token name for ${o}`);
const u = {
chainId: n,
tokenName: v(d),
tokenAddress: o,
callData: r,
version: i
};
let c;
if (r.startsWith(P) ? c = e.recoverPermitOwnerFromCallData({
...s,
let m;
if (r.startsWith(h) ? m = e.recoverPermitOwnerFromCallData({
...u,
// I don't know why this was removed, ok?

@@ -27,202 +35,249 @@ // We added it back on buildPermitCallData.ts

// Check the test for this method https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.test.ts#L85-L106
callData: r.replace(P, "0x")
}) : r.startsWith(g) && (c = e.recoverDaiLikePermitOwnerFromCallData({
...s,
callData: r.replace(g, "0x")
})), !!c)
callData: r.replace(h, "0x")
}) : r.startsWith(k) && (m = e.recoverDaiLikePermitOwnerFromCallData({
...u,
callData: r.replace(k, "0x")
})), !!m)
try {
return (await c).toLowerCase() === a.toLowerCase();
} catch (d) {
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${a} with callData ${r}`, d), !1;
return (await m).toLowerCase() === t.toLowerCase();
} catch (s) {
return console.debug(`[checkHasValidPendingPermit] Failed to check permit validity for owner ${t} with callData ${r}`, s), !1;
}
}
const F = "0xc58a2a421ca71ca57ae698f1c32feeb0b0ccb434da0b8089d88d80fb918f3f9d", h = new M(F), O = {
const G = "0xec10458cfaafb32533a4a27e18d9da345758094dde7052521b939a41c55dd1b0", w = new H(G), K = {
1: 55e3,
100: 55e3,
5: 36e3
}, p = "80000", b = N.toString(), x = 157788e6, G = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
async function v({
eip2162Utils: a,
callDataParams: t
}, P = "80000", S = M.toString(), V = 157788e6, B = /* @__PURE__ */ new Set(["0x6b175474e89094c44da98b954eedeac495271d0f"]);
async function L({
eip2162Utils: t,
callDataParams: n
}) {
const [e, n, o, ...r] = t;
return (await a.buildPermitCallData(e, n, D(o), ...r)).replace("0x", P);
const [e, o, a, ...r] = n;
return (await t.buildPermitCallData(e, o, v(a), ...r)).replace("0x", h);
}
async function S({
eip2162Utils: a,
callDataParams: t
async function $({
eip2162Utils: t,
callDataParams: n
}) {
return (await a.buildDaiLikePermitCallData(...t)).replace("0x", g);
return (await t.buildDaiLikePermitCallData(...n)).replace("0x", k);
}
function I() {
return Math.ceil((Date.now() + x) / 1e3);
function T() {
return Math.ceil((Date.now() + V) / 1e3);
}
function q(t) {
return !!t && t.type !== "unsupported";
}
const f = {};
async function oe(a) {
const t = B(a), e = f[t];
async function pe(t) {
const n = Q(t), e = f[n];
if (e)
try {
return await e;
} catch (o) {
console.debug("[generatePermitHookWith] cached request failed", o), delete f[t];
} catch (a) {
console.debug("[generatePermitHookWith] cached request failed", a), delete f[n];
}
const n = K(a).then((o) => (delete f[t], o));
return f[t] = n, n;
const o = W(t).then((a) => (delete f[n], a));
return f[n] = o, o;
}
async function K(a) {
async function W(t) {
const {
inputToken: t,
inputToken: n,
spender: e,
chainId: n,
permitInfo: o,
chainId: o,
permitInfo: a,
provider: r,
account: i,
eip2162Utils: s,
nonce: c
} = a, d = t.address, m = t.name || d, l = i || h.address, u = c === void 0 ? await s.getTokenNonce(d, l) : c, w = I(), k = b, T = o.type === "eip-2612" ? await v({
eip2162Utils: s,
eip2162Utils: c,
nonce: l
} = t, d = n.address, u = a.name || n.name;
if (!q(a))
throw new Error(`Trying to generate permit hook for unsupported token: ${d}`);
if (!u)
throw new Error(`No token name for token: ${d}`);
const m = i || w.address, s = l === void 0 ? await c.getTokenNonce(d, m) : l, p = T(), I = S, E = a.type === "eip-2612" ? await L({
eip2162Utils: c,
callDataParams: [{
owner: l,
owner: m,
spender: e,
value: k,
nonce: u,
deadline: w
}, n, m, d, o.version]
}) : await S({
eip2162Utils: s,
value: I,
nonce: s,
deadline: p
}, o, u, d, a.version]
}) : await $({
eip2162Utils: c,
callDataParams: [{
holder: l,
holder: m,
spender: e,
allowed: !0,
value: k,
nonce: u,
expiry: w
}, n, m, d, o.version]
}), L = await V(T, l, d, r, !!i);
value: I,
nonce: s,
expiry: p
}, o, u, d, a.version]
}), R = await j(E, m, d, r, !!i);
return {
target: d,
callData: T,
gasLimit: L
callData: E,
gasLimit: R
};
}
async function V(a, t, e, n, o) {
async function j(t, n, e, o, a) {
try {
const r = await n.estimateGas({
data: a,
from: t,
const r = await o.estimateGas({
data: t,
from: n,
to: e
}), i = o ? r : r.add(r.div(10));
return i.gt(p) ? i.toString() : p;
}), i = a ? r : r.add(r.div(10));
return i.gt(P) ? i.toString() : P;
} catch (r) {
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", r), p;
return console.debug("[calculatePermitGasLimit] Failed to estimateGas, using default", r), P;
}
}
function B(a) {
function Q(t) {
const {
inputToken: t,
inputToken: n,
chainId: e,
account: n
} = a;
return `${t.address.toLowerCase()}-${e}${n ? `-${n.toLowerCase()}` : ""}`;
account: o
} = t;
return `${n.address.toLowerCase()}-${e}${o ? `-${o.toLowerCase()}` : ""}`;
}
class q {
constructor(t, e) {
this.provider = t, this.walletSigner = e;
function N(t, n, e) {
return new O(t, n, e);
}
class Y {
constructor(n, e) {
this.provider = n, this.walletSigner = e;
}
getContract(t, e, n) {
return new H(t, e, n);
contractEncodeABI(n, e, o, a) {
return N(e || "", n, this.provider).interface.encodeFunctionData(o, a);
}
contractEncodeABI(t, e, n, o) {
return this.getContract(e || "", t, this.provider).interface.encodeFunctionData(n, o);
signTypedData(n, e, o) {
const a = Object.keys(e.types).reduce((i, c) => (c !== "EIP712Domain" && (i[c] = e.types[c]), i), {});
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, a, e.message);
}
signTypedData(t, e, n) {
const o = Object.keys(e.types).reduce((i, s) => (s !== "EIP712Domain" && (i[s] = e.types[s]), i), {});
return (this.walletSigner || this.provider.getSigner())._signTypedData(e.domain, o, e.message);
}
ethCall(t, e) {
ethCall(n, e) {
return this.provider.call({
to: t,
to: n,
data: e
});
}
decodeABIParameter(t, e) {
return E.decode([t], e)[0];
decodeABIParameter(n, e) {
return y.decode([n], e)[0];
}
decodeABIParameters(t, e) {
const n = E.decode(t, e);
if (n && typeof n == "object") {
const o = {};
return Object.keys(n).forEach((r) => {
const i = n[r];
$.isBigNumber(i) ? o[r] = i.toHexString() : o[r] = i;
}), o;
decodeABIParameters(n, e) {
const o = y.decode(n, e);
if (o && typeof o == "object") {
const a = {};
return Object.keys(o).forEach((r) => {
const i = o[r];
F.isBigNumber(i) ? a[r] = i.toHexString() : a[r] = i;
}), a;
}
return n;
return o;
}
}
const C = /* @__PURE__ */ new Map(), _ = /* @__PURE__ */ new Map();
function W(a, t, e) {
const n = C.get(a);
if (!e && n)
return n;
const o = `${a}-${e}`, r = _.get(o);
const C = /* @__PURE__ */ new Map(), b = /* @__PURE__ */ new Map();
function z(t, n, e) {
const o = C.get(t);
if (!e && o)
return o;
const a = `${t}-${e}`, r = b.get(a);
if (r)
return r;
const i = new q(t, e ? void 0 : h), s = new R(i);
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${a}-${e}`, s), _.set(o, s)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${a}`, s), C.set(a, s)), s;
const i = new Y(n, e ? void 0 : w), c = new U(i);
return e ? (console.log(`[getPermitUtilsInstance] Set cached provider utils for chain ${t}-${e}`, c), b.set(a, c)) : (console.log(`[getPermitUtilsInstance] Set cached chain utils for chain ${t}`, c), C.set(t, c)), c;
}
const j = {
value: b,
const J = [
{
constant: !0,
inputs: [],
name: "name",
outputs: [
{
name: "",
type: "string"
}
],
payable: !1,
stateMutability: "view",
type: "function"
}
];
async function X(t, n, e) {
const o = x(t);
return N(o, J, e).callStatic.name();
}
const Z = {
value: S,
nonce: 0,
deadline: I()
}, Q = {
deadline: T()
}, ee = {
allowed: !0,
nonce: 0,
expiry: I()
}, y = {};
async function ie(a) {
expiry: T()
}, D = {}, g = {
type: "unsupported"
};
async function fe(t) {
const {
tokenAddress: t,
tokenAddress: n,
chainId: e
} = a, n = `${e}-${t.toLowerCase()}`, o = y[n];
if (o)
return o;
const r = Y(a);
return y[n] = r, r;
} = t, o = `${e}-${n.toLowerCase()}`, a = D[o];
if (a)
return a;
const r = te(t);
return D[o] = r, r;
}
async function Y(a) {
async function te(t) {
const {
spender: t,
spender: n,
tokenAddress: e,
tokenName: n,
chainId: o,
tokenName: o,
chainId: a,
provider: r
} = a, i = W(o, r), s = h.address;
let c;
} = t, i = z(a, r), c = w.address;
let l = o;
try {
c = await i.getTokenNonce(e, s);
} catch (l) {
return l === "nonce not supported" || l.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e}`, l?.message || l), !1) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e}`, l), {
error: l.message || l.toString()
l = await X(e, a, r);
} catch (s) {
if (/ETIMEDOUT/.test(s) && !l)
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}'`, s);
}
if (!l)
return {
error: `Token name could not be determined for ${e}`
};
let d;
try {
d = await i.getTokenNonce(e, c);
} catch (s) {
return s === "nonce not supported" || s.message === "nonce is NaN" ? (console.debug(`[checkTokenIsPermittable] Not a permittable token ${e} - ${l}`, s?.message || s), {
...g,
name: l
}) : (console.debug(`[checkTokenIsPermittable] Failed to get nonce for ${e} - ${l}`, s), {
error: s.message || s.toString()
});
}
let d;
if (!G.has(e))
let u;
if (!B.has(e))
try {
d = await i.getTokenVersion(e);
} catch (l) {
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e}`, l);
u = await i.getTokenVersion(e);
} catch (s) {
console.debug(`[checkTokenIsPermittable] Failed to get version for ${e} - ${l}`, s);
}
const m = {
chainId: o,
chainId: a,
eip2612PermitUtils: i,
nonce: c,
spender: t,
nonce: d,
spender: n,
tokenAddress: e,
tokenName: n,
walletAddress: s,
version: d
tokenName: l,
walletAddress: c,
version: u
};
try {
return await A({
return await _({
...m,

@@ -232,13 +287,17 @@ type: "eip-2612",

});
} catch (l) {
console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e}`, l);
} catch (s) {
try {
return await A({
return await re(e, i) ? await _({
...m,
type: "dai-like",
provider: r
}) : /invalid signature/.test(s) || s?.code === "UNPREDICTABLE_GAS_LIMIT" ? (console.debug(`[checkTokenIsPermittable] Token ${e} - ${l} might be permittable, but it's not supported for now. Reason:`, s?.reason), {
...g,
name: l
}) : (console.debug(`[checkTokenIsPermittable] Failed to estimate eip-2612 permit for ${e} - ${l}`, s), {
error: s.message || s.toString()
});
} catch (u) {
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e}`, u), {
error: u.message || u.toString()
} catch (p) {
return console.debug(`[checkTokenIsPermittable] Failed to estimate dai-like permit for ${e} - ${l}`, p), {
error: p.message || p.toString()
};

@@ -248,67 +307,79 @@ }

}
async function A(a) {
async function _(t) {
const {
provider: t,
provider: n,
chainId: e,
walletAddress: n,
tokenAddress: o,
type: r,
version: i
} = a, c = await (r === "eip-2612" ? z : J)(a);
return c && (await t.estimateGas({
data: c,
from: n,
to: o
})).toNumber() > O[e] ? {
type: r,
version: i
} : !1;
walletAddress: o,
tokenAddress: a,
tokenName: r,
type: i,
version: c
} = t, d = await (i === "eip-2612" ? ne : oe)(t);
return d ? (await n.estimateGas({
data: d,
from: o,
to: a
})).toNumber() > K[e] ? {
type: i,
version: c,
name: r
} : {
...g,
name: r
} : {
...g,
name: r
};
}
async function z(a) {
async function ne(t) {
const {
eip2612PermitUtils: t,
eip2612PermitUtils: n,
walletAddress: e,
spender: n,
nonce: o,
spender: o,
nonce: a,
chainId: r,
tokenName: i,
tokenAddress: s,
version: c
} = a;
return v({
eip2162Utils: t,
tokenAddress: c,
version: l
} = t;
return L({
eip2162Utils: n,
callDataParams: [{
...j,
...Z,
owner: e,
spender: n,
nonce: o
}, +r, i, s, c]
spender: o,
nonce: a
}, +r, i, c, l]
});
}
async function J(a) {
async function re(t, n) {
return await n.getPermitTypeHash(t) === A;
}
async function oe(t) {
const {
eip2612PermitUtils: t,
eip2612PermitUtils: n,
tokenAddress: e,
walletAddress: n,
spender: o,
walletAddress: o,
spender: a,
nonce: r,
chainId: i,
tokenName: s,
version: c
} = a;
return await t.getPermitTypeHash(e) === U ? S({
eip2162Utils: t,
tokenName: c,
version: l
} = t;
return await n.getPermitTypeHash(e) === A ? $({
eip2162Utils: n,
callDataParams: [{
...Q,
holder: n,
spender: o,
...ee,
holder: o,
spender: a,
nonce: r
}, i, s, e, c]
}, i, c, e, l]
}) : !1;
}
export {
ae as checkIsCallDataAValidPermit,
oe as generatePermitHook,
W as getPermitUtilsInstance,
ie as getTokenPermitInfo
ue as checkIsCallDataAValidPermit,
pe as generatePermitHook,
z as getPermitUtilsInstance,
fe as getTokenPermitInfo,
q as isSupportedPermitInfo
};
import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
import { SupportedPermitInfo } from '../types';
export declare function checkIsCallDataAValidPermit(owner: string, chainId: number, eip2162Utils: Eip2612PermitUtils, tokenAddress: string, tokenName: string, callData: string, { version }: SupportedPermitInfo): Promise<boolean | undefined>;
import { PermitInfo } from '../types';
export declare function checkIsCallDataAValidPermit(owner: string, chainId: number, eip2162Utils: Eip2612PermitUtils, tokenAddress: string, _tokenName: string | undefined, callData: string, { version, type, name }: PermitInfo): Promise<boolean | undefined>;
{
"name": "@cowprotocol/permit-utils",
"version": "0.0.1-RC.1",
"version": "0.0.1",
"type": "module",

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

import { Eip2612PermitUtils } from '@1inch/permit-signed-approvals-utils';
import { latest } from '@cowprotocol/app-data';
import { JsonRpcProvider } from '@ethersproject/providers';
export type PermitType = 'dai-like' | 'eip-2612';
export type SupportedPermitInfo = {
export type PermitType = 'dai-like' | 'eip-2612' | 'unsupported';
export type PermitInfo = {
type: PermitType;
version: string | undefined;
name?: string;
version?: string | undefined;
};
type UnsupportedPermitInfo = false;
export type PermitInfo = SupportedPermitInfo | UnsupportedPermitInfo;
type TokenInfo = {

@@ -19,3 +18,3 @@ address: string;

chainId: number;
permitInfo: SupportedPermitInfo;
permitInfo: PermitInfo;
provider: JsonRpcProvider;

@@ -30,3 +29,3 @@ eip2162Utils: Eip2612PermitUtils;

};
export type GetTokenPermitIntoResult = SupportedPermitInfo | FailedToIdentify | UnsupportedPermitInfo;
export type GetTokenPermitIntoResult = PermitInfo | FailedToIdentify;
type BasePermitCallDataParams = {

@@ -44,3 +43,3 @@ eip2162Utils: Eip2612PermitUtils;

tokenAddress: string;
tokenName: string;
tokenName?: string | undefined;
chainId: number;

@@ -47,0 +46,0 @@ provider: JsonRpcProvider;

@@ -8,3 +8,2 @@ import type { JsonRpcProvider } from '@ethersproject/providers';

constructor(provider: JsonRpcProvider, walletSigner?: Wallet | undefined);
private getContract;
contractEncodeABI(abi: AbiItem[], address: string | null, methodName: string, methodParams: unknown[]): string;

@@ -11,0 +10,0 @@ signTypedData(_walletAddress: string, typedData: EIP712TypedData, _typedDataHash: string): Promise<string>;

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