@lifi/sdk-provider-ethereum 4.0.12 → 4.0.13

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cjs/actions/checkPermitSupport.js +2 -1
  3. package/dist/cjs/actions/checkPermitSupport.js.map +1 -1
  4. package/dist/cjs/core/tasks/EthereumNativePermitTask.js +2 -1
  5. package/dist/cjs/core/tasks/EthereumNativePermitTask.js.map +1 -1
  6. package/dist/cjs/permits/acceptsRawEcdsaSignature.d.ts +18 -0
  7. package/dist/cjs/permits/acceptsRawEcdsaSignature.js +58 -0
  8. package/dist/cjs/permits/acceptsRawEcdsaSignature.js.map +1 -0
  9. package/dist/cjs/permits/canAccountUseNativePermits.d.ts +28 -6
  10. package/dist/cjs/permits/canAccountUseNativePermits.js +30 -10
  11. package/dist/cjs/permits/canAccountUseNativePermits.js.map +1 -1
  12. package/dist/cjs/permits/canAccountUsePermit2.js +2 -57
  13. package/dist/cjs/permits/canAccountUsePermit2.js.map +1 -1
  14. package/dist/cjs/permits/getNativePermit.d.ts +7 -1
  15. package/dist/cjs/permits/getNativePermit.js +12 -9
  16. package/dist/cjs/permits/getNativePermit.js.map +1 -1
  17. package/dist/cjs/version.d.ts +1 -1
  18. package/dist/cjs/version.js +1 -1
  19. package/dist/cjs/version.js.map +1 -1
  20. package/dist/esm/actions/checkPermitSupport.js +2 -1
  21. package/dist/esm/actions/checkPermitSupport.js.map +1 -1
  22. package/dist/esm/core/tasks/EthereumNativePermitTask.js +2 -1
  23. package/dist/esm/core/tasks/EthereumNativePermitTask.js.map +1 -1
  24. package/dist/esm/permits/acceptsRawEcdsaSignature.d.ts +18 -0
  25. package/dist/esm/permits/acceptsRawEcdsaSignature.d.ts.map +1 -0
  26. package/dist/esm/permits/acceptsRawEcdsaSignature.js +57 -0
  27. package/dist/esm/permits/acceptsRawEcdsaSignature.js.map +1 -0
  28. package/dist/esm/permits/canAccountUseNativePermits.d.ts +28 -6
  29. package/dist/esm/permits/canAccountUseNativePermits.d.ts.map +1 -1
  30. package/dist/esm/permits/canAccountUseNativePermits.js +30 -10
  31. package/dist/esm/permits/canAccountUseNativePermits.js.map +1 -1
  32. package/dist/esm/permits/canAccountUsePermit2.d.ts.map +1 -1
  33. package/dist/esm/permits/canAccountUsePermit2.js +1 -56
  34. package/dist/esm/permits/canAccountUsePermit2.js.map +1 -1
  35. package/dist/esm/permits/getNativePermit.d.ts +7 -1
  36. package/dist/esm/permits/getNativePermit.d.ts.map +1 -1
  37. package/dist/esm/permits/getNativePermit.js +12 -9
  38. package/dist/esm/permits/getNativePermit.js.map +1 -1
  39. package/dist/esm/version.d.ts +1 -1
  40. package/dist/esm/version.js +1 -1
  41. package/dist/esm/version.js.map +1 -1
  42. package/package.json +3 -3
  43. package/src/actions/checkPermitSupport.ts +8 -2
  44. package/src/core/tasks/EthereumNativePermitTask.ts +1 -0
  45. package/src/permits/acceptsRawEcdsaSignature.ts +73 -0
  46. package/src/permits/canAccountUseNativePermits.ts +38 -18
  47. package/src/permits/canAccountUsePermit2.ts +2 -76
  48. package/src/permits/getNativePermit.ts +23 -9
  49. package/src/version.ts +1 -1
