@buildonspark/spark-sdk 0.0.8 → 0.0.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.
Files changed (105) hide show
  1. package/dist/LightningSendRequest-CNJFhLVc.d.cts +374 -0
  2. package/dist/LightningSendRequest-CNJFhLVc.d.ts +374 -0
  3. package/dist/auto-bind.d.ts +7 -0
  4. package/dist/auto-bind.js +41 -0
  5. package/dist/auto-bind.js.map +1 -0
  6. package/dist/chunk-5SAJ52IV.js +10309 -0
  7. package/dist/chunk-COXVABEU.js +1524 -0
  8. package/dist/chunk-F4JW24C4.js +78 -0
  9. package/dist/chunk-H4A2WXR3.js +331 -0
  10. package/dist/chunk-HTNOFUHX.js +1547 -0
  11. package/dist/chunk-JQFHUW4I.js +21 -0
  12. package/dist/chunk-K3Y7DVLD.js +19 -0
  13. package/dist/chunk-NDKNVHGP.js +127 -0
  14. package/dist/chunk-PMVJGQCP.js +627 -0
  15. package/dist/chunk-QX3ZJH2S.js +527 -0
  16. package/dist/chunk-SL2YOBVM.js +127 -0
  17. package/dist/chunk-SWCOMKD6.js +333 -0
  18. package/dist/chunk-SWFFNBSR.js +1244 -0
  19. package/dist/chunk-WLK5POBV.js +527 -0
  20. package/dist/chunk-WZ74TD7N.js +660 -0
  21. package/dist/chunk-WZYVI3M3.js +1244 -0
  22. package/dist/chunk-ZGU3XW7W.js +78 -0
  23. package/dist/connection-BgWj7Hnd.d.cts +77 -0
  24. package/dist/connection-BgbVJtzh.d.ts +77 -0
  25. package/dist/connection-DX-9yFl8.d.ts +77 -0
  26. package/dist/connection-hITj9Mgk.d.cts +77 -0
  27. package/dist/graphql/objects/index.cjs +626 -0
  28. package/dist/graphql/objects/index.d.cts +140 -0
  29. package/dist/index.cjs +17202 -0
  30. package/dist/index.d.cts +413 -0
  31. package/dist/index.d.ts +413 -0
  32. package/dist/index.js +3390 -0
  33. package/dist/proto/spark.cjs +10451 -0
  34. package/dist/proto/spark.d.cts +3 -0
  35. package/dist/services/config.d.ts +2 -0
  36. package/dist/services/config.js +5 -0
  37. package/dist/services/config.js.map +1 -1
  38. package/dist/services/connection.d.ts +1 -0
  39. package/dist/services/connection.js +19 -12
  40. package/dist/services/connection.js.map +1 -1
  41. package/dist/services/index.cjs +12503 -0
  42. package/dist/services/index.d.cts +23 -0
  43. package/dist/services/index.d.ts +23 -0
  44. package/dist/services/index.js +17 -0
  45. package/dist/services/lightning.js +16 -4
  46. package/dist/services/lightning.js.map +1 -1
  47. package/dist/services/lrc20.d.ts +5 -0
  48. package/dist/services/lrc20.js +27 -0
  49. package/dist/services/lrc20.js.map +1 -0
  50. package/dist/services/token-transactions.js +21 -3
  51. package/dist/services/token-transactions.js.map +1 -1
  52. package/dist/services/transfer.d.ts +1 -0
  53. package/dist/services/transfer.js +109 -3
  54. package/dist/services/transfer.js.map +1 -1
  55. package/dist/services/tree-creation.d.ts +0 -1
  56. package/dist/services/tree-creation.js +4 -9
  57. package/dist/services/tree-creation.js.map +1 -1
  58. package/dist/signer/signer.cjs +894 -0
  59. package/dist/signer/signer.d.cts +5 -0
  60. package/dist/signer/signer.d.ts +2 -0
  61. package/dist/signer/signer.js +9 -3
  62. package/dist/signer/signer.js.map +1 -1
  63. package/dist/signer-BaC_ZP1g.d.ts +138 -0
  64. package/dist/signer-C6h1OnSQ.d.cts +138 -0
  65. package/dist/signer-CO4owhHI.d.ts +154 -0
  66. package/dist/signer-DDkpXvNZ.d.cts +154 -0
  67. package/dist/spark-BUTdOtMz.d.cts +1170 -0
  68. package/dist/spark-BUTdOtMz.d.ts +1170 -0
  69. package/dist/spark-sdk.d.ts +25 -18
  70. package/dist/spark-sdk.js +257 -142
  71. package/dist/spark-sdk.js.map +1 -1
  72. package/dist/tests/adaptor-signature.test.js +1 -2
  73. package/dist/tests/adaptor-signature.test.js.map +1 -1
  74. package/dist/tests/coop-exit.test.js.map +1 -1
  75. package/dist/tests/lightning.test.js.map +1 -1
  76. package/dist/tests/test-util.cjs +12269 -0
  77. package/dist/tests/test-util.d.cts +90 -0
  78. package/dist/tests/test-util.d.ts +19 -0
  79. package/dist/tests/test-util.js +19 -0
  80. package/dist/tests/test-util.js.map +1 -1
  81. package/dist/tests/transfer.test.js.map +1 -1
  82. package/dist/types/grpc.d.ts +1 -1
  83. package/dist/utils/index.cjs +1825 -0
  84. package/dist/utils/index.d.cts +280 -0
  85. package/dist/utils/keys.d.ts +2 -0
  86. package/dist/utils/keys.js +4 -0
  87. package/dist/utils/keys.js.map +1 -1
  88. package/dist/utils/network.d.ts +17 -0
  89. package/dist/utils/network.js +16 -0
  90. package/dist/utils/network.js.map +1 -1
  91. package/dist/utils/response-validation.js.map +1 -1
  92. package/dist/utils/token-hashing.js +1 -2
  93. package/dist/utils/token-hashing.js.map +1 -1
  94. package/dist/utils/transaction.d.ts +3 -5
  95. package/dist/utils/transaction.js +14 -16
  96. package/dist/utils/transaction.js.map +1 -1
  97. package/dist/utils/wasm-wrapper.js +4 -4
  98. package/dist/utils/wasm-wrapper.js.map +1 -1
  99. package/dist/wasm/spark_bindings.js +7 -3
  100. package/dist/wasm/spark_bindings.js.map +1 -1
  101. package/dist/wasm/spark_bindings_bg.wasm +0 -0
  102. package/package.json +12 -1
  103. package/dist/tests/jest.setup.d.ts +0 -1
  104. package/dist/tests/jest.setup.js +0 -8
  105. package/dist/tests/jest.setup.js.map +0 -1
