@lifi/perps-sdk 36.5.0 → 37.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/README.md CHANGED
@@ -939,9 +939,20 @@ This required contract method is a breaking SDK change for custom providers.
939
939
 
940
940
  Every provider persists its trading credentials (agent keys, API keys, session tokens) through a pluggable `StorageAdapter`. The default `localStorageAdapter` encrypts values with AES-GCM before writing to browser `localStorage`, holding the master key as a non-extractable `CryptoKey` handle in IndexedDB — key material is never stored as plaintext. Environments without WebCrypto or IndexedDB degrade to non-persistent sessions rather than plaintext writes. Pass a custom adapter to a provider's store to use another backend.
941
941
 
942
+ Node.js scripts can use `LocalJSONFileAdapter` from `@lifi/perps-sdk/node`. Pass the adapter as the provider's `storage` option.
943
+ The adapter stores credentials in plaintext. Keep the file private and use one adapter instance to write to it.
944
+
945
+ ```ts
946
+ import { LocalJSONFileAdapter } from '@lifi/perps-sdk/node'
947
+ import { hyperliquidProvider } from '@lifi/perps-sdk-provider-hyperliquid'
948
+
949
+ const storage = new LocalJSONFileAdapter('./.perps-storage.json')
950
+ const provider = hyperliquidProvider({ storage })
951
+ ```
952
+
942
953
  ## Examples
943
954
 
