@routstr/sdk 0.1.4 → 0.1.6
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/client/index.js +45 -6
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +45 -6
- package/dist/client/index.mjs.map +1 -1
- package/dist/discovery/index.d.mts +3 -1
- package/dist/discovery/index.d.ts +3 -1
- package/dist/discovery/index.js +17 -13
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +17 -13
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/index.js +62 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -19
- package/dist/index.mjs.map +1 -1
- package/dist/wallet/index.js +3 -0
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +3 -0
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -123,7 +123,7 @@ var ModelManager = class _ModelManager {
|
|
|
123
123
|
const manager = new _ModelManager(adapter, config);
|
|
124
124
|
const torMode = options.torMode ?? false;
|
|
125
125
|
const forceRefresh = options.forceRefresh ?? false;
|
|
126
|
-
const providers = await manager.bootstrapProviders(torMode);
|
|
126
|
+
const providers = await manager.bootstrapProviders(torMode, forceRefresh);
|
|
127
127
|
await manager.fetchModels(providers, forceRefresh);
|
|
128
128
|
return manager;
|
|
129
129
|
}
|
|
@@ -131,17 +131,20 @@ var ModelManager = class _ModelManager {
|
|
|
131
131
|
* Bootstrap provider list from the provider directory
|
|
132
132
|
* First tries to fetch from Nostr (kind 30421), falls back to HTTP
|
|
133
133
|
* @param torMode Whether running in Tor context
|
|
134
|
+
* @param forceRefresh Ignore provider cache and refresh provider sources
|
|
134
135
|
* @returns Array of provider base URLs
|
|
135
136
|
* @throws ProviderBootstrapError if all providers fail to fetch
|
|
136
137
|
*/
|
|
137
|
-
async bootstrapProviders(torMode = false) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
async bootstrapProviders(torMode = false, forceRefresh = false) {
|
|
139
|
+
if (!forceRefresh) {
|
|
140
|
+
const cachedUrls = this.adapter.getBaseUrlsList();
|
|
141
|
+
if (cachedUrls.length > 0) {
|
|
142
|
+
const lastUpdate = this.adapter.getBaseUrlsLastUpdate();
|
|
143
|
+
const cacheValid = lastUpdate && Date.now() - lastUpdate <= this.cacheTTL;
|
|
144
|
+
if (cacheValid) {
|
|
145
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
146
|
+
return this.filterBaseUrlsForTor(cachedUrls, torMode);
|
|
147
|
+
}
|
|
145
148
|
}
|
|
146
149
|
}
|
|
147
150
|
try {
|
|
@@ -150,13 +153,13 @@ var ModelManager = class _ModelManager {
|
|
|
150
153
|
const filtered = this.filterBaseUrlsForTor(nostrProviders, torMode);
|
|
151
154
|
this.adapter.setBaseUrlsList(filtered);
|
|
152
155
|
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
153
|
-
await this.fetchRoutstr21Models();
|
|
156
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
154
157
|
return filtered;
|
|
155
158
|
}
|
|
156
159
|
} catch (e) {
|
|
157
160
|
console.warn("Nostr bootstrap failed, falling back to HTTP:", e);
|
|
158
161
|
}
|
|
159
|
-
return this.bootstrapFromHttp(torMode);
|
|
162
|
+
return this.bootstrapFromHttp(torMode, forceRefresh);
|
|
160
163
|
}
|
|
161
164
|
/**
|
|
162
165
|
* Bootstrap providers from Nostr network (kind 30421)
|
|
@@ -252,9 +255,10 @@ var ModelManager = class _ModelManager {
|
|
|
252
255
|
/**
|
|
253
256
|
* Bootstrap providers from HTTP endpoint
|
|
254
257
|
* @param torMode Whether running in Tor context
|
|
258
|
+
* @param forceRefresh Ignore routstr21 cache and fetch fresh data
|
|
255
259
|
* @returns Array of provider base URLs
|
|
256
260
|
*/
|
|
257
|
-
async bootstrapFromHttp(torMode) {
|
|
261
|
+
async bootstrapFromHttp(torMode, forceRefresh = false) {
|
|
258
262
|
try {
|
|
259
263
|
const res = await fetch(this.providerDirectoryUrl);
|
|
260
264
|
if (!res.ok) {
|
|
@@ -282,7 +286,7 @@ var ModelManager = class _ModelManager {
|
|
|
282
286
|
if (list.length > 0) {
|
|
283
287
|
this.adapter.setBaseUrlsList(list);
|
|
284
288
|
this.adapter.setBaseUrlsLastUpdate(Date.now());
|
|
285
|
-
await this.fetchRoutstr21Models();
|
|
289
|
+
await this.fetchRoutstr21Models(forceRefresh);
|
|
286
290
|
}
|
|
287
291
|
return list;
|
|
288
292
|
} catch (e) {
|
|
@@ -1314,6 +1318,9 @@ var BalanceManager = class {
|
|
|
1314
1318
|
fetchResult.token
|
|
1315
1319
|
);
|
|
1316
1320
|
const totalAmountMsat = receiveResult.unit === "msat" ? receiveResult.amount : receiveResult.amount * 1e3;
|
|
1321
|
+
if (receiveResult.success) {
|
|
1322
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
1323
|
+
}
|
|
1317
1324
|
return {
|
|
1318
1325
|
success: receiveResult.success,
|
|
1319
1326
|
refundedAmount: totalAmountMsat,
|
|
@@ -2450,7 +2457,7 @@ var ProviderManager = class {
|
|
|
2450
2457
|
};
|
|
2451
2458
|
|
|
2452
2459
|
// client/RoutstrClient.ts
|
|
2453
|
-
var TOPUP_MARGIN =
|
|
2460
|
+
var TOPUP_MARGIN = 1.2;
|
|
2454
2461
|
var RoutstrClient = class {
|
|
2455
2462
|
constructor(walletAdapter, storageAdapter, providerRegistry, alertLevel, mode = "xcashu") {
|
|
2456
2463
|
this.walletAdapter = walletAdapter;
|
|
@@ -2850,8 +2857,7 @@ var RoutstrClient = class {
|
|
|
2850
2857
|
);
|
|
2851
2858
|
}
|
|
2852
2859
|
}
|
|
2853
|
-
if (
|
|
2854
|
-
console.log("RESPONSFE ", responseBody);
|
|
2860
|
+
if (status === 402 && !tryNextProvider && (this.mode === "apikeys" || this.mode === "lazyrefund")) {
|
|
2855
2861
|
const topupResult = await this.balanceManager.topUp({
|
|
2856
2862
|
mintUrl,
|
|
2857
2863
|
baseUrl,
|
|
@@ -2894,6 +2900,41 @@ var RoutstrClient = class {
|
|
|
2894
2900
|
headers: this._withAuthHeader(params.baseHeaders, params.token)
|
|
2895
2901
|
});
|
|
2896
2902
|
}
|
|
2903
|
+
const isInsufficientBalance413 = status === 413 && responseBody?.includes("Insufficient balance");
|
|
2904
|
+
if (isInsufficientBalance413 && !tryNextProvider && this.mode === "apikeys") {
|
|
2905
|
+
let retryToken = params.token;
|
|
2906
|
+
try {
|
|
2907
|
+
const latestBalanceInfo = await this.balanceManager.getTokenBalance(
|
|
2908
|
+
params.token,
|
|
2909
|
+
baseUrl
|
|
2910
|
+
);
|
|
2911
|
+
const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
|
|
2912
|
+
if (latestBalanceInfo.apiKey) {
|
|
2913
|
+
const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
|
|
2914
|
+
if (storedApiKeyEntry?.key !== latestBalanceInfo.apiKey) {
|
|
2915
|
+
if (storedApiKeyEntry) {
|
|
2916
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
2917
|
+
}
|
|
2918
|
+
this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
|
|
2919
|
+
}
|
|
2920
|
+
retryToken = latestBalanceInfo.apiKey;
|
|
2921
|
+
}
|
|
2922
|
+
if (latestTokenBalance >= 0) {
|
|
2923
|
+
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
2924
|
+
}
|
|
2925
|
+
} catch (error) {
|
|
2926
|
+
this._log(
|
|
2927
|
+
"WARN",
|
|
2928
|
+
`[RoutstrClient] _handleErrorResponse: Failed to refresh API key after 413 insufficient balance for ${baseUrl}`,
|
|
2929
|
+
error
|
|
2930
|
+
);
|
|
2931
|
+
}
|
|
2932
|
+
return this._makeRequest({
|
|
2933
|
+
...params,
|
|
2934
|
+
token: retryToken,
|
|
2935
|
+
headers: this._withAuthHeader(params.baseHeaders, retryToken)
|
|
2936
|
+
});
|
|
2937
|
+
}
|
|
2897
2938
|
if ((status === 401 || status === 403 || status === 413 || status === 400 || status === 500 || status === 502 || status === 503 || status === 504 || status === 521) && !tryNextProvider) {
|
|
2898
2939
|
this._log(
|
|
2899
2940
|
"DEBUG",
|
|
@@ -2954,8 +2995,6 @@ var RoutstrClient = class {
|
|
|
2954
2995
|
status,
|
|
2955
2996
|
refundResult.message ?? "Unknown error"
|
|
2956
2997
|
);
|
|
2957
|
-
} else {
|
|
2958
|
-
this.storageAdapter.removeApiKey(baseUrl);
|
|
2959
2998
|
}
|
|
2960
2999
|
}
|
|
2961
3000
|
}
|
|
@@ -3054,7 +3093,11 @@ var RoutstrClient = class {
|
|
|
3054
3093
|
baseUrl
|
|
3055
3094
|
);
|
|
3056
3095
|
const latestTokenBalance = latestBalanceInfo.unit === "msat" ? latestBalanceInfo.amount / 1e3 : latestBalanceInfo.amount;
|
|
3057
|
-
this.storageAdapter.
|
|
3096
|
+
const storedApiKeyEntry = this.storageAdapter.getApiKey(baseUrl);
|
|
3097
|
+
if (storedApiKeyEntry?.key.startsWith("cashu") && latestBalanceInfo.apiKey) {
|
|
3098
|
+
this.storageAdapter.removeApiKey(baseUrl);
|
|
3099
|
+
this.storageAdapter.setApiKey(baseUrl, latestBalanceInfo.apiKey);
|
|
3100
|
+
}
|
|
3058
3101
|
this.storageAdapter.updateApiKeyBalance(baseUrl, latestTokenBalance);
|
|
3059
3102
|
satsSpent = initialTokenBalance - latestTokenBalance;
|
|
3060
3103
|
} catch (e) {
|