@buildonspark/spark-sdk 0.3.4 → 0.3.6

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.
Files changed (101) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/bare/index.cjs +547 -1238
  3. package/dist/bare/index.d.cts +676 -79
  4. package/dist/bare/index.d.ts +676 -79
  5. package/dist/bare/index.js +543 -1058
  6. package/dist/chunk-EHKP3Y65.js +140 -0
  7. package/dist/chunk-FJ7LTA2O.js +605 -0
  8. package/dist/chunk-LIZFXQWK.js +7 -0
  9. package/dist/{chunk-KIQTO4FX.js → chunk-XPHYQ2L6.js} +8128 -8727
  10. package/dist/{client-DWml6sjL.d.cts → client-AHn11NHe.d.cts} +1 -1
  11. package/dist/{client-DBZ43pJT.d.ts → client-GOlkXliC.d.ts} +1 -1
  12. package/dist/debug.cjs +581 -1222
  13. package/dist/debug.d.cts +8 -8
  14. package/dist/debug.d.ts +8 -8
  15. package/dist/debug.js +5 -3
  16. package/dist/graphql/objects/index.d.cts +3 -3
  17. package/dist/graphql/objects/index.d.ts +3 -3
  18. package/dist/index.cjs +531 -1231
  19. package/dist/index.d.cts +7 -18
  20. package/dist/index.d.ts +7 -18
  21. package/dist/index.js +5 -4
  22. package/dist/index.node.cjs +1200 -1441
  23. package/dist/index.node.d.cts +6 -7
  24. package/dist/index.node.d.ts +6 -7
  25. package/dist/index.node.js +7 -66
  26. package/dist/{logging-BUpzk4Z6.d.cts → logging-CW3kwBaM.d.cts} +3 -3
  27. package/dist/{logging-Dt2ooQiP.d.ts → logging-D7ukPwRA.d.ts} +3 -3
  28. package/dist/native/{chunk-D3SZRO65.js → chunk-X2QXUON7.js} +15 -0
  29. package/dist/native/index.cjs +702 -1214
  30. package/dist/native/index.d.cts +1077 -477
  31. package/dist/native/index.d.ts +1077 -477
  32. package/dist/native/index.js +700 -1042
  33. package/dist/native/{wasm-KT5NZXRN.js → wasm-GKEDPGTM.js} +1 -2
  34. package/dist/proto/spark.d.cts +1 -1
  35. package/dist/proto/spark.d.ts +1 -1
  36. package/dist/proto/spark_token.d.cts +1 -1
  37. package/dist/proto/spark_token.d.ts +1 -1
  38. package/dist/{spark-DasxuVfm.d.cts → spark-WA_4wcBr.d.cts} +1 -1
  39. package/dist/{spark-DasxuVfm.d.ts → spark-WA_4wcBr.d.ts} +1 -1
  40. package/dist/{spark-wallet-jlC0XN5f.d.ts → spark-wallet-NxG55m7K.d.cts} +105 -74
  41. package/dist/{spark-wallet-BoMIOPWW.d.cts → spark-wallet-jwNvWvpK.d.ts} +105 -74
  42. package/dist/spark-wallet.browser-Cg4fB-Nm.d.ts +26 -0
  43. package/dist/spark-wallet.browser-Db7Y95Kt.d.cts +26 -0
  44. package/dist/spark-wallet.node-DB3ZqtJG.d.ts +90 -0
  45. package/dist/spark-wallet.node-HEG2ahNd.d.cts +90 -0
  46. package/dist/tests/test-utils.cjs +17560 -7615
  47. package/dist/tests/test-utils.d.cts +7 -21
  48. package/dist/tests/test-utils.d.ts +7 -21
  49. package/dist/tests/test-utils.js +30 -4
  50. package/dist/{token-transactions-DscJaJOE.d.ts → token-transactions-B2-BO7Oz.d.ts} +2 -2
  51. package/dist/{token-transactions-BDzCrQSk.d.cts → token-transactions-BAN68xwg.d.cts} +2 -2
  52. package/dist/types/index.d.cts +2 -2
  53. package/dist/types/index.d.ts +2 -2
  54. package/package.json +7 -13
  55. package/src/bare/index.ts +1 -1
  56. package/src/debug.ts +1 -1
  57. package/src/index.node.ts +2 -1
  58. package/src/index.ts +2 -1
  59. package/src/native/index.ts +3 -2
  60. package/src/services/connection/connection.browser.ts +130 -0
  61. package/src/services/connection/connection.node.ts +158 -0
  62. package/src/services/{connection.ts → connection/connection.ts} +48 -259
  63. package/src/services/coop-exit.ts +3 -3
  64. package/src/services/deposit.ts +1 -1
  65. package/src/services/index.ts +1 -1
  66. package/src/services/lightning.ts +1 -1
  67. package/src/services/token-transactions.ts +1 -1
  68. package/src/services/transfer.ts +1 -1
  69. package/src/spark-wallet/spark-wallet.bare.ts +12 -0
  70. package/src/spark-wallet/spark-wallet.browser.ts +10 -24
  71. package/src/spark-wallet/spark-wallet.node.ts +4 -24
  72. package/src/spark-wallet/spark-wallet.react-native.ts +15 -0
  73. package/src/spark-wallet/spark-wallet.ts +87 -75
  74. package/src/spark-wallet/types.ts +4 -2
  75. package/src/tests/integration/coop-exit.test.ts +3 -3
  76. package/src/tests/integration/lightning.test.ts +4 -4
  77. package/src/tests/integration/ssp/coop-exit-validation.test.ts +3 -5
  78. package/src/tests/integration/ssp/coop-exit.test.ts +3 -5
  79. package/src/tests/integration/ssp/lightning.test.ts +1 -1
  80. package/src/tests/integration/ssp/static-deposit-validation.test.ts +2 -2
  81. package/src/tests/integration/ssp/static_deposit.test.ts +49 -66
  82. package/src/tests/integration/ssp/swap.test.ts +4 -5
  83. package/src/tests/integration/ssp/transfers.test.ts +10 -11
  84. package/src/tests/integration/static_deposit.test.ts +4 -4
  85. package/src/tests/integration/token-output.test.ts +2 -2
  86. package/src/tests/integration/transfer.test.ts +30 -26
  87. package/src/tests/integration/watchtower.test.ts +3 -3
  88. package/src/tests/spark-wallet/queryNodes.test.ts +1 -2
  89. package/src/tests/test-utils.ts +3 -3
  90. package/src/tests/token-outputs.test.ts +1 -1
  91. package/src/tests/utils/spark-testing-wallet.ts +18 -58
  92. package/src/tests/utils/utils.ts +63 -0
  93. package/src/tests/wrapWithOtelSpan.test.ts +7 -0
  94. package/src/utils/network.ts +11 -10
  95. package/dist/bare/xhr-transport-EEEC7FYA.js +0 -165
  96. package/dist/chunk-YH7MDVTT.js +0 -70
  97. package/dist/native/chunk-C3WN3D4O.js +0 -19
  98. package/dist/native/xhr-transport-TNCG4HTW.js +0 -168
  99. package/dist/spark-wallet.node-07PksUHH.d.cts +0 -12
  100. package/dist/spark-wallet.node-CdWkKMSq.d.ts +0 -12
  101. package/dist/xhr-transport-IWJPYF7F.js +0 -167
