@secondlayer/stacks 2.15.0 → 2.15.1

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 (37) hide show
  1. package/dist/shared/{stacks-f66v961k.js → stacks-1k0kmpcw.js} +2 -2
  2. package/dist/shared/{stacks-1xhz0fbf.cjs → stacks-1xk4wby6.cjs} +10 -7
  3. package/dist/shared/{stacks-1xhz0fbf.cjs.map → stacks-1xk4wby6.cjs.map} +4 -4
  4. package/dist/shared/{stacks-tfpspzwd.js → stacks-2x04a6ss.js} +11 -8
  5. package/dist/shared/{stacks-tfpspzwd.js.map → stacks-2x04a6ss.js.map} +4 -4
  6. package/dist/shared/{stacks-jcgvn5v5.cjs → stacks-70xxh5h1.cjs} +15 -6
  7. package/dist/shared/{stacks-dmrvk1mz.js.map → stacks-70xxh5h1.cjs.map} +3 -3
  8. package/dist/shared/{stacks-pperea7f.js → stacks-q45x3389.js} +2 -2
  9. package/dist/shared/{stacks-0tp2dk00.js → stacks-refg7hca.js} +3 -3
  10. package/dist/shared/{stacks-dmrvk1mz.js → stacks-y9w78e2b.js} +15 -6
  11. package/dist/shared/{stacks-jcgvn5v5.cjs.map → stacks-y9w78e2b.js.map} +3 -3
  12. package/dist/src/actions/index.js +5 -5
  13. package/dist/src/bns/index.js +3 -3
  14. package/dist/src/connect/index.cjs +47 -6
  15. package/dist/src/connect/index.cjs.map +8 -4
  16. package/dist/src/connect/index.js +47 -6
  17. package/dist/src/connect/index.js.map +8 -4
  18. package/dist/src/index.js +4 -4
  19. package/dist/src/pox/index.js +3 -3
  20. package/dist/src/pox5/index.js +2 -2
  21. package/dist/src/sbtc/index.js +3 -3
  22. package/dist/src/stackingdao/index.js +3 -3
  23. package/dist/src/tools/btc/index.cjs +6 -5
  24. package/dist/src/tools/btc/index.cjs.map +3 -3
  25. package/dist/src/tools/btc/index.d.cts +2 -1
  26. package/dist/src/tools/btc/index.d.ts +2 -1
  27. package/dist/src/tools/btc/index.js +6 -5
  28. package/dist/src/tools/btc/index.js.map +3 -3
  29. package/dist/src/tools/index.cjs +323 -43
  30. package/dist/src/tools/index.cjs.map +10 -9
  31. package/dist/src/tools/index.js +323 -46
  32. package/dist/src/tools/index.js.map +10 -9
  33. package/dist/src/x402/index.js +2 -2
  34. package/package.json +4 -2
  35. /package/dist/shared/{stacks-f66v961k.js.map → stacks-1k0kmpcw.js.map} +0 -0
  36. /package/dist/shared/{stacks-pperea7f.js.map → stacks-q45x3389.js.map} +0 -0
  37. /package/dist/shared/{stacks-0tp2dk00.js.map → stacks-refg7hca.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) {
@@ -603,6 +634,9 @@ function serializeTransaction(tx) {
603
634
  }
604
635
 
605
636
  // src/actions/public/estimateFee.ts
637
+ function isValidEstimation(e) {
638
+ 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");
639
+ }
606
640
  async function estimateFee(client, params) {
607
641
  const payloadHex = bytesToHex(serializePayload(params.transaction.payload));
608
642
  const txHex = bytesToHex(serializeTransaction(params.transaction));
@@ -613,10 +647,16 @@ async function estimateFee(client, params) {
613
647
  transaction_payload: `0x${payloadHex}`
614
648
  }
615
649
  });
616
- return (data.estimations ?? []).map((e) => ({
617
- feeRate: e.fee_rate,
618
- fee: e.fee
619
- }));
650
+ const raw = data?.estimations ?? [];
651
+ return raw.map((e) => {
652
+ if (!isValidEstimation(e)) {
653
+ throw new MalformedResponseError("estimateFee: node response contains invalid fee estimation entry");
654
+ }
655
+ return {
656
+ feeRate: Number(e.fee_rate),
657
+ fee: Number(e.fee)
658
+ };
659
+ });
620
660
  }
621
661
 
622
662
  // src/actions/public/getAccountHistory.ts
@@ -625,48 +665,19 @@ async function getAccountHistory(client, params) {
625
665
  return client.request(`/extended/v2/addresses/${params.address}/transactions?limit=${limit}`, { method: "GET" });
626
666
  }
627
667
 
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
668
  // src/actions/public/getAccountInfo.ts
660
669
  async function getAccountInfo(client, params) {
661
670
  const data = await client.request(`/v2/accounts/${params.address}?proof=1`, {
662
671
  method: "GET"
663
672
  });
664
- if (data?.balance === undefined || data?.nonce === undefined) {
673
+ const balance = data?.balance;
674
+ const nonce = data?.nonce;
675
+ if (typeof balance !== "string" && typeof balance !== "number" || typeof nonce !== "string" && typeof nonce !== "number") {
665
676
  throw new MalformedResponseError(`getAccountInfo: /v2/accounts/${params.address} response is missing "balance" or "nonce"`);
666
677
  }
667
678
  return {
668
- balance: BigInt(data.balance),
669
- nonce: BigInt(data.nonce),
679
+ balance: BigInt(balance),
680
+ nonce: BigInt(nonce),
670
681
  balanceProof: data.balance_proof ?? "",
671
682
  nonceProof: data.nonce_proof ?? ""
672
683
  };
@@ -677,10 +688,11 @@ async function getBalance(client, params) {
677
688
  const data = await client.request(`/v2/accounts/${params.address}`, {
678
689
  method: "GET"
679
690
  });
680
- if (data?.balance === undefined) {
691
+ const balance = data?.balance;
692
+ if (typeof balance !== "string" && typeof balance !== "number") {
681
693
  throw new MalformedResponseError(`getBalance: /v2/accounts/${params.address} response is missing "balance"`);
682
694
  }
683
- return BigInt(data.balance);
695
+ return BigInt(balance);
684
696
  }
685
697
 
686
698
  // src/actions/public/getBlock.ts
@@ -2493,6 +2505,258 @@ async function getPrimaryName(client, address) {
2493
2505
  }
2494
2506
  }
2495
2507
 
2508
+ // src/transactions/wire/deserialize.ts
2509
+ function readSpendingCondition(r) {
2510
+ const hashMode = r.readUInt8();
2511
+ const signer = bytesToHex(r.readBytes(20));
2512
+ const nonce = r.readBigUInt64BE();
2513
+ const fee = r.readBigUInt64BE();
2514
+ if (hashMode === AddressHashMode.P2PKH || hashMode === AddressHashMode.P2WPKH) {
2515
+ const keyEncoding = r.readUInt8();
2516
+ const signature = bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES));
2517
+ return {
2518
+ hashMode,
2519
+ signer,
2520
+ nonce,
2521
+ fee,
2522
+ keyEncoding,
2523
+ signature
2524
+ };
2525
+ }
2526
+ const fieldCount = r.readUInt32BE();
2527
+ const fields = [];
2528
+ for (let i = 0;i < fieldCount; i++) {
2529
+ const fieldType = r.readUInt8();
2530
+ if (fieldType <= 1) {
2531
+ const keyLen = fieldType === 0 ? 33 : 65;
2532
+ fields.push({
2533
+ type: "publicKey",
2534
+ pubKeyEncoding: fieldType,
2535
+ data: bytesToHex(r.readBytes(keyLen))
2536
+ });
2537
+ } else {
2538
+ fields.push({
2539
+ type: "signature",
2540
+ pubKeyEncoding: fieldType === 2 ? 0 : 1,
2541
+ data: bytesToHex(r.readBytes(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES))
2542
+ });
2543
+ }
2544
+ }
2545
+ const signaturesRequired = r.readUInt16BE();
2546
+ return {
2547
+ hashMode,
2548
+ signer,
2549
+ nonce,
2550
+ fee,
2551
+ fields,
2552
+ signaturesRequired
2553
+ };
2554
+ }
2555
+ function readAuthorization(r) {
2556
+ const authType = r.readUInt8();
2557
+ const spendingCondition = readSpendingCondition(r);
2558
+ if (authType === AuthType.Standard) {
2559
+ return { authType: AuthType.Standard, spendingCondition };
2560
+ }
2561
+ const sponsorSpendingCondition = readSpendingCondition(r);
2562
+ return {
2563
+ authType: AuthType.Sponsored,
2564
+ spendingCondition,
2565
+ sponsorSpendingCondition
2566
+ };
2567
+ }
2568
+ function readAssetInfo(r) {
2569
+ return {
2570
+ address: readAddress(r),
2571
+ contractName: readLPString(r),
2572
+ assetName: readLPString(r)
2573
+ };
2574
+ }
2575
+ function readPostConditionPrincipal(r) {
2576
+ const type = r.readUInt8();
2577
+ switch (type) {
2578
+ case PostConditionPrincipalId.Origin:
2579
+ return { type: "origin" };
2580
+ case PostConditionPrincipalId.Standard:
2581
+ return { type: "standard", address: readAddress(r) };
2582
+ case PostConditionPrincipalId.Contract:
2583
+ return {
2584
+ type: "contract",
2585
+ address: readAddress(r),
2586
+ contractName: readLPString(r)
2587
+ };
2588
+ default:
2589
+ return { type: "origin" };
2590
+ }
2591
+ }
2592
+ function readPostConditions(r) {
2593
+ const count = r.readUInt32BE();
2594
+ const pcs = [];
2595
+ for (let i = 0;i < count; i++) {
2596
+ const assetType = r.readUInt8();
2597
+ const principal = readPostConditionPrincipal(r);
2598
+ switch (assetType) {
2599
+ case AssetType.STX:
2600
+ pcs.push({
2601
+ type: "stx",
2602
+ principal,
2603
+ conditionCode: r.readUInt8(),
2604
+ amount: r.readBigUInt64BE()
2605
+ });
2606
+ break;
2607
+ case AssetType.Fungible:
2608
+ pcs.push({
2609
+ type: "ft",
2610
+ principal,
2611
+ asset: readAssetInfo(r),
2612
+ conditionCode: r.readUInt8(),
2613
+ amount: r.readBigUInt64BE()
2614
+ });
2615
+ break;
2616
+ case AssetType.NonFungible:
2617
+ pcs.push({
2618
+ type: "nft",
2619
+ principal,
2620
+ asset: readAssetInfo(r),
2621
+ assetId: readCV(r),
2622
+ conditionCode: r.readUInt8()
2623
+ });
2624
+ break;
2625
+ case AssetType.Staking:
2626
+ pcs.push({
2627
+ type: "staking",
2628
+ principal,
2629
+ conditionCode: r.readUInt8(),
2630
+ amount: r.readBigUInt64BE()
2631
+ });
2632
+ break;
2633
+ case AssetType.Pox:
2634
+ pcs.push({
2635
+ type: "pox",
2636
+ principal,
2637
+ conditionCode: r.readUInt8()
2638
+ });
2639
+ break;
2640
+ default:
2641
+ throw new Error(`Unknown post-condition asset type: 0x${assetType.toString(16).padStart(2, "0")} (post-condition ${i + 1}/${count})`);
2642
+ }
2643
+ }
2644
+ return pcs;
2645
+ }
2646
+ function readPayload(r) {
2647
+ const payloadType = r.readUInt8();
2648
+ switch (payloadType) {
2649
+ case PayloadType.TokenTransfer: {
2650
+ const recipient = readCV(r);
2651
+ const amount = r.readBigUInt64BE();
2652
+ const memoBytes = r.readBytes(MEMO_MAX_LENGTH_BYTES);
2653
+ const memo = bytesToAscii(memoBytes).replace(/\0+$/, "");
2654
+ return {
2655
+ payloadType: PayloadType.TokenTransfer,
2656
+ recipient,
2657
+ amount,
2658
+ memo
2659
+ };
2660
+ }
2661
+ case PayloadType.ContractCall: {
2662
+ const contractAddress = readAddress(r);
2663
+ const contractName = readLPString(r);
2664
+ const functionName = readLPString(r);
2665
+ const numArgs = r.readUInt32BE();
2666
+ const functionArgs = [];
2667
+ for (let i = 0;i < numArgs; i++)
2668
+ functionArgs.push(readCV(r));
2669
+ return {
2670
+ payloadType: PayloadType.ContractCall,
2671
+ contractAddress,
2672
+ contractName,
2673
+ functionName,
2674
+ functionArgs
2675
+ };
2676
+ }
2677
+ case PayloadType.SmartContract: {
2678
+ const contractName = readLPString(r);
2679
+ const codeBody = readLPString(r, 4);
2680
+ return { payloadType: PayloadType.SmartContract, contractName, codeBody };
2681
+ }
2682
+ case PayloadType.VersionedSmartContract: {
2683
+ const clarityVersion = r.readUInt8();
2684
+ const contractName = readLPString(r);
2685
+ const codeBody = readLPString(r, 4);
2686
+ return {
2687
+ payloadType: PayloadType.VersionedSmartContract,
2688
+ clarityVersion,
2689
+ contractName,
2690
+ codeBody
2691
+ };
2692
+ }
2693
+ case PayloadType.Coinbase: {
2694
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2695
+ return { payloadType: PayloadType.Coinbase, coinbaseBuffer };
2696
+ }
2697
+ case PayloadType.CoinbaseToAltRecipient: {
2698
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2699
+ const recipient = readCV(r);
2700
+ return {
2701
+ payloadType: PayloadType.CoinbaseToAltRecipient,
2702
+ coinbaseBuffer,
2703
+ recipient
2704
+ };
2705
+ }
2706
+ case PayloadType.PoisonMicroblock: {
2707
+ const header1 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
2708
+ const header2 = bytesToHex(r.readBytes(MICROBLOCK_HEADER_BYTES_LENGTH));
2709
+ return { payloadType: PayloadType.PoisonMicroblock, header1, header2 };
2710
+ }
2711
+ case PayloadType.TenureChange: {
2712
+ const tenureConsensusHash = bytesToHex(r.readBytes(20));
2713
+ const prevTenureConsensusHash = bytesToHex(r.readBytes(20));
2714
+ const burnViewConsensusHash = bytesToHex(r.readBytes(20));
2715
+ const previousTenureEnd = bytesToHex(r.readBytes(32));
2716
+ const previousTenureBlocks = r.readUInt32BE();
2717
+ const cause = r.readUInt8();
2718
+ const pubkeyHash = bytesToHex(r.readBytes(20));
2719
+ return {
2720
+ payloadType: PayloadType.TenureChange,
2721
+ tenureConsensusHash,
2722
+ prevTenureConsensusHash,
2723
+ burnViewConsensusHash,
2724
+ previousTenureEnd,
2725
+ previousTenureBlocks,
2726
+ cause,
2727
+ pubkeyHash
2728
+ };
2729
+ }
2730
+ case PayloadType.NakamotoCoinbase: {
2731
+ const coinbaseBuffer = bytesToHex(r.readBytes(COINBASE_BYTES_LENGTH));
2732
+ const optionalCV = readCV(r);
2733
+ const recipient = optionalCV.type === "none" ? null : optionalCV.type === "some" ? optionalCV.value : optionalCV;
2734
+ const vrfProof = bytesToHex(r.readBytes(VRF_PROOF_BYTES_LENGTH));
2735
+ return {
2736
+ payloadType: PayloadType.NakamotoCoinbase,
2737
+ coinbaseBuffer,
2738
+ recipient,
2739
+ vrfProof
2740
+ };
2741
+ }
2742
+ default:
2743
+ throw new Error(`Unknown payload type: ${payloadType}`);
2744
+ }
2745
+ }
2746
+ function deserializeTransaction(input) {
2747
+ const bytes = typeof input === "string" ? hexToBytes(without0x(input)) : input;
2748
+ const r = new BytesReader(bytes);
2749
+ return {
2750
+ version: r.readUInt8(),
2751
+ chainId: r.readUInt32BE(),
2752
+ auth: readAuthorization(r),
2753
+ anchorMode: r.readUInt8(),
2754
+ postConditionMode: r.readUInt8(),
2755
+ postConditions: readPostConditions(r),
2756
+ payload: readPayload(r)
2757
+ };
2758
+ }
2759
+
2496
2760
  // src/chains/definitions.ts
2497
2761
  var mainnet = {
2498
2762
  id: 1,
@@ -2935,11 +3199,24 @@ async function contractRead(client, args) {
2935
3199
  const result = await readContract(client, args);
2936
3200
  return { result: JSON.stringify(result) };
2937
3201
  }
3202
+ function assertValidHex(raw) {
3203
+ if (raw.length % 2 !== 0) {
3204
+ throw new Error("serializedTxHex: odd-length hex string");
3205
+ }
3206
+ if (!/^[0-9a-fA-F]+$/.test(raw)) {
3207
+ throw new Error("serializedTxHex: contains non-hex characters");
3208
+ }
3209
+ }
2938
3210
  async function fee(client, serializedTxHex) {
2939
3211
  const raw = serializedTxHex.startsWith("0x") ? serializedTxHex.slice(2) : serializedTxHex;
3212
+ assertValidHex(raw);
2940
3213
  const txBytes = new Uint8Array(Buffer.from(raw, "hex"));
2941
- const { deserializeTransaction } = await import("@stacks/transactions");
2942
- const transaction = deserializeTransaction(txBytes);
3214
+ let transaction;
3215
+ try {
3216
+ transaction = deserializeTransaction(txBytes);
3217
+ } catch (e) {
3218
+ throw new Error(`serializedTxHex: deserialization failed — ${e instanceof Error ? e.message : String(e)}`);
3219
+ }
2943
3220
  const fees = await estimateFee(client, {
2944
3221
  transaction
2945
3222
  });
@@ -3139,5 +3416,5 @@ export {
3139
3416
  bnsResolve
3140
3417
  };
3141
3418
 
3142
- //# debugId=8FE40CC86F96048464756E2164756E21
3419
+ //# debugId=21FD1F399CA68BB164756E2164756E21
3143
3420
  //# sourceMappingURL=index.js.map