@cowprotocol/permit-utils 3.0.0 → 3.0.2
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/1inchPermitUtils-BDO_CqjL.cjs +1 -0
- package/1inchPermitUtils-CXu6m1-9.js +4 -0
- package/abi/versionAbis.d.ts +15 -0
- package/const.d.ts +24 -0
- package/consts/1inchPermitUtils.d.ts +25 -0
- package/{src/imports/1inchPermitUtils.ts → imports/1inchPermitUtils.d.ts} +1 -3
- package/index.cjs +1 -0
- package/{src/index.ts → index.d.ts} +8 -11
- package/index.js +547 -0
- package/lib/checkIsCallDataAValidPermit.d.ts +3 -0
- package/lib/generatePermitHook.d.ts +2 -0
- package/lib/getPermitUtilsInstance.d.ts +3 -0
- package/lib/getTokenPermitInfo.d.ts +2 -0
- package/package.json +19 -7
- package/types.d.ts +47 -0
- package/utils/PermitProviderConnector.d.ts +13 -0
- package/utils/buildPermitCallData.d.ts +3 -0
- package/utils/getContract.d.ts +3 -0
- package/utils/getEip712Domain.d.ts +13 -0
- package/utils/getPermitDeadline.d.ts +1 -0
- package/utils/getTokenName.d.ts +2 -0
- package/utils/getTokenPermitVersion.d.ts +2 -0
- package/utils/isSupportedPermitInfo.d.ts +2 -0
- package/CHANGELOG.md +0 -308
- package/jest.config.ts +0 -11
- package/project.json +0 -42
- package/src/abi/erc20.json +0 -59
- package/src/abi/versionAbis.ts +0 -37
- package/src/const.ts +0 -20
- package/src/consts/1inchPermitUtils.ts +0 -68
- package/src/lib/checkIsCallDataAValidPermit.ts +0 -64
- package/src/lib/generatePermitHook.ts +0 -133
- package/src/lib/getPermitUtilsInstance.ts +0 -51
- package/src/lib/getTokenPermitInfo.ts +0 -281
- package/src/types.ts +0 -61
- package/src/utils/PermitProviderConnector.ts +0 -75
- package/src/utils/buildPermitCallData.ts +0 -26
- package/src/utils/getContract.ts +0 -6
- package/src/utils/getEip712Domain.ts +0 -70
- package/src/utils/getPermitDeadline.ts +0 -7
- package/src/utils/getTokenName.ts +0 -13
- package/src/utils/getTokenPermitVersion.ts +0 -35
- package/src/utils/isSupportedPermitInfo.ts +0 -5
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -10
- package/tsconfig.spec.json +0 -9
- package/vite.config.mts +0 -51
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { defaultAbiCoder, ParamType } from '@ethersproject/abi'
|
|
2
|
-
import { TypedDataField } from '@ethersproject/abstract-signer'
|
|
3
|
-
import { BigNumber } from '@ethersproject/bignumber'
|
|
4
|
-
import type { JsonRpcProvider } from '@ethersproject/providers'
|
|
5
|
-
import { Wallet } from '@ethersproject/wallet'
|
|
6
|
-
|
|
7
|
-
import { getContract } from './getContract'
|
|
8
|
-
|
|
9
|
-
import type { AbiInput, AbiItem, EIP712TypedData, ProviderConnector } from '@1inch/permit-signed-approvals-utils'
|
|
10
|
-
|
|
11
|
-
export class PermitProviderConnector implements ProviderConnector {
|
|
12
|
-
constructor(
|
|
13
|
-
private provider: JsonRpcProvider,
|
|
14
|
-
private walletSigner?: Wallet | undefined,
|
|
15
|
-
) {}
|
|
16
|
-
|
|
17
|
-
contractEncodeABI(abi: AbiItem[], address: string | null, methodName: string, methodParams: unknown[]): string {
|
|
18
|
-
const contract = getContract(address || '', abi, this.provider)
|
|
19
|
-
|
|
20
|
-
return contract.interface.encodeFunctionData(methodName, methodParams)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
signTypedData(_walletAddress: string, typedData: EIP712TypedData, _typedDataHash: string): Promise<string> {
|
|
24
|
-
// Removes `EIP712Domain` as it's already part of EIP712 (see https://ethereum.stackexchange.com/a/151930/55204)
|
|
25
|
-
// and EthersJS complains when a type is not needed (see https://github.com/ethers-io/ethers.js/discussions/4000)
|
|
26
|
-
const types = Object.keys(typedData.types).reduce<Record<string, TypedDataField[]>>((acc, type) => {
|
|
27
|
-
if (type !== 'EIP712Domain') {
|
|
28
|
-
acc[type] = typedData.types[type]
|
|
29
|
-
}
|
|
30
|
-
return acc
|
|
31
|
-
}, {})
|
|
32
|
-
|
|
33
|
-
const signer = this.walletSigner || this.provider.getSigner()
|
|
34
|
-
|
|
35
|
-
return signer._signTypedData(typedData.domain, types, typedData.message)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
ethCall(contractAddress: string, callData: string): Promise<string> {
|
|
39
|
-
return this.provider.call({
|
|
40
|
-
to: contractAddress,
|
|
41
|
-
data: callData,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
decodeABIParameter<T>(type: string, hex: string): T {
|
|
46
|
-
return defaultAbiCoder.decode([type], hex)[0]
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
decodeABIParameters<T>(types: AbiInput[], hex: string): T {
|
|
50
|
-
const decodedValues = defaultAbiCoder.decode(types as unknown as (ParamType | string)[], hex) as T
|
|
51
|
-
|
|
52
|
-
// Ethersjs decodes numbers as BigNumber instances
|
|
53
|
-
// However, 1inch utils do not deal with BigNumber instances,
|
|
54
|
-
// so we need this mess to convert them to hex strings, which 1inch understands
|
|
55
|
-
// TODO: Any way to make this typing mess any cleaner?
|
|
56
|
-
if (decodedValues && typeof decodedValues === 'object') {
|
|
57
|
-
const copy: Record<string, unknown> = {}
|
|
58
|
-
|
|
59
|
-
Object.keys(decodedValues).forEach((key) => {
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
const value = decodedValues[key]
|
|
62
|
-
if (BigNumber.isBigNumber(value)) {
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
copy[key] = value.toHexString()
|
|
65
|
-
} else {
|
|
66
|
-
copy[key] = value
|
|
67
|
-
}
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
return copy as T
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return decodedValues
|
|
74
|
-
}
|
|
75
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { oneInchPermitUtilsConsts } from '..'
|
|
2
|
-
import { BuildDaiLikePermitCallDataParams, BuildEip2612PermitCallDataParams } from '../types'
|
|
3
|
-
|
|
4
|
-
export async function buildEip2612PermitCallData({
|
|
5
|
-
eip2612Utils,
|
|
6
|
-
callDataParams,
|
|
7
|
-
}: BuildEip2612PermitCallDataParams): Promise<string> {
|
|
8
|
-
const [permitParams, chainId, tokenName, ...rest] = callDataParams
|
|
9
|
-
|
|
10
|
-
const callData = await eip2612Utils.buildPermitCallData(permitParams, chainId, tokenName, ...rest)
|
|
11
|
-
// For some reason, the method above removes the permit selector prefix
|
|
12
|
-
// https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.utils.ts#L92
|
|
13
|
-
// Adding it back
|
|
14
|
-
return callData.replace('0x', oneInchPermitUtilsConsts.EIP_2612_PERMIT_SELECTOR)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export async function buildDaiLikePermitCallData({
|
|
18
|
-
eip2612Utils,
|
|
19
|
-
callDataParams,
|
|
20
|
-
}: BuildDaiLikePermitCallDataParams): Promise<string> {
|
|
21
|
-
const callData = await eip2612Utils.buildDaiLikePermitCallData(...callDataParams)
|
|
22
|
-
|
|
23
|
-
// Same as above, but for dai like
|
|
24
|
-
// https://github.com/1inch/permit-signed-approvals-utils/blob/master/src/eip-2612-permit.utils.ts#L140
|
|
25
|
-
return callData.replace('0x', oneInchPermitUtilsConsts.DAI_PERMIT_SELECTOR)
|
|
26
|
-
}
|
package/src/utils/getContract.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Contract, ContractInterface } from '@ethersproject/contracts'
|
|
2
|
-
import type { JsonRpcProvider } from '@ethersproject/providers'
|
|
3
|
-
|
|
4
|
-
export function getContract(address: string, abi: ContractInterface, provider: JsonRpcProvider): Contract {
|
|
5
|
-
return new Contract(address, abi, provider)
|
|
6
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { getAddress } from '@ethersproject/address'
|
|
2
|
-
import type { JsonRpcProvider } from '@ethersproject/providers'
|
|
3
|
-
|
|
4
|
-
import { getContract } from './getContract'
|
|
5
|
-
|
|
6
|
-
import Erc20Abi from '../abi/erc20.json'
|
|
7
|
-
|
|
8
|
-
// See https://eips.ethereum.org/EIPS/eip-5267
|
|
9
|
-
|
|
10
|
-
export async function getEip712Domain(
|
|
11
|
-
tokenAddress: string,
|
|
12
|
-
chainId: number,
|
|
13
|
-
provider: JsonRpcProvider,
|
|
14
|
-
): Promise<Eip712Domain> {
|
|
15
|
-
const formattedAddress = getAddress(tokenAddress)
|
|
16
|
-
const erc20Contract = getContract(formattedAddress, Erc20Abi, provider)
|
|
17
|
-
|
|
18
|
-
const eip5267Domain: Required<Eip5267Return> = await erc20Contract.callStatic['eip712Domain']()
|
|
19
|
-
|
|
20
|
-
return processDomain(eip5267Domain, chainId, tokenAddress)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
type Eip5267Return = {
|
|
24
|
-
fields: string
|
|
25
|
-
name?: string
|
|
26
|
-
version?: string
|
|
27
|
-
chainId?: string
|
|
28
|
-
verifyingContract?: string
|
|
29
|
-
salt?: string
|
|
30
|
-
extensions?: string[]
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type Eip712Domain = Omit<Eip5267Return, 'fields' | 'extensions'>
|
|
34
|
-
|
|
35
|
-
/** Retrieves the EIP-712 domain of a contract using EIP-5267 without extensions.
|
|
36
|
-
*
|
|
37
|
-
* Derived from https://eips.ethereum.org/EIPS/eip-5267 reference Javascript implementation
|
|
38
|
-
**/
|
|
39
|
-
async function processDomain(domain: Required<Eip5267Return>, chainId: number, address: string): Promise<Eip712Domain> {
|
|
40
|
-
const { extensions, ...rest } = domain
|
|
41
|
-
|
|
42
|
-
if (extensions.length > 0) {
|
|
43
|
-
throw Error('Extensions not implemented')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (rest.chainId && chainId !== parseInt(rest.chainId, 16)) {
|
|
47
|
-
throw Error(`ChainId mismatch. Received: '${rest.chainId}', expected: '${chainId}'`)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (rest.verifyingContract && address.toLowerCase() !== rest.verifyingContract.toLowerCase()) {
|
|
51
|
-
throw Error(`Address mismatch. Received: '${rest.verifyingContract}', expected: '${address}'`)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return buildBasicDomain(rest)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const FIELD_NAMES = ['name', 'version', 'chainId', 'verifyingContract', 'salt']
|
|
58
|
-
|
|
59
|
-
/** Builds a domain object without extensions based on the return values of `eip712Domain()`. */
|
|
60
|
-
function buildBasicDomain(domain: Required<Omit<Eip5267Return, 'extensions'>>): Eip712Domain {
|
|
61
|
-
// Will be a 5bit number as a hex string
|
|
62
|
-
const fields = parseInt(domain.fields, 16)
|
|
63
|
-
|
|
64
|
-
return FIELD_NAMES.reduce<Eip712Domain>((acc, field, i) => {
|
|
65
|
-
if (fields & (1 << i)) {
|
|
66
|
-
acc[field as keyof Eip712Domain] = domain[field as keyof Omit<Eip5267Return, 'extensions'>]
|
|
67
|
-
}
|
|
68
|
-
return acc
|
|
69
|
-
}, {})
|
|
70
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { DEFAULT_PERMIT_DURATION } from '../const'
|
|
2
|
-
|
|
3
|
-
// TODO: Add proper return type annotation
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
5
|
-
export function getPermitDeadline() {
|
|
6
|
-
return Math.ceil((Date.now() + DEFAULT_PERMIT_DURATION) / 1000)
|
|
7
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { getAddress } from '@ethersproject/address'
|
|
2
|
-
import type { JsonRpcProvider } from '@ethersproject/providers'
|
|
3
|
-
|
|
4
|
-
import { getContract } from './getContract'
|
|
5
|
-
|
|
6
|
-
import Erc20Abi from '../abi/erc20.json'
|
|
7
|
-
|
|
8
|
-
export async function getTokenName(tokenAddress: string, chainId: number, provider: JsonRpcProvider): Promise<string> {
|
|
9
|
-
const formattedAddress = getAddress(tokenAddress)
|
|
10
|
-
const erc20Contract = getContract(formattedAddress, Erc20Abi, provider)
|
|
11
|
-
|
|
12
|
-
return erc20Contract.callStatic['name']()
|
|
13
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { defaultAbiCoder } from '@ethersproject/abi'
|
|
2
|
-
import type { JsonRpcProvider } from '@ethersproject/providers'
|
|
3
|
-
|
|
4
|
-
import { getContract } from './getContract'
|
|
5
|
-
|
|
6
|
-
import { VERSION_ABIS } from '../abi/versionAbis'
|
|
7
|
-
|
|
8
|
-
export async function getTokenPermitVersion(
|
|
9
|
-
tokenAddress: string,
|
|
10
|
-
provider: JsonRpcProvider,
|
|
11
|
-
index = 0,
|
|
12
|
-
): Promise<string> {
|
|
13
|
-
const abi = VERSION_ABIS[index]
|
|
14
|
-
|
|
15
|
-
if (!abi) {
|
|
16
|
-
return '1'
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
const contract = getContract(tokenAddress, abi, provider)
|
|
21
|
-
const data = contract.interface.encodeFunctionData(abi[0].name)
|
|
22
|
-
const response = await provider.call({ to: tokenAddress, data })
|
|
23
|
-
|
|
24
|
-
if (response === '0x' || Number.isNaN(Number(response))) {
|
|
25
|
-
return '1'
|
|
26
|
-
}
|
|
27
|
-
if (!response.startsWith('0x')) {
|
|
28
|
-
return response
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return defaultAbiCoder.decode(['string'], response).toString()
|
|
32
|
-
} catch {
|
|
33
|
-
return getTokenPermitVersion(tokenAddress, provider, index + 1)
|
|
34
|
-
}
|
|
35
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"forceConsistentCasingInFileNames": true,
|
|
6
|
-
"strict": true,
|
|
7
|
-
"noImplicitOverride": true,
|
|
8
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
9
|
-
"noImplicitReturns": true,
|
|
10
|
-
"noFallthroughCasesInSwitch": true
|
|
11
|
-
},
|
|
12
|
-
"files": [],
|
|
13
|
-
"include": [],
|
|
14
|
-
"references": [
|
|
15
|
-
{
|
|
16
|
-
"path": "./tsconfig.lib.json"
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
"path": "./tsconfig.spec.json"
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
}
|
package/tsconfig.lib.json
DELETED
package/tsconfig.spec.json
DELETED
package/vite.config.mts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/// <reference types="vitest" />
|
|
2
|
-
import { defineConfig } from 'vite'
|
|
3
|
-
import macrosPlugin from 'vite-plugin-babel-macros'
|
|
4
|
-
import dts from 'vite-plugin-dts'
|
|
5
|
-
import viteTsConfigPaths from 'vite-tsconfig-paths'
|
|
6
|
-
|
|
7
|
-
import * as path from 'path'
|
|
8
|
-
|
|
9
|
-
export default defineConfig({
|
|
10
|
-
root: path.resolve(__dirname, './'),
|
|
11
|
-
cacheDir: '../../../node_modules/.vite/permit-utils',
|
|
12
|
-
|
|
13
|
-
plugins: [
|
|
14
|
-
dts({
|
|
15
|
-
entryRoot: 'src',
|
|
16
|
-
tsconfigPath: path.join(__dirname, 'tsconfig.lib.json'),
|
|
17
|
-
}),
|
|
18
|
-
|
|
19
|
-
macrosPlugin(),
|
|
20
|
-
|
|
21
|
-
viteTsConfigPaths({
|
|
22
|
-
root: '../../../',
|
|
23
|
-
}),
|
|
24
|
-
],
|
|
25
|
-
// Uncomment this if you are using workers.
|
|
26
|
-
// worker: {
|
|
27
|
-
// plugins: [
|
|
28
|
-
// viteTsConfigPaths({
|
|
29
|
-
// root: '../../',
|
|
30
|
-
// }),
|
|
31
|
-
// ],
|
|
32
|
-
// },
|
|
33
|
-
|
|
34
|
-
// Configuration for building your library.
|
|
35
|
-
// See: https://vitejs.dev/guide/build.html#library-mode
|
|
36
|
-
build: {
|
|
37
|
-
lib: {
|
|
38
|
-
// Could also be a dictionary or array of multiple entry points.
|
|
39
|
-
entry: 'src/index.ts',
|
|
40
|
-
name: 'permit-utils',
|
|
41
|
-
fileName: 'index',
|
|
42
|
-
// Change this to the formats you want to support.
|
|
43
|
-
// Don't forget to update your package.json as well.
|
|
44
|
-
formats: ['es', 'cjs'],
|
|
45
|
-
},
|
|
46
|
-
rollupOptions: {
|
|
47
|
-
// External packages that should not be bundled into your library.
|
|
48
|
-
external: [/@1inch/, /@cowprotocol/, /@ethersproject/],
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
})
|