@@ -51,7 +51,7 @@ import {
51
51
  } from "../proto/spark.js";
52
52
  import { QueryTokenTransactionsResponse } from "../proto/spark_token.js";
53
53
  import { WalletConfigService } from "../services/config.js";
54
- import { ConnectionManager } from "../services/connection.js";
54
+ import { ConnectionManager } from "../services/connection/connection.js";
55
55
  import { CoopExitService } from "../services/coop-exit.js";
56
56
  import { DepositService } from "../services/deposit.js";
57
57
  import { LightningService } from "../services/lightning.js";
@@ -157,7 +157,7 @@ import { SparkWalletEvent } from "./types.js";
157
157
  * It provides methods for creating and managing wallets, handling deposits, executing transfers,
158
158
  * and interacting with the Lightning Network.
159
159
  */
160
- export class SparkWallet extends EventEmitter<SparkWalletEvents> {
160
+ export abstract class SparkWallet extends EventEmitter<SparkWalletEvents> {
161
161
  protected config: WalletConfigService;
162
162
 
163
163
  protected connectionManager: ConnectionManager;
@@ -193,11 +193,11 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
193
193
 
194
194
  private tracer: Tracer | null = null;
195
195
 
196
- protected constructor(options?: ConfigOptions, signer?: SparkSigner) {
196
+ constructor(options?: ConfigOptions, signer?: SparkSigner) {
197
197
  super();
198
198
 
199
199
  this.config = new WalletConfigService(options, signer);
200
- this.connectionManager = new ConnectionManager(this.config);
200
+ this.connectionManager = this.buildConnectionManager(this.config);
201
201
  this.signingService = new SigningService(this.config);
202
202
  this.depositService = new DepositService(
203
203
  this.config,
@@ -225,34 +225,23 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
225
225
 
226
226
  this.tracer = trace.getTracer(this.tracerId);
227
227
  this.wrapSparkWalletMethodsWithTracing();
228
+ this.initializeTracer(this);
228
229
  }
229
230
 
230
- public static async initialize({
231
- mnemonicOrSeed,
232
- accountNumber,
233
- signer,
234
- options,
235
- }: SparkWalletProps) {
236
- const wallet = new SparkWallet(options, signer);
237
- wallet.initializeTracer(wallet);
238
-
239
- if (options && options.signerWithPreExistingKeys) {
240
- await wallet.initWalletWithoutSeed();
241
-
242
- return {
243
- wallet,
244
- };
245
- }
246
-
247
- const initResponse = await wallet.initWallet(mnemonicOrSeed, accountNumber);
248
-
249
- return {
250
- wallet,
251
- ...initResponse,
252
- };
231
+ public static async initialize<T extends SparkWallet>(
232
+ this: new (options?: ConfigOptions, signer?: SparkSigner) => T,
233
+ { mnemonicOrSeed, accountNumber, signer, options = {} }: SparkWalletProps,
234
+ ): Promise<InitWalletResponse<T>> {
235
+ const wallet = new this(options, signer);
236
+ const initWalletResponse = await wallet.initWallet(
237
+ mnemonicOrSeed,
238
+ accountNumber,
239
+ options,
240
+ );
241
+ return initWalletResponse as InitWalletResponse<T>;
253
242
  }
254
243
 
255
- private async initializeWallet() {
244
+ private async createClientsAndSyncWallet() {
256
245
  this.sspClient = new SspClient(this.config);
257
246
  await this.connectionManager.createClients();
258
247
 
@@ -274,6 +263,10 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
274
263
  return this.sspClient;
275
264
  }
276
265
 
266
+ protected buildConnectionManager(config: WalletConfigService) {
267
+ return new ConnectionManager(config);
268
+ }
269
+
277
270
  private async handleStreamEvent({ event }: SubscribeToEventsResponse) {
278
271
  try {
279
272
  if (
@@ -880,14 +873,22 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
880
873
  *
881
874
  * @returns {Promise<Object>} Object containing:
882
875
  * - mnemonic: The mnemonic if one was generated (undefined for raw seed)
883
- * - balance: The wallet's initial balance in satoshis
884
- * - tokenBalance: Map of token balances
876
+ * - wallet: The wallet instance
885
877
  * @private
886
878
  */
887
879
  protected async initWallet(
888
880
  mnemonicOrSeed?: Uint8Array | string,
889
881
  accountNumber?: number,
890
- ): Promise<InitWalletResponse | undefined> {
882
+ options: ConfigOptions = {},
883
+ ): Promise<InitWalletResponse<this>> {
884
+ if (options.signerWithPreExistingKeys) {
885
+ await this.initWalletWithoutSeed();
886
+ return {
887
+ wallet: this,
888
+ mnemonic: undefined,
889
+ };
890
+ }
891
+
891
892
  if (accountNumber === undefined) {
892
893
  if (this.config.getNetwork() === Network.REGTEST) {
893
894
  accountNumber = 0;
@@ -917,6 +918,7 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
917
918
 
918
919
  return {
919
920
  mnemonic,
921
+ wallet: this,
920
922
  };
921
923
  }
922
924
 
@@ -925,7 +927,7 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
925
927
  * @private
926
928
  */
927
929
  protected async initWalletWithoutSeed() {
928
- await this.initializeWallet();
930
+ await this.createClientsAndSyncWallet();
929
931
 
930
932
  const identityPublicKey = await this.config.signer.getIdentityPublicKey();
931
933
 
@@ -957,7 +959,7 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
957
959
  ) {
958
960
  const identityPublicKey =
959
961
  await this.config.signer.createSparkWalletFromSeed(seed, accountNumber);
960
- await this.initializeWallet();
962
+ await this.createClientsAndSyncWallet();
961
963
 
962
964
  this.sparkAddress = encodeSparkAddress({
963
965
  identityPublicKey: identityPublicKey,
@@ -2708,7 +2710,7 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
2708
2710
  amountSatsArray.push(amountSats);
2709
2711
  }
2710
2712
 
2711
- const transferJobs = await this.withLeaves(async () => {
2713
+ return await this.withLeaves(async () => {
2712
2714
  const selectLeavesToSendMap: Map<number, TreeNode[][]> =
2713
2715
  await this.selectLeaves(amountSatsArray);
2714
2716
 
@@ -2751,50 +2753,49 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
2751
2753
  this.leaves = this.leaves.filter((leaf) => !idsToRemove.has(leaf.id));
2752
2754
  }
2753
2755
 
2754
- return jobs;
2755
- });
2756
+ const signerIdentityPublicKey =
2757
+ await this.config.signer.getIdentityPublicKey();
2756
2758
 
2757
- const signerIdentityPublicKey =
2758
- await this.config.signer.getIdentityPublicKey();
2759
-
2760
- const outcomes = await Promise.all(
2761
- transferJobs.map(async (job) => {
2762
- try {
2763
- const transfer = await this.transferService.sendTransferWithKeyTweaks(
2764
- job.leafKeyTweaks,
2765
- job.receiverIdentityPubkey,
2766
- job.sparkInvoice,
2767
- );
2768
- const isSelfTransfer = equalBytes(
2769
- signerIdentityPublicKey,
2770
- job.receiverIdentityPubkey,
2771
- );
2772
- if (isSelfTransfer) {
2773
- const pending = await this.transferService.queryTransfer(
2774
- transfer.id,
2759
+ const outcomes = await Promise.all(
2760
+ jobs.map(async (job) => {
2761
+ try {
2762
+ const transfer =
2763
+ await this.transferService.sendTransferWithKeyTweaks(
2764
+ job.leafKeyTweaks,
2765
+ job.receiverIdentityPubkey,
2766
+ job.sparkInvoice,
2767
+ );
2768
+ const isSelfTransfer = equalBytes(
2769
+ signerIdentityPublicKey,
2770
+ job.receiverIdentityPubkey,
2775
2771
  );
2776
- if (pending) {
2777
- await this.claimTransfer({ transfer: pending, optimize: true });
2772
+ if (isSelfTransfer) {
2773
+ const pending = await this.transferService.queryTransfer(
2774
+ transfer.id,
2775
+ );
2776
+ if (pending) {
2777
+ await this.claimTransfer({ transfer: pending, optimize: true });
2778
+ }
2778
2779
  }
2780
+ return {
2781
+ ok: true as const,
2782
+ transfer: mapTransferToWalletTransfer(
2783
+ transfer,
2784
+ bytesToHex(await this.config.signer.getIdentityPublicKey()),
2785
+ ),
2786
+ param: job.param,
2787
+ };
2788
+ } catch (error) {
2789
+ return {
2790
+ ok: false as const,
2791
+ error: error instanceof Error ? error : new Error(String(error)),
2792
+ param: job.param,
2793
+ };
2779
2794
  }
2780
- return {
2781
- ok: true as const,
2782
- transfer: mapTransferToWalletTransfer(
2783
- transfer,
2784
- bytesToHex(await this.config.signer.getIdentityPublicKey()),
2785
- ),
2786
- param: job.param,
2787
- };
2788
- } catch (error) {
2789
- return {
2790
- ok: false as const,
2791
- error: error instanceof Error ? error : new Error(String(error)),
2792
- param: job.param,
2793
- };
2794
- }
2795
- }),
2796
- );
2797
- return outcomes;
2795
+ }),
2796
+ );
2797
+ return outcomes;
2798
+ });
2798
2799
  }
2799
2800
 
2800
2801
  private buildTweaksByAmount(
@@ -3289,7 +3290,18 @@ export class SparkWallet extends EventEmitter<SparkWalletEvents> {
3289
3290
  });
3290
3291
  }
3291
3292
 
3292
- if (decodedInvoice.amountMSats !== BigInt(amountSats * 1000)) {
3293
+ if (decodedInvoice.amountMSats === null && amountSats !== 0) {
3294
+ throw new ValidationError("Amount mismatch", {
3295
+ field: "amountMSats",
3296
+ value: "null",
3297
+ expected: amountSats * 1000,
3298
+ });
3299
+ }
3300
+
3301
+ if (
3302
+ decodedInvoice.amountMSats !== null &&
3303
+ decodedInvoice.amountMSats !== BigInt(amountSats * 1000)
3304
+ ) {
3293
3305
  throw new ValidationError("Amount mismatch", {
3294
3306
  field: "amountMSats",
3295
3307
  value: decodedInvoice.amountMSats,
@@ -7,6 +7,7 @@ import { KeyDerivation } from "../signer/types.js";
7
7
  import { Bech32mTokenIdentifier } from "../utils/token-identifier.js";
8
8
  import { SparkAddressFormat } from "../utils/address.js";
9
9
  import { WalletTransfer } from "../types/index.js";
10
+ import type { SparkWallet } from "./spark-wallet.js";
10
11
 
11
12
  export type CreateLightningInvoiceParams = {
12
13
  amountSats: number;
@@ -140,8 +141,9 @@ export type TokenOutputsMap = Map<
140
141
 
141
142
  export type TokenMetadataMap = Map<Bech32mTokenIdentifier, TokenMetadata>;
142
143
 
143
- export type InitWalletResponse = {
144
- mnemonic?: string | undefined;
144
+ export type InitWalletResponse<T extends SparkWallet = SparkWallet> = {
145
+ wallet: T;
146
+ mnemonic: string | undefined;
145
147
  };
146
148
  export interface SparkWalletProps {
147
149
  mnemonicOrSeed?: Uint8Array | string;
@@ -6,7 +6,7 @@ import { TransactionInput } from "@scure/btc-signer/psbt";
6
6
  import { equalBytes } from "@scure/btc-signer/utils";
7
7
  import { uuidv7 } from "uuidv7";
8
8
  import { WalletConfigService } from "../../services/config.js";
9
- import { ConnectionManager } from "../../services/connection.js";
9
+ import { ConnectionManagerNodeJS } from "../../services/connection/connection.node.js";
10
10
  import { CoopExitService } from "../../services/coop-exit.js";
11
11
  import { SigningService } from "../../services/signing.js";
12
12
  import type { LeafKeyTweak } from "../../services/transfer.js";
@@ -47,7 +47,7 @@ describe.each(walletTypes)("coop exit", ({ name, Signer, createTree }) => {
47
47
  options,
48
48
  userWallet.getSigner(),
49
49
  );
50
- const connectionManager = new ConnectionManager(configService);
50
+ const connectionManager = new ConnectionManagerNodeJS(configService);
51
51
  const signingService = new SigningService(configService);
52
52
  const coopExitService = new CoopExitService(
53
53
  configService,
@@ -69,7 +69,7 @@ describe.each(walletTypes)("coop exit", ({ name, Signer, createTree }) => {
69
69
  options,
70
70
  sspWallet.getSigner(),
71
71
  );
72
- const sspConnectionManager = new ConnectionManager(sspConfigService);
72
+ const sspConnectionManager = new ConnectionManagerNodeJS(sspConfigService);
73
73
  const sspSigningService = new SigningService(sspConfigService);
74
74
  const sspTransferService = new TransferService(
75
75
  sspConfigService,
@@ -11,7 +11,7 @@ import {
11
11
  } from "../../index.js";
12
12
  import { TransferStatus } from "../../proto/spark.js";
13
13
  import { WalletConfigService } from "../../services/config.js";
14
- import { ConnectionManager } from "../../services/connection.js";
14
+ import { ConnectionManagerNodeJS } from "../../services/connection/connection.node.js";
15
15
  import { LightningService } from "../../services/lightning.js";
16
16
  import { SigningService } from "../../services/signing.js";
17
17
  import type { LeafKeyTweak } from "../../services/transfer.js";
@@ -34,7 +34,7 @@ async function cleanUp() {
34
34
  const paymentHash = sha256(preimage);
35
35
 
36
36
  const configService = new WalletConfigService(config);
37
- const connectionManager = new ConnectionManager(configService);
37
+ const connectionManager = new ConnectionManagerNodeJS(configService);
38
38
  for (const operator of Object.values(config.signingOperators!)) {
39
39
  const client = await connectionManager.createMockClient(operator!.address);
40
40
  await client.clean_up_preimage_share({
@@ -108,7 +108,7 @@ describe.each(walletTypes)(
108
108
  },
109
109
  userWallet.getSigner(),
110
110
  );
111
- const connectionManager = new ConnectionManager(userConfig);
111
+ const connectionManager = new ConnectionManagerNodeJS(userConfig);
112
112
  signingService = new SigningService(userConfig);
113
113
  lightningService = new LightningService(
114
114
  userConfig,
@@ -136,7 +136,7 @@ describe.each(walletTypes)(
136
136
  },
137
137
  sspWallet.getSigner(),
138
138
  );
139
- const sspConnectionManager = new ConnectionManager(sspConfig);
139
+ const sspConnectionManager = new ConnectionManagerNodeJS(sspConfig);
140
140
  sspSigningService = new SigningService(sspConfig);
141
141
  sspLightningService = new LightningService(
142
142
  sspConfig,
@@ -1,6 +1,6 @@
1
1
  import { Transaction } from "@scure/btc-signer";
2
2
  import {
3
- initWallet,
3
+ initTestingWallet,
4
4
  SparkWalletTesting,
5
5
  } from "../../utils/spark-testing-wallet.js";
6
6
  import { expect } from "@jest/globals";
@@ -16,10 +16,8 @@ describe("SSP coop exit basic validation", () => {
16
16
  let quoteAmount: number;
17
17
 
18
18
  beforeAll(async () => {
19
- const { wallet, depositAddress, signedTx, vout, faucet } = await initWallet(
20
- DEPOSIT_AMOUNT,
21
- "LOCAL",
22
- );
19
+ const { wallet, depositAddress, signedTx, vout, faucet } =
20
+ await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
23
21
 
24
22
  // Wait for the transaction to be mined
25
23
  await new Promise((resolve) => setTimeout(resolve, 30000));
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, it } from "@jest/globals";
2
2
  import { ExitSpeed } from "../../../types/index.js";
3
3
  import {
4
- initWallet,
4
+ initTestingWallet,
5
5
  SparkWalletTesting,
6
6
  } from "../../utils/spark-testing-wallet.js";
7
7
  import { getNewAddress } from "../../utils/regtest-test-faucet.js";
@@ -15,10 +15,8 @@ describe("SSP coop exit integration", () => {
15
15
  let quoteAmount: number;
16
16
 
17
17
  beforeEach(async () => {
18
- const { wallet, depositAddress, signedTx, vout, faucet } = await initWallet(
19
- DEPOSIT_AMOUNT,
20
- "LOCAL",
21
- );
18
+ const { wallet, depositAddress, signedTx, vout, faucet } =
19
+ await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
22
20
 
23
21
  // Wait for the transaction to be mined
24
22
  await new Promise((resolve) => setTimeout(resolve, 30000));
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from "@jest/globals";
2
2
  import { ConfigOptions } from "../../../services/wallet-config.js";
3
- import { SparkWallet } from "../../../spark-wallet/spark-wallet.js";
3
+ import { SparkWallet } from "../../../spark-wallet/spark-wallet.node.js";
4
4
  import {
5
5
  BitcoinNetwork,
6
6
  CurrencyUnit,
@@ -1,5 +1,5 @@
1
1
  import {
2
- initWallet,
2
+ initTestingWallet,
3
3
  SparkWalletTesting,
4
4
  } from "../../utils/spark-testing-wallet.js";
5
5
  import { sha256 } from "@noble/hashes/sha2";
@@ -60,7 +60,7 @@ describe("SSP static deposit validation tests", () => {
60
60
  signedTx,
61
61
  vout,
62
62
  faucet,
63
- } = await initWallet(DEPOSIT_AMOUNT, "LOCAL");
63
+ } = await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
64
64
 
65
65
  await new Promise((resolve) => setTimeout(resolve, 30000));
66
66
 
@@ -1,9 +1,10 @@
1
1
  import {
2
- SparkWalletTesting,
3
- initWallet,
2
+ SparkWalletTestingWithStream,
3
+ initTestingWallet,
4
4
  } from "../../utils/spark-testing-wallet.js";
5
5
  import { bytesToHex } from "@noble/hashes/utils";
6
6
  import { BitcoinFaucet } from "../../utils/test-faucet.js";
7
+ import { retryUntilSuccess, waitForClaim } from "../../utils/utils.js";
7
8
 
8
9
  export const DEPOSIT_AMOUNT = 10000n;
9
10
  const SECOND_DEPOSIT_AMOUNT = 20000n;
@@ -13,14 +14,12 @@ describe("SSP static deposit address integration", () => {
13
14
  describe("Happy path testing", () => {
14
15
  it("should claim deposits to a static deposit address", async () => {
15
16
  const faucet = BitcoinFaucet.getInstance();
16
- const { wallet: userWallet } = await SparkWalletTesting.initialize(
17
- {
17
+ const { wallet: userWallet } =
18
+ await SparkWalletTestingWithStream.initialize({
18
19
  options: {
19
20
  network: "LOCAL",
20
21
  },
21
- },
22
- false,
23
- );
22
+ });
24
23
 
25
24
  const depositAddress = await userWallet.getStaticDepositAddress();
26
25
  expect(depositAddress).toBeDefined();
@@ -111,14 +110,12 @@ describe("SSP static deposit address integration", () => {
111
110
  it("should create a refund transaction", async () => {
112
111
  const faucet = BitcoinFaucet.getInstance();
113
112
 
114
- const { wallet: userWallet } = await SparkWalletTesting.initialize(
115
- {
113
+ const { wallet: userWallet } =
114
+ await SparkWalletTestingWithStream.initialize({
116
115
  options: {
117
116
  network: "LOCAL",
118
117
  },
119
- },
120
- false,
121
- );
118
+ });
122
119
 
123
120
  const depositAddress = await userWallet.getStaticDepositAddress();
124
121
  expect(depositAddress).toBeDefined();
@@ -147,11 +144,15 @@ describe("SSP static deposit address integration", () => {
147
144
 
148
145
  const refundAddress = await faucet.getNewAddress();
149
146
 
150
- const refundTx = await userWallet.refundStaticDeposit({
151
- depositTransactionId: transactionId,
152
- destinationAddress: refundAddress,
153
- satsPerVbyteFee: 2,
154
- });
147
+ // Chainwatcher needs to catch up. Could take a few seconds so retry until success.
148
+ const refundTx = await retryUntilSuccess(
149
+ async () =>
150
+ await userWallet.refundStaticDeposit({
151
+ depositTransactionId: transactionId,
152
+ destinationAddress: refundAddress,
153
+ satsPerVbyteFee: 2,
154
+ }),
155
+ );
155
156
 
156
157
  expect(refundTx).toBeDefined();
157
158
 
@@ -170,14 +171,12 @@ describe("SSP static deposit address integration", () => {
170
171
 
171
172
  it("should return the right amount of txns when querying for utxos sent to a static deposit address", async () => {
172
173
  const faucet = BitcoinFaucet.getInstance();
173
- const { wallet: userWallet } = await SparkWalletTesting.initialize(
174
- {
174
+ const { wallet: userWallet } =
175
+ await SparkWalletTestingWithStream.initialize({
175
176
  options: {
176
177
  network: "LOCAL",
177
178
  },
178
- },
179
- false,
180
- );
179
+ });
181
180
 
182
181
  const depositAddress = await userWallet.getStaticDepositAddress();
183
182
  expect(depositAddress).toBeDefined();
@@ -301,25 +300,19 @@ describe("SSP static deposit address integration", () => {
301
300
  it("Wallet balance should be correct after concurrent claims of the same wallet initted in different places", async () => {
302
301
  const faucet = BitcoinFaucet.getInstance();
303
302
  const { wallet: aliceWallet, mnemonic: aliceMnemonic } =
304
- await SparkWalletTesting.initialize(
305
- {
306
- options: {
307
- network: "LOCAL",
308
- },
303
+ await SparkWalletTestingWithStream.initialize({
304
+ options: {
305
+ network: "LOCAL",
309
306
  },
310
- false,
311
- );
307
+ });
312
308
 
313
309
  const { wallet: aliceWallet2, mnemonic: aliceMnemonic2 } =
314
- await SparkWalletTesting.initialize(
315
- {
316
- options: {
317
- network: "LOCAL",
318
- },
319
- mnemonicOrSeed: aliceMnemonic,
310
+ await SparkWalletTestingWithStream.initialize({
311
+ options: {
312
+ network: "LOCAL",
320
313
  },
321
- false,
322
- );
314
+ mnemonicOrSeed: aliceMnemonic,
315
+ });
323
316
 
324
317
  expect(aliceMnemonic).toEqual(aliceMnemonic2);
325
318
 
@@ -368,7 +361,7 @@ describe("SSP static deposit address integration", () => {
368
361
  expect(successes).toHaveLength(1);
369
362
  expect(failures).toHaveLength(1);
370
363
 
371
- await new Promise((resolve) => setTimeout(resolve, 1000));
364
+ await waitForClaim({ wallet: aliceWallet });
372
365
 
373
366
  const { balance: aliceBalance } = await aliceWallet.getBalance();
374
367
  const { balance: alice2Balance } = await aliceWallet2.getBalance();
@@ -381,14 +374,12 @@ describe("SSP static deposit address integration", () => {
381
374
 
382
375
  it("Wallet balance should be correct after concurrent claims of the same initted wallet", async () => {
383
376
  const faucet = BitcoinFaucet.getInstance();
384
- const { wallet: aliceWallet } = await SparkWalletTesting.initialize(
385
- {
377
+ const { wallet: aliceWallet } =
378
+ await SparkWalletTestingWithStream.initialize({
386
379
  options: {
387
380
  network: "LOCAL",
388
381
  },
389
- },
390
- false,
391
- );
382
+ });
392
383
 
393
384
  const depositAddress = await aliceWallet.getStaticDepositAddress();
394
385
 
@@ -442,23 +433,19 @@ describe("SSP static deposit address integration", () => {
442
433
 
443
434
  describe("Quote unhappy path testing", () => {
444
435
  it("should error claim quote from a different wallet", async () => {
445
- const { wallet: aliceWallet } = await SparkWalletTesting.initialize(
446
- {
436
+ const { wallet: aliceWallet } =
437
+ await SparkWalletTestingWithStream.initialize({
447
438
  options: {
448
439
  network: "LOCAL",
449
440
  },
450
- },
451
- false,
452
- );
441
+ });
453
442
 
454
- const { wallet: bobWallet } = await SparkWalletTesting.initialize(
455
- {
443
+ const { wallet: bobWallet } =
444
+ await SparkWalletTestingWithStream.initialize({
456
445
  options: {
457
446
  network: "LOCAL",
458
447
  },
459
- },
460
- false,
461
- );
448
+ });
462
449
 
463
450
  const faucet = BitcoinFaucet.getInstance();
464
451
 
@@ -483,14 +470,12 @@ describe("SSP static deposit address integration", () => {
483
470
  }, 60000);
484
471
 
485
472
  it("should error if txid does not exist", async () => {
486
- const { wallet: aliceWallet } = await SparkWalletTesting.initialize(
487
- {
473
+ const { wallet: aliceWallet } =
474
+ await SparkWalletTestingWithStream.initialize({
488
475
  options: {
489
476
  network: "LOCAL",
490
477
  },
491
- },
492
- false,
493
- );
478
+ });
494
479
 
495
480
  const faucet = BitcoinFaucet.getInstance();
496
481
 
@@ -517,14 +502,12 @@ describe("SSP static deposit address integration", () => {
517
502
  });
518
503
 
519
504
  it("should error claim quote if tx already claimed", async () => {
520
- const { wallet: aliceWallet } = await SparkWalletTesting.initialize(
521
- {
505
+ const { wallet: aliceWallet } =
506
+ await SparkWalletTestingWithStream.initialize({
522
507
  options: {
523
508
  network: "LOCAL",
524
509
  },
525
- },
526
- false,
527
- );
510
+ });
528
511
 
529
512
  const faucet = BitcoinFaucet.getInstance();
530
513
 
@@ -570,7 +553,7 @@ describe("SSP static deposit address integration", () => {
570
553
  signedTx,
571
554
  vout,
572
555
  faucet,
573
- } = await initWallet(DEPOSIT_AMOUNT, "LOCAL");
556
+ } = await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
574
557
 
575
558
  // Wait for the transaction to be mined
576
559
  await faucet.mineBlocks(6);
@@ -610,7 +593,7 @@ describe("SSP static deposit address integration", () => {
610
593
  signedTx,
611
594
  vout,
612
595
  faucet,
613
- } = await initWallet(DEPOSIT_AMOUNT, "LOCAL");
596
+ } = await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
614
597
 
615
598
  // Wait for the transaction to be mined
616
599
  await faucet.mineBlocks(6);
@@ -661,7 +644,7 @@ describe("SSP static deposit address integration", () => {
661
644
  signedTx,
662
645
  vout,
663
646
  faucet,
664
- } = await initWallet(DEPOSIT_AMOUNT, "LOCAL");
647
+ } = await initTestingWallet(DEPOSIT_AMOUNT, "LOCAL");
665
648
 
666
649
  // Wait for the transaction to be mined
667
650
  await faucet.mineBlocks(6);
@@ -1,6 +1,7 @@
1
1
  import { describe, expect, it } from "@jest/globals";
2
2
  import { SparkWalletTesting } from "../../utils/spark-testing-wallet.js";
3
3
  import { BitcoinFaucet } from "../../utils/test-faucet.js";
4
+ import { SparkWalletTestingWithStream } from "../../utils/spark-testing-wallet.js";
4
5
 
5
6
  const DEPOSIT_AMOUNT = 10000n;
6
7
 
@@ -8,14 +9,12 @@ describe("SSP swap", () => {
8
9
  it("it should swap with the SSP before sending a transfer if the user does not have exact leaf amount", async () => {
9
10
  const faucet = BitcoinFaucet.getInstance();
10
11
 
11
- const { wallet: userWallet } = await SparkWalletTesting.initialize(
12
- {
12
+ const { wallet: userWallet } =
13
+ await SparkWalletTestingWithStream.initialize({
13
14
  options: {
14
15
  network: "LOCAL",
15
16
  },
16
- },
17
- false,
18
- );
17
+ });
19
18
 
20
19
  const depositAddress = await userWallet.getSingleUseDepositAddress();
21
20
  expect(depositAddress).toBeDefined();