@routstr/sdk 0.2.7 → 0.2.8
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 +67 -51
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs +67 -51
- package/dist/client/index.mjs.map +1 -1
- package/dist/index.js +67 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +67 -51
- package/dist/index.mjs.map +1 -1
- package/dist/wallet/index.d.mts +1 -0
- package/dist/wallet/index.d.ts +1 -0
- package/dist/wallet/index.js +39 -18
- package/dist/wallet/index.js.map +1 -1
- package/dist/wallet/index.mjs +39 -18
- package/dist/wallet/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { RelayPool, onlyEvents } from 'applesauce-relay';
|
|
2
2
|
import { EventStore } from 'applesauce-core';
|
|
3
3
|
import { tap } from 'rxjs';
|
|
4
|
+
import { getDecodedToken } from '@cashu/cashu-ts';
|
|
4
5
|
import { createStore } from 'zustand/vanilla';
|
|
5
6
|
import { Transform, Readable } from 'stream';
|
|
6
7
|
|
|
@@ -733,8 +734,6 @@ function selectMintWithBalance(balances, units, amount, excludeMints = []) {
|
|
|
733
734
|
}
|
|
734
735
|
return { selectedMintUrl: null, selectedMintBalance: 0 };
|
|
735
736
|
}
|
|
736
|
-
|
|
737
|
-
// wallet/CashuSpender.ts
|
|
738
737
|
var CashuSpender = class {
|
|
739
738
|
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager) {
|
|
740
739
|
this.walletAdapter = walletAdapter;
|
|
@@ -745,23 +744,43 @@ var CashuSpender = class {
|
|
|
745
744
|
_isBusy = false;
|
|
746
745
|
debugLevel = "WARN";
|
|
747
746
|
async receiveToken(token) {
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
747
|
+
try {
|
|
748
|
+
const result = await this.walletAdapter.receiveToken(token);
|
|
749
|
+
return result;
|
|
750
|
+
} catch (error) {
|
|
751
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
752
|
+
if (errorMessage.includes("Failed to fetch mint")) {
|
|
753
|
+
const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
|
|
754
|
+
const existingIndex = cachedTokens.findIndex((t) => t.token === token);
|
|
755
|
+
if (existingIndex === -1) {
|
|
756
|
+
const { amount: amount2, unit: unit2 } = this._decodeTokenAmount(token);
|
|
757
|
+
this.storageAdapter.setCachedReceiveTokens([
|
|
758
|
+
...cachedTokens,
|
|
759
|
+
{
|
|
760
|
+
token,
|
|
761
|
+
amount: amount2,
|
|
762
|
+
unit: unit2,
|
|
763
|
+
createdAt: Date.now()
|
|
764
|
+
}
|
|
765
|
+
]);
|
|
766
|
+
}
|
|
762
767
|
}
|
|
768
|
+
const { amount, unit } = this._decodeTokenAmount(token);
|
|
769
|
+
return { success: false, amount, unit, message: errorMessage };
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
_decodeTokenAmount(token) {
|
|
773
|
+
try {
|
|
774
|
+
const decoded = getDecodedToken(token);
|
|
775
|
+
const amount = decoded.proofs.reduce(
|
|
776
|
+
(acc, proof) => acc + proof.amount,
|
|
777
|
+
0
|
|
778
|
+
);
|
|
779
|
+
const unit = decoded.unit || "sat";
|
|
780
|
+
return { amount, unit };
|
|
781
|
+
} catch {
|
|
782
|
+
return { amount: 0, unit: "sat" };
|
|
763
783
|
}
|
|
764
|
-
return result;
|
|
765
784
|
}
|
|
766
785
|
async _getBalanceState() {
|
|
767
786
|
if (this.balanceManager) {
|
|
@@ -1136,7 +1155,7 @@ var CashuSpender = class {
|
|
|
1136
1155
|
});
|
|
1137
1156
|
this._log(
|
|
1138
1157
|
"DEBUG",
|
|
1139
|
-
`[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}`
|
|
1158
|
+
`[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}: ${receiveResult.message}`
|
|
1140
1159
|
);
|
|
1141
1160
|
}
|
|
1142
1161
|
} catch (error) {
|
|
@@ -1351,6 +1370,7 @@ var BalanceManager = class {
|
|
|
1351
1370
|
return {
|
|
1352
1371
|
success: receiveResult.success,
|
|
1353
1372
|
refundedAmount: totalAmountMsat,
|
|
1373
|
+
message: receiveResult.message,
|
|
1354
1374
|
requestId: fetchResult.requestId
|
|
1355
1375
|
};
|
|
1356
1376
|
} catch (error) {
|
|
@@ -4981,19 +5001,19 @@ var RoutstrClient = class {
|
|
|
4981
5001
|
`[RoutstrClient] _handleErrorResponse: Attempting to receive/restore token for ${baseUrl}`
|
|
4982
5002
|
);
|
|
4983
5003
|
if (params.token.startsWith("cashu")) {
|
|
4984
|
-
const
|
|
5004
|
+
const receiveResult = await this.cashuSpender.receiveToken(
|
|
4985
5005
|
params.token
|
|
4986
5006
|
);
|
|
4987
|
-
if (
|
|
5007
|
+
if (receiveResult.success) {
|
|
4988
5008
|
this._log(
|
|
4989
5009
|
"DEBUG",
|
|
4990
|
-
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${
|
|
5010
|
+
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
|
|
4991
5011
|
);
|
|
4992
5012
|
tryNextProvider = true;
|
|
4993
5013
|
} else {
|
|
4994
5014
|
this._log(
|
|
4995
5015
|
"DEBUG",
|
|
4996
|
-
`[RoutstrClient] _handleErrorResponse: Failed to receive token.
|
|
5016
|
+
`[RoutstrClient] _handleErrorResponse: Failed to receive token: ${receiveResult.message}`
|
|
4997
5017
|
);
|
|
4998
5018
|
}
|
|
4999
5019
|
}
|
|
@@ -5003,23 +5023,18 @@ var RoutstrClient = class {
|
|
|
5003
5023
|
"DEBUG",
|
|
5004
5024
|
`[RoutstrClient] _handleErrorResponse: Attempting to receive xcashu refund token, preview=${xCashuRefundToken.substring(0, 20)}...`
|
|
5005
5025
|
);
|
|
5006
|
-
|
|
5007
|
-
|
|
5008
|
-
|
|
5009
|
-
|
|
5010
|
-
|
|
5011
|
-
|
|
5012
|
-
|
|
5013
|
-
|
|
5014
|
-
|
|
5015
|
-
|
|
5016
|
-
|
|
5017
|
-
|
|
5018
|
-
"xcashu refund failed",
|
|
5019
|
-
requestId
|
|
5020
|
-
);
|
|
5021
|
-
} catch (error) {
|
|
5022
|
-
this._log("ERROR", "[xcashu] Failed to receive refund token:", error);
|
|
5026
|
+
const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
|
|
5027
|
+
if (receiveResult.success) {
|
|
5028
|
+
this._log(
|
|
5029
|
+
"DEBUG",
|
|
5030
|
+
`[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
|
|
5031
|
+
);
|
|
5032
|
+
tryNextProvider = true;
|
|
5033
|
+
} else {
|
|
5034
|
+
this._log(
|
|
5035
|
+
"ERROR",
|
|
5036
|
+
`[xcashu] Failed to receive refund token: ${receiveResult.message}`
|
|
5037
|
+
);
|
|
5023
5038
|
throw new ProviderError(
|
|
5024
5039
|
baseUrl,
|
|
5025
5040
|
status,
|
|
@@ -5312,14 +5327,15 @@ var RoutstrClient = class {
|
|
|
5312
5327
|
if (this.mode === "xcashu" && response) {
|
|
5313
5328
|
const refundToken = response.headers.get("x-cashu") ?? void 0;
|
|
5314
5329
|
if (refundToken) {
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5320
|
-
|
|
5321
|
-
|
|
5322
|
-
|
|
5330
|
+
const receiveResult = await this.cashuSpender.receiveToken(refundToken);
|
|
5331
|
+
if (receiveResult.success) {
|
|
5332
|
+
this.storageAdapter.removeXcashuToken(baseUrl, token);
|
|
5333
|
+
satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
|
|
5334
|
+
} else {
|
|
5335
|
+
this._log(
|
|
5336
|
+
"ERROR",
|
|
5337
|
+
`[xcashu] Failed to receive refund token: ${receiveResult.message}`
|
|
5338
|
+
);
|
|
5323
5339
|
}
|
|
5324
5340
|
}
|
|
5325
5341
|
} else if (this.mode === "apikeys") {
|
|
@@ -5571,18 +5587,18 @@ var RoutstrClient = class {
|
|
|
5571
5587
|
this.storageAdapter.setApiKey(baseUrl, spendResult2.token);
|
|
5572
5588
|
} catch (error) {
|
|
5573
5589
|
if (error instanceof Error && error.message.includes("ApiKey already exists")) {
|
|
5574
|
-
const
|
|
5590
|
+
const receiveResult = await this.cashuSpender.receiveToken(
|
|
5575
5591
|
spendResult2.token
|
|
5576
5592
|
);
|
|
5577
|
-
if (
|
|
5593
|
+
if (receiveResult.success) {
|
|
5578
5594
|
this._log(
|
|
5579
5595
|
"DEBUG",
|
|
5580
|
-
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${
|
|
5596
|
+
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
|
|
5581
5597
|
);
|
|
5582
5598
|
} else {
|
|
5583
5599
|
this._log(
|
|
5584
5600
|
"DEBUG",
|
|
5585
|
-
`[RoutstrClient] _handleErrorResponse: Token restore failed
|
|
5601
|
+
`[RoutstrClient] _handleErrorResponse: Token restore failed: ${receiveResult.message}`
|
|
5586
5602
|
);
|
|
5587
5603
|
}
|
|
5588
5604
|
this._log(
|