@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
@@ -1663,11 +1663,10 @@ function getSigningCommitmentFromNonce(nonce) {
1663
1663
  }
1664
1664
 
1665
1665
  // src/signer/signer.ts
1666
- var import_lrc20_sdk = require("@buildonspark/lrc20-sdk");
1667
- var import_lrc20_sdk2 = require("@buildonspark/lrc20-sdk");
1668
1666
  var import_secp256k16 = require("@bitcoinerlab/secp256k1");
1667
+ var import_lrc20_sdk = require("@buildonspark/lrc20-sdk");
1669
1668
  var import_sha2 = require("@noble/hashes/sha2");
1670
- var import_lrc20_sdk3 = require("@buildonspark/lrc20-sdk");
1669
+ var import_utils5 = require("@scure/btc-signer/utils");
1671
1670
  var sparkFrostModule = void 0;
1672
1671
  var getSparkFrostModule = async () => {
1673
1672
  if (isReactNative) {
@@ -1679,8 +1678,8 @@ var getSparkFrostModule = async () => {
1679
1678
  return sparkFrostModule;
1680
1679
  };
1681
1680
  var HARDENED_OFFSET = 2147483648;
1682
- var DefaultHDKeyGenerator = class {
1683
- async deriveHDKeysFromSeed(seed, accountNumber) {
1681
+ var DefaultSparkKeysGenerator = class {
1682
+ async deriveKeysFromSeed(seed, accountNumber) {
1684
1683
  const hdkey = import_bip32.HDKey.fromMasterSeed(seed);
1685
1684
  if (!hdkey.privateKey || !hdkey.publicKey) {
1686
1685
  throw new ValidationError("Failed to derive keys from seed", {
@@ -1701,27 +1700,21 @@ var DefaultHDKeyGenerator = class {
1701
1700
  );
1702
1701
  }
1703
1702
  return {
1704
- masterKey: {
1705
- hdKey: hdkey,
1706
- privateKey: hdkey.privateKey,
1707
- publicKey: hdkey.publicKey
1708
- },
1703
+ masterPublicKey: hdkey.publicKey,
1709
1704
  identityKey: {
1710
- hdKey: identityKey,
1711
1705
  privateKey: identityKey.privateKey,
1712
1706
  publicKey: identityKey.publicKey
1713
1707
  },
1714
- signingKey: {
1708
+ signingHDKey: {
1715
1709
  hdKey: signingKey,
1716
1710
  privateKey: signingKey.privateKey,
1717
1711
  publicKey: signingKey.publicKey
1718
1712
  },
1719
1713
  depositKey: {
1720
- hdKey: depositKey,
1721
1714
  privateKey: depositKey.privateKey,
1722
1715
  publicKey: depositKey.publicKey
1723
1716
  },
1724
- staticDepositKey: {
1717
+ staticDepositHDKey: {
1725
1718
  hdKey: staticDepositKey,
1726
1719
  privateKey: staticDepositKey.privateKey,
1727
1720
  publicKey: staticDepositKey.publicKey
@@ -1730,7 +1723,7 @@ var DefaultHDKeyGenerator = class {
1730
1723
  }
1731
1724
  };
1732
1725
  var DefaultSparkSigner = class {
1733
- masterKey = null;
1726
+ masterPublicKey = null;
1734
1727
  identityKey = null;
1735
1728
  signingKey = null;
1736
1729
  depositKey = null;
@@ -1739,14 +1732,16 @@ var DefaultSparkSigner = class {
1739
1732
  // <hex, hex>
1740
1733
  publicKeyToPrivateKeyMap = /* @__PURE__ */ new Map();
1741
1734
  commitmentToNonceMap = /* @__PURE__ */ new Map();
1742
- hdKeyGenerator;
1743
- constructor({ hdKeyGenerator } = {}) {
1744
- this.hdKeyGenerator = hdKeyGenerator ?? new DefaultHDKeyGenerator();
1735
+ keysGenerator;
1736
+ constructor({
1737
+ sparkKeysGenerator
1738
+ } = {}) {
1739
+ this.keysGenerator = sparkKeysGenerator ?? new DefaultSparkKeysGenerator();
1745
1740
  }
1746
1741
  deriveSigningKey(hash) {
1747
- if (!this.masterKey) {
1742
+ if (!this.signingKey) {
1748
1743
  throw new ValidationError("Private key not initialized", {
1749
- field: "masterKey"
1744
+ field: "signingKey"
1750
1745
  });
1751
1746
  }
1752
1747
  const view = new DataView(hash.buffer);
@@ -1760,9 +1755,9 @@ var DefaultSparkSigner = class {
1760
1755
  return newPrivateKey;
1761
1756
  }
1762
1757
  async restoreSigningKeysFromLeafs(leafs) {
1763
- if (!this.masterKey) {
1764
- throw new ValidationError("Master key is not set", {
1765
- field: "masterKey"
1758
+ if (!this.signingKey) {
1759
+ throw new ValidationError("Signing key is not set", {
1760
+ field: "signingKey"
1766
1761
  });
1767
1762
  }
1768
1763
  for (const leaf of leafs) {
@@ -1882,9 +1877,9 @@ var DefaultSparkSigner = class {
1882
1877
  return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(import_utils4.hexToBytes);
1883
1878
  }
1884
1879
  async generatePublicKey(hash) {
1885
- if (!this.masterKey) {
1880
+ if (!this.signingKey) {
1886
1881
  throw new ValidationError("Private key is not set", {
1887
- field: "masterKey"
1882
+ field: "signingKey"
1888
1883
  });
1889
1884
  }
1890
1885
  let newPrivateKey = null;
@@ -2020,10 +2015,16 @@ var DefaultSparkSigner = class {
2020
2015
  if (typeof seed === "string") {
2021
2016
  seed = (0, import_utils4.hexToBytes)(seed);
2022
2017
  }
2023
- const { masterKey, identityKey, signingKey, depositKey, staticDepositKey } = await this.hdKeyGenerator.deriveHDKeysFromSeed(seed, accountNumber ?? 0);
2024
- this.masterKey = masterKey.hdKey;
2025
- this.identityKey = identityKey.hdKey;
2026
- this.depositKey = depositKey.hdKey;
2018
+ const {
2019
+ masterPublicKey,
2020
+ identityKey,
2021
+ signingHDKey: signingKey,
2022
+ depositKey,
2023
+ staticDepositHDKey: staticDepositKey
2024
+ } = await this.keysGenerator.deriveKeysFromSeed(seed, accountNumber ?? 0);
2025
+ this.masterPublicKey = masterPublicKey;
2026
+ this.identityKey = identityKey;
2027
+ this.depositKey = depositKey;
2027
2028
  this.signingKey = signingKey.hdKey;
2028
2029
  this.staticDepositKey = staticDepositKey.hdKey;
2029
2030
  this.publicKeyToPrivateKeyMap.set(
@@ -2112,10 +2113,10 @@ var DefaultSparkSigner = class {
2112
2113
  };
2113
2114
  }
2114
2115
  async getMasterPublicKey() {
2115
- if (!this.masterKey?.publicKey) {
2116
+ if (!this.masterPublicKey) {
2116
2117
  throw new Error("Private key is not set");
2117
2118
  }
2118
- return this.masterKey.publicKey;
2119
+ return this.masterPublicKey;
2119
2120
  }
2120
2121
  async validateMessageWithIdentityKey(message, signature) {
2121
2122
  if (!this.identityKey?.publicKey) {
@@ -2133,16 +2134,16 @@ var DefaultSparkSigner = class {
2133
2134
  }
2134
2135
  if (receipt) {
2135
2136
  const receiptPrivateKey = this.getReceiptPrivateKey(receipt);
2136
- const tweakedKeyPair = (0, import_lrc20_sdk3.fromPrivateKey)(import_buffer.Buffer.from(receiptPrivateKey));
2137
+ const tweakedKeyPair = (0, import_lrc20_sdk.fromPrivateKey)(import_buffer.Buffer.from(receiptPrivateKey));
2137
2138
  psbt.signInput(input, tweakedKeyPair, sighashTypes);
2138
2139
  return psbt;
2139
2140
  }
2140
- const keypair = (0, import_lrc20_sdk3.fromPrivateKey)(import_buffer.Buffer.from(this.identityKey.privateKey));
2141
+ const keypair = (0, import_lrc20_sdk.fromPrivateKey)(import_buffer.Buffer.from(this.identityKey.privateKey));
2141
2142
  psbt.signInput(input, keypair, sighashTypes);
2142
2143
  return psbt;
2143
2144
  }
2144
2145
  getReceiptPrivateKey(receipt) {
2145
- const pxh = import_lrc20_sdk2.Receipt.receiptHash(receipt);
2146
+ const pxh = import_lrc20_sdk.Receipt.receiptHash(receipt);
2146
2147
  let innerKey = this.identityKey.publicKey;
2147
2148
  let privateKey = this.identityKey.privateKey;
2148
2149
  if (innerKey[0] === 3) {
@@ -2153,11 +2154,30 @@ var DefaultSparkSigner = class {
2153
2154
  const receiptProof = (0, import_secp256k16.privateAdd)(privateKey, pxhPubkey);
2154
2155
  return import_buffer.Buffer.from(receiptProof);
2155
2156
  }
2157
+ signTransactionIndex(tx, index, publicKey) {
2158
+ let privateKey;
2159
+ if ((0, import_utils4.equalBytes)(publicKey, this.identityKey?.publicKey ?? new Uint8Array())) {
2160
+ privateKey = this.identityKey?.privateKey;
2161
+ } else if ((0, import_utils4.equalBytes)(publicKey, this.depositKey?.publicKey ?? new Uint8Array())) {
2162
+ privateKey = this.depositKey?.privateKey;
2163
+ } else {
2164
+ privateKey = (0, import_utils4.hexToBytes)(
2165
+ this.publicKeyToPrivateKeyMap.get((0, import_utils4.bytesToHex)(publicKey)) ?? ""
2166
+ );
2167
+ }
2168
+ if (!privateKey) {
2169
+ throw new ValidationError("Private key not found for public key", {
2170
+ field: "privateKey",
2171
+ value: (0, import_utils4.bytesToHex)(publicKey)
2172
+ });
2173
+ }
2174
+ tx.signIdx(privateKey, index);
2175
+ }
2156
2176
  };
2157
2177
 
2158
2178
  // src/utils/network.ts
2159
2179
  init_buffer();
2160
- var import_lrc20_sdk4 = require("@buildonspark/lrc20-sdk");
2180
+ var import_lrc20_sdk2 = require("@buildonspark/lrc20-sdk");
2161
2181
  var btc = __toESM(require("@scure/btc-signer"), 1);
2162
2182
  var bitcoin = __toESM(require("bitcoinjs-lib"), 1);
2163
2183
 
@@ -2478,6 +2498,9 @@ function tokenTransactionStatusFromJSON(object) {
2478
2498
  case 1:
2479
2499
  case "TOKEN_TRANSACTION_SIGNED":
2480
2500
  return 1 /* TOKEN_TRANSACTION_SIGNED */;
2501
+ case 5:
2502
+ case "TOKEN_TRANSACTION_REVEALED":
2503
+ return 5 /* TOKEN_TRANSACTION_REVEALED */;
2481
2504
  case 2:
2482
2505
  case "TOKEN_TRANSACTION_FINALIZED":
2483
2506
  return 2 /* TOKEN_TRANSACTION_FINALIZED */;
@@ -2502,6 +2525,8 @@ function tokenTransactionStatusToJSON(object) {
2502
2525
  return "TOKEN_TRANSACTION_STARTED";
2503
2526
  case 1 /* TOKEN_TRANSACTION_SIGNED */:
2504
2527
  return "TOKEN_TRANSACTION_SIGNED";
2528
+ case 5 /* TOKEN_TRANSACTION_REVEALED */:
2529
+ return "TOKEN_TRANSACTION_REVEALED";
2505
2530
  case 2 /* TOKEN_TRANSACTION_FINALIZED */:
2506
2531
  return "TOKEN_TRANSACTION_FINALIZED";
2507
2532
  case 3 /* TOKEN_TRANSACTION_STARTED_CANCELLED */:
@@ -7177,59 +7202,6 @@ var SecretShare = {
7177
7202
  return message;
7178
7203
  }
7179
7204
  };
7180
- function createBaseSecretProof() {
7181
- return { proofs: [] };
7182
- }
7183
- var SecretProof = {
7184
- encode(message, writer = new import_wire4.BinaryWriter()) {
7185
- for (const v of message.proofs) {
7186
- writer.uint32(10).bytes(v);
7187
- }
7188
- return writer;
7189
- },
7190
- decode(input, length) {
7191
- const reader = input instanceof import_wire4.BinaryReader ? input : new import_wire4.BinaryReader(input);
7192
- let end = length === void 0 ? reader.len : reader.pos + length;
7193
- const message = createBaseSecretProof();
7194
- while (reader.pos < end) {
7195
- const tag = reader.uint32();
7196
- switch (tag >>> 3) {
7197
- case 1: {
7198
- if (tag !== 10) {
7199
- break;
7200
- }
7201
- message.proofs.push(reader.bytes());
7202
- continue;
7203
- }
7204
- }
7205
- if ((tag & 7) === 4 || tag === 0) {
7206
- break;
7207
- }
7208
- reader.skip(tag & 7);
7209
- }
7210
- return message;
7211
- },
7212
- fromJSON(object) {
7213
- return {
7214
- proofs: globalThis.Array.isArray(object?.proofs) ? object.proofs.map((e) => bytesFromBase642(e)) : []
7215
- };
7216
- },
7217
- toJSON(message) {
7218
- const obj = {};
7219
- if (message.proofs?.length) {
7220
- obj.proofs = message.proofs.map((e) => base64FromBytes2(e));
7221
- }
7222
- return obj;
7223
- },
7224
- create(base) {
7225
- return SecretProof.fromPartial(base ?? {});
7226
- },
7227
- fromPartial(object) {
7228
- const message = createBaseSecretProof();
7229
- message.proofs = object.proofs?.map((e) => e) || [];
7230
- return message;
7231
- }
7232
- };
7233
7205
  function createBaseLeafRefundTxSigningJob() {
7234
7206
  return { leafId: "", refundTxSigningJob: void 0 };
7235
7207
  }
@@ -7642,7 +7614,6 @@ function createBaseStartTransferRequest() {
7642
7614
  leavesToSend: [],
7643
7615
  receiverIdentityPublicKey: new Uint8Array(0),
7644
7616
  expiryTime: void 0,
7645
- keyTweakProofs: {},
7646
7617
  transferPackage: void 0
7647
7618
  };
7648
7619
  }
@@ -7663,9 +7634,6 @@ var StartTransferRequest = {
7663
7634
  if (message.expiryTime !== void 0) {
7664
7635
  Timestamp.encode(toTimestamp(message.expiryTime), writer.uint32(42).fork()).join();
7665
7636
  }
7666
- Object.entries(message.keyTweakProofs).forEach(([key, value]) => {
7667
- StartTransferRequest_KeyTweakProofsEntry.encode({ key, value }, writer.uint32(50).fork()).join();
7668
- });
7669
7637
  if (message.transferPackage !== void 0) {
7670
7638
  TransferPackage.encode(message.transferPackage, writer.uint32(58).fork()).join();
7671
7639
  }
@@ -7713,16 +7681,6 @@ var StartTransferRequest = {
7713
7681
  message.expiryTime = fromTimestamp(Timestamp.decode(reader, reader.uint32()));
7714
7682
  continue;
7715
7683
  }
7716
- case 6: {
7717
- if (tag !== 50) {
7718
- break;
7719
- }
7720
- const entry6 = StartTransferRequest_KeyTweakProofsEntry.decode(reader, reader.uint32());
7721
- if (entry6.value !== void 0) {
7722
- message.keyTweakProofs[entry6.key] = entry6.value;
7723
- }
7724
- continue;
7725
- }
7726
7684
  case 7: {
7727
7685
  if (tag !== 58) {
7728
7686
  break;
@@ -7745,10 +7703,6 @@ var StartTransferRequest = {
7745
7703
  leavesToSend: globalThis.Array.isArray(object?.leavesToSend) ? object.leavesToSend.map((e) => LeafRefundTxSigningJob.fromJSON(e)) : [],
7746
7704
  receiverIdentityPublicKey: isSet3(object.receiverIdentityPublicKey) ? bytesFromBase642(object.receiverIdentityPublicKey) : new Uint8Array(0),
7747
7705
  expiryTime: isSet3(object.expiryTime) ? fromJsonTimestamp(object.expiryTime) : void 0,
7748
- keyTweakProofs: isObject(object.keyTweakProofs) ? Object.entries(object.keyTweakProofs).reduce((acc, [key, value]) => {
7749
- acc[key] = SecretProof.fromJSON(value);
7750
- return acc;
7751
- }, {}) : {},
7752
7706
  transferPackage: isSet3(object.transferPackage) ? TransferPackage.fromJSON(object.transferPackage) : void 0
7753
7707
  };
7754
7708
  },
@@ -7769,15 +7723,6 @@ var StartTransferRequest = {
7769
7723
  if (message.expiryTime !== void 0) {
7770
7724
  obj.expiryTime = message.expiryTime.toISOString();
7771
7725
  }
7772
- if (message.keyTweakProofs) {
7773
- const entries = Object.entries(message.keyTweakProofs);
7774
- if (entries.length > 0) {
7775
- obj.keyTweakProofs = {};
7776
- entries.forEach(([k, v]) => {
7777
- obj.keyTweakProofs[k] = SecretProof.toJSON(v);
7778
- });
7779
- }
7780
- }
7781
7726
  if (message.transferPackage !== void 0) {
7782
7727
  obj.transferPackage = TransferPackage.toJSON(message.transferPackage);
7783
7728
  }
@@ -7793,87 +7738,10 @@ var StartTransferRequest = {
7793
7738
  message.leavesToSend = object.leavesToSend?.map((e) => LeafRefundTxSigningJob.fromPartial(e)) || [];
7794
7739
  message.receiverIdentityPublicKey = object.receiverIdentityPublicKey ?? new Uint8Array(0);
7795
7740
  message.expiryTime = object.expiryTime ?? void 0;
7796
- message.keyTweakProofs = Object.entries(object.keyTweakProofs ?? {}).reduce(
7797
- (acc, [key, value]) => {
7798
- if (value !== void 0) {
7799
- acc[key] = SecretProof.fromPartial(value);
7800
- }
7801
- return acc;
7802
- },
7803
- {}
7804
- );
7805
7741
  message.transferPackage = object.transferPackage !== void 0 && object.transferPackage !== null ? TransferPackage.fromPartial(object.transferPackage) : void 0;
7806
7742
  return message;
7807
7743
  }
7808
7744
  };
7809
- function createBaseStartTransferRequest_KeyTweakProofsEntry() {
7810
- return { key: "", value: void 0 };
7811
- }
7812
- var StartTransferRequest_KeyTweakProofsEntry = {
7813
- encode(message, writer = new import_wire4.BinaryWriter()) {
7814
- if (message.key !== "") {
7815
- writer.uint32(10).string(message.key);
7816
- }
7817
- if (message.value !== void 0) {
7818
- SecretProof.encode(message.value, writer.uint32(18).fork()).join();
7819
- }
7820
- return writer;
7821
- },
7822
- decode(input, length) {
7823
- const reader = input instanceof import_wire4.BinaryReader ? input : new import_wire4.BinaryReader(input);
7824
- let end = length === void 0 ? reader.len : reader.pos + length;
7825
- const message = createBaseStartTransferRequest_KeyTweakProofsEntry();
7826
- while (reader.pos < end) {
7827
- const tag = reader.uint32();
7828
- switch (tag >>> 3) {
7829
- case 1: {
7830
- if (tag !== 10) {
7831
- break;
7832
- }
7833
- message.key = reader.string();
7834
- continue;
7835
- }
7836
- case 2: {
7837
- if (tag !== 18) {
7838
- break;
7839
- }
7840
- message.value = SecretProof.decode(reader, reader.uint32());
7841
- continue;
7842
- }
7843
- }
7844
- if ((tag & 7) === 4 || tag === 0) {
7845
- break;
7846
- }
7847
- reader.skip(tag & 7);
7848
- }
7849
- return message;
7850
- },
7851
- fromJSON(object) {
7852
- return {
7853
- key: isSet3(object.key) ? globalThis.String(object.key) : "",
7854
- value: isSet3(object.value) ? SecretProof.fromJSON(object.value) : void 0
7855
- };
7856
- },
7857
- toJSON(message) {
7858
- const obj = {};
7859
- if (message.key !== "") {
7860
- obj.key = message.key;
7861
- }
7862
- if (message.value !== void 0) {
7863
- obj.value = SecretProof.toJSON(message.value);
7864
- }
7865
- return obj;
7866
- },
7867
- create(base) {
7868
- return StartTransferRequest_KeyTweakProofsEntry.fromPartial(base ?? {});
7869
- },
7870
- fromPartial(object) {
7871
- const message = createBaseStartTransferRequest_KeyTweakProofsEntry();
7872
- message.key = object.key ?? "";
7873
- message.value = object.value !== void 0 && object.value !== null ? SecretProof.fromPartial(object.value) : void 0;
7874
- return message;
7875
- }
7876
- };
7877
7745
  function createBaseStartTransferResponse() {
7878
7746
  return { transfer: void 0, signingResults: [] };
7879
7747
  }
@@ -8420,12 +8288,7 @@ var FinalizeTransferRequest = {
8420
8288
  }
8421
8289
  };
8422
8290
  function createBaseFinalizeTransferWithTransferPackageRequest() {
8423
- return {
8424
- transferId: "",
8425
- ownerIdentityPublicKey: new Uint8Array(0),
8426
- transferPackage: void 0,
8427
- refundSignatures: {}
8428
- };
8291
+ return { transferId: "", ownerIdentityPublicKey: new Uint8Array(0), transferPackage: void 0 };
8429
8292
  }
8430
8293
  var FinalizeTransferWithTransferPackageRequest = {
8431
8294
  encode(message, writer = new import_wire4.BinaryWriter()) {
@@ -8438,12 +8301,6 @@ var FinalizeTransferWithTransferPackageRequest = {
8438
8301
  if (message.transferPackage !== void 0) {
8439
8302
  TransferPackage.encode(message.transferPackage, writer.uint32(26).fork()).join();
8440
8303
  }
8441
- Object.entries(message.refundSignatures).forEach(([key, value]) => {
8442
- FinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry.encode(
8443
- { key, value },
8444
- writer.uint32(34).fork()
8445
- ).join();
8446
- });
8447
8304
  return writer;
8448
8305
  },
8449
8306
  decode(input, length) {
@@ -8474,19 +8331,6 @@ var FinalizeTransferWithTransferPackageRequest = {
8474
8331
  message.transferPackage = TransferPackage.decode(reader, reader.uint32());
8475
8332
  continue;
8476
8333
  }
8477
- case 4: {
8478
- if (tag !== 34) {
8479
- break;
8480
- }
8481
- const entry4 = FinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry.decode(
8482
- reader,
8483
- reader.uint32()
8484
- );
8485
- if (entry4.value !== void 0) {
8486
- message.refundSignatures[entry4.key] = entry4.value;
8487
- }
8488
- continue;
8489
- }
8490
8334
  }
8491
8335
  if ((tag & 7) === 4 || tag === 0) {
8492
8336
  break;
@@ -8499,11 +8343,7 @@ var FinalizeTransferWithTransferPackageRequest = {
8499
8343
  return {
8500
8344
  transferId: isSet3(object.transferId) ? globalThis.String(object.transferId) : "",
8501
8345
  ownerIdentityPublicKey: isSet3(object.ownerIdentityPublicKey) ? bytesFromBase642(object.ownerIdentityPublicKey) : new Uint8Array(0),
8502
- transferPackage: isSet3(object.transferPackage) ? TransferPackage.fromJSON(object.transferPackage) : void 0,
8503
- refundSignatures: isObject(object.refundSignatures) ? Object.entries(object.refundSignatures).reduce((acc, [key, value]) => {
8504
- acc[key] = bytesFromBase642(value);
8505
- return acc;
8506
- }, {}) : {}
8346
+ transferPackage: isSet3(object.transferPackage) ? TransferPackage.fromJSON(object.transferPackage) : void 0
8507
8347
  };
8508
8348
  },
8509
8349
  toJSON(message) {
@@ -8517,15 +8357,6 @@ var FinalizeTransferWithTransferPackageRequest = {
8517
8357
  if (message.transferPackage !== void 0) {
8518
8358
  obj.transferPackage = TransferPackage.toJSON(message.transferPackage);
8519
8359
  }
8520
- if (message.refundSignatures) {
8521
- const entries = Object.entries(message.refundSignatures);
8522
- if (entries.length > 0) {
8523
- obj.refundSignatures = {};
8524
- entries.forEach(([k, v]) => {
8525
- obj.refundSignatures[k] = base64FromBytes2(v);
8526
- });
8527
- }
8528
- }
8529
8360
  return obj;
8530
8361
  },
8531
8362
  create(base) {
@@ -8536,83 +8367,6 @@ var FinalizeTransferWithTransferPackageRequest = {
8536
8367
  message.transferId = object.transferId ?? "";
8537
8368
  message.ownerIdentityPublicKey = object.ownerIdentityPublicKey ?? new Uint8Array(0);
8538
8369
  message.transferPackage = object.transferPackage !== void 0 && object.transferPackage !== null ? TransferPackage.fromPartial(object.transferPackage) : void 0;
8539
- message.refundSignatures = Object.entries(object.refundSignatures ?? {}).reduce(
8540
- (acc, [key, value]) => {
8541
- if (value !== void 0) {
8542
- acc[key] = value;
8543
- }
8544
- return acc;
8545
- },
8546
- {}
8547
- );
8548
- return message;
8549
- }
8550
- };
8551
- function createBaseFinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry() {
8552
- return { key: "", value: new Uint8Array(0) };
8553
- }
8554
- var FinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry = {
8555
- encode(message, writer = new import_wire4.BinaryWriter()) {
8556
- if (message.key !== "") {
8557
- writer.uint32(10).string(message.key);
8558
- }
8559
- if (message.value.length !== 0) {
8560
- writer.uint32(18).bytes(message.value);
8561
- }
8562
- return writer;
8563
- },
8564
- decode(input, length) {
8565
- const reader = input instanceof import_wire4.BinaryReader ? input : new import_wire4.BinaryReader(input);
8566
- let end = length === void 0 ? reader.len : reader.pos + length;
8567
- const message = createBaseFinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry();
8568
- while (reader.pos < end) {
8569
- const tag = reader.uint32();
8570
- switch (tag >>> 3) {
8571
- case 1: {
8572
- if (tag !== 10) {
8573
- break;
8574
- }
8575
- message.key = reader.string();
8576
- continue;
8577
- }
8578
- case 2: {
8579
- if (tag !== 18) {
8580
- break;
8581
- }
8582
- message.value = reader.bytes();
8583
- continue;
8584
- }
8585
- }
8586
- if ((tag & 7) === 4 || tag === 0) {
8587
- break;
8588
- }
8589
- reader.skip(tag & 7);
8590
- }
8591
- return message;
8592
- },
8593
- fromJSON(object) {
8594
- return {
8595
- key: isSet3(object.key) ? globalThis.String(object.key) : "",
8596
- value: isSet3(object.value) ? bytesFromBase642(object.value) : new Uint8Array(0)
8597
- };
8598
- },
8599
- toJSON(message) {
8600
- const obj = {};
8601
- if (message.key !== "") {
8602
- obj.key = message.key;
8603
- }
8604
- if (message.value.length !== 0) {
8605
- obj.value = base64FromBytes2(message.value);
8606
- }
8607
- return obj;
8608
- },
8609
- create(base) {
8610
- return FinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry.fromPartial(base ?? {});
8611
- },
8612
- fromPartial(object) {
8613
- const message = createBaseFinalizeTransferWithTransferPackageRequest_RefundSignaturesEntry();
8614
- message.key = object.key ?? "";
8615
- message.value = object.value ?? new Uint8Array(0);
8616
8370
  return message;
8617
8371
  }
8618
8372
  };
@@ -13812,13 +13566,16 @@ var QueryBalanceResponse_NodeBalancesEntry = {
13812
13566
  }
13813
13567
  };
13814
13568
  function createBaseSparkAddress() {
13815
- return { identityPublicKey: new Uint8Array(0) };
13569
+ return { identityPublicKey: new Uint8Array(0), paymentIntentFields: void 0 };
13816
13570
  }
13817
13571
  var SparkAddress = {
13818
13572
  encode(message, writer = new import_wire4.BinaryWriter()) {
13819
13573
  if (message.identityPublicKey.length !== 0) {
13820
13574
  writer.uint32(10).bytes(message.identityPublicKey);
13821
13575
  }
13576
+ if (message.paymentIntentFields !== void 0) {
13577
+ PaymentIntentFields.encode(message.paymentIntentFields, writer.uint32(18).fork()).join();
13578
+ }
13822
13579
  return writer;
13823
13580
  },
13824
13581
  decode(input, length) {
@@ -13835,6 +13592,13 @@ var SparkAddress = {
13835
13592
  message.identityPublicKey = reader.bytes();
13836
13593
  continue;
13837
13594
  }
13595
+ case 2: {
13596
+ if (tag !== 18) {
13597
+ break;
13598
+ }
13599
+ message.paymentIntentFields = PaymentIntentFields.decode(reader, reader.uint32());
13600
+ continue;
13601
+ }
13838
13602
  }
13839
13603
  if ((tag & 7) === 4 || tag === 0) {
13840
13604
  break;
@@ -13845,7 +13609,8 @@ var SparkAddress = {
13845
13609
  },
13846
13610
  fromJSON(object) {
13847
13611
  return {
13848
- identityPublicKey: isSet3(object.identityPublicKey) ? bytesFromBase642(object.identityPublicKey) : new Uint8Array(0)
13612
+ identityPublicKey: isSet3(object.identityPublicKey) ? bytesFromBase642(object.identityPublicKey) : new Uint8Array(0),
13613
+ paymentIntentFields: isSet3(object.paymentIntentFields) ? PaymentIntentFields.fromJSON(object.paymentIntentFields) : void 0
13849
13614
  };
13850
13615
  },
13851
13616
  toJSON(message) {
@@ -13853,6 +13618,9 @@ var SparkAddress = {
13853
13618
  if (message.identityPublicKey.length !== 0) {
13854
13619
  obj.identityPublicKey = base64FromBytes2(message.identityPublicKey);
13855
13620
  }
13621
+ if (message.paymentIntentFields !== void 0) {
13622
+ obj.paymentIntentFields = PaymentIntentFields.toJSON(message.paymentIntentFields);
13623
+ }
13856
13624
  return obj;
13857
13625
  },
13858
13626
  create(base) {
@@ -13861,16 +13629,115 @@ var SparkAddress = {
13861
13629
  fromPartial(object) {
13862
13630
  const message = createBaseSparkAddress();
13863
13631
  message.identityPublicKey = object.identityPublicKey ?? new Uint8Array(0);
13632
+ message.paymentIntentFields = object.paymentIntentFields !== void 0 && object.paymentIntentFields !== null ? PaymentIntentFields.fromPartial(object.paymentIntentFields) : void 0;
13864
13633
  return message;
13865
13634
  }
13866
13635
  };
13867
- function createBaseInitiateUtxoSwapRequest() {
13868
- return {
13869
- onChainUtxo: void 0,
13870
- requestType: 0,
13871
- amount: void 0,
13872
- sspSignature: new Uint8Array(0),
13873
- userSignature: new Uint8Array(0),
13636
+ function createBasePaymentIntentFields() {
13637
+ return { id: new Uint8Array(0), assetIdentifier: void 0, assetAmount: new Uint8Array(0), memo: void 0 };
13638
+ }
13639
+ var PaymentIntentFields = {
13640
+ encode(message, writer = new import_wire4.BinaryWriter()) {
13641
+ if (message.id.length !== 0) {
13642
+ writer.uint32(10).bytes(message.id);
13643
+ }
13644
+ if (message.assetIdentifier !== void 0) {
13645
+ writer.uint32(18).bytes(message.assetIdentifier);
13646
+ }
13647
+ if (message.assetAmount.length !== 0) {
13648
+ writer.uint32(26).bytes(message.assetAmount);
13649
+ }
13650
+ if (message.memo !== void 0) {
13651
+ writer.uint32(34).string(message.memo);
13652
+ }
13653
+ return writer;
13654
+ },
13655
+ decode(input, length) {
13656
+ const reader = input instanceof import_wire4.BinaryReader ? input : new import_wire4.BinaryReader(input);
13657
+ let end = length === void 0 ? reader.len : reader.pos + length;
13658
+ const message = createBasePaymentIntentFields();
13659
+ while (reader.pos < end) {
13660
+ const tag = reader.uint32();
13661
+ switch (tag >>> 3) {
13662
+ case 1: {
13663
+ if (tag !== 10) {
13664
+ break;
13665
+ }
13666
+ message.id = reader.bytes();
13667
+ continue;
13668
+ }
13669
+ case 2: {
13670
+ if (tag !== 18) {
13671
+ break;
13672
+ }
13673
+ message.assetIdentifier = reader.bytes();
13674
+ continue;
13675
+ }
13676
+ case 3: {
13677
+ if (tag !== 26) {
13678
+ break;
13679
+ }
13680
+ message.assetAmount = reader.bytes();
13681
+ continue;
13682
+ }
13683
+ case 4: {
13684
+ if (tag !== 34) {
13685
+ break;
13686
+ }
13687
+ message.memo = reader.string();
13688
+ continue;
13689
+ }
13690
+ }
13691
+ if ((tag & 7) === 4 || tag === 0) {
13692
+ break;
13693
+ }
13694
+ reader.skip(tag & 7);
13695
+ }
13696
+ return message;
13697
+ },
13698
+ fromJSON(object) {
13699
+ return {
13700
+ id: isSet3(object.id) ? bytesFromBase642(object.id) : new Uint8Array(0),
13701
+ assetIdentifier: isSet3(object.assetIdentifier) ? bytesFromBase642(object.assetIdentifier) : void 0,
13702
+ assetAmount: isSet3(object.assetAmount) ? bytesFromBase642(object.assetAmount) : new Uint8Array(0),
13703
+ memo: isSet3(object.memo) ? globalThis.String(object.memo) : void 0
13704
+ };
13705
+ },
13706
+ toJSON(message) {
13707
+ const obj = {};
13708
+ if (message.id.length !== 0) {
13709
+ obj.id = base64FromBytes2(message.id);
13710
+ }
13711
+ if (message.assetIdentifier !== void 0) {
13712
+ obj.assetIdentifier = base64FromBytes2(message.assetIdentifier);
13713
+ }
13714
+ if (message.assetAmount.length !== 0) {
13715
+ obj.assetAmount = base64FromBytes2(message.assetAmount);
13716
+ }
13717
+ if (message.memo !== void 0) {
13718
+ obj.memo = message.memo;
13719
+ }
13720
+ return obj;
13721
+ },
13722
+ create(base) {
13723
+ return PaymentIntentFields.fromPartial(base ?? {});
13724
+ },
13725
+ fromPartial(object) {
13726
+ const message = createBasePaymentIntentFields();
13727
+ message.id = object.id ?? new Uint8Array(0);
13728
+ message.assetIdentifier = object.assetIdentifier ?? void 0;
13729
+ message.assetAmount = object.assetAmount ?? new Uint8Array(0);
13730
+ message.memo = object.memo ?? void 0;
13731
+ return message;
13732
+ }
13733
+ };
13734
+ function createBaseInitiateUtxoSwapRequest() {
13735
+ return {
13736
+ onChainUtxo: void 0,
13737
+ requestType: 0,
13738
+ amount: void 0,
13739
+ sspSignature: new Uint8Array(0),
13740
+ userSignature: new Uint8Array(0),
13874
13741
  transfer: void 0,
13875
13742
  spendTxSigningJob: void 0
13876
13743
  };
@@ -15385,13 +15252,13 @@ function isSet3(value) {
15385
15252
  }
15386
15253
 
15387
15254
  // src/utils/network.ts
15388
- var Network2 = /* @__PURE__ */ ((Network3) => {
15389
- Network3[Network3["MAINNET"] = 0] = "MAINNET";
15390
- Network3[Network3["TESTNET"] = 1] = "TESTNET";
15391
- Network3[Network3["SIGNET"] = 2] = "SIGNET";
15392
- Network3[Network3["REGTEST"] = 3] = "REGTEST";
15393
- Network3[Network3["LOCAL"] = 4] = "LOCAL";
15394
- return Network3;
15255
+ var Network2 = /* @__PURE__ */ ((Network4) => {
15256
+ Network4[Network4["MAINNET"] = 0] = "MAINNET";
15257
+ Network4[Network4["TESTNET"] = 1] = "TESTNET";
15258
+ Network4[Network4["SIGNET"] = 2] = "SIGNET";
15259
+ Network4[Network4["REGTEST"] = 3] = "REGTEST";
15260
+ Network4[Network4["LOCAL"] = 4] = "LOCAL";
15261
+ return Network4;
15395
15262
  })(Network2 || {});
15396
15263
  var NetworkToProto = {
15397
15264
  [0 /* MAINNET */]: 1 /* MAINNET */,
@@ -15415,11 +15282,11 @@ var LRC_WALLET_NETWORK = Object.freeze({
15415
15282
  [4 /* LOCAL */]: bitcoin.networks.regtest
15416
15283
  });
15417
15284
  var LRC_WALLET_NETWORK_TYPE = Object.freeze({
15418
- [0 /* MAINNET */]: import_lrc20_sdk4.NetworkType.MAINNET,
15419
- [1 /* TESTNET */]: import_lrc20_sdk4.NetworkType.TESTNET,
15420
- [2 /* SIGNET */]: import_lrc20_sdk4.NetworkType.TESTNET,
15421
- [3 /* REGTEST */]: import_lrc20_sdk4.NetworkType.REGTEST,
15422
- [4 /* LOCAL */]: import_lrc20_sdk4.NetworkType.LOCAL
15285
+ [0 /* MAINNET */]: import_lrc20_sdk2.NetworkType.MAINNET,
15286
+ [1 /* TESTNET */]: import_lrc20_sdk2.NetworkType.TESTNET,
15287
+ [2 /* SIGNET */]: import_lrc20_sdk2.NetworkType.TESTNET,
15288
+ [3 /* REGTEST */]: import_lrc20_sdk2.NetworkType.REGTEST,
15289
+ [4 /* LOCAL */]: import_lrc20_sdk2.NetworkType.LOCAL
15423
15290
  });
15424
15291
 
15425
15292
  // src/services/wallet-config.ts
@@ -15557,6 +15424,7 @@ var BASE_CONFIG = {
15557
15424
  threshold: 2,
15558
15425
  signingOperators: getLocalSigningOperators(),
15559
15426
  tokenSignatures: "SCHNORR",
15427
+ tokenTransactionVersion: "V0",
15560
15428
  electrsUrl: getElectrsUrl("LOCAL"),
15561
15429
  expectedWithdrawBondSats: 1e4,
15562
15430
  expectedWithdrawRelativeBlockLocktime: 1e3,
@@ -15764,6 +15632,9 @@ var WalletConfigService = class {
15764
15632
  getTokenSignatures() {
15765
15633
  return this.config.tokenSignatures;
15766
15634
  }
15635
+ getTokenTransactionVersion() {
15636
+ return this.config.tokenTransactionVersion;
15637
+ }
15767
15638
  getElectrsUrl() {
15768
15639
  return this.config.electrsUrl;
15769
15640
  }
@@ -15774,31 +15645,9 @@ var WalletConfigService = class {
15774
15645
 
15775
15646
  // src/services/token-transactions.ts
15776
15647
  init_buffer();
15777
- var import_utils7 = require("@noble/curves/abstract/utils");
15648
+ var import_utils9 = require("@noble/curves/abstract/utils");
15778
15649
  var import_secp256k19 = require("@noble/curves/secp256k1");
15779
15650
 
15780
- // src/utils/response-validation.ts
15781
- init_buffer();
15782
- function collectResponses(responses) {
15783
- const successfulResponses = responses.filter(
15784
- (result) => result.status === "fulfilled"
15785
- ).map((result) => result.value);
15786
- const failedResponses = responses.filter(
15787
- (result) => result.status === "rejected"
15788
- );
15789
- if (failedResponses.length > 0) {
15790
- const errors = failedResponses.map((result) => result.reason).join("\n");
15791
- throw new NetworkError(
15792
- `${failedResponses.length} out of ${responses.length} requests failed, please try again`,
15793
- {
15794
- errorCount: failedResponses.length,
15795
- errors
15796
- }
15797
- );
15798
- }
15799
- return successfulResponses;
15800
- }
15801
-
15802
15651
  // src/utils/token-hashing.ts
15803
15652
  init_buffer();
15804
15653
  var import_sha22 = require("@noble/hashes/sha2");
@@ -16099,35 +15948,17 @@ function hashOperatorSpecificTokenTransactionSignablePayload(payload) {
16099
15948
  return finalHashObj.digest();
16100
15949
  }
16101
15950
 
16102
- // src/utils/token-keyshares.ts
16103
- init_buffer();
16104
- var import_secp256k17 = require("@noble/curves/secp256k1");
16105
- function recoverRevocationSecretFromKeyshares(keyshares, threshold) {
16106
- const shares = keyshares.map((keyshare) => ({
16107
- fieldModulus: BigInt("0x" + import_secp256k17.secp256k1.CURVE.n.toString(16)),
16108
- // secp256k1 curve order
16109
- threshold,
16110
- index: BigInt(keyshare.operatorIndex),
16111
- share: BigInt(
16112
- "0x" + import_buffer.Buffer.from(keyshare.keyshare.keyshare).toString("hex")
16113
- ),
16114
- proofs: []
16115
- }));
16116
- const recoveredSecret = recoverSecret(shares);
16117
- return bigIntToPrivateKey(recoveredSecret);
16118
- }
16119
-
16120
15951
  // src/utils/token-transactions.ts
16121
15952
  init_buffer();
16122
- var import_utils5 = require("@noble/curves/abstract/utils");
15953
+ var import_utils6 = require("@noble/curves/abstract/utils");
16123
15954
  function calculateAvailableTokenAmount(outputLeaves) {
16124
15955
  return outputLeaves.reduce(
16125
- (sum, output) => sum + BigInt((0, import_utils5.bytesToNumberBE)(output.output.tokenAmount)),
15956
+ (sum, output) => sum + BigInt((0, import_utils6.bytesToNumberBE)(output.output.tokenAmount)),
16126
15957
  BigInt(0)
16127
15958
  );
16128
15959
  }
16129
15960
  function checkIfSelectedOutputsAreAvailable(selectedOutputs, tokenOutputs, tokenPublicKey) {
16130
- const tokenPubKeyHex = (0, import_utils5.bytesToHex)(tokenPublicKey);
15961
+ const tokenPubKeyHex = (0, import_utils6.bytesToHex)(tokenPublicKey);
16131
15962
  const tokenOutputsAvailable = tokenOutputs.get(tokenPubKeyHex);
16132
15963
  if (!tokenOutputsAvailable) {
16133
15964
  return false;
@@ -16403,16 +16234,18 @@ function validateTokenTransaction(finalTokenTransaction, partialTokenTransaction
16403
16234
  }
16404
16235
 
16405
16236
  // src/services/token-transactions.ts
16406
- var import_utils8 = require("@noble/hashes/utils");
16237
+ var import_utils10 = require("@noble/hashes/utils");
16407
16238
 
16408
16239
  // src/address/index.ts
16409
16240
  init_buffer();
16410
16241
 
16411
16242
  // src/address/address.ts
16412
16243
  init_buffer();
16413
- var import_secp256k18 = require("@noble/curves/secp256k1");
16414
- var import_utils6 = require("@noble/hashes/utils");
16244
+ var import_secp256k17 = require("@noble/curves/secp256k1");
16245
+ var import_utils7 = require("@noble/hashes/utils");
16415
16246
  var import_base2 = require("@scure/base");
16247
+ var import_uuidv7 = require("uuidv7");
16248
+ var import_utils8 = require("@noble/curves/abstract/utils");
16416
16249
  var AddressNetwork = {
16417
16250
  MAINNET: "sp",
16418
16251
  TESTNET: "spt",
@@ -16422,7 +16255,7 @@ var AddressNetwork = {
16422
16255
  };
16423
16256
  function decodeSparkAddress(address2, network) {
16424
16257
  try {
16425
- const decoded = import_base2.bech32m.decode(address2, 200);
16258
+ const decoded = import_base2.bech32m.decode(address2, 500);
16426
16259
  if (decoded.prefix !== AddressNetwork[network]) {
16427
16260
  throw new ValidationError("Invalid Spark address prefix", {
16428
16261
  field: "address",
@@ -16431,9 +16264,19 @@ function decodeSparkAddress(address2, network) {
16431
16264
  });
16432
16265
  }
16433
16266
  const payload = SparkAddress.decode(import_base2.bech32m.fromWords(decoded.words));
16434
- const publicKey = (0, import_utils6.bytesToHex)(payload.identityPublicKey);
16267
+ const publicKey = (0, import_utils7.bytesToHex)(payload.identityPublicKey);
16435
16268
  isValidPublicKey(publicKey);
16436
- return publicKey;
16269
+ const paymentIntentFields = payload.paymentIntentFields;
16270
+ return {
16271
+ identityPublicKey: publicKey,
16272
+ network,
16273
+ paymentIntentFields: paymentIntentFields && {
16274
+ id: import_uuidv7.UUID.ofInner(paymentIntentFields.id).toString(),
16275
+ assetIdentifier: paymentIntentFields.assetIdentifier ? (0, import_utils7.bytesToHex)(paymentIntentFields.assetIdentifier) : void 0,
16276
+ assetAmount: (0, import_utils8.bytesToNumberBE)(paymentIntentFields.assetAmount),
16277
+ memo: paymentIntentFields.memo
16278
+ }
16279
+ };
16437
16280
  } catch (error) {
16438
16281
  if (error instanceof ValidationError) {
16439
16282
  throw error;
@@ -16450,7 +16293,7 @@ function decodeSparkAddress(address2, network) {
16450
16293
  }
16451
16294
  function isValidPublicKey(publicKey) {
16452
16295
  try {
16453
- const point = import_secp256k18.secp256k1.ProjectivePoint.fromHex(publicKey);
16296
+ const point = import_secp256k17.secp256k1.ProjectivePoint.fromHex(publicKey);
16454
16297
  point.assertValidity();
16455
16298
  } catch (error) {
16456
16299
  throw new ValidationError(
@@ -16464,8 +16307,48 @@ function isValidPublicKey(publicKey) {
16464
16307
  }
16465
16308
  }
16466
16309
 
16310
+ // src/utils/response-validation.ts
16311
+ init_buffer();
16312
+ function collectResponses(responses) {
16313
+ const successfulResponses = responses.filter(
16314
+ (result) => result.status === "fulfilled"
16315
+ ).map((result) => result.value);
16316
+ const failedResponses = responses.filter(
16317
+ (result) => result.status === "rejected"
16318
+ );
16319
+ if (failedResponses.length > 0) {
16320
+ const errors = failedResponses.map((result) => result.reason).join("\n");
16321
+ throw new NetworkError(
16322
+ `${failedResponses.length} out of ${responses.length} requests failed, please try again`,
16323
+ {
16324
+ errorCount: failedResponses.length,
16325
+ errors
16326
+ }
16327
+ );
16328
+ }
16329
+ return successfulResponses;
16330
+ }
16331
+
16332
+ // src/utils/token-keyshares.ts
16333
+ init_buffer();
16334
+ var import_secp256k18 = require("@noble/curves/secp256k1");
16335
+ function recoverRevocationSecretFromKeyshares(keyshares, threshold) {
16336
+ const shares = keyshares.map((keyshare) => ({
16337
+ fieldModulus: BigInt("0x" + import_secp256k18.secp256k1.CURVE.n.toString(16)),
16338
+ // secp256k1 curve order
16339
+ threshold,
16340
+ index: BigInt(keyshare.operatorIndex),
16341
+ share: BigInt(
16342
+ "0x" + import_buffer.Buffer.from(keyshare.keyshare.keyshare).toString("hex")
16343
+ ),
16344
+ proofs: []
16345
+ }));
16346
+ const recoveredSecret = recoverSecret(shares);
16347
+ return bigIntToPrivateKey(recoveredSecret);
16348
+ }
16349
+
16467
16350
  // src/services/token-transactions.ts
16468
- var MAX_TOKEN_OUTPUTS = 1e3;
16351
+ var MAX_TOKEN_OUTPUTS = 500;
16469
16352
  var TokenTransactionService = class {
16470
16353
  config;
16471
16354
  connectionManager;
@@ -16491,7 +16374,7 @@ var TokenTransactionService = class {
16491
16374
  if (!checkIfSelectedOutputsAreAvailable(
16492
16375
  outputsToUse,
16493
16376
  tokenOutputs,
16494
- (0, import_utils8.hexToBytes)(receiverOutputs[0].tokenPublicKey)
16377
+ (0, import_utils10.hexToBytes)(receiverOutputs[0].tokenPublicKey)
16495
16378
  )) {
16496
16379
  throw new ValidationError(
16497
16380
  "One or more selected TTXOs are not available",
@@ -16532,15 +16415,23 @@ var TokenTransactionService = class {
16532
16415
  this.config.getNetworkType()
16533
16416
  );
16534
16417
  return {
16535
- receiverSparkAddress: (0, import_utils8.hexToBytes)(receiverAddress),
16536
- tokenPublicKey: (0, import_utils8.hexToBytes)(transfer.tokenPublicKey),
16418
+ receiverSparkAddress: (0, import_utils10.hexToBytes)(receiverAddress.identityPublicKey),
16419
+ tokenPublicKey: (0, import_utils10.hexToBytes)(transfer.tokenPublicKey),
16537
16420
  tokenAmount: transfer.tokenAmount
16538
16421
  };
16539
16422
  });
16540
- const tokenTransaction = await this.constructTransferTokenTransaction(
16541
- outputsToUse,
16542
- tokenOutputData
16543
- );
16423
+ let tokenTransaction;
16424
+ if (this.config.getTokenTransactionVersion() === "V0") {
16425
+ tokenTransaction = await this.constructTransferTokenTransactionV0(
16426
+ outputsToUse,
16427
+ tokenOutputData
16428
+ );
16429
+ } else {
16430
+ tokenTransaction = await this.constructTransferTokenTransaction(
16431
+ outputsToUse,
16432
+ tokenOutputData
16433
+ );
16434
+ }
16544
16435
  const txId = await this.broadcastTokenTransaction(
16545
16436
  tokenTransaction,
16546
16437
  outputsToUse.map((output) => output.output.ownerPublicKey),
@@ -16548,7 +16439,7 @@ var TokenTransactionService = class {
16548
16439
  );
16549
16440
  return txId;
16550
16441
  }
16551
- async constructTransferTokenTransaction(selectedOutputs, tokenOutputData) {
16442
+ async constructTransferTokenTransactionV0(selectedOutputs, tokenOutputData) {
16552
16443
  const availableTokenAmount = calculateAvailableTokenAmount(selectedOutputs);
16553
16444
  const totalRequestedAmount = tokenOutputData.reduce(
16554
16445
  (sum, output) => sum + output.tokenAmount,
@@ -16557,7 +16448,7 @@ var TokenTransactionService = class {
16557
16448
  const tokenOutputs = tokenOutputData.map((output) => ({
16558
16449
  ownerPublicKey: output.receiverSparkAddress,
16559
16450
  tokenPublicKey: output.tokenPublicKey,
16560
- tokenAmount: (0, import_utils7.numberToBytesBE)(output.tokenAmount, 16)
16451
+ tokenAmount: (0, import_utils9.numberToBytesBE)(output.tokenAmount, 16)
16561
16452
  }));
16562
16453
  if (availableTokenAmount > totalRequestedAmount) {
16563
16454
  const changeAmount = availableTokenAmount - totalRequestedAmount;
@@ -16565,7 +16456,7 @@ var TokenTransactionService = class {
16565
16456
  tokenOutputs.push({
16566
16457
  ownerPublicKey: await this.config.signer.getIdentityPublicKey(),
16567
16458
  tokenPublicKey: firstTokenPublicKey,
16568
- tokenAmount: (0, import_utils7.numberToBytesBE)(changeAmount, 16)
16459
+ tokenAmount: (0, import_utils9.numberToBytesBE)(changeAmount, 16)
16569
16460
  });
16570
16461
  }
16571
16462
  return {
@@ -16583,24 +16474,78 @@ var TokenTransactionService = class {
16583
16474
  sparkOperatorIdentityPublicKeys: this.collectOperatorIdentityPublicKeys()
16584
16475
  };
16585
16476
  }
16477
+ async constructTransferTokenTransaction(selectedOutputs, tokenOutputData) {
16478
+ const availableTokenAmount = calculateAvailableTokenAmount(selectedOutputs);
16479
+ const totalRequestedAmount = tokenOutputData.reduce(
16480
+ (sum, output) => sum + output.tokenAmount,
16481
+ 0n
16482
+ );
16483
+ const tokenOutputs = tokenOutputData.map((output) => ({
16484
+ ownerPublicKey: output.receiverSparkAddress,
16485
+ tokenPublicKey: output.tokenPublicKey,
16486
+ tokenAmount: (0, import_utils9.numberToBytesBE)(output.tokenAmount, 16)
16487
+ }));
16488
+ if (availableTokenAmount > totalRequestedAmount) {
16489
+ const changeAmount = availableTokenAmount - totalRequestedAmount;
16490
+ const firstTokenPublicKey = tokenOutputData[0].tokenPublicKey;
16491
+ tokenOutputs.push({
16492
+ ownerPublicKey: await this.config.signer.getIdentityPublicKey(),
16493
+ tokenPublicKey: firstTokenPublicKey,
16494
+ tokenAmount: (0, import_utils9.numberToBytesBE)(changeAmount, 16)
16495
+ });
16496
+ }
16497
+ return {
16498
+ version: 1,
16499
+ network: this.config.getNetworkProto(),
16500
+ tokenInputs: {
16501
+ $case: "transferInput",
16502
+ transferInput: {
16503
+ outputsToSpend: selectedOutputs.map((output) => ({
16504
+ prevTokenTransactionHash: output.previousTransactionHash,
16505
+ prevTokenTransactionVout: output.previousTransactionVout
16506
+ }))
16507
+ }
16508
+ },
16509
+ tokenOutputs,
16510
+ sparkOperatorIdentityPublicKeys: this.collectOperatorIdentityPublicKeys(),
16511
+ expiryTime: void 0
16512
+ };
16513
+ }
16586
16514
  collectOperatorIdentityPublicKeys() {
16587
16515
  const operatorKeys = [];
16588
16516
  for (const [_, operator] of Object.entries(
16589
16517
  this.config.getSigningOperators()
16590
16518
  )) {
16591
- operatorKeys.push((0, import_utils8.hexToBytes)(operator.identityPublicKey));
16519
+ operatorKeys.push((0, import_utils10.hexToBytes)(operator.identityPublicKey));
16592
16520
  }
16593
16521
  return operatorKeys;
16594
16522
  }
16595
16523
  async broadcastTokenTransaction(tokenTransaction, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16596
16524
  const signingOperators = this.config.getSigningOperators();
16597
- const { finalTokenTransaction, finalTokenTransactionHash, threshold } = await this.startTokenTransaction(
16525
+ if (!isTokenTransaction(tokenTransaction)) {
16526
+ return this.broadcastTokenTransactionV0(
16527
+ tokenTransaction,
16528
+ signingOperators,
16529
+ outputsToSpendSigningPublicKeys,
16530
+ outputsToSpendCommitments
16531
+ );
16532
+ } else {
16533
+ return this.broadcastTokenTransactionV1(
16534
+ tokenTransaction,
16535
+ signingOperators,
16536
+ outputsToSpendSigningPublicKeys,
16537
+ outputsToSpendCommitments
16538
+ );
16539
+ }
16540
+ }
16541
+ async broadcastTokenTransactionV0(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16542
+ const { finalTokenTransaction, finalTokenTransactionHash, threshold } = await this.startTokenTransactionV0(
16598
16543
  tokenTransaction,
16599
16544
  signingOperators,
16600
16545
  outputsToSpendSigningPublicKeys,
16601
16546
  outputsToSpendCommitments
16602
16547
  );
16603
- const { successfulSignatures } = await this.signTokenTransaction(
16548
+ const { successfulSignatures } = await this.signTokenTransactionV0(
16604
16549
  finalTokenTransaction,
16605
16550
  finalTokenTransactionHash,
16606
16551
  signingOperators
@@ -16655,7 +16600,7 @@ var TokenTransactionService = class {
16655
16600
  {
16656
16601
  field: "revocationCommitment",
16657
16602
  value: derivedRevocationCommitment,
16658
- expected: (0, import_utils7.bytesToHex)(outputsToSpendCommitments[outputIndex]),
16603
+ expected: (0, import_utils9.bytesToHex)(outputsToSpendCommitments[outputIndex]),
16659
16604
  outputIndex
16660
16605
  }
16661
16606
  )
@@ -16681,9 +16626,23 @@ var TokenTransactionService = class {
16681
16626
  threshold
16682
16627
  );
16683
16628
  }
16684
- return (0, import_utils7.bytesToHex)(finalTokenTransactionHash);
16629
+ return (0, import_utils9.bytesToHex)(finalTokenTransactionHash);
16685
16630
  }
16686
- async startTokenTransaction(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16631
+ async broadcastTokenTransactionV1(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16632
+ const { finalTokenTransaction, finalTokenTransactionHash, threshold } = await this.startTokenTransaction(
16633
+ tokenTransaction,
16634
+ signingOperators,
16635
+ outputsToSpendSigningPublicKeys,
16636
+ outputsToSpendCommitments
16637
+ );
16638
+ await this.signTokenTransaction(
16639
+ finalTokenTransaction,
16640
+ finalTokenTransactionHash,
16641
+ signingOperators
16642
+ );
16643
+ return (0, import_utils9.bytesToHex)(finalTokenTransactionHash);
16644
+ }
16645
+ async startTokenTransactionV0(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16687
16646
  const sparkClient = await this.connectionManager.createSparkClient(
16688
16647
  this.config.getCoordinatorAddress()
16689
16648
  );
@@ -16778,49 +16737,141 @@ var TokenTransactionService = class {
16778
16737
  threshold: startResponse.keyshareInfo.threshold
16779
16738
  };
16780
16739
  }
16781
- async signTokenTransaction(finalTokenTransaction, finalTokenTransactionHash, signingOperators) {
16782
- const soSignatures = await Promise.allSettled(
16783
- Object.entries(signingOperators).map(
16784
- async ([identifier, operator], index) => {
16785
- const internalSparkClient = await this.connectionManager.createSparkClient(operator.address);
16786
- const identityPublicKey = await this.config.signer.getIdentityPublicKey();
16787
- const payload = {
16788
- finalTokenTransactionHash,
16789
- operatorIdentityPublicKey: (0, import_utils8.hexToBytes)(operator.identityPublicKey)
16790
- };
16791
- const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
16792
- let operatorSpecificSignatures = [];
16793
- if (finalTokenTransaction.tokenInputs.$case === "mintInput") {
16794
- const issuerPublicKey = finalTokenTransaction.tokenInputs.mintInput.issuerPublicKey;
16795
- if (!issuerPublicKey) {
16796
- throw new ValidationError("Invalid mint input", {
16797
- field: "issuerPublicKey",
16798
- value: null,
16799
- expected: "Non-null issuer public key"
16800
- });
16801
- }
16802
- const ownerSignature = await this.signMessageWithKey(
16803
- payloadHash,
16804
- issuerPublicKey
16805
- );
16806
- operatorSpecificSignatures.push({
16807
- ownerSignature: {
16808
- signature: ownerSignature,
16809
- inputIndex: 0
16810
- },
16811
- payload
16812
- });
16813
- }
16814
- if (finalTokenTransaction.tokenInputs.$case === "transferInput") {
16815
- const transferInput = finalTokenTransaction.tokenInputs.transferInput;
16816
- for (let i = 0; i < transferInput.outputsToSpend.length; i++) {
16817
- let ownerSignature;
16818
- if (this.config.getTokenSignatures() === "SCHNORR") {
16819
- ownerSignature = await this.config.signer.signSchnorrWithIdentityKey(
16820
- payloadHash
16821
- );
16822
- } else {
16823
- ownerSignature = await this.config.signer.signMessageWithIdentityKey(
16740
+ async startTokenTransaction(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16741
+ const sparkClient = await this.connectionManager.createSparkTokenClient(
16742
+ this.config.getCoordinatorAddress()
16743
+ );
16744
+ const partialTokenTransactionHash = hashTokenTransaction(
16745
+ tokenTransaction,
16746
+ true
16747
+ );
16748
+ const ownerSignaturesWithIndex = [];
16749
+ if (tokenTransaction.tokenInputs.$case === "mintInput") {
16750
+ const issuerPublicKey = tokenTransaction.tokenInputs.mintInput.issuerPublicKey;
16751
+ if (!issuerPublicKey) {
16752
+ throw new ValidationError("Invalid mint input", {
16753
+ field: "issuerPublicKey",
16754
+ value: null,
16755
+ expected: "Non-null issuer public key"
16756
+ });
16757
+ }
16758
+ const ownerSignature = await this.signMessageWithKey(
16759
+ partialTokenTransactionHash,
16760
+ issuerPublicKey
16761
+ );
16762
+ ownerSignaturesWithIndex.push({
16763
+ signature: ownerSignature,
16764
+ inputIndex: 0
16765
+ });
16766
+ } else if (tokenTransaction.tokenInputs.$case === "transferInput") {
16767
+ if (!outputsToSpendSigningPublicKeys || !outputsToSpendCommitments) {
16768
+ throw new ValidationError("Invalid transfer input", {
16769
+ field: "outputsToSpend",
16770
+ value: {
16771
+ signingPublicKeys: outputsToSpendSigningPublicKeys,
16772
+ revocationPublicKeys: outputsToSpendCommitments
16773
+ },
16774
+ expected: "Non-null signing and revocation public keys"
16775
+ });
16776
+ }
16777
+ for (const [i, key] of outputsToSpendSigningPublicKeys.entries()) {
16778
+ if (!key) {
16779
+ throw new ValidationError("Invalid signing key", {
16780
+ field: "outputsToSpendSigningPublicKeys",
16781
+ value: i,
16782
+ expected: "Non-null signing key"
16783
+ });
16784
+ }
16785
+ const ownerSignature = await this.signMessageWithKey(
16786
+ partialTokenTransactionHash,
16787
+ key
16788
+ );
16789
+ ownerSignaturesWithIndex.push({
16790
+ signature: ownerSignature,
16791
+ inputIndex: i
16792
+ });
16793
+ }
16794
+ }
16795
+ const startResponse = await sparkClient.start_transaction(
16796
+ {
16797
+ identityPublicKey: await this.config.signer.getIdentityPublicKey(),
16798
+ partialTokenTransaction: tokenTransaction
16799
+ },
16800
+ {
16801
+ retry: true,
16802
+ retryableStatuses: ["UNKNOWN", "UNAVAILABLE", "CANCELLED", "INTERNAL"],
16803
+ retryMaxAttempts: 3
16804
+ }
16805
+ );
16806
+ if (!startResponse.finalTokenTransaction) {
16807
+ throw new Error("Final token transaction missing in start response");
16808
+ }
16809
+ if (!startResponse.keyshareInfo) {
16810
+ throw new Error("Keyshare info missing in start response");
16811
+ }
16812
+ validateTokenTransaction(
16813
+ startResponse.finalTokenTransaction,
16814
+ tokenTransaction,
16815
+ signingOperators,
16816
+ startResponse.keyshareInfo,
16817
+ this.config.getExpectedWithdrawBondSats(),
16818
+ this.config.getExpectedWithdrawRelativeBlockLocktime(),
16819
+ this.config.getThreshold()
16820
+ );
16821
+ const finalTokenTransaction = startResponse.finalTokenTransaction;
16822
+ const finalTokenTransactionHash = hashTokenTransaction(
16823
+ finalTokenTransaction,
16824
+ false
16825
+ );
16826
+ return {
16827
+ finalTokenTransaction,
16828
+ finalTokenTransactionHash,
16829
+ threshold: startResponse.keyshareInfo.threshold
16830
+ };
16831
+ }
16832
+ async signTokenTransactionV0(finalTokenTransaction, finalTokenTransactionHash, signingOperators) {
16833
+ const soSignatures = await Promise.allSettled(
16834
+ Object.entries(signingOperators).map(
16835
+ async ([identifier, operator], index) => {
16836
+ const internalSparkClient = await this.connectionManager.createSparkClient(operator.address);
16837
+ const identityPublicKey = await this.config.signer.getIdentityPublicKey();
16838
+ const payload = {
16839
+ finalTokenTransactionHash,
16840
+ operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
16841
+ };
16842
+ const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
16843
+ let operatorSpecificSignatures = [];
16844
+ if (finalTokenTransaction.tokenInputs.$case === "mintInput") {
16845
+ const issuerPublicKey = finalTokenTransaction.tokenInputs.mintInput.issuerPublicKey;
16846
+ if (!issuerPublicKey) {
16847
+ throw new ValidationError("Invalid mint input", {
16848
+ field: "issuerPublicKey",
16849
+ value: null,
16850
+ expected: "Non-null issuer public key"
16851
+ });
16852
+ }
16853
+ const ownerSignature = await this.signMessageWithKey(
16854
+ payloadHash,
16855
+ issuerPublicKey
16856
+ );
16857
+ operatorSpecificSignatures.push({
16858
+ ownerSignature: {
16859
+ signature: ownerSignature,
16860
+ inputIndex: 0
16861
+ },
16862
+ payload
16863
+ });
16864
+ }
16865
+ if (finalTokenTransaction.tokenInputs.$case === "transferInput") {
16866
+ const transferInput = finalTokenTransaction.tokenInputs.transferInput;
16867
+ for (let i = 0; i < transferInput.outputsToSpend.length; i++) {
16868
+ let ownerSignature;
16869
+ if (this.config.getTokenSignatures() === "SCHNORR") {
16870
+ ownerSignature = await this.config.signer.signSchnorrWithIdentityKey(
16871
+ payloadHash
16872
+ );
16873
+ } else {
16874
+ ownerSignature = await this.config.signer.signMessageWithIdentityKey(
16824
16875
  payloadHash
16825
16876
  );
16826
16877
  }
@@ -16875,49 +16926,44 @@ var TokenTransactionService = class {
16875
16926
  successfulSignatures
16876
16927
  };
16877
16928
  }
16878
- async finalizeTokenTransaction(finalTokenTransaction, revocationSecrets, threshold) {
16879
- const signingOperators = this.config.getSigningOperators();
16880
- const soResponses = await Promise.allSettled(
16881
- Object.entries(signingOperators).map(async ([identifier, operator]) => {
16882
- const internalSparkClient = await this.connectionManager.createSparkClient(operator.address);
16883
- const identityPublicKey = await this.config.signer.getIdentityPublicKey();
16884
- try {
16885
- const response = await internalSparkClient.finalize_token_transaction(
16886
- {
16887
- finalTokenTransaction,
16888
- revocationSecrets,
16889
- identityPublicKey
16890
- },
16891
- {
16892
- retry: true,
16893
- retryableStatuses: [
16894
- "UNKNOWN",
16895
- "UNAVAILABLE",
16896
- "CANCELLED",
16897
- "INTERNAL"
16898
- ],
16899
- retryMaxAttempts: 3
16900
- }
16901
- );
16902
- return {
16903
- identifier,
16904
- response
16905
- };
16906
- } catch (error) {
16907
- throw new NetworkError(
16908
- "Failed to finalize token transaction",
16909
- {
16910
- operation: "finalize_token_transaction",
16911
- errorCount: 1,
16912
- errors: error instanceof Error ? error.message : String(error)
16913
- },
16914
- error
16915
- );
16916
- }
16917
- })
16929
+ async signTokenTransaction(finalTokenTransaction, finalTokenTransactionHash, signingOperators) {
16930
+ const coordinatorClient = await this.connectionManager.createSparkTokenClient(
16931
+ this.config.getCoordinatorAddress()
16918
16932
  );
16919
- collectResponses(soResponses);
16920
- return finalTokenTransaction;
16933
+ const inputTtxoSignaturesPerOperator = await this.createSignaturesForOperators(
16934
+ finalTokenTransaction,
16935
+ finalTokenTransactionHash,
16936
+ signingOperators
16937
+ );
16938
+ try {
16939
+ await coordinatorClient.commit_transaction(
16940
+ {
16941
+ finalTokenTransaction,
16942
+ inputTtxoSignaturesPerOperator,
16943
+ ownerIdentityPublicKey: await this.config.signer.getIdentityPublicKey()
16944
+ },
16945
+ {
16946
+ retry: true,
16947
+ retryableStatuses: [
16948
+ "UNKNOWN",
16949
+ "UNAVAILABLE",
16950
+ "CANCELLED",
16951
+ "INTERNAL"
16952
+ ],
16953
+ retryMaxAttempts: 3
16954
+ }
16955
+ );
16956
+ } catch (error) {
16957
+ throw new NetworkError(
16958
+ "Failed to sign token transaction",
16959
+ {
16960
+ operation: "sign_token_transaction",
16961
+ errorCount: 1,
16962
+ errors: error instanceof Error ? error.message : String(error)
16963
+ },
16964
+ error
16965
+ );
16966
+ }
16921
16967
  }
16922
16968
  async fetchOwnedTokenOutputs(ownerPublicKeys, tokenPublicKeys) {
16923
16969
  const sparkClient = await this.connectionManager.createSparkClient(
@@ -16948,7 +16994,7 @@ var TokenTransactionService = class {
16948
16994
  []
16949
16995
  );
16950
16996
  unsortedTokenOutputs.forEach((output) => {
16951
- const tokenKey = (0, import_utils7.bytesToHex)(output.output.tokenPublicKey);
16997
+ const tokenKey = (0, import_utils9.bytesToHex)(output.output.tokenPublicKey);
16952
16998
  const index = output.previousTransactionVout;
16953
16999
  tokenOutputs.set(tokenKey, [
16954
17000
  { ...output, previousTransactionVout: index }
@@ -16964,7 +17010,7 @@ var TokenTransactionService = class {
16964
17010
  });
16965
17011
  }
16966
17012
  const exactMatch = tokenOutputs.find(
16967
- (item) => (0, import_utils7.bytesToNumberBE)(item.output.tokenAmount) === tokenAmount
17013
+ (item) => (0, import_utils9.bytesToNumberBE)(item.output.tokenAmount) === tokenAmount
16968
17014
  );
16969
17015
  if (exactMatch) {
16970
17016
  return [exactMatch];
@@ -16975,7 +17021,7 @@ var TokenTransactionService = class {
16975
17021
  for (const outputWithPreviousTransactionData of tokenOutputs) {
16976
17022
  if (remainingAmount <= 0n) break;
16977
17023
  selectedOutputs.push(outputWithPreviousTransactionData);
16978
- remainingAmount -= (0, import_utils7.bytesToNumberBE)(
17024
+ remainingAmount -= (0, import_utils9.bytesToNumberBE)(
16979
17025
  outputWithPreviousTransactionData.output.tokenAmount
16980
17026
  );
16981
17027
  }
@@ -16991,13 +17037,13 @@ var TokenTransactionService = class {
16991
17037
  if (strategy === "SMALL_FIRST") {
16992
17038
  tokenOutputs.sort((a, b) => {
16993
17039
  return Number(
16994
- (0, import_utils7.bytesToNumberBE)(a.output.tokenAmount) - (0, import_utils7.bytesToNumberBE)(b.output.tokenAmount)
17040
+ (0, import_utils9.bytesToNumberBE)(a.output.tokenAmount) - (0, import_utils9.bytesToNumberBE)(b.output.tokenAmount)
16995
17041
  );
16996
17042
  });
16997
17043
  } else {
16998
17044
  tokenOutputs.sort((a, b) => {
16999
17045
  return Number(
17000
- (0, import_utils7.bytesToNumberBE)(b.output.tokenAmount) - (0, import_utils7.bytesToNumberBE)(a.output.tokenAmount)
17046
+ (0, import_utils9.bytesToNumberBE)(b.output.tokenAmount) - (0, import_utils9.bytesToNumberBE)(a.output.tokenAmount)
17001
17047
  );
17002
17048
  });
17003
17049
  }
@@ -17005,7 +17051,7 @@ var TokenTransactionService = class {
17005
17051
  // Helper function for deciding if the signer public key is the identity public key
17006
17052
  async signMessageWithKey(message, publicKey) {
17007
17053
  const tokenSignatures = this.config.getTokenSignatures();
17008
- if ((0, import_utils7.bytesToHex)(publicKey) === (0, import_utils7.bytesToHex)(await this.config.signer.getIdentityPublicKey())) {
17054
+ if ((0, import_utils9.bytesToHex)(publicKey) === (0, import_utils9.bytesToHex)(await this.config.signer.getIdentityPublicKey())) {
17009
17055
  if (tokenSignatures === "SCHNORR") {
17010
17056
  return await this.config.signer.signSchnorrWithIdentityKey(message);
17011
17057
  } else {
@@ -17022,7 +17068,107 @@ var TokenTransactionService = class {
17022
17068
  }
17023
17069
  }
17024
17070
  }
17071
+ async finalizeTokenTransaction(finalTokenTransaction, revocationSecrets, threshold) {
17072
+ const signingOperators = this.config.getSigningOperators();
17073
+ const soResponses = await Promise.allSettled(
17074
+ Object.entries(signingOperators).map(async ([identifier, operator]) => {
17075
+ const internalSparkClient = await this.connectionManager.createSparkClient(operator.address);
17076
+ const identityPublicKey = await this.config.signer.getIdentityPublicKey();
17077
+ try {
17078
+ const response = await internalSparkClient.finalize_token_transaction(
17079
+ {
17080
+ finalTokenTransaction,
17081
+ revocationSecrets,
17082
+ identityPublicKey
17083
+ },
17084
+ {
17085
+ retry: true,
17086
+ retryableStatuses: [
17087
+ "UNKNOWN",
17088
+ "UNAVAILABLE",
17089
+ "CANCELLED",
17090
+ "INTERNAL"
17091
+ ],
17092
+ retryMaxAttempts: 3
17093
+ }
17094
+ );
17095
+ return {
17096
+ identifier,
17097
+ response
17098
+ };
17099
+ } catch (error) {
17100
+ throw new NetworkError(
17101
+ "Failed to finalize token transaction",
17102
+ {
17103
+ operation: "finalize_token_transaction",
17104
+ errorCount: 1,
17105
+ errors: error instanceof Error ? error.message : String(error)
17106
+ },
17107
+ error
17108
+ );
17109
+ }
17110
+ })
17111
+ );
17112
+ collectResponses(soResponses);
17113
+ return finalTokenTransaction;
17114
+ }
17115
+ async createSignaturesForOperators(finalTokenTransaction, finalTokenTransactionHash, signingOperators) {
17116
+ const inputTtxoSignaturesPerOperator = [];
17117
+ for (const [_, operator] of Object.entries(signingOperators)) {
17118
+ let ttxoSignatures = [];
17119
+ if (finalTokenTransaction.tokenInputs.$case === "mintInput") {
17120
+ const issuerPublicKey = finalTokenTransaction.tokenInputs.mintInput.issuerPublicKey;
17121
+ if (!issuerPublicKey) {
17122
+ throw new ValidationError("Invalid mint input", {
17123
+ field: "issuerPublicKey",
17124
+ value: null,
17125
+ expected: "Non-null issuer public key"
17126
+ });
17127
+ }
17128
+ const payload = {
17129
+ finalTokenTransactionHash,
17130
+ operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17131
+ };
17132
+ const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
17133
+ const ownerSignature = await this.signMessageWithKey(
17134
+ payloadHash,
17135
+ issuerPublicKey
17136
+ );
17137
+ ttxoSignatures.push({
17138
+ signature: ownerSignature,
17139
+ inputIndex: 0
17140
+ });
17141
+ } else if (finalTokenTransaction.tokenInputs.$case === "transferInput") {
17142
+ const transferInput = finalTokenTransaction.tokenInputs.transferInput;
17143
+ for (let i = 0; i < transferInput.outputsToSpend.length; i++) {
17144
+ const payload = {
17145
+ finalTokenTransactionHash,
17146
+ operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17147
+ };
17148
+ const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
17149
+ let ownerSignature;
17150
+ if (this.config.getTokenSignatures() === "SCHNORR") {
17151
+ ownerSignature = await this.config.signer.signSchnorrWithIdentityKey(payloadHash);
17152
+ } else {
17153
+ ownerSignature = await this.config.signer.signMessageWithIdentityKey(payloadHash);
17154
+ }
17155
+ ttxoSignatures.push({
17156
+ signature: ownerSignature,
17157
+ inputIndex: i
17158
+ });
17159
+ }
17160
+ }
17161
+ inputTtxoSignaturesPerOperator.push({
17162
+ ttxoSignatures,
17163
+ operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17164
+ });
17165
+ }
17166
+ return inputTtxoSignaturesPerOperator;
17167
+ }
17025
17168
  };
17169
+ function isTokenTransaction(tokenTransaction) {
17170
+ return "version" in tokenTransaction && "expiryTime" in tokenTransaction;
17171
+ }
17026
17172
 
17027
17173
  // src/services/connection.ts
17028
17174
  init_buffer();
@@ -17793,10 +17939,1062 @@ function isSet5(value) {
17793
17939
  return value !== null && value !== void 0;
17794
17940
  }
17795
17941
 
17942
+ // src/proto/spark_token.ts
17943
+ init_buffer();
17944
+ var import_wire7 = require("@bufbuild/protobuf/wire");
17945
+ function createBaseTokenOutputToSpend2() {
17946
+ return { prevTokenTransactionHash: new Uint8Array(0), prevTokenTransactionVout: 0 };
17947
+ }
17948
+ var TokenOutputToSpend2 = {
17949
+ encode(message, writer = new import_wire7.BinaryWriter()) {
17950
+ if (message.prevTokenTransactionHash.length !== 0) {
17951
+ writer.uint32(10).bytes(message.prevTokenTransactionHash);
17952
+ }
17953
+ if (message.prevTokenTransactionVout !== 0) {
17954
+ writer.uint32(16).uint32(message.prevTokenTransactionVout);
17955
+ }
17956
+ return writer;
17957
+ },
17958
+ decode(input, length) {
17959
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
17960
+ let end = length === void 0 ? reader.len : reader.pos + length;
17961
+ const message = createBaseTokenOutputToSpend2();
17962
+ while (reader.pos < end) {
17963
+ const tag = reader.uint32();
17964
+ switch (tag >>> 3) {
17965
+ case 1: {
17966
+ if (tag !== 10) {
17967
+ break;
17968
+ }
17969
+ message.prevTokenTransactionHash = reader.bytes();
17970
+ continue;
17971
+ }
17972
+ case 2: {
17973
+ if (tag !== 16) {
17974
+ break;
17975
+ }
17976
+ message.prevTokenTransactionVout = reader.uint32();
17977
+ continue;
17978
+ }
17979
+ }
17980
+ if ((tag & 7) === 4 || tag === 0) {
17981
+ break;
17982
+ }
17983
+ reader.skip(tag & 7);
17984
+ }
17985
+ return message;
17986
+ },
17987
+ fromJSON(object) {
17988
+ return {
17989
+ prevTokenTransactionHash: isSet6(object.prevTokenTransactionHash) ? bytesFromBase645(object.prevTokenTransactionHash) : new Uint8Array(0),
17990
+ prevTokenTransactionVout: isSet6(object.prevTokenTransactionVout) ? globalThis.Number(object.prevTokenTransactionVout) : 0
17991
+ };
17992
+ },
17993
+ toJSON(message) {
17994
+ const obj = {};
17995
+ if (message.prevTokenTransactionHash.length !== 0) {
17996
+ obj.prevTokenTransactionHash = base64FromBytes5(message.prevTokenTransactionHash);
17997
+ }
17998
+ if (message.prevTokenTransactionVout !== 0) {
17999
+ obj.prevTokenTransactionVout = Math.round(message.prevTokenTransactionVout);
18000
+ }
18001
+ return obj;
18002
+ },
18003
+ create(base) {
18004
+ return TokenOutputToSpend2.fromPartial(base ?? {});
18005
+ },
18006
+ fromPartial(object) {
18007
+ const message = createBaseTokenOutputToSpend2();
18008
+ message.prevTokenTransactionHash = object.prevTokenTransactionHash ?? new Uint8Array(0);
18009
+ message.prevTokenTransactionVout = object.prevTokenTransactionVout ?? 0;
18010
+ return message;
18011
+ }
18012
+ };
18013
+ function createBaseTokenTransferInput2() {
18014
+ return { outputsToSpend: [] };
18015
+ }
18016
+ var TokenTransferInput2 = {
18017
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18018
+ for (const v of message.outputsToSpend) {
18019
+ TokenOutputToSpend2.encode(v, writer.uint32(10).fork()).join();
18020
+ }
18021
+ return writer;
18022
+ },
18023
+ decode(input, length) {
18024
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18025
+ let end = length === void 0 ? reader.len : reader.pos + length;
18026
+ const message = createBaseTokenTransferInput2();
18027
+ while (reader.pos < end) {
18028
+ const tag = reader.uint32();
18029
+ switch (tag >>> 3) {
18030
+ case 1: {
18031
+ if (tag !== 10) {
18032
+ break;
18033
+ }
18034
+ message.outputsToSpend.push(TokenOutputToSpend2.decode(reader, reader.uint32()));
18035
+ continue;
18036
+ }
18037
+ }
18038
+ if ((tag & 7) === 4 || tag === 0) {
18039
+ break;
18040
+ }
18041
+ reader.skip(tag & 7);
18042
+ }
18043
+ return message;
18044
+ },
18045
+ fromJSON(object) {
18046
+ return {
18047
+ outputsToSpend: globalThis.Array.isArray(object?.outputsToSpend) ? object.outputsToSpend.map((e) => TokenOutputToSpend2.fromJSON(e)) : []
18048
+ };
18049
+ },
18050
+ toJSON(message) {
18051
+ const obj = {};
18052
+ if (message.outputsToSpend?.length) {
18053
+ obj.outputsToSpend = message.outputsToSpend.map((e) => TokenOutputToSpend2.toJSON(e));
18054
+ }
18055
+ return obj;
18056
+ },
18057
+ create(base) {
18058
+ return TokenTransferInput2.fromPartial(base ?? {});
18059
+ },
18060
+ fromPartial(object) {
18061
+ const message = createBaseTokenTransferInput2();
18062
+ message.outputsToSpend = object.outputsToSpend?.map((e) => TokenOutputToSpend2.fromPartial(e)) || [];
18063
+ return message;
18064
+ }
18065
+ };
18066
+ function createBaseTokenMintInput2() {
18067
+ return { issuerPublicKey: new Uint8Array(0), issuerProvidedTimestamp: 0 };
18068
+ }
18069
+ var TokenMintInput2 = {
18070
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18071
+ if (message.issuerPublicKey.length !== 0) {
18072
+ writer.uint32(10).bytes(message.issuerPublicKey);
18073
+ }
18074
+ if (message.issuerProvidedTimestamp !== 0) {
18075
+ writer.uint32(16).uint64(message.issuerProvidedTimestamp);
18076
+ }
18077
+ return writer;
18078
+ },
18079
+ decode(input, length) {
18080
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18081
+ let end = length === void 0 ? reader.len : reader.pos + length;
18082
+ const message = createBaseTokenMintInput2();
18083
+ while (reader.pos < end) {
18084
+ const tag = reader.uint32();
18085
+ switch (tag >>> 3) {
18086
+ case 1: {
18087
+ if (tag !== 10) {
18088
+ break;
18089
+ }
18090
+ message.issuerPublicKey = reader.bytes();
18091
+ continue;
18092
+ }
18093
+ case 2: {
18094
+ if (tag !== 16) {
18095
+ break;
18096
+ }
18097
+ message.issuerProvidedTimestamp = longToNumber4(reader.uint64());
18098
+ continue;
18099
+ }
18100
+ }
18101
+ if ((tag & 7) === 4 || tag === 0) {
18102
+ break;
18103
+ }
18104
+ reader.skip(tag & 7);
18105
+ }
18106
+ return message;
18107
+ },
18108
+ fromJSON(object) {
18109
+ return {
18110
+ issuerPublicKey: isSet6(object.issuerPublicKey) ? bytesFromBase645(object.issuerPublicKey) : new Uint8Array(0),
18111
+ issuerProvidedTimestamp: isSet6(object.issuerProvidedTimestamp) ? globalThis.Number(object.issuerProvidedTimestamp) : 0
18112
+ };
18113
+ },
18114
+ toJSON(message) {
18115
+ const obj = {};
18116
+ if (message.issuerPublicKey.length !== 0) {
18117
+ obj.issuerPublicKey = base64FromBytes5(message.issuerPublicKey);
18118
+ }
18119
+ if (message.issuerProvidedTimestamp !== 0) {
18120
+ obj.issuerProvidedTimestamp = Math.round(message.issuerProvidedTimestamp);
18121
+ }
18122
+ return obj;
18123
+ },
18124
+ create(base) {
18125
+ return TokenMintInput2.fromPartial(base ?? {});
18126
+ },
18127
+ fromPartial(object) {
18128
+ const message = createBaseTokenMintInput2();
18129
+ message.issuerPublicKey = object.issuerPublicKey ?? new Uint8Array(0);
18130
+ message.issuerProvidedTimestamp = object.issuerProvidedTimestamp ?? 0;
18131
+ return message;
18132
+ }
18133
+ };
18134
+ function createBaseTokenOutput2() {
18135
+ return {
18136
+ id: void 0,
18137
+ ownerPublicKey: new Uint8Array(0),
18138
+ revocationCommitment: void 0,
18139
+ withdrawBondSats: void 0,
18140
+ withdrawRelativeBlockLocktime: void 0,
18141
+ tokenPublicKey: new Uint8Array(0),
18142
+ tokenAmount: new Uint8Array(0)
18143
+ };
18144
+ }
18145
+ var TokenOutput2 = {
18146
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18147
+ if (message.id !== void 0) {
18148
+ writer.uint32(10).string(message.id);
18149
+ }
18150
+ if (message.ownerPublicKey.length !== 0) {
18151
+ writer.uint32(18).bytes(message.ownerPublicKey);
18152
+ }
18153
+ if (message.revocationCommitment !== void 0) {
18154
+ writer.uint32(26).bytes(message.revocationCommitment);
18155
+ }
18156
+ if (message.withdrawBondSats !== void 0) {
18157
+ writer.uint32(32).uint64(message.withdrawBondSats);
18158
+ }
18159
+ if (message.withdrawRelativeBlockLocktime !== void 0) {
18160
+ writer.uint32(40).uint64(message.withdrawRelativeBlockLocktime);
18161
+ }
18162
+ if (message.tokenPublicKey.length !== 0) {
18163
+ writer.uint32(50).bytes(message.tokenPublicKey);
18164
+ }
18165
+ if (message.tokenAmount.length !== 0) {
18166
+ writer.uint32(58).bytes(message.tokenAmount);
18167
+ }
18168
+ return writer;
18169
+ },
18170
+ decode(input, length) {
18171
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18172
+ let end = length === void 0 ? reader.len : reader.pos + length;
18173
+ const message = createBaseTokenOutput2();
18174
+ while (reader.pos < end) {
18175
+ const tag = reader.uint32();
18176
+ switch (tag >>> 3) {
18177
+ case 1: {
18178
+ if (tag !== 10) {
18179
+ break;
18180
+ }
18181
+ message.id = reader.string();
18182
+ continue;
18183
+ }
18184
+ case 2: {
18185
+ if (tag !== 18) {
18186
+ break;
18187
+ }
18188
+ message.ownerPublicKey = reader.bytes();
18189
+ continue;
18190
+ }
18191
+ case 3: {
18192
+ if (tag !== 26) {
18193
+ break;
18194
+ }
18195
+ message.revocationCommitment = reader.bytes();
18196
+ continue;
18197
+ }
18198
+ case 4: {
18199
+ if (tag !== 32) {
18200
+ break;
18201
+ }
18202
+ message.withdrawBondSats = longToNumber4(reader.uint64());
18203
+ continue;
18204
+ }
18205
+ case 5: {
18206
+ if (tag !== 40) {
18207
+ break;
18208
+ }
18209
+ message.withdrawRelativeBlockLocktime = longToNumber4(reader.uint64());
18210
+ continue;
18211
+ }
18212
+ case 6: {
18213
+ if (tag !== 50) {
18214
+ break;
18215
+ }
18216
+ message.tokenPublicKey = reader.bytes();
18217
+ continue;
18218
+ }
18219
+ case 7: {
18220
+ if (tag !== 58) {
18221
+ break;
18222
+ }
18223
+ message.tokenAmount = reader.bytes();
18224
+ continue;
18225
+ }
18226
+ }
18227
+ if ((tag & 7) === 4 || tag === 0) {
18228
+ break;
18229
+ }
18230
+ reader.skip(tag & 7);
18231
+ }
18232
+ return message;
18233
+ },
18234
+ fromJSON(object) {
18235
+ return {
18236
+ id: isSet6(object.id) ? globalThis.String(object.id) : void 0,
18237
+ ownerPublicKey: isSet6(object.ownerPublicKey) ? bytesFromBase645(object.ownerPublicKey) : new Uint8Array(0),
18238
+ revocationCommitment: isSet6(object.revocationCommitment) ? bytesFromBase645(object.revocationCommitment) : void 0,
18239
+ withdrawBondSats: isSet6(object.withdrawBondSats) ? globalThis.Number(object.withdrawBondSats) : void 0,
18240
+ withdrawRelativeBlockLocktime: isSet6(object.withdrawRelativeBlockLocktime) ? globalThis.Number(object.withdrawRelativeBlockLocktime) : void 0,
18241
+ tokenPublicKey: isSet6(object.tokenPublicKey) ? bytesFromBase645(object.tokenPublicKey) : new Uint8Array(0),
18242
+ tokenAmount: isSet6(object.tokenAmount) ? bytesFromBase645(object.tokenAmount) : new Uint8Array(0)
18243
+ };
18244
+ },
18245
+ toJSON(message) {
18246
+ const obj = {};
18247
+ if (message.id !== void 0) {
18248
+ obj.id = message.id;
18249
+ }
18250
+ if (message.ownerPublicKey.length !== 0) {
18251
+ obj.ownerPublicKey = base64FromBytes5(message.ownerPublicKey);
18252
+ }
18253
+ if (message.revocationCommitment !== void 0) {
18254
+ obj.revocationCommitment = base64FromBytes5(message.revocationCommitment);
18255
+ }
18256
+ if (message.withdrawBondSats !== void 0) {
18257
+ obj.withdrawBondSats = Math.round(message.withdrawBondSats);
18258
+ }
18259
+ if (message.withdrawRelativeBlockLocktime !== void 0) {
18260
+ obj.withdrawRelativeBlockLocktime = Math.round(message.withdrawRelativeBlockLocktime);
18261
+ }
18262
+ if (message.tokenPublicKey.length !== 0) {
18263
+ obj.tokenPublicKey = base64FromBytes5(message.tokenPublicKey);
18264
+ }
18265
+ if (message.tokenAmount.length !== 0) {
18266
+ obj.tokenAmount = base64FromBytes5(message.tokenAmount);
18267
+ }
18268
+ return obj;
18269
+ },
18270
+ create(base) {
18271
+ return TokenOutput2.fromPartial(base ?? {});
18272
+ },
18273
+ fromPartial(object) {
18274
+ const message = createBaseTokenOutput2();
18275
+ message.id = object.id ?? void 0;
18276
+ message.ownerPublicKey = object.ownerPublicKey ?? new Uint8Array(0);
18277
+ message.revocationCommitment = object.revocationCommitment ?? void 0;
18278
+ message.withdrawBondSats = object.withdrawBondSats ?? void 0;
18279
+ message.withdrawRelativeBlockLocktime = object.withdrawRelativeBlockLocktime ?? void 0;
18280
+ message.tokenPublicKey = object.tokenPublicKey ?? new Uint8Array(0);
18281
+ message.tokenAmount = object.tokenAmount ?? new Uint8Array(0);
18282
+ return message;
18283
+ }
18284
+ };
18285
+ function createBaseTokenTransaction2() {
18286
+ return {
18287
+ version: 0,
18288
+ tokenInputs: void 0,
18289
+ tokenOutputs: [],
18290
+ sparkOperatorIdentityPublicKeys: [],
18291
+ expiryTime: void 0,
18292
+ network: 0
18293
+ };
18294
+ }
18295
+ var TokenTransaction2 = {
18296
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18297
+ if (message.version !== 0) {
18298
+ writer.uint32(8).uint32(message.version);
18299
+ }
18300
+ switch (message.tokenInputs?.$case) {
18301
+ case "mintInput":
18302
+ TokenMintInput2.encode(message.tokenInputs.mintInput, writer.uint32(18).fork()).join();
18303
+ break;
18304
+ case "transferInput":
18305
+ TokenTransferInput2.encode(message.tokenInputs.transferInput, writer.uint32(26).fork()).join();
18306
+ break;
18307
+ }
18308
+ for (const v of message.tokenOutputs) {
18309
+ TokenOutput2.encode(v, writer.uint32(34).fork()).join();
18310
+ }
18311
+ for (const v of message.sparkOperatorIdentityPublicKeys) {
18312
+ writer.uint32(42).bytes(v);
18313
+ }
18314
+ if (message.expiryTime !== void 0) {
18315
+ Timestamp.encode(toTimestamp2(message.expiryTime), writer.uint32(50).fork()).join();
18316
+ }
18317
+ if (message.network !== 0) {
18318
+ writer.uint32(56).int32(message.network);
18319
+ }
18320
+ return writer;
18321
+ },
18322
+ decode(input, length) {
18323
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18324
+ let end = length === void 0 ? reader.len : reader.pos + length;
18325
+ const message = createBaseTokenTransaction2();
18326
+ while (reader.pos < end) {
18327
+ const tag = reader.uint32();
18328
+ switch (tag >>> 3) {
18329
+ case 1: {
18330
+ if (tag !== 8) {
18331
+ break;
18332
+ }
18333
+ message.version = reader.uint32();
18334
+ continue;
18335
+ }
18336
+ case 2: {
18337
+ if (tag !== 18) {
18338
+ break;
18339
+ }
18340
+ message.tokenInputs = { $case: "mintInput", mintInput: TokenMintInput2.decode(reader, reader.uint32()) };
18341
+ continue;
18342
+ }
18343
+ case 3: {
18344
+ if (tag !== 26) {
18345
+ break;
18346
+ }
18347
+ message.tokenInputs = {
18348
+ $case: "transferInput",
18349
+ transferInput: TokenTransferInput2.decode(reader, reader.uint32())
18350
+ };
18351
+ continue;
18352
+ }
18353
+ case 4: {
18354
+ if (tag !== 34) {
18355
+ break;
18356
+ }
18357
+ message.tokenOutputs.push(TokenOutput2.decode(reader, reader.uint32()));
18358
+ continue;
18359
+ }
18360
+ case 5: {
18361
+ if (tag !== 42) {
18362
+ break;
18363
+ }
18364
+ message.sparkOperatorIdentityPublicKeys.push(reader.bytes());
18365
+ continue;
18366
+ }
18367
+ case 6: {
18368
+ if (tag !== 50) {
18369
+ break;
18370
+ }
18371
+ message.expiryTime = fromTimestamp2(Timestamp.decode(reader, reader.uint32()));
18372
+ continue;
18373
+ }
18374
+ case 7: {
18375
+ if (tag !== 56) {
18376
+ break;
18377
+ }
18378
+ message.network = reader.int32();
18379
+ continue;
18380
+ }
18381
+ }
18382
+ if ((tag & 7) === 4 || tag === 0) {
18383
+ break;
18384
+ }
18385
+ reader.skip(tag & 7);
18386
+ }
18387
+ return message;
18388
+ },
18389
+ fromJSON(object) {
18390
+ return {
18391
+ version: isSet6(object.version) ? globalThis.Number(object.version) : 0,
18392
+ tokenInputs: isSet6(object.mintInput) ? { $case: "mintInput", mintInput: TokenMintInput2.fromJSON(object.mintInput) } : isSet6(object.transferInput) ? { $case: "transferInput", transferInput: TokenTransferInput2.fromJSON(object.transferInput) } : void 0,
18393
+ tokenOutputs: globalThis.Array.isArray(object?.tokenOutputs) ? object.tokenOutputs.map((e) => TokenOutput2.fromJSON(e)) : [],
18394
+ sparkOperatorIdentityPublicKeys: globalThis.Array.isArray(object?.sparkOperatorIdentityPublicKeys) ? object.sparkOperatorIdentityPublicKeys.map((e) => bytesFromBase645(e)) : [],
18395
+ expiryTime: isSet6(object.expiryTime) ? fromJsonTimestamp2(object.expiryTime) : void 0,
18396
+ network: isSet6(object.network) ? networkFromJSON(object.network) : 0
18397
+ };
18398
+ },
18399
+ toJSON(message) {
18400
+ const obj = {};
18401
+ if (message.version !== 0) {
18402
+ obj.version = Math.round(message.version);
18403
+ }
18404
+ if (message.tokenInputs?.$case === "mintInput") {
18405
+ obj.mintInput = TokenMintInput2.toJSON(message.tokenInputs.mintInput);
18406
+ } else if (message.tokenInputs?.$case === "transferInput") {
18407
+ obj.transferInput = TokenTransferInput2.toJSON(message.tokenInputs.transferInput);
18408
+ }
18409
+ if (message.tokenOutputs?.length) {
18410
+ obj.tokenOutputs = message.tokenOutputs.map((e) => TokenOutput2.toJSON(e));
18411
+ }
18412
+ if (message.sparkOperatorIdentityPublicKeys?.length) {
18413
+ obj.sparkOperatorIdentityPublicKeys = message.sparkOperatorIdentityPublicKeys.map((e) => base64FromBytes5(e));
18414
+ }
18415
+ if (message.expiryTime !== void 0) {
18416
+ obj.expiryTime = message.expiryTime.toISOString();
18417
+ }
18418
+ if (message.network !== 0) {
18419
+ obj.network = networkToJSON(message.network);
18420
+ }
18421
+ return obj;
18422
+ },
18423
+ create(base) {
18424
+ return TokenTransaction2.fromPartial(base ?? {});
18425
+ },
18426
+ fromPartial(object) {
18427
+ const message = createBaseTokenTransaction2();
18428
+ message.version = object.version ?? 0;
18429
+ switch (object.tokenInputs?.$case) {
18430
+ case "mintInput": {
18431
+ if (object.tokenInputs?.mintInput !== void 0 && object.tokenInputs?.mintInput !== null) {
18432
+ message.tokenInputs = {
18433
+ $case: "mintInput",
18434
+ mintInput: TokenMintInput2.fromPartial(object.tokenInputs.mintInput)
18435
+ };
18436
+ }
18437
+ break;
18438
+ }
18439
+ case "transferInput": {
18440
+ if (object.tokenInputs?.transferInput !== void 0 && object.tokenInputs?.transferInput !== null) {
18441
+ message.tokenInputs = {
18442
+ $case: "transferInput",
18443
+ transferInput: TokenTransferInput2.fromPartial(object.tokenInputs.transferInput)
18444
+ };
18445
+ }
18446
+ break;
18447
+ }
18448
+ }
18449
+ message.tokenOutputs = object.tokenOutputs?.map((e) => TokenOutput2.fromPartial(e)) || [];
18450
+ message.sparkOperatorIdentityPublicKeys = object.sparkOperatorIdentityPublicKeys?.map((e) => e) || [];
18451
+ message.expiryTime = object.expiryTime ?? void 0;
18452
+ message.network = object.network ?? 0;
18453
+ return message;
18454
+ }
18455
+ };
18456
+ function createBaseSignatureWithIndex2() {
18457
+ return { signature: new Uint8Array(0), inputIndex: 0 };
18458
+ }
18459
+ var SignatureWithIndex2 = {
18460
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18461
+ if (message.signature.length !== 0) {
18462
+ writer.uint32(10).bytes(message.signature);
18463
+ }
18464
+ if (message.inputIndex !== 0) {
18465
+ writer.uint32(16).uint32(message.inputIndex);
18466
+ }
18467
+ return writer;
18468
+ },
18469
+ decode(input, length) {
18470
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18471
+ let end = length === void 0 ? reader.len : reader.pos + length;
18472
+ const message = createBaseSignatureWithIndex2();
18473
+ while (reader.pos < end) {
18474
+ const tag = reader.uint32();
18475
+ switch (tag >>> 3) {
18476
+ case 1: {
18477
+ if (tag !== 10) {
18478
+ break;
18479
+ }
18480
+ message.signature = reader.bytes();
18481
+ continue;
18482
+ }
18483
+ case 2: {
18484
+ if (tag !== 16) {
18485
+ break;
18486
+ }
18487
+ message.inputIndex = reader.uint32();
18488
+ continue;
18489
+ }
18490
+ }
18491
+ if ((tag & 7) === 4 || tag === 0) {
18492
+ break;
18493
+ }
18494
+ reader.skip(tag & 7);
18495
+ }
18496
+ return message;
18497
+ },
18498
+ fromJSON(object) {
18499
+ return {
18500
+ signature: isSet6(object.signature) ? bytesFromBase645(object.signature) : new Uint8Array(0),
18501
+ inputIndex: isSet6(object.inputIndex) ? globalThis.Number(object.inputIndex) : 0
18502
+ };
18503
+ },
18504
+ toJSON(message) {
18505
+ const obj = {};
18506
+ if (message.signature.length !== 0) {
18507
+ obj.signature = base64FromBytes5(message.signature);
18508
+ }
18509
+ if (message.inputIndex !== 0) {
18510
+ obj.inputIndex = Math.round(message.inputIndex);
18511
+ }
18512
+ return obj;
18513
+ },
18514
+ create(base) {
18515
+ return SignatureWithIndex2.fromPartial(base ?? {});
18516
+ },
18517
+ fromPartial(object) {
18518
+ const message = createBaseSignatureWithIndex2();
18519
+ message.signature = object.signature ?? new Uint8Array(0);
18520
+ message.inputIndex = object.inputIndex ?? 0;
18521
+ return message;
18522
+ }
18523
+ };
18524
+ function createBaseInputTtxoSignaturesPerOperator() {
18525
+ return { ttxoSignatures: [], operatorIdentityPublicKey: new Uint8Array(0) };
18526
+ }
18527
+ var InputTtxoSignaturesPerOperator = {
18528
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18529
+ for (const v of message.ttxoSignatures) {
18530
+ SignatureWithIndex2.encode(v, writer.uint32(10).fork()).join();
18531
+ }
18532
+ if (message.operatorIdentityPublicKey.length !== 0) {
18533
+ writer.uint32(18).bytes(message.operatorIdentityPublicKey);
18534
+ }
18535
+ return writer;
18536
+ },
18537
+ decode(input, length) {
18538
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18539
+ let end = length === void 0 ? reader.len : reader.pos + length;
18540
+ const message = createBaseInputTtxoSignaturesPerOperator();
18541
+ while (reader.pos < end) {
18542
+ const tag = reader.uint32();
18543
+ switch (tag >>> 3) {
18544
+ case 1: {
18545
+ if (tag !== 10) {
18546
+ break;
18547
+ }
18548
+ message.ttxoSignatures.push(SignatureWithIndex2.decode(reader, reader.uint32()));
18549
+ continue;
18550
+ }
18551
+ case 2: {
18552
+ if (tag !== 18) {
18553
+ break;
18554
+ }
18555
+ message.operatorIdentityPublicKey = reader.bytes();
18556
+ continue;
18557
+ }
18558
+ }
18559
+ if ((tag & 7) === 4 || tag === 0) {
18560
+ break;
18561
+ }
18562
+ reader.skip(tag & 7);
18563
+ }
18564
+ return message;
18565
+ },
18566
+ fromJSON(object) {
18567
+ return {
18568
+ ttxoSignatures: globalThis.Array.isArray(object?.ttxoSignatures) ? object.ttxoSignatures.map((e) => SignatureWithIndex2.fromJSON(e)) : [],
18569
+ operatorIdentityPublicKey: isSet6(object.operatorIdentityPublicKey) ? bytesFromBase645(object.operatorIdentityPublicKey) : new Uint8Array(0)
18570
+ };
18571
+ },
18572
+ toJSON(message) {
18573
+ const obj = {};
18574
+ if (message.ttxoSignatures?.length) {
18575
+ obj.ttxoSignatures = message.ttxoSignatures.map((e) => SignatureWithIndex2.toJSON(e));
18576
+ }
18577
+ if (message.operatorIdentityPublicKey.length !== 0) {
18578
+ obj.operatorIdentityPublicKey = base64FromBytes5(message.operatorIdentityPublicKey);
18579
+ }
18580
+ return obj;
18581
+ },
18582
+ create(base) {
18583
+ return InputTtxoSignaturesPerOperator.fromPartial(base ?? {});
18584
+ },
18585
+ fromPartial(object) {
18586
+ const message = createBaseInputTtxoSignaturesPerOperator();
18587
+ message.ttxoSignatures = object.ttxoSignatures?.map((e) => SignatureWithIndex2.fromPartial(e)) || [];
18588
+ message.operatorIdentityPublicKey = object.operatorIdentityPublicKey ?? new Uint8Array(0);
18589
+ return message;
18590
+ }
18591
+ };
18592
+ function createBaseStartTransactionRequest() {
18593
+ return {
18594
+ identityPublicKey: new Uint8Array(0),
18595
+ partialTokenTransaction: void 0,
18596
+ partialTokenTransactionOwnerSignatures: [],
18597
+ validityDurationSeconds: 0
18598
+ };
18599
+ }
18600
+ var StartTransactionRequest = {
18601
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18602
+ if (message.identityPublicKey.length !== 0) {
18603
+ writer.uint32(10).bytes(message.identityPublicKey);
18604
+ }
18605
+ if (message.partialTokenTransaction !== void 0) {
18606
+ TokenTransaction2.encode(message.partialTokenTransaction, writer.uint32(18).fork()).join();
18607
+ }
18608
+ for (const v of message.partialTokenTransactionOwnerSignatures) {
18609
+ SignatureWithIndex2.encode(v, writer.uint32(26).fork()).join();
18610
+ }
18611
+ if (message.validityDurationSeconds !== 0) {
18612
+ writer.uint32(32).uint64(message.validityDurationSeconds);
18613
+ }
18614
+ return writer;
18615
+ },
18616
+ decode(input, length) {
18617
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18618
+ let end = length === void 0 ? reader.len : reader.pos + length;
18619
+ const message = createBaseStartTransactionRequest();
18620
+ while (reader.pos < end) {
18621
+ const tag = reader.uint32();
18622
+ switch (tag >>> 3) {
18623
+ case 1: {
18624
+ if (tag !== 10) {
18625
+ break;
18626
+ }
18627
+ message.identityPublicKey = reader.bytes();
18628
+ continue;
18629
+ }
18630
+ case 2: {
18631
+ if (tag !== 18) {
18632
+ break;
18633
+ }
18634
+ message.partialTokenTransaction = TokenTransaction2.decode(reader, reader.uint32());
18635
+ continue;
18636
+ }
18637
+ case 3: {
18638
+ if (tag !== 26) {
18639
+ break;
18640
+ }
18641
+ message.partialTokenTransactionOwnerSignatures.push(SignatureWithIndex2.decode(reader, reader.uint32()));
18642
+ continue;
18643
+ }
18644
+ case 4: {
18645
+ if (tag !== 32) {
18646
+ break;
18647
+ }
18648
+ message.validityDurationSeconds = longToNumber4(reader.uint64());
18649
+ continue;
18650
+ }
18651
+ }
18652
+ if ((tag & 7) === 4 || tag === 0) {
18653
+ break;
18654
+ }
18655
+ reader.skip(tag & 7);
18656
+ }
18657
+ return message;
18658
+ },
18659
+ fromJSON(object) {
18660
+ return {
18661
+ identityPublicKey: isSet6(object.identityPublicKey) ? bytesFromBase645(object.identityPublicKey) : new Uint8Array(0),
18662
+ partialTokenTransaction: isSet6(object.partialTokenTransaction) ? TokenTransaction2.fromJSON(object.partialTokenTransaction) : void 0,
18663
+ partialTokenTransactionOwnerSignatures: globalThis.Array.isArray(object?.partialTokenTransactionOwnerSignatures) ? object.partialTokenTransactionOwnerSignatures.map((e) => SignatureWithIndex2.fromJSON(e)) : [],
18664
+ validityDurationSeconds: isSet6(object.validityDurationSeconds) ? globalThis.Number(object.validityDurationSeconds) : 0
18665
+ };
18666
+ },
18667
+ toJSON(message) {
18668
+ const obj = {};
18669
+ if (message.identityPublicKey.length !== 0) {
18670
+ obj.identityPublicKey = base64FromBytes5(message.identityPublicKey);
18671
+ }
18672
+ if (message.partialTokenTransaction !== void 0) {
18673
+ obj.partialTokenTransaction = TokenTransaction2.toJSON(message.partialTokenTransaction);
18674
+ }
18675
+ if (message.partialTokenTransactionOwnerSignatures?.length) {
18676
+ obj.partialTokenTransactionOwnerSignatures = message.partialTokenTransactionOwnerSignatures.map(
18677
+ (e) => SignatureWithIndex2.toJSON(e)
18678
+ );
18679
+ }
18680
+ if (message.validityDurationSeconds !== 0) {
18681
+ obj.validityDurationSeconds = Math.round(message.validityDurationSeconds);
18682
+ }
18683
+ return obj;
18684
+ },
18685
+ create(base) {
18686
+ return StartTransactionRequest.fromPartial(base ?? {});
18687
+ },
18688
+ fromPartial(object) {
18689
+ const message = createBaseStartTransactionRequest();
18690
+ message.identityPublicKey = object.identityPublicKey ?? new Uint8Array(0);
18691
+ message.partialTokenTransaction = object.partialTokenTransaction !== void 0 && object.partialTokenTransaction !== null ? TokenTransaction2.fromPartial(object.partialTokenTransaction) : void 0;
18692
+ message.partialTokenTransactionOwnerSignatures = object.partialTokenTransactionOwnerSignatures?.map((e) => SignatureWithIndex2.fromPartial(e)) || [];
18693
+ message.validityDurationSeconds = object.validityDurationSeconds ?? 0;
18694
+ return message;
18695
+ }
18696
+ };
18697
+ function createBaseStartTransactionResponse() {
18698
+ return { finalTokenTransaction: void 0, keyshareInfo: void 0 };
18699
+ }
18700
+ var StartTransactionResponse = {
18701
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18702
+ if (message.finalTokenTransaction !== void 0) {
18703
+ TokenTransaction2.encode(message.finalTokenTransaction, writer.uint32(10).fork()).join();
18704
+ }
18705
+ if (message.keyshareInfo !== void 0) {
18706
+ SigningKeyshare.encode(message.keyshareInfo, writer.uint32(18).fork()).join();
18707
+ }
18708
+ return writer;
18709
+ },
18710
+ decode(input, length) {
18711
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18712
+ let end = length === void 0 ? reader.len : reader.pos + length;
18713
+ const message = createBaseStartTransactionResponse();
18714
+ while (reader.pos < end) {
18715
+ const tag = reader.uint32();
18716
+ switch (tag >>> 3) {
18717
+ case 1: {
18718
+ if (tag !== 10) {
18719
+ break;
18720
+ }
18721
+ message.finalTokenTransaction = TokenTransaction2.decode(reader, reader.uint32());
18722
+ continue;
18723
+ }
18724
+ case 2: {
18725
+ if (tag !== 18) {
18726
+ break;
18727
+ }
18728
+ message.keyshareInfo = SigningKeyshare.decode(reader, reader.uint32());
18729
+ continue;
18730
+ }
18731
+ }
18732
+ if ((tag & 7) === 4 || tag === 0) {
18733
+ break;
18734
+ }
18735
+ reader.skip(tag & 7);
18736
+ }
18737
+ return message;
18738
+ },
18739
+ fromJSON(object) {
18740
+ return {
18741
+ finalTokenTransaction: isSet6(object.finalTokenTransaction) ? TokenTransaction2.fromJSON(object.finalTokenTransaction) : void 0,
18742
+ keyshareInfo: isSet6(object.keyshareInfo) ? SigningKeyshare.fromJSON(object.keyshareInfo) : void 0
18743
+ };
18744
+ },
18745
+ toJSON(message) {
18746
+ const obj = {};
18747
+ if (message.finalTokenTransaction !== void 0) {
18748
+ obj.finalTokenTransaction = TokenTransaction2.toJSON(message.finalTokenTransaction);
18749
+ }
18750
+ if (message.keyshareInfo !== void 0) {
18751
+ obj.keyshareInfo = SigningKeyshare.toJSON(message.keyshareInfo);
18752
+ }
18753
+ return obj;
18754
+ },
18755
+ create(base) {
18756
+ return StartTransactionResponse.fromPartial(base ?? {});
18757
+ },
18758
+ fromPartial(object) {
18759
+ const message = createBaseStartTransactionResponse();
18760
+ message.finalTokenTransaction = object.finalTokenTransaction !== void 0 && object.finalTokenTransaction !== null ? TokenTransaction2.fromPartial(object.finalTokenTransaction) : void 0;
18761
+ message.keyshareInfo = object.keyshareInfo !== void 0 && object.keyshareInfo !== null ? SigningKeyshare.fromPartial(object.keyshareInfo) : void 0;
18762
+ return message;
18763
+ }
18764
+ };
18765
+ function createBaseCommitTransactionRequest() {
18766
+ return {
18767
+ finalTokenTransaction: void 0,
18768
+ finalTokenTransactionHash: new Uint8Array(0),
18769
+ inputTtxoSignaturesPerOperator: [],
18770
+ ownerIdentityPublicKey: new Uint8Array(0)
18771
+ };
18772
+ }
18773
+ var CommitTransactionRequest = {
18774
+ encode(message, writer = new import_wire7.BinaryWriter()) {
18775
+ if (message.finalTokenTransaction !== void 0) {
18776
+ TokenTransaction2.encode(message.finalTokenTransaction, writer.uint32(10).fork()).join();
18777
+ }
18778
+ if (message.finalTokenTransactionHash.length !== 0) {
18779
+ writer.uint32(18).bytes(message.finalTokenTransactionHash);
18780
+ }
18781
+ for (const v of message.inputTtxoSignaturesPerOperator) {
18782
+ InputTtxoSignaturesPerOperator.encode(v, writer.uint32(26).fork()).join();
18783
+ }
18784
+ if (message.ownerIdentityPublicKey.length !== 0) {
18785
+ writer.uint32(34).bytes(message.ownerIdentityPublicKey);
18786
+ }
18787
+ return writer;
18788
+ },
18789
+ decode(input, length) {
18790
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18791
+ let end = length === void 0 ? reader.len : reader.pos + length;
18792
+ const message = createBaseCommitTransactionRequest();
18793
+ while (reader.pos < end) {
18794
+ const tag = reader.uint32();
18795
+ switch (tag >>> 3) {
18796
+ case 1: {
18797
+ if (tag !== 10) {
18798
+ break;
18799
+ }
18800
+ message.finalTokenTransaction = TokenTransaction2.decode(reader, reader.uint32());
18801
+ continue;
18802
+ }
18803
+ case 2: {
18804
+ if (tag !== 18) {
18805
+ break;
18806
+ }
18807
+ message.finalTokenTransactionHash = reader.bytes();
18808
+ continue;
18809
+ }
18810
+ case 3: {
18811
+ if (tag !== 26) {
18812
+ break;
18813
+ }
18814
+ message.inputTtxoSignaturesPerOperator.push(InputTtxoSignaturesPerOperator.decode(reader, reader.uint32()));
18815
+ continue;
18816
+ }
18817
+ case 4: {
18818
+ if (tag !== 34) {
18819
+ break;
18820
+ }
18821
+ message.ownerIdentityPublicKey = reader.bytes();
18822
+ continue;
18823
+ }
18824
+ }
18825
+ if ((tag & 7) === 4 || tag === 0) {
18826
+ break;
18827
+ }
18828
+ reader.skip(tag & 7);
18829
+ }
18830
+ return message;
18831
+ },
18832
+ fromJSON(object) {
18833
+ return {
18834
+ finalTokenTransaction: isSet6(object.finalTokenTransaction) ? TokenTransaction2.fromJSON(object.finalTokenTransaction) : void 0,
18835
+ finalTokenTransactionHash: isSet6(object.finalTokenTransactionHash) ? bytesFromBase645(object.finalTokenTransactionHash) : new Uint8Array(0),
18836
+ inputTtxoSignaturesPerOperator: globalThis.Array.isArray(object?.inputTtxoSignaturesPerOperator) ? object.inputTtxoSignaturesPerOperator.map((e) => InputTtxoSignaturesPerOperator.fromJSON(e)) : [],
18837
+ ownerIdentityPublicKey: isSet6(object.ownerIdentityPublicKey) ? bytesFromBase645(object.ownerIdentityPublicKey) : new Uint8Array(0)
18838
+ };
18839
+ },
18840
+ toJSON(message) {
18841
+ const obj = {};
18842
+ if (message.finalTokenTransaction !== void 0) {
18843
+ obj.finalTokenTransaction = TokenTransaction2.toJSON(message.finalTokenTransaction);
18844
+ }
18845
+ if (message.finalTokenTransactionHash.length !== 0) {
18846
+ obj.finalTokenTransactionHash = base64FromBytes5(message.finalTokenTransactionHash);
18847
+ }
18848
+ if (message.inputTtxoSignaturesPerOperator?.length) {
18849
+ obj.inputTtxoSignaturesPerOperator = message.inputTtxoSignaturesPerOperator.map(
18850
+ (e) => InputTtxoSignaturesPerOperator.toJSON(e)
18851
+ );
18852
+ }
18853
+ if (message.ownerIdentityPublicKey.length !== 0) {
18854
+ obj.ownerIdentityPublicKey = base64FromBytes5(message.ownerIdentityPublicKey);
18855
+ }
18856
+ return obj;
18857
+ },
18858
+ create(base) {
18859
+ return CommitTransactionRequest.fromPartial(base ?? {});
18860
+ },
18861
+ fromPartial(object) {
18862
+ const message = createBaseCommitTransactionRequest();
18863
+ message.finalTokenTransaction = object.finalTokenTransaction !== void 0 && object.finalTokenTransaction !== null ? TokenTransaction2.fromPartial(object.finalTokenTransaction) : void 0;
18864
+ message.finalTokenTransactionHash = object.finalTokenTransactionHash ?? new Uint8Array(0);
18865
+ message.inputTtxoSignaturesPerOperator = object.inputTtxoSignaturesPerOperator?.map((e) => InputTtxoSignaturesPerOperator.fromPartial(e)) || [];
18866
+ message.ownerIdentityPublicKey = object.ownerIdentityPublicKey ?? new Uint8Array(0);
18867
+ return message;
18868
+ }
18869
+ };
18870
+ function createBaseCommitTransactionResponse() {
18871
+ return {};
18872
+ }
18873
+ var CommitTransactionResponse = {
18874
+ encode(_, writer = new import_wire7.BinaryWriter()) {
18875
+ return writer;
18876
+ },
18877
+ decode(input, length) {
18878
+ const reader = input instanceof import_wire7.BinaryReader ? input : new import_wire7.BinaryReader(input);
18879
+ let end = length === void 0 ? reader.len : reader.pos + length;
18880
+ const message = createBaseCommitTransactionResponse();
18881
+ while (reader.pos < end) {
18882
+ const tag = reader.uint32();
18883
+ switch (tag >>> 3) {
18884
+ }
18885
+ if ((tag & 7) === 4 || tag === 0) {
18886
+ break;
18887
+ }
18888
+ reader.skip(tag & 7);
18889
+ }
18890
+ return message;
18891
+ },
18892
+ fromJSON(_) {
18893
+ return {};
18894
+ },
18895
+ toJSON(_) {
18896
+ const obj = {};
18897
+ return obj;
18898
+ },
18899
+ create(base) {
18900
+ return CommitTransactionResponse.fromPartial(base ?? {});
18901
+ },
18902
+ fromPartial(_) {
18903
+ const message = createBaseCommitTransactionResponse();
18904
+ return message;
18905
+ }
18906
+ };
18907
+ var SparkTokenServiceDefinition = {
18908
+ name: "SparkTokenService",
18909
+ fullName: "spark_token.SparkTokenService",
18910
+ methods: {
18911
+ /**
18912
+ * Start process to create final token transaction with all inputs required
18913
+ * from user and SOs (including revocation secret commitment)
18914
+ */
18915
+ start_transaction: {
18916
+ name: "start_transaction",
18917
+ requestType: StartTransactionRequest,
18918
+ requestStream: false,
18919
+ responseType: StartTransactionResponse,
18920
+ responseStream: false,
18921
+ options: {}
18922
+ },
18923
+ /**
18924
+ * Complete the transaction and commit it with all SOs. This will be
18925
+ * coordinated by one SO.
18926
+ */
18927
+ commit_transaction: {
18928
+ name: "commit_transaction",
18929
+ requestType: CommitTransactionRequest,
18930
+ requestStream: false,
18931
+ responseType: CommitTransactionResponse,
18932
+ responseStream: false,
18933
+ options: {}
18934
+ }
18935
+ }
18936
+ };
18937
+ function bytesFromBase645(b64) {
18938
+ if (globalThis.Buffer) {
18939
+ return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
18940
+ } else {
18941
+ const bin = globalThis.atob(b64);
18942
+ const arr = new Uint8Array(bin.length);
18943
+ for (let i = 0; i < bin.length; ++i) {
18944
+ arr[i] = bin.charCodeAt(i);
18945
+ }
18946
+ return arr;
18947
+ }
18948
+ }
18949
+ function base64FromBytes5(arr) {
18950
+ if (globalThis.Buffer) {
18951
+ return globalThis.Buffer.from(arr).toString("base64");
18952
+ } else {
18953
+ const bin = [];
18954
+ arr.forEach((byte) => {
18955
+ bin.push(globalThis.String.fromCharCode(byte));
18956
+ });
18957
+ return globalThis.btoa(bin.join(""));
18958
+ }
18959
+ }
18960
+ function toTimestamp2(date) {
18961
+ const seconds = Math.trunc(date.getTime() / 1e3);
18962
+ const nanos = date.getTime() % 1e3 * 1e6;
18963
+ return { seconds, nanos };
18964
+ }
18965
+ function fromTimestamp2(t) {
18966
+ let millis = (t.seconds || 0) * 1e3;
18967
+ millis += (t.nanos || 0) / 1e6;
18968
+ return new globalThis.Date(millis);
18969
+ }
18970
+ function fromJsonTimestamp2(o) {
18971
+ if (o instanceof globalThis.Date) {
18972
+ return o;
18973
+ } else if (typeof o === "string") {
18974
+ return new globalThis.Date(o);
18975
+ } else {
18976
+ return fromTimestamp2(Timestamp.fromJSON(o));
18977
+ }
18978
+ }
18979
+ function longToNumber4(int64) {
18980
+ const num = globalThis.Number(int64.toString());
18981
+ if (num > globalThis.Number.MAX_SAFE_INTEGER) {
18982
+ throw new globalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
18983
+ }
18984
+ if (num < globalThis.Number.MIN_SAFE_INTEGER) {
18985
+ throw new globalThis.Error("Value is smaller than Number.MIN_SAFE_INTEGER");
18986
+ }
18987
+ return num;
18988
+ }
18989
+ function isSet6(value) {
18990
+ return value !== null && value !== void 0;
18991
+ }
18992
+
17796
18993
  // src/services/connection.ts
17797
18994
  var ConnectionManager = class {
17798
18995
  config;
17799
18996
  clients = /* @__PURE__ */ new Map();
18997
+ tokenClients = /* @__PURE__ */ new Map();
17800
18998
  // We are going to .unref() the underlying channels for stream clients
17801
18999
  // to prevent the stream from keeping the process alive
17802
19000
  // Using a different map to avoid unforeseen problems with unary calls
@@ -17920,6 +19118,22 @@ var ConnectionManager = class {
17920
19118
  this.clients.set(address2, { client, authToken });
17921
19119
  return client;
17922
19120
  }
19121
+ async createSparkTokenClient(address2, certPath) {
19122
+ if (this.tokenClients.has(address2)) {
19123
+ return this.tokenClients.get(address2).client;
19124
+ }
19125
+ const authToken = await this.authenticate(address2);
19126
+ const channel = await this.createChannelWithTLS(address2, certPath);
19127
+ const middleware = this.createMiddleware(address2, authToken);
19128
+ const tokenClient = await this.createGrpcClient(
19129
+ SparkTokenServiceDefinition,
19130
+ channel,
19131
+ true,
19132
+ middleware
19133
+ );
19134
+ this.tokenClients.set(address2, { client: tokenClient, authToken });
19135
+ return tokenClient;
19136
+ }
17923
19137
  async getStreamChannel(address2) {
17924
19138
  return this.streamClients.get(address2)?.channel;
17925
19139
  }