@buildonspark/spark-sdk 0.1.41 → 0.1.43

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 (156) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/android/src/main/jniLibs/arm64-v8a/libuniffi_spark_frost.so +0 -0
  3. package/android/src/main/jniLibs/armeabi-v7a/libuniffi_spark_frost.so +0 -0
  4. package/android/src/main/jniLibs/x86/libuniffi_spark_frost.so +0 -0
  5. package/android/src/main/jniLibs/x86_64/libuniffi_spark_frost.so +0 -0
  6. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.cts +10 -0
  7. package/dist/LightningSendFeeEstimateInput-BgOhEAI-.d.ts +10 -0
  8. package/dist/{RequestLightningSendInput-mXUWn_cp.d.ts → RequestLightningSendInput-D7fZdT4A.d.ts} +35 -16
  9. package/dist/{RequestLightningSendInput-DXcLoiCe.d.cts → RequestLightningSendInput-Na1mHdWg.d.cts} +35 -16
  10. package/dist/address/index.cjs +38 -7
  11. package/dist/address/index.d.cts +2 -2
  12. package/dist/address/index.d.ts +2 -2
  13. package/dist/address/index.js +3 -3
  14. package/dist/{chunk-ATEHMLKP.js → chunk-6AFUC5M2.js} +1 -1
  15. package/dist/{chunk-ZXDE2XMU.js → chunk-BUTZWYBW.js} +9 -6
  16. package/dist/{chunk-7EFSUADA.js → chunk-DOA6QXYQ.js} +1 -0
  17. package/dist/{chunk-J5W5Q2ZP.js → chunk-DQYKQJRZ.js} +291 -7
  18. package/dist/{chunk-TWF35O6M.js → chunk-GSI4OLXZ.js} +32 -1
  19. package/dist/{chunk-2ZXXLPG2.js → chunk-GYQR4B4P.js} +5 -4
  20. package/dist/{chunk-ROKY5KS4.js → chunk-HRQRRDSS.js} +53 -15
  21. package/dist/{chunk-YEZDPUFY.js → chunk-IRW5TWMH.js} +8 -8
  22. package/dist/{chunk-7VMYMQLF.js → chunk-NSJF5F5O.js} +1 -1
  23. package/dist/{chunk-TM4TOEOX.js → chunk-O4RYNJNB.js} +3 -3
  24. package/dist/{chunk-MGPRLH6Q.js → chunk-QNNSEJ4P.js} +1 -1
  25. package/dist/{chunk-UKT6OFLO.js → chunk-TIUBYNN5.js} +13 -7
  26. package/dist/{chunk-6YVPOQ2A.js → chunk-TOSP3INR.js} +235 -143
  27. package/dist/chunk-VFJQNBFX.js +21 -0
  28. package/dist/{chunk-KKSU7OZO.js → chunk-WWOTVNPP.js} +195 -67
  29. package/dist/{chunk-HK6LPV6Z.js → chunk-Z5HIAYFT.js} +1 -1
  30. package/dist/graphql/objects/index.cjs +229 -135
  31. package/dist/graphql/objects/index.d.cts +54 -9
  32. package/dist/graphql/objects/index.d.ts +54 -9
  33. package/dist/graphql/objects/index.js +3 -3
  34. package/dist/{index-OSDtPMmC.d.ts → index-7RYRH5wc.d.ts} +10 -8
  35. package/dist/{index-CFh4uWzi.d.cts → index-BJOc8Ur-.d.cts} +10 -8
  36. package/dist/index.cjs +790 -315
  37. package/dist/index.d.cts +8 -7
  38. package/dist/index.d.ts +8 -7
  39. package/dist/index.js +26 -26
  40. package/dist/index.node.cjs +790 -315
  41. package/dist/index.node.d.cts +9 -8
  42. package/dist/index.node.d.ts +9 -8
  43. package/dist/index.node.js +26 -26
  44. package/dist/native/index.cjs +812 -332
  45. package/dist/native/index.d.cts +53 -18
  46. package/dist/native/index.d.ts +53 -18
  47. package/dist/native/index.js +659 -181
  48. package/dist/{network-BiwBmoOg.d.cts → network-D5lKssVl.d.cts} +1 -1
  49. package/dist/{network-BF2GYPye.d.ts → network-xkBSpaTn.d.ts} +1 -1
  50. package/dist/proto/lrc20.d.cts +1 -1
  51. package/dist/proto/lrc20.d.ts +1 -1
  52. package/dist/proto/spark.d.cts +1 -1
  53. package/dist/proto/spark.d.ts +1 -1
  54. package/dist/proto/spark_token.d.cts +1 -1
  55. package/dist/proto/spark_token.d.ts +1 -1
  56. package/dist/{sdk-types-CfhdFnsA.d.cts → sdk-types-B-q9py_P.d.cts} +1 -1
  57. package/dist/{sdk-types-MnQrHolg.d.ts → sdk-types-BPoPgzda.d.ts} +1 -1
  58. package/dist/services/config.cjs +118 -69
  59. package/dist/services/config.d.cts +6 -4
  60. package/dist/services/config.d.ts +6 -4
  61. package/dist/services/config.js +9 -9
  62. package/dist/services/connection.cjs +95 -15
  63. package/dist/services/connection.d.cts +6 -4
  64. package/dist/services/connection.d.ts +6 -4
  65. package/dist/services/connection.js +3 -3
  66. package/dist/services/index.cjs +487 -117
  67. package/dist/services/index.d.cts +5 -4
  68. package/dist/services/index.d.ts +5 -4
  69. package/dist/services/index.js +19 -19
  70. package/dist/services/lrc-connection.cjs +50 -7
  71. package/dist/services/lrc-connection.d.cts +5 -4
  72. package/dist/services/lrc-connection.d.ts +5 -4
  73. package/dist/services/lrc-connection.js +3 -3
  74. package/dist/services/token-transactions.cjs +351 -36
  75. package/dist/services/token-transactions.d.cts +5 -4
  76. package/dist/services/token-transactions.d.ts +5 -4
  77. package/dist/services/token-transactions.js +6 -6
  78. package/dist/services/wallet-config.cjs +1 -0
  79. package/dist/services/wallet-config.d.cts +6 -4
  80. package/dist/services/wallet-config.d.ts +6 -4
  81. package/dist/services/wallet-config.js +1 -1
  82. package/dist/signer/signer.cjs +117 -64
  83. package/dist/signer/signer.d.cts +4 -3
  84. package/dist/signer/signer.d.ts +4 -3
  85. package/dist/signer/signer.js +15 -7
  86. package/dist/{signer-CylxIujU.d.ts → signer-IO3oMRNj.d.cts} +2 -1
  87. package/dist/{signer-BhLS7SYR.d.cts → signer-wqesWifN.d.ts} +2 -1
  88. package/dist/{spark-DjR1b3TC.d.cts → spark-CDm4gqS6.d.cts} +1 -1
  89. package/dist/{spark-DjR1b3TC.d.ts → spark-CDm4gqS6.d.ts} +1 -1
  90. package/dist/types/index.cjs +282 -188
  91. package/dist/types/index.d.cts +7 -6
  92. package/dist/types/index.d.ts +7 -6
  93. package/dist/types/index.js +3 -3
  94. package/dist/utils/index.cjs +90 -58
  95. package/dist/utils/index.d.cts +6 -5
  96. package/dist/utils/index.d.ts +6 -5
  97. package/dist/utils/index.js +16 -16
  98. package/ios/spark_frostFFI.xcframework/ios-arm64/SparkFrost +0 -0
  99. package/ios/spark_frostFFI.xcframework/ios-arm64/spark_frostFFI.framework/spark_frostFFI +0 -0
  100. package/ios/spark_frostFFI.xcframework/ios-arm64_x86_64-simulator/SparkFrost +0 -0
  101. package/ios/spark_frostFFI.xcframework/ios-arm64_x86_64-simulator/spark_frostFFI.framework/spark_frostFFI +0 -0
  102. package/ios/spark_frostFFI.xcframework/macos-arm64_x86_64/spark_frostFFI.framework/spark_frostFFI +0 -0
  103. package/package.json +4 -4
  104. package/src/constants.ts +21 -0
  105. package/src/errors/base.ts +43 -1
  106. package/src/graphql/client.ts +4 -0
  107. package/src/graphql/mutations/RequestLightningSend.ts +2 -0
  108. package/src/graphql/objects/ClaimStaticDepositInput.ts +1 -1
  109. package/src/graphql/objects/ClaimStaticDepositStatus.ts +4 -2
  110. package/src/graphql/objects/Connection.ts +7 -7
  111. package/src/graphql/objects/CoopExitFeeEstimate.ts +1 -1
  112. package/src/graphql/objects/CoopExitFeeQuote.ts +202 -0
  113. package/src/graphql/objects/CoopExitFeeQuoteInput.ts +41 -0
  114. package/src/graphql/objects/CoopExitFeeQuoteOutput.ts +45 -0
  115. package/src/graphql/objects/CoopExitRequest.ts +21 -0
  116. package/src/graphql/objects/CurrencyUnit.ts +26 -28
  117. package/src/graphql/objects/Entity.ts +84 -0
  118. package/src/graphql/objects/Invoice.ts +2 -2
  119. package/src/graphql/objects/Leaf.ts +1 -1
  120. package/src/graphql/objects/LeavesSwapFeeEstimateOutput.ts +1 -1
  121. package/src/graphql/objects/LeavesSwapRequest.ts +6 -0
  122. package/src/graphql/objects/LightningReceiveRequest.ts +11 -0
  123. package/src/graphql/objects/LightningSendFeeEstimateInput.ts +8 -0
  124. package/src/graphql/objects/LightningSendFeeEstimateOutput.ts +1 -1
  125. package/src/graphql/objects/LightningSendRequest.ts +3 -0
  126. package/src/graphql/objects/RequestCoopExitInput.ts +8 -0
  127. package/src/graphql/objects/RequestLeavesSwapInput.ts +5 -1
  128. package/src/graphql/objects/RequestLightningReceiveInput.ts +9 -2
  129. package/src/graphql/objects/RequestLightningSendInput.ts +8 -0
  130. package/src/graphql/objects/SparkCoopExitRequestStatus.ts +2 -0
  131. package/src/graphql/objects/SparkUserRequestType.ts +2 -0
  132. package/src/graphql/objects/SparkWalletUser.ts +20 -0
  133. package/src/graphql/objects/UserRequest.ts +32 -0
  134. package/src/graphql/objects/VerifyChallengeInput.ts +1 -1
  135. package/src/graphql/objects/index.ts +12 -3
  136. package/src/graphql/queries/LightningSendFeeEstimate.ts +2 -0
  137. package/src/logger.ts +3 -0
  138. package/src/native/index.ts +1 -0
  139. package/src/services/config.ts +4 -0
  140. package/src/services/connection.ts +68 -29
  141. package/src/services/coop-exit.ts +1 -1
  142. package/src/services/lightning.ts +25 -1
  143. package/src/services/lrc-connection.ts +3 -3
  144. package/src/services/token-transactions.ts +6 -2
  145. package/src/services/wallet-config.ts +2 -0
  146. package/src/signer/signer.ts +4 -1
  147. package/src/spark-wallet/spark-wallet.ts +51 -15
  148. package/src/spark-wallet/types.ts +1 -0
  149. package/src/tests/errors.test.ts +58 -0
  150. package/src/tests/integration/lightning.test.ts +184 -0
  151. package/src/tests/integration/ssp/static_deposit.test.ts +1 -2
  152. package/src/tests/tokens.test.ts +52 -3
  153. package/src/utils/token-hashing.ts +335 -1
  154. package/dist/LightningSendFeeEstimateInput-CJvPnCSB.d.cts +0 -5
  155. package/dist/LightningSendFeeEstimateInput-CJvPnCSB.d.ts +0 -5
  156. package/dist/chunk-HKAKEKCE.js +0 -8
@@ -1315,7 +1315,7 @@ init_buffer();
1315
1315
 
1316
1316
  // src/signer/signer.ts
1317
1317
  init_buffer();
1318
- var import_utils4 = require("@noble/curves/abstract/utils");
1318
+ var import_utils5 = require("@noble/curves/abstract/utils");
1319
1319
  var import_secp256k15 = require("@noble/curves/secp256k1");
1320
1320
  var import_bip32 = require("@scure/bip32");
1321
1321
  var import_bip39 = require("@scure/bip39");
@@ -1324,14 +1324,27 @@ var ecies = __toESM(require("eciesjs"), 1);
1324
1324
 
1325
1325
  // src/constants.ts
1326
1326
  init_buffer();
1327
+ var import_core = require("@lightsparkdev/core");
1327
1328
  var isReactNative = typeof navigator !== "undefined" && navigator.product === "ReactNative";
1328
1329
  var isBun = globalThis.Bun !== void 0;
1330
+ var packageVersion = true ? "0.1.43" : "unknown";
1331
+ var baseEnvStr = "unknown";
1332
+ if (import_core.isNode) {
1333
+ baseEnvStr = `node/${process.version}`;
1334
+ } else if (isReactNative) {
1335
+ baseEnvStr = "react-native";
1336
+ } else {
1337
+ const userAgent = typeof navigator !== "undefined" && navigator.userAgent || "unknown-user-agent";
1338
+ baseEnvStr = `browser/${userAgent}`;
1339
+ }
1340
+ var clientEnv = `js-spark-sdk/${packageVersion} ${baseEnvStr}`;
1329
1341
 
1330
1342
  // src/errors/types.ts
1331
1343
  init_buffer();
1332
1344
 
1333
1345
  // src/errors/base.ts
1334
1346
  init_buffer();
1347
+ var import_utils = require("@noble/hashes/utils");
1335
1348
  var SparkSDKError = class extends Error {
1336
1349
  context;
1337
1350
  originalError;
@@ -1363,12 +1376,42 @@ var SparkSDKError = class extends Error {
1363
1376
  }
1364
1377
  };
1365
1378
  function getMessage(message, context = {}, originalError) {
1366
- const contextStr = Object.entries(context).map(([key, value]) => `${key}: ${JSON.stringify(value)}`).join(", ");
1379
+ const contextStr = Object.entries(context).map(([key, value]) => `${key}: ${safeStringify(value)}`).join(", ");
1367
1380
  const originalErrorStr = originalError ? `
1368
1381
  Original Error: ${originalError.message}` : "";
1369
1382
  return `SparkSDKError: ${message}${contextStr ? `
1370
1383
  Context: ${contextStr}` : ""}${originalErrorStr}`;
1371
1384
  }
1385
+ function safeStringify(value) {
1386
+ const replacer = (_, v) => {
1387
+ if (typeof v === "bigint") {
1388
+ return v.toString();
1389
+ }
1390
+ if (v instanceof Uint8Array) {
1391
+ return formatUint8Array(v);
1392
+ }
1393
+ return v;
1394
+ };
1395
+ if (typeof value === "bigint") {
1396
+ return `"${value.toString()}"`;
1397
+ }
1398
+ if (value instanceof Uint8Array) {
1399
+ return `"${formatUint8Array(value)}"`;
1400
+ }
1401
+ try {
1402
+ const result = JSON.stringify(value, replacer);
1403
+ return result === void 0 ? String(value) : result;
1404
+ } catch {
1405
+ try {
1406
+ return String(value);
1407
+ } catch {
1408
+ return "[Unserializable]";
1409
+ }
1410
+ }
1411
+ }
1412
+ function formatUint8Array(arr) {
1413
+ return `Uint8Array(0x${(0, import_utils.bytesToHex)(arr)})`;
1414
+ }
1372
1415
 
1373
1416
  // src/errors/types.ts
1374
1417
  var NetworkError = class extends SparkSDKError {
@@ -1400,7 +1443,7 @@ var ConfigurationError = class extends SparkSDKError {
1400
1443
  // src/utils/adaptor-signature.ts
1401
1444
  init_buffer();
1402
1445
  var import_modular = require("@noble/curves/abstract/modular");
1403
- var import_utils = require("@noble/curves/abstract/utils");
1446
+ var import_utils2 = require("@noble/curves/abstract/utils");
1404
1447
  var import_secp256k1 = require("@noble/curves/secp256k1");
1405
1448
 
1406
1449
  // src/errors/index.ts
@@ -1410,10 +1453,10 @@ init_buffer();
1410
1453
  function generateAdaptorFromSignature(signature) {
1411
1454
  const adaptorPrivateKey = import_secp256k1.secp256k1.utils.randomPrivateKey();
1412
1455
  const { r, s } = parseSignature(signature);
1413
- const sBigInt = (0, import_utils.bytesToNumberBE)(s);
1414
- const tBigInt = (0, import_utils.bytesToNumberBE)(adaptorPrivateKey);
1456
+ const sBigInt = (0, import_utils2.bytesToNumberBE)(s);
1457
+ const tBigInt = (0, import_utils2.bytesToNumberBE)(adaptorPrivateKey);
1415
1458
  const newS = (0, import_modular.mod)(sBigInt - tBigInt, import_secp256k1.secp256k1.CURVE.n);
1416
- const newSignature = new Uint8Array([...r, ...(0, import_utils.numberToBytesBE)(newS, 32)]);
1459
+ const newSignature = new Uint8Array([...r, ...(0, import_utils2.numberToBytesBE)(newS, 32)]);
1417
1460
  return {
1418
1461
  adaptorSignature: newSignature,
1419
1462
  adaptorPrivateKey
@@ -1434,15 +1477,15 @@ function parseSignature(signature) {
1434
1477
  }
1435
1478
  const r = signature.slice(0, 32);
1436
1479
  const s = signature.slice(32, 64);
1437
- if ((0, import_utils.bytesToNumberBE)(r) >= import_secp256k1.secp256k1.CURVE.Fp.ORDER) {
1480
+ if ((0, import_utils2.bytesToNumberBE)(r) >= import_secp256k1.secp256k1.CURVE.Fp.ORDER) {
1438
1481
  throw new ValidationError("Invalid signature: r >= field prime", {
1439
- rValue: (0, import_utils.bytesToNumberBE)(r),
1482
+ rValue: (0, import_utils2.bytesToNumberBE)(r),
1440
1483
  fieldPrime: import_secp256k1.secp256k1.CURVE.Fp.ORDER
1441
1484
  });
1442
1485
  }
1443
- if ((0, import_utils.bytesToNumberBE)(s) >= import_secp256k1.secp256k1.CURVE.n) {
1486
+ if ((0, import_utils2.bytesToNumberBE)(s) >= import_secp256k1.secp256k1.CURVE.n) {
1444
1487
  throw new ValidationError("Invalid signature: s >= group order", {
1445
- sValue: (0, import_utils.bytesToNumberBE)(s),
1488
+ sValue: (0, import_utils2.bytesToNumberBE)(s),
1446
1489
  groupOrder: import_secp256k1.secp256k1.CURVE.n
1447
1490
  });
1448
1491
  }
@@ -1451,7 +1494,7 @@ function parseSignature(signature) {
1451
1494
 
1452
1495
  // src/utils/keys.ts
1453
1496
  init_buffer();
1454
- var import_utils2 = require("@noble/curves/abstract/utils");
1497
+ var import_utils3 = require("@noble/curves/abstract/utils");
1455
1498
  var import_secp256k12 = require("@noble/curves/secp256k1");
1456
1499
  function subtractPrivateKeys(a, b) {
1457
1500
  if (a.length !== 32 || b.length !== 32) {
@@ -1464,12 +1507,12 @@ function subtractPrivateKeys(a, b) {
1464
1507
  const privA = import_secp256k12.secp256k1.utils.normPrivateKeyToScalar(a);
1465
1508
  const privB = import_secp256k12.secp256k1.utils.normPrivateKeyToScalar(b);
1466
1509
  const sum = (import_secp256k12.secp256k1.CURVE.n - privB + privA) % import_secp256k12.secp256k1.CURVE.n;
1467
- return (0, import_utils2.numberToBytesBE)(sum, 32);
1510
+ return (0, import_utils3.numberToBytesBE)(sum, 32);
1468
1511
  }
1469
1512
 
1470
1513
  // src/utils/secret-sharing.ts
1471
1514
  init_buffer();
1472
- var import_utils3 = require("@noble/curves/abstract/utils");
1515
+ var import_utils4 = require("@noble/curves/abstract/utils");
1473
1516
  var import_secp256k13 = require("@noble/curves/secp256k1");
1474
1517
 
1475
1518
  // src/utils/crypto.ts
@@ -1488,7 +1531,7 @@ function getRandomBigInt(max) {
1488
1531
  const mask = (1n << BigInt(max.toString(2).length)) - 1n;
1489
1532
  while (true) {
1490
1533
  const randBytes = crypto.getRandomValues(new Uint8Array(byteLength + 1));
1491
- const randValue = BigInt("0x" + (0, import_utils3.bytesToHex)(randBytes)) & mask;
1534
+ const randValue = BigInt("0x" + (0, import_utils4.bytesToHex)(randBytes)) & mask;
1492
1535
  if (randValue < maxBigInt) {
1493
1536
  return randValue;
1494
1537
  }
@@ -1666,7 +1709,9 @@ function getSigningCommitmentFromNonce(nonce) {
1666
1709
  var import_secp256k16 = require("@bitcoinerlab/secp256k1");
1667
1710
  var import_lrc20_sdk = require("@buildonspark/lrc20-sdk");
1668
1711
  var import_sha2 = require("@noble/hashes/sha2");
1669
- var import_utils5 = require("@scure/btc-signer/utils");
1712
+ var import_utils6 = require("@scure/btc-signer/utils");
1713
+ var import_lrc20_sdk2 = require("@buildonspark/lrc20-sdk");
1714
+ var import_types2 = require("@buildonspark/lrc20-sdk/lrc/types");
1670
1715
  var sparkFrostModule = void 0;
1671
1716
  var getSparkFrostModule = async () => {
1672
1717
  if (isReactNative) {
@@ -1765,28 +1810,28 @@ var DefaultSparkSigner = class {
1765
1810
  const privateKey = this.deriveSigningKey(hash);
1766
1811
  const publicKey = import_secp256k15.secp256k1.getPublicKey(privateKey);
1767
1812
  this.publicKeyToPrivateKeyMap.set(
1768
- (0, import_utils4.bytesToHex)(publicKey),
1769
- (0, import_utils4.bytesToHex)(privateKey)
1813
+ (0, import_utils5.bytesToHex)(publicKey),
1814
+ (0, import_utils5.bytesToHex)(privateKey)
1770
1815
  );
1771
1816
  }
1772
1817
  }
1773
1818
  async getSchnorrPublicKey(publicKey) {
1774
- const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils4.bytesToHex)(publicKey));
1819
+ const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils5.bytesToHex)(publicKey));
1775
1820
  if (!privateKey) {
1776
1821
  throw new ValidationError("Private key is not set", {
1777
1822
  field: "privateKey"
1778
1823
  });
1779
1824
  }
1780
- return import_secp256k15.schnorr.getPublicKey((0, import_utils4.hexToBytes)(privateKey));
1825
+ return import_secp256k15.schnorr.getPublicKey((0, import_utils5.hexToBytes)(privateKey));
1781
1826
  }
1782
1827
  async signSchnorr(message, publicKey) {
1783
- const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils4.bytesToHex)(publicKey));
1828
+ const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils5.bytesToHex)(publicKey));
1784
1829
  if (!privateKey) {
1785
1830
  throw new ValidationError("Private key is not set", {
1786
1831
  field: "privateKey"
1787
1832
  });
1788
1833
  }
1789
- return import_secp256k15.schnorr.sign(message, (0, import_utils4.hexToBytes)(privateKey));
1834
+ return import_secp256k15.schnorr.sign(message, (0, import_utils5.hexToBytes)(privateKey));
1790
1835
  }
1791
1836
  async signSchnorrWithIdentityKey(message) {
1792
1837
  if (!this.identityKey?.privateKey) {
@@ -1828,8 +1873,8 @@ var DefaultSparkSigner = class {
1828
1873
  );
1829
1874
  this.staticDepositKeyMap.set(idx, staticDepositKey);
1830
1875
  this.publicKeyToPrivateKeyMap.set(
1831
- (0, import_utils4.bytesToHex)(staticDepositKey.publicKey),
1832
- (0, import_utils4.bytesToHex)(staticDepositKey.privateKey)
1876
+ (0, import_utils5.bytesToHex)(staticDepositKey.publicKey),
1877
+ (0, import_utils5.bytesToHex)(staticDepositKey.privateKey)
1833
1878
  );
1834
1879
  return staticDepositKey.publicKey;
1835
1880
  }
@@ -1874,7 +1919,7 @@ var DefaultSparkSigner = class {
1874
1919
  return await (0, import_bip39.mnemonicToSeed)(mnemonic);
1875
1920
  }
1876
1921
  async getTrackedPublicKeys() {
1877
- return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(import_utils4.hexToBytes);
1922
+ return Array.from(this.publicKeyToPrivateKeyMap.keys()).map(import_utils5.hexToBytes);
1878
1923
  }
1879
1924
  async generatePublicKey(hash) {
1880
1925
  if (!this.signingKey) {
@@ -1894,31 +1939,31 @@ var DefaultSparkSigner = class {
1894
1939
  });
1895
1940
  }
1896
1941
  const publicKey = import_secp256k15.secp256k1.getPublicKey(newPrivateKey);
1897
- const pubKeyHex = (0, import_utils4.bytesToHex)(publicKey);
1898
- const privKeyHex = (0, import_utils4.bytesToHex)(newPrivateKey);
1942
+ const pubKeyHex = (0, import_utils5.bytesToHex)(publicKey);
1943
+ const privKeyHex = (0, import_utils5.bytesToHex)(newPrivateKey);
1899
1944
  this.publicKeyToPrivateKeyMap.set(pubKeyHex, privKeyHex);
1900
1945
  return publicKey;
1901
1946
  }
1902
1947
  async removePublicKey(publicKey) {
1903
- this.publicKeyToPrivateKeyMap.delete((0, import_utils4.bytesToHex)(publicKey));
1948
+ this.publicKeyToPrivateKeyMap.delete((0, import_utils5.bytesToHex)(publicKey));
1904
1949
  }
1905
1950
  async subtractPrivateKeysGivenPublicKeys(first, second) {
1906
- const firstPubKeyHex = (0, import_utils4.bytesToHex)(first);
1907
- const secondPubKeyHex = (0, import_utils4.bytesToHex)(second);
1951
+ const firstPubKeyHex = (0, import_utils5.bytesToHex)(first);
1952
+ const secondPubKeyHex = (0, import_utils5.bytesToHex)(second);
1908
1953
  const firstPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(firstPubKeyHex);
1909
1954
  const secondPrivateKeyHex = this.publicKeyToPrivateKeyMap.get(secondPubKeyHex);
1910
1955
  if (!firstPrivateKeyHex || !secondPrivateKeyHex) {
1911
1956
  throw new Error("Private key is not set");
1912
1957
  }
1913
- const firstPrivateKey = (0, import_utils4.hexToBytes)(firstPrivateKeyHex);
1914
- const secondPrivateKey = (0, import_utils4.hexToBytes)(secondPrivateKeyHex);
1958
+ const firstPrivateKey = (0, import_utils5.hexToBytes)(firstPrivateKeyHex);
1959
+ const secondPrivateKey = (0, import_utils5.hexToBytes)(secondPrivateKeyHex);
1915
1960
  const resultPrivKey = subtractPrivateKeys(
1916
1961
  firstPrivateKey,
1917
1962
  secondPrivateKey
1918
1963
  );
1919
1964
  const resultPubKey = import_secp256k15.secp256k1.getPublicKey(resultPrivKey);
1920
- const resultPrivKeyHex = (0, import_utils4.bytesToHex)(resultPrivKey);
1921
- const resultPubKeyHex = (0, import_utils4.bytesToHex)(resultPubKey);
1965
+ const resultPrivKeyHex = (0, import_utils5.bytesToHex)(resultPrivKey);
1966
+ const resultPubKeyHex = (0, import_utils5.bytesToHex)(resultPubKey);
1922
1967
  this.publicKeyToPrivateKeyMap.set(resultPubKeyHex, resultPrivKeyHex);
1923
1968
  return resultPubKey;
1924
1969
  }
@@ -1930,14 +1975,14 @@ var DefaultSparkSigner = class {
1930
1975
  isSecretPubkey = false
1931
1976
  }) {
1932
1977
  if (isSecretPubkey) {
1933
- const pubKeyHex = (0, import_utils4.bytesToHex)(secret);
1978
+ const pubKeyHex = (0, import_utils5.bytesToHex)(secret);
1934
1979
  const privateKey = this.publicKeyToPrivateKeyMap.get(pubKeyHex);
1935
1980
  if (!privateKey) {
1936
1981
  throw new Error("Private key is not set");
1937
1982
  }
1938
- secret = (0, import_utils4.hexToBytes)(privateKey);
1983
+ secret = (0, import_utils5.hexToBytes)(privateKey);
1939
1984
  }
1940
- const secretAsInt = (0, import_utils4.bytesToNumberBE)(secret);
1985
+ const secretAsInt = (0, import_utils5.bytesToNumberBE)(secret);
1941
1986
  return splitSecretWithProofs(secretAsInt, curveOrder, threshold, numShares);
1942
1987
  }
1943
1988
  async signFrost({
@@ -1955,7 +2000,7 @@ var DefaultSparkSigner = class {
1955
2000
  field: "SparkFrost"
1956
2001
  });
1957
2002
  }
1958
- const privateAsPubKeyHex = (0, import_utils4.bytesToHex)(privateAsPubKey);
2003
+ const privateAsPubKeyHex = (0, import_utils5.bytesToHex)(privateAsPubKey);
1959
2004
  const signingPrivateKey = this.publicKeyToPrivateKeyMap.get(privateAsPubKeyHex);
1960
2005
  if (!signingPrivateKey) {
1961
2006
  throw new ValidationError("Private key not found for public key", {
@@ -1969,7 +2014,7 @@ var DefaultSparkSigner = class {
1969
2014
  });
1970
2015
  }
1971
2016
  const keyPackage = {
1972
- secretKey: (0, import_utils4.hexToBytes)(signingPrivateKey),
2017
+ secretKey: (0, import_utils5.hexToBytes)(signingPrivateKey),
1973
2018
  publicKey,
1974
2019
  verifyingKey
1975
2020
  };
@@ -2013,7 +2058,7 @@ var DefaultSparkSigner = class {
2013
2058
  }
2014
2059
  async createSparkWalletFromSeed(seed, accountNumber) {
2015
2060
  if (typeof seed === "string") {
2016
- seed = (0, import_utils4.hexToBytes)(seed);
2061
+ seed = (0, import_utils5.hexToBytes)(seed);
2017
2062
  }
2018
2063
  const {
2019
2064
  masterPublicKey,
@@ -2028,28 +2073,28 @@ var DefaultSparkSigner = class {
2028
2073
  this.signingKey = signingKey.hdKey;
2029
2074
  this.staticDepositKey = staticDepositKey.hdKey;
2030
2075
  this.publicKeyToPrivateKeyMap.set(
2031
- (0, import_utils4.bytesToHex)(identityKey.publicKey),
2032
- (0, import_utils4.bytesToHex)(identityKey.privateKey)
2076
+ (0, import_utils5.bytesToHex)(identityKey.publicKey),
2077
+ (0, import_utils5.bytesToHex)(identityKey.privateKey)
2033
2078
  );
2034
2079
  this.publicKeyToPrivateKeyMap.set(
2035
- (0, import_utils4.bytesToHex)(depositKey.publicKey),
2036
- (0, import_utils4.bytesToHex)(depositKey.privateKey)
2080
+ (0, import_utils5.bytesToHex)(depositKey.publicKey),
2081
+ (0, import_utils5.bytesToHex)(depositKey.privateKey)
2037
2082
  );
2038
2083
  this.publicKeyToPrivateKeyMap.set(
2039
- (0, import_utils4.bytesToHex)(staticDepositKey.publicKey),
2040
- (0, import_utils4.bytesToHex)(staticDepositKey.privateKey)
2084
+ (0, import_utils5.bytesToHex)(staticDepositKey.publicKey),
2085
+ (0, import_utils5.bytesToHex)(staticDepositKey.privateKey)
2041
2086
  );
2042
- return (0, import_utils4.bytesToHex)(identityKey.publicKey);
2087
+ return (0, import_utils5.bytesToHex)(identityKey.publicKey);
2043
2088
  }
2044
2089
  async signMessageWithPublicKey(message, publicKey, compact) {
2045
- const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils4.bytesToHex)(publicKey));
2090
+ const privateKey = this.publicKeyToPrivateKeyMap.get((0, import_utils5.bytesToHex)(publicKey));
2046
2091
  if (!privateKey) {
2047
2092
  throw new ValidationError("Private key not found for public key", {
2048
2093
  field: "privateKey",
2049
- value: (0, import_utils4.bytesToHex)(publicKey)
2094
+ value: (0, import_utils5.bytesToHex)(publicKey)
2050
2095
  });
2051
2096
  }
2052
- const signature = import_secp256k15.secp256k1.sign(message, (0, import_utils4.hexToBytes)(privateKey));
2097
+ const signature = import_secp256k15.secp256k1.sign(message, (0, import_utils5.hexToBytes)(privateKey));
2053
2098
  if (compact) {
2054
2099
  return signature.toCompactRawBytes();
2055
2100
  }
@@ -2068,12 +2113,12 @@ var DefaultSparkSigner = class {
2068
2113
  return signature.toDERRawBytes();
2069
2114
  }
2070
2115
  async encryptLeafPrivateKeyEcies(receiverPublicKey, publicKey) {
2071
- const publicKeyHex = (0, import_utils4.bytesToHex)(publicKey);
2116
+ const publicKeyHex = (0, import_utils5.bytesToHex)(publicKey);
2072
2117
  const privateKey = this.publicKeyToPrivateKeyMap.get(publicKeyHex);
2073
2118
  if (!privateKey) {
2074
2119
  throw new Error("Private key is not set");
2075
2120
  }
2076
- return ecies.encrypt(receiverPublicKey, (0, import_utils4.hexToBytes)(privateKey));
2121
+ return ecies.encrypt(receiverPublicKey, (0, import_utils5.hexToBytes)(privateKey));
2077
2122
  }
2078
2123
  async decryptEcies(ciphertext) {
2079
2124
  if (!this.identityKey?.privateKey) {
@@ -2082,12 +2127,12 @@ var DefaultSparkSigner = class {
2082
2127
  });
2083
2128
  }
2084
2129
  const receiverEciesPrivKey = ecies.PrivateKey.fromHex(
2085
- (0, import_utils4.bytesToHex)(this.identityKey.privateKey)
2130
+ (0, import_utils5.bytesToHex)(this.identityKey.privateKey)
2086
2131
  );
2087
2132
  const privateKey = ecies.decrypt(receiverEciesPrivKey.toHex(), ciphertext);
2088
2133
  const publicKey = import_secp256k15.secp256k1.getPublicKey(privateKey);
2089
- const publicKeyHex = (0, import_utils4.bytesToHex)(publicKey);
2090
- const privateKeyHex = (0, import_utils4.bytesToHex)(privateKey);
2134
+ const publicKeyHex = (0, import_utils5.bytesToHex)(publicKey);
2135
+ const privateKeyHex = (0, import_utils5.bytesToHex)(privateKey);
2091
2136
  this.publicKeyToPrivateKeyMap.set(publicKeyHex, privateKeyHex);
2092
2137
  return publicKey;
2093
2138
  }
@@ -2104,8 +2149,8 @@ var DefaultSparkSigner = class {
2104
2149
  const adaptor = generateAdaptorFromSignature(signature);
2105
2150
  const adaptorPublicKey = import_secp256k15.secp256k1.getPublicKey(adaptor.adaptorPrivateKey);
2106
2151
  this.publicKeyToPrivateKeyMap.set(
2107
- (0, import_utils4.bytesToHex)(adaptorPublicKey),
2108
- (0, import_utils4.bytesToHex)(adaptor.adaptorPrivateKey)
2152
+ (0, import_utils5.bytesToHex)(adaptorPublicKey),
2153
+ (0, import_utils5.bytesToHex)(adaptor.adaptorPrivateKey)
2109
2154
  );
2110
2155
  return {
2111
2156
  adaptorSignature: signature,
@@ -2156,19 +2201,19 @@ var DefaultSparkSigner = class {
2156
2201
  }
2157
2202
  signTransactionIndex(tx, index, publicKey) {
2158
2203
  let privateKey;
2159
- if ((0, import_utils4.equalBytes)(publicKey, this.identityKey?.publicKey ?? new Uint8Array())) {
2204
+ if ((0, import_utils5.equalBytes)(publicKey, this.identityKey?.publicKey ?? new Uint8Array())) {
2160
2205
  privateKey = this.identityKey?.privateKey;
2161
- } else if ((0, import_utils4.equalBytes)(publicKey, this.depositKey?.publicKey ?? new Uint8Array())) {
2206
+ } else if ((0, import_utils5.equalBytes)(publicKey, this.depositKey?.publicKey ?? new Uint8Array())) {
2162
2207
  privateKey = this.depositKey?.privateKey;
2163
2208
  } else {
2164
- privateKey = (0, import_utils4.hexToBytes)(
2165
- this.publicKeyToPrivateKeyMap.get((0, import_utils4.bytesToHex)(publicKey)) ?? ""
2209
+ privateKey = (0, import_utils5.hexToBytes)(
2210
+ this.publicKeyToPrivateKeyMap.get((0, import_utils5.bytesToHex)(publicKey)) ?? ""
2166
2211
  );
2167
2212
  }
2168
2213
  if (!privateKey) {
2169
2214
  throw new ValidationError("Private key not found for public key", {
2170
2215
  field: "privateKey",
2171
- value: (0, import_utils4.bytesToHex)(publicKey)
2216
+ value: (0, import_utils5.bytesToHex)(publicKey)
2172
2217
  });
2173
2218
  }
2174
2219
  tx.signIdx(privateKey, index);
@@ -2177,7 +2222,7 @@ var DefaultSparkSigner = class {
2177
2222
 
2178
2223
  // src/utils/network.ts
2179
2224
  init_buffer();
2180
- var import_lrc20_sdk2 = require("@buildonspark/lrc20-sdk");
2225
+ var import_lrc20_sdk3 = require("@buildonspark/lrc20-sdk");
2181
2226
  var btc = __toESM(require("@scure/btc-signer"), 1);
2182
2227
  var bitcoin = __toESM(require("bitcoinjs-lib"), 1);
2183
2228
 
@@ -15282,11 +15327,11 @@ var LRC_WALLET_NETWORK = Object.freeze({
15282
15327
  [4 /* LOCAL */]: bitcoin.networks.regtest
15283
15328
  });
15284
15329
  var LRC_WALLET_NETWORK_TYPE = Object.freeze({
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
15330
+ [0 /* MAINNET */]: import_lrc20_sdk3.NetworkType.MAINNET,
15331
+ [1 /* TESTNET */]: import_lrc20_sdk3.NetworkType.TESTNET,
15332
+ [2 /* SIGNET */]: import_lrc20_sdk3.NetworkType.TESTNET,
15333
+ [3 /* REGTEST */]: import_lrc20_sdk3.NetworkType.REGTEST,
15334
+ [4 /* LOCAL */]: import_lrc20_sdk3.NetworkType.LOCAL
15290
15335
  });
15291
15336
 
15292
15337
  // src/services/wallet-config.ts
@@ -15294,10 +15339,10 @@ init_buffer();
15294
15339
 
15295
15340
  // src/tests/isHermeticTest.ts
15296
15341
  init_buffer();
15297
- var import_core = require("@lightsparkdev/core");
15342
+ var import_core2 = require("@lightsparkdev/core");
15298
15343
  var import_fs = __toESM(require("fs"), 1);
15299
15344
  function isHermeticTest() {
15300
- if (import_core.isNode) {
15345
+ if (import_core2.isNode) {
15301
15346
  return (import_fs.default?.existsSync?.("/tmp/spark_hermetic") ?? false) || process.env.HERMETIC_TEST === "true";
15302
15347
  }
15303
15348
  return typeof process !== "undefined" && process.env?.HERMETIC_TEST === "true" || false;
@@ -15425,6 +15470,7 @@ var BASE_CONFIG = {
15425
15470
  signingOperators: getLocalSigningOperators(),
15426
15471
  tokenSignatures: "SCHNORR",
15427
15472
  tokenTransactionVersion: "V0",
15473
+ tokenValidityDurationSeconds: 180,
15428
15474
  electrsUrl: getElectrsUrl("LOCAL"),
15429
15475
  expectedWithdrawBondSats: 1e4,
15430
15476
  expectedWithdrawRelativeBlockLocktime: 1e3,
@@ -15635,6 +15681,9 @@ var WalletConfigService = class {
15635
15681
  getTokenTransactionVersion() {
15636
15682
  return this.config.tokenTransactionVersion;
15637
15683
  }
15684
+ getTokenValidityDurationSeconds() {
15685
+ return this.config.tokenValidityDurationSeconds;
15686
+ }
15638
15687
  getElectrsUrl() {
15639
15688
  return this.config.electrsUrl;
15640
15689
  }
@@ -15645,19 +15694,290 @@ var WalletConfigService = class {
15645
15694
 
15646
15695
  // src/services/token-transactions.ts
15647
15696
  init_buffer();
15648
- var import_utils9 = require("@noble/curves/abstract/utils");
15697
+ var import_utils10 = require("@noble/curves/abstract/utils");
15649
15698
  var import_secp256k19 = require("@noble/curves/secp256k1");
15650
15699
 
15651
15700
  // src/utils/token-hashing.ts
15652
15701
  init_buffer();
15653
15702
  var import_sha22 = require("@noble/hashes/sha2");
15654
15703
  function hashTokenTransaction(tokenTransaction, partialHash = false) {
15704
+ switch (tokenTransaction.version) {
15705
+ case 0:
15706
+ return hashTokenTransactionV0(tokenTransaction, partialHash);
15707
+ case 1:
15708
+ return hashTokenTransactionV1(tokenTransaction, partialHash);
15709
+ default:
15710
+ throw new ValidationError("invalid token transaction version", {
15711
+ field: "tokenTransaction.version",
15712
+ value: tokenTransaction.version
15713
+ });
15714
+ }
15715
+ }
15716
+ function hashTokenTransactionV0(tokenTransaction, partialHash = false) {
15717
+ if (!tokenTransaction) {
15718
+ throw new ValidationError("token transaction cannot be nil", {
15719
+ field: "tokenTransaction"
15720
+ });
15721
+ }
15722
+ let allHashes = [];
15723
+ if (tokenTransaction.tokenInputs?.$case === "transferInput") {
15724
+ if (!tokenTransaction.tokenInputs.transferInput.outputsToSpend) {
15725
+ throw new ValidationError("outputs to spend cannot be null", {
15726
+ field: "tokenInputs.transferInput.outputsToSpend"
15727
+ });
15728
+ }
15729
+ if (tokenTransaction.tokenInputs.transferInput.outputsToSpend.length === 0) {
15730
+ throw new ValidationError("outputs to spend cannot be empty", {
15731
+ field: "tokenInputs.transferInput.outputsToSpend"
15732
+ });
15733
+ }
15734
+ for (const [
15735
+ i,
15736
+ output
15737
+ ] of tokenTransaction.tokenInputs.transferInput.outputsToSpend.entries()) {
15738
+ if (!output) {
15739
+ throw new ValidationError(`output cannot be null at index ${i}`, {
15740
+ field: `tokenInputs.transferInput.outputsToSpend[${i}]`,
15741
+ index: i
15742
+ });
15743
+ }
15744
+ const hashObj2 = import_sha22.sha256.create();
15745
+ if (output.prevTokenTransactionHash) {
15746
+ const prevHash = output.prevTokenTransactionHash;
15747
+ if (output.prevTokenTransactionHash.length !== 32) {
15748
+ throw new ValidationError(
15749
+ `invalid previous transaction hash length at index ${i}`,
15750
+ {
15751
+ field: `tokenInputs.transferInput.outputsToSpend[${i}].prevTokenTransactionHash`,
15752
+ value: prevHash,
15753
+ expectedLength: 32,
15754
+ actualLength: prevHash.length,
15755
+ index: i
15756
+ }
15757
+ );
15758
+ }
15759
+ hashObj2.update(output.prevTokenTransactionHash);
15760
+ }
15761
+ const voutBytes = new Uint8Array(4);
15762
+ new DataView(voutBytes.buffer).setUint32(
15763
+ 0,
15764
+ output.prevTokenTransactionVout,
15765
+ false
15766
+ );
15767
+ hashObj2.update(voutBytes);
15768
+ allHashes.push(hashObj2.digest());
15769
+ }
15770
+ }
15771
+ if (tokenTransaction.tokenInputs?.$case === "mintInput") {
15772
+ const hashObj2 = import_sha22.sha256.create();
15773
+ if (tokenTransaction.tokenInputs.mintInput.issuerPublicKey) {
15774
+ const issuerPubKey = tokenTransaction.tokenInputs.mintInput.issuerPublicKey;
15775
+ if (issuerPubKey.length === 0) {
15776
+ throw new ValidationError("issuer public key cannot be empty", {
15777
+ field: "tokenInputs.mintInput.issuerPublicKey",
15778
+ value: issuerPubKey,
15779
+ expectedLength: 1,
15780
+ actualLength: 0
15781
+ });
15782
+ }
15783
+ hashObj2.update(issuerPubKey);
15784
+ if (tokenTransaction.tokenInputs.mintInput.issuerProvidedTimestamp != 0) {
15785
+ const timestampBytes = new Uint8Array(8);
15786
+ new DataView(timestampBytes.buffer).setBigUint64(
15787
+ 0,
15788
+ BigInt(
15789
+ tokenTransaction.tokenInputs.mintInput.issuerProvidedTimestamp
15790
+ ),
15791
+ true
15792
+ // true for little-endian to match Go implementation
15793
+ );
15794
+ hashObj2.update(timestampBytes);
15795
+ }
15796
+ allHashes.push(hashObj2.digest());
15797
+ }
15798
+ }
15799
+ if (!tokenTransaction.tokenOutputs) {
15800
+ throw new ValidationError("token outputs cannot be null", {
15801
+ field: "tokenOutputs"
15802
+ });
15803
+ }
15804
+ if (tokenTransaction.tokenOutputs.length === 0) {
15805
+ throw new ValidationError("token outputs cannot be empty", {
15806
+ field: "tokenOutputs"
15807
+ });
15808
+ }
15809
+ for (const [i, output] of tokenTransaction.tokenOutputs.entries()) {
15810
+ if (!output) {
15811
+ throw new ValidationError(`output cannot be null at index ${i}`, {
15812
+ field: `tokenOutputs[${i}]`,
15813
+ index: i
15814
+ });
15815
+ }
15816
+ const hashObj2 = import_sha22.sha256.create();
15817
+ if (output.id && !partialHash) {
15818
+ if (output.id.length === 0) {
15819
+ throw new ValidationError(`output ID at index ${i} cannot be empty`, {
15820
+ field: `tokenOutputs[${i}].id`,
15821
+ index: i
15822
+ });
15823
+ }
15824
+ hashObj2.update(new TextEncoder().encode(output.id));
15825
+ }
15826
+ if (output.ownerPublicKey) {
15827
+ if (output.ownerPublicKey.length === 0) {
15828
+ throw new ValidationError(
15829
+ `owner public key at index ${i} cannot be empty`,
15830
+ {
15831
+ field: `tokenOutputs[${i}].ownerPublicKey`,
15832
+ index: i
15833
+ }
15834
+ );
15835
+ }
15836
+ hashObj2.update(output.ownerPublicKey);
15837
+ }
15838
+ if (!partialHash) {
15839
+ const revPubKey = output.revocationCommitment;
15840
+ if (revPubKey) {
15841
+ if (revPubKey.length === 0) {
15842
+ throw new ValidationError(
15843
+ `revocation commitment at index ${i} cannot be empty`,
15844
+ {
15845
+ field: `tokenOutputs[${i}].revocationCommitment`,
15846
+ index: i
15847
+ }
15848
+ );
15849
+ }
15850
+ hashObj2.update(revPubKey);
15851
+ }
15852
+ const bondBytes = new Uint8Array(8);
15853
+ new DataView(bondBytes.buffer).setBigUint64(
15854
+ 0,
15855
+ BigInt(output.withdrawBondSats),
15856
+ false
15857
+ );
15858
+ hashObj2.update(bondBytes);
15859
+ const locktimeBytes = new Uint8Array(8);
15860
+ new DataView(locktimeBytes.buffer).setBigUint64(
15861
+ 0,
15862
+ BigInt(output.withdrawRelativeBlockLocktime),
15863
+ false
15864
+ );
15865
+ hashObj2.update(locktimeBytes);
15866
+ }
15867
+ if (output.tokenPublicKey) {
15868
+ if (output.tokenPublicKey.length === 0) {
15869
+ throw new ValidationError(
15870
+ `token public key at index ${i} cannot be empty`,
15871
+ {
15872
+ field: `tokenOutputs[${i}].tokenPublicKey`,
15873
+ index: i
15874
+ }
15875
+ );
15876
+ }
15877
+ hashObj2.update(output.tokenPublicKey);
15878
+ }
15879
+ if (output.tokenAmount) {
15880
+ if (output.tokenAmount.length === 0) {
15881
+ throw new ValidationError(
15882
+ `token amount at index ${i} cannot be empty`,
15883
+ {
15884
+ field: `tokenOutputs[${i}].tokenAmount`,
15885
+ index: i
15886
+ }
15887
+ );
15888
+ }
15889
+ if (output.tokenAmount.length > 16) {
15890
+ throw new ValidationError(
15891
+ `token amount at index ${i} exceeds maximum length`,
15892
+ {
15893
+ field: `tokenOutputs[${i}].tokenAmount`,
15894
+ value: output.tokenAmount,
15895
+ expectedLength: 16,
15896
+ actualLength: output.tokenAmount.length,
15897
+ index: i
15898
+ }
15899
+ );
15900
+ }
15901
+ hashObj2.update(output.tokenAmount);
15902
+ }
15903
+ allHashes.push(hashObj2.digest());
15904
+ }
15905
+ if (!tokenTransaction.sparkOperatorIdentityPublicKeys) {
15906
+ throw new ValidationError(
15907
+ "spark operator identity public keys cannot be null",
15908
+ {}
15909
+ );
15910
+ }
15911
+ const sortedPubKeys = [
15912
+ ...tokenTransaction.sparkOperatorIdentityPublicKeys || []
15913
+ ].sort((a, b) => {
15914
+ for (let i = 0; i < a.length && i < b.length; i++) {
15915
+ if (a[i] !== b[i]) return a[i] - b[i];
15916
+ }
15917
+ return a.length - b.length;
15918
+ });
15919
+ for (const [i, pubKey] of sortedPubKeys.entries()) {
15920
+ if (!pubKey) {
15921
+ throw new ValidationError(
15922
+ `operator public key at index ${i} cannot be null`,
15923
+ {
15924
+ field: `sparkOperatorIdentityPublicKeys[${i}]`,
15925
+ index: i
15926
+ }
15927
+ );
15928
+ }
15929
+ if (pubKey.length === 0) {
15930
+ throw new ValidationError(
15931
+ `operator public key at index ${i} cannot be empty`,
15932
+ {
15933
+ field: `sparkOperatorIdentityPublicKeys[${i}]`,
15934
+ index: i
15935
+ }
15936
+ );
15937
+ }
15938
+ const hashObj2 = import_sha22.sha256.create();
15939
+ hashObj2.update(pubKey);
15940
+ allHashes.push(hashObj2.digest());
15941
+ }
15942
+ const hashObj = import_sha22.sha256.create();
15943
+ let networkBytes = new Uint8Array(4);
15944
+ new DataView(networkBytes.buffer).setUint32(
15945
+ 0,
15946
+ tokenTransaction.network.valueOf(),
15947
+ false
15948
+ // false for big-endian
15949
+ );
15950
+ hashObj.update(networkBytes);
15951
+ allHashes.push(hashObj.digest());
15952
+ const finalHashObj = import_sha22.sha256.create();
15953
+ const concatenatedHashes = new Uint8Array(
15954
+ allHashes.reduce((sum, hash) => sum + hash.length, 0)
15955
+ );
15956
+ let offset = 0;
15957
+ for (const hash of allHashes) {
15958
+ concatenatedHashes.set(hash, offset);
15959
+ offset += hash.length;
15960
+ }
15961
+ finalHashObj.update(concatenatedHashes);
15962
+ return finalHashObj.digest();
15963
+ }
15964
+ function hashTokenTransactionV1(tokenTransaction, partialHash = false) {
15655
15965
  if (!tokenTransaction) {
15656
15966
  throw new ValidationError("token transaction cannot be nil", {
15657
15967
  field: "tokenTransaction"
15658
15968
  });
15659
15969
  }
15660
15970
  let allHashes = [];
15971
+ const versionHashObj = import_sha22.sha256.create();
15972
+ const versionBytes = new Uint8Array(4);
15973
+ new DataView(versionBytes.buffer).setUint32(
15974
+ 0,
15975
+ tokenTransaction.version,
15976
+ false
15977
+ // false for big-endian
15978
+ );
15979
+ versionHashObj.update(versionBytes);
15980
+ allHashes.push(versionHashObj.digest());
15661
15981
  if (tokenTransaction.tokenInputs?.$case === "transferInput") {
15662
15982
  if (!tokenTransaction.tokenInputs.transferInput.outputsToSpend) {
15663
15983
  throw new ValidationError("outputs to spend cannot be null", {
@@ -15887,6 +16207,17 @@ function hashTokenTransaction(tokenTransaction, partialHash = false) {
15887
16207
  );
15888
16208
  hashObj.update(networkBytes);
15889
16209
  allHashes.push(hashObj.digest());
16210
+ const expiryHashObj = import_sha22.sha256.create();
16211
+ const validityDurationBytes = new Uint8Array(8);
16212
+ const expiryUnixTime = tokenTransaction.expiryTime ? Math.floor(tokenTransaction.expiryTime.getTime() / 1e3) : 0;
16213
+ new DataView(validityDurationBytes.buffer).setBigUint64(
16214
+ 0,
16215
+ BigInt(expiryUnixTime),
16216
+ false
16217
+ // false for big-endian
16218
+ );
16219
+ expiryHashObj.update(validityDurationBytes);
16220
+ allHashes.push(expiryHashObj.digest());
15890
16221
  const finalHashObj = import_sha22.sha256.create();
15891
16222
  const concatenatedHashes = new Uint8Array(
15892
16223
  allHashes.reduce((sum, hash) => sum + hash.length, 0)
@@ -15950,15 +16281,15 @@ function hashOperatorSpecificTokenTransactionSignablePayload(payload) {
15950
16281
 
15951
16282
  // src/utils/token-transactions.ts
15952
16283
  init_buffer();
15953
- var import_utils6 = require("@noble/curves/abstract/utils");
16284
+ var import_utils7 = require("@noble/curves/abstract/utils");
15954
16285
  function calculateAvailableTokenAmount(outputLeaves) {
15955
16286
  return outputLeaves.reduce(
15956
- (sum, output) => sum + BigInt((0, import_utils6.bytesToNumberBE)(output.output.tokenAmount)),
16287
+ (sum, output) => sum + BigInt((0, import_utils7.bytesToNumberBE)(output.output.tokenAmount)),
15957
16288
  BigInt(0)
15958
16289
  );
15959
16290
  }
15960
16291
  function checkIfSelectedOutputsAreAvailable(selectedOutputs, tokenOutputs, tokenPublicKey) {
15961
- const tokenPubKeyHex = (0, import_utils6.bytesToHex)(tokenPublicKey);
16292
+ const tokenPubKeyHex = (0, import_utils7.bytesToHex)(tokenPublicKey);
15962
16293
  const tokenOutputsAvailable = tokenOutputs.get(tokenPubKeyHex);
15963
16294
  if (!tokenOutputsAvailable) {
15964
16295
  return false;
@@ -16234,7 +16565,7 @@ function validateTokenTransaction(finalTokenTransaction, partialTokenTransaction
16234
16565
  }
16235
16566
 
16236
16567
  // src/services/token-transactions.ts
16237
- var import_utils10 = require("@noble/hashes/utils");
16568
+ var import_utils11 = require("@noble/hashes/utils");
16238
16569
 
16239
16570
  // src/address/index.ts
16240
16571
  init_buffer();
@@ -16242,10 +16573,10 @@ init_buffer();
16242
16573
  // src/address/address.ts
16243
16574
  init_buffer();
16244
16575
  var import_secp256k17 = require("@noble/curves/secp256k1");
16245
- var import_utils7 = require("@noble/hashes/utils");
16576
+ var import_utils8 = require("@noble/hashes/utils");
16246
16577
  var import_base2 = require("@scure/base");
16247
16578
  var import_uuidv7 = require("uuidv7");
16248
- var import_utils8 = require("@noble/curves/abstract/utils");
16579
+ var import_utils9 = require("@noble/curves/abstract/utils");
16249
16580
  var AddressNetwork = {
16250
16581
  MAINNET: "sp",
16251
16582
  TESTNET: "spt",
@@ -16264,7 +16595,7 @@ function decodeSparkAddress(address2, network) {
16264
16595
  });
16265
16596
  }
16266
16597
  const payload = SparkAddress.decode(import_base2.bech32m.fromWords(decoded.words));
16267
- const publicKey = (0, import_utils7.bytesToHex)(payload.identityPublicKey);
16598
+ const publicKey = (0, import_utils8.bytesToHex)(payload.identityPublicKey);
16268
16599
  isValidPublicKey(publicKey);
16269
16600
  const paymentIntentFields = payload.paymentIntentFields;
16270
16601
  return {
@@ -16272,8 +16603,8 @@ function decodeSparkAddress(address2, network) {
16272
16603
  network,
16273
16604
  paymentIntentFields: paymentIntentFields && {
16274
16605
  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),
16606
+ assetIdentifier: paymentIntentFields.assetIdentifier ? (0, import_utils8.bytesToHex)(paymentIntentFields.assetIdentifier) : void 0,
16607
+ assetAmount: (0, import_utils9.bytesToNumberBE)(paymentIntentFields.assetAmount),
16277
16608
  memo: paymentIntentFields.memo
16278
16609
  }
16279
16610
  };
@@ -16374,7 +16705,7 @@ var TokenTransactionService = class {
16374
16705
  if (!checkIfSelectedOutputsAreAvailable(
16375
16706
  outputsToUse,
16376
16707
  tokenOutputs,
16377
- (0, import_utils10.hexToBytes)(receiverOutputs[0].tokenPublicKey)
16708
+ (0, import_utils11.hexToBytes)(receiverOutputs[0].tokenPublicKey)
16378
16709
  )) {
16379
16710
  throw new ValidationError(
16380
16711
  "One or more selected TTXOs are not available",
@@ -16415,8 +16746,8 @@ var TokenTransactionService = class {
16415
16746
  this.config.getNetworkType()
16416
16747
  );
16417
16748
  return {
16418
- receiverSparkAddress: (0, import_utils10.hexToBytes)(receiverAddress.identityPublicKey),
16419
- tokenPublicKey: (0, import_utils10.hexToBytes)(transfer.tokenPublicKey),
16749
+ receiverSparkAddress: (0, import_utils11.hexToBytes)(receiverAddress.identityPublicKey),
16750
+ tokenPublicKey: (0, import_utils11.hexToBytes)(transfer.tokenPublicKey),
16420
16751
  tokenAmount: transfer.tokenAmount
16421
16752
  };
16422
16753
  });
@@ -16448,7 +16779,7 @@ var TokenTransactionService = class {
16448
16779
  const tokenOutputs = tokenOutputData.map((output) => ({
16449
16780
  ownerPublicKey: output.receiverSparkAddress,
16450
16781
  tokenPublicKey: output.tokenPublicKey,
16451
- tokenAmount: (0, import_utils9.numberToBytesBE)(output.tokenAmount, 16)
16782
+ tokenAmount: (0, import_utils10.numberToBytesBE)(output.tokenAmount, 16)
16452
16783
  }));
16453
16784
  if (availableTokenAmount > totalRequestedAmount) {
16454
16785
  const changeAmount = availableTokenAmount - totalRequestedAmount;
@@ -16456,7 +16787,7 @@ var TokenTransactionService = class {
16456
16787
  tokenOutputs.push({
16457
16788
  ownerPublicKey: await this.config.signer.getIdentityPublicKey(),
16458
16789
  tokenPublicKey: firstTokenPublicKey,
16459
- tokenAmount: (0, import_utils9.numberToBytesBE)(changeAmount, 16)
16790
+ tokenAmount: (0, import_utils10.numberToBytesBE)(changeAmount, 16)
16460
16791
  });
16461
16792
  }
16462
16793
  return {
@@ -16483,7 +16814,7 @@ var TokenTransactionService = class {
16483
16814
  const tokenOutputs = tokenOutputData.map((output) => ({
16484
16815
  ownerPublicKey: output.receiverSparkAddress,
16485
16816
  tokenPublicKey: output.tokenPublicKey,
16486
- tokenAmount: (0, import_utils9.numberToBytesBE)(output.tokenAmount, 16)
16817
+ tokenAmount: (0, import_utils10.numberToBytesBE)(output.tokenAmount, 16)
16487
16818
  }));
16488
16819
  if (availableTokenAmount > totalRequestedAmount) {
16489
16820
  const changeAmount = availableTokenAmount - totalRequestedAmount;
@@ -16491,7 +16822,7 @@ var TokenTransactionService = class {
16491
16822
  tokenOutputs.push({
16492
16823
  ownerPublicKey: await this.config.signer.getIdentityPublicKey(),
16493
16824
  tokenPublicKey: firstTokenPublicKey,
16494
- tokenAmount: (0, import_utils9.numberToBytesBE)(changeAmount, 16)
16825
+ tokenAmount: (0, import_utils10.numberToBytesBE)(changeAmount, 16)
16495
16826
  });
16496
16827
  }
16497
16828
  return {
@@ -16516,7 +16847,7 @@ var TokenTransactionService = class {
16516
16847
  for (const [_, operator] of Object.entries(
16517
16848
  this.config.getSigningOperators()
16518
16849
  )) {
16519
- operatorKeys.push((0, import_utils10.hexToBytes)(operator.identityPublicKey));
16850
+ operatorKeys.push((0, import_utils11.hexToBytes)(operator.identityPublicKey));
16520
16851
  }
16521
16852
  return operatorKeys;
16522
16853
  }
@@ -16600,7 +16931,7 @@ var TokenTransactionService = class {
16600
16931
  {
16601
16932
  field: "revocationCommitment",
16602
16933
  value: derivedRevocationCommitment,
16603
- expected: (0, import_utils9.bytesToHex)(outputsToSpendCommitments[outputIndex]),
16934
+ expected: (0, import_utils10.bytesToHex)(outputsToSpendCommitments[outputIndex]),
16604
16935
  outputIndex
16605
16936
  }
16606
16937
  )
@@ -16626,7 +16957,7 @@ var TokenTransactionService = class {
16626
16957
  threshold
16627
16958
  );
16628
16959
  }
16629
- return (0, import_utils9.bytesToHex)(finalTokenTransactionHash);
16960
+ return (0, import_utils10.bytesToHex)(finalTokenTransactionHash);
16630
16961
  }
16631
16962
  async broadcastTokenTransactionV1(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16632
16963
  const { finalTokenTransaction, finalTokenTransactionHash, threshold } = await this.startTokenTransaction(
@@ -16640,13 +16971,13 @@ var TokenTransactionService = class {
16640
16971
  finalTokenTransactionHash,
16641
16972
  signingOperators
16642
16973
  );
16643
- return (0, import_utils9.bytesToHex)(finalTokenTransactionHash);
16974
+ return (0, import_utils10.bytesToHex)(finalTokenTransactionHash);
16644
16975
  }
16645
16976
  async startTokenTransactionV0(tokenTransaction, signingOperators, outputsToSpendSigningPublicKeys, outputsToSpendCommitments) {
16646
16977
  const sparkClient = await this.connectionManager.createSparkClient(
16647
16978
  this.config.getCoordinatorAddress()
16648
16979
  );
16649
- const partialTokenTransactionHash = hashTokenTransaction(
16980
+ const partialTokenTransactionHash = hashTokenTransactionV0(
16650
16981
  tokenTransaction,
16651
16982
  true
16652
16983
  );
@@ -16727,7 +17058,7 @@ var TokenTransactionService = class {
16727
17058
  this.config.getThreshold()
16728
17059
  );
16729
17060
  const finalTokenTransaction = startResponse.finalTokenTransaction;
16730
- const finalTokenTransactionHash = hashTokenTransaction(
17061
+ const finalTokenTransactionHash = hashTokenTransactionV0(
16731
17062
  finalTokenTransaction,
16732
17063
  false
16733
17064
  );
@@ -16795,7 +17126,9 @@ var TokenTransactionService = class {
16795
17126
  const startResponse = await sparkClient.start_transaction(
16796
17127
  {
16797
17128
  identityPublicKey: await this.config.signer.getIdentityPublicKey(),
16798
- partialTokenTransaction: tokenTransaction
17129
+ partialTokenTransaction: tokenTransaction,
17130
+ validityDurationSeconds: await this.config.getTokenValidityDurationSeconds(),
17131
+ partialTokenTransactionOwnerSignatures: ownerSignaturesWithIndex
16799
17132
  },
16800
17133
  {
16801
17134
  retry: true,
@@ -16837,7 +17170,7 @@ var TokenTransactionService = class {
16837
17170
  const identityPublicKey = await this.config.signer.getIdentityPublicKey();
16838
17171
  const payload = {
16839
17172
  finalTokenTransactionHash,
16840
- operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17173
+ operatorIdentityPublicKey: (0, import_utils11.hexToBytes)(operator.identityPublicKey)
16841
17174
  };
16842
17175
  const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
16843
17176
  let operatorSpecificSignatures = [];
@@ -16994,7 +17327,7 @@ var TokenTransactionService = class {
16994
17327
  []
16995
17328
  );
16996
17329
  unsortedTokenOutputs.forEach((output) => {
16997
- const tokenKey = (0, import_utils9.bytesToHex)(output.output.tokenPublicKey);
17330
+ const tokenKey = (0, import_utils10.bytesToHex)(output.output.tokenPublicKey);
16998
17331
  const index = output.previousTransactionVout;
16999
17332
  tokenOutputs.set(tokenKey, [
17000
17333
  { ...output, previousTransactionVout: index }
@@ -17010,7 +17343,7 @@ var TokenTransactionService = class {
17010
17343
  });
17011
17344
  }
17012
17345
  const exactMatch = tokenOutputs.find(
17013
- (item) => (0, import_utils9.bytesToNumberBE)(item.output.tokenAmount) === tokenAmount
17346
+ (item) => (0, import_utils10.bytesToNumberBE)(item.output.tokenAmount) === tokenAmount
17014
17347
  );
17015
17348
  if (exactMatch) {
17016
17349
  return [exactMatch];
@@ -17021,7 +17354,7 @@ var TokenTransactionService = class {
17021
17354
  for (const outputWithPreviousTransactionData of tokenOutputs) {
17022
17355
  if (remainingAmount <= 0n) break;
17023
17356
  selectedOutputs.push(outputWithPreviousTransactionData);
17024
- remainingAmount -= (0, import_utils9.bytesToNumberBE)(
17357
+ remainingAmount -= (0, import_utils10.bytesToNumberBE)(
17025
17358
  outputWithPreviousTransactionData.output.tokenAmount
17026
17359
  );
17027
17360
  }
@@ -17037,13 +17370,13 @@ var TokenTransactionService = class {
17037
17370
  if (strategy === "SMALL_FIRST") {
17038
17371
  tokenOutputs.sort((a, b) => {
17039
17372
  return Number(
17040
- (0, import_utils9.bytesToNumberBE)(a.output.tokenAmount) - (0, import_utils9.bytesToNumberBE)(b.output.tokenAmount)
17373
+ (0, import_utils10.bytesToNumberBE)(a.output.tokenAmount) - (0, import_utils10.bytesToNumberBE)(b.output.tokenAmount)
17041
17374
  );
17042
17375
  });
17043
17376
  } else {
17044
17377
  tokenOutputs.sort((a, b) => {
17045
17378
  return Number(
17046
- (0, import_utils9.bytesToNumberBE)(b.output.tokenAmount) - (0, import_utils9.bytesToNumberBE)(a.output.tokenAmount)
17379
+ (0, import_utils10.bytesToNumberBE)(b.output.tokenAmount) - (0, import_utils10.bytesToNumberBE)(a.output.tokenAmount)
17047
17380
  );
17048
17381
  });
17049
17382
  }
@@ -17051,7 +17384,7 @@ var TokenTransactionService = class {
17051
17384
  // Helper function for deciding if the signer public key is the identity public key
17052
17385
  async signMessageWithKey(message, publicKey) {
17053
17386
  const tokenSignatures = this.config.getTokenSignatures();
17054
- if ((0, import_utils9.bytesToHex)(publicKey) === (0, import_utils9.bytesToHex)(await this.config.signer.getIdentityPublicKey())) {
17387
+ if ((0, import_utils10.bytesToHex)(publicKey) === (0, import_utils10.bytesToHex)(await this.config.signer.getIdentityPublicKey())) {
17055
17388
  if (tokenSignatures === "SCHNORR") {
17056
17389
  return await this.config.signer.signSchnorrWithIdentityKey(message);
17057
17390
  } else {
@@ -17127,7 +17460,7 @@ var TokenTransactionService = class {
17127
17460
  }
17128
17461
  const payload = {
17129
17462
  finalTokenTransactionHash,
17130
- operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17463
+ operatorIdentityPublicKey: (0, import_utils11.hexToBytes)(operator.identityPublicKey)
17131
17464
  };
17132
17465
  const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
17133
17466
  const ownerSignature = await this.signMessageWithKey(
@@ -17143,7 +17476,7 @@ var TokenTransactionService = class {
17143
17476
  for (let i = 0; i < transferInput.outputsToSpend.length; i++) {
17144
17477
  const payload = {
17145
17478
  finalTokenTransactionHash,
17146
- operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17479
+ operatorIdentityPublicKey: (0, import_utils11.hexToBytes)(operator.identityPublicKey)
17147
17480
  };
17148
17481
  const payloadHash = await hashOperatorSpecificTokenTransactionSignablePayload(payload);
17149
17482
  let ownerSignature;
@@ -17160,7 +17493,7 @@ var TokenTransactionService = class {
17160
17493
  }
17161
17494
  inputTtxoSignaturesPerOperator.push({
17162
17495
  ttxoSignatures,
17163
- operatorIdentityPublicKey: (0, import_utils10.hexToBytes)(operator.identityPublicKey)
17496
+ operatorIdentityPublicKey: (0, import_utils11.hexToBytes)(operator.identityPublicKey)
17164
17497
  });
17165
17498
  }
17166
17499
  return inputTtxoSignaturesPerOperator;
@@ -17172,7 +17505,7 @@ function isTokenTransaction(tokenTransaction) {
17172
17505
 
17173
17506
  // src/services/connection.ts
17174
17507
  init_buffer();
17175
- var import_core2 = require("@lightsparkdev/core");
17508
+ var import_core3 = require("@lightsparkdev/core");
17176
17509
  var import_sha23 = require("@noble/hashes/sha2");
17177
17510
  var import_nice_grpc_client_middleware_retry = require("nice-grpc-client-middleware-retry");
17178
17511
  var import_nice_grpc_common2 = require("nice-grpc-common");
@@ -19021,7 +19354,7 @@ var ConnectionManager = class {
19021
19354
  async createMockClient(address2) {
19022
19355
  const channel = await this.createChannelWithTLS(address2);
19023
19356
  const isNodeChannel = "close" in channel;
19024
- if (import_core2.isNode && isNodeChannel && !isBun) {
19357
+ if (import_core3.isNode && isNodeChannel && !isBun) {
19025
19358
  const grpcModule = await import("nice-grpc");
19026
19359
  const { createClient } = "default" in grpcModule ? grpcModule.default : grpcModule;
19027
19360
  const client = createClient(MockServiceDefinition, channel);
@@ -19038,7 +19371,7 @@ var ConnectionManager = class {
19038
19371
  }
19039
19372
  async createChannelWithTLS(address2, certPath) {
19040
19373
  try {
19041
- if (import_core2.isNode && !isBun) {
19374
+ if (import_core3.isNode && !isBun) {
19042
19375
  const grpcModule = await import("nice-grpc");
19043
19376
  const { ChannelCredentials, createChannel } = "default" in grpcModule ? grpcModule.default : grpcModule;
19044
19377
  if (certPath) {
@@ -19179,14 +19512,38 @@ var ConnectionManager = class {
19179
19512
  }
19180
19513
  async createSparkAuthnGrpcConnection(address2, certPath) {
19181
19514
  const channel = await this.createChannelWithTLS(address2, certPath);
19515
+ const authnMiddleware = this.createAuthnMiddleware();
19182
19516
  return this.createGrpcClient(
19183
19517
  SparkAuthnServiceDefinition,
19184
19518
  channel,
19185
- false
19519
+ false,
19520
+ authnMiddleware
19186
19521
  );
19187
19522
  }
19523
+ createAuthnMiddleware() {
19524
+ if (import_core3.isNode) {
19525
+ return async function* (call, options) {
19526
+ const metadata = (0, import_nice_grpc_common2.Metadata)(options.metadata).set(
19527
+ "X-Client-Env",
19528
+ clientEnv
19529
+ );
19530
+ return yield* call.next(call.request, {
19531
+ ...options,
19532
+ metadata
19533
+ });
19534
+ }.bind(this);
19535
+ } else {
19536
+ return async function* (call, options) {
19537
+ const metadata = (0, import_nice_grpc_common2.Metadata)(options.metadata).set("X-Requested-With", "XMLHttpRequest").set("X-Grpc-Web", "1").set("X-Client-Env", clientEnv).set("Content-Type", "application/grpc-web+proto");
19538
+ return yield* call.next(call.request, {
19539
+ ...options,
19540
+ metadata
19541
+ });
19542
+ }.bind(this);
19543
+ }
19544
+ }
19188
19545
  createMiddleware(address2, authToken) {
19189
- if (import_core2.isNode) {
19546
+ if (import_core3.isNode) {
19190
19547
  return this.createNodeMiddleware(address2, authToken);
19191
19548
  } else {
19192
19549
  return this.createBrowserMiddleware(address2, authToken);
@@ -19194,21 +19551,29 @@ var ConnectionManager = class {
19194
19551
  }
19195
19552
  createNodeMiddleware(address2, initialAuthToken) {
19196
19553
  return async function* (call, options) {
19554
+ const metadata = (0, import_nice_grpc_common2.Metadata)(options.metadata).set(
19555
+ "X-Client-Env",
19556
+ clientEnv
19557
+ );
19197
19558
  try {
19198
19559
  return yield* call.next(call.request, {
19199
19560
  ...options,
19200
- metadata: (0, import_nice_grpc_common2.Metadata)(options.metadata).set(
19561
+ metadata: metadata.set(
19201
19562
  "Authorization",
19202
19563
  `Bearer ${this.clients.get(address2)?.authToken || initialAuthToken}`
19203
- ).set("User-Agent", "spark-js-sdk")
19564
+ )
19204
19565
  });
19205
19566
  } catch (error) {
19206
19567
  if (error.message?.includes("token has expired")) {
19207
19568
  const newAuthToken = await this.authenticate(address2);
19208
- this.clients.get(address2).authToken = newAuthToken;
19569
+ const clientData = this.clients.get(address2);
19570
+ if (!clientData) {
19571
+ throw new Error(`No client found for address: ${address2}`);
19572
+ }
19573
+ clientData.authToken = newAuthToken;
19209
19574
  return yield* call.next(call.request, {
19210
19575
  ...options,
19211
- metadata: (0, import_nice_grpc_common2.Metadata)(options.metadata).set("Authorization", `Bearer ${newAuthToken}`).set("User-Agent", "spark-js-sdk")
19576
+ metadata: metadata.set("Authorization", `Bearer ${newAuthToken}`)
19212
19577
  });
19213
19578
  }
19214
19579
  throw error;
@@ -19217,21 +19582,26 @@ var ConnectionManager = class {
19217
19582
  }
19218
19583
  createBrowserMiddleware(address2, initialAuthToken) {
19219
19584
  return async function* (call, options) {
19585
+ const metadata = (0, import_nice_grpc_common2.Metadata)(options.metadata).set("X-Requested-With", "XMLHttpRequest").set("X-Grpc-Web", "1").set("X-Client-Env", clientEnv).set("Content-Type", "application/grpc-web+proto");
19220
19586
  try {
19221
19587
  return yield* call.next(call.request, {
19222
19588
  ...options,
19223
- metadata: (0, import_nice_grpc_common2.Metadata)(options.metadata).set(
19589
+ metadata: metadata.set(
19224
19590
  "Authorization",
19225
19591
  `Bearer ${this.clients.get(address2)?.authToken || initialAuthToken}`
19226
- ).set("X-Requested-With", "XMLHttpRequest").set("X-Grpc-Web", "1").set("Content-Type", "application/grpc-web+proto").set("User-Agent", "spark-js-sdk")
19592
+ )
19227
19593
  });
19228
19594
  } catch (error) {
19229
19595
  if (error.message?.includes("token has expired")) {
19230
19596
  const newAuthToken = await this.authenticate(address2);
19231
- this.clients.get(address2).authToken = newAuthToken;
19597
+ const clientData = this.clients.get(address2);
19598
+ if (!clientData) {
19599
+ throw new Error(`No client found for address: ${address2}`);
19600
+ }
19601
+ clientData.authToken = newAuthToken;
19232
19602
  return yield* call.next(call.request, {
19233
19603
  ...options,
19234
- metadata: (0, import_nice_grpc_common2.Metadata)(options.metadata).set("Authorization", `Bearer ${newAuthToken}`).set("X-Requested-With", "XMLHttpRequest").set("X-Grpc-Web", "1").set("Content-Type", "application/grpc-web+proto").set("User-Agent", "spark-js-sdk")
19604
+ metadata: metadata.set("Authorization", `Bearer ${newAuthToken}`)
19235
19605
  });
19236
19606
  }
19237
19607
  throw error;
@@ -19246,7 +19616,7 @@ var ConnectionManager = class {
19246
19616
  };
19247
19617
  let options = {};
19248
19618
  const isNodeChannel = "close" in channel;
19249
- if (import_core2.isNode && isNodeChannel && !isBun) {
19619
+ if (import_core3.isNode && isNodeChannel && !isBun) {
19250
19620
  const grpcModule = await import("nice-grpc");
19251
19621
  const { openTelemetryClientMiddleware } = await import("nice-grpc-opentelemetry");
19252
19622
  const { createClientFactory } = "default" in grpcModule ? grpcModule.default : grpcModule;