@routstr/sdk 0.3.7 → 0.3.9

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.
Files changed (39) hide show
  1. package/dist/client/index.d.mts +411 -0
  2. package/dist/client/index.d.ts +411 -0
  3. package/dist/client/index.js +4938 -0
  4. package/dist/client/index.js.map +1 -0
  5. package/dist/client/index.mjs +4932 -0
  6. package/dist/client/index.mjs.map +1 -0
  7. package/dist/discovery/index.d.mts +247 -0
  8. package/dist/discovery/index.d.ts +247 -0
  9. package/dist/discovery/index.js +894 -0
  10. package/dist/discovery/index.js.map +1 -0
  11. package/dist/discovery/index.mjs +891 -0
  12. package/dist/discovery/index.mjs.map +1 -0
  13. package/dist/index.d.mts +195 -0
  14. package/dist/index.d.ts +195 -0
  15. package/dist/index.js +6797 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/index.mjs +6746 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/interfaces-C-DYd9Jy.d.ts +176 -0
  20. package/dist/interfaces-Csn8Uq04.d.mts +176 -0
  21. package/dist/interfaces-Cv1k2EUK.d.mts +118 -0
  22. package/dist/interfaces-iL7CWeG5.d.ts +118 -0
  23. package/dist/storage/index.d.mts +113 -0
  24. package/dist/storage/index.d.ts +113 -0
  25. package/dist/storage/index.js +2019 -0
  26. package/dist/storage/index.js.map +1 -0
  27. package/dist/storage/index.mjs +1995 -0
  28. package/dist/storage/index.mjs.map +1 -0
  29. package/dist/store-58VcEUoA.d.ts +172 -0
  30. package/dist/store-C6dfj1cc.d.mts +172 -0
  31. package/dist/types-_21yYFZG.d.mts +234 -0
  32. package/dist/types-_21yYFZG.d.ts +234 -0
  33. package/dist/wallet/index.d.mts +249 -0
  34. package/dist/wallet/index.d.ts +249 -0
  35. package/dist/wallet/index.js +1383 -0
  36. package/dist/wallet/index.js.map +1 -0
  37. package/dist/wallet/index.mjs +1380 -0
  38. package/dist/wallet/index.mjs.map +1 -0
  39. package/package.json +3 -2
