@buildonspark/spark-sdk 0.1.45 → 0.1.47

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 (146) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/{chunk-I54FARY2.js → chunk-EAP3U3CW.js} +14 -14
  3. package/dist/chunk-GWFQ7EBA.js +3773 -0
  4. package/dist/{chunk-J2IE4Z7Y.js → chunk-NNX4OK44.js} +3487 -934
  5. package/dist/{RequestLightningSendInput-Du0z7Om7.d.cts → client-CvpTRpcw.d.cts} +422 -212
  6. package/dist/{RequestLightningSendInput-DEPd_fPO.d.ts → client-D7KgLN44.d.ts} +422 -212
  7. package/dist/graphql/objects/index.d.cts +5 -9
  8. package/dist/graphql/objects/index.d.ts +5 -9
  9. package/dist/graphql/objects/index.js +1 -1
  10. package/dist/index.cjs +20461 -23377
  11. package/dist/index.d.cts +15 -769
  12. package/dist/index.d.ts +15 -769
  13. package/dist/index.js +81 -71
  14. package/dist/index.node.cjs +21994 -25018
  15. package/dist/index.node.d.cts +312 -34
  16. package/dist/index.node.d.ts +312 -34
  17. package/dist/index.node.js +82 -176
  18. package/dist/native/index.cjs +22847 -25841
  19. package/dist/native/index.d.cts +974 -1138
  20. package/dist/native/index.d.ts +974 -1138
  21. package/dist/native/index.js +10604 -13592
  22. package/dist/proto/lrc20.d.cts +2 -2
  23. package/dist/proto/lrc20.d.ts +2 -2
  24. package/dist/proto/lrc20.js +3098 -46
  25. package/dist/proto/spark.d.cts +1 -1
  26. package/dist/proto/spark.d.ts +1 -1
  27. package/dist/proto/spark_token.d.cts +1 -1
  28. package/dist/proto/spark_token.d.ts +1 -1
  29. package/dist/{sdk-types-Cc4l4kb1.d.ts → sdk-types-BGCeea0G.d.ts} +1 -1
  30. package/dist/{sdk-types-B0SwjolI.d.cts → sdk-types-XUeQMLFP.d.cts} +1 -1
  31. package/dist/{spark-dM7EYXYQ.d.cts → spark-BbUrbvZz.d.cts} +1 -1
  32. package/dist/{spark-dM7EYXYQ.d.ts → spark-BbUrbvZz.d.ts} +1 -1
  33. package/dist/spark-wallet-BAFPpPtY.d.cts +923 -0
  34. package/dist/spark-wallet-CJkQW8pK.d.ts +923 -0
  35. package/dist/spark_bindings/native/index.d.cts +1 -1
  36. package/dist/spark_bindings/native/index.d.ts +1 -1
  37. package/dist/spark_bindings/wasm/index.d.cts +1 -1
  38. package/dist/spark_bindings/wasm/index.d.ts +1 -1
  39. package/dist/{services/index.cjs → tests/test-utils.cjs} +2512 -4380
  40. package/dist/tests/test-utils.d.cts +79 -0
  41. package/dist/tests/test-utils.d.ts +79 -0
  42. package/dist/tests/test-utils.js +85 -0
  43. package/dist/types/index.d.cts +5 -9
  44. package/dist/types/index.d.ts +5 -9
  45. package/dist/types/index.js +5 -5
  46. package/dist/{types-C-Rp0Oo7.d.cts → types-BADxR3bm.d.cts} +1 -1
  47. package/dist/{types-C-Rp0Oo7.d.ts → types-BADxR3bm.d.ts} +1 -1
  48. package/package.json +7 -35
  49. package/src/graphql/client.ts +59 -20
  50. package/src/index.node.ts +28 -2
  51. package/src/index.ts +31 -1
  52. package/src/native/index.ts +16 -2
  53. package/src/services/config.ts +4 -6
  54. package/src/services/connection.ts +131 -64
  55. package/src/services/lightning.ts +1 -2
  56. package/src/services/token-transactions.ts +7 -7
  57. package/src/services/transfer.ts +1 -1
  58. package/src/services/tree-creation.ts +1 -1
  59. package/src/services/wallet-config.ts +18 -10
  60. package/src/signer/signer.react-native.ts +2 -5
  61. package/src/signer/signer.ts +138 -64
  62. package/src/signer/types.ts +52 -0
  63. package/src/spark-wallet/spark-wallet.ts +79 -36
  64. package/src/spark-wallet/types.ts +4 -4
  65. package/src/tests/integration/coop-exit.test.ts +2 -1
  66. package/src/tests/integration/lightning.test.ts +2 -2
  67. package/src/tests/integration/swap.test.ts +1 -1
  68. package/src/tests/integration/transfer.test.ts +5 -5
  69. package/src/tests/integration/tree-creation.test.ts +1 -1
  70. package/src/tests/integration/wallet.test.ts +1 -0
  71. package/src/tests/isHermeticTest.ts +3 -24
  72. package/src/tests/{test-util.ts → test-utils.ts} +3 -7
  73. package/src/tests/wrapWithOtelSpan.test.ts +1 -1
  74. package/src/{address → utils}/address.ts +1 -1
  75. package/src/utils/crypto.ts +19 -9
  76. package/src/utils/index.ts +2 -0
  77. package/src/utils/network.ts +17 -0
  78. package/src/utils/secret-sharing.ts +1 -2
  79. package/src/utils/signing.ts +1 -1
  80. package/src/utils/token-transactions.ts +3 -3
  81. package/src/utils/unilateral-exit.ts +32 -0
  82. package/src/utils/xchain-address.ts +1 -1
  83. package/dist/BitcoinNetwork-TnABML0T.d.cts +0 -18
  84. package/dist/BitcoinNetwork-TnABML0T.d.ts +0 -18
  85. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.cts +0 -10
  86. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.ts +0 -10
  87. package/dist/address/index.cjs +0 -458
  88. package/dist/address/index.d.cts +0 -32
  89. package/dist/address/index.d.ts +0 -32
  90. package/dist/address/index.js +0 -17
  91. package/dist/chunk-5FUB65LX.js +0 -838
  92. package/dist/chunk-6264CGDM.js +0 -113
  93. package/dist/chunk-7V6N75CC.js +0 -24
  94. package/dist/chunk-C2S227QR.js +0 -2336
  95. package/dist/chunk-GSI4OLXZ.js +0 -117
  96. package/dist/chunk-GZ5IPPJ2.js +0 -170
  97. package/dist/chunk-HWJWKEIU.js +0 -75
  98. package/dist/chunk-KMUMFYFX.js +0 -137
  99. package/dist/chunk-L3EHBOUX.js +0 -0
  100. package/dist/chunk-NSJF5F5O.js +0 -325
  101. package/dist/chunk-NTFKFRQ2.js +0 -3146
  102. package/dist/chunk-PQN3C2MF.js +0 -1122
  103. package/dist/chunk-QNNSEJ4P.js +0 -232
  104. package/dist/chunk-R5PXJZQS.js +0 -277
  105. package/dist/chunk-VTUGIIWI.js +0 -0
  106. package/dist/chunk-YUPMXTCJ.js +0 -622
  107. package/dist/chunk-Z5HIAYFT.js +0 -84
  108. package/dist/index-B2AwKW5J.d.cts +0 -214
  109. package/dist/index-CJDi1HWc.d.ts +0 -214
  110. package/dist/network-BTJl-Sul.d.ts +0 -46
  111. package/dist/network-CqgsdUF2.d.cts +0 -46
  112. package/dist/services/config.cjs +0 -2354
  113. package/dist/services/config.d.cts +0 -42
  114. package/dist/services/config.d.ts +0 -42
  115. package/dist/services/config.js +0 -17
  116. package/dist/services/connection.cjs +0 -17691
  117. package/dist/services/connection.d.cts +0 -95
  118. package/dist/services/connection.d.ts +0 -95
  119. package/dist/services/connection.js +0 -11
  120. package/dist/services/index.d.cts +0 -21
  121. package/dist/services/index.d.ts +0 -21
  122. package/dist/services/index.js +0 -58
  123. package/dist/services/lrc-connection.cjs +0 -4713
  124. package/dist/services/lrc-connection.d.cts +0 -34
  125. package/dist/services/lrc-connection.d.ts +0 -34
  126. package/dist/services/lrc-connection.js +0 -11
  127. package/dist/services/token-transactions.cjs +0 -2877
  128. package/dist/services/token-transactions.d.cts +0 -75
  129. package/dist/services/token-transactions.d.ts +0 -75
  130. package/dist/services/token-transactions.js +0 -15
  131. package/dist/services/wallet-config.cjs +0 -340
  132. package/dist/services/wallet-config.d.cts +0 -56
  133. package/dist/services/wallet-config.d.ts +0 -56
  134. package/dist/services/wallet-config.js +0 -33
  135. package/dist/signer/signer.cjs +0 -2004
  136. package/dist/signer/signer.d.cts +0 -10
  137. package/dist/signer/signer.d.ts +0 -10
  138. package/dist/signer/signer.js +0 -24
  139. package/dist/signer-BocS_J6B.d.ts +0 -187
  140. package/dist/signer-DKS0AJkw.d.cts +0 -187
  141. package/dist/utils/index.cjs +0 -2947
  142. package/dist/utils/index.d.cts +0 -18
  143. package/dist/utils/index.d.ts +0 -18
  144. package/dist/utils/index.js +0 -157
  145. package/src/address/index.ts +0 -1
  146. package/src/services/lrc-connection.ts +0 -215
