@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/client/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var cashuTs = require('@cashu/cashu-ts');
|
|
3
4
|
var vanilla = require('zustand/vanilla');
|
|
4
5
|
var stream = require('stream');
|
|
5
6
|
|
|
@@ -101,8 +102,6 @@ function selectMintWithBalance(balances, units, amount, excludeMints = []) {
|
|
|
101
102
|
}
|
|
102
103
|
return { selectedMintUrl: null, selectedMintBalance: 0 };
|
|
103
104
|
}
|
|
104
|
-
|
|
105
|
-
// wallet/CashuSpender.ts
|
|
106
105
|
var CashuSpender = class {
|
|
107
106
|
constructor(walletAdapter, storageAdapter, _providerRegistry, balanceManager) {
|
|
108
107
|
this.walletAdapter = walletAdapter;
|
|
@@ -113,23 +112,43 @@ var CashuSpender = class {
|
|
|
113
112
|
_isBusy = false;
|
|
114
113
|
debugLevel = "WARN";
|
|
115
114
|
async receiveToken(token) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
115
|
+
try {
|
|
116
|
+
const result = await this.walletAdapter.receiveToken(token);
|
|
117
|
+
return result;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
120
|
+
if (errorMessage.includes("Failed to fetch mint")) {
|
|
121
|
+
const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
|
|
122
|
+
const existingIndex = cachedTokens.findIndex((t) => t.token === token);
|
|
123
|
+
if (existingIndex === -1) {
|
|
124
|
+
const { amount: amount2, unit: unit2 } = this._decodeTokenAmount(token);
|
|
125
|
+
this.storageAdapter.setCachedReceiveTokens([
|
|
126
|
+
...cachedTokens,
|
|
127
|
+
{
|
|
128
|
+
token,
|
|
129
|
+
amount: amount2,
|
|
130
|
+
unit: unit2,
|
|
131
|
+
createdAt: Date.now()
|
|
132
|
+
}
|
|
133
|
+
]);
|
|
134
|
+
}
|
|
130
135
|
}
|
|
136
|
+
const { amount, unit } = this._decodeTokenAmount(token);
|
|
137
|
+
return { success: false, amount, unit, message: errorMessage };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
_decodeTokenAmount(token) {
|
|
141
|
+
try {
|
|
142
|
+
const decoded = cashuTs.getDecodedToken(token);
|
|
143
|
+
const amount = decoded.proofs.reduce(
|
|
144
|
+
(acc, proof) => acc + proof.amount,
|
|
145
|
+
0
|
|
146
|
+
);
|
|
147
|
+
const unit = decoded.unit || "sat";
|
|
148
|
+
return { amount, unit };
|
|
149
|
+
} catch {
|
|
150
|
+
return { amount: 0, unit: "sat" };
|
|
131
151
|
}
|
|
132
|
-
return result;
|
|
133
152
|
}
|
|
134
153
|
async _getBalanceState() {
|
|
135
154
|
if (this.balanceManager) {
|
|
@@ -504,7 +523,7 @@ var CashuSpender = class {
|
|
|
504
523
|
});
|
|
505
524
|
this._log(
|
|
506
525
|
"DEBUG",
|
|
507
|
-
`[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}`
|
|
526
|
+
`[CashuSpender] refundXcashuTokens: Failed to receive refund token for ${baseUrl}, incremented tryCount to ${newTryCount}: ${receiveResult.message}`
|
|
508
527
|
);
|
|
509
528
|
}
|
|
510
529
|
} catch (error) {
|
|
@@ -719,6 +738,7 @@ var BalanceManager = class {
|
|
|
719
738
|
return {
|
|
720
739
|
success: receiveResult.success,
|
|
721
740
|
refundedAmount: totalAmountMsat,
|
|
741
|
+
message: receiveResult.message,
|
|
722
742
|
requestId: fetchResult.requestId
|
|
723
743
|
};
|
|
724
744
|
} catch (error) {
|
|
@@ -3896,19 +3916,19 @@ var RoutstrClient = class {
|
|
|
3896
3916
|
`[RoutstrClient] _handleErrorResponse: Attempting to receive/restore token for ${baseUrl}`
|
|
3897
3917
|
);
|
|
3898
3918
|
if (params.token.startsWith("cashu")) {
|
|
3899
|
-
const
|
|
3919
|
+
const receiveResult = await this.cashuSpender.receiveToken(
|
|
3900
3920
|
params.token
|
|
3901
3921
|
);
|
|
3902
|
-
if (
|
|
3922
|
+
if (receiveResult.success) {
|
|
3903
3923
|
this._log(
|
|
3904
3924
|
"DEBUG",
|
|
3905
|
-
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${
|
|
3925
|
+
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
|
|
3906
3926
|
);
|
|
3907
3927
|
tryNextProvider = true;
|
|
3908
3928
|
} else {
|
|
3909
3929
|
this._log(
|
|
3910
3930
|
"DEBUG",
|
|
3911
|
-
`[RoutstrClient] _handleErrorResponse: Failed to receive token.
|
|
3931
|
+
`[RoutstrClient] _handleErrorResponse: Failed to receive token: ${receiveResult.message}`
|
|
3912
3932
|
);
|
|
3913
3933
|
}
|
|
3914
3934
|
}
|
|
@@ -3918,23 +3938,18 @@ var RoutstrClient = class {
|
|
|
3918
3938
|
"DEBUG",
|
|
3919
3939
|
`[RoutstrClient] _handleErrorResponse: Attempting to receive xcashu refund token, preview=${xCashuRefundToken.substring(0, 20)}...`
|
|
3920
3940
|
);
|
|
3921
|
-
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
"xcashu refund failed",
|
|
3934
|
-
requestId
|
|
3935
|
-
);
|
|
3936
|
-
} catch (error) {
|
|
3937
|
-
this._log("ERROR", "[xcashu] Failed to receive refund token:", error);
|
|
3941
|
+
const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
|
|
3942
|
+
if (receiveResult.success) {
|
|
3943
|
+
this._log(
|
|
3944
|
+
"DEBUG",
|
|
3945
|
+
`[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
|
|
3946
|
+
);
|
|
3947
|
+
tryNextProvider = true;
|
|
3948
|
+
} else {
|
|
3949
|
+
this._log(
|
|
3950
|
+
"ERROR",
|
|
3951
|
+
`[xcashu] Failed to receive refund token: ${receiveResult.message}`
|
|
3952
|
+
);
|
|
3938
3953
|
throw new ProviderError(
|
|
3939
3954
|
baseUrl,
|
|
3940
3955
|
status,
|
|
@@ -4227,14 +4242,15 @@ var RoutstrClient = class {
|
|
|
4227
4242
|
if (this.mode === "xcashu" && response) {
|
|
4228
4243
|
const refundToken = response.headers.get("x-cashu") ?? void 0;
|
|
4229
4244
|
if (refundToken) {
|
|
4230
|
-
|
|
4231
|
-
|
|
4232
|
-
|
|
4233
|
-
|
|
4234
|
-
|
|
4235
|
-
|
|
4236
|
-
|
|
4237
|
-
|
|
4245
|
+
const receiveResult = await this.cashuSpender.receiveToken(refundToken);
|
|
4246
|
+
if (receiveResult.success) {
|
|
4247
|
+
this.storageAdapter.removeXcashuToken(baseUrl, token);
|
|
4248
|
+
satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
|
|
4249
|
+
} else {
|
|
4250
|
+
this._log(
|
|
4251
|
+
"ERROR",
|
|
4252
|
+
`[xcashu] Failed to receive refund token: ${receiveResult.message}`
|
|
4253
|
+
);
|
|
4238
4254
|
}
|
|
4239
4255
|
}
|
|
4240
4256
|
} else if (this.mode === "apikeys") {
|
|
@@ -4486,18 +4502,18 @@ var RoutstrClient = class {
|
|
|
4486
4502
|
this.storageAdapter.setApiKey(baseUrl, spendResult2.token);
|
|
4487
4503
|
} catch (error) {
|
|
4488
4504
|
if (error instanceof Error && error.message.includes("ApiKey already exists")) {
|
|
4489
|
-
const
|
|
4505
|
+
const receiveResult = await this.cashuSpender.receiveToken(
|
|
4490
4506
|
spendResult2.token
|
|
4491
4507
|
);
|
|
4492
|
-
if (
|
|
4508
|
+
if (receiveResult.success) {
|
|
4493
4509
|
this._log(
|
|
4494
4510
|
"DEBUG",
|
|
4495
|
-
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${
|
|
4511
|
+
`[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
|
|
4496
4512
|
);
|
|
4497
4513
|
} else {
|
|
4498
4514
|
this._log(
|
|
4499
4515
|
"DEBUG",
|
|
4500
|
-
`[RoutstrClient] _handleErrorResponse: Token restore failed
|
|
4516
|
+
`[RoutstrClient] _handleErrorResponse: Token restore failed: ${receiveResult.message}`
|
|
4501
4517
|
);
|
|
4502
4518
|
}
|
|
4503
4519
|
this._log(
|