@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.
@@ -1 +1 @@
1
- export { buildPartialFillScript, buildPartialFillScriptHex, buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex, parsePartialFillScript, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, parsePartialFillScriptPQ, isPartialFillScriptPQ, DEFAULT_PQ_TXHASH_SELECTOR } from './partial-fill/index.js';
1
+ export { buildPartialFillScript, buildPartialFillScriptHex, buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex, parsePartialFillScript, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, parsePartialFillScriptPQ, isPartialFillScriptPQ, DEFAULT_PQ_TXHASH_SELECTOR, buildFillWitnessStack, buildCancelWitnessStack, buildCancelWitnessStackPQ } from './partial-fill/index.js';
@@ -2,5 +2,6 @@ export { buildPartialFillScript, buildPartialFillScriptHex, encodeP2PKHScriptPub
2
2
  export { buildPartialFillScriptPQ, buildPartialFillScriptPQHex, DEFAULT_PQ_TXHASH_SELECTOR } from './script-pq.js';
3
3
  export { buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex } from './spend.js';
4
4
  export { buildCancelScriptSigPQ, buildCancelScriptSigPQHex } from './spend-pq.js';
5
+ export { buildFillWitnessStack, buildCancelWitnessStack, buildCancelWitnessStackPQ } from './witness.js';
5
6
  export { parsePartialFillScript } from './parse.js';
6
7
  export { parsePartialFillScriptPQ, isPartialFillScriptPQ } from './parse-pq.js';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Witness-stack builders for the Partial-Fill Sell Order covenant.
3
+ *
4
+ * When the covenant lives behind an AuthScript commitment (the only
5
+ * asset-compatible deployment since the node's OP_XNA_ASSET placement rules
6
+ * — bare covenant outputs can no longer carry assets), the unlock data goes
7
+ * in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
8
+ * return a serialized script that PUSHES the elements; a witness needs the
9
+ * elements THEMSELVES, one per stack slot, so those blobs cannot be reused
10
+ * as a single `args` entry.
11
+ *
12
+ * These builders return the raw `args` stack (bottom → top), mirroring the
13
+ * shapes documented in `spend.ts`:
14
+ *
15
+ * Full fill: [ <1>, <0> ]
16
+ * Partial fill: [ <N>, <0>, <0> ]
17
+ * Cancel: [ <sig>, <pubkey>, <1> ]
18
+ *
19
+ * Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
20
+ * result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
21
+ * (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
22
+ * witness.
23
+ */
24
+ /**
25
+ * Witness `args` for the public fill branches. Same semantics and
26
+ * validations as `buildFillScriptSig`.
27
+ */
28
+ export declare function buildFillWitnessStack(amount: bigint, total: bigint): Uint8Array[];
29
+ /**
30
+ * Witness `args` for the seller's ECDSA cancel branch. Same semantics and
31
+ * validations as `buildCancelScriptSig`.
32
+ */
33
+ export declare function buildCancelWitnessStack(signatureDer: Uint8Array, pubKey: Uint8Array): Uint8Array[];
34
+ /**
35
+ * Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
36
+ * semantics and validations as `buildCancelScriptSigPQ`.
37
+ */
38
+ export declare function buildCancelWitnessStackPQ(sigPQ: Uint8Array, pubKey: Uint8Array): Uint8Array[];
package/dist/index.cjs CHANGED
@@ -2541,6 +2541,87 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
2541
2541
  return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
2542
2542
  }
2543
2543
 
2544
+ /**
2545
+ * Witness-stack builders for the Partial-Fill Sell Order covenant.
2546
+ *
2547
+ * When the covenant lives behind an AuthScript commitment (the only
2548
+ * asset-compatible deployment since the node's OP_XNA_ASSET placement rules
2549
+ * — bare covenant outputs can no longer carry assets), the unlock data goes
2550
+ * in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
2551
+ * return a serialized script that PUSHES the elements; a witness needs the
2552
+ * elements THEMSELVES, one per stack slot, so those blobs cannot be reused
2553
+ * as a single `args` entry.
2554
+ *
2555
+ * These builders return the raw `args` stack (bottom → top), mirroring the
2556
+ * shapes documented in `spend.ts`:
2557
+ *
2558
+ * Full fill: [ <1>, <0> ]
2559
+ * Partial fill: [ <N>, <0>, <0> ]
2560
+ * Cancel: [ <sig>, <pubkey>, <1> ]
2561
+ *
2562
+ * Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
2563
+ * result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
2564
+ * (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
2565
+ * witness.
2566
+ */
2567
+ const MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072;
2568
+ /**
2569
+ * Witness `args` for the public fill branches. Same semantics and
2570
+ * validations as `buildFillScriptSig`.
2571
+ */
2572
+ function buildFillWitnessStack(amount, total) {
2573
+ if (typeof amount !== 'bigint' || typeof total !== 'bigint') {
2574
+ throw new Error('amount and total must be bigint');
2575
+ }
2576
+ if (amount <= 0n) {
2577
+ throw new Error('fill amount must be > 0');
2578
+ }
2579
+ if (total <= 0n) {
2580
+ throw new Error('total must be > 0');
2581
+ }
2582
+ if (amount > total) {
2583
+ throw new Error('fill amount exceeds the covenant total');
2584
+ }
2585
+ if (amount === total) {
2586
+ // Full fill: covenant drains entirely; buyer does not push N.
2587
+ return [encodeScriptNum(1n), encodeScriptNum(0n)];
2588
+ }
2589
+ // Partial fill: N, then both flag values.
2590
+ return [encodeScriptNum(amount), encodeScriptNum(0n), encodeScriptNum(0n)];
2591
+ }
2592
+ /**
2593
+ * Witness `args` for the seller's ECDSA cancel branch. Same semantics and
2594
+ * validations as `buildCancelScriptSig`.
2595
+ */
2596
+ function buildCancelWitnessStack(signatureDer, pubKey) {
2597
+ if (!(signatureDer instanceof Uint8Array) || signatureDer.length === 0) {
2598
+ throw new Error('signatureDer must be a non-empty Uint8Array');
2599
+ }
2600
+ if (!(pubKey instanceof Uint8Array) || (pubKey.length !== 33 && pubKey.length !== 65)) {
2601
+ throw new Error('pubKey must be a compressed (33B) or uncompressed (65B) secp256k1 key');
2602
+ }
2603
+ return [signatureDer, pubKey, encodeScriptNum(1n)];
2604
+ }
2605
+ /**
2606
+ * Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
2607
+ * semantics and validations as `buildCancelScriptSigPQ`.
2608
+ */
2609
+ function buildCancelWitnessStackPQ(sigPQ, pubKey) {
2610
+ if (!(sigPQ instanceof Uint8Array) || sigPQ.length === 0) {
2611
+ throw new Error('sigPQ must be a non-empty Uint8Array');
2612
+ }
2613
+ if (sigPQ.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2614
+ throw new Error(`sigPQ of ${sigPQ.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2615
+ }
2616
+ if (!(pubKey instanceof Uint8Array) || pubKey.length === 0) {
2617
+ throw new Error('pubKey must be a non-empty Uint8Array');
2618
+ }
2619
+ if (pubKey.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2620
+ throw new Error(`pubKey of ${pubKey.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2621
+ }
2622
+ return [sigPQ, pubKey, encodeScriptNum(1n)];
2623
+ }
2624
+
2544
2625
  /**
2545
2626
  * Parser for the Partial-Fill Sell Order covenant (three-branch).
2546
2627
  *
@@ -2866,8 +2947,11 @@ exports.buildCancelScriptSig = buildCancelScriptSig;
2866
2947
  exports.buildCancelScriptSigHex = buildCancelScriptSigHex;
2867
2948
  exports.buildCancelScriptSigPQ = buildCancelScriptSigPQ;
2868
2949
  exports.buildCancelScriptSigPQHex = buildCancelScriptSigPQHex;
2950
+ exports.buildCancelWitnessStack = buildCancelWitnessStack;
2951
+ exports.buildCancelWitnessStackPQ = buildCancelWitnessStackPQ;
2869
2952
  exports.buildFillScriptSig = buildFillScriptSig;
2870
2953
  exports.buildFillScriptSigHex = buildFillScriptSigHex;
2954
+ exports.buildFillWitnessStack = buildFillWitnessStack;
2871
2955
  exports.buildPartialFillScript = buildPartialFillScript;
2872
2956
  exports.buildPartialFillScriptHex = buildPartialFillScriptHex;
2873
2957
  exports.buildPartialFillScriptPQ = buildPartialFillScriptPQ;