@andy-liquid-labs/lighter-ts-sdk 1.0.3 → 1.0.5

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/index.d.mts CHANGED
@@ -272,9 +272,9 @@ interface KeyManager extends Signer {
272
272
  prvKeyBytes(): Uint8Array;
273
273
  }
274
274
  /**
275
- * Create a new KeyManager from private key bytes
275
+ * Create a new KeyManager from private key bytes and public key bytes
276
276
  */
277
- declare function newKeyManager(privateKeyBytes: Uint8Array): KeyManager;
277
+ declare function newKeyManager(privateKeyBytes: Uint8Array, publicKeyBytes: Uint8Array): KeyManager;
278
278
  /**
279
279
  * Generate a new API key pair
280
280
  * Returns [privateKey, publicKey] as hex strings
@@ -1622,8 +1622,9 @@ declare class TxClient {
1622
1622
  private readonly chainId;
1623
1623
  private readonly keyManager;
1624
1624
  private readonly _accountIndex;
1625
+ private readonly _pubkey;
1625
1626
  private readonly _apiKeyIndex;
1626
- constructor(apiClient: MinimalHTTPClient | null, privateKey: string, accountIndex: bigint, apiKeyIndex: number, chainId: number);
1627
+ constructor(apiClient: MinimalHTTPClient | null, privateKey: string, publicKey: string, accountIndex: bigint, apiKeyIndex: number, chainId: number);
1627
1628
  /**
1628
1629
  * Fill in default transaction options
1629
1630
  */
