@lifi/perps-sdk 1.5.6 → 2.0.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/dist/cjs/client/PerpsClient.d.ts +9 -4
- package/dist/cjs/client/PerpsClient.d.ts.map +1 -1
- package/dist/cjs/client/PerpsClient.js +42 -4
- package/dist/cjs/client/PerpsClient.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/api.d.ts +10 -1
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/types/config.d.ts +1 -0
- package/dist/cjs/types/config.d.ts.map +1 -1
- package/dist/cjs/types/provider.d.ts +1 -0
- package/dist/cjs/types/provider.d.ts.map +1 -1
- package/dist/cjs/utils/switchChain.d.ts +5 -0
- package/dist/cjs/utils/switchChain.d.ts.map +1 -0
- package/dist/cjs/utils/switchChain.js +39 -0
- package/dist/cjs/utils/switchChain.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/client/PerpsClient.d.ts +46 -7
- package/dist/esm/client/PerpsClient.d.ts.map +1 -1
- package/dist/esm/client/PerpsClient.js +85 -7
- package/dist/esm/client/PerpsClient.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/api.d.ts +24 -7
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/types/config.d.ts +11 -0
- package/dist/esm/types/config.d.ts.map +1 -1
- package/dist/esm/types/provider.d.ts +11 -0
- package/dist/esm/types/provider.d.ts.map +1 -1
- package/dist/esm/utils/switchChain.d.ts +21 -0
- package/dist/esm/utils/switchChain.d.ts.map +1 -0
- package/dist/esm/utils/switchChain.js +51 -0
- package/dist/esm/utils/switchChain.js.map +1 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/client/PerpsClient.d.ts +46 -7
- package/dist/types/client/PerpsClient.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +24 -7
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/types/config.d.ts +11 -0
- package/dist/types/types/config.d.ts.map +1 -1
- package/dist/types/types/provider.d.ts +11 -0
- package/dist/types/types/provider.d.ts.map +1 -1
- package/dist/types/utils/switchChain.d.ts +21 -0
- package/dist/types/utils/switchChain.d.ts.map +1 -0
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/client/PerpsClient.ts +106 -10
- package/src/index.ts +3 -2
- package/src/types/api.ts +29 -7
- package/src/types/config.ts +14 -0
- package/src/types/provider.ts +11 -0
- package/src/utils/switchChain.ts +73 -0
- package/src/version.ts +1 -1
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ActionStep,
|
|
3
|
+
Eip712ActionStep,
|
|
4
|
+
ProviderAction,
|
|
5
|
+
} from '@lifi/perps-types'
|
|
6
|
+
import { PerpsErrorCode, PerpsSigner, SigningMethod } from '@lifi/perps-types'
|
|
7
|
+
import { getChainId } from 'viem/actions'
|
|
8
|
+
import { PerpsError } from '../errors/PerpsError.js'
|
|
9
|
+
import type { PerpsClientSigner, SwitchChainHook } from '../types/config.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The chain a batch of `actions` must be signed on, or `undefined` when no
|
|
13
|
+
* wallet chain switch applies. Only USER-signed EIP-712 batches carry a target
|
|
14
|
+
* (agent-signed batches sign with a chain-bound keypair; other schemes are not
|
|
15
|
+
* EIP-712). Reads the first numeric `typedData.domain.chainId`; a batch whose
|
|
16
|
+
* steps omit `chainId` yields `undefined`.
|
|
17
|
+
*/
|
|
18
|
+
export function userEip712TargetChainId(
|
|
19
|
+
descriptor: ProviderAction,
|
|
20
|
+
actions: ActionStep[]
|
|
21
|
+
): number | undefined {
|
|
22
|
+
if (
|
|
23
|
+
descriptor.signingMethod !== SigningMethod.EIP712 ||
|
|
24
|
+
!descriptor.signers.includes(PerpsSigner.USER)
|
|
25
|
+
) {
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
for (const step of actions) {
|
|
29
|
+
const chainId = (step as Eip712ActionStep).typedData?.domain?.chainId
|
|
30
|
+
if (typeof chainId === 'number') {
|
|
31
|
+
return chainId
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Ensure `wallet` is on `targetChainId` before signing, returning the wallet to
|
|
39
|
+
* sign with. Without a `switchChain` hook the wallet is returned unchanged (no
|
|
40
|
+
* throw, no RPC) — a local/private-key signer signs EIP-712 offline regardless
|
|
41
|
+
* of its transport chain. With a hook: probe the current chain via viem
|
|
42
|
+
* `getChainId`, return the wallet untouched when already on target, else invoke
|
|
43
|
+
* the hook and re-verify. Throws `PerpsErrorCode.SDKError` when the hook yields
|
|
44
|
+
* no client or a client still on the wrong chain.
|
|
45
|
+
*/
|
|
46
|
+
export async function switchSigningChain(
|
|
47
|
+
wallet: PerpsClientSigner,
|
|
48
|
+
targetChainId: number,
|
|
49
|
+
switchChain?: SwitchChainHook
|
|
50
|
+
): Promise<PerpsClientSigner> {
|
|
51
|
+
if (!switchChain) {
|
|
52
|
+
return wallet
|
|
53
|
+
}
|
|
54
|
+
const currentChainId = await getChainId(wallet)
|
|
55
|
+
if (currentChainId === targetChainId) {
|
|
56
|
+
return wallet
|
|
57
|
+
}
|
|
58
|
+
const switched = await switchChain(targetChainId)
|
|
59
|
+
if (!switched) {
|
|
60
|
+
throw new PerpsError(
|
|
61
|
+
PerpsErrorCode.SDKError,
|
|
62
|
+
`Wallet chain switch to ${targetChainId} was not completed.`
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
const switchedChainId = await getChainId(switched)
|
|
66
|
+
if (switchedChainId !== targetChainId) {
|
|
67
|
+
throw new PerpsError(
|
|
68
|
+
PerpsErrorCode.SDKError,
|
|
69
|
+
`Wallet is on chain ${switchedChainId} but chain ${targetChainId} is required to sign.`
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
return switched
|
|
73
|
+
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const name = '@lifi/perps-sdk'
|
|
2
|
-
export const version = '
|
|
2
|
+
export const version = '2.0.0'
|