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