944
- Runnable scripts live in the [`examples/`](https://github.com/lifinance/perps-sdk/tree/main/examples) directory of the repository — market data, account data, agent trading, error handling, custom storage, and WebSocket subscriptions.
955
+ The [type-checked examples](https://github.com/lifinance/perps-sdk/tree/main/examples) show common SDK operations and how to handle failures.
945
956
 
946
957
  ## Documentation
947
958
 
@@ -0,0 +1,12 @@
1
+ import type { StorageAdapter } from './types.js';
2
+ export declare class LocalJSONFileAdapter implements StorageAdapter {
3
+ private readonly file;
4
+ private pending;
5
+ constructor(file: string | URL);
6
+ get(key: string): Promise<string | null>;
7
+ set(key: string, value: string): Promise<void>;
8
+ remove(key: string): Promise<void>;
9
+ private read;
10
+ private update;
11
+ }
12
+ //# sourceMappingURL=LocalJSONFileAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalJSONFileAdapter.d.ts","sourceRoot":"","sources":["../../../src/storage/LocalJSONFileAdapter.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAGhD,qBAAa,oBAAqB,YAAW,cAAc;IACzD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,OAAO,CAAoB;gBAEvB,IAAI,EAAE,MAAM,GAAG,GAAG;IAIxB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAK9C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM9C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAIpB,IAAI;IAkClB,OAAO,CAAC,MAAM;CAkBf"}
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalJSONFileAdapter = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const promises_1 = require("node:fs/promises");
6
+ const node_path_1 = require("node:path");
7
+ const node_url_1 = require("node:url");
8
+ class LocalJSONFileAdapter {
9
+ file;
10
+ pending = Promise.resolve();
11
+ constructor(file) {
12
+ this.file = (0, node_path_1.resolve)(typeof file === 'string' ? file : (0, node_url_1.fileURLToPath)(file));
13
+ }
14
+ async get(key) {
15
+ await this.pending;
16
+ return (await this.read()).get(key) ?? null;
17
+ }
18
+ set(key, value) {
19
+ return this.update((values) => {
20
+ values.set(key, value);
21
+ });
22
+ }
23
+ remove(key) {
24
+ return this.update((values) => values.delete(key));
25
+ }
26
+ async read() {
27
+ let contents;
28
+ try {
29
+ contents = await (0, promises_1.readFile)(this.file, 'utf8');
30
+ }
31
+ catch (error) {
32
+ if (error instanceof Error &&
33
+ 'code' in error &&
34
+ error.code === 'ENOENT') {
35
+ return new Map();
36
+ }
37
+ throw error;
38
+ }
39
+ let parsed;
40
+ try {
41
+ parsed = JSON.parse(contents);
42
+ }
43
+ catch {
44
+ throw new Error('Credential storage contains invalid JSON.');
45
+ }
46
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
47
+ throw new Error('Credential storage must contain a JSON object.');
48
+ }
49
+ const values = new Map();
50
+ for (const [key, value] of Object.entries(parsed)) {
51
+ if (typeof value !== 'string') {
52
+ throw new Error('Credential storage values must be strings.');
53
+ }
54
+ values.set(key, value);
55
+ }
56
+ return values;
57
+ }
58
+ update(change) {
59
+ const operation = this.pending.then(async () => {
60
+ const values = await this.read();
61
+ change(values);
62
+ const temp = `${this.file}.${(0, node_crypto_1.randomUUID)()}.tmp`;
63
+ try {
64
+ await (0, promises_1.writeFile)(temp, JSON.stringify(Object.fromEntries(values)), {
65
+ flag: 'wx',
66
+ mode: 0o600,
67
+ });
68
+ await (0, promises_1.rename)(temp, this.file);
69
+ }
70
+ finally {
71
+ await (0, promises_1.rm)(temp, { force: true });
72
+ }
73
+ });
74
+ this.pending = operation.catch(() => { });
75
+ return operation;
76
+ }
77
+ }
78
+ exports.LocalJSONFileAdapter = LocalJSONFileAdapter;
79
+ //# sourceMappingURL=LocalJSONFileAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalJSONFileAdapter.js","sourceRoot":"","sources":["../../../src/storage/LocalJSONFileAdapter.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,+CAAkE;AAClE,yCAAmC;AACnC,uCAAwC;AAIxC,MAAa,oBAAoB;IACd,IAAI,CAAQ;IACrB,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;IAEnC,YAAY,IAAkB;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAA,mBAAO,EAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,wBAAa,EAAC,IAAI,CAAC,CAA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A;QACxC,OAAO,SAAS,CAAA;IAClB,CAAC;CACF;AA3ED,oDA2EC"}
@@ -1,3 +1,3 @@
1
1
  export declare const name = "@lifi/perps-sdk";
2
- export declare const version = "36.5.0";
2
+ export declare const version = "37.0.0";
3
3
  //# sourceMappingURL=version.d.ts.map
@@ -2,5 +2,5 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = exports.name = void 0;
4
4
  exports.name = '@lifi/perps-sdk';
5
- exports.version = '36.5.0';
5
+ exports.version = '37.0.0';
6
6
  //# sourceMappingURL=version.js.map
@@ -0,0 +1,13 @@
1
+ import type { StorageAdapter } from './types.js';
2
+ /** Stores credentials as plaintext JSON. Use one writer per file. */
3
+ export declare class LocalJSONFileAdapter implements StorageAdapter {
4
+ private readonly file;
5
+ private pending;
6
+ constructor(file: string | URL);
7
+ get(key: string): Promise<string | null>;
8
+ set(key: string, value: string): Promise<void>;
9
+ remove(key: string): Promise<void>;
10
+ private read;
11
+ private update;
12
+ }
13
+ //# sourceMappingURL=LocalJSONFileAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalJSONFileAdapter.d.ts","sourceRoot":"","sources":["../../../src/storage/LocalJSONFileAdapter.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,qEAAqE;AACrE,qBAAa,oBAAqB,YAAW,cAAc;IACzD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,OAAO,CAAoB;gBAEvB,IAAI,EAAE,MAAM,GAAG,GAAG;IAIxB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAK9C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM9C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAIpB,IAAI;IAkClB,OAAO,CAAC,MAAM;CAkBf"}
@@ -0,0 +1,76 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { readFile, rename, rm, writeFile } from 'node:fs/promises';
3
+ import { resolve } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ /** Stores credentials as plaintext JSON. Use one writer per file. */
6
+ export class LocalJSONFileAdapter {
7
+ file;
8
+ pending = Promise.resolve();
9
+ constructor(file) {
10
+ this.file = resolve(typeof file === 'string' ? file : fileURLToPath(file));
11
+ }
12
+ async get(key) {
13
+ await this.pending;
14
+ return (await this.read()).get(key) ?? null;
15
+ }
16
+ set(key, value) {
17
+ return this.update((values) => {
18
+ values.set(key, value);
19
+ });
20
+ }
21
+ remove(key) {
22
+ return this.update((values) => values.delete(key));
23
+ }
24
+ async read() {
25
+ let contents;
26
+ try {
27
+ contents = await readFile(this.file, 'utf8');
28
+ }
29
+ catch (error) {
30
+ if (error instanceof Error &&
31
+ 'code' in error &&
32
+ error.code === 'ENOENT') {
33
+ return new Map();
34
+ }
35
+ throw error;
36
+ }
37
+ let parsed;
38
+ try {
39
+ parsed = JSON.parse(contents);
40
+ }
41
+ catch {
42
+ throw new Error('Credential storage contains invalid JSON.');
43
+ }
44
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
45
+ throw new Error('Credential storage must contain a JSON object.');
46
+ }
47
+ const values = new Map();
48
+ for (const [key, value] of Object.entries(parsed)) {
49
+ if (typeof value !== 'string') {
50
+ throw new Error('Credential storage values must be strings.');
51
+ }
52
+ values.set(key, value);
53
+ }
54
+ return values;
55
+ }
56
+ update(change) {
57
+ const operation = this.pending.then(async () => {
58
+ const values = await this.read();
59
+ change(values);
60
+ const temp = `${this.file}.${randomUUID()}.tmp`;
61
+ try {
62
+ await writeFile(temp, JSON.stringify(Object.fromEntries(values)), {
63
+ flag: 'wx',
64
+ mode: 0o600,
65
+ });
66
+ await rename(temp, this.file);
67
+ }
68
+ finally {
69
+ await rm(temp, { force: true });
70
+ }
71
+ });
72
+ this.pending = operation.catch(() => { });
73
+ return operation;
74
+ }
75
+ }
76
+ //# sourceMappingURL=LocalJSONFileAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalJSONFileAdapter.js","sourceRoot":"","sources":["../../../src/storage/LocalJSONFileAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAGxC,qEAAqE;AACrE,MAAM,OAAO,oBAAoB;IACd,IAAI,CAAQ;IACrB,OAAO,GAAG,OAAO,CAAC,OAAO,EAAE,CAAA;IAEnC,YAAY,IAAkB;QAC5B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAA;IAC5E,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,GAAW;QACnB,MAAM,IAAI,CAAC,OAAO,CAAA;QAClB,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,CAAA;IAC7C,CAAC;IAED,GAAG,CAAC,GAAW,EAAE,KAAa;QAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE;YAC5B,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;QACxB,CAAC,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,CAAC,GAAW;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM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I,CAAC,KAAK,IAAI,EAAE;YAC7C,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,CAAA;YAChC,MAAM,CAAC,MAAM,CAAC,CAAA;YACd,MAAM,IAAI,GAAG,GAAG,IAAI,CAAC,IAAI,IAAI,UAAU,EAAE,MAAM,CAAA;YAC/C,IAAI,CAAC;gBACH,MAAM,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE;oBAChE,IAAI,EAAE,IAAI;oBACV,IAAI,EAAE,KAAK;iBACZ,CAAC,CAAA;gBACF,MAAM,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;YAC/B,CAAC;oBAAS,CAAC;gBACT,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;YACjC,CAAC;QACH,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;QACxC,OAAO,SAAS,CAAA;IAClB,CAAC;CACF"}
@@ -9,5 +9,5 @@ export declare const name = "@lifi/perps-sdk";
9
9
  *