@@ -0,0 +1,249 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-Csn8Uq04.mjs';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks, X as XCashuTokenEntry } from '../interfaces-Csn8Uq04.mjs';
3
+ import { S as SdkLogger, R as RefundResult, T as TopUpResult, l as SpendResult } from '../types-_21yYFZG.mjs';
4
+ export { k as ProviderInfo } from '../types-_21yYFZG.mjs';
5
+
6
+ /**
7
+ * BalanceManager - Handles refunding and topping up tokens from providers
8
+ *
9
+ * Handles:
10
+ * - Fetching refund tokens from provider API
11
+ * - Receiving/storing refunded tokens
12
+ * - Topping up API key balances with cashu tokens
13
+ * - Error handling for various refund/topup failure modes
14
+ *
15
+ * Extracted from utils/cashuUtils.ts
16
+ */
17
+
18
+ /**
19
+ * Options for refunding API key balance
20
+ */
21
+ interface RefundApiKeyOptions {
22
+ /** The mint URL (for NIP-60 wallet operations) */
23
+ mintUrl: string;
24
+ /** The provider base URL */
25
+ baseUrl: string;
26
+ /** The API key to use for authentication */
27
+ apiKey: string;
28
+ /** If true, forces refund even if the API key was used recently */
29
+ forceRefund?: boolean;
30
+ }
31
+ /**
32
+ * Options for topping up API key balance
33
+ */
34
+ interface TopUpOptions {
35
+ /** The mint URL to spend from */
36
+ mintUrl: string;
37
+ /** The provider base URL */
38
+ baseUrl: string;
39
+ /** Amount to top up in sats */
40
+ amount: number;
41
+ /** Optional specific API key to top up (if not provided, uses stored token) */
42
+ token?: string;
43
+ }
44
+ interface CreateProviderTokenOptions {
45
+ mintUrl: string;
46
+ baseUrl: string;
47
+ amount: number;
48
+ p2pkPubkey?: string;
49
+ excludeMints?: string[];
50
+ retryCount?: number;
51
+ }
52
+ interface ProviderTokenResult {
53
+ success: boolean;
54
+ token?: string;
55
+ error?: string;
56
+ selectedMintUrl?: string;
57
+ amountSpent?: number;
58
+ }
59
+ interface BalanceState {
60
+ totalBalance: number;
61
+ providerBalances: Record<string, number>;
62
+ mintBalances: Record<string, number>;
63
+ }
64
+ /**
65
+ * BalanceManager handles token refunds and topups from providers
66
+ */
67
+ declare class BalanceManager {
68
+ private walletAdapter;
69
+ private storageAdapter;
70
+ private providerRegistry?;
71
+ private cashuSpender;
72
+ /** In-memory guard for per-provider wallet mutations (topup / refund) */
73
+ private providerWalletOps;
74
+ /** Cooldown (ms) between opposite operations on the same provider */
75
+ private static readonly PROVIDER_WALLET_COOLDOWN_MS;
76
+ private readonly logger;
77
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender, logger?: SdkLogger);
78
+ /**
79
+ * Check whether a wallet operation (topup/refund) may run for a provider.
80
+ * Returns the reason when blocked.
81
+ */
82
+ private _canRunProviderWalletOperation;
83
+ private _beginProviderWalletOperation;
84
+ private _endProviderWalletOperation;
85
+ getBalanceState(): Promise<BalanceState>;
86
+ /**
87
+ * Refund API key balance - convert remaining API key balance to cashu token
88
+ * @param options - Refund options including forceRefund flag
89
+ * @returns Refund result
90
+ */
91
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
92
+ private _refundApiKeyImpl;
93
+ /**
94
+ * Fetch refund token from provider API using API key (or xcashu token) authentication
95
+ */
96
+ fetchRefundToken(baseUrl: string, apiKeyOrToken: string, xCashu?: boolean): Promise<{
97
+ success: boolean;
98
+ token?: string;
99
+ requestId?: string;
100
+ error?: string;
101
+ }>;
102
+ /**
103
+ * Top up API key balance with a cashu token
104
+ */
105
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
106
+ private _topUpImpl;
107
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
108
+ private _selectCandidateMints;
109
+ private _refundOtherProvidersForTopUp;
110
+ /**
111
+ * Post topup request to provider API
112
+ */
113
+ private _postTopUp;
114
+ /**
115
+ * Attempt to receive token back after failed top up
116
+ */
117
+ private _recoverFailedTopUp;
118
+ /**
119
+ * Handle refund errors with specific error types
120
+ */
121
+ private _handleRefundError;
122
+ /**
123
+ * Get token balance from provider
124
+ */
125
+ getTokenBalance(token: string, baseUrl: string): Promise<{
126
+ amount: number;
127
+ reserved: number;
128
+ unit: "sat" | "msat";
129
+ apiKey: string;
130
+ isInvalidApiKey?: boolean;
131
+ /** True when the balance could not be determined (network error, non-OK
132
+ * response, etc.). Callers MUST NOT use `amount` in arithmetic when
133
+ * this flag is set — it is 0, not a real balance. */
134
+ balanceUnknown?: boolean;
135
+ }>;
136
+ /**
137
+ * Handle topup errors with specific error types
138
+ */
139
+ private _handleTopUpError;
140
+ }
141
+
142
+ /**
143
+ * CashuSpender - Core spending logic for Cashu tokens
144
+ *
145
+ * Handles:
146
+ * - Mint selection with sufficient balance
147
+ * - Provider mint compatibility checks
148
+ * - Retry logic with alternate mints
149
+ * - Critical section management (busy state)
150
+ *
151
+ * Extracted from hooks/useCashuWithXYZ.ts
152
+ */
153
+
154
+ /**
155
+ * Options for spending cashu tokens
156
+ */
157
+ interface SpendOptions {
158
+ /** The mint URL to send from (can be overridden if insufficient balance) */
159
+ mintUrl: string;
160
+ /** The amount to spend in sats */
161
+ amount: number;
162
+ /** The provider base URL (for token storage and provider mint checks) */
163
+ baseUrl: string;
164
+ /** Whether to reuse an existing token if available */
165
+ reuseToken?: boolean;
166
+ /** Optional P2PK public key */
167
+ p2pkPubkey?: string;
168
+ /** Array of mint URLs to exclude (for retry logic) */
169
+ excludeMints?: string[];
170
+ /** Current retry count (for internal recursion) */
171
+ retryCount?: number;
172
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
173
+ refundBaseUrls?: string[];
174
+ }
175
+ type DebugLevel = "DEBUG" | "WARN" | "ERROR";
176
+ /**
177
+ * CashuSpender manages the spending of Cashu tokens
178
+ */
179
+ declare class CashuSpender {
180
+ private walletAdapter;
181
+ private storageAdapter;
182
+ private _providerRegistry?;
183
+ private balanceManager?;
184
+ private _isBusy;
185
+ private debugLevel;
186
+ private readonly logger;
187
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined, logger?: SdkLogger);
188
+ receiveToken(token: string): Promise<{
189
+ success: boolean;
190
+ amount: number;
191
+ unit: "sat" | "msat";
192
+ message?: string;
193
+ }>;
194
+ private _decodeTokenAmount;
195
+ private _getBalanceState;
196
+ private _logTransaction;
197
+ /**
198
+ * Check if the spender is currently in a critical operation
199
+ */
200
+ get isBusy(): boolean;
201
+ getDebugLevel(): DebugLevel;
202
+ setDebugLevel(level: DebugLevel): void;
203
+ private _log;
204
+ /**
205
+ * Spend Cashu tokens with automatic mint selection and retry logic
206
+ * Throws errors on failure instead of returning failed SpendResult
207
+ */
208
+ spend(options: SpendOptions): Promise<SpendResult>;
209
+ /**
210
+ * Check if error message indicates a network error
211
+ */
212
+ private _isNetworkError;
213
+ /**
214
+ * Internal spending logic
215
+ */
216
+ private _spendInternal;
217
+ /**
218
+ * Try to reuse an existing API key
219
+ */
220
+ private _tryReuseToken;
221
+ /**
222
+ * Refund all xcashu tokens from storage by calling the provider's refund endpoint.
223
+ * The xcashu token acts as an API key to claim the refund, and the response contains
224
+ * the actual refunded Cashu token which is then received into the wallet.
225
+ * @param mintUrl - The mint URL for receiving tokens
226
+ * @param excludeBaseUrls - Base URLs to exclude from refund (optional)
227
+ * @returns Results for each xcashu token refund attempt
228
+ */
229
+ refundXcashuTokens(mintUrl: string, excludeBaseUrls?: string[]): Promise<{
230
+ baseUrl: string;
231
+ token: string;
232
+ success: boolean;
233
+ error?: string;
234
+ }[]>;
235
+ /**
236
+ * Refund specific providers without retrying spend
237
+ */
238
+ refundProviders(mintUrl: string, forceRefund?: boolean): Promise<{
239
+ baseUrl: string;
240
+ success: boolean;
241
+ }[]>;
242
+ /**
243
+ * Create an insufficient balance error result
244
+ */
245
+ private _createInsufficientBalanceError;
246
+ private _getProviderTokenBalance;
247
+ }
248
+
249
+ export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
@@ -0,0 +1,249 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-C-DYd9Jy.js';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks, X as XCashuTokenEntry } from '../interfaces-C-DYd9Jy.js';
3
+ import { S as SdkLogger, R as RefundResult, T as TopUpResult, l as SpendResult } from '../types-_21yYFZG.js';
4
+ export { k as ProviderInfo } from '../types-_21yYFZG.js';
5
+
6
+ /**
7
+ * BalanceManager - Handles refunding and topping up tokens from providers
8
+ *
9
+ * Handles:
10
+ * - Fetching refund tokens from provider API
11
+ * - Receiving/storing refunded tokens
12
+ * - Topping up API key balances with cashu tokens
13
+ * - Error handling for various refund/topup failure modes
14
+ *
15
+ * Extracted from utils/cashuUtils.ts
16
+ */
17
+
18
+ /**
19
+ * Options for refunding API key balance
20
+ */
21
+ interface RefundApiKeyOptions {
22
+ /** The mint URL (for NIP-60 wallet operations) */
23
+ mintUrl: string;
24
+ /** The provider base URL */
25
+ baseUrl: string;
26
+ /** The API key to use for authentication */
27
+ apiKey: string;
28
+ /** If true, forces refund even if the API key was used recently */
29
+ forceRefund?: boolean;
30
+ }
31
+ /**
32
+ * Options for topping up API key balance
33
+ */
34
+ interface TopUpOptions {
35
+ /** The mint URL to spend from */
36
+ mintUrl: string;
37
+ /** The provider base URL */
38
+ baseUrl: string;
39
+ /** Amount to top up in sats */
40
+ amount: number;
41
+ /** Optional specific API key to top up (if not provided, uses stored token) */
42
+ token?: string;
43
+ }
44
+ interface CreateProviderTokenOptions {
45
+ mintUrl: string;
46
+ baseUrl: string;
47
+ amount: number;
48
+ p2pkPubkey?: string;
49
+ excludeMints?: string[];
50
+ retryCount?: number;
51
+ }
52
+ interface ProviderTokenResult {
53
+ success: boolean;
54
+ token?: string;
55
+ error?: string;
56
+ selectedMintUrl?: string;
57
+ amountSpent?: number;
58
+ }
59
+ interface BalanceState {
60
+ totalBalance: number;
61
+ providerBalances: Record<string, number>;
62
+ mintBalances: Record<string, number>;
63
+ }
64
+ /**
65
+ * BalanceManager handles token refunds and topups from providers
66
+ */
67
+ declare class BalanceManager {
68
+ private walletAdapter;
69
+ private storageAdapter;
70
+ private providerRegistry?;
71
+ private cashuSpender;
72
+ /** In-memory guard for per-provider wallet mutations (topup / refund) */
73
+ private providerWalletOps;
74
+ /** Cooldown (ms) between opposite operations on the same provider */
75
+ private static readonly PROVIDER_WALLET_COOLDOWN_MS;
76
+ private readonly logger;
77
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender, logger?: SdkLogger);
78
+ /**
79
+ * Check whether a wallet operation (topup/refund) may run for a provider.
80
+ * Returns the reason when blocked.
81
+ */
82
+ private _canRunProviderWalletOperation;
83
+ private _beginProviderWalletOperation;
84
+ private _endProviderWalletOperation;
85
+ getBalanceState(): Promise<BalanceState>;
86
+ /**
87
+ * Refund API key balance - convert remaining API key balance to cashu token
88
+ * @param options - Refund options including forceRefund flag
89
+ * @returns Refund result
90
+ */
91
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
92
+ private _refundApiKeyImpl;
93
+ /**
94
+ * Fetch refund token from provider API using API key (or xcashu token) authentication
95
+ */
96
+ fetchRefundToken(baseUrl: string, apiKeyOrToken: string, xCashu?: boolean): Promise<{
97
+ success: boolean;
98
+ token?: string;
99
+ requestId?: string;
100
+ error?: string;
101
+ }>;
102
+ /**
103
+ * Top up API key balance with a cashu token
104
+ */
105
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
106
+ private _topUpImpl;
107
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
108
+ private _selectCandidateMints;
109
+ private _refundOtherProvidersForTopUp;
110
+ /**
111
+ * Post topup request to provider API
112
+ */
113
+ private _postTopUp;
114
+ /**
115
+ * Attempt to receive token back after failed top up
116
+ */
117
+ private _recoverFailedTopUp;
118
+ /**
119
+ * Handle refund errors with specific error types
120
+ */
121
+ private _handleRefundError;
122
+ /**
123
+ * Get token balance from provider
124
+ */
125
+ getTokenBalance(token: string, baseUrl: string): Promise<{
126
+ amount: number;
127
+ reserved: number;
128
+ unit: "sat" | "msat";
129
+ apiKey: string;
130
+ isInvalidApiKey?: boolean;
131
+ /** True when the balance could not be determined (network error, non-OK
132
+ * response, etc.). Callers MUST NOT use `amount` in arithmetic when
133
+ * this flag is set — it is 0, not a real balance. */
134
+ balanceUnknown?: boolean;
135
+ }>;
136
+ /**
137
+ * Handle topup errors with specific error types
138
+ */
139
+ private _handleTopUpError;
140
+ }
141
+
142
+ /**
143
+ * CashuSpender - Core spending logic for Cashu tokens
144
+ *
145
+ * Handles:
146
+ * - Mint selection with sufficient balance
147
+ * - Provider mint compatibility checks
148
+ * - Retry logic with alternate mints
149
+ * - Critical section management (busy state)
150
+ *
151
+ * Extracted from hooks/useCashuWithXYZ.ts
152
+ */
153
+
154
+ /**
155
+ * Options for spending cashu tokens
156
+ */
157
+ interface SpendOptions {
158
+ /** The mint URL to send from (can be overridden if insufficient balance) */
159
+ mintUrl: string;
160
+ /** The amount to spend in sats */
161
+ amount: number;
162
+ /** The provider base URL (for token storage and provider mint checks) */
163
+ baseUrl: string;
164
+ /** Whether to reuse an existing token if available */
165
+ reuseToken?: boolean;
166
+ /** Optional P2PK public key */
167
+ p2pkPubkey?: string;
168
+ /** Array of mint URLs to exclude (for retry logic) */
169
+ excludeMints?: string[];
170
+ /** Current retry count (for internal recursion) */
171
+ retryCount?: number;
172
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
173
+ refundBaseUrls?: string[];
174
+ }
175
+ type DebugLevel = "DEBUG" | "WARN" | "ERROR";
176
+ /**
177
+ * CashuSpender manages the spending of Cashu tokens
178
+ */
179
+ declare class CashuSpender {
180
+ private walletAdapter;
181
+ private storageAdapter;
182
+ private _providerRegistry?;
183
+ private balanceManager?;
184
+ private _isBusy;
185
+ private debugLevel;
186
+ private readonly logger;
187
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined, logger?: SdkLogger);
188
+ receiveToken(token: string): Promise<{
189
+ success: boolean;
190
+ amount: number;
191
+ unit: "sat" | "msat";
192
+ message?: string;
193
+ }>;
194
+ private _decodeTokenAmount;
195
+ private _getBalanceState;
196
+ private _logTransaction;
197
+ /**
198
+ * Check if the spender is currently in a critical operation
199
+ */
200
+ get isBusy(): boolean;
201
+ getDebugLevel(): DebugLevel;
202
+ setDebugLevel(level: DebugLevel): void;
203
+ private _log;
204
+ /**
205
+ * Spend Cashu tokens with automatic mint selection and retry logic
206
+ * Throws errors on failure instead of returning failed SpendResult
207
+ */
208
+ spend(options: SpendOptions): Promise<SpendResult>;
209
+ /**
210
+ * Check if error message indicates a network error
211
+ */
212
+ private _isNetworkError;
213
+ /**
214
+ * Internal spending logic
215
+ */
216
+ private _spendInternal;
217
+ /**
218
+ * Try to reuse an existing API key
219
+ */
220
+ private _tryReuseToken;
221
+ /**
222
+ * Refund all xcashu tokens from storage by calling the provider's refund endpoint.
223
+ * The xcashu token acts as an API key to claim the refund, and the response contains
224
+ * the actual refunded Cashu token which is then received into the wallet.
225
+ * @param mintUrl - The mint URL for receiving tokens
226
+ * @param excludeBaseUrls - Base URLs to exclude from refund (optional)
227
+ * @returns Results for each xcashu token refund attempt
228
+ */
229
+ refundXcashuTokens(mintUrl: string, excludeBaseUrls?: string[]): Promise<{
230
+ baseUrl: string;
231
+ token: string;
232
+ success: boolean;
233
+ error?: string;
234
+ }[]>;
235
+ /**
236
+ * Refund specific providers without retrying spend
237
+ */
238
+ refundProviders(mintUrl: string, forceRefund?: boolean): Promise<{
239
+ baseUrl: string;
240
+ success: boolean;
241
+ }[]>;
242
+ /**
243
+ * Create an insufficient balance error result
244
+ */
245
+ private _createInsufficientBalanceError;
246
+ private _getProviderTokenBalance;
247
+ }
248
+
249
+ export { BalanceManager, type BalanceState, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };