@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
@@ -4,12 +4,12 @@ import {
4
4
  getSigningCommitmentFromNonce,
5
5
  subtractPrivateKeys
6
6
  } from "./chunk-7VMYMQLF.js";
7
- import {
8
- splitSecretWithProofs
9
- } from "./chunk-MGPRLH6Q.js";
10
7
  import {
11
8
  isReactNative
12
9
  } from "./chunk-HKAKEKCE.js";
10
+ import {
11
+ splitSecretWithProofs
12
+ } from "./chunk-MGPRLH6Q.js";
13
13
  import {
14
14
  ConfigurationError,
15
15
  ValidationError
@@ -22,6 +22,7 @@ import {
22
22
  import {
23
23
  bytesToHex,
24
24
  bytesToNumberBE,
25
+ equalBytes,
25
26
  hexToBytes
26
27
  } from "@noble/curves/abstract/utils";
27
28
  import { schnorr, secp256k1 } from "@noble/curves/secp256k1";
@@ -29,11 +30,14 @@ import { HDKey } from "@scure/bip32";
29
30
  import { generateMnemonic, mnemonicToSeed } from "@scure/bip39";
30
31
  import { wordlist } from "@scure/bip39/wordlists/english";
31
32
  import * as ecies from "eciesjs";
32
- import { PARITY } from "@buildonspark/lrc20-sdk";
33
- import { Receipt } from "@buildonspark/lrc20-sdk";
34
- import { privateNegate, privateAdd } from "@bitcoinerlab/secp256k1";
33
+ import { privateAdd, privateNegate } from "@bitcoinerlab/secp256k1";
34
+ import {
35
+ fromPrivateKey,
36
+ PARITY,
37
+ Receipt
38
+ } from "@buildonspark/lrc20-sdk";
35
39
  import { sha256 } from "@noble/hashes/sha2";
36
- import { fromPrivateKey } from "@buildonspark/lrc20-sdk";
40
+ import { taprootTweakPrivKey } from "@scure/btc-signer/utils";
37
41
  var sparkFrostModule = void 0;
38
42
  var getSparkFrostModule = async () => {
39
43
  if (isReactNative) {
@@ -45,8 +49,8 @@ var getSparkFrostModule = async () => {
45
49
  return sparkFrostModule;
46
50
  };
47
51
  var HARDENED_OFFSET = 2147483648;
48
- var DefaultHDKeyGenerator = class {
49
- async deriveHDKeysFromSeed(seed, accountNumber) {
52
+ var DefaultSparkKeysGenerator = class {
53
+ async deriveKeysFromSeed(seed, accountNumber) {
50
54
  const hdkey = HDKey.fromMasterSeed(seed);
51
55
  if (!hdkey.privateKey || !hdkey.publicKey) {
52
56
  throw new ValidationError("Failed to derive keys from seed", {
@@ -67,27 +71,79 @@ var DefaultHDKeyGenerator = class {
67
71
  );
68
72
  }
69
73
  return {
70
- masterKey: {
71
- hdKey: hdkey,
72
- privateKey: hdkey.privateKey,
73
- publicKey: hdkey.publicKey
74
+ masterPublicKey: hdkey.publicKey,
75
+ identityKey: {
76
+ privateKey: identityKey.privateKey,
77
+ publicKey: identityKey.publicKey
78
+ },
79
+ signingHDKey: {
80
+ hdKey: signingKey,
81
+ privateKey: signingKey.privateKey,
82
+ publicKey: signingKey.publicKey
74
83
  },
84
+ depositKey: {
85
+ privateKey: depositKey.privateKey,
86
+ publicKey: depositKey.publicKey
87
+ },
88
+ staticDepositHDKey: {
89
+ hdKey: staticDepositKey,
90
+ privateKey: staticDepositKey.privateKey,
91
+ publicKey: staticDepositKey.publicKey
92
+ }
93
+ };
94
+ }
95
+ };
96
+ var TaprootOutputKeysGenerator = class {
97
+ constructor(useAddressIndex = false) {
98
+ this.useAddressIndex = useAddressIndex;
99
+ }
100
+ async deriveKeysFromSeed(seed, accountNumber) {
101
+ const hdkey = HDKey.fromMasterSeed(seed);
102
+ if (!hdkey.privateKey || !hdkey.publicKey) {
103
+ throw new ValidationError("Failed to derive keys from seed", {
104
+ field: "hdkey",
105
+ value: seed
106
+ });
107
+ }
108
+ const derivationPath = this.useAddressIndex ? `m/86'/0'/0'/0/${accountNumber}` : `m/86'/0'/${accountNumber}'/0/0`;
109
+ const taprootInternalKey = hdkey.derive(derivationPath);
110
+ let tweakedPrivateKey = taprootTweakPrivKey(taprootInternalKey.privateKey);
111
+ let tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
112
+ if (tweakedPublicKey[0] === 3) {
113
+ tweakedPrivateKey = privateNegate(tweakedPrivateKey);
114
+ tweakedPublicKey = secp256k1.getPublicKey(tweakedPrivateKey);
115
+ }
116
+ const identityKey = {
117
+ publicKey: tweakedPublicKey,
118
+ privateKey: tweakedPrivateKey
119
+ };
120
+ const signingKey = hdkey.derive(`${derivationPath}/1'`);
121
+ const depositKey = hdkey.derive(`${derivationPath}/2'`);
122
+ const staticDepositKey = hdkey.derive(`${derivationPath}/3'`);
123
+ if (!signingKey.privateKey || !signingKey.publicKey || !depositKey.privateKey || !depositKey.publicKey || !staticDepositKey.privateKey || !staticDepositKey.publicKey) {
124
+ throw new ValidationError(
125
+ "Failed to derive all required keys from seed",
126
+ {
127
+ field: "derivedKeys"
128
+ }
129
+ );
130
+ }
131
+ return {
132
+ masterPublicKey: hdkey.publicKey,
75
133
  identityKey: {
76
- hdKey: identityKey,
77
134
  privateKey: identityKey.privateKey,
78
135
  publicKey: identityKey.publicKey
79
136
  },
80
- signingKey: {
137
+ signingHDKey: {
81
138
  hdKey: signingKey,
82
139
  privateKey: signingKey.privateKey,
83
140
  publicKey: signingKey.publicKey
84
141
  },
85
142
  depositKey: {
86
- hdKey: depositKey,
87
143
  privateKey: depositKey.privateKey,
88
144
  publicKey: depositKey.publicKey
89
145
  },
90
- staticDepositKey: {
146
+ staticDepositHDKey: {
91
147
  hdKey: staticDepositKey,
92
148
  privateKey: staticDepositKey.privateKey,
93
149
  publicKey: staticDepositKey.publicKey
@@ -96,7 +152,7 @@ var DefaultHDKeyGenerator = class {
96
152
  }
97
153
  };
98
154
  var DefaultSparkSigner = class {
99
- masterKey = null;
155
+ masterPublicKey = null;
100
156
  identityKey = null;
101
157
  signingKey = null;
102
158
  depositKey = null;
@@ -105,14 +161,16 @@ var DefaultSparkSigner = class {
105
161
  // <hex, hex>
106
162
  publicKeyToPrivateKeyMap = /* @__PURE__ */ new Map();
107
163
  commitmentToNonceMap = /* @__PURE__ */ new Map();
108
- hdKeyGenerator;
109
- constructor({ hdKeyGenerator } = {}) {
110
- this.hdKeyGenerator = hdKeyGenerator ?? new DefaultHDKeyGenerator();
164
+ keysGenerator;
165
+ constructor({
166
+ sparkKeysGenerator
167
+ } = {}) {
168
+ this.keysGenerator = sparkKeysGenerator ?? new DefaultSparkKeysGenerator();
111
169
  }
112
170
  deriveSigningKey(hash) {
113
- if (!this.masterKey) {
171
+ if (!this.signingKey) {
114
172
  throw new ValidationError("Private key not initialized", {
115
- field: "masterKey"
173
+ field: "signingKey"
116
174
  });
117
175
  }
118
176
  const view = new DataView(hash.buffer);
@@ -126,9 +184,9 @@ var DefaultSparkSigner = class {
126
184
  return newPrivateKey;
127
185
  }
128
186
  async restoreSigningKeysFromLeafs(leafs) {
129
- if (!this.masterKey) {
130
- throw new ValidationError("Master key is not set", {
131
- field: "masterKey"
187
+ if (!this.signingKey) {
188
+ throw new ValidationError("Signing key is not set", {
189
+ field: "signingKey"
132
190
  });
133
191
  }
134
192
  for (const leaf of leafs) {
@@ -248,9 +306,9 @@ var DefaultSparkSigner = class {
248
306
  return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(hexToBytes);
249
307
  }
250
308
  async generatePublicKey(hash) {
251
- if (!this.masterKey) {
309
+ if (!this.signingKey) {
252
310
  throw new ValidationError("Private key is not set", {
253
- field: "masterKey"
311
+ field: "signingKey"
254
312
  });
255
313
  }
256
314
  let newPrivateKey = null;
@@ -386,10 +444,16 @@ var DefaultSparkSigner = class {
386
444
  if (typeof seed === "string") {
387
445
  seed = hexToBytes(seed);
388
446
  }
389
- const { masterKey, identityKey, signingKey, depositKey, staticDepositKey } = await this.hdKeyGenerator.deriveHDKeysFromSeed(seed, accountNumber ?? 0);
390
- this.masterKey = masterKey.hdKey;
391
- this.identityKey = identityKey.hdKey;
392
- this.depositKey = depositKey.hdKey;
447
+ const {
448
+ masterPublicKey,
449
+ identityKey,
450
+ signingHDKey: signingKey,
451
+ depositKey,
452
+ staticDepositHDKey: staticDepositKey
453
+ } = await this.keysGenerator.deriveKeysFromSeed(seed, accountNumber ?? 0);
454
+ this.masterPublicKey = masterPublicKey;
455
+ this.identityKey = identityKey;
456
+ this.depositKey = depositKey;
393
457
  this.signingKey = signingKey.hdKey;
394
458
  this.staticDepositKey = staticDepositKey.hdKey;
395
459
  this.publicKeyToPrivateKeyMap.set(
@@ -478,10 +542,10 @@ var DefaultSparkSigner = class {
478
542
  };
479
543
  }
480
544
  async getMasterPublicKey() {
481
- if (!this.masterKey?.publicKey) {
545
+ if (!this.masterPublicKey) {
482
546
  throw new Error("Private key is not set");
483
547
  }
484
- return this.masterKey.publicKey;
548
+ return this.masterPublicKey;
485
549
  }
486
550
  async validateMessageWithIdentityKey(message, signature) {
487
551
  if (!this.identityKey?.publicKey) {
@@ -519,8 +583,34 @@ var DefaultSparkSigner = class {
519
583
  const receiptProof = privateAdd(privateKey, pxhPubkey);
520
584
  return Buffer.from(receiptProof);
521
585
  }
586
+ signTransactionIndex(tx, index, publicKey) {
587
+ let privateKey;
588
+ if (equalBytes(publicKey, this.identityKey?.publicKey ?? new Uint8Array())) {
589
+ privateKey = this.identityKey?.privateKey;
590
+ } else if (equalBytes(publicKey, this.depositKey?.publicKey ?? new Uint8Array())) {
591
+ privateKey = this.depositKey?.privateKey;
592
+ } else {
593
+ privateKey = hexToBytes(
594
+ this.publicKeyToPrivateKeyMap.get(bytesToHex(publicKey)) ?? ""
595
+ );
596
+ }
597
+ if (!privateKey) {
598
+ throw new ValidationError("Private key not found for public key", {
599
+ field: "privateKey",
600
+ value: bytesToHex(publicKey)
601
+ });
602
+ }
603
+ tx.signIdx(privateKey, index);
604
+ }
605
+ };
606
+ var TaprootSparkSigner = class extends DefaultSparkSigner {
607
+ constructor() {
608
+ super({ sparkKeysGenerator: new TaprootOutputKeysGenerator() });
609
+ }
522
610
  };
523
611
 
524
612
  export {
525
- DefaultSparkSigner
613
+ TaprootOutputKeysGenerator,
614
+ DefaultSparkSigner,
615
+ TaprootSparkSigner
526
616
  };