@@ -0,0 +1,73 @@
1
+ import type { SDKClient } from '@lifi/sdk'
2
+ import { type Address, encodeFunctionData, type Hex } from 'viem'
3
+ import { call } from 'viem/actions'
4
+ import { getAction } from 'viem/utils'
5
+ import { getPublicClient } from '../client/publicClient.js'
6
+
7
+ const ERC1271_ABI = [
8
+ {
9
+ name: 'isValidSignature',
10
+ type: 'function',
11
+ stateMutability: 'view',
12
+ inputs: [{ type: 'bytes32' }, { type: 'bytes' }],
13
+ outputs: [{ type: 'bytes4' }],
14
+ },
15
+ ] as const
16
+
17
+ /**
18
+ * A real 65-byte signature over {@link PROBE_HASH} from the public key `0x01…01`,
19
+ * recovering to a third party — never the account under test. Throwaway probe
20
+ * value, not a credential.
21
+ *
22
+ * MUST stay recoverable. An unrecoverable signature makes implementations that
23
+ * revert (rather than return a failure value) look like they rejected the
24
+ * envelope, misclassifying accounts that would have accepted ours.
25
+ */
26
+ const PROBE_HASH = `0x${'ab'.repeat(32)}` as Hex
27
+ const PROBE_SIGNATURE =
28
+ '0x997c61aa10e2330c076d6ba7abca1e71703a8f039291db103a62e085c9b2cf0d3f7e31ec9028229acc83093fbb722fa700fd6e4ea15d584df1bf384eb0051e0d1c' as Hex
29
+
30
+ /**
31
+ * Asks an account with code whether it evaluates a plain 65-byte ECDSA
32
+ * signature: a return means it recovered a signer and merely found a mismatch,
33
+ * so the real root-key signature will verify, while a revert means it rejected
34
+ * the envelope and nothing we produce will. Keyed on revert-vs-return, never on
35
+ * the value — the probe signature recovers to a third party on purpose, so a
36
+ * correct implementation returns a *non*-magic value, and comparing against the
37
+ * magic value would reject every wallet this exists to accept. Only call it for
38
+ * accounts that have code; a code-less address returns empty data and would read
39
+ * as incapable.
40
+ */
41
+ export const acceptsRawEcdsaSignature = async (
42
+ client: SDKClient,
43
+ chainId: number,
44
+ address: Address
45
+ ): Promise<boolean> => {
46
+ try {
47
+ const publicClient = await getPublicClient(client, chainId)
48
+ const { data } = await getAction(
49
+ // CCIP-Read off, or an `OffchainLookup` revert comes back as a return and
50
+ // inverts the signal read below. viem gates it on the client, not the
51
+ // call, so clone rather than disable it on the shared ENS-using client.
52
+ // Relies on `getPublicClient` staying bare: add `publicActions` and
53
+ // `getAction` prefers a decorated `client.call`, re-enabling CCIP-Read.
54
+ { ...publicClient, ccipRead: false },
55
+ call,
56
+ 'call'
57
+ )({
58
+ to: address,
59
+ data: encodeFunctionData({
60
+ abi: ERC1271_ABI,
61
+ functionName: 'isValidSignature',
62
+ args: [PROBE_HASH, PROBE_SIGNATURE],
63
+ }),
64
+ })
65
+ // A full 32-byte word (`0x` + 64 chars) or nothing: verifiers compare against
66
+ // a magic value, so anything shorter can only be a false positive.
67
+ return !!data && data.length >= 66
68
+ } catch {
69
+ // Revert, or an RPC failure we can't tell apart from one. Either way we
70
+ // must not promise the signature will verify.
71
+ return false
72
+ }
73
+ }
@@ -1,31 +1,51 @@
1
1
  import type { SDKClient } from '@lifi/sdk'
2
- import type { Client } from 'viem'
2
+ import type { Address } from 'viem'
3
3
  import { getAccountCode } from '../actions/getAccountCode.js'
