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