@chainstream-io/sdk 2.0.10 → 2.0.11

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.
@@ -1,2 +1,2 @@
1
- export { C as ChainStreamClient, y as ChainStreamClientOptions, z as ChainStreamRequestContext, gI as PostOptions, T as TokenProvider } from './chainstream-LmorAvq7.cjs';
1
+ export { C as ChainStreamClient, a as ChainStreamClientOptions, b as ChainStreamRequestContext, gK as PostOptions, T as TokenProvider } from './chainstream-BQsiLm9_.cjs';
2
2
  import 'axios';
@@ -1,2 +1,2 @@
1
- export { C as ChainStreamClient, y as ChainStreamClientOptions, z as ChainStreamRequestContext, gI as PostOptions, T as TokenProvider } from './chainstream-LmorAvq7.js';
1
+ export { C as ChainStreamClient, a as ChainStreamClientOptions, b as ChainStreamRequestContext, gK as PostOptions, T as TokenProvider } from './chainstream-BQsiLm9_.js';
2
2
  import 'axios';
@@ -13,24 +13,65 @@ import { EventSourcePolyfill } from "event-source-polyfill";
13
13
  import Axios from "axios";
14
14
  import axiosRetryImport, { exponentialDelay } from "axios-retry";
15
15
 
16
- // src/wallet-auth.ts
16
+ // src/siwx-auth.ts
17
17
  import { randomBytes } from "crypto";
18
- var SIGNATURE_PREFIX = "chainstream";
19
- function buildSignMessage(chain, address, timestamp, nonce) {
20
- return `${SIGNATURE_PREFIX}:${chain}:${address}:${timestamp}:${nonce}`;
18
+ var SIWX_DOMAIN = "api.chainstream.io";
19
+ var SIWX_URI = "https://api.chainstream.io";
20
+ var SIWX_VERSION = "1";
21
+ var DEFAULT_CHAIN_ID = "8453";
22
+ var DEFAULT_EXPIRES_IN_SECONDS = 3600;
23
+ var REFRESH_BEFORE_SECONDS = 60;
24
+ function buildSiwxMessage(address, chain, nonce, issuedAt, expiresAt) {
25
+ const chainLabel = chain === "evm" ? "Ethereum" : "Solana";
26
+ const chainId = chain === "evm" ? DEFAULT_CHAIN_ID : "mainnet";
27
+ return [
28
+ `${SIWX_DOMAIN} wants you to sign in with your ${chainLabel} account:`,
29
+ address,
30
+ "",
31
+ "Sign in to ChainStream API",
32
+ "",
33
+ `URI: ${SIWX_URI}`,
34
+ `Version: ${SIWX_VERSION}`,
35
+ `Chain ID: ${chainId}`,
36
+ `Nonce: ${nonce}`,
37
+ `Issued At: ${issuedAt.toISOString()}`,
38
+ `Expiration Time: ${expiresAt.toISOString()}`
39
+ ].join("\n");
21
40
  }
22
- async function createWalletAuthHeaders(signer) {
23
- const timestamp = Math.floor(Date.now() / 1e3).toString();
41
+ async function createSiwxToken(signer, expiresInSeconds = DEFAULT_EXPIRES_IN_SECONDS) {
24
42
  const nonce = randomBytes(16).toString("hex");
25
- const message = buildSignMessage(signer.chain, signer.address, timestamp, nonce);
43
+ const issuedAt = /* @__PURE__ */ new Date();
44
+ const expiresAt = new Date(Date.now() + expiresInSeconds * 1e3);
45
+ const message = buildSiwxMessage(signer.address, signer.chain, nonce, issuedAt, expiresAt);
26
46
  const signature = await signer.signMessage(message);
27
- return {
28
- "X-Wallet-Address": signer.address,
29
- "X-Wallet-Chain": signer.chain,
30
- "X-Wallet-Signature": signature,
31
- "X-Wallet-Timestamp": timestamp,
32
- "X-Wallet-Nonce": nonce
33
- };
47
+ const messageB64 = Buffer.from(message).toString("base64");
48
+ const token = `${messageB64}.${signature}`;
49
+ return { token, expiresAt };
50
+ }
51
+ var cachedToken = null;
52
+ var cachedExpiresAt = null;
53
+ var refreshPromise = null;
54
+ async function getSiwxToken(signer) {
55
+ if (cachedToken && cachedExpiresAt) {
56
+ const now = /* @__PURE__ */ new Date();
57
+ const refreshThreshold = new Date(cachedExpiresAt.getTime() - REFRESH_BEFORE_SECONDS * 1e3);
58
+ if (now < refreshThreshold) {
59
+ return cachedToken;
60
+ }
61
+ if (now < cachedExpiresAt && !refreshPromise) {
62
+ refreshPromise = refreshToken(signer).finally(() => {
63
+ refreshPromise = null;
64
+ });
65
+ return cachedToken;
66
+ }
67
+ }
68
+ await refreshToken(signer);
69
+ return cachedToken;
70
+ }
71
+ async function refreshToken(signer) {
72
+ const { token, expiresAt } = await createSiwxToken(signer);
73
+ cachedToken = token;
74
+ cachedExpiresAt = expiresAt;
34
75
  }
35
76
 
36
77
  // src/openapi-client/chainstreamApiClient.ts
@@ -51,10 +92,8 @@ var configure = (options) => {
51
92
  });
52
93
  axiosInstance.interceptors.request.use(async (config) => {
53
94
  if (options.walletSigner) {
54
- const walletHeaders = await createWalletAuthHeaders(options.walletSigner);
55
- Object.entries(walletHeaders).forEach(([key, value]) => {
56
- config.headers[key] = value;
57
- });
95
+ const token = await getSiwxToken(options.walletSigner);
96
+ config.headers.Authorization = `SIWX ${token}`;
58
97
  } else if (options.apiKey) {
59
98
  config.headers["X-API-KEY"] = options.apiKey;
60
99
  } else if (options.accessToken) {