@buildonspark/spark-sdk 0.0.9 → 0.0.10

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