10
10
  * @public
11
11
  */
12
- export declare const version = "36.5.0";
12
+ export declare const version = "37.0.0";
13
13
  //# sourceMappingURL=version.d.ts.map
@@ -9,5 +9,5 @@ export const name = '@lifi/perps-sdk';
9
9
  *
10
10
  * @public
11
11
  */
12
- export const version = '36.5.0';
12
+ export const version = '37.0.0';
13
13
  //# sourceMappingURL=version.js.map
@@ -0,0 +1,13 @@
1
+ import type { StorageAdapter } from './types.js';
2
+ /** Stores credentials as plaintext JSON. Use one writer per file. */
3
+ export declare class LocalJSONFileAdapter implements StorageAdapter {
4
+ private readonly file;
5
+ private pending;
6
+ constructor(file: string | URL);
7
+ get(key: string): Promise<string | null>;
8
+ set(key: string, value: string): Promise<void>;
9
+ remove(key: string): Promise<void>;
10
+ private read;
11
+ private update;
12
+ }
13
+ //# sourceMappingURL=LocalJSONFileAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"LocalJSONFileAdapter.d.ts","sourceRoot":"","sources":["../../../src/storage/LocalJSONFileAdapter.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEhD,qEAAqE;AACrE,qBAAa,oBAAqB,YAAW,cAAc;IACzD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,OAAO,CAAoB;gBAEvB,IAAI,EAAE,MAAM,GAAG,GAAG;IAIxB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAK9C,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM9C,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAIpB,IAAI;IAkClB,OAAO,CAAC,MAAM;CAkBf"}
@@ -9,5 +9,5 @@ export declare const name = "@lifi/perps-sdk";
9
9
  *
10
10
  * @public
11
11
  */
