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