@buildonspark/spark-sdk 0.1.39 → 0.1.41

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 (125) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -1
  3. package/dist/{RequestLightningSendInput-39_zGri6.d.cts → RequestLightningSendInput-DXcLoiCe.d.cts} +10 -2
  4. package/dist/{RequestLightningSendInput-B4JdzclX.d.ts → RequestLightningSendInput-mXUWn_cp.d.ts} +10 -2
  5. package/dist/address/index.cjs +138 -6
  6. package/dist/address/index.d.cts +18 -6
  7. package/dist/address/index.d.ts +18 -6
  8. package/dist/address/index.js +5 -2
  9. package/dist/{chunk-FWQPAPXK.js → chunk-2ZXXLPG2.js} +1 -1
  10. package/dist/{chunk-S7KD6DDL.js → chunk-6YVPOQ2A.js} +41 -20
  11. package/dist/{chunk-ZUVYYR5T.js → chunk-7EFSUADA.js} +1 -0
  12. package/dist/{chunk-NS4UZRQ7.js → chunk-ABZA6R5S.js} +1 -1
  13. package/dist/{chunk-57XLH3ZR.js → chunk-ATEHMLKP.js} +23 -23
  14. package/dist/{chunk-VJTDG4BQ.js → chunk-HK6LPV6Z.js} +10 -1
  15. package/dist/{chunk-W3EC5XSA.js → chunk-J5W5Q2ZP.js} +337 -72
  16. package/dist/{chunk-TKYOYOYJ.js → chunk-KKSU7OZO.js} +653 -76
  17. package/dist/chunk-L3EHBOUX.js +0 -0
  18. package/dist/{chunk-C5LTJBI7.js → chunk-M6A4KFIG.js} +125 -226
  19. package/dist/{chunk-A74XSEW3.js → chunk-MIVX3GHD.js} +1 -1
  20. package/dist/{chunk-RGWBSZIO.js → chunk-ROKY5KS4.js} +23 -3
  21. package/dist/{chunk-LIP2K6KR.js → chunk-TM4TOEOX.js} +26 -8
  22. package/dist/{chunk-RAPBVYJY.js → chunk-UKT6OFLO.js} +125 -35
  23. package/dist/chunk-VA7MV4MZ.js +1073 -0
  24. package/dist/chunk-YEZDPUFY.js +840 -0
  25. package/dist/{chunk-DI7QXUQJ.js → chunk-ZXDE2XMU.js} +8 -5
  26. package/dist/graphql/objects/index.cjs +6 -3
  27. package/dist/graphql/objects/index.d.cts +6 -5
  28. package/dist/graphql/objects/index.d.ts +6 -5
  29. package/dist/graphql/objects/index.js +1 -1
  30. package/dist/{index-DEo_hdN3.d.cts → index-CFh4uWzi.d.cts} +60 -6
  31. package/dist/{index-BVY0yH_H.d.ts → index-OSDtPMmC.d.ts} +60 -6
  32. package/dist/index.cjs +3316 -954
  33. package/dist/index.d.cts +9 -8
  34. package/dist/index.d.ts +9 -8
  35. package/dist/index.js +48 -26
  36. package/dist/index.node.cjs +3316 -954
  37. package/dist/index.node.d.cts +9 -8
  38. package/dist/index.node.d.ts +9 -8
  39. package/dist/index.node.js +48 -26
  40. package/dist/native/index.cjs +3323 -961
  41. package/dist/native/index.d.cts +542 -260
  42. package/dist/native/index.d.ts +542 -260
  43. package/dist/native/index.js +3192 -838
  44. package/dist/{network-DobHpaV6.d.ts → network-BF2GYPye.d.ts} +9 -2
  45. package/dist/{network-GFGEHkS4.d.cts → network-BiwBmoOg.d.cts} +9 -2
  46. package/dist/proto/lrc20.d.cts +1 -1
  47. package/dist/proto/lrc20.d.ts +1 -1
  48. package/dist/proto/lrc20.js +2 -2
  49. package/dist/proto/spark.cjs +125 -226
  50. package/dist/proto/spark.d.cts +1 -1
  51. package/dist/proto/spark.d.ts +1 -1
  52. package/dist/proto/spark.js +3 -5
  53. package/dist/proto/spark_token.cjs +1364 -0
  54. package/dist/proto/spark_token.d.cts +209 -0
  55. package/dist/proto/spark_token.d.ts +209 -0
  56. package/dist/proto/spark_token.js +32 -0
  57. package/dist/{sdk-types-BuVMn2rX.d.cts → sdk-types-CfhdFnsA.d.cts} +1 -1
  58. package/dist/{sdk-types-BeI6DM_M.d.ts → sdk-types-MnQrHolg.d.ts} +1 -1
  59. package/dist/services/config.cjs +64 -40
  60. package/dist/services/config.d.cts +6 -5
  61. package/dist/services/config.d.ts +6 -5
  62. package/dist/services/config.js +7 -7
  63. package/dist/services/connection.cjs +1108 -306
  64. package/dist/services/connection.d.cts +10 -5
  65. package/dist/services/connection.d.ts +10 -5
  66. package/dist/services/connection.js +3 -2
  67. package/dist/services/index.cjs +1702 -488
  68. package/dist/services/index.d.cts +6 -5
  69. package/dist/services/index.d.ts +6 -5
  70. package/dist/services/index.js +16 -14
  71. package/dist/services/lrc-connection.d.cts +5 -5
  72. package/dist/services/lrc-connection.d.ts +5 -5
  73. package/dist/services/lrc-connection.js +3 -3
  74. package/dist/services/token-transactions.cjs +637 -247
  75. package/dist/services/token-transactions.d.cts +19 -8
  76. package/dist/services/token-transactions.d.ts +19 -8
  77. package/dist/services/token-transactions.js +5 -4
  78. package/dist/services/wallet-config.cjs +1 -0
  79. package/dist/services/wallet-config.d.cts +6 -5
  80. package/dist/services/wallet-config.d.ts +6 -5
  81. package/dist/services/wallet-config.js +1 -1
  82. package/dist/signer/signer.cjs +122 -35
  83. package/dist/signer/signer.d.cts +4 -3
  84. package/dist/signer/signer.d.ts +4 -3
  85. package/dist/signer/signer.js +8 -4
  86. package/dist/{signer-C1t40Wus.d.cts → signer-BhLS7SYR.d.cts} +35 -14
  87. package/dist/{signer-DFGw9RRp.d.ts → signer-CylxIujU.d.ts} +35 -14
  88. package/dist/{spark-DXYE9gMM.d.ts → spark-DjR1b3TC.d.cts} +13 -21
  89. package/dist/{spark-DXYE9gMM.d.cts → spark-DjR1b3TC.d.ts} +13 -21
  90. package/dist/types/index.cjs +130 -227
  91. package/dist/types/index.d.cts +6 -5
  92. package/dist/types/index.d.ts +6 -5
  93. package/dist/types/index.js +3 -3
  94. package/dist/utils/index.cjs +1169 -3
  95. package/dist/utils/index.d.cts +66 -6
  96. package/dist/utils/index.d.ts +66 -6
  97. package/dist/utils/index.js +35 -14
  98. package/package.json +6 -2
  99. package/src/address/address.ts +41 -6
  100. package/src/graphql/client.ts +15 -0
  101. package/src/graphql/objects/Transfer.ts +7 -0
  102. package/src/graphql/queries/Transfer.ts +10 -0
  103. package/src/proto/spark.ts +215 -337
  104. package/src/proto/spark_token.ts +1407 -0
  105. package/src/services/config.ts +4 -0
  106. package/src/services/connection.ts +37 -1
  107. package/src/services/deposit.ts +23 -5
  108. package/src/services/token-transactions.ts +426 -75
  109. package/src/services/transfer.ts +182 -11
  110. package/src/services/tree-creation.ts +29 -14
  111. package/src/services/wallet-config.ts +2 -0
  112. package/src/signer/signer.ts +190 -48
  113. package/src/spark-wallet/spark-wallet.ts +510 -6
  114. package/src/tests/integration/transfer.test.ts +186 -214
  115. package/src/tests/integration/tree-creation.test.ts +5 -1
  116. package/src/tests/signer.test.ts +34 -0
  117. package/src/tests/transaction.test.ts +12 -0
  118. package/src/tests/xchain-address.test.ts +28 -0
  119. package/src/utils/index.ts +2 -0
  120. package/src/utils/mempool.ts +26 -1
  121. package/src/utils/network.ts +15 -0
  122. package/src/utils/transaction.ts +51 -3
  123. package/src/utils/unilateral-exit.ts +729 -0
  124. package/src/utils/xchain-address.ts +36 -0
  125. package/dist/chunk-E5SL7XTO.js +0 -301
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  bytesToHex,
3
3
  bytesToNumberBE,