12
- export declare const version = "36.5.0";
12
+ export declare const version = "37.0.0";
13
13
  //# sourceMappingURL=version.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifi/perps-sdk",
3
- "version": "36.5.0",
3
+ "version": "37.0.0",
4
4
  "description": "LI.FI Perps SDK for perpetuals trading",
5
5
  "homepage": "https://github.com/lifinance/perps-sdk",
6
6
  "bugs": {
@@ -24,6 +24,13 @@
24
24
  "import": "./dist/esm/index.js",
25
25
  "default": "./dist/cjs/index.js"
26
26
  },
27
+ "./node": {
28
+ "types": "./dist/types/storage/LocalJSONFileAdapter.d.ts",
29
+ "node": {
30
+ "import": "./dist/esm/storage/LocalJSONFileAdapter.js",
31
+ "default": "./dist/cjs/storage/LocalJSONFileAdapter.js"
32
+ }
33
+ },
27
34
  "./package.json": "./package.json"
28
35
  },
29
36
  "files": [
@@ -45,7 +52,7 @@
45
52
  "@lifi/types": "^18.3.0",
46
53
  "big.js": "^7.0.1",
47
54
  "partysocket": "^1.1.19",
48
- "@lifi/perps-types": "^30.6.0"
55
+ "@lifi/perps-types": "^31.0.0"
49
56
  },
50
57
  "peerDependencies": {
51
58
  "viem": "2.57.11 || ^2.57.17"
@@ -0,0 +1,83 @@
1
+ import { randomUUID } from 'node:crypto'
2
+ import { readFile, rename, rm, writeFile } from 'node:fs/promises'
3
+ import { resolve } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+ import type { StorageAdapter } from './types.js'
6
+
7
+ /** Stores credentials as plaintext JSON. Use one writer per file. */
8
+ export class LocalJSONFileAdapter implements StorageAdapter {
9
+ private readonly file: string
10
+ private pending = Promise.resolve()
11
+
12
+ constructor(file: string | URL) {
13
+ this.file = resolve(typeof file === 'string' ? file : fileURLToPath(file))
14
+ }
15
+
16
+ async get(key: string): Promise<string | null> {
17
+ await this.pending
18
+ return (await this.read()).get(key) ?? null
19
+ }
20
+
21
+ set(key: string, value: string): Promise<void> {
22
+ return this.update((values) => {
23
+ values.set(key, value)
24
+ })
25
+ }
26
+
27
+ remove(key: string): Promise<void> {
28
+ return this.update((values) => values.delete(key))
29
+ }
30
+
31
+ private async read(): Promise<Map<string, string>> {
32
+ let contents: string
33
+ try {
34
+ contents = await readFile(this.file, 'utf8')
35
+ } catch (error) {
36
+ if (
37
+ error instanceof Error &&
38
+ 'code' in error &&
39
+ error.code === 'ENOENT'
40
+ ) {
41
+ return new Map()
42
+ }
43
+ throw error
44
+ }
45
+
46
+ let parsed: unknown
47
+ try {
48
+ parsed = JSON.parse(contents)
49
+ } catch {
50
+ throw new Error('Credential storage contains invalid JSON.')
51
+ }
52
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
53
+ throw new Error('Credential storage must contain a JSON object.')
54
+ }
55
+ const values = new Map<string, string>()
56
+ for (const [key, value] of Object.entries(parsed)) {
57
+ if (typeof value !== 'string') {
58
+ throw new Error('Credential storage values must be strings.')
59
+ }
60
+ values.set(key, value)
61
+ }
62
+ return values
63
+ }
64
+
65
+ private update(change: (values: Map<string, string>) => void): Promise<void> {
66
+ const operation = this.pending.then(async () => {
67
+ const values = await this.read()
68
+ change(values)
69
+ const temp = `${this.file}.${randomUUID()}.tmp`
70
+ try {
71
+ await writeFile(temp, JSON.stringify(Object.fromEntries(values)), {
72
+ flag: 'wx',
73
+ mode: 0o600,
74
+ })
75
+ await rename(temp, this.file)
76
+ } finally {
77
+ await rm(temp, { force: true })
78
+ }
79
+ })
80
+ this.pending = operation.catch(() => {})
81
+ return operation
82
+ }
83
+ }
package/src/version.ts CHANGED
@@ -10,4 +10,4 @@ export const name = '@lifi/perps-sdk'
10
10
  *
11
11
  * @public
12
12
  */
13
- export const version = '36.5.0'
13
+ export const version = '37.0.0'