@@ -1,622 +0,0 @@
1
- import {
2
- generateAdaptorFromSignature,
3
- getRandomSigningNonce,
4
- getSigningCommitmentFromNonce,
5
- subtractPrivateKeys
6
- } from "./chunk-NSJF5F5O.js";
7
- import {
8
- splitSecretWithProofs
9
- } from "./chunk-QNNSEJ4P.js";
10
- import {
11
- isReactNative
12
- } from "./chunk-7V6N75CC.js";
13
- import {
14
- ConfigurationError,
15
- ValidationError
16
- } from "./chunk-GSI4OLXZ.js";
17
- import {
18
- Buffer
19
- } from "./chunk-MVRQ5US7.js";
20
-
21
- // src/signer/signer.ts
22
- import {
23
- bytesToHex,
24
- bytesToNumberBE,
25
- equalBytes,
26
- hexToBytes
27
- } from "@noble/curves/abstract/utils";
28
- import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
29
- import { HDKey } from "@scure/bip32";
30
- import { generateMnemonic, mnemonicToSeed } from "@scure/bip39";
31
- import { wordlist } from "@scure/bip39/wordlists/english";
32
- import * as ecies from "eciesjs";
33
- import { privateAdd, privateNegate } from "@bitcoinerlab/secp256k1";
34
- import {
35
- fromPrivateKey,
36
- PARITY,
37
- Receipt
38
- } from "@buildonspark/lrc20-sdk";
39
- import { sha256 } from "@noble/hashes/sha2";
40
- import { taprootTweakPrivKey } from "@scure/btc-signer/utils";
41
- import { Receipt as Receipt2, PARITY as PARITY2, fromPrivateKey as fromPrivateKey2 } from "@buildonspark/lrc20-sdk";
42
- import { MultisigReceiptInput } from "@buildonspark/lrc20-sdk/lrc/types";
43
- var sparkFrostModule = void 0;
44
- var getSparkFrostModule = async () => {
45
- if (isReactNative) {
46
- return void 0;
47
- }
48
- if (!sparkFrostModule) {
49
- sparkFrostModule = await import("./spark_bindings/wasm/index.js");
50
- }
51
- return sparkFrostModule;
52
- };
53
- var HARDENED_OFFSET = 2147483648;
54
- var DefaultSparkKeysGenerator = class {
55
- async deriveKeysFromSeed(seed, accountNumber) {
56
- const hdkey = HDKey.fromMasterSeed(seed);
57
- if (!hdkey.privateKey || !hdkey.publicKey) {
58
- throw new ValidationError("Failed to derive keys from seed", {
59
- field: "hdkey",
60
- value: seed
61
- });
62
- }
63
- const identityKey = hdkey.derive(`m/8797555'/${accountNumber}'/0'`);
64
- const signingKey = hdkey.derive(`m/8797555'/${accountNumber}'/1'`);
65
- const depositKey = hdkey.derive(`m/8797555'/${accountNumber}'/2'`);
66
- const staticDepositKey = hdkey.derive(`m/8797555'/${accountNumber}'/3'`);
67
- if (!identityKey.privateKey || !depositKey.privateKey || !signingKey.privateKey || !identityKey.publicKey || !depositKey.publicKey || !signingKey.publicKey || !staticDepositKey.privateKey || !staticDepositKey.publicKey) {
68
- throw new ValidationError(
69
- "Failed to derive all required keys from seed",
70
- {
71
- field: "derivedKeys"
72
- }
73
- );
74
- }
75
- return {
76
- masterPublicKey: hdkey.publicKey,
77
- identityKey: {
78
- privateKey: identityKey.privateKey,
79
- publicKey: identityKey.publicKey
80
- },
81
- signingHDKey: {
82
- hdKey: signingKey,
83
- privateKey: signingKey.privateKey,
84
- publicKey: signingKey.publicKey
85
- },
86
- depositKey: {
87
- privateKey: depositKey.privateKey,
88
- publicKey: depositKey.publicKey
89
- },
90
- staticDepositHDKey: {
91
- hdKey: staticDepositKey,
92
- privateKey: staticDepositKey.privateKey,
93
- publicKey: staticDepositKey.publicKey
94
- }
95
- };
96
- }
97
- };
98
- var TaprootOutputKeysGenerator = class {
99
- constructor(useAddressIndex = false) {
100
- this.useAddressIndex = useAddressIndex;
101
- }
102
- async deriveKeysFromSeed(seed, accountNumber) {
103
- const hdkey = HDKey.fromMasterSeed(seed);
104
- if (!hdkey.privateKey || !hdkey.publicKey) {
105
- throw new ValidationError("Failed to derive keys from seed", {
106
- field: "hdkey",
107
- value: seed
108
- });
109
- }
110
- const derivationPath = this.useAddressIndex ? `m/86'/0'/0'/0/${accountNumber}` : `m/86'/0'/${accountNumber}'/0/0`;
111
- const taprootInternalKey = hdkey.derive(derivationPath);
112
- let tweakedPrivateKey = taprootTweakPrivKey(taprootInternalKey.privateKey);
113
- let tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
114
- if (tweakedPublicKey[0] === 3) {
115
- tweakedPrivateKey = privateNegate(tweakedPrivateKey);
116
- tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
117
- }
118
- const identityKey = {
119
- publicKey: tweakedPublicKey,
120
- privateKey: tweakedPrivateKey
121
- };
122
- const signingKey = hdkey.derive(`${derivationPath}/1'`);
123
- const depositKey = hdkey.derive(`${derivationPath}/2'`);
124
- const staticDepositKey = hdkey.derive(`${derivationPath}/3'`);
125
- if (!signingKey.privateKey || !signingKey.publicKey || !depositKey.privateKey || !depositKey.publicKey || !staticDepositKey.privateKey || !staticDepositKey.publicKey) {
126
- throw new ValidationError(
127
- "Failed to derive all required keys from seed",
128
- {
129
- field: "derivedKeys"
130
- }
131
- );
132
- }
133
- return {
134
- masterPublicKey: hdkey.publicKey,
135
- identityKey: {
136
- privateKey: identityKey.privateKey,
137
- publicKey: identityKey.publicKey
138
- },
139
- signingHDKey: {
140
- hdKey: signingKey,
141
- privateKey: signingKey.privateKey,
142
- publicKey: signingKey.publicKey
143
- },
144
- depositKey: {
145
- privateKey: depositKey.privateKey,
146
- publicKey: depositKey.publicKey
147
- },
148
- staticDepositHDKey: {
149
- hdKey: staticDepositKey,
150
- privateKey: staticDepositKey.privateKey,
151
- publicKey: staticDepositKey.publicKey
152
- }
153
- };
154
- }
155
- };
156
- var DefaultSparkSigner = class {
157
- masterPublicKey = null;
158
- identityKey = null;
159
- signingKey = null;
160
- depositKey = null;
161
- staticDepositKey = null;
162
- staticDepositKeyMap = /* @__PURE__ */ new Map();
163
- // <hex, hex>
164
- publicKeyToPrivateKeyMap = /* @__PURE__ */ new Map();
165
- commitmentToNonceMap = /* @__PURE__ */ new Map();
166
- keysGenerator;
167
- constructor({
168
- sparkKeysGenerator
169
- } = {}) {
170
- this.keysGenerator = sparkKeysGenerator ?? new DefaultSparkKeysGenerator();
171
- }
172
- deriveSigningKey(hash) {
173
- if (!this.signingKey) {
174
- throw new ValidationError("Private key not initialized", {
175
- field: "signingKey"
176
- });
177
- }
178
- const view = new DataView(hash.buffer);
179
- const amount = view.getUint32(0, false) % HARDENED_OFFSET + HARDENED_OFFSET;
180
- const newPrivateKey = this.signingKey?.deriveChild(amount).privateKey;
181
- if (!newPrivateKey) {
182
- throw new ValidationError("Failed to recover signing key", {
183
- field: "privateKey"
184
- });
185
- }
186
- return newPrivateKey;
187
- }
188
- async restoreSigningKeysFromLeafs(leafs) {
189
- if (!this.signingKey) {
190
- throw new ValidationError("Signing key is not set", {
191
- field: "signingKey"
192
- });
193
- }
194
- for (const leaf of leafs) {
195
- const hash = sha256(leaf.id);
196
- const privateKey = this.deriveSigningKey(hash);
197
- const publicKey = secp256k1.getPublicKey(privateKey);
198
- this.publicKeyToPrivateKeyMap.set(
199
- bytesToHex(publicKey),
200
- bytesToHex(privateKey)
201
- );
202
- }
203
- }
204
- async getSchnorrPublicKey(publicKey) {
205
- const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
206
- if (!privateKey) {
207
- throw new ValidationError("Private key is not set", {
208
- field: "privateKey"
209
- });
210
- }
211
- return schnorr.getPublicKey(hexToBytes(privateKey));
212
- }
213
- async signSchnorr(message, publicKey) {
214
- const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
215
- if (!privateKey) {
216
- throw new ValidationError("Private key is not set", {
217
- field: "privateKey"
218
- });
219
- }
220
- return schnorr.sign(message, hexToBytes(privateKey));
221
- }
222
- async signSchnorrWithIdentityKey(message) {
223
- if (!this.identityKey?.privateKey) {
224
- throw new ValidationError("Private key not set", {
225
- field: "identityKey"
226
- });
227
- }
228
- const signature = schnorr.sign(message, this.identityKey.privateKey);
229
- return signature;
230
- }
231
- async getIdentityPublicKey() {
232
- if (!this.identityKey?.publicKey) {
233
- throw new ValidationError("Private key is not set", {
234
- field: "identityKey"
235
- });
236
- }
237
- return this.identityKey.publicKey;
238
- }
239
- async getDepositSigningKey() {
240
- if (!this.depositKey?.publicKey) {
241
- throw new ValidationError("Deposit key is not set", {
242
- field: "depositKey"
243
- });
244
- }
245
- return this.depositKey.publicKey;
246
- }
247
- async generateStaticDepositKey(idx) {
248
- if (!this.staticDepositKey?.privateKey) {
249
- throw new ValidationError("Static deposit key is not set", {
250
- field: "staticDepositKey"
251
- });
252
- }
253
- if (this.staticDepositKeyMap.has(idx)) {
254
- const staticDepositKey2 = this.staticDepositKeyMap.get(idx);
255
- return staticDepositKey2?.publicKey;
256
- }
257
- const staticDepositKey = this.staticDepositKey.deriveChild(
258
- HARDENED_OFFSET + idx
259
- );
260
- this.staticDepositKeyMap.set(idx, staticDepositKey);
261
- this.publicKeyToPrivateKeyMap.set(
262
- bytesToHex(staticDepositKey.publicKey),
263
- bytesToHex(staticDepositKey.privateKey)
264
- );
265
- return staticDepositKey.publicKey;
266
- }
267
- async getStaticDepositSigningKey(idx) {
268
- if (!this.staticDepositKey) {
269
- throw new ValidationError("Static deposit key is not set", {
270
- field: "staticDepositKey"
271
- });
272
- }
273
- if (!this.staticDepositKeyMap.has(idx)) {
274
- await this.generateStaticDepositKey(idx);
275
- }
276
- const staticDepositKey = this.staticDepositKeyMap.get(idx);
277
- if (!staticDepositKey?.publicKey) {
278
- throw new ValidationError("Static deposit key is not set", {
279
- field: "staticDepositKey"
280
- });
281
- }
282
- return staticDepositKey.publicKey;
283
- }
284
- async getStaticDepositSecretKey(idx) {
285
- if (!this.staticDepositKey) {
286
- throw new ValidationError("Static deposit key is not set", {
287
- field: "staticDepositKey"
288
- });
289
- }
290
- if (!this.staticDepositKeyMap.has(idx)) {
291
- await this.generateStaticDepositKey(idx);
292
- }
293
- const staticDepositKey = this.staticDepositKeyMap.get(idx);
294
- if (!staticDepositKey?.privateKey) {
295
- throw new ValidationError("Static deposit key is not set", {
296
- field: "staticDepositKey"
297
- });
298
- }
299
- return staticDepositKey.privateKey;
300
- }
301
- async generateMnemonic() {
302
- return generateMnemonic(wordlist);
303
- }
304
- async mnemonicToSeed(mnemonic) {
305
- return await mnemonicToSeed(mnemonic);
306
- }
307
- async getTrackedPublicKeys() {
308
- return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(hexToBytes);
309
- }
310
- async generatePublicKey(hash) {
311
- if (!this.signingKey) {
312
- throw new ValidationError("Private key is not set", {
313
- field: "signingKey"
314
- });
315
- }
316
- let newPrivateKey = null;
317
- if (hash) {
318
- newPrivateKey = this.deriveSigningKey(hash);
319
- } else {
320
- newPrivateKey = secp256k1.utils.randomPrivateKey();
321
- }
322
- if (!newPrivateKey) {
323
- throw new ValidationError("Failed to generate new private key", {
324
- field: "privateKey"
325
- });
326
- }
327
- const publicKey = secp256k1.getPublicKey(newPrivateKey);
328
- const pubKeyHex = bytesToHex(publicKey);
329
- const privKeyHex = bytesToHex(newPrivateKey);
330
- this.publicKeyToPrivateKeyMap.set(pubKeyHex, privKeyHex);
331
- return publicKey;
332
- }
333
- async removePublicKey(publicKey) {
334
- this.publicKeyToPrivateKeyMap.delete(bytesToHex(publicKey));
335
- }
336
- async subtractPrivateKeysGivenPublicKeys(first, second) {
337
- const firstPubKeyHex = bytesToHex(first);
338
- const secondPubKeyHex = bytesToHex(second);
339
- const firstPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(firstPubKeyHex);
340
- const secondPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(secondPubKeyHex);
341
- if (!firstPrivateKeyHex || !secondPrivateKeyHex) {
342
- throw new Error("Private key is not set");
343
- }
344
- const firstPrivateKey = hexToBytes(firstPrivateKeyHex);
345
- const secondPrivateKey = hexToBytes(secondPrivateKeyHex);
346
- const resultPrivKey = subtractPrivateKeys(
347
- firstPrivateKey,
348
- secondPrivateKey
349
- );
350
- const resultPubKey = secp256k1.getPublicKey(resultPrivKey);
351
- const resultPrivKeyHex = bytesToHex(resultPrivKey);
352
- const resultPubKeyHex = bytesToHex(resultPubKey);
353
- this.publicKeyToPrivateKeyMap.set(resultPubKeyHex, resultPrivKeyHex);
354
- return resultPubKey;
355
- }
356
- async splitSecretWithProofs({
357
- secret,
358
- curveOrder,
359
- threshold,
360
- numShares,
361
- isSecretPubkey = false
362
- }) {
363
- if (isSecretPubkey) {
364
- const pubKeyHex = bytesToHex(secret);
365
- const privateKey = this.publicKeyToPrivateKeyMap.get(pubKeyHex);
366
- if (!privateKey) {
367
- throw new Error("Private key is not set");
368
- }
369
- secret = hexToBytes(privateKey);
370
- }
371
- const secretAsInt = bytesToNumberBE(secret);
372
- return splitSecretWithProofs(secretAsInt, curveOrder, threshold, numShares);
373
- }
374
- async signFrost({
375
- message,
376
- privateAsPubKey,
377
- publicKey,
378
- verifyingKey,
379
- selfCommitment,
380
- statechainCommitments,
381
- adaptorPubKey
382
- }) {
383
- const SparkFrost = await getSparkFrostModule();
384
- if (!SparkFrost) {
385
- throw new ValidationError("SparkFrost module not found", {
386
- field: "SparkFrost"
387
- });
388
- }
389
- const privateAsPubKeyHex = bytesToHex(privateAsPubKey);
390
- const signingPrivateKey = this.publicKeyToPrivateKeyMap.get(privateAsPubKeyHex);
391
- if (!signingPrivateKey) {
392
- throw new ValidationError("Private key not found for public key", {
393
- field: "privateKey"
394
- });
395
- }
396
- const nonce = this.commitmentToNonceMap.get(selfCommitment);
397
- if (!nonce) {
398
- throw new ValidationError("Nonce not found for commitment", {
399
- field: "nonce"
400
- });
401
- }
402
- const keyPackage = {
403
- secretKey: hexToBytes(signingPrivateKey),
404
- publicKey,
405
- verifyingKey
406
- };
407
- return SparkFrost.signFrost({
408
- message,
409
- keyPackage,
410
- nonce,
411
- selfCommitment,
412
- statechainCommitments,
413
- adaptorPubKey
414
- });
415
- }
416
- async aggregateFrost({
417
- message,
418
- publicKey,
419
- verifyingKey,
420
- selfCommitment,
421
- statechainCommitments,
422
- adaptorPubKey,
423
- selfSignature,
424
- statechainSignatures,
425
- statechainPublicKeys
426
- }) {
427
- const SparkFrost = await getSparkFrostModule();
428
- if (!SparkFrost) {
429
- throw new ValidationError("SparkFrost module not found", {
430
- field: "SparkFrost"
431
- });
432
- }
433
- return SparkFrost.aggregateFrost({
434
- message,
435
- statechainSignatures,
436
- statechainPublicKeys,
437
- verifyingKey,
438
- statechainCommitments,
439
- selfCommitment,
440
- selfPublicKey: publicKey,
441
- selfSignature,
442
- adaptorPubKey
443
- });
444
- }
445
- async createSparkWalletFromSeed(seed, accountNumber) {
446
- if (typeof seed === "string") {
447
- seed = hexToBytes(seed);
448
- }
449
- const {
450
- masterPublicKey,
451
- identityKey,
452
- signingHDKey: signingKey,
453
- depositKey,
454
- staticDepositHDKey: staticDepositKey
455
- } = await this.keysGenerator.deriveKeysFromSeed(seed, accountNumber ?? 0);
456
- this.masterPublicKey = masterPublicKey;
457
- this.identityKey = identityKey;
458
- this.depositKey = depositKey;
459
- this.signingKey = signingKey.hdKey;
460
- this.staticDepositKey = staticDepositKey.hdKey;
461
- this.publicKeyToPrivateKeyMap.set(
462
- bytesToHex(identityKey.publicKey),
463
- bytesToHex(identityKey.privateKey)
464
- );
465
- this.publicKeyToPrivateKeyMap.set(
466
- bytesToHex(depositKey.publicKey),
467
- bytesToHex(depositKey.privateKey)
468
- );
469
- this.publicKeyToPrivateKeyMap.set(
470
- bytesToHex(staticDepositKey.publicKey),
471
- bytesToHex(staticDepositKey.privateKey)
472
- );
473
- return bytesToHex(identityKey.publicKey);
474
- }
475
- async signMessageWithPublicKey(message, publicKey, compact) {
476
- const privateKey = this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey));
477
- if (!privateKey) {
478
- throw new ValidationError("Private key not found for public key", {
479
- field: "privateKey",
480
- value: bytesToHex(publicKey)
481
- });
482
- }
483
- const signature = secp256k1.sign(message, hexToBytes(privateKey));
484
- if (compact) {
485
- return signature.toCompactRawBytes();
486
- }
487
- return signature.toDERRawBytes();
488
- }
489
- async signMessageWithIdentityKey(message, compact) {
490
- if (!this.identityKey?.privateKey) {
491
- throw new ConfigurationError("Identity key not initialized", {
492
- configKey: "identityKey"
493
- });
494
- }
495
- const signature = secp256k1.sign(message, this.identityKey.privateKey);
496
- if (compact) {
497
- return signature.toCompactRawBytes();
498
- }
499
- return signature.toDERRawBytes();
500
- }
501
- async encryptLeafPrivateKeyEcies(receiverPublicKey, publicKey) {
502
- const publicKeyHex = bytesToHex(publicKey);
503
- const privateKey = this.publicKeyToPrivateKeyMap.get(publicKeyHex);
504
- if (!privateKey) {
505
- throw new Error("Private key is not set");
506
- }
507
- return ecies.encrypt(receiverPublicKey, hexToBytes(privateKey));
508
- }
509
- async decryptEcies(ciphertext) {
510
- if (!this.identityKey?.privateKey) {
511
- throw new ConfigurationError("Identity key not initialized", {
512
- configKey: "identityKey"
513
- });
514
- }
515
- const receiverEciesPrivKey = ecies.PrivateKey.fromHex(
516
- bytesToHex(this.identityKey.privateKey)
517
- );
518
- const privateKey = ecies.decrypt(receiverEciesPrivKey.toHex(), ciphertext);
519
- const publicKey = secp256k1.getPublicKey(privateKey);
520
- const publicKeyHex = bytesToHex(publicKey);
521
- const privateKeyHex = bytesToHex(privateKey);
522
- this.publicKeyToPrivateKeyMap.set(publicKeyHex, privateKeyHex);
523
- return publicKey;
524
- }
525
- async getRandomSigningCommitment() {
526
- const nonce = getRandomSigningNonce();
527
- const commitment = getSigningCommitmentFromNonce(nonce);
528
- this.commitmentToNonceMap.set(commitment, nonce);
529
- return commitment;
530
- }
531
- async hashRandomPrivateKey() {
532
- return sha256(secp256k1.utils.randomPrivateKey());
533
- }
534
- async generateAdaptorFromSignature(signature) {
535
- const adaptor = generateAdaptorFromSignature(signature);
536
- const adaptorPublicKey = secp256k1.getPublicKey(adaptor.adaptorPrivateKey);
537
- this.publicKeyToPrivateKeyMap.set(
538
- bytesToHex(adaptorPublicKey),
539
- bytesToHex(adaptor.adaptorPrivateKey)
540
- );
541
- return {
542
- adaptorSignature: signature,
543
- adaptorPublicKey
544
- };
545
- }
546
- async getMasterPublicKey() {
547
- if (!this.masterPublicKey) {
548
- throw new Error("Private key is not set");
549
- }
550
- return this.masterPublicKey;
551
- }
552
- async validateMessageWithIdentityKey(message, signature) {
553
- if (!this.identityKey?.publicKey) {
554
- throw new ConfigurationError("Identity key not initialized", {
555
- configKey: "identityKey"
556
- });
557
- }
558
- return secp256k1.verify(signature, message, this.identityKey.publicKey);
559
- }
560
- async signPsbt(psbt, input, sighashTypes, receipt) {
561
- if (!this.identityKey?.privateKey) {
562
- throw new ConfigurationError("Identity key not initialized", {
563
- configKey: "identityKey"
564
- });
565
- }
566
- if (receipt) {
567
- const receiptPrivateKey = this.getReceiptPrivateKey(receipt);
568
- const tweakedKeyPair = fromPrivateKey(Buffer.from(receiptPrivateKey));
569
- psbt.signInput(input, tweakedKeyPair, sighashTypes);
570
- return psbt;
571
- }
572
- const keypair = fromPrivateKey(Buffer.from(this.identityKey.privateKey));
573
- psbt.signInput(input, keypair, sighashTypes);
574
- return psbt;
575
- }
576
- getReceiptPrivateKey(receipt) {
577
- const pxh = Receipt.receiptHash(receipt);
578
- let innerKey = this.identityKey.publicKey;
579
- let privateKey = this.identityKey.privateKey;
580
- if (innerKey[0] === 3) {
581
- innerKey = Buffer.concat([PARITY, innerKey.slice(1)]);
582
- privateKey = Buffer.from(privateNegate(privateKey));
583
- }
584
- const pxhPubkey = sha256(Buffer.concat([pxh, innerKey]));
585
- const receiptProof = privateAdd(privateKey, pxhPubkey);
586
- return Buffer.from(receiptProof);
587
- }
588
- signTransactionIndex(tx, index, publicKey) {
589
- let privateKey;
590
- if (equalBytes(publicKey, this.identityKey?.publicKey ?? new Uint8Array())) {
591
- privateKey = this.identityKey?.privateKey;
592
- } else if (equalBytes(publicKey, this.depositKey?.publicKey ?? new Uint8Array())) {
593
- privateKey = this.depositKey?.privateKey;
594
- } else {
595
- privateKey = hexToBytes(
596
- this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey)) ?? ""
597
- );
598
- }
599
- if (!privateKey) {
600
- throw new ValidationError("Private key not found for public key", {
601
- field: "privateKey",
602
- value: bytesToHex(publicKey)
603
- });
604
- }
605
- tx.signIdx(privateKey, index);
606
- }
607
- };
608
- var TaprootSparkSigner = class extends DefaultSparkSigner {
609
- constructor() {
610
- super({ sparkKeysGenerator: new TaprootOutputKeysGenerator() });
611
- }
612
- };
613
-
614
- export {
615
- TaprootOutputKeysGenerator,
616
- DefaultSparkSigner,
617
- TaprootSparkSigner,
618
- Receipt2 as Receipt,
619
- PARITY2 as PARITY,
620
- fromPrivateKey2 as fromPrivateKey,
621
- MultisigReceiptInput
622
- };
@@ -1,84 +0,0 @@
1
- import {
2
- ValidationError
3
- } from "./chunk-GSI4OLXZ.js";
4
-
5
- // src/utils/network.ts
6
- import { NetworkType as Lrc20NetworkType } from "@buildonspark/lrc20-sdk";
7
- import * as btc from "@scure/btc-signer";
8
- import * as bitcoin from "bitcoinjs-lib";
9
- var Network2 = /* @__PURE__ */ ((Network3) => {
10
- Network3[Network3["MAINNET"] = 0] = "MAINNET";
11
- Network3[Network3["TESTNET"] = 1] = "TESTNET";
12
- Network3[Network3["SIGNET"] = 2] = "SIGNET";
13
- Network3[Network3["REGTEST"] = 3] = "REGTEST";
14
- Network3[Network3["LOCAL"] = 4] = "LOCAL";
15
- return Network3;
16
- })(Network2 || {});
17
- var NetworkToProto = {
18
- [0 /* MAINNET */]: 1 /* MAINNET */,
19
- [1 /* TESTNET */]: 3 /* TESTNET */,
20
- [2 /* SIGNET */]: 4 /* SIGNET */,
21
- [3 /* REGTEST */]: 2 /* REGTEST */,
22
- [4 /* LOCAL */]: 2 /* REGTEST */
23
- };
24
- var NetworkConfig = {
25
- [0 /* MAINNET */]: btc.NETWORK,
26
- [1 /* TESTNET */]: btc.TEST_NETWORK,
27
- [2 /* SIGNET */]: btc.TEST_NETWORK,
28
- [3 /* REGTEST */]: { ...btc.TEST_NETWORK, bech32: "bcrt" },
29
- [4 /* LOCAL */]: { ...btc.TEST_NETWORK, bech32: "bcrt" }
30
- };
31
- var getNetwork = (network) => NetworkConfig[network];
32
- var LRC_WALLET_NETWORK = Object.freeze({
33
- [0 /* MAINNET */]: bitcoin.networks.bitcoin,
34
- [1 /* TESTNET */]: bitcoin.networks.testnet,
35
- [2 /* SIGNET */]: bitcoin.networks.testnet,
36
- [3 /* REGTEST */]: bitcoin.networks.regtest,
37
- [4 /* LOCAL */]: bitcoin.networks.regtest
38
- });
39
- var LRC_WALLET_NETWORK_TYPE = Object.freeze({
40
- [0 /* MAINNET */]: Lrc20NetworkType.MAINNET,
41
- [1 /* TESTNET */]: Lrc20NetworkType.TESTNET,
42
- [2 /* SIGNET */]: Lrc20NetworkType.TESTNET,
43
- [3 /* REGTEST */]: Lrc20NetworkType.REGTEST,
44
- [4 /* LOCAL */]: Lrc20NetworkType.LOCAL
45
- });
46
- function getNetworkFromAddress(address2) {
47
- try {
48
- const decoded = bitcoin.address.fromBech32(address2);
49
- if (decoded.prefix === "bc") {
50
- return "MAINNET" /* MAINNET */;
51
- } else if (decoded.prefix === "bcrt") {
52
- return "REGTEST" /* REGTEST */;
53
- }
54
- } catch (err) {
55
- throw new ValidationError(
56
- "Invalid Bitcoin address",
57
- {
58
- field: "address",
59
- value: address2,
60
- expected: "Valid Bech32 address with prefix 'bc' or 'bcrt'"
61
- },
62
- err instanceof Error ? err : void 0
63
- );
64
- }
65
- return null;
66
- }
67
- function getNetworkFromString(network) {
68
- const net = (network ?? "REGTEST").toUpperCase();
69
- if (net === "MAINNET") return 0 /* MAINNET */;
70
- if (net === "TESTNET") return 1 /* TESTNET */;
71
- if (net === "SIGNET") return 2 /* SIGNET */;
72
- if (net === "LOCAL") return 4 /* LOCAL */;
73
- return 3 /* REGTEST */;
74
- }
75
-
76
- export {
77
- Network2 as Network,
78
- NetworkToProto,
79
- getNetwork,
80
- LRC_WALLET_NETWORK,
81
- LRC_WALLET_NETWORK_TYPE,
82
- getNetworkFromAddress,
83
- getNetworkFromString
84
- };