4
- import { isSmartContractWalletCode } from '../actions/isSmartContractWallet.js'
4
+ import { acceptsRawEcdsaSignature } from './acceptsRawEcdsaSignature.js'
5
+ import { isDelegationDesignatorCode } from './isDelegationDesignatorCode.js'
5
6
 
6
7
  /**
7
- * Whether the account can use native EIP-2612 permits — true for EOAs and
8
- * EIP-7702 delegated EOAs (both produce ECDSA signatures), false for
9
- * smart-contract wallets. Conservatively false on missing chain id or RPC
10
- * failure ("if unsure, don't use permits").
8
+ * Whether the account can produce a signature an EIP-2612 `permit` will accept.
9
+ *
10
+ * Signing ECDSA is not the question — verification is. Tokens whose `permit`
11
+ * routes through a `SignatureChecker` branch on `owner.code.length` exactly as
12
+ * Permit2 does, so an owner with code takes the EIP-1271 path where strict
13
+ * delegates reject a bare signature (Circle's `FiatTokenV2_2`, i.e. USDC), hence
14
+ * the {@link acceptsRawEcdsaSignature} probe for EIP-7702 delegates.
15
+ *
16
+ * Other contract accounts stay blocked on shape alone and are never probed.
17
+ * `encodeNativePermitData` splits the signature with `parseSignature`, so this
18
+ * path needs 65 ECDSA bytes — which a 7702 delegate signing with its root key
19
+ * always produces, and a contract wallet generally does not. Probing them would
20
+ * admit any implementation that *returns* a failure value rather than reverting,
21
+ * skip the approval, then throw after the user had already signed.
22
+ *
23
+ * The probe describes the account while the verifier lives in the token, so a
24
+ * strict delegate spending a plain-`ecrecover` token is refused a permit it
25
+ * could have used — an approval rather than a revert, and the token's path is
26
+ * not reliably introspectable. `false` on RPC failure.
27
+ *
28
+ * Takes the chain and owner explicitly, mirroring {@link canAccountUsePermit2}:
29
+ * reading them off a wallet client silently reported "no native permit" for
30
+ * every caller that falls back to a public client.
11
31
  */
