@neuraiproject/neurai-scripts 0.9.1 → 0.10.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.
@@ -136,12 +136,16 @@ export declare const CHAINCONTEXT_HEIGHT = 1;
136
136
  export declare const CHAINCONTEXT_MTP = 2;
137
137
  export declare const CHAINCONTEXT_CHAIN_ID = 3;
138
138
  export declare const OP_CHECKMERKLEINCLUSION = 193;
139
+ export declare const OP_OUTPUTAUTHDEST = 194;
140
+ export declare const OP_ZKVERIFY = 195;
141
+ export declare const OP_INPUTFIELD = 196;
139
142
  export declare const OP_CAT = 126;
140
143
  export declare const OP_SPLIT = 183;
141
144
  export declare const OP_REVERSEBYTES = 188;
142
145
  export declare const TXFIELD_VALUE = 1;
143
146
  export declare const TXFIELD_AUTHSCRIPT_COMMITMENT = 2;
144
147
  export declare const TXFIELD_SCRIPTPUBKEY = 3;
148
+ export declare const TXFIELD_AUTHDEST = 4;
145
149
  export declare const TXHASH_VERSION = 1;
146
150
  export declare const TXHASH_LOCKTIME = 2;
147
151
  export declare const TXHASH_INPUT_PREVOUTS = 4;
@@ -150,7 +154,8 @@ export declare const TXHASH_OUTPUTS = 16;
150
154
  export declare const TXHASH_CURRENT_PREVOUT = 32;
151
155
  export declare const TXHASH_CURRENT_SEQUENCE = 64;
152
156
  export declare const TXHASH_CURRENT_INDEX = 128;
153
- export declare const TXHASH_ALL = 255;
157
+ export declare const TXHASH_REFINPUTS = 256;
158
+ export declare const TXHASH_ALL = 511;
154
159
  export declare const ASSETFIELD_NAME = 1;
155
160
  export declare const ASSETFIELD_AMOUNT = 2;
156
161
  export declare const ASSETFIELD_UNITS = 3;
@@ -158,3 +163,9 @@ export declare const ASSETFIELD_REISSUABLE = 4;
158
163
  export declare const ASSETFIELD_HAS_IPFS = 5;
159
164
  export declare const ASSETFIELD_IPFS_HASH = 6;
160
165
  export declare const ASSETFIELD_TYPE = 7;
166
+ export declare const ASSETFIELD_MESSAGE = 8;
167
+ export declare const MERKLE_SCHEME_BITCOIN_NEURAI = 1;
168
+ export declare const MERKLE_SCHEME_SHA256_PLAIN = 2;
169
+ export declare const MERKLE_SCHEME_KECCAK256_PLAIN = 3;
170
+ export declare const MERKLE_SCHEME_BLAKE2B_PLAIN = 4;
171
+ export declare const MERKLE_SCHEME_POSEIDON_BN254 = 5;
@@ -34,15 +34,8 @@ export declare function decodeScriptNum(data: Uint8Array, label: string): bigint
34
34
  */
35
35
  export declare function readPushPositiveInt(c: Cursor, label: string): bigint;
36
36
  /**
37
- * Read a 1-byte selector as an UNSIGNED 8-bit integer (0..255). Accepts
38
- * two on-wire encodings, because old vs new covenant builders differ:
39
- * - `OP_1..OP_16` shorthand (single opcode) → values 1..16.
40
- * - `0x01 <byte>` raw 1-byte push → any value 1..255.
41
- *
42
- * Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
43
- * would need a 0x00 padding byte and become 2 bytes on-stack, which
44
- * consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
45
- * raw-push form unconditionally; the parser stays lenient so covenants
46
- * built by older tools (using OP_N for small values) still round-trip.
37
+ * Read a one-byte unsigned selector. This accepts OP_1..OP_16 shorthand
38
+ * and a raw one-byte push for scripts that permit either form. NIP-042
39
+ * TXHASH masks use a separate strict two-byte parser.
47
40
  */
