@neuraiproject/neurai-scripts 0.6.2 → 0.7.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.
@@ -2542,6 +2542,87 @@ var NeuraiScriptsBundle = (function (exports) {
2542
2542
  return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
2543
2543
  }
2544
2544
 
2545
+ /**
2546
+ * Witness-stack builders for the Partial-Fill Sell Order covenant.
2547
+ *
2548
+ * When the covenant lives behind an AuthScript commitment (the only
2549
+ * asset-compatible deployment since the node's OP_XNA_ASSET placement rules
2550
+ * — bare covenant outputs can no longer carry assets), the unlock data goes
2551
+ * in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
2552
+ * return a serialized script that PUSHES the elements; a witness needs the
2553
+ * elements THEMSELVES, one per stack slot, so those blobs cannot be reused
2554
+ * as a single `args` entry.
2555
+ *
2556
+ * These builders return the raw `args` stack (bottom → top), mirroring the
2557
+ * shapes documented in `spend.ts`:
2558
+ *
2559
+ * Full fill: [ <1>, <0> ]
2560
+ * Partial fill: [ <N>, <0>, <0> ]
2561
+ * Cancel: [ <sig>, <pubkey>, <1> ]
2562
+ *
2563
+ * Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
2564
+ * result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
2565
+ * (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
2566
+ * witness.
2567
+ */
2568
+ const MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072;
2569
+ /**
2570
+ * Witness `args` for the public fill branches. Same semantics and
2571
+ * validations as `buildFillScriptSig`.
2572
+ */
2573
+ function buildFillWitnessStack(amount, total) {
2574
+ if (typeof amount !== 'bigint' || typeof total !== 'bigint') {
2575
+ throw new Error('amount and total must be bigint');
2576
+ }
2577
+ if (amount <= 0n) {
2578
+ throw new Error('fill amount must be > 0');
2579
+ }
2580
+ if (total <= 0n) {
2581
+ throw new Error('total must be > 0');
2582
+ }
2583
+ if (amount > total) {
2584
+ throw new Error('fill amount exceeds the covenant total');
2585
+ }
2586
+ if (amount === total) {
2587
+ // Full fill: covenant drains entirely; buyer does not push N.
2588
+ return [encodeScriptNum(1n), encodeScriptNum(0n)];
2589
+ }
2590
+ // Partial fill: N, then both flag values.
2591
+ return [encodeScriptNum(amount), encodeScriptNum(0n), encodeScriptNum(0n)];
2592
+ }
2593
+ /**
2594
+ * Witness `args` for the seller's ECDSA cancel branch. Same semantics and
2595
+ * validations as `buildCancelScriptSig`.
2596
+ */
2597
+ function buildCancelWitnessStack(signatureDer, pubKey) {
2598
+ if (!(signatureDer instanceof Uint8Array) || signatureDer.length === 0) {
2599
+ throw new Error('signatureDer must be a non-empty Uint8Array');
2600
+ }
2601
+ if (!(pubKey instanceof Uint8Array) || (pubKey.length !== 33 && pubKey.length !== 65)) {
2602
+ throw new Error('pubKey must be a compressed (33B) or uncompressed (65B) secp256k1 key');
2603
+ }
2604
+ return [signatureDer, pubKey, encodeScriptNum(1n)];
2605
+ }
2606
+ /**
2607
+ * Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
2608
+ * semantics and validations as `buildCancelScriptSigPQ`.
2609
+ */
2610
+ function buildCancelWitnessStackPQ(sigPQ, pubKey) {
2611
+ if (!(sigPQ instanceof Uint8Array) || sigPQ.length === 0) {
2612
+ throw new Error('sigPQ must be a non-empty Uint8Array');
2613
+ }
2614
+ if (sigPQ.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2615
+ throw new Error(`sigPQ of ${sigPQ.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2616
+ }
2617
+ if (!(pubKey instanceof Uint8Array) || pubKey.length === 0) {
2618
+ throw new Error('pubKey must be a non-empty Uint8Array');
2619
+ }
2620
+ if (pubKey.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2621
+ throw new Error(`pubKey of ${pubKey.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2622
+ }
2623
+ return [sigPQ, pubKey, encodeScriptNum(1n)];
2624
+ }
2625
+
2545
2626
  /**
2546
2627
  * Parser for the Partial-Fill Sell Order covenant (three-branch).
2547
2628
  *
@@ -2871,8 +2952,11 @@ var NeuraiScriptsBundle = (function (exports) {
2871
2952
  buildCancelScriptSigHex: buildCancelScriptSigHex,
2872
2953
  buildCancelScriptSigPQ: buildCancelScriptSigPQ,
2873
2954
  buildCancelScriptSigPQHex: buildCancelScriptSigPQHex,
2955
+ buildCancelWitnessStack: buildCancelWitnessStack,
2956
+ buildCancelWitnessStackPQ: buildCancelWitnessStackPQ,
2874
2957
  buildFillScriptSig: buildFillScriptSig,
2875
2958
  buildFillScriptSigHex: buildFillScriptSigHex,
2959
+ buildFillWitnessStack: buildFillWitnessStack,
2876
2960
  buildPartialFillScript: buildPartialFillScript,
2877
2961
  buildPartialFillScriptHex: buildPartialFillScriptHex,
2878
2962
  buildPartialFillScriptPQ: buildPartialFillScriptPQ,