@lifi/perps-sdk 2.0.0 → 2.2.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/index.d.ts +2 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/storage/encryptedStorage.d.ts +3 -0
- package/dist/cjs/storage/encryptedStorage.d.ts.map +1 -0
- package/dist/cjs/storage/encryptedStorage.js +139 -0
- package/dist/cjs/storage/encryptedStorage.js.map +1 -0
- package/dist/cjs/storage/storage.d.ts +0 -1
- package/dist/cjs/storage/storage.d.ts.map +1 -1
- package/dist/cjs/storage/storage.js +0 -27
- package/dist/cjs/storage/storage.js.map +1 -1
- package/dist/cjs/types/api.d.ts +1 -0
- package/dist/cjs/types/api.d.ts.map +1 -1
- package/dist/cjs/utils/accountSummary.d.ts +1 -1
- package/dist/cjs/utils/accountSummary.d.ts.map +1 -1
- package/dist/cjs/utils/accountSummary.js +6 -2
- package/dist/cjs/utils/accountSummary.js.map +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/storage/encryptedStorage.d.ts +23 -0
- package/dist/esm/storage/encryptedStorage.d.ts.map +1 -0
- package/dist/esm/storage/encryptedStorage.js +171 -0
- package/dist/esm/storage/encryptedStorage.js.map +1 -0
- package/dist/esm/storage/storage.d.ts +0 -7
- package/dist/esm/storage/storage.d.ts.map +1 -1
- package/dist/esm/storage/storage.js +0 -34
- package/dist/esm/storage/storage.js.map +1 -1
- package/dist/esm/types/api.d.ts +4 -0
- package/dist/esm/types/api.d.ts.map +1 -1
- package/dist/esm/utils/accountSummary.d.ts +4 -1
- package/dist/esm/utils/accountSummary.d.ts.map +1 -1
- package/dist/esm/utils/accountSummary.js +6 -2
- package/dist/esm/utils/accountSummary.js.map +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/storage/encryptedStorage.d.ts +23 -0
- package/dist/types/storage/encryptedStorage.d.ts.map +1 -0
- package/dist/types/storage/storage.d.ts +0 -7
- package/dist/types/storage/storage.d.ts.map +1 -1
- package/dist/types/types/api.d.ts +4 -0
- package/dist/types/types/api.d.ts.map +1 -1
- package/dist/types/utils/accountSummary.d.ts +4 -1
- package/dist/types/utils/accountSummary.d.ts.map +1 -1
- package/dist/types/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/index.ts +2 -1
- package/src/storage/encryptedStorage.ts +190 -0
- package/src/storage/storage.ts +0 -34
- package/src/types/api.ts +4 -0
- package/src/utils/accountSummary.ts +10 -3
- package/src/version.ts +1 -1
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import type { StorageAdapter } from './types.js'
|
|
2
|
+
|
|
3
|
+
const DB_NAME = 'lifi-perps-sdk'
|
|
4
|
+
const OBJECT_STORE = 'keys'
|
|
5
|
+
const MASTER_KEY_ID = 'storage-master-key'
|
|
6
|
+
const IV_BYTE_LENGTH = 12
|
|
7
|
+
const AES_KEY_PARAMS = { name: 'AES-GCM', length: 256 } as const
|
|
8
|
+
|
|
9
|
+
let masterKeyPromise: Promise<CryptoKey | null> | undefined
|
|
10
|
+
let warnedDegraded = false
|
|
11
|
+
|
|
12
|
+
// Silent in SSR (no `window`), where missing browser storage is expected.
|
|
13
|
+
function warnDegraded(reason: string): void {
|
|
14
|
+
if (warnedDegraded || typeof window === 'undefined') {
|
|
15
|
+
return
|
|
16
|
+
}
|
|
17
|
+
warnedDegraded = true
|
|
18
|
+
console.warn(
|
|
19
|
+
`[perps-sdk] Persistent session storage disabled: ${reason}. Session keys will not survive a page reload.`
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function openDb(): Promise<IDBDatabase> {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const request = indexedDB.open(DB_NAME, 1)
|
|
26
|
+
request.onupgradeneeded = () => {
|
|
27
|
+
request.result.createObjectStore(OBJECT_STORE)
|
|
28
|
+
}
|
|
29
|
+
request.onsuccess = () => resolve(request.result)
|
|
30
|
+
request.onerror = () => reject(request.error)
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function requestToPromise<T>(request: IDBRequest<T>): Promise<T> {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
request.onsuccess = () => resolve(request.result)
|
|
37
|
+
request.onerror = () => reject(request.error)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Resolve the AES-GCM master key, generating and persisting one on first use.
|
|
43
|
+
* The key is non-extractable and lives as a structured-cloned handle in
|
|
44
|
+
* IndexedDB — the raw bytes never leave the browser's crypto layer. Resolves
|
|
45
|
+
* `null` when IndexedDB or WebCrypto is unavailable so callers degrade to
|
|
46
|
+
* no-op writes / null reads rather than persisting plaintext.
|
|
47
|
+
*/
|
|
48
|
+
async function loadMasterKey(): Promise<CryptoKey | null> {
|
|
49
|
+
if (typeof crypto === 'undefined' || !crypto.subtle) {
|
|
50
|
+
warnDegraded('crypto.subtle is unavailable (non-HTTPS context?)')
|
|
51
|
+
return null
|
|
52
|
+
}
|
|
53
|
+
if (typeof indexedDB === 'undefined') {
|
|
54
|
+
warnDegraded('indexedDB is unavailable')
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
// Generated before the transaction: awaiting a non-IDB promise inside an
|
|
59
|
+
// IDB transaction auto-commits it, breaking the atomic get-or-adopt below.
|
|
60
|
+
const candidate = await crypto.subtle.generateKey(AES_KEY_PARAMS, false, [
|
|
61
|
+
'encrypt',
|
|
62
|
+
'decrypt',
|
|
63
|
+
])
|
|
64
|
+
const db = await openDb()
|
|
65
|
+
try {
|
|
66
|
+
// Single readwrite transaction so concurrent tabs can't interleave the
|
|
67
|
+
// check and the write: the loser's get sees the winner's key and adopts
|
|
68
|
+
// it instead of clobbering, discarding its own candidate.
|
|
69
|
+
const store = db
|
|
70
|
+
.transaction(OBJECT_STORE, 'readwrite')
|
|
71
|
+
.objectStore(OBJECT_STORE)
|
|
72
|
+
const existing = await requestToPromise(store.get(MASTER_KEY_ID))
|
|
73
|
+
if (existing instanceof CryptoKey) {
|
|
74
|
+
return existing
|
|
75
|
+
}
|
|
76
|
+
await requestToPromise(store.put(candidate, MASTER_KEY_ID))
|
|
77
|
+
return candidate
|
|
78
|
+
} finally {
|
|
79
|
+
db.close()
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
warnDegraded('indexedDB failed to open or store the master key')
|
|
83
|
+
return null
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getMasterKey(): Promise<CryptoKey | null> {
|
|
88
|
+
masterKeyPromise ??= loadMasterKey()
|
|
89
|
+
return masterKeyPromise
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function bytesToBase64(bytes: Uint8Array): string {
|
|
93
|
+
let binary = ''
|
|
94
|
+
for (const byte of bytes) {
|
|
95
|
+
binary += String.fromCharCode(byte)
|
|
96
|
+
}
|
|
97
|
+
return btoa(binary)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function base64ToBytes(value: string): Uint8Array {
|
|
101
|
+
const binary = atob(value)
|
|
102
|
+
const bytes = new Uint8Array(binary.length)
|
|
103
|
+
for (let i = 0; i < binary.length; i += 1) {
|
|
104
|
+
bytes[i] = binary.charCodeAt(i)
|
|
105
|
+
}
|
|
106
|
+
return bytes
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Default storage adapter: persists AES-GCM-256 ciphertext to browser
|
|
111
|
+
* `localStorage`, keyed by a non-extractable master {@link CryptoKey} held in
|
|
112
|
+
* IndexedDB. Each `set` uses a fresh 12-byte IV and
|
|
113
|
+
* stores `base64(iv ‖ ciphertext)`. Falls back to a no-op / `null` when
|
|
114
|
+
* `localStorage`, `indexedDB`, or `crypto.subtle` is unavailable (e.g. SSR) —
|
|
115
|
+
* never writing plaintext. In browser contexts the degradation logs a one-time
|
|
116
|
+
* `console.warn` naming the missing capability.
|
|
117
|
+
*
|
|
118
|
+
* `get` resolves `null` on any failure (missing master key, tampered or
|
|
119
|
+
* truncated ciphertext, malformed value), matching the poisoned-record
|
|
120
|
+
* eviction philosophy so callers treat undecryptable data as absent.
|
|
121
|
+
*
|
|
122
|
+
* @security Encryption at rest defeats generic browser-storage/disk scanning
|
|
123
|
+
* and raw key exfiltration, not malware targeting this SDK or a fully
|
|
124
|
+
* compromised page — a same-origin script can still drive this adapter to
|
|
125
|
+
* decrypt.
|
|
126
|
+
*
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
export const localStorageAdapter: StorageAdapter = {
|
|
130
|
+
async get(key: string): Promise<string | null> {
|
|
131
|
+
try {
|
|
132
|
+
const raw = localStorage.getItem(key)
|
|
133
|
+
if (raw === null) {
|
|
134
|
+
return null
|
|
135
|
+
}
|
|
136
|
+
const masterKey = await getMasterKey()
|
|
137
|
+
if (!masterKey) {
|
|
138
|
+
return null
|
|
139
|
+
}
|
|
140
|
+
const combined = base64ToBytes(raw)
|
|
141
|
+
const iv = combined.slice(0, IV_BYTE_LENGTH)
|
|
142
|
+
const ciphertext = combined.slice(IV_BYTE_LENGTH)
|
|
143
|
+
const plaintext = await crypto.subtle.decrypt(
|
|
144
|
+
{ name: 'AES-GCM', iv },
|
|
145
|
+
masterKey,
|
|
146
|
+
ciphertext
|
|
147
|
+
)
|
|
148
|
+
return new TextDecoder().decode(plaintext)
|
|
149
|
+
} catch {
|
|
150
|
+
if (typeof localStorage === 'undefined') {
|
|
151
|
+
warnDegraded('localStorage is unavailable')
|
|
152
|
+
}
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
async set(key: string, value: string): Promise<void> {
|
|
158
|
+
try {
|
|
159
|
+
const masterKey = await getMasterKey()
|
|
160
|
+
if (!masterKey) {
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
const iv = crypto.getRandomValues(new Uint8Array(IV_BYTE_LENGTH))
|
|
164
|
+
const ciphertext = new Uint8Array(
|
|
165
|
+
await crypto.subtle.encrypt(
|
|
166
|
+
{ name: 'AES-GCM', iv },
|
|
167
|
+
masterKey,
|
|
168
|
+
new TextEncoder().encode(value)
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
const combined = new Uint8Array(iv.length + ciphertext.length)
|
|
172
|
+
combined.set(iv, 0)
|
|
173
|
+
combined.set(ciphertext, iv.length)
|
|
174
|
+
localStorage.setItem(key, bytesToBase64(combined))
|
|
175
|
+
} catch {
|
|
176
|
+
// Encryption failed or localStorage unavailable — never persist plaintext
|
|
177
|
+
if (typeof localStorage === 'undefined') {
|
|
178
|
+
warnDegraded('localStorage is unavailable')
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
async remove(key: string): Promise<void> {
|
|
184
|
+
try {
|
|
185
|
+
localStorage.removeItem(key)
|
|
186
|
+
} catch {
|
|
187
|
+
// localStorage not available
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
}
|
package/src/storage/storage.ts
CHANGED
|
@@ -1,39 +1,5 @@
|
|
|
1
1
|
import type { StorageAdapter } from './types.js'
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* localStorage adapter for browser environments. Falls back to a no-op when
|
|
5
|
-
* `localStorage` is unavailable (e.g. SSR).
|
|
6
|
-
*
|
|
7
|
-
* @public
|
|
8
|
-
*/
|
|
9
|
-
export const localStorageAdapter: StorageAdapter = {
|
|
10
|
-
get: (key: string): Promise<string | null> => {
|
|
11
|
-
try {
|
|
12
|
-
return Promise.resolve(localStorage.getItem(key))
|
|
13
|
-
} catch {
|
|
14
|
-
return Promise.resolve(null)
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
set: (key: string, value: string): Promise<void> => {
|
|
19
|
-
try {
|
|
20
|
-
localStorage.setItem(key, value)
|
|
21
|
-
} catch {
|
|
22
|
-
// localStorage not available
|
|
23
|
-
}
|
|
24
|
-
return Promise.resolve()
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
remove: (key: string): Promise<void> => {
|
|
28
|
-
try {
|
|
29
|
-
localStorage.removeItem(key)
|
|
30
|
-
} catch {
|
|
31
|
-
// localStorage not available
|
|
32
|
-
}
|
|
33
|
-
return Promise.resolve()
|
|
34
|
-
},
|
|
35
|
-
}
|
|
36
|
-
|
|
37
3
|
/**
|
|
38
4
|
* In-memory storage adapter for testing or server-side use.
|
|
39
5
|
*
|
package/src/types/api.ts
CHANGED
|
@@ -179,7 +179,11 @@ export interface SendAssetActionParams {
|
|
|
179
179
|
export interface CancelOrdersParams {
|
|
180
180
|
provider: string
|
|
181
181
|
address: Address
|
|
182
|
+
/** Venue order ids. Venues whose ids are scoped per market (e.g. Lighter's
|
|
183
|
+
* `order_index`) also accept the composite `"<market_id>:<order_id>"`. */
|
|
182
184
|
ids: string[]
|
|
185
|
+
/** Market context for per-market order ids; the order's `market.id`. */
|
|
186
|
+
assetId?: string
|
|
183
187
|
}
|
|
184
188
|
|
|
185
189
|
/**
|
|
@@ -15,6 +15,9 @@ const sumValueUsd = (balances: AccountResponse['balances']): number =>
|
|
|
15
15
|
* - `'free'` — free collateral only; locked margin and unrealized PnL are
|
|
16
16
|
* both carried by the positions. Available margin is the free collateral
|
|
17
17
|
* as-is; unrealized PnL is not counted toward it.
|
|
18
|
+
* - `'net'` — free collateral with unrealized PnL already marked in; only
|
|
19
|
+
* the locked margin is carried by the positions (e.g. a venue-reported
|
|
20
|
+
* available balance).
|
|
18
21
|
* - `'gross'` — locked margin included, unrealized PnL carried by the
|
|
19
22
|
* positions (e.g. spot holdings backing a unified account). The venue
|
|
20
23
|
* counts unrealized PnL toward buying power, so it is added to available
|
|
@@ -25,7 +28,7 @@ const sumValueUsd = (balances: AccountResponse['balances']): number =>
|
|
|
25
28
|
*
|
|
26
29
|
* @public
|
|
27
30
|
*/
|
|
28
|
-
export type CollateralSemantics = 'free' | 'gross' | 'equity'
|
|
31
|
+
export type CollateralSemantics = 'free' | 'net' | 'gross' | 'equity'
|
|
29
32
|
|
|
30
33
|
/**
|
|
31
34
|
* Roll an {@link AccountResponse} and its open positions up into an
|
|
@@ -51,9 +54,13 @@ export function summarizeAccount(
|
|
|
51
54
|
const balances = sumValueUsd(account.balances)
|
|
52
55
|
|
|
53
56
|
const grossCollateral =
|
|
54
|
-
semantics === 'free'
|
|
57
|
+
semantics === 'free' || semantics === 'net'
|
|
58
|
+
? collateral + marginUsed
|
|
59
|
+
: collateral
|
|
55
60
|
const equity =
|
|
56
|
-
semantics === 'equity'
|
|
61
|
+
semantics === 'equity' || semantics === 'net'
|
|
62
|
+
? grossCollateral
|
|
63
|
+
: grossCollateral + unrealizedPnl
|
|
57
64
|
|
|
58
65
|
// Buying power is equity net of locked margin, so unrealized PnL counts
|
|
59
66
|
// toward it. The `'free'` rows exclude uPnL from available margin, holding
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export const name = '@lifi/perps-sdk'
|
|
2
|
-
export const version = '2.
|
|
2
|
+
export const version = '2.2.0'
|