@buildonspark/spark-sdk 0.2.5 → 0.2.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 (69) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/chunk-AVI5E5VT.js +66 -0
  3. package/dist/{chunk-U7LRIWTF.js → chunk-GUZ3WCB4.js} +313 -143
  4. package/dist/{client-C88GCTPB.d.cts → client-CusuvuCe.d.cts} +2 -2
  5. package/dist/{client-Dg6vS_2I.d.ts → client-Dn4Ld8pD.d.ts} +2 -2
  6. package/dist/debug.cjs +452 -286
  7. package/dist/debug.d.cts +8 -6
  8. package/dist/debug.d.ts +8 -6
  9. package/dist/debug.js +1 -1
  10. package/dist/graphql/objects/index.d.cts +3 -3
  11. package/dist/graphql/objects/index.d.ts +3 -3
  12. package/dist/index.cjs +526 -303
  13. package/dist/index.d.cts +19 -187
  14. package/dist/index.d.ts +19 -187
  15. package/dist/index.js +11 -6
  16. package/dist/index.node.cjs +451 -328
  17. package/dist/index.node.d.cts +7 -6
  18. package/dist/index.node.d.ts +7 -6
  19. package/dist/index.node.js +55 -98
  20. package/dist/native/index.cjs +468 -301
  21. package/dist/native/index.d.cts +37 -18
  22. package/dist/native/index.d.ts +37 -18
  23. package/dist/native/index.js +317 -148
  24. package/dist/proto/lrc20.d.cts +1 -1
  25. package/dist/proto/lrc20.d.ts +1 -1
  26. package/dist/proto/spark.d.cts +1 -1
  27. package/dist/proto/spark.d.ts +1 -1
  28. package/dist/proto/spark_token.d.cts +1 -1
  29. package/dist/proto/spark_token.d.ts +1 -1
  30. package/dist/{spark-ESAfZARg.d.cts → spark-Cj4brrP5.d.cts} +1 -1
  31. package/dist/{spark-ESAfZARg.d.ts → spark-Cj4brrP5.d.ts} +1 -1
  32. package/dist/{spark-wallet-B2WwKN8W.d.ts → spark-wallet-B6YthxDI.d.ts} +36 -17
  33. package/dist/{spark-wallet-Di65w0Us.d.cts → spark-wallet-BbOf2P2l.d.cts} +36 -17
  34. package/dist/spark-wallet.node-BBk1sGS2.d.cts +12 -0
  35. package/dist/spark-wallet.node-Bffethig.d.ts +12 -0
  36. package/dist/tests/test-utils.cjs +78 -50
  37. package/dist/tests/test-utils.d.cts +24 -23
  38. package/dist/tests/test-utils.d.ts +24 -23
  39. package/dist/tests/test-utils.js +2 -2
  40. package/dist/token-transactions-0_5XMWjs.d.ts +184 -0
  41. package/dist/token-transactions-CD-Adb5y.d.cts +184 -0
  42. package/dist/types/index.d.cts +2 -2
  43. package/dist/types/index.d.ts +2 -2
  44. package/dist/{xchain-address-CqRu3F21.d.cts → xchain-address-BnKZ0-dY.d.cts} +5 -5
  45. package/dist/{xchain-address-BsveIy5l.d.ts → xchain-address-Di3lu4Wy.d.ts} +5 -5
  46. package/package.json +7 -2
  47. package/src/index.node.ts +5 -1
  48. package/src/index.ts +4 -1
  49. package/src/services/config.ts +13 -2
  50. package/src/services/token-transactions.ts +22 -8
  51. package/src/services/wallet-config.ts +22 -13
  52. package/src/spark-wallet/spark-wallet.browser.ts +72 -0
  53. package/src/spark-wallet/spark-wallet.node.ts +60 -118
  54. package/src/spark-wallet/spark-wallet.ts +273 -146
  55. package/src/tests/integration/ssp/coop-exit-validation.test.ts +233 -0
  56. package/src/tests/integration/ssp/coop-exit.test.ts +112 -93
  57. package/src/tests/integration/ssp/static-deposit-validation.test.ts +145 -0
  58. package/src/tests/integration/ssp/static_deposit.test.ts +439 -132
  59. package/src/tests/integration/ssp/transfers.test.ts +7 -2
  60. package/src/tests/integration/static_deposit.test.ts +92 -0
  61. package/src/tests/integration/transfer.test.ts +1 -1
  62. package/src/tests/utils/regtest-test-faucet.ts +8 -0
  63. package/src/tests/utils/spark-testing-wallet.ts +42 -0
  64. package/src/tests/utils/test-faucet.ts +6 -2
  65. package/src/utils/token-identifier.ts +47 -4
  66. package/src/utils/token-transactions.ts +13 -9
  67. package/dist/chunk-LQZL2D3Y.js +0 -7
  68. package/dist/spark-wallet.node-7R0Rxyj9.d.cts +0 -13
  69. package/dist/spark-wallet.node-CSPWOWRu.d.ts +0 -13