48
41
  export declare function readPushUint8(c: Cursor, label: string): number;
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Buyer fill branches shared by both Partial-Fill Sell Order variants
3
+ * (ECDSA cancel in `./script.ts`, ML-DSA-44 cancel in `./script-pq.ts`).
4
+ * The variants only differ in their cancel branch; everything after the
5
+ * outer `OP_ELSE` is emitted and parsed here.
6
+ *
7
+ * Input-0 binding. The fill branches read the order's asset amount with
8
+ * `<0> OP_INPUTASSETFIELD` and constrain outputs 0..2 by fixed index. Input
9
+ * indices are absolute and Script cannot ask for the index of the input
10
+ * being evaluated, so without a binding a buyer could:
11
+ * - put a foreign asset UTXO at input 0 and the order at input 1: the
12
+ * order then prices the fill by the foreign amount and the rest of the
13
+ * order leaves through an unconstrained output;
14
+ * - spend several identical orders together, every one of them satisfied
15
+ * by the same payment output.
16
+ * The fill block therefore opens with
17
+ *
18
+ * <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
19
+ *
20
+ * NIP-042 mask 0x0080 commits only to the index of the input being
21
+ * evaluated, so the digest equals the constant exactly when this covenant
22
+ * is spent as input 0. At most one order per transaction can take a fill
23
+ * branch; the seller's cancel branch is not bound.
24
+ *
25
+ * Layout (after the variant's `OP_IF <cancel> OP_ELSE`):
26
+ *
27
+ * <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
28
+ * OP_IF ← full fill, unlock <1> <0>
29
+ * [expiration gate]
30
+ * <0> <2> OP_INPUTASSETFIELD (N = order amount)
31
+ * DUP <price> MUL <0> OUTPUTVALUE SWAP GE VERIFY (payment value)
32
+ * <0> OUTPUTSCRIPT <paymentSpk> EQUALVERIFY (payment destination)
33
+ * DUP <1> <2> OUTPUTASSETFIELD EQUALVERIFY (buyer amount == N)
34
+ * <1> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (buyer asset name)
35
+ * DROP <1>
36
+ * OP_ELSE ← partial fill, unlock <N> <0> <0>
37
+ * [expiration gate]
38
+ * payment value, payment destination, buyer amount, buyer name (as above)
39
+ * <2> OUTPUTAUTHCOMMITMENT <2> TXFIELD EQUALVERIFY (continuation, NIP-023)
40
+ * <2> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (continuation name)
41
+ * <2> <2> OUTPUTASSETFIELD OVER <0> <2> INPUTASSETFIELD SWAP SUB EQUALVERIFY
42
+ * DROP <1>
43
+ * OP_ENDIF
44
+ */
45
+ import type { ScriptBuilder } from '../../core/script-builder.js';
46
+ import type { Cursor } from '../../core/script-parser.js';
47
+ import type { PartialFillExpiration } from '../../types.js';
48
+ /** NIP-042 mask (two bytes, little-endian) selecting only the current input index. */
49
+ export declare const INPUT_INDEX_TXHASH_SELECTOR: Uint8Array<ArrayBuffer>;
50
+ /**
51
+ * `OP_TXHASH(0x0080)` evaluated at input 0:
52
+ * `SHA256(tag || tag || 80 00 || 00 00 00 00)` with `tag = SHA256("NeuraiTxHash")`.
53
+ */
54
+ export declare const INPUT_ZERO_TXHASH: Uint8Array<ArrayBufferLike>;
55
+ export interface FillBranchParams {
56
+ /** scriptPubKey output 0 must pay (any destination type, no asset suffix). */
57
+ paymentScriptPubKey: Uint8Array;
58
+ tokenIdBytes: Uint8Array;
59
+ unitPriceSats: bigint;
60
+ expiration?: PartialFillExpiration;
61
+ }
62
+ export interface ParsedFillBranches {
63
+ paymentScriptPubKey: Uint8Array;
64
+ tokenId: string;
65
+ unitPriceSats: bigint;
66
+ expiration?: PartialFillExpiration;
67
+ }
68
+ /** Emit the input-0 binding and both fill branches, ending with the inner `OP_ENDIF`. */
69
+ export declare function appendFillBranches(b: ScriptBuilder, p: FillBranchParams): void;
70
+ /**
71
+ * Parse what `appendFillBranches` emits. Both branches must agree on price,
72
+ * payment destination, tokenId and expiration; any deviation throws.
73
+ */
74
+ export declare function readFillBranches(c: Cursor): ParsedFillBranches;
@@ -4,9 +4,8 @@
4
4
  * payment scriptPubKey bytes (which may be P2PKH or AuthScript) and the
5
5
  * configured TXHASH selector.
6
6
  *
7
- * The full-fill and partial-fill branches share `(paymentScriptPubKey,
8
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
9
- * consistency; inconsistency throws.
7
+ * The fill branches (and their input-0 binding) are read by the shared
8
+ * `readFillBranches`; covenants built before the binding are rejected.
10
9
  */
11
10
  import type { Network, ParsedPartialFillOrderPQ } from '../../types.js';
12
11
  /** Quick discriminator without throwing — useful for indexers. */
@@ -1,15 +1,13 @@
1
1
  /**
2
- * Parser for the Partial-Fill Sell Order covenant (three-branch).
2
+ * Parser for the Partial-Fill Sell Order covenant (three-branch, ECDSA cancel).
3
3
  *
4
- * Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
5
- * that was produced by `buildPartialFillScript`. Walks the exact byte
6
- * layout emitted by the builder and fails on any deviation — this is
7
- * deliberate, so a downstream indexer can unambiguously classify a UTXO as
8
- * "partial-fill order" or "unknown script" with no false positives.
9
- *
10
- * The full-fill and partial-fill branches share `(sellerScriptPubKey,
11
- * unitPriceSats, tokenId)`. The parser reads both branches and verifies
12
- * consistency; inconsistency throws.
4
+ * Extracts `(sellerPubKeyHash, paymentScriptPubKey, unitPriceSats, tokenId)`
5
+ * from a scriptPubKey that was produced by `buildPartialFillScript`. Walks
6
+ * the exact byte layout emitted by the builder and fails on any deviation —
7
+ * this is deliberate, so a downstream indexer can unambiguously classify a
8
+ * UTXO as "partial-fill order" or "unknown script" with no false positives.
9
+ * Covenants built before the input-0 binding (neurai-scripts < 0.10.0) are
10
+ * rejected: their fill branches can be drained through a foreign input 0.
13
11
  */
14
12
  import type { Network, ParsedPartialFillOrder } from '../../types.js';
15
13
  /**
@@ -19,14 +19,14 @@
19
19
  *
20
20
  * The message that gets signed is **SHA256(OP_TXHASH(selector))** — CSFS
21
21
  * single-SHA256s its message argument before verification, and OP_TXHASH
22
- * produces its own 32-byte hash, so the seller computes:
23
- * `sign(pqSeckey, SHA256(doubleSHA256(selected_tx_fields)))`
22
+ * produces its own 32-byte NIP-042 tagged hash, so the seller computes:
23
+ * `sign(pqSeckey, SHA256(TaggedHash("NeuraiTxHash", mask || selected_tx_fields)))`
24
24
  *
25
- * Fill branches are identical in structure to the legacy variant — see
26
- * `./script.ts` for the three-branch layout description.
25
+ * The fill branches, including the input-0 binding, are shared with the
26
+ * ECDSA variant — see `./fill-branches.ts`.
27
27
  */
28
28
  import type { PartialFillOrderPQParams } from '../../types.js';
29
- export declare const DEFAULT_PQ_TXHASH_SELECTOR = 255;
29
+ export declare const DEFAULT_PQ_TXHASH_SELECTOR = 511;
30
30
  /**
31
31
  * Build the scriptPubKey of a PQ Partial-Fill Sell Order covenant UTXO.
32
32
  */
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Partial-Fill Sell Order covenant script (three-branch).
2
+ * Partial-Fill Sell Order covenant script (three-branch), ECDSA cancel.
3
3
  *
4
4
  * The covenant has three branches selected by the top of the unlock stack:
5
5
  *
@@ -19,11 +19,8 @@
19
19
  * amount == inputAmount - N)
20
20
  * output[3+] = optional buyer change (not constrained by the covenant)
21
21
  *
22
- * Continuity — the remainder UTXO reuses the spent covenant's AuthScript v1
23
- * commitment via `OP_OUTPUTAUTHCOMMITMENT(2) == OP_TXFIELD(0x02)` (NIP-023).
24
- * Comparing commitments rather than full scriptPubKeys is mandatory because
25
- * the remainder's asset wrapper carries a different `amountRaw` than the
26
- * spent UTXO's wrapper.
22
+ * The order must be input 0 for either fill branch to succeed: see the
23
+ * input-0 binding in `./fill-branches.ts`.
27
24
  *
28
25
  * Unlock stack shapes (scriptSig pushes, top → bottom):
29
26
  *
@@ -31,10 +28,12 @@
31
28
  * Full fill: <1> <0> (full-flag=1, cancel-flag=0)
32
29
  * Partial fill: <N> <0> <0> (N, full-flag=0, cancel-flag=0)
33
30
  *
34
- * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
35
- * commits to a 20-byte PKH — this variant only accepts legacy P2PKH
36
- * addresses as the seller destination. For AuthScript bech32m destinations
37
- * and post-quantum signing, use `buildPartialFillScriptPQ`.
31
+ * The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`)
32
+ * against a 20-byte key hash. The payment destination is independent of
33
+ * that key: a legacy P2PKH seller passes `sellerAddress` (both at once),
34
+ * any other seller with a secp256k1 key — typically a strict ECDSA v3
35
+ * `nq1r…` wallet — passes `paymentAddress` and `sellerPubKeyHash`. For
36
+ * post-quantum cancellation use `buildPartialFillScriptPQ`.
38
37
  */
39
38
  import type { PartialFillOrderParams } from '../../types.js';
40
39
  export { encodeP2PKHScriptPubKey } from '../../standard/p2pkh.js';