@routstr/sdk 0.1.0 → 0.1.2

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.
@@ -0,0 +1,218 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-B85Wx7ni.mjs';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-B85Wx7ni.mjs';
3
+ import { R as RefundResult, m as TopUpResult, S as SpendResult } from '../types-BlHjmWRK.mjs';
4
+ export { P as ProviderInfo } from '../types-BlHjmWRK.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 tokens
20
+ */
21
+ interface RefundOptions {
22
+ /** The mint URL (for NIP-60 wallet operations) */
23
+ mintUrl: string;
24
+ /** The provider base URL */
25
+ baseUrl: string;
26
+ /** Optional specific token to refund (if not provided, uses stored token) */
27
+ token?: string;
28
+ }
29
+ /**
30
+ * Options for refunding API key balance
31
+ */
32
+ interface RefundApiKeyOptions {
33
+ /** The mint URL (for NIP-60 wallet operations) */
34
+ mintUrl: string;
35
+ /** The provider base URL */
36
+ baseUrl: string;
37
+ /** The API key to use for authentication */
38
+ apiKey: string;
39
+ }
40
+ /**
41
+ * Options for topping up API key balance
42
+ */
43
+ interface TopUpOptions {
44
+ /** The mint URL to spend from */
45
+ mintUrl: string;
46
+ /** The provider base URL */
47
+ baseUrl: string;
48
+ /** Amount to top up in sats */
49
+ amount: number;
50
+ /** Optional specific API key to top up (if not provided, uses stored token) */
51
+ token?: string;
52
+ }
53
+ interface CreateProviderTokenOptions {
54
+ mintUrl: string;
55
+ baseUrl: string;
56
+ amount: number;
57
+ p2pkPubkey?: string;
58
+ excludeMints?: string[];
59
+ retryCount?: number;
60
+ }
61
+ interface ProviderTokenResult {
62
+ success: boolean;
63
+ token?: string;
64
+ error?: string;
65
+ selectedMintUrl?: string;
66
+ amountSpent?: number;
67
+ }
68
+ /**
69
+ * BalanceManager handles token refunds and topups from providers
70
+ */
71
+ declare class BalanceManager {
72
+ private walletAdapter;
73
+ private storageAdapter;
74
+ private providerRegistry?;
75
+ private cashuSpender;
76
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender);
77
+ /**
78
+ * Unified refund - handles both NIP-60 and legacy wallet refunds
79
+ */
80
+ refund(options: RefundOptions): Promise<RefundResult>;
81
+ /**
82
+ * Refund API key balance - convert remaining API key balance to cashu token
83
+ */
84
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
85
+ /**
86
+ * Fetch refund token from provider API using API key authentication
87
+ */
88
+ private _fetchRefundTokenWithApiKey;
89
+ /**
90
+ * Top up API key balance with a cashu token
91
+ */
92
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
93
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
94
+ private _selectCandidateMints;
95
+ private _refundOtherProvidersForTopUp;
96
+ /**
97
+ * Fetch refund token from provider API
98
+ */
99
+ private _fetchRefundToken;
100
+ /**
101
+ * Post topup request to provider API
102
+ */
103
+ private _postTopUp;
104
+ /**
105
+ * Attempt to receive token back after failed top up
106
+ */
107
+ private _recoverFailedTopUp;
108
+ /**
109
+ * Handle refund errors with specific error types
110
+ */
111
+ private _handleRefundError;
112
+ /**
113
+ * Get token balance from provider
114
+ */
115
+ getTokenBalance(token: string, baseUrl: string): Promise<{
116
+ amount: number;
117
+ reserved: number;
118
+ unit: "sat" | "msat";
119
+ apiKey: string;
120
+ }>;
121
+ /**
122
+ * Handle topup errors with specific error types
123
+ */
124
+ private _handleTopUpError;
125
+ }
126
+
127
+ /**
128
+ * CashuSpender - Core spending logic for Cashu tokens
129
+ *
130
+ * Handles:
131
+ * - Mint selection with sufficient balance
132
+ * - Provider mint compatibility checks
133
+ * - Retry logic with alternate mints
134
+ * - Critical section management (busy state)
135
+ *
136
+ * Extracted from hooks/useCashuWithXYZ.ts
137
+ */
138
+
139
+ /**
140
+ * Options for spending cashu tokens
141
+ */
142
+ interface SpendOptions {
143
+ /** The mint URL to send from (can be overridden if insufficient balance) */
144
+ mintUrl: string;
145
+ /** The amount to spend in sats */
146
+ amount: number;
147
+ /** The provider base URL (for token storage and provider mint checks) */
148
+ baseUrl: string;
149
+ /** Whether to reuse an existing token if available */
150
+ reuseToken?: boolean;
151
+ /** Optional P2PK public key */
152
+ p2pkPubkey?: string;
153
+ /** Array of mint URLs to exclude (for retry logic) */
154
+ excludeMints?: string[];
155
+ /** Current retry count (for internal recursion) */
156
+ retryCount?: number;
157
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
158
+ refundBaseUrls?: string[];
159
+ }
160
+ type DebugLevel = "DEBUG" | "WARN" | "ERROR";
161
+ /**
162
+ * CashuSpender manages the spending of Cashu tokens
163
+ */
164
+ declare class CashuSpender {
165
+ private walletAdapter;
166
+ private storageAdapter;
167
+ private _providerRegistry?;
168
+ private balanceManager?;
169
+ private _isBusy;
170
+ private debugLevel;
171
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined);
172
+ receiveToken(token: string): Promise<{
173
+ success: boolean;
174
+ amount: number;
175
+ unit: "sat" | "msat";
176
+ message?: string;
177
+ }>;
178
+ private _getBalanceState;
179
+ private _logTransaction;
180
+ /**
181
+ * Check if the spender is currently in a critical operation
182
+ */
183
+ get isBusy(): boolean;
184
+ getDebugLevel(): DebugLevel;
185
+ setDebugLevel(level: DebugLevel): void;
186
+ private _log;
187
+ /**
188
+ * Spend Cashu tokens with automatic mint selection and retry logic
189
+ * Throws errors on failure instead of returning failed SpendResult
190
+ */
191
+ spend(options: SpendOptions): Promise<SpendResult>;
192
+ /**
193
+ * Check if error message indicates a network error
194
+ */
195
+ private _isNetworkError;
196
+ /**
197
+ * Internal spending logic
198
+ */
199
+ private _spendInternal;
200
+ /**
201
+ * Try to reuse an existing token
202
+ */
203
+ private _tryReuseToken;
204
+ /**
205
+ * Refund specific providers without retrying spend
206
+ */
207
+ refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean): Promise<{
208
+ baseUrl: string;
209
+ success: boolean;
210
+ }[]>;
211
+ /**
212
+ * Create an insufficient balance error result
213
+ */
214
+ private _createInsufficientBalanceError;
215
+ private _getProviderTokenBalance;
216
+ }
217
+
218
+ export { BalanceManager, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
@@ -0,0 +1,218 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-BVNyAmKu.js';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-BVNyAmKu.js';
3
+ import { R as RefundResult, m as TopUpResult, S as SpendResult } from '../types-BlHjmWRK.js';
4
+ export { P as ProviderInfo } from '../types-BlHjmWRK.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 tokens
20
+ */
21
+ interface RefundOptions {
22
+ /** The mint URL (for NIP-60 wallet operations) */
23
+ mintUrl: string;
24
+ /** The provider base URL */
25
+ baseUrl: string;
26
+ /** Optional specific token to refund (if not provided, uses stored token) */
27
+ token?: string;
28
+ }
29
+ /**
30
+ * Options for refunding API key balance
31
+ */
32
+ interface RefundApiKeyOptions {
33
+ /** The mint URL (for NIP-60 wallet operations) */
34
+ mintUrl: string;
35
+ /** The provider base URL */
36
+ baseUrl: string;
37
+ /** The API key to use for authentication */
38
+ apiKey: string;
39
+ }
40
+ /**
41
+ * Options for topping up API key balance
42
+ */
43
+ interface TopUpOptions {
44
+ /** The mint URL to spend from */
45
+ mintUrl: string;
46
+ /** The provider base URL */
47
+ baseUrl: string;
48
+ /** Amount to top up in sats */
49
+ amount: number;
50
+ /** Optional specific API key to top up (if not provided, uses stored token) */
51
+ token?: string;
52
+ }
53
+ interface CreateProviderTokenOptions {
54
+ mintUrl: string;
55
+ baseUrl: string;
56
+ amount: number;
57
+ p2pkPubkey?: string;
58
+ excludeMints?: string[];
59
+ retryCount?: number;
60
+ }
61
+ interface ProviderTokenResult {
62
+ success: boolean;
63
+ token?: string;
64
+ error?: string;
65
+ selectedMintUrl?: string;
66
+ amountSpent?: number;
67
+ }
68
+ /**
69
+ * BalanceManager handles token refunds and topups from providers
70
+ */
71
+ declare class BalanceManager {
72
+ private walletAdapter;
73
+ private storageAdapter;
74
+ private providerRegistry?;
75
+ private cashuSpender;
76
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined, cashuSpender?: CashuSpender);
77
+ /**
78
+ * Unified refund - handles both NIP-60 and legacy wallet refunds
79
+ */
80
+ refund(options: RefundOptions): Promise<RefundResult>;
81
+ /**
82
+ * Refund API key balance - convert remaining API key balance to cashu token
83
+ */
84
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
85
+ /**
86
+ * Fetch refund token from provider API using API key authentication
87
+ */
88
+ private _fetchRefundTokenWithApiKey;
89
+ /**
90
+ * Top up API key balance with a cashu token
91
+ */
92
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
93
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
94
+ private _selectCandidateMints;
95
+ private _refundOtherProvidersForTopUp;
96
+ /**
97
+ * Fetch refund token from provider API
98
+ */
99
+ private _fetchRefundToken;
100
+ /**
101
+ * Post topup request to provider API
102
+ */
103
+ private _postTopUp;
104
+ /**
105
+ * Attempt to receive token back after failed top up
106
+ */
107
+ private _recoverFailedTopUp;
108
+ /**
109
+ * Handle refund errors with specific error types
110
+ */
111
+ private _handleRefundError;
112
+ /**
113
+ * Get token balance from provider
114
+ */
115
+ getTokenBalance(token: string, baseUrl: string): Promise<{
116
+ amount: number;
117
+ reserved: number;
118
+ unit: "sat" | "msat";
119
+ apiKey: string;
120
+ }>;
121
+ /**
122
+ * Handle topup errors with specific error types
123
+ */
124
+ private _handleTopUpError;
125
+ }
126
+
127
+ /**
128
+ * CashuSpender - Core spending logic for Cashu tokens
129
+ *
130
+ * Handles:
131
+ * - Mint selection with sufficient balance
132
+ * - Provider mint compatibility checks
133
+ * - Retry logic with alternate mints
134
+ * - Critical section management (busy state)
135
+ *
136
+ * Extracted from hooks/useCashuWithXYZ.ts
137
+ */
138
+
139
+ /**
140
+ * Options for spending cashu tokens
141
+ */
142
+ interface SpendOptions {
143
+ /** The mint URL to send from (can be overridden if insufficient balance) */
144
+ mintUrl: string;
145
+ /** The amount to spend in sats */
146
+ amount: number;
147
+ /** The provider base URL (for token storage and provider mint checks) */
148
+ baseUrl: string;
149
+ /** Whether to reuse an existing token if available */
150
+ reuseToken?: boolean;
151
+ /** Optional P2PK public key */
152
+ p2pkPubkey?: string;
153
+ /** Array of mint URLs to exclude (for retry logic) */
154
+ excludeMints?: string[];
155
+ /** Current retry count (for internal recursion) */
156
+ retryCount?: number;
157
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
158
+ refundBaseUrls?: string[];
159
+ }
160
+ type DebugLevel = "DEBUG" | "WARN" | "ERROR";
161
+ /**
162
+ * CashuSpender manages the spending of Cashu tokens
163
+ */
164
+ declare class CashuSpender {
165
+ private walletAdapter;
166
+ private storageAdapter;
167
+ private _providerRegistry?;
168
+ private balanceManager?;
169
+ private _isBusy;
170
+ private debugLevel;
171
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined);
172
+ receiveToken(token: string): Promise<{
173
+ success: boolean;
174
+ amount: number;
175
+ unit: "sat" | "msat";
176
+ message?: string;
177
+ }>;
178
+ private _getBalanceState;
179
+ private _logTransaction;
180
+ /**
181
+ * Check if the spender is currently in a critical operation
182
+ */
183
+ get isBusy(): boolean;
184
+ getDebugLevel(): DebugLevel;
185
+ setDebugLevel(level: DebugLevel): void;
186
+ private _log;
187
+ /**
188
+ * Spend Cashu tokens with automatic mint selection and retry logic
189
+ * Throws errors on failure instead of returning failed SpendResult
190
+ */
191
+ spend(options: SpendOptions): Promise<SpendResult>;
192
+ /**
193
+ * Check if error message indicates a network error
194
+ */
195
+ private _isNetworkError;
196
+ /**
197
+ * Internal spending logic
198
+ */
199
+ private _spendInternal;
200
+ /**
201
+ * Try to reuse an existing token
202
+ */
203
+ private _tryReuseToken;
204
+ /**
205
+ * Refund specific providers without retrying spend
206
+ */
207
+ refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean): Promise<{
208
+ baseUrl: string;
209
+ success: boolean;
210
+ }[]>;
211
+ /**
212
+ * Create an insufficient balance error result
213
+ */
214
+ private _createInsufficientBalanceError;
215
+ private _getProviderTokenBalance;
216
+ }
217
+
218
+ export { BalanceManager, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };