@secondlayer/stacks 2.15.0 → 2.16.0

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 (70) hide show
  1. package/dist/shared/{stacks-tfpspzwd.js → stacks-1x9b2k5y.js} +16 -11
  2. package/dist/shared/{stacks-tfpspzwd.js.map → stacks-1x9b2k5y.js.map} +5 -5
  3. package/dist/shared/{stacks-pperea7f.js → stacks-58e4vbaw.js} +3 -3
  4. package/dist/shared/{stacks-jbmcwky5.js → stacks-905052xe.js} +33 -3
  5. package/dist/shared/{stacks-jbmcwky5.js.map → stacks-905052xe.js.map} +3 -3
  6. package/dist/shared/{stacks-shn9zw99.cjs → stacks-9mqveyjv.cjs} +3 -3
  7. package/dist/shared/stacks-9mqveyjv.cjs.map +10 -0
  8. package/dist/shared/{stacks-1xhz0fbf.cjs → stacks-fvnxj77s.cjs} +13 -8
  9. package/dist/shared/{stacks-1xhz0fbf.cjs.map → stacks-fvnxj77s.cjs.map} +5 -5
  10. package/dist/shared/{stacks-wamwv8q8.cjs → stacks-g2qnr5qn.cjs} +12 -3
  11. package/dist/shared/{stacks-wamwv8q8.cjs.map → stacks-g2qnr5qn.cjs.map} +5 -5
  12. package/dist/shared/{stacks-dmrvk1mz.js → stacks-mbnfwekf.js} +34 -8
  13. package/dist/shared/{stacks-dmrvk1mz.js.map → stacks-mbnfwekf.js.map} +5 -5
  14. package/dist/shared/{stacks-f66v961k.js → stacks-p2c0w0sq.js} +3 -3
  15. package/dist/shared/{stacks-g11v9a35.js → stacks-qchg7s2m.js} +9 -5
  16. package/dist/shared/{stacks-g11v9a35.js.map → stacks-qchg7s2m.js.map} +3 -3
  17. package/dist/shared/{stacks-5nskdw7w.cjs → stacks-qjgh6pqr.cjs} +8 -4
  18. package/dist/shared/{stacks-5nskdw7w.cjs.map → stacks-qjgh6pqr.cjs.map} +3 -3
  19. package/dist/shared/{stacks-jcgvn5v5.cjs → stacks-s361pmzs.cjs} +32 -7
  20. package/dist/shared/{stacks-jcgvn5v5.cjs.map → stacks-s361pmzs.cjs.map} +5 -5
  21. package/dist/shared/{stacks-ssndnbbd.js → stacks-sd9sc71n.js} +13 -4
  22. package/dist/shared/{stacks-ssndnbbd.js.map → stacks-sd9sc71n.js.map} +5 -5
  23. package/dist/shared/{stacks-0tp2dk00.js → stacks-srvk63qq.js} +8 -7
  24. package/dist/shared/stacks-srvk63qq.js.map +10 -0
  25. package/dist/shared/{stacks-b9904rc2.cjs → stacks-v889ttgp.cjs} +37 -2
  26. package/dist/shared/{stacks-b9904rc2.cjs.map → stacks-v889ttgp.cjs.map} +3 -3
  27. package/dist/src/actions/index.d.cts +5 -0
  28. package/dist/src/actions/index.d.ts +5 -0
  29. package/dist/src/actions/index.js +7 -7
  30. package/dist/src/bitcoin/index.js +1 -1
  31. package/dist/src/bns/index.js +5 -5
  32. package/dist/src/clarity/index.d.cts +11 -1
  33. package/dist/src/clarity/index.d.ts +11 -1
  34. package/dist/src/clarity/index.js +12 -2
  35. package/dist/src/clarity/index.js.map +1 -1
  36. package/dist/src/connect/index.cjs +47 -6
  37. package/dist/src/connect/index.cjs.map +8 -4
  38. package/dist/src/connect/index.js +47 -6
  39. package/dist/src/connect/index.js.map +8 -4
  40. package/dist/src/index.cjs +4 -4
  41. package/dist/src/index.cjs.map +3 -3
  42. package/dist/src/index.d.cts +11 -0
  43. package/dist/src/index.d.ts +11 -0
  44. package/dist/src/index.js +11 -10
  45. package/dist/src/index.js.map +3 -3
  46. package/dist/src/pox/index.js +4 -4
  47. package/dist/src/pox5/index.js +3 -3
  48. package/dist/src/sbtc/index.js +4 -4
  49. package/dist/src/stackingdao/index.js +4 -4
  50. package/dist/src/tools/btc/index.cjs +6 -5
  51. package/dist/src/tools/btc/index.cjs.map +3 -3
  52. package/dist/src/tools/btc/index.d.cts +3 -1
  53. package/dist/src/tools/btc/index.d.ts +3 -1
  54. package/dist/src/tools/btc/index.js +6 -5
  55. package/dist/src/tools/btc/index.js.map +3 -3
  56. package/dist/src/tools/index.cjs +339 -45
  57. package/dist/src/tools/index.cjs.map +16 -15
  58. package/dist/src/tools/index.js +339 -48
  59. package/dist/src/tools/index.js.map +16 -15
  60. package/dist/src/transactions/index.d.cts +10 -1
  61. package/dist/src/transactions/index.d.ts +10 -1
  62. package/dist/src/transactions/index.js +2 -2
  63. package/dist/src/x402/index.d.cts +5 -0
  64. package/dist/src/x402/index.d.ts +5 -0
  65. package/dist/src/x402/index.js +4 -4
  66. package/package.json +4 -2
  67. package/dist/shared/stacks-0tp2dk00.js.map +0 -10
  68. package/dist/shared/stacks-shn9zw99.cjs.map +0 -10
  69. /package/dist/shared/{stacks-pperea7f.js.map → stacks-58e4vbaw.js.map} +0 -0
  70. /package/dist/shared/{stacks-f66v961k.js.map → stacks-p2c0w0sq.js.map} +0 -0
@@ -1,10 +1,38 @@
1
- import { createRequire } from "node:module";
2
- var __require = /* @__PURE__ */ createRequire(import.meta.url);
3
-
4
1
  // src/tools/index.ts
5
2
  import { tool } from "ai";
6
3
  import { z } from "zod";
7
4
 
5
+ // src/errors/base.ts
6
+ class BaseError extends Error {
7
+ name = "StacksError";
8
+ shortMessage;
9
+ details;
10
+ constructor(shortMessage, options) {
11
+ const message = [
12
+ shortMessage,
13
+ options?.details ? `
14
+ ${options.details}` : ""
15
+ ].join("");
16
+ super(message, { cause: options?.cause });
17
+ this.shortMessage = shortMessage;
18
+ this.details = options?.details;
19
+ }
20
+ toJSON() {
21
+ return {
22
+ name: this.name,
23
+ message: this.message,
24
+ shortMessage: this.shortMessage,
25
+ details: this.details,
26
+ cause: this.cause instanceof Error ? this.cause.message : undefined
27
+ };
28
+ }
29
+ }
30
+
31
+ // src/errors/response.ts
32
+ class MalformedResponseError extends BaseError {
33
+ name = "MalformedResponseError";
34
+ }
35
+
8
36
  // src/utils/c32.ts
9
37
  import { sha256 } from "@noble/hashes/sha2.js";
10
38
 
@@ -418,6 +446,9 @@ var AssetType = {
418
446
  };
419
447
  var RECOVERABLE_ECDSA_SIG_LENGTH_BYTES = 65;
420
448
  var MEMO_MAX_LENGTH_BYTES = 34;
449
+ var COINBASE_BYTES_LENGTH = 32;
450
+ var VRF_PROOF_BYTES_LENGTH = 80;
451
+ var MICROBLOCK_HEADER_BYTES_LENGTH = 132;
421
452
 
422
453
  // src/transactions/wire/serialize.ts
423
454
  function serializeAddress2(c32Address) {
@@ -598,11 +629,23 @@ function serializePayload(payload) {
598
629
  }
599
630
  return concatBytes(...parts);
600
631
  }
632
+ var _txCache = new WeakMap;
633
+ function clearTxCache(tx) {
634
+ _txCache.delete(tx);
635
+ }
601
636
  function serializeTransaction(tx) {
602
- return concatBytes(writeUInt8(tx.version), writeUInt32BE(tx.chainId), serializeAuthorization(tx.auth), writeUInt8(tx.anchorMode), writeUInt8(tx.postConditionMode), serializePostConditions(tx.postConditions), serializePayload(tx.payload));
637
+ const cached = _txCache.get(tx);
638
+ if (cached)
639
+ return cached;
640
+ const bytes = concatBytes(writeUInt8(tx.version), writeUInt32BE(tx.chainId), serializeAuthorization(tx.auth), writeUInt8(tx.anchorMode), writeUInt8(tx.postConditionMode), serializePostConditions(tx.postConditions), serializePayload(tx.payload));
641
+ _txCache.set(tx, bytes);
642
+ return bytes;
603
643
  }
604
644
 
605
645
  // src/actions/public/estimateFee.ts
646
+ function isValidEstimation(e) {
647
+ return typeof e === "object" && e !== null && "fee_rate" in e && "fee" in e && (typeof e.fee_rate === "number" || typeof e.fee_rate === "string") && (typeof e.fee === "number" || typeof e.fee === "string");
648
+ }
606
649
  async function estimateFee(client, params) {
607
650
  const payloadHex = bytesToHex(serializePayload(params.transaction.payload));
608
651
  const txHex = bytesToHex(serializeTransaction(params.transaction));
@@ -613,10 +656,16 @@ async function estimateFee(client, params) {
613
656
  transaction_payload: `0x${payloadHex}`
614
657
  }
615
658
  });
616
- return (data.estimations ?? []).map((e) => ({
617
- feeRate: e.fee_rate,
618
- fee: e.fee
619
- }));
659
+ const raw = data?.estimations ?? [];
660
+ return raw.map((e) => {
661
+ if (!isValidEstimation(e)) {
662
+ throw new MalformedResponseError("estimateFee: node response contains invalid fee estimation entry");
663
+ }
664
+ return {
665
+ feeRate: Number(e.fee_rate),
666
+ fee: Number(e.fee)
667
+ };
668
+ });
620
669
  }
621
670
 
622
671
  // src/actions/public/getAccountHistory.ts
@@ -625,48 +674,19 @@ async function getAccountHistory(client, params) {
625
674
  return client.request(`/extended/v2/addresses/${params.address}/transactions?limit=${limit}`, { method: "GET" });
626
675
  }
627
676
 
628
- // src/errors/base.ts
629
- class BaseError extends Error {
630
- name = "StacksError";
631
- shortMessage;
632
- details;
633
- constructor(shortMessage, options) {
634
- const message = [
635
- shortMessage,
636
- options?.details ? `
637
- ${options.details}` : ""
638
- ].join("");
639
- super(message, { cause: options?.cause });
640
- this.shortMessage = shortMessage;
641
- this.details = options?.details;
642
- }
643
- toJSON() {
644
- return {
645
- name: this.name,
646
- message: this.message,
647
- shortMessage: this.shortMessage,
648
- details: this.details,
649
- cause: this.cause instanceof Error ? this.cause.message : undefined
650
- };
651
- }
652
- }
653
-
654
- // src/errors/response.ts
655
- class MalformedResponseError extends BaseError {
656
- name = "MalformedResponseError";
657
- }
658
-
659
677
  // src/actions/public/getAccountInfo.ts
660
678
  async function getAccountInfo(client, params) {
661
679
  const data = await client.request(`/v2/accounts/${params.address}?proof=1`, {
662
680
  method: "GET"
663
681
  });
664
- if (data?.balance === undefined || data?.nonce === undefined) {
682
+ const balance = data?.balance;
683
+ const nonce = data?.nonce;
684
+ if (typeof balance !== "string" && typeof balance !== "number" || typeof nonce !== "string" && typeof nonce !== "number") {
665
685
  throw new MalformedResponseError(`getAccountInfo: /v2/accounts/${params.address} response is missing "balance" or "nonce"`);
666
686
  }
667
687
  return {
668
- balance: BigInt(data.balance),
669
- nonce: BigInt(data.nonce),
688
+ balance: BigInt(balance),
689
+ nonce: BigInt(nonce),
670
690
  balanceProof: data.balance_proof ?? "",
671
691
  nonceProof: data.nonce_proof ?? ""
672
692
  };
@@ -677,10 +697,11 @@ async function getBalance(client, params) {
677
697
  const data = await client.request(`/v2/accounts/${params.address}`, {
678
698
  method: "GET"
679
699
  });
680
- if (data?.balance === undefined) {
700
+ const balance = data?.balance;
701
+ if (typeof balance !== "string" && typeof balance !== "number") {
681
702
  throw new MalformedResponseError(`getBalance: /v2/accounts/${params.address} response is missing "balance"`);
682
703
  }
683
- return BigInt(data.balance);
704
+ return BigInt(balance);
684
705
  }
685
706
 
686
707
  // src/actions/public/getBlock.ts
@@ -1747,6 +1768,7 @@ async function resolveFee(client, transaction, fee) {
1747
1768
  }
1748
1769
  function setUnsignedFee(transaction, fee) {
1749
1770
  transaction.auth.spendingCondition.fee = fee;
1771
+ clearTxCache(transaction);
1750
1772
  }
1751
1773
  function assertNoFeeTierForProvider(fee) {
1752
1774
  if (isFeeTier(fee)) {
@@ -1890,7 +1912,7 @@ function getContract(params) {
1890
1912
  });
1891
1913
  const mid = estimates[1] ?? estimates[0];
1892
1914
  if (mid) {
1893
- unsigned.auth.spendingCondition.fee = BigInt(mid.fee);
1915
+ setUnsignedFee(unsigned, BigInt(mid.fee));
1894
1916
  }
1895
1917
  }
1896
1918
  return unsigned;
@@ -2493,6 +2515,262 @@ async function getPrimaryName(client, address) {
2493
2515
  }
2494
2516
  }
2495
2517
 
2518
+ // src/transactions/wire/deserialize.ts
2519
+ function readSpendingCondition(r) {
2520
+ const hashMode = r.readUInt8();
2521
+ const signer = bytesToHex(r.readBytes(20));
2522
+ const nonce = r.readBigUInt64BE();
2523
+ const fee = r.readBigUInt64BE();
2524
+ if (hashMode === AddressHashMode.P2PKH || hashMode === AddressHashMode.P2WPKH) {
2525
+ const keyEncoding = r.readUInt8();
2526
+ const signature = bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES));
2527
+ return {
2528
+ hashMode,
2529
+ signer,
2530
+ nonce,
2531
+ fee,
2532
+ keyEncoding,
2533
+ signature
2534
+ };
2535
+ }
2536
+ const fieldCount = r.readUInt32BE();
2537
+ const fields = [];
2538
+ for (let i = 0;i < fieldCount; i++) {
2539
+ const fieldType = r.readUInt8();
2540
+ if (fieldType <= 1) {
2541
+ const keyLen = fieldType === 0 ? 33 : 65;
2542
+ fields.push({
2543
+ type: "publicKey",
2544
+ pubKeyEncoding: fieldType,
2545
+ data: bytesToHex(r.readBytes(keyLen))
2546
+ });
2547
+ } else {
2548
+ fields.push({
2549
+ type: "signature",
2550
+ pubKeyEncoding: fieldType === 2 ? 0 : 1,
2551
+ data: bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES))
2552
+ });
2553
+ }
2554
+ }
2555
+ const signaturesRequired = r.readUInt16BE();
2556
+ return {
2557
+ hashMode,
2558
+ signer,
2559
+ nonce,
2560
+ fee,
2561
+ fields,
2562
+ signaturesRequired
2563
+ };
2564
+ }
2565
+ function readAuthorization(r) {
2566
+ const authType = r.readUInt8();
2567
+ const spendingCondition = readSpendingCondition(r);
2568
+ if (authType === AuthType.Standard) {
2569
+ return { authType: AuthType.Standard, spendingCondition };
2570
+ }
2571
+ const sponsorSpendingCondition = readSpendingCondition(r);
2572
+ return {
2573
+ authType: AuthType.Sponsored,
2574
+ spendingCondition,
2575
+ sponsorSpendingCondition
2576
+ };
2577
+ }
2578
+ function readAssetInfo(r) {
2579
+ return {
2580
+ address: readAddress(r),
2581
+ contractName: readLPString(r),
2582
+ assetName: readLPString(r)
2583
+ };
2584
+ }
2585
+ function readPostConditionPrincipal(r) {
2586
+ const type = r.readUInt8();
2587
+ switch (type) {
2588
+ case PostConditionPrincipalId.Origin:
2589
+ return { type: "origin" };
2590
+ case PostConditionPrincipalId.Standard:
2591
+ return { type: "standard", address: readAddress(r) };
2592
+ case PostConditionPrincipalId.Contract:
2593
+ return {
2594
+ type: "contract",
2595
+ address: readAddress(r),
2596
+ contractName: readLPString(r)
2597
+ };
2598
+ default:
2599
+ return { type: "origin" };
2600
+ }
2601
+ }
2602
+ function readPostConditions(r) {
2603
+ const count = r.readUInt32BE();
2604
+ const pcs = [];
2605
+ for (let i = 0;i < count; i++) {
2606
+ const assetType = r.readUInt8();
2607
+ const principal = readPostConditionPrincipal(r);
2608
+ switch (assetType) {
2609
+ case AssetType.STX:
2610
+ pcs.push({
2611
+ type: "stx",
2612
+ principal,
2613
+ conditionCode: r.readUInt8(),
2614
+ amount: r.readBigUInt64BE()
2615
+ });
2616
+ break;
2617
+ case AssetType.Fungible:
2618
+ pcs.push({
2619
+ type: "ft",
2620
+ principal,
2621
+ asset: readAssetInfo(r),
2622
+ conditionCode: r.readUInt8(),
2623
+ amount: r.readBigUInt64BE()
2624
+ });
2625
+ break;
2626
+ case AssetType.NonFungible:
2627
+ pcs.push({
2628
+ type: "nft",
2629
+ principal,
2630
+ asset: readAssetInfo(r),
2631
+ assetId: readCV(r),
2632
+ conditionCode: r.readUInt8()
2633
+ });
2634
+ break;
2635
+ case AssetType.Staking:
2636
+ pcs.push({
2637
+ type: "staking",
2638
+ principal,
2639
+ conditionCode: r.readUInt8(),
2640
+ amount: r.readBigUInt64BE()
2641
+ });
2642
+ break;
2643
+ case AssetType.Pox:
2644
+ pcs.push({
2645
+ type: "pox",
2646
+ principal,
2647
+ conditionCode: r.readUInt8()
2648
+ });
2649
+ break;
2650
+ default:
2651
+ throw new Error(`Unknown post-condition asset type: 0x${assetType.toString(16).padStart(2, "0")} (post-condition ${i + 1}/${count})`);
2652
+ }
2653
+ }
2654
+ return pcs;
2655
+ }
2656
+ function readPayload(r) {
2657
+ const payloadType = r.readUInt8();
2658
+ switch (payloadType) {
2659
+ case PayloadType.TokenTransfer: {
2660
+ const recipient = readCV(r);
2661
+ const amount = r.readBigUInt64BE();
2662
+ const memoBytes = r.readBytes(MEMO_MAX_LENGTH_BYTES);
2663
+ const memo = bytesToAscii(memoBytes).replace(/\0+$/, "");
2664
+ return {
2665
+ payloadType: PayloadType.TokenTransfer,
2666
+ recipient,
2667
+ amount,
2668
+ memo
2669
+ };
2670
+ }
2671
+ case PayloadType.ContractCall: {
2672
+ const contractAddress = readAddress(r);
2673
+ const contractName = readLPString(r);
2674
+ const functionName = readLPString(r);
2675
+ const numArgs = r.readUInt32BE();
2676
+ const functionArgs = [];
2677
+ for (let i = 0;i < numArgs; i++)
2678
+ functionArgs.push(readCV(r));
2679
+ return {
2680
+ payloadType: PayloadType.ContractCall,
2681
+ contractAddress,
2682
+ contractName,
2683
+ functionName,
2684
+ functionArgs
2685
+ };
2686
+ }
2687
+ case PayloadType.SmartContract: {
2688
+ const contractName = readLPString(r);
2689
+ const codeBody = readLPString(r, 4);
2690
+ return { payloadType: PayloadType.SmartContract, contractName, codeBody };
2691
+ }
2692
+ case PayloadType.VersionedSmartContract: {
2693
+ const clarityVersion = r.readUInt8();
2694
+ const contractName = readLPString(r);
2695
+ const codeBody = readLPString(r, 4);
2696
+ return {
2697
+ payloadType: PayloadType.VersionedSmartContract,
2698
+ clarityVersion,
2699
+ contractName,
2700
+ codeBody
2701
+ };
2702
+ }
2703
+ case PayloadType.Coinbase: {
2704
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2705
+ return { payloadType: PayloadType.Coinbase, coinbaseBuffer };
2706
+ }
2707
+ case PayloadType.CoinbaseToAltRecipient: {
2708
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2709
+ const recipient = readCV(r);
2710
+ return {
2711
+ payloadType: PayloadType.CoinbaseToAltRecipient,
2712
+ coinbaseBuffer,
2713
+ recipient
2714
+ };
2715
+ }
2716
+ case PayloadType.PoisonMicroblock: {
2717
+ const header1 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
2718
+ const header2 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
2719
+ return { payloadType: PayloadType.PoisonMicroblock, header1, header2 };
2720
+ }
2721
+ case PayloadType.TenureChange: {
2722
+ const tenureConsensusHash = bytesToHex(r.readBytes(20));
2723
+ const prevTenureConsensusHash = bytesToHex(r.readBytes(20));
2724
+ const burnViewConsensusHash = bytesToHex(r.readBytes(20));
2725
+ const previousTenureEnd = bytesToHex(r.readBytes(32));
2726
+ const previousTenureBlocks = r.readUInt32BE();
2727
+ const cause = r.readUInt8();
2728
+ const pubkeyHash = bytesToHex(r.readBytes(20));
2729
+ return {
2730
+ payloadType: PayloadType.TenureChange,
2731
+ tenureConsensusHash,
2732
+ prevTenureConsensusHash,
2733
+ burnViewConsensusHash,
2734
+ previousTenureEnd,
2735
+ previousTenureBlocks,
2736
+ cause,
2737
+ pubkeyHash
2738
+ };
2739
+ }
2740
+ case PayloadType.NakamotoCoinbase: {
2741
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2742
+ const optionalCV = readCV(r);
2743
+ const recipient = optionalCV.type === "none" ? null : optionalCV.type === "some" ? optionalCV.value : optionalCV;
2744
+ const vrfProof = bytesToHex(r.readBytes(VRF_PROOF_BYTES_LENGTH));
2745
+ return {
2746
+ payloadType: PayloadType.NakamotoCoinbase,
2747
+ coinbaseBuffer,
2748
+ recipient,
2749
+ vrfProof
2750
+ };
2751
+ }
2752
+ default:
2753
+ throw new Error(`Unknown payload type: ${payloadType}`);
2754
+ }
2755
+ }
2756
+ function deserializeTransaction(input, meta) {
2757
+ const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
2758
+ const r = new BytesReader(bytes);
2759
+ const tx = {
2760
+ version: r.readUInt8(),
2761
+ chainId: r.readUInt32BE(),
2762
+ auth: readAuthorization(r),
2763
+ anchorMode: r.readUInt8(),
2764
+ postConditionMode: r.readUInt8(),
2765
+ postConditions: readPostConditions(r),
2766
+ payload: readPayload(r)
2767
+ };
2768
+ if (meta?._multisig) {
2769
+ tx._multisig = meta._multisig;
2770
+ }
2771
+ return tx;
2772
+ }
2773
+
2496
2774
  // src/chains/definitions.ts
2497
2775
  var mainnet = {
2498
2776
  id: 1,
@@ -2935,11 +3213,24 @@ async function contractRead(client, args) {
2935
3213
  const result = await readContract(client, args);
2936
3214
  return { result: JSON.stringify(result) };
2937
3215
  }
3216
+ function assertValidHex(raw) {
3217
+ if (raw.length % 2 !== 0) {
3218
+ throw new Error("serializedTxHex: odd-length hex string");
3219
+ }
3220
+ if (!/^[0-9a-fA-F]+$/.test(raw)) {
3221
+ throw new Error("serializedTxHex: contains non-hex characters");
3222
+ }
3223
+ }
2938
3224
  async function fee(client, serializedTxHex) {
2939
3225
  const raw = serializedTxHex.startsWith("0x") ? serializedTxHex.slice(2) : serializedTxHex;
3226
+ assertValidHex(raw);
2940
3227
  const txBytes = new Uint8Array(Buffer.from(raw, "hex"));
2941
- const { deserializeTransaction } = await import("@stacks/transactions");
2942
- const transaction = deserializeTransaction(txBytes);
3228
+ let transaction;
3229
+ try {
3230
+ transaction = deserializeTransaction(txBytes);
3231
+ } catch (e) {
3232
+ throw new Error(`serializedTxHex: deserialization failed — ${e instanceof Error ? e.message : String(e)}`);
3233
+ }
2943
3234
  const fees = await estimateFee(client, {
2944
3235
  transaction
2945
3236
  });
@@ -3139,5 +3430,5 @@ export {
3139
3430
  bnsResolve
3140
3431
  };
3141
3432
 
3142
- //# debugId=8FE40CC86F96048464756E2164756E21
3433
+ //# debugId=A60A0C3D6BD9651064756E2164756E21
3143
3434
  //# sourceMappingURL=index.js.map