@routstr/sdk 0.1.0 → 0.1.1

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,206 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-BOfiz3Tt.mjs';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-BOfiz3Tt.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
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined);
76
+ /**
77
+ * Unified refund - handles both NIP-60 and legacy wallet refunds
78
+ */
79
+ refund(options: RefundOptions): Promise<RefundResult>;
80
+ /**
81
+ * Refund API key balance - convert remaining API key balance to cashu token
82
+ */
83
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
84
+ /**
85
+ * Fetch refund token from provider API using API key authentication
86
+ */
87
+ private _fetchRefundTokenWithApiKey;
88
+ /**
89
+ * Top up API key balance with a cashu token
90
+ */
91
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
92
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
93
+ private _selectCandidateMints;
94
+ private _refundOtherProvidersForTopUp;
95
+ /**
96
+ * Fetch refund token from provider API
97
+ */
98
+ private _fetchRefundToken;
99
+ /**
100
+ * Post topup request to provider API
101
+ */
102
+ private _postTopUp;
103
+ /**
104
+ * Attempt to receive token back after failed top up
105
+ */
106
+ private _recoverFailedTopUp;
107
+ /**
108
+ * Handle refund errors with specific error types
109
+ */
110
+ private _handleRefundError;
111
+ /**
112
+ * Get token balance from provider
113
+ */
114
+ getTokenBalance(token: string, baseUrl: string): Promise<{
115
+ amount: number;
116
+ reserved: number;
117
+ unit: "sat" | "msat";
118
+ apiKey: string;
119
+ }>;
120
+ /**
121
+ * Handle topup errors with specific error types
122
+ */
123
+ private _handleTopUpError;
124
+ }
125
+
126
+ /**
127
+ * CashuSpender - Core spending logic for Cashu tokens
128
+ *
129
+ * Handles:
130
+ * - Mint selection with sufficient balance
131
+ * - Provider mint compatibility checks
132
+ * - Retry logic with alternate mints
133
+ * - Critical section management (busy state)
134
+ *
135
+ * Extracted from hooks/useCashuWithXYZ.ts
136
+ */
137
+
138
+ /**
139
+ * Options for spending cashu tokens
140
+ */
141
+ interface SpendOptions {
142
+ /** The mint URL to send from (can be overridden if insufficient balance) */
143
+ mintUrl: string;
144
+ /** The amount to spend in sats */
145
+ amount: number;
146
+ /** The provider base URL (for token storage and provider mint checks) */
147
+ baseUrl: string;
148
+ /** Whether to reuse an existing token if available */
149
+ reuseToken?: boolean;
150
+ /** Optional P2PK public key */
151
+ p2pkPubkey?: string;
152
+ /** Array of mint URLs to exclude (for retry logic) */
153
+ excludeMints?: string[];
154
+ /** Current retry count (for internal recursion) */
155
+ retryCount?: number;
156
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
157
+ refundBaseUrls?: string[];
158
+ }
159
+ /**
160
+ * CashuSpender manages the spending of Cashu tokens
161
+ */
162
+ declare class CashuSpender {
163
+ private walletAdapter;
164
+ private storageAdapter;
165
+ private _providerRegistry?;
166
+ private balanceManager?;
167
+ private _isBusy;
168
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined);
169
+ private _getBalanceState;
170
+ private _logTransaction;
171
+ /**
172
+ * Check if the spender is currently in a critical operation
173
+ */
174
+ get isBusy(): boolean;
175
+ /**
176
+ * Spend Cashu tokens with automatic mint selection and retry logic
177
+ * Throws errors on failure instead of returning failed SpendResult
178
+ */
179
+ spend(options: SpendOptions): Promise<SpendResult>;
180
+ /**
181
+ * Check if error message indicates a network error
182
+ */
183
+ private _isNetworkError;
184
+ /**
185
+ * Internal spending logic
186
+ */
187
+ private _spendInternal;
188
+ /**
189
+ * Try to reuse an existing token
190
+ */
191
+ private _tryReuseToken;
192
+ /**
193
+ * Refund specific providers without retrying spend
194
+ */
195
+ refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean): Promise<{
196
+ baseUrl: string;
197
+ success: boolean;
198
+ }[]>;
199
+ /**
200
+ * Create an insufficient balance error result
201
+ */
202
+ private _createInsufficientBalanceError;
203
+ private _getProviderTokenBalance;
204
+ }
205
+
206
+ export { BalanceManager, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };
@@ -0,0 +1,206 @@
1
+ import { W as WalletAdapter, S as StorageAdapter, P as ProviderRegistry } from '../interfaces-BbAtkq7z.js';
2
+ export { A as ApiKeyEntry, C as ChildKeyEntry, R as RoutstrClientOptions, a as StreamingCallbacks } from '../interfaces-BbAtkq7z.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
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, providerRegistry?: ProviderRegistry | undefined);
76
+ /**
77
+ * Unified refund - handles both NIP-60 and legacy wallet refunds
78
+ */
79
+ refund(options: RefundOptions): Promise<RefundResult>;
80
+ /**
81
+ * Refund API key balance - convert remaining API key balance to cashu token
82
+ */
83
+ refundApiKey(options: RefundApiKeyOptions): Promise<RefundResult>;
84
+ /**
85
+ * Fetch refund token from provider API using API key authentication
86
+ */
87
+ private _fetchRefundTokenWithApiKey;
88
+ /**
89
+ * Top up API key balance with a cashu token
90
+ */
91
+ topUp(options: TopUpOptions): Promise<TopUpResult>;
92
+ createProviderToken(options: CreateProviderTokenOptions): Promise<ProviderTokenResult>;
93
+ private _selectCandidateMints;
94
+ private _refundOtherProvidersForTopUp;
95
+ /**
96
+ * Fetch refund token from provider API
97
+ */
98
+ private _fetchRefundToken;
99
+ /**
100
+ * Post topup request to provider API
101
+ */
102
+ private _postTopUp;
103
+ /**
104
+ * Attempt to receive token back after failed top up
105
+ */
106
+ private _recoverFailedTopUp;
107
+ /**
108
+ * Handle refund errors with specific error types
109
+ */
110
+ private _handleRefundError;
111
+ /**
112
+ * Get token balance from provider
113
+ */
114
+ getTokenBalance(token: string, baseUrl: string): Promise<{
115
+ amount: number;
116
+ reserved: number;
117
+ unit: "sat" | "msat";
118
+ apiKey: string;
119
+ }>;
120
+ /**
121
+ * Handle topup errors with specific error types
122
+ */
123
+ private _handleTopUpError;
124
+ }
125
+
126
+ /**
127
+ * CashuSpender - Core spending logic for Cashu tokens
128
+ *
129
+ * Handles:
130
+ * - Mint selection with sufficient balance
131
+ * - Provider mint compatibility checks
132
+ * - Retry logic with alternate mints
133
+ * - Critical section management (busy state)
134
+ *
135
+ * Extracted from hooks/useCashuWithXYZ.ts
136
+ */
137
+
138
+ /**
139
+ * Options for spending cashu tokens
140
+ */
141
+ interface SpendOptions {
142
+ /** The mint URL to send from (can be overridden if insufficient balance) */
143
+ mintUrl: string;
144
+ /** The amount to spend in sats */
145
+ amount: number;
146
+ /** The provider base URL (for token storage and provider mint checks) */
147
+ baseUrl: string;
148
+ /** Whether to reuse an existing token if available */
149
+ reuseToken?: boolean;
150
+ /** Optional P2PK public key */
151
+ p2pkPubkey?: string;
152
+ /** Array of mint URLs to exclude (for retry logic) */
153
+ excludeMints?: string[];
154
+ /** Current retry count (for internal recursion) */
155
+ retryCount?: number;
156
+ /** Specific provider baseUrls to refund (if not provided, refunds all except current) */
157
+ refundBaseUrls?: string[];
158
+ }
159
+ /**
160
+ * CashuSpender manages the spending of Cashu tokens
161
+ */
162
+ declare class CashuSpender {
163
+ private walletAdapter;
164
+ private storageAdapter;
165
+ private _providerRegistry?;
166
+ private balanceManager?;
167
+ private _isBusy;
168
+ constructor(walletAdapter: WalletAdapter, storageAdapter: StorageAdapter, _providerRegistry?: unknown | undefined, balanceManager?: BalanceManager | undefined);
169
+ private _getBalanceState;
170
+ private _logTransaction;
171
+ /**
172
+ * Check if the spender is currently in a critical operation
173
+ */
174
+ get isBusy(): boolean;
175
+ /**
176
+ * Spend Cashu tokens with automatic mint selection and retry logic
177
+ * Throws errors on failure instead of returning failed SpendResult
178
+ */
179
+ spend(options: SpendOptions): Promise<SpendResult>;
180
+ /**
181
+ * Check if error message indicates a network error
182
+ */
183
+ private _isNetworkError;
184
+ /**
185
+ * Internal spending logic
186
+ */
187
+ private _spendInternal;
188
+ /**
189
+ * Try to reuse an existing token
190
+ */
191
+ private _tryReuseToken;
192
+ /**
193
+ * Refund specific providers without retrying spend
194
+ */
195
+ refundProviders(baseUrls: string[], mintUrl: string, refundApiKeys?: boolean): Promise<{
196
+ baseUrl: string;
197
+ success: boolean;
198
+ }[]>;
199
+ /**
200
+ * Create an insufficient balance error result
201
+ */
202
+ private _createInsufficientBalanceError;
203
+ private _getProviderTokenBalance;
204
+ }
205
+
206
+ export { BalanceManager, CashuSpender, type CreateProviderTokenOptions, ProviderRegistry, type ProviderTokenResult, type RefundApiKeyOptions, type RefundOptions, type SpendOptions, StorageAdapter, type TopUpOptions, WalletAdapter };