@@ -1657,9 +1658,15 @@ declare class TxClient {
1657
1658
  */
1658
1659
  getNextNonce: () => Promise<bigint>;
1659
1660
  /**
1660
- * Create an auth token
1661
+ * Create an auth token with caching
1662
+ * Generates a new token if:
1663
+ * 1. No cached token exists for this account
1664
+ * 2. The cached token expires in less than 1 hour
1665
+ *
1666
+ * @param deadline - Optional deadline for the token (defaults to 24 hours from now)
1667
+ * @returns Auth token string in format: "timestamp:accountIndex:apiKeyIndex:signature"
1661
1668
  */
1662
- getAuthToken(deadline: Date): string;
1669
+ getAuthToken(deadline?: Date): string;
1663
1670
  /**
1664
1671
  * Sign a create order transaction
1665
1672
  */
@@ -1736,7 +1743,7 @@ declare function getClient(apiKeyIndex?: number, accountIndex?: bigint): TxClien
1736
1743
  /**
1737
1744
  * Create a new TxClient and store it
1738
1745
  */
1739
- declare function createClient(httpClient: MinimalHTTPClient | null, privateKey: string, chainId: number, apiKeyIndex: number, accountIndex: bigint): TxClient;
1746
+ declare function createClient(httpClient: MinimalHTTPClient | null, privateKey: string, publicKey: string, chainId: number, apiKeyIndex: number, accountIndex: bigint): TxClient;
1740
1747
  /**
1741
1748
  * Check that a client exists and the API key matches the one on the server
1742
1749
  */
@@ -1887,6 +1894,7 @@ interface Candlestick {
1887
1894
  l: number;
1888
1895
  c: number;
1889
1896
  v: number;
1897
+ V: number;
1890
1898
  }
1891
1899
  interface CandlesRequest {
1892
1900
  market_id: number;
@@ -2949,7 +2957,7 @@ interface InfoPrivateClientConfig {
2949
2957
  *
2950
2958
  * @example
2951
2959
  * ```typescript
2952
- * const txClient = createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex);
2960
+ * const txClient = createClient(httpClient, privateKey, publicKey, chainId, apiKeyIndex, accountIndex);
2953
2961
  * const client = new InfoPrivateClient({
2954
2962
  * baseURL: "https://mainnet.zklighter.elliot.ai",
2955
2963
  * txClient
@@ -3346,7 +3354,7 @@ interface ExchangeClientConfig {
3346
3354
  *
3347
3355
  * @example
3348
3356
  * ```typescript
3349
- * const txClient = createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex);
3357
+ * const txClient = createClient(httpClient, privateKey, publicKey, chainId, apiKeyIndex, accountIndex);
3350
3358
  * const client = new ExchangeClient({
3351
3359
  * baseURL: "https://mainnet.zklighter.elliot.ai",
3352
3360
  * txClient
package/dist/index.d.ts CHANGED
@@ -272,9 +272,9 @@ interface KeyManager extends Signer {
272
272
  prvKeyBytes(): Uint8Array;
273
273
  }
274
274
  /**
275
- * Create a new KeyManager from private key bytes
275
+ * Create a new KeyManager from private key bytes and public key bytes
276
276
  */
277
- declare function newKeyManager(privateKeyBytes: Uint8Array): KeyManager;
277
+ declare function newKeyManager(privateKeyBytes: Uint8Array, publicKeyBytes: Uint8Array): KeyManager;
278
278
  /**
279
279
  * Generate a new API key pair
280
280
  * Returns [privateKey, publicKey] as hex strings
@@ -1622,8 +1622,9 @@ declare class TxClient {
1622
1622
  private readonly chainId;
1623
1623
  private readonly keyManager;
1624
1624
  private readonly _accountIndex;
1625
+ private readonly _pubkey;
1625
1626
  private readonly _apiKeyIndex;
1626
- constructor(apiClient: MinimalHTTPClient | null, privateKey: string, accountIndex: bigint, apiKeyIndex: number, chainId: number);
1627
+ constructor(apiClient: MinimalHTTPClient | null, privateKey: string, publicKey: string, accountIndex: bigint, apiKeyIndex: number, chainId: number);
1627
1628
  /**
1628
1629
  * Fill in default transaction options
1629
1630
  */
@@ -1657,9 +1658,15 @@ declare class TxClient {
1657
1658
  */
1658
1659
  getNextNonce: () => Promise<bigint>;
1659
1660
  /**
1660
- * Create an auth token
1661
+ * Create an auth token with caching
1662
+ * Generates a new token if:
1663
+ * 1. No cached token exists for this account
1664
+ * 2. The cached token expires in less than 1 hour
1665
+ *
1666
+ * @param deadline - Optional deadline for the token (defaults to 24 hours from now)
1667
+ * @returns Auth token string in format: "timestamp:accountIndex:apiKeyIndex:signature"
1661
1668
  */
1662
- getAuthToken(deadline: Date): string;
1669
+ getAuthToken(deadline?: Date): string;
1663
1670
  /**
1664
1671
  * Sign a create order transaction
1665
1672
  */
@@ -1736,7 +1743,7 @@ declare function getClient(apiKeyIndex?: number, accountIndex?: bigint): TxClien
1736
1743
  /**
1737
1744
  * Create a new TxClient and store it
1738
1745
  */
1739
- declare function createClient(httpClient: MinimalHTTPClient | null, privateKey: string, chainId: number, apiKeyIndex: number, accountIndex: bigint): TxClient;
1746
+ declare function createClient(httpClient: MinimalHTTPClient | null, privateKey: string, publicKey: string, chainId: number, apiKeyIndex: number, accountIndex: bigint): TxClient;
1740
1747
  /**
1741
1748
  * Check that a client exists and the API key matches the one on the server
1742
1749
  */
@@ -1887,6 +1894,7 @@ interface Candlestick {
1887
1894
  l: number;
1888
1895
  c: number;
1889
1896
  v: number;
1897
+ V: number;
1890
1898
  }
1891
1899
  interface CandlesRequest {
1892
1900
  market_id: number;
@@ -2949,7 +2957,7 @@ interface InfoPrivateClientConfig {
2949
2957
  *
2950
2958
  * @example
2951
2959
  * ```typescript
2952
- * const txClient = createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex);
2960
+ * const txClient = createClient(httpClient, privateKey, publicKey, chainId, apiKeyIndex, accountIndex);
2953
2961
  * const client = new InfoPrivateClient({
2954
2962
  * baseURL: "https://mainnet.zklighter.elliot.ai",
2955
2963
  * txClient
@@ -3346,7 +3354,7 @@ interface ExchangeClientConfig {
3346
3354
  *
3347
3355
  * @example
3348
3356
  * ```typescript
3349
- * const txClient = createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex);
3357
+ * const txClient = createClient(httpClient, privateKey, publicKey, chainId, apiKeyIndex, accountIndex);
3350
3358
  * const client = new ExchangeClient({
3351
3359
  * baseURL: "https://mainnet.zklighter.elliot.ai",
3352
3360
  * txClient
package/dist/index.js CHANGED
@@ -1962,14 +1962,19 @@ function schnorrSignHashedMessageDeterministic(hashedMessage, sk, k) {
1962
1962
 
1963
1963
  // src/signer/keyManager.ts
1964
1964
  var KeyManagerImpl = class {
1965
- constructor(privateKeyBytes) {
1965
+ constructor(privateKeyBytes, publicKeyBytes) {
1966
1966
  if (privateKeyBytes.length !== 40) {
1967
1967
  throw new Error(
1968
1968
  `Invalid private key length. expected: 40, got: ${privateKeyBytes.length}`
1969
1969
  );
1970
1970
  }
1971
+ if (publicKeyBytes.length !== 40) {
1972
+ throw new Error(
1973
+ `Invalid public key length. expected: 40, got: ${publicKeyBytes.length}`
1974
+ );
1975
+ }
1971
1976
  this.key = ECgFp5Scalar.fromLittleEndianBytes(privateKeyBytes);
1972
- this._pubKey = schnorrPkFromSk(this.key);
1977
+ this._pubKey = GFp5Element.fromLittleEndianBytes(publicKeyBytes);
1973
1978
  }
1974
1979
  sign(hashedMessage) {
1975
1980
  if (hashedMessage.length !== 40) {
@@ -1989,8 +1994,8 @@ var KeyManagerImpl = class {
1989
1994
  return this.key.toLittleEndianBytes();
1990
1995
  }
1991
1996
  };
1992
- function newKeyManager(privateKeyBytes) {
1993
- return new KeyManagerImpl(privateKeyBytes);
1997
+ function newKeyManager(privateKeyBytes, publicKeyBytes) {
1998
+ return new KeyManagerImpl(privateKeyBytes, publicKeyBytes);
1994
1999
  }
1995
2000
  async function generateApiKey(seed) {
1996
2001
  const key = seed ? await sampleScalarFromSeed(seed) : sampleScalar();
@@ -3595,8 +3600,11 @@ function bigIntSafeStringify(obj) {
3595
3600
  }
3596
3601
 
3597
3602
  // src/client/txClient.ts
3603
+ var EIGHT_HOUR_MS = 8 * 60 * 60 * 1e3;
3604
+ var HAFT_HOUR_MS = 30 * 60 * 1e3;
3605
+ var authTokenCache = /* @__PURE__ */ new Map();
3598
3606
  var TxClient = class {
3599
- constructor(apiClient, privateKey, accountIndex, apiKeyIndex, chainId) {
3607
+ constructor(apiClient, privateKey, publicKey, accountIndex, apiKeyIndex, chainId) {
3600
3608
  /**
3601
3609
  * Get the next nonce for the current account from the server
3602
3610
  */
@@ -3617,9 +3625,22 @@ var TxClient = class {
3617
3625
  `Invalid private key length: expected 40, got ${privateKeyBytes.length}`
3618
3626
  );
3619
3627
  }
3628
+ let publicKeyBytes;
3629
+ try {
3630
+ publicKeyBytes = hexToBytes(publicKey);
3631
+ } catch (e) {
3632
+ console.log("error parsing public key:", e);
3633
+ throw new Error("Invalid public key format");
3634
+ }
3635
+ if (publicKeyBytes.length !== 40) {
3636
+ throw new Error(
3637
+ `Invalid public key length: expected 40, got ${publicKeyBytes.length}`
3638
+ );
3639
+ }
3640
+ this._pubkey = publicKey;
3620
3641
  this.apiClient = apiClient;
3621
3642
  this.chainId = chainId;
3622
- this.keyManager = newKeyManager(privateKeyBytes);
3643
+ this.keyManager = newKeyManager(privateKeyBytes, publicKeyBytes);
3623
3644
  this._accountIndex = accountIndex;
3624
3645
  this._apiKeyIndex = apiKeyIndex;
3625
3646
  }
@@ -3702,16 +3723,37 @@ var TxClient = class {
3702
3723
  }
3703
3724
  }
3704
3725
  /**
3705
- * Create an auth token
3726
+ * Create an auth token with caching
3727
+ * Generates a new token if:
3728
+ * 1. No cached token exists for this account
3729
+ * 2. The cached token expires in less than 1 hour
3730
+ *
3731
+ * @param deadline - Optional deadline for the token (defaults to 24 hours from now)
3732
+ * @returns Auth token string in format: "timestamp:accountIndex:apiKeyIndex:signature"
3706
3733
  */
3707
3734
  getAuthToken(deadline) {
3708
- const message = `${Math.floor(deadline.getTime() / 1e3)}:${this._accountIndex}:${this._apiKeyIndex}`;
3735
+ const tokenDeadline = deadline || new Date(Date.now() + EIGHT_HOUR_MS);
3736
+ const cacheKey = `${this._accountIndex}:${this._apiKeyIndex}:${this._pubkey}`;
3737
+ const cached = authTokenCache.get(cacheKey);
3738
+ if (cached) {
3739
+ const now = Date.now();
3740
+ const timeUntilExpiry = cached.deadline.getTime();
3741
+ if (timeUntilExpiry > HAFT_HOUR_MS) {
3742
+ return cached.token;
3743
+ }
3744
+ }
3745
+ const message = `${Math.floor(tokenDeadline.getTime() / 1e3)}:${this._accountIndex}:${this._apiKeyIndex}`;
3709
3746
  const msgBytes = new TextEncoder().encode(message);
3710
3747
  const msgElements = bytesToFieldElementsCanonicalLE(msgBytes);
3711
3748
  const msgHash = hashToQuinticExtension(msgElements);
3712
3749
  const signatureBytes = this.keyManager.sign(msgHash);
3713
3750
  const signature = bytesToHex(signatureBytes);
3714
- return `${message}:${signature}`;
3751
+ const token = `${message}:${signature}`;
3752
+ authTokenCache.set(cacheKey, {
3753
+ token,
3754
+ deadline: tokenDeadline
3755
+ });
3756
+ return token;
3715
3757
  }
3716
3758
  /**
3717
3759
  * Sign a create order transaction
@@ -4060,13 +4102,14 @@ function getClient(apiKeyIndex = DEFAULT_API_KEY_INDEX, accountIndex = DEFAULT_A
4060
4102
  }
4061
4103
  return client;
4062
4104
  }
4063
- function createClient(httpClient, privateKey, chainId, apiKeyIndex, accountIndex) {
4105
+ function createClient(httpClient, privateKey, publicKey, chainId, apiKeyIndex, accountIndex) {
4064
4106
  if (accountIndex <= 0n) {
4065
4107
  throw new Error("Invalid account index");
4066
4108
  }
4067
4109
  const txClientInstance = new TxClient(
4068
4110
  httpClient,
4069
4111
  privateKey,
4112
+ publicKey,
4070
4113
  accountIndex,
4071
4114
  apiKeyIndex,
4072
4115
  chainId