@routstr/sdk 0.2.7 → 0.2.9

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.
@@ -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
- const result = await this.walletAdapter.receiveToken(token);
117
- if (!result.success && result.message?.includes("Failed to fetch mint")) {
118
- const cachedTokens = this.storageAdapter.getCachedReceiveTokens();
119
- const existingIndex = cachedTokens.findIndex((t) => t.token === token);
120
- if (existingIndex === -1) {
121
- this.storageAdapter.setCachedReceiveTokens([
122
- ...cachedTokens,
123
- {
124
- token,
125
- amount: result.amount,
126
- unit: result.unit,
127
- createdAt: Date.now()
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 tryReceiveTokenResult = await this.cashuSpender.receiveToken(
3919
+ const receiveResult = await this.cashuSpender.receiveToken(
3900
3920
  params.token
3901
3921
  );
3902
- if (tryReceiveTokenResult.success) {
3922
+ if (receiveResult.success) {
3903
3923
  this._log(
3904
3924
  "DEBUG",
3905
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${tryReceiveTokenResult.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
- try {
3922
- const receiveResult = await this.cashuSpender.receiveToken(xCashuRefundToken);
3923
- if (receiveResult.success) {
3924
- this._log(
3925
- "DEBUG",
3926
- `[RoutstrClient] _handleErrorResponse: xcashu refund received, amount=${receiveResult.amount}`
3927
- );
3928
- tryNextProvider = true;
3929
- } else
3930
- throw new ProviderError(
3931
- baseUrl,
3932
- status,
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
- try {
4231
- const receiveResult = await this.cashuSpender.receiveToken(refundToken);
4232
- if (receiveResult.success) {
4233
- this.storageAdapter.removeXcashuToken(baseUrl, token);
4234
- satsSpent = initialTokenBalance - receiveResult.amount * (receiveResult.unit == "sat" ? 1 : 1e3);
4235
- }
4236
- } catch (error) {
4237
- this._log("ERROR", "[xcashu] Failed to receive refund token:", error);
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") {
@@ -4275,12 +4291,6 @@ var RoutstrClient = class {
4275
4291
  clientApiKey
4276
4292
  });
4277
4293
  (async () => {
4278
- try {
4279
- const xcashuResults = await this.cashuSpender.refundXcashuTokens(mintUrl);
4280
- this._log("DEBUG", "Refund xcashu tokens results:", xcashuResults);
4281
- } catch (error) {
4282
- this._log("ERROR", "Failed to refund providers:", error);
4283
- }
4284
4294
  })();
4285
4295
  return satsSpent;
4286
4296
  }
@@ -4486,18 +4496,18 @@ var RoutstrClient = class {
4486
4496
  this.storageAdapter.setApiKey(baseUrl, spendResult2.token);
4487
4497
  } catch (error) {
4488
4498
  if (error instanceof Error && error.message.includes("ApiKey already exists")) {
4489
- const tryReceiveTokenResult = await this.cashuSpender.receiveToken(
4499
+ const receiveResult = await this.cashuSpender.receiveToken(
4490
4500
  spendResult2.token
4491
4501
  );
4492
- if (tryReceiveTokenResult.success) {
4502
+ if (receiveResult.success) {
4493
4503
  this._log(
4494
4504
  "DEBUG",
4495
- `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${tryReceiveTokenResult.amount}`
4505
+ `[RoutstrClient] _handleErrorResponse: Token restored successfully, amount=${receiveResult.amount}`
4496
4506
  );
4497
4507
  } else {
4498
4508
  this._log(
4499
4509
  "DEBUG",
4500
- `[RoutstrClient] _handleErrorResponse: Token restore failed or not needed`
4510
+ `[RoutstrClient] _handleErrorResponse: Token restore failed: ${receiveResult.message}`
4501
4511
  );
4502
4512
  }
4503
4513
  this._log(