@@ -132,11 +132,16 @@ export type SigningOperator = {
132
132
  readonly identityPublicKey: string;
133
133
  };
134
134
 
135
+ /* Console logging and debug tool options */
136
+ export type ConsoleOptions = {
137
+ otel?: boolean;
138
+ };
139
+
135
140
  export type ConfigOptions = MayHaveLrc20WalletApiConfig &
136
141
  MayHaveSspClientOptions & {
137
142
  readonly network?: NetworkType;
138
143
  readonly signingOperators?: Readonly<Record<string, SigningOperator>>;
139
- readonly coodinatorIdentifier?: string;
144
+ readonly coordinatorIdentifier?: string;
140
145
  readonly frostSignerAddress?: string;
141
146
  readonly lrc20Address?: string;
142
147
  readonly threshold?: number;
@@ -148,14 +153,9 @@ export type ConfigOptions = MayHaveLrc20WalletApiConfig &
148
153
  readonly sspClientOptions?: SspClientOptions;
149
154
  readonly expectedWithdrawBondSats?: number;
150
155
  readonly expectedWithdrawRelativeBlockLocktime?: number;
156
+ readonly console?: ConsoleOptions;
151
157
  };
152
158
 
153
- const DEV_PUBKEYS = [
154
- "03acd9a5a88db102730ff83dee69d69088cc4c9d93bbee893e90fd5051b7da9651",
155
- "02d2d103cacb1d6355efeab27637c74484e2a7459e49110c3fe885210369782e23",
156
- "0350f07ffc21bfd59d31e0a7a600e2995273938444447cb9bc4c75b8a895dbb853",
157
- ];
158
-
159
159
  const PROD_PUBKEYS = [
160
160
  "03dfbdff4b6332c220f8fa2ba8ed496c698ceada563fa01b67d9983bfc5c95e763",
161
161
  "03e625e9768651c9be268e287245cc33f96a68ce9141b0b4769205db027ee8ed77",
@@ -169,7 +169,7 @@ function getLocalFrostSignerAddress(): string {
169
169
  const BASE_CONFIG: Required<ConfigOptions> = {
170
170
  network: "LOCAL",
171
171
  lrc20Address: getLrc20Url("LOCAL"),
172
- coodinatorIdentifier:
172
+ coordinatorIdentifier:
173
173
  "0000000000000000000000000000000000000000000000000000000000000001",
174
174
  frostSignerAddress: getLocalFrostSignerAddress(),
175
175
  threshold: 2,
@@ -190,6 +190,9 @@ const BASE_CONFIG: Required<ConfigOptions> = {
190
190
  identityPublicKey: getSspIdentityPublicKey("LOCAL"),
191
191
  schemaEndpoint: getSspSchemaEndpoint("LOCAL"),
192
192
  },
193
+ console: {
194
+ otel: false,
195
+ },
193
196
  };
194
197
 
195
198
  const LOCAL_WALLET_CONFIG: Required<ConfigOptions> = {
@@ -280,6 +283,12 @@ function getSigningOperators(): Record<string, SigningOperator> {
280
283
  }
281
284
 
282
285
  export function getLocalSigningOperators(): Record<string, SigningOperator> {
286
+ const addresses = Array.from({ length: 5 }, (_, i) =>
287
+ isHermeticTest
288
+ ? `https://${i}.spark.minikube.local`
289
+ : `https://localhost:${i + 8535}`,
290
+ );
291
+
283
292
  const pubkeys = [
284
293
  "0322ca18fc489ae25418a0e768273c2c61cabb823edfb14feb891e9bec62016510",
285
294
  "0341727a6c41b168f07eb50865ab8c397a53c7eef628ac1020956b705e43b6cb27",
@@ -293,35 +302,35 @@ export function getLocalSigningOperators(): Record<string, SigningOperator> {
293
302
  id: 0,
294
303
  identifier:
295
304
  "0000000000000000000000000000000000000000000000000000000000000001",
296
- address: "https://localhost:8535",
305
+ address: addresses[0]!,
297
306
  identityPublicKey: pubkeys[0]!,
298
307
  },
299
308
  "0000000000000000000000000000000000000000000000000000000000000002": {
300
309
  id: 1,
301
310
  identifier:
302
311
  "0000000000000000000000000000000000000000000000000000000000000002",
303
- address: "https://localhost:8536",
312
+ address: addresses[1]!,
304
313
  identityPublicKey: pubkeys[1]!,
305
314
  },
306
315
  "0000000000000000000000000000000000000000000000000000000000000003": {
307
316
  id: 2,
308
317
  identifier:
309
318
  "0000000000000000000000000000000000000000000000000000000000000003",
310
- address: "https://localhost:8537",
319
+ address: addresses[2]!,
311
320
  identityPublicKey: pubkeys[2]!,
312
321
  },
313
322
  "0000000000000000000000000000000000000000000000000000000000000004": {
314
323
  id: 3,
315
324
  identifier:
316
325
  "0000000000000000000000000000000000000000000000000000000000000004",
317
- address: "https://localhost:8538",
326
+ address: addresses[3]!,
318
327
  identityPublicKey: pubkeys[3]!,
319
328
  },
320
329
  "0000000000000000000000000000000000000000000000000000000000000005": {
321
330
  id: 4,
322
331
  identifier:
323
332
  "0000000000000000000000000000000000000000000000000000000000000005",
324
- address: "https://localhost:8539",
333
+ address: addresses[4]!,
325
334
  identityPublicKey: pubkeys[4]!,
326
335
  },
327
336
  };
@@ -0,0 +1,72 @@
1
+ import { SparkWallet as BaseSparkWallet } from "./spark-wallet.js";
2
+ import {
3
+ ConsoleSpanExporter,
4
+ SimpleSpanProcessor,
5
+ SpanProcessor,
6
+ WebTracerProvider,
7
+ } from "@opentelemetry/sdk-trace-web";
8
+ import { registerInstrumentations } from "@opentelemetry/instrumentation";
9
+ import { FetchInstrumentation } from "@opentelemetry/instrumentation-fetch";
10
+ import { W3CTraceContextPropagator } from "@opentelemetry/core";
11
+ import { propagation } from "@opentelemetry/api";
12
+ import { SparkWalletProps } from "../spark-wallet/types.js";
13
+ import type { ConfigOptions } from "../services/wallet-config.js";
14
+ import type { SparkSigner } from "../signer/signer.js";
15
+
16
+ export class SparkWalletBrowser extends BaseSparkWallet {
17
+ public static async initialize({
18
+ mnemonicOrSeed,
19
+ accountNumber,
20
+ signer,
21
+ options,
22
+ }: SparkWalletProps) {
23
+ const wallet = new SparkWalletBrowser(options, signer);
24
+ wallet.initializeTracer(wallet);
25
+
26
+ const initResponse = await wallet.initWallet(mnemonicOrSeed, accountNumber);
27
+
28
+ return {
29
+ wallet,
30
+ ...initResponse,
31
+ };
32
+ }
33
+
34
+ protected initializeTracerEnv({
35
+ spanProcessors,
36
+ traceUrls,
37
+ }: Parameters<BaseSparkWallet["initializeTracerEnv"]>[0]) {
38
+ initializeTracerEnvBrowser({ spanProcessors, traceUrls });
39
+ }
40
+ }
41
+
42
+ export function initializeTracerEnvBrowser({
43
+ spanProcessors,
44
+ traceUrls,
45
+ }: Parameters<BaseSparkWallet["initializeTracerEnv"]>[0]) {
46
+ const provider = new WebTracerProvider({ spanProcessors });
47
+ provider.register();
48
+
49
+ propagation.setGlobalPropagator(new W3CTraceContextPropagator());
50
+
51
+ registerInstrumentations({
52
+ instrumentations: [
53
+ new FetchInstrumentation({
54
+ ignoreUrls: [
55
+ /* Since we're wrapping global fetch we should be careful to avoid
56
+ adding headers for unrelated requests */
57
+ new RegExp(
58
+ `^(?!(${traceUrls
59
+ .map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
60
+ .join("|")}))`,
61
+ ),
62
+ ],
63
+ propagateTraceHeaderCorsUrls: /.*/,
64
+ }),
65
+ ],
66
+ });
67
+ }
68
+
69
+ export {
70
+ SparkWalletBrowser as SparkWallet,
71
+ initializeTracerEnvBrowser as initializeTracerEnv,
72
+ };
@@ -1,129 +1,71 @@
1
- import { Tracer } from "@opentelemetry/api";
2
1
  import { SparkWallet as BaseSparkWallet } from "./spark-wallet.js";
3
- import type { InitWalletResponse } from "./types.js";
4
- import { isObject } from "@lightsparkdev/core";
2
+ import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
3
+ import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
4
+ import { W3CTraceContextPropagator } from "@opentelemetry/core";
5
+ import { registerInstrumentations } from "@opentelemetry/instrumentation";
6
+ import { UndiciInstrumentation } from "@opentelemetry/instrumentation-undici";
7
+ import {
8
+ ConsoleSpanExporter,
9
+ SimpleSpanProcessor,
10
+ } from "@opentelemetry/sdk-trace-base";
11
+ import { SparkWalletProps } from "./types.js";
5
12
 
6
- export class SparkWallet extends BaseSparkWallet {
7
- private tracer: Tracer | null = null;
13
+ export class SparkWalletNodeJS extends BaseSparkWallet {
14
+ public static async initialize({
15
+ mnemonicOrSeed,
16
+ accountNumber,
17
+ signer,
18
+ options,
19
+ }: SparkWalletProps) {
20
+ const wallet = new SparkWalletNodeJS(options, signer);
21
+ wallet.initializeTracer(wallet);
8
22
 
9
- protected wrapWithOtelSpan<A extends unknown[], R>(
10
- name: string,
11
- fn: (...args: A) => Promise<R>,
12
- ) {
13
- return async (...args: A) => {
14
- if (!this.tracer) {
15
- throw new Error("Tracer not initialized");
16
- }
23
+ const initResponse = await wallet.initWallet(mnemonicOrSeed, accountNumber);
17
24
 
18
- return await this.tracer.startActiveSpan(name, async (span) => {
19
- const traceId = span.spanContext().traceId;
20
- try {
21
- const result = await fn(...args);
22
- return result;
23
- } catch (error) {
24
- if (error instanceof Error) {
25
- error.message += ` [traceId: ${traceId}]`;
26
- } else if (isObject(error)) {
27
- error["traceId"] = traceId;
28
- }
29
- throw error;
30
- } finally {
31
- span.end();
32
- }
33
- });
25
+ return {
26
+ wallet,
27
+ ...initResponse,
34
28
  };
35
29
  }
36
30
 
37
- protected async initializeTracer(tracerName: string) {
38
- const { trace, propagation, context } = await import("@opentelemetry/api");
39
- const { W3CTraceContextPropagator } = await import("@opentelemetry/core");
40
- const { AsyncLocalStorageContextManager } = await import(
41
- "@opentelemetry/context-async-hooks"
42
- );
43
- const { BasicTracerProvider } = await import(
44
- "@opentelemetry/sdk-trace-base"
45
- );
46
-
47
- trace.setGlobalTracerProvider(new BasicTracerProvider());
48
- propagation.setGlobalPropagator(new W3CTraceContextPropagator());
49
- context.setGlobalContextManager(new AsyncLocalStorageContextManager());
50
-
51
- this.tracer = trace.getTracer(tracerName);
52
- }
53
-
54
- private getTraceName(methodName: string) {
55
- return `SparkWallet.${methodName}`;
31
+ protected initializeTracerEnv({
32
+ spanProcessors,
33
+ traceUrls,
34
+ }: Parameters<BaseSparkWallet["initializeTracerEnv"]>[0]) {
35
+ initializeTracerEnvNodeJS({ spanProcessors, traceUrls });
56
36
  }
37
+ }
57
38
 
58
- private wrapPublicMethodsWithOtelSpan<M extends keyof SparkWallet>(
59
- methodName: M,
60
- ) {
61
- const original = this[methodName];
62
-
63
- if (typeof original !== "function") {
64
- throw new Error(`Method ${methodName} is not a function on SparkWallet.`);
65
- }
66
-
67
- const wrapped = this.wrapWithOtelSpan(
68
- this.getTraceName(methodName),
69
- original.bind(this) as (...args: unknown[]) => Promise<unknown>,
70
- ) as SparkWallet[M];
71
-
72
- (this as SparkWallet)[methodName] = wrapped;
73
- }
74
-
75
- private wrapSparkWalletWithTracing() {
76
- const methods = [
77
- "getLeaves",
78
- "getIdentityPublicKey",
79
- "getSparkAddress",
80
- "createSparkPaymentIntent",
81
- "getSwapFeeEstimate",
82
- "getTransfers",
83
- "getBalance",
84
- "getSingleUseDepositAddress",
85
- "getStaticDepositAddress",
86
- "queryStaticDepositAddresses",
87
- "getClaimStaticDepositQuote",
88
- "claimStaticDeposit",
89
- "refundStaticDeposit",
90
- "getUnusedDepositAddresses",
91
- "claimDeposit",
92
- "advancedDeposit",
93
- "transfer",
94
- "createLightningInvoice",
95
- "payLightningInvoice",
96
- "getLightningSendFeeEstimate",
97
- "withdraw",
98
- "getWithdrawalFeeQuote",
99
- "getTransferFromSsp",
100
- "getTransfer",
101
- "transferTokens",
102
- "batchTransferTokens",
103
- "queryTokenTransactions",
104
- "getLightningReceiveRequest",
105
- "getLightningSendRequest",
106
- "getCoopExitRequest",
107
- "checkTimelock",
108
- "testOnly_expireTimelock",
109
- ] as const;
110
-
111
- methods.forEach((m) => this.wrapPublicMethodsWithOtelSpan(m));
112
-
113
- /* Private methods can't be indexed on `this` and need to be wrapped individually: */
114
- this.initWallet = this.wrapWithOtelSpan(
115
- this.getTraceName("initWallet"),
116
- this.initWallet.bind(this),
117
- );
118
- }
39
+ export function initializeTracerEnvNodeJS({
40
+ spanProcessors,
41
+ traceUrls,
42
+ }: Parameters<BaseSparkWallet["initializeTracerEnv"]>[0]) {
43
+ const provider = new NodeTracerProvider({ spanProcessors });
44
+ provider.register({
45
+ contextManager: new AsyncLocalStorageContextManager(),
46
+ propagator: new W3CTraceContextPropagator(),
47
+ });
119
48
 
120
- protected async initWallet(
121
- mnemonicOrSeed?: Uint8Array | string,
122
- accountNumber?: number,
123
- ): Promise<InitWalletResponse | undefined> {
124
- const res = super.initWallet(mnemonicOrSeed, accountNumber);
125
- await this.initializeTracer(this.tracerId);
126
- this.wrapSparkWalletWithTracing();
127
- return res;
128
- }
49
+ registerInstrumentations({
50
+ instrumentations: [
51
+ new UndiciInstrumentation({
52
+ ignoreRequestHook: (request) => {
53
+ /* Since we're wrapping global fetch we should be careful to avoid
54
+ adding headers or causing errors for unrelated requests */
55
+ try {
56
+ return !traceUrls.some((prefix) =>
57
+ request.origin.startsWith(prefix),
58
+ );
59
+ } catch {
60
+ return true;
61
+ }
62
+ },
63
+ }),
64
+ ],
65
+ });
129
66
  }
67
+
68
+ export {
69
+ SparkWalletNodeJS as SparkWallet,
70
+ initializeTracerEnvNodeJS as initializeTracerEnv,
71
+ };