4
+ equalBytes,
4
5
  hexToBytes,
5
6
  } from "@noble/curves/abstract/utils";
6
7
  import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
@@ -35,12 +36,17 @@ const getSparkFrostModule = async () => {
35
36
  return sparkFrostModule;
36
37
  };
37
38
 
38
- import { PARITY } from "@buildonspark/lrc20-sdk";
39
- import type { Psbt } from "bitcoinjs-lib";
40
- import { TokenSigner, Receipt } from "@buildonspark/lrc20-sdk";
41
- import { privateNegate, privateAdd } from "@bitcoinerlab/secp256k1";
39
+ import { privateAdd, privateNegate } from "@bitcoinerlab/secp256k1";
40
+ import {
41
+ fromPrivateKey,
42
+ PARITY,
43
+ Receipt,
44
+ TokenSigner,
45
+ } from "@buildonspark/lrc20-sdk";
42
46
  import { sha256 } from "@noble/hashes/sha2";
43
- import { fromPrivateKey } from "@buildonspark/lrc20-sdk";
47
+ import { Transaction } from "@scure/btc-signer";
48
+ import { taprootTweakPrivKey } from "@scure/btc-signer/utils";
49
+ import type { Psbt } from "bitcoinjs-lib";
44
50
 
45
51
  export type SigningNonce = {
46
52
  binding: Uint8Array;
@@ -82,31 +88,36 @@ type DerivedHDKey = {
82
88
  publicKey: Uint8Array;
83
89
  };
84
90
 
85
- interface HDKeyGenerator {
86
- deriveHDKeysFromSeed(
91
+ type KeyPair = {
92
+ privateKey: Uint8Array;
93
+ publicKey: Uint8Array;
94
+ };
95
+
96
+ interface SparkKeysGenerator {
97
+ deriveKeysFromSeed(
87
98
  seed: Uint8Array,
88
99
  accountNumber: number,
89
100
  ): Promise<{
90
- masterKey: DerivedHDKey;
91
- identityKey: DerivedHDKey;
92
- signingKey: DerivedHDKey;
93
- depositKey: DerivedHDKey;
94
- staticDepositKey: DerivedHDKey;
101
+ masterPublicKey: Uint8Array;
102
+ identityKey: KeyPair;
103
+ signingHDKey: DerivedHDKey;
104
+ depositKey: KeyPair;
105
+ staticDepositHDKey: DerivedHDKey;
95
106
  }>;
96
107
  }
97
108
 
98
109
  const HARDENED_OFFSET = 0x80000000; // 2^31
99
110
 
100
- class DefaultHDKeyGenerator implements HDKeyGenerator {
101
- async deriveHDKeysFromSeed(
111
+ class DefaultSparkKeysGenerator implements SparkKeysGenerator {
112
+ async deriveKeysFromSeed(
102
113
  seed: Uint8Array,
103
114
  accountNumber: number,
104
115
  ): Promise<{
105
- masterKey: DerivedHDKey;
106
- identityKey: DerivedHDKey;
107
- signingKey: DerivedHDKey;
108
- depositKey: DerivedHDKey;
109
- staticDepositKey: DerivedHDKey;
116
+ masterPublicKey: Uint8Array;
117
+ identityKey: KeyPair;
118
+ signingHDKey: DerivedHDKey;
119
+ depositKey: KeyPair;
120
+ staticDepositHDKey: DerivedHDKey;
110
121
  }> {
111
122
  const hdkey = HDKey.fromMasterSeed(seed);
112
123
 
@@ -141,27 +152,107 @@ class DefaultHDKeyGenerator implements HDKeyGenerator {
141
152
  }
142
153
 
143
154
  return {
144
- masterKey: {
145
- hdKey: hdkey,
146
- privateKey: hdkey.privateKey,
147
- publicKey: hdkey.publicKey,
155
+ masterPublicKey: hdkey.publicKey,
156
+ identityKey: {
157
+ privateKey: identityKey.privateKey,
158
+ publicKey: identityKey.publicKey,
148
159
  },
160
+ signingHDKey: {
161
+ hdKey: signingKey,
162
+ privateKey: signingKey.privateKey,
163
+ publicKey: signingKey.publicKey,
164
+ },
165
+ depositKey: {
166
+ privateKey: depositKey.privateKey,
167
+ publicKey: depositKey.publicKey,
168
+ },
169
+ staticDepositHDKey: {
170
+ hdKey: staticDepositKey,
171
+ privateKey: staticDepositKey.privateKey,
172
+ publicKey: staticDepositKey.publicKey,
173
+ },
174
+ };
175
+ }
176
+ }
177
+
178
+ class TaprootOutputKeysGenerator implements SparkKeysGenerator {
179
+ constructor(private readonly useAddressIndex: boolean = false) {}
180
+
181
+ async deriveKeysFromSeed(
182
+ seed: Uint8Array,
183
+ accountNumber: number,
184
+ ): Promise<{
185
+ masterPublicKey: Uint8Array;
186
+ identityKey: KeyPair;
187
+ signingHDKey: DerivedHDKey;
188
+ depositKey: KeyPair;
189
+ staticDepositHDKey: DerivedHDKey;
190
+ }> {
191
+ const hdkey = HDKey.fromMasterSeed(seed);
192
+
193
+ if (!hdkey.privateKey || !hdkey.publicKey) {
194
+ throw new ValidationError("Failed to derive keys from seed", {
195
+ field: "hdkey",
196
+ value: seed,
197
+ });
198
+ }
199
+
200
+ const derivationPath = this.useAddressIndex
201
+ ? `m/86'/0'/0'/0/${accountNumber}`
202
+ : `m/86'/0'/${accountNumber}'/0/0`;
203
+
204
+ const taprootInternalKey = hdkey.derive(derivationPath);
205
+
206
+ let tweakedPrivateKey = taprootTweakPrivKey(taprootInternalKey.privateKey!);
207
+ let tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
208
+
209
+ // always use the even key
210
+ if (tweakedPublicKey[0] === 3) {
211
+ tweakedPrivateKey = privateNegate(tweakedPrivateKey);
212
+ tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
213
+ }
214
+
215
+ const identityKey = {
216
+ publicKey: tweakedPublicKey,
217
+ privateKey: tweakedPrivateKey,
218
+ };
219
+
220
+ const signingKey = hdkey.derive(`${derivationPath}/1'`);
221
+ const depositKey = hdkey.derive(`${derivationPath}/2'`);
222
+ const staticDepositKey = hdkey.derive(`${derivationPath}/3'`);
223
+
224
+ if (
225
+ !signingKey.privateKey ||
226
+ !signingKey.publicKey ||
227
+ !depositKey.privateKey ||
228
+ !depositKey.publicKey ||
229
+ !staticDepositKey.privateKey ||
230
+ !staticDepositKey.publicKey
231
+ ) {
232
+ throw new ValidationError(
233
+ "Failed to derive all required keys from seed",
234
+ {
235
+ field: "derivedKeys",
236
+ },
237
+ );
238
+ }
239
+
240
+ return {
241
+ masterPublicKey: hdkey.publicKey,
149
242
  identityKey: {
150
- hdKey: identityKey,
151
243
  privateKey: identityKey.privateKey,
152
244
  publicKey: identityKey.publicKey,
153
245
  },
154
- signingKey: {
246
+ signingHDKey: {
155
247
  hdKey: signingKey,
156
248
  privateKey: signingKey.privateKey,
157
249
  publicKey: signingKey.publicKey,
158
250
  },
159
251
  depositKey: {
160
- hdKey: depositKey,
161
252
  privateKey: depositKey.privateKey,
162
253
  publicKey: depositKey.publicKey,
163
254
  },
164
- staticDepositKey: {
255
+ staticDepositHDKey: {
165
256
  hdKey: staticDepositKey,
166
257
  privateKey: staticDepositKey.privateKey,
167
258
  publicKey: staticDepositKey.publicKey,
@@ -223,6 +314,12 @@ interface SparkSigner extends TokenSigner {
223
314
  signature: Uint8Array,
224
315
  ): Promise<boolean>;
225
316
 
317
+ signTransactionIndex(
318
+ tx: Transaction,
319
+ index: number,
320
+ publicKey: Uint8Array,
321
+ ): void;
322
+
226
323
  encryptLeafPrivateKeyEcies(
227
324
  receiverPublicKey: Uint8Array,
228
325
  publicKey: Uint8Array,
@@ -242,10 +339,10 @@ interface SparkSigner extends TokenSigner {
242
339
  }
243
340
 
244
341
  class DefaultSparkSigner implements SparkSigner {
245
- private masterKey: HDKey | null = null;
246
- private identityKey: HDKey | null = null;
342
+ private masterPublicKey: Uint8Array | null = null;
343
+ private identityKey: KeyPair | null = null;
247
344
  private signingKey: HDKey | null = null;
248
- private depositKey: HDKey | null = null;
345
+ private depositKey: KeyPair | null = null;
249
346
  private staticDepositKey: HDKey | null = null;
250
347
  private staticDepositKeyMap: Map<number, HDKey> = new Map();
251
348
 
@@ -255,16 +352,18 @@ class DefaultSparkSigner implements SparkSigner {
255
352
  protected commitmentToNonceMap: Map<SigningCommitment, SigningNonce> =
256
353
  new Map();
257
354
 
258
- private readonly hdKeyGenerator: HDKeyGenerator;
355
+ private readonly keysGenerator: SparkKeysGenerator;
259
356
 
260
- constructor({ hdKeyGenerator }: { hdKeyGenerator?: HDKeyGenerator } = {}) {
261
- this.hdKeyGenerator = hdKeyGenerator ?? new DefaultHDKeyGenerator();
357
+ constructor({
358
+ sparkKeysGenerator,
359
+ }: { sparkKeysGenerator?: SparkKeysGenerator } = {}) {
360
+ this.keysGenerator = sparkKeysGenerator ?? new DefaultSparkKeysGenerator();
262
361
  }
263
362
 
264
363
  private deriveSigningKey(hash: Uint8Array): Uint8Array {
265
- if (!this.masterKey) {
364
+ if (!this.signingKey) {
266
365
  throw new ValidationError("Private key not initialized", {
267
- field: "masterKey",
366
+ field: "signingKey",
268
367
  });
269
368
  }
270
369
 
@@ -284,9 +383,9 @@ class DefaultSparkSigner implements SparkSigner {
284
383
  }
285
384
 
286
385
  async restoreSigningKeysFromLeafs(leafs: TreeNode[]) {
287
- if (!this.masterKey) {
288
- throw new ValidationError("Master key is not set", {
289
- field: "masterKey",
386
+ if (!this.signingKey) {
387
+ throw new ValidationError("Signing key is not set", {
388
+ field: "signingKey",
290
389
  });
291
390
  }
292
391
 
@@ -439,9 +538,9 @@ class DefaultSparkSigner implements SparkSigner {
439
538
  }
440
539
 
441
540
  async generatePublicKey(hash?: Uint8Array): Promise<Uint8Array> {
442
- if (!this.masterKey) {
541
+ if (!this.signingKey) {
443
542
  throw new ValidationError("Private key is not set", {
444
- field: "masterKey",
543
+ field: "signingKey",
445
544
  });
446
545
  }
447
546
 
@@ -607,12 +706,17 @@ class DefaultSparkSigner implements SparkSigner {
607
706
  seed = hexToBytes(seed);
608
707
  }
609
708
 
610
- const { masterKey, identityKey, signingKey, depositKey, staticDepositKey } =
611
- await this.hdKeyGenerator.deriveHDKeysFromSeed(seed, accountNumber ?? 0);
709
+ const {
710
+ masterPublicKey,
711
+ identityKey,
712
+ signingHDKey: signingKey,
713
+ depositKey,
714
+ staticDepositHDKey: staticDepositKey,
715
+ } = await this.keysGenerator.deriveKeysFromSeed(seed, accountNumber ?? 0);
612
716
 
613
- this.masterKey = masterKey.hdKey;
614
- this.identityKey = identityKey.hdKey;
615
- this.depositKey = depositKey.hdKey;
717
+ this.masterPublicKey = masterPublicKey;
718
+ this.identityKey = identityKey;
719
+ this.depositKey = depositKey;
616
720
  this.signingKey = signingKey.hdKey;
617
721
  this.staticDepositKey = staticDepositKey.hdKey;
618
722
 
@@ -733,11 +837,11 @@ class DefaultSparkSigner implements SparkSigner {
733
837
  }
734
838
 
735
839
  async getMasterPublicKey(): Promise<Uint8Array> {
736
- if (!this.masterKey?.publicKey) {
840
+ if (!this.masterPublicKey) {
737
841
  throw new Error("Private key is not set");
738
842
  }
739
843
 
740
- return this.masterKey.publicKey;
844
+ return this.masterPublicKey;
741
845
  }
742
846
 
743
847
  async validateMessageWithIdentityKey(
@@ -790,6 +894,44 @@ class DefaultSparkSigner implements SparkSigner {
790
894
  const receiptProof = privateAdd(privateKey, pxhPubkey)!;
791
895
  return Buffer.from(receiptProof);
792
896
  }
897
+
898
+ signTransactionIndex(
899
+ tx: Transaction,
900
+ index: number,
901
+ publicKey: Uint8Array,
902
+ ): void {
903
+ let privateKey: Uint8Array | undefined | null;
904
+
905
+ if (
906
+ equalBytes(publicKey, this.identityKey?.publicKey ?? new Uint8Array())
907
+ ) {
908
+ privateKey = this.identityKey?.privateKey;
909
+ } else if (
910
+ equalBytes(publicKey, this.depositKey?.publicKey ?? new Uint8Array())
911
+ ) {
912
+ privateKey = this.depositKey?.privateKey;
913
+ } else {
914
+ privateKey = hexToBytes(
915
+ this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey)) ?? "",
916
+ );
917
+ }
918
+
919
+ if (!privateKey) {
920
+ throw new ValidationError("Private key not found for public key", {
921
+ field: "privateKey",
922
+ value: bytesToHex(publicKey),
923
+ });
924
+ }
925
+
926
+ tx.signIdx(privateKey, index);
927
+ }
793
928
  }
794
- export { DefaultSparkSigner };
929
+
930
+ class TaprootSparkSigner extends DefaultSparkSigner {
931
+ constructor() {
932
+ super({ sparkKeysGenerator: new TaprootOutputKeysGenerator() });
933
+ }
934
+ }
935
+
936
+ export { DefaultSparkSigner, TaprootSparkSigner, TaprootOutputKeysGenerator };
795
937
  export type { SparkSigner };