12
32
  export const canAccountUseNativePermits = async (
13
33
  client: SDKClient,
14
- viemClient: Client
34
+ { chainId, address }: { chainId: number; address: Address }
15
35
  ): Promise<boolean> => {
16
- const chainId = viemClient.chain?.id
17
- if (chainId === undefined) {
36
+ const code = await getAccountCode({ client, chainId, address })
37
+ // `undefined` means the lookup failed, not "no code" — `getAccountCode`
38
+ // normalizes an empty result to `'0x'` precisely so these stay distinct.
39
+ if (code === undefined) {
18
40
  return false
19
41
  }
20
- const code = await getAccountCode({
21
- client,
22
- chainId,
23
- address: viemClient.account!.address,
24
- })
25
- // Load-bearing: without this guard, `!isSmartContractWalletCode(undefined)`
26
- // would flip RPC failure from "no permits" to "use permits" (regression).
27
- if (code === undefined) {
42
+ if (code === '0x') {
43
+ return true
44
+ }
45
+ // Shape first: only 7702 delegates reach the probe. See the note above on
46
+ // `parseSignature` — deliberately narrower than the Permit2 gate.
47
+ if (!isDelegationDesignatorCode(code)) {
28
48
  return false
29
49
  }
30
- return !isSmartContractWalletCode(code)
50
+ return acceptsRawEcdsaSignature(client, chainId, address)
31
51
  }
@@ -1,81 +1,7 @@
1
1
  import type { SDKClient } from '@lifi/sdk'
2
- import { type Address, encodeFunctionData, type Hex } from 'viem'
3
- import { call } from 'viem/actions'
4
- import { getAction } from 'viem/utils'
2
+ import type { Address } from 'viem'
5
3
  import { getAccountCode } from '../actions/getAccountCode.js'
6
- import { getPublicClient } from '../client/publicClient.js'
7
-
8
- const ERC1271_ABI = [
9
- {
10
- name: 'isValidSignature',
11
- type: 'function',
12
- stateMutability: 'view',
13
- inputs: [{ type: 'bytes32' }, { type: 'bytes' }],
14
- outputs: [{ type: 'bytes4' }],
15
- },
16
- ] as const
17
-
18
- /**
19
- * A real 65-byte signature over {@link PROBE_HASH} from the public key `0x01…01`,
20
- * recovering to a third party — never the account under test. Throwaway probe
21
- * value, not a credential.
22
- *
23
- * MUST stay recoverable. An unrecoverable signature makes implementations that
24
- * revert (rather than return a failure value) look like they rejected the
25
- * envelope, misclassifying accounts that would have accepted ours.
26
- */
27
- const PROBE_HASH = `0x${'ab'.repeat(32)}` as Hex
28
- const PROBE_SIGNATURE =
29
- '0x997c61aa10e2330c076d6ba7abca1e71703a8f039291db103a62e085c9b2cf0d3f7e31ec9028229acc83093fbb722fa700fd6e4ea15d584df1bf384eb0051e0d1c' as Hex
30
-
31
- /**
32
- * Asks a code-bearing account the same question Permit2 will: does it evaluate a
33
- * plain 65-byte ECDSA signature, or reject it outright?
34
- *
35
- * - **Returns** → it recovered a signer and found a mismatch, so it will accept
36
- * the real root-key signature (MetaMask's `EIP7702StatelessDeleGator`).
37
- * - **Reverts** → it rejected the envelope before recovery and can't validate
38
- * anything we produce (Alchemy's ERC-6900 `SemiModularAccount7702`).
39
- *
40
- * Keyed on revert-vs-return, never on the value returned: the probe signature
41
- * recovers to a third party on purpose, so a correct implementation must return
42
- * a *non*-magic value, and comparing against the magic value would reject every
43
- * wallet this exists to accept. Accepted cost: an implementation that returns
44
- * failure for *everything* is classified capable and still fails on-chain.
45
- */
46
- const acceptsRawEcdsaSignature = async (
47
- client: SDKClient,
48
- chainId: number,
49
- address: Address
50
- ): Promise<boolean> => {
51
- try {
52
- const publicClient = await getPublicClient(client, chainId)
53
- const { data } = await getAction(
54
- // CCIP-Read off, or an `OffchainLookup` revert comes back as a return and
55
- // inverts the signal read below. viem gates it on the client, not the
56
- // call, so clone rather than disable it on the shared ENS-using client.
57
- // Relies on `getPublicClient` staying bare: add `publicActions` and
58
- // `getAction` prefers a decorated `client.call`, re-enabling CCIP-Read.
59
- { ...publicClient, ccipRead: false },
60
- call,
61
- 'call'
62
- )({
63
- to: address,
64
- data: encodeFunctionData({
65
- abi: ERC1271_ABI,
66
- functionName: 'isValidSignature',
67
- args: [PROBE_HASH, PROBE_SIGNATURE],
68
- }),
69
- })
70
- // A full 32-byte word (`0x` + 64 chars) or nothing: Permit2 compares against
71
- // a magic value, so anything shorter can only be a false positive.
72
- return !!data && data.length >= 66
73
- } catch {
74
- // Revert, or an RPC failure we can't tell apart from one. Either way we
75
- // must not promise Permit2 will work.
76
- return false
77
- }
78
- }
4
+ import { acceptsRawEcdsaSignature } from './acceptsRawEcdsaSignature.js'
79
5
 
80
6
  /**
81
7
  * Whether the account can produce a signature Uniswap Permit2 will accept.
@@ -29,6 +29,12 @@ type GetNativePermitParams = {
29
29
  tokenAddress: Address
30
30
  spenderAddress: Address
31
31
  amount: bigint
32
+ /**
33
+ * The address that will sign the permit. Passed explicitly rather than read
34
+ * off `viemClient.account`, which is absent whenever a caller falls back to a
35
+ * public client (`checkPermitSupport` does) — that used to throw here.
36
+ */
37
+ ownerAddress: Address
32
38
  }
33
39
 
34
40
  function makeDomainSeparator({
@@ -139,7 +145,8 @@ const getEIP712DomainData = async (
139
145
  client: SDKClient,
140
146
  viemClient: Client,
141
147
  chainId: number,
142
- tokenAddress: Address
148
+ tokenAddress: Address,
149
+ ownerAddress: Address
143
150
  ) => {
144
151
  try {
145
152
  const multicallAddress = await getMulticallAddress(client, chainId)
@@ -154,7 +161,7 @@ const getEIP712DomainData = async (
154
161
  address: tokenAddress,
155
162
  abi: eip2612Abi,
156
163
  functionName: 'nonces',
157
- args: [viemClient.account!.address],
164
+ args: [ownerAddress],
158
165
  },
159
166
  ] as const
160
167
 
@@ -296,7 +303,8 @@ const getContractData = async (
296
303
  client: SDKClient,
297
304
  viemClient: Client,
298
305
  chainId: number,
299
- tokenAddress: Address
306
+ tokenAddress: Address,
307
+ ownerAddress: Address
300
308
  ) => {
301
309
  try {
302
310
  // First try EIP-5267 approach - returns domain object directly
@@ -304,7 +312,8 @@ const getContractData = async (
304
312
  client,
305
313
  viemClient,
306
314
  chainId,
307
- tokenAddress
315
+ tokenAddress,
316
+ ownerAddress
308
317
  )
309
318
  if (eip5267Data) {
310
319
  return eip5267Data
@@ -333,7 +342,7 @@ const getContractData = async (
333
342
  address: tokenAddress,
334
343
  abi: eip2612Abi,
335
344
  functionName: 'nonces',
336
- args: [viemClient.account!.address],
345
+ args: [ownerAddress],
337
346
  },
338
347
  {
339
348
  address: tokenAddress,
@@ -479,10 +488,14 @@ export const getNativePermit = async (
479
488
  tokenAddress,
480
489
  spenderAddress,
481
490
  amount,
491
+ ownerAddress,
482
492
  }: GetNativePermitParams
483
493
  ): Promise<NativePermitData | undefined> => {
484
- // Check if the account can use native permits (EOA or EIP-7702 delegated account)
485
- const canUsePermits = await canAccountUseNativePermits(client, viemClient)
494
+ // Check that the account can produce a signature the token will accept
495
+ const canUsePermits = await canAccountUseNativePermits(client, {
496
+ chainId,
497
+ address: ownerAddress,
498
+ })
486
499
  if (!canUsePermits) {
487
500
  return undefined
488
501
  }
@@ -491,7 +504,8 @@ export const getNativePermit = async (
491
504
  client,
492
505
  viemClient,
493
506
  chainId,
494
- tokenAddress
507
+ tokenAddress,
508
+ ownerAddress
495
509
  )
496
510
  if (!contractData) {
497
511
  return undefined
@@ -506,7 +520,7 @@ export const getNativePermit = async (
506
520
  const deadline = BigInt(Math.floor(Date.now() / 1000) + 30 * 60).toString() // 30 minutes
507
521
 
508
522
  const message = {
509
- owner: viemClient.account!.address,
523
+ owner: ownerAddress,
510
524
  spender: spenderAddress,
511
525
  value: amount.toString(),
512
526
  nonce: contractData.nonce!.toString(),
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export const name = '@lifi/sdk-provider-ethereum'
2
- export const version = '4.0.12'
2
+ export const version = '4.0.13'