@@ -0,0 +1,78 @@
1
+ import {
2
+ getP2TRScriptFromPublicKey
3
+ } from "./chunk-NDKNVHGP.js";
4
+ import {
5
+ spark_bindings_default
6
+ } from "./chunk-COXVABEU.js";
7
+
8
+ // src/utils/transaction.ts
9
+ import { Transaction } from "@scure/btc-signer";
10
+ var TIME_LOCK_INTERVAL = 100;
11
+ function createRefundTx(sequence, nodeOutPoint, amountSats, receivingPubkey, network) {
12
+ const newRefundTx = new Transaction({ allowUnknownOutputs: true });
13
+ newRefundTx.addInput({
14
+ ...nodeOutPoint,
15
+ sequence
16
+ });
17
+ const refundPkScript = getP2TRScriptFromPublicKey(receivingPubkey, network);
18
+ newRefundTx.addOutput({
19
+ script: refundPkScript,
20
+ amount: amountSats
21
+ });
22
+ newRefundTx.addOutput(getEphemeralAnchorOutput());
23
+ return newRefundTx;
24
+ }
25
+ function getNextTransactionSequence(currSequence) {
26
+ const currentTimelock = (currSequence || 0) & 65535;
27
+ if (currentTimelock - TIME_LOCK_INTERVAL <= 0) {
28
+ throw new Error("timelock interval is less or equal to 0");
29
+ }
30
+ return 1 << 30 | currentTimelock - TIME_LOCK_INTERVAL;
31
+ }
32
+ function getEphemeralAnchorOutput() {
33
+ return {
34
+ script: new Uint8Array([81]),
35
+ // OP_TRUE
36
+ amount: 0n
37
+ };
38
+ }
39
+
40
+ // src/utils/wasm-wrapper.ts
41
+ async function initWasm() {
42
+ let wasmModule;
43
+ try {
44
+ if (typeof window === "undefined") {
45
+ const fs = await import("fs/promises");
46
+ const path = await import("path");
47
+ const { fileURLToPath } = await import("url");
48
+ const __filename = fileURLToPath(import.meta.url);
49
+ const __dirname = path.dirname(__filename);
50
+ const wasmPath = path.resolve(
51
+ __dirname,
52
+ "../wasm/spark_bindings_bg.wasm"
53
+ );
54
+ const wasmBuffer = await fs.readFile(wasmPath);
55
+ wasmModule = await spark_bindings_default({ module_or_path: wasmBuffer }).catch((e) => {
56
+ console.error("WASM initialization error:", e);
57
+ throw e;
58
+ });
59
+ return wasmModule;
60
+ } else {
61
+ wasmModule = await spark_bindings_default();
62
+ }
63
+ } catch (e) {
64
+ console.error("WASM initialization error:", e);
65
+ throw e;
66
+ }
67
+ if (!wasmModule || typeof wasmModule !== "object") {
68
+ throw new Error("WASM module not properly initialized");
69
+ }
70
+ return wasmModule;
71
+ }
72
+
73
+ export {
74
+ createRefundTx,
75
+ getNextTransactionSequence,
76
+ getEphemeralAnchorOutput,
77
+ initWasm
78
+ };
@@ -0,0 +1,331 @@
1
+ import {
2
+ KeyPackage,
3
+ aggregateFrost,
4
+ createWasmSigningCommitment,
5
+ createWasmSigningNonce,
6
+ generateAdaptorFromSignature,
7
+ getMasterHDKeyFromSeed,
8
+ getRandomSigningNonce,
9
+ getSigningCommitmentFromNonce,
10
+ signFrost,
11
+ splitSecretWithProofs,
12
+ subtractPrivateKeys
13
+ } from "./chunk-HTNOFUHX.js";
14
+
15
+ // src/signer/signer.ts
16
+ import {
17
+ bytesToHex,
18
+ bytesToNumberBE,
19
+ hexToBytes
20
+ } from "@noble/curves/abstract/utils";
21
+ import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
22
+ import * as bip39 from "@scure/bip39";
23
+ import { generateMnemonic } from "@scure/bip39";
24
+ import { wordlist } from "@scure/bip39/wordlists/english";
25
+ import { sha256 } from "@scure/btc-signer/utils";
26
+ import { Buffer } from "buffer";
27
+ import * as ecies from "eciesjs";
28
+ globalThis.Buffer = Buffer;
29
+ var DefaultSparkSigner = class {
30
+ identityPrivateKey = null;
31
+ // <hex, hex>
32
+ publicKeyToPrivateKeyMap = /* @__PURE__ */ new Map();
33
+ commitmentToNonceMap = /* @__PURE__ */ new Map();
34
+ deriveSigningKey(hash) {
35
+ if (!this.identityPrivateKey) {
36
+ throw new Error("Private key is not set");
37
+ }
38
+ let amount = 0;
39
+ for (let i = 0; i < 8; i++) {
40
+ const view = new DataView(hash.buffer, i * 4, 4);
41
+ amount += view.getUint32(0, false);
42
+ amount = amount % 2147483648;
43
+ }
44
+ const newPrivateKey = this.identityPrivateKey.deriveChild(
45
+ Number(amount) + 2147483648
46
+ ).privateKey;
47
+ if (!newPrivateKey) {
48
+ throw new Error("Failed to recover signing key");
49
+ }
50
+ return newPrivateKey;
51
+ }
52
+ async restoreSigningKeysFromLeafs(leafs) {
53
+ if (!this.identityPrivateKey) {
54
+ throw new Error("Private key is not set");
55
+ }
56
+ for (const leaf of leafs) {
57
+ const hash = sha256(leaf.id);
58
+ const privateKey = this.deriveSigningKey(hash);
59
+ const publicKey = secp256k1.getPublicKey(privateKey);
60
+ this.publicKeyToPrivateKeyMap.set(
61
+ bytesToHex(publicKey),
62
+ bytesToHex(privateKey)
63
+ );
64
+ }
65
+ }
66
+ async getSchnorrPublicKey(publicKey) {
67
+ const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
68
+ if (!privateKey) {
69
+ throw new Error("Private key is not set");
70
+ }
71
+ return schnorr.getPublicKey(hexToBytes(privateKey));
72
+ }
73
+ async signSchnorr(message, publicKey) {
74
+ const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
75
+ if (!privateKey) {
76
+ throw new Error("Private key is not set");
77
+ }
78
+ return schnorr.sign(message, hexToBytes(privateKey));
79
+ }
80
+ async signSchnorrWithIdentityKey(message) {
81
+ if (!this.identityPrivateKey?.privateKey) {
82
+ throw new Error("Private key is not set");
83
+ }
84
+ const signature = schnorr.sign(message, this.identityPrivateKey.privateKey);
85
+ return signature;
86
+ }
87
+ async getIdentityPublicKey() {
88
+ if (!this.identityPrivateKey?.privateKey) {
89
+ throw new Error("Private key is not set");
90
+ }
91
+ return secp256k1.getPublicKey(this.identityPrivateKey.privateKey);
92
+ }
93
+ async generateMnemonic() {
94
+ return generateMnemonic(wordlist);
95
+ }
96
+ async createSparkWalletFromMnemonic(mnemonic, network) {
97
+ const seed = await bip39.mnemonicToSeed(mnemonic);
98
+ return this.createSparkWalletFromSeed(seed, network);
99
+ }
100
+ async getTrackedPublicKeys() {
101
+ return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(hexToBytes);
102
+ }
103
+ async generatePublicKey(hash) {
104
+ if (!this.identityPrivateKey) {
105
+ throw new Error("Private key is not set");
106
+ }
107
+ let newPrivateKey = null;
108
+ if (hash) {
109
+ newPrivateKey = this.deriveSigningKey(hash);
110
+ } else {
111
+ newPrivateKey = secp256k1.utils.randomPrivateKey();
112
+ }
113
+ if (!newPrivateKey) {
114
+ throw new Error("Failed to generate new private key");
115
+ }
116
+ const publicKey = secp256k1.getPublicKey(newPrivateKey);
117
+ const pubKeyHex = bytesToHex(publicKey);
118
+ const privKeyHex = bytesToHex(newPrivateKey);
119
+ this.publicKeyToPrivateKeyMap.set(pubKeyHex, privKeyHex);
120
+ return publicKey;
121
+ }
122
+ async removePublicKey(publicKey) {
123
+ this.publicKeyToPrivateKeyMap.delete(bytesToHex(publicKey));
124
+ }
125
+ async subtractPrivateKeysGivenPublicKeys(first, second) {
126
+ const firstPubKeyHex = bytesToHex(first);
127
+ const secondPubKeyHex = bytesToHex(second);
128
+ const firstPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(firstPubKeyHex);
129
+ const secondPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(secondPubKeyHex);
130
+ if (!firstPrivateKeyHex || !secondPrivateKeyHex) {
131
+ throw new Error("Private key is not set");
132
+ }
133
+ const firstPrivateKey = hexToBytes(firstPrivateKeyHex);
134
+ const secondPrivateKey = hexToBytes(secondPrivateKeyHex);
135
+ const resultPrivKey = subtractPrivateKeys(
136
+ firstPrivateKey,
137
+ secondPrivateKey
138
+ );
139
+ const resultPubKey = secp256k1.getPublicKey(resultPrivKey);
140
+ const resultPrivKeyHex = bytesToHex(resultPrivKey);
141
+ const resultPubKeyHex = bytesToHex(resultPubKey);
142
+ this.publicKeyToPrivateKeyMap.set(resultPubKeyHex, resultPrivKeyHex);
143
+ return resultPubKey;
144
+ }
145
+ async splitSecretWithProofs({
146
+ secret,
147
+ curveOrder,
148
+ threshold,
149
+ numShares,
150
+ isSecretPubkey = false
151
+ }) {
152
+ if (isSecretPubkey) {
153
+ const pubKeyHex = bytesToHex(secret);
154
+ const privateKey = this.publicKeyToPrivateKeyMap.get(pubKeyHex);
155
+ if (!privateKey) {
156
+ throw new Error("Private key is not set");
157
+ }
158
+ secret = hexToBytes(privateKey);
159
+ }
160
+ const secretAsInt = bytesToNumberBE(secret);
161
+ return splitSecretWithProofs(secretAsInt, curveOrder, threshold, numShares);
162
+ }
163
+ async signFrost({
164
+ message,
165
+ privateAsPubKey,
166
+ publicKey,
167
+ verifyingKey,
168
+ selfCommitment,
169
+ statechainCommitments,
170
+ adaptorPubKey
171
+ }) {
172
+ const privateAsPubKeyHex = bytesToHex(privateAsPubKey);
173
+ const signingPrivateKey = this.publicKeyToPrivateKeyMap.get(privateAsPubKeyHex);
174
+ if (!signingPrivateKey) {
175
+ throw new Error("Private key is not set");
176
+ }
177
+ const nonce = this.commitmentToNonceMap.get(selfCommitment);
178
+ if (!nonce) {
179
+ throw new Error("Nonce is not set");
180
+ }
181
+ const keyPackage = new KeyPackage(
182
+ hexToBytes(signingPrivateKey),
183
+ publicKey,
184
+ verifyingKey
185
+ );
186
+ return signFrost({
187
+ msg: message,
188
+ keyPackage,
189
+ nonce: createWasmSigningNonce(nonce),
190
+ selfCommitment: createWasmSigningCommitment(selfCommitment),
191
+ statechainCommitments,
192
+ adaptorPubKey
193
+ });
194
+ }
195
+ async aggregateFrost({
196
+ message,
197
+ publicKey,
198
+ verifyingKey,
199
+ selfCommitment,
200
+ statechainCommitments,
201
+ adaptorPubKey,
202
+ selfSignature,
203
+ statechainSignatures,
204
+ statechainPublicKeys
205
+ }) {
206
+ return aggregateFrost({
207
+ msg: message,
208
+ statechainSignatures,
209
+ statechainPublicKeys,
210
+ verifyingKey,
211
+ statechainCommitments,
212
+ selfCommitment: createWasmSigningCommitment(selfCommitment),
213
+ selfPublicKey: publicKey,
214
+ selfSignature,
215
+ adaptorPubKey
216
+ });
217
+ }
218
+ async createSparkWalletFromSeed(seed, network) {
219
+ if (typeof seed === "string") {
220
+ seed = hexToBytes(seed);
221
+ }
222
+ const hdkey = getMasterHDKeyFromSeed(
223
+ seed,
224
+ network == 3 /* REGTEST */ ? 0 : 1
225
+ );
226
+ if (!hdkey.privateKey) {
227
+ throw new Error("Could not derive private key from seed");
228
+ }
229
+ this.identityPrivateKey = hdkey;
230
+ return bytesToHex(secp256k1.getPublicKey(hdkey.privateKey, true));
231
+ }
232
+ async signMessageWithPublicKey(message, publicKey, compact) {
233
+ const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
234
+ if (!privateKey) {
235
+ throw new Error(
236
+ `No private key found for public key: ${bytesToHex(publicKey)}`
237
+ );
238
+ }
239
+ const signature = secp256k1.sign(message, hexToBytes(privateKey));
240
+ if (compact) {
241
+ return signature.toCompactRawBytes();
242
+ }
243
+ return signature.toDERRawBytes();
244
+ }
245
+ async signMessageWithIdentityKey(message, compact) {
246
+ if (!this.identityPrivateKey?.privateKey) {
247
+ throw new Error("Private key is not set");
248
+ }
249
+ const signature = secp256k1.sign(
250
+ message,
251
+ this.identityPrivateKey.privateKey
252
+ );
253
+ if (compact) {
254
+ return signature.toCompactRawBytes();
255
+ }
256
+ return signature.toDERRawBytes();
257
+ }
258
+ async encryptLeafPrivateKeyEcies(receiverPublicKey, publicKey) {
259
+ const publicKeyHex = bytesToHex(publicKey);
260
+ const privateKey = this.publicKeyToPrivateKeyMap.get(publicKeyHex);
261
+ if (!privateKey) {
262
+ throw new Error("Private key is not set");
263
+ }
264
+ return ecies.encrypt(receiverPublicKey, hexToBytes(privateKey));
265
+ }
266
+ async decryptEcies(ciphertext) {
267
+ if (!this.identityPrivateKey?.privateKey) {
268
+ throw new Error("Private key is not set");
269
+ }
270
+ const receiverEciesPrivKey = ecies.PrivateKey.fromHex(
271
+ bytesToHex(this.identityPrivateKey.privateKey)
272
+ );
273
+ const privateKey = ecies.decrypt(receiverEciesPrivKey.toHex(), ciphertext);
274
+ const publicKey = secp256k1.getPublicKey(privateKey);
275
+ const publicKeyHex = bytesToHex(publicKey);
276
+ const privateKeyHex = bytesToHex(privateKey);
277
+ this.publicKeyToPrivateKeyMap.set(publicKeyHex, privateKeyHex);
278
+ return publicKey;
279
+ }
280
+ async getRandomSigningCommitment() {
281
+ const nonce = getRandomSigningNonce();
282
+ const commitment = getSigningCommitmentFromNonce(nonce);
283
+ this.commitmentToNonceMap.set(commitment, nonce);
284
+ return commitment;
285
+ }
286
+ // Hardcode this for default ssp
287
+ async getSspIdentityPublicKey(network) {
288
+ if (network === 0 /* MAINNET */) {
289
+ return hexToBytes(
290
+ "02e0b8d42c5d3b5fe4c5beb6ea796ab3bc8aaf28a3d3195407482c67e0b58228a5"
291
+ );
292
+ } else {
293
+ return hexToBytes(
294
+ "028c094a432d46a0ac95349d792c2e3730bd60c29188db716f56a99e39b95338b4"
295
+ );
296
+ }
297
+ }
298
+ async hashRandomPrivateKey() {
299
+ return sha256(secp256k1.utils.randomPrivateKey());
300
+ }
301
+ async generateAdaptorFromSignature(signature) {
302
+ const adaptor = generateAdaptorFromSignature(signature);
303
+ const adaptorPublicKey = secp256k1.getPublicKey(adaptor.adaptorPrivateKey);
304
+ this.publicKeyToPrivateKeyMap.set(
305
+ bytesToHex(adaptorPublicKey),
306
+ bytesToHex(adaptor.adaptorPrivateKey)
307
+ );
308
+ return {
309
+ adaptorSignature: signature,
310
+ adaptorPublicKey
311
+ };
312
+ }
313
+ async getDepositSigningKey() {
314
+ if (!this.identityPrivateKey?.privateKey) {
315
+ throw new Error("Private key is not set");
316
+ }
317
+ const depositSigningKey = this.identityPrivateKey.derive("M/8797555'/0'/2'");
318
+ if (!depositSigningKey.privateKey) {
319
+ throw new Error("Could not derive deposit signing key");
320
+ }
321
+ this.publicKeyToPrivateKeyMap.set(
322
+ bytesToHex(secp256k1.getPublicKey(depositSigningKey.privateKey)),
323
+ bytesToHex(depositSigningKey.privateKey)
324
+ );
325
+ return secp256k1.getPublicKey(depositSigningKey.privateKey);
326
+ }
327
+ };
328
+
329
+ export {
330
+ DefaultSparkSigner
331
+ };