@neuraiproject/neurai-scripts 0.1.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.
- package/LICENSE +21 -0
- package/README.md +546 -0
- package/dist/address.d.ts +31 -0
- package/dist/core/bytes.d.ts +10 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/opcodes.d.ts +145 -0
- package/dist/core/script-builder.d.ts +46 -0
- package/dist/core/script-parser.d.ts +35 -0
- package/dist/covenants/index.d.ts +1 -0
- package/dist/covenants/partial-fill/index.d.ts +6 -0
- package/dist/covenants/partial-fill/parse-pq.d.ts +14 -0
- package/dist/covenants/partial-fill/parse.d.ts +15 -0
- package/dist/covenants/partial-fill/script-pq.d.ts +31 -0
- package/dist/covenants/partial-fill/script.d.ts +37 -0
- package/dist/covenants/partial-fill/spend-pq.d.ts +23 -0
- package/dist/covenants/partial-fill/spend.d.ts +40 -0
- package/dist/entries/browser.d.ts +1 -0
- package/dist/entries/global.d.ts +2 -0
- package/dist/entries/index.d.ts +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/standard/authscript.d.ts +88 -0
- package/dist/standard/index.d.ts +9 -0
- package/dist/standard/multisig.d.ts +26 -0
- package/dist/standard/nulldata.d.ts +15 -0
- package/dist/standard/p2pkh.d.ts +5 -0
- package/dist/standard/p2wpkh.d.ts +8 -0
- package/dist/standard/p2wsh.d.ts +9 -0
- package/dist/types.d.ts +122 -0
- package/package.json +69 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opcode constants for Neurai Script.
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - Classic Script opcodes used by covenants.
|
|
6
|
+
* - New opcodes activated in the `DePIN-Test` branch (BIP 119, BIP 347 and
|
|
7
|
+
* Neurai-specific introspection / asset / arithmetic additions).
|
|
8
|
+
*
|
|
9
|
+
* Authority: `src/script/script.h` (enum `opcodetype`) in the Neurai repo.
|
|
10
|
+
* Reference: `doc/new-opcodes-depin-branch.md`.
|
|
11
|
+
*
|
|
12
|
+
* Disabled in consensus and intentionally omitted: OP_SUBSTR, OP_LEFT,
|
|
13
|
+
* OP_RIGHT, OP_INVERT, OP_AND, OP_OR, OP_XOR, OP_2MUL, OP_2DIV, OP_LSHIFT,
|
|
14
|
+
* OP_RSHIFT. Also omitted: OP_VER / OP_VERIF / OP_VERNOTIF (reserved), the
|
|
15
|
+
* template-matching pseudo-opcodes (OP_SMALLINTEGER, OP_PUBKEYS,
|
|
16
|
+
* OP_PUBKEYHASH, OP_PUBKEY) and OP_INVALIDOPCODE.
|
|
17
|
+
*/
|
|
18
|
+
export declare const OP_0 = 0;
|
|
19
|
+
export declare const OP_FALSE = 0;
|
|
20
|
+
export declare const OP_PUSHDATA1 = 76;
|
|
21
|
+
export declare const OP_PUSHDATA2 = 77;
|
|
22
|
+
export declare const OP_PUSHDATA4 = 78;
|
|
23
|
+
export declare const OP_1NEGATE = 79;
|
|
24
|
+
export declare const OP_RESERVED = 80;
|
|
25
|
+
export declare const OP_1 = 81;
|
|
26
|
+
export declare const OP_TRUE = 81;
|
|
27
|
+
export declare const OP_2 = 82;
|
|
28
|
+
export declare const OP_3 = 83;
|
|
29
|
+
export declare const OP_4 = 84;
|
|
30
|
+
export declare const OP_5 = 85;
|
|
31
|
+
export declare const OP_6 = 86;
|
|
32
|
+
export declare const OP_7 = 87;
|
|
33
|
+
export declare const OP_8 = 88;
|
|
34
|
+
export declare const OP_9 = 89;
|
|
35
|
+
export declare const OP_10 = 90;
|
|
36
|
+
export declare const OP_11 = 91;
|
|
37
|
+
export declare const OP_12 = 92;
|
|
38
|
+
export declare const OP_13 = 93;
|
|
39
|
+
export declare const OP_14 = 94;
|
|
40
|
+
export declare const OP_15 = 95;
|
|
41
|
+
export declare const OP_16 = 96;
|
|
42
|
+
export declare const OP_NOP = 97;
|
|
43
|
+
export declare const OP_IF = 99;
|
|
44
|
+
export declare const OP_NOTIF = 100;
|
|
45
|
+
export declare const OP_ELSE = 103;
|
|
46
|
+
export declare const OP_ENDIF = 104;
|
|
47
|
+
export declare const OP_VERIFY = 105;
|
|
48
|
+
export declare const OP_RETURN = 106;
|
|
49
|
+
export declare const OP_TOALTSTACK = 107;
|
|
50
|
+
export declare const OP_FROMALTSTACK = 108;
|
|
51
|
+
export declare const OP_2DROP = 109;
|
|
52
|
+
export declare const OP_2DUP = 110;
|
|
53
|
+
export declare const OP_3DUP = 111;
|
|
54
|
+
export declare const OP_2OVER = 112;
|
|
55
|
+
export declare const OP_2ROT = 113;
|
|
56
|
+
export declare const OP_2SWAP = 114;
|
|
57
|
+
export declare const OP_IFDUP = 115;
|
|
58
|
+
export declare const OP_DEPTH = 116;
|
|
59
|
+
export declare const OP_DROP = 117;
|
|
60
|
+
export declare const OP_DUP = 118;
|
|
61
|
+
export declare const OP_NIP = 119;
|
|
62
|
+
export declare const OP_OVER = 120;
|
|
63
|
+
export declare const OP_PICK = 121;
|
|
64
|
+
export declare const OP_ROLL = 122;
|
|
65
|
+
export declare const OP_ROT = 123;
|
|
66
|
+
export declare const OP_SWAP = 124;
|
|
67
|
+
export declare const OP_TUCK = 125;
|
|
68
|
+
export declare const OP_SIZE = 130;
|
|
69
|
+
export declare const OP_EQUAL = 135;
|
|
70
|
+
export declare const OP_EQUALVERIFY = 136;
|
|
71
|
+
export declare const OP_1ADD = 139;
|
|
72
|
+
export declare const OP_1SUB = 140;
|
|
73
|
+
export declare const OP_NEGATE = 143;
|
|
74
|
+
export declare const OP_ABS = 144;
|
|
75
|
+
export declare const OP_NOT = 145;
|
|
76
|
+
export declare const OP_0NOTEQUAL = 146;
|
|
77
|
+
export declare const OP_ADD = 147;
|
|
78
|
+
export declare const OP_SUB = 148;
|
|
79
|
+
export declare const OP_MUL = 149;
|
|
80
|
+
export declare const OP_DIV = 150;
|
|
81
|
+
export declare const OP_MOD = 151;
|
|
82
|
+
export declare const OP_BOOLAND = 154;
|
|
83
|
+
export declare const OP_BOOLOR = 155;
|
|
84
|
+
export declare const OP_NUMEQUAL = 156;
|
|
85
|
+
export declare const OP_NUMEQUALVERIFY = 157;
|
|
86
|
+
export declare const OP_NUMNOTEQUAL = 158;
|
|
87
|
+
export declare const OP_LESSTHAN = 159;
|
|
88
|
+
export declare const OP_GREATERTHAN = 160;
|
|
89
|
+
export declare const OP_LESSTHANOREQUAL = 161;
|
|
90
|
+
export declare const OP_GREATERTHANOREQUAL = 162;
|
|
91
|
+
export declare const OP_MIN = 163;
|
|
92
|
+
export declare const OP_MAX = 164;
|
|
93
|
+
export declare const OP_WITHIN = 165;
|
|
94
|
+
export declare const OP_RIPEMD160 = 166;
|
|
95
|
+
export declare const OP_SHA1 = 167;
|
|
96
|
+
export declare const OP_SHA256 = 168;
|
|
97
|
+
export declare const OP_HASH160 = 169;
|
|
98
|
+
export declare const OP_HASH256 = 170;
|
|
99
|
+
export declare const OP_CODESEPARATOR = 171;
|
|
100
|
+
export declare const OP_CHECKSIG = 172;
|
|
101
|
+
export declare const OP_CHECKSIGVERIFY = 173;
|
|
102
|
+
export declare const OP_CHECKMULTISIG = 174;
|
|
103
|
+
export declare const OP_CHECKMULTISIGVERIFY = 175;
|
|
104
|
+
export declare const OP_NOP1 = 176;
|
|
105
|
+
export declare const OP_NOP9 = 184;
|
|
106
|
+
export declare const OP_NOP10 = 185;
|
|
107
|
+
export declare const OP_CHECKLOCKTIMEVERIFY = 177;
|
|
108
|
+
export declare const OP_CHECKSEQUENCEVERIFY = 178;
|
|
109
|
+
export declare const OP_CHECKTEMPLATEVERIFY = 179;
|
|
110
|
+
export declare const OP_CHECKSIGFROMSTACK = 180;
|
|
111
|
+
export declare const OP_TXHASH = 181;
|
|
112
|
+
export declare const OP_TXFIELD = 182;
|
|
113
|
+
export declare const OP_TXLOCKTIME = 197;
|
|
114
|
+
export declare const OP_OUTPUTVALUE = 204;
|
|
115
|
+
export declare const OP_OUTPUTSCRIPT = 205;
|
|
116
|
+
export declare const OP_INPUTCOUNT = 208;
|
|
117
|
+
export declare const OP_OUTPUTCOUNT = 209;
|
|
118
|
+
export declare const OP_OUTPUTASSETFIELD = 206;
|
|
119
|
+
export declare const OP_INPUTASSETFIELD = 207;
|
|
120
|
+
export declare const OP_XNA_ASSET = 192;
|
|
121
|
+
export declare const OP_REFINPUTFIELD = 210;
|
|
122
|
+
export declare const OP_REFINPUTASSETFIELD = 211;
|
|
123
|
+
export declare const OP_REFINPUTCOUNT = 212;
|
|
124
|
+
export declare const OP_CAT = 126;
|
|
125
|
+
export declare const OP_SPLIT = 183;
|
|
126
|
+
export declare const OP_REVERSEBYTES = 188;
|
|
127
|
+
export declare const TXFIELD_VALUE = 1;
|
|
128
|
+
export declare const TXFIELD_AUTHSCRIPT_COMMITMENT = 2;
|
|
129
|
+
export declare const TXFIELD_SCRIPTPUBKEY = 3;
|
|
130
|
+
export declare const TXHASH_VERSION = 1;
|
|
131
|
+
export declare const TXHASH_LOCKTIME = 2;
|
|
132
|
+
export declare const TXHASH_INPUT_PREVOUTS = 4;
|
|
133
|
+
export declare const TXHASH_INPUT_SEQUENCES = 8;
|
|
134
|
+
export declare const TXHASH_OUTPUTS = 16;
|
|
135
|
+
export declare const TXHASH_CURRENT_PREVOUT = 32;
|
|
136
|
+
export declare const TXHASH_CURRENT_SEQUENCE = 64;
|
|
137
|
+
export declare const TXHASH_CURRENT_INDEX = 128;
|
|
138
|
+
export declare const TXHASH_ALL = 255;
|
|
139
|
+
export declare const ASSETFIELD_NAME = 1;
|
|
140
|
+
export declare const ASSETFIELD_AMOUNT = 2;
|
|
141
|
+
export declare const ASSETFIELD_UNITS = 3;
|
|
142
|
+
export declare const ASSETFIELD_REISSUABLE = 4;
|
|
143
|
+
export declare const ASSETFIELD_HAS_IPFS = 5;
|
|
144
|
+
export declare const ASSETFIELD_IPFS_HASH = 6;
|
|
145
|
+
export declare const ASSETFIELD_TYPE = 7;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Low-level Script assembler. Emits the exact byte layout expected by the
|
|
3
|
+
* Neurai interpreter: pushdata prefixes follow the same rules as Bitcoin
|
|
4
|
+
* (direct push for 1..75 bytes, OP_PUSHDATA1/2/4 otherwise), and integers
|
|
5
|
+
* are minimally-encoded as CScriptNum.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Minimal CScriptNum encoding (Bitcoin consensus rules).
|
|
9
|
+
*
|
|
10
|
+
* - 0 → empty vector
|
|
11
|
+
* - 1..16 → single opcode OP_1..OP_16 (handled in `pushInt`, not here)
|
|
12
|
+
* - -1 → OP_1NEGATE (handled in `pushInt`)
|
|
13
|
+
* - otherwise: sign-magnitude little-endian, with a sign bit on the last byte
|
|
14
|
+
*/
|
|
15
|
+
export declare function encodeScriptNum(value: bigint | number): Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* Emit a pushdata opcode followed by the payload. Chooses the shortest
|
|
18
|
+
* valid encoding.
|
|
19
|
+
*
|
|
20
|
+
* Neurai Script caps stack elements at `MAX_SCRIPT_ELEMENT_SIZE = 520`
|
|
21
|
+
* bytes by default, but NIP-18 raises the cap to
|
|
22
|
+
* `MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072` whenever
|
|
23
|
+
* `SCRIPT_VERIFY_CHECKSIGFROMSTACK` is active (testnet today, mainnet once
|
|
24
|
+
* the CSFS fork lands). The builder here emits pushes up to 3072 bytes so
|
|
25
|
+
* it can target either regime; legacy scripts should stay under 520 bytes
|
|
26
|
+
* per push, and the node will reject anything larger if the CSFS flag is
|
|
27
|
+
* not set during evaluation.
|
|
28
|
+
*/
|
|
29
|
+
export declare function pushBytes(data: Uint8Array): Uint8Array;
|
|
30
|
+
/**
|
|
31
|
+
* Emit a minimally-encoded integer push. Uses OP_1NEGATE, OP_0 and OP_1..OP_16
|
|
32
|
+
* when available to match how the node's own templates look on the wire.
|
|
33
|
+
*/
|
|
34
|
+
export declare function pushInt(value: bigint | number): Uint8Array;
|
|
35
|
+
export declare function pushHex(hex: string): Uint8Array;
|
|
36
|
+
/** Fluent assembler for readable script definitions. */
|
|
37
|
+
export declare class ScriptBuilder {
|
|
38
|
+
private readonly parts;
|
|
39
|
+
op(...opcodes: number[]): this;
|
|
40
|
+
pushInt(value: bigint | number): this;
|
|
41
|
+
pushBytes(data: Uint8Array): this;
|
|
42
|
+
pushHex(hex: string): this;
|
|
43
|
+
raw(bytes: Uint8Array): this;
|
|
44
|
+
build(): Uint8Array;
|
|
45
|
+
buildHex(): string;
|
|
46
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared parsing primitives for strict covenant parsers. Each covenant
|
|
3
|
+
* parser walks the exact byte layout emitted by its builder and fails on
|
|
4
|
+
* any deviation; the primitives here centralize the cursor arithmetic,
|
|
5
|
+
* pushdata decoding, and CScriptNum decoding so the legacy and PQ parsers
|
|
6
|
+
* (and future covenant parsers) cannot drift in rigor.
|
|
7
|
+
*/
|
|
8
|
+
export interface Cursor {
|
|
9
|
+
readonly bytes: Uint8Array;
|
|
10
|
+
pos: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function makeCursor(bytes: Uint8Array): Cursor;
|
|
13
|
+
/** Consume one byte and verify it equals `expected`. */
|
|
14
|
+
export declare function expectByte(c: Cursor, expected: number, label: string): void;
|
|
15
|
+
/** Fail if the cursor has not consumed every byte of the script. */
|
|
16
|
+
export declare function assertTrailing(c: Cursor): void;
|
|
17
|
+
/**
|
|
18
|
+
* Read one pushdata element from the cursor. Supports direct pushes
|
|
19
|
+
* (1..75 bytes), `OP_PUSHDATA1` and `OP_PUSHDATA2`. `OP_PUSHDATA4` is not
|
|
20
|
+
* supported by any current covenant script and would overflow the
|
|
21
|
+
* per-element cap anyway. Truncation is checked for all length fields
|
|
22
|
+
* and payload ranges.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readPush(c: Cursor, label: string): Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* Decode a `CScriptNum` byte vector (little-endian sign-magnitude, up to
|
|
27
|
+
* 8 bytes) into a BigInt. Empty vector encodes 0.
|
|
28
|
+
*/
|
|
29
|
+
export declare function decodeScriptNum(data: Uint8Array, label: string): bigint;
|
|
30
|
+
/**
|
|
31
|
+
* Read a push as a non-negative CScriptNum. Recognises OP_1..OP_16
|
|
32
|
+
* shorthand. `OP_0` is not accepted because the covenant callers use this
|
|
33
|
+
* only for values that are strictly positive (prices, selectors, indices).
|
|
34
|
+
*/
|
|
35
|
+
export declare function readPushPositiveInt(c: Cursor, label: string): bigint;
|
|
@@ -0,0 +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';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { buildPartialFillScript, buildPartialFillScriptHex, encodeP2PKHScriptPubKey } from './script.js';
|
|
2
|
+
export { buildPartialFillScriptPQ, buildPartialFillScriptPQHex, DEFAULT_PQ_TXHASH_SELECTOR } from './script-pq.js';
|
|
3
|
+
export { buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex } from './spend.js';
|
|
4
|
+
export { buildCancelScriptSigPQ, buildCancelScriptSigPQHex } from './spend-pq.js';
|
|
5
|
+
export { parsePartialFillScript } from './parse.js';
|
|
6
|
+
export { parsePartialFillScriptPQ, isPartialFillScriptPQ } from './parse-pq.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parser for the PQ Partial-Fill Sell Order covenant. Returns the same
|
|
3
|
+
* economic parameters as the legacy parser, plus the payment scriptPubKey
|
|
4
|
+
* bytes (which may be P2PKH or AuthScript) and the configured TXHASH
|
|
5
|
+
* selector.
|
|
6
|
+
*/
|
|
7
|
+
import type { Network, ParsedPartialFillOrderPQ } from '../../types.js';
|
|
8
|
+
/** Quick discriminator without throwing — useful for indexers. */
|
|
9
|
+
export declare function isPartialFillScriptPQ(script: Uint8Array | string): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Parse a PQ partial-fill covenant. Throws if the bytes do not match the
|
|
12
|
+
* exact layout produced by `buildPartialFillScriptPQ`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function parsePartialFillScriptPQ(script: Uint8Array | string, network?: Network): ParsedPartialFillOrderPQ;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parser for the Partial-Fill Sell Order covenant.
|
|
3
|
+
*
|
|
4
|
+
* Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)` from a scriptPubKey
|
|
5
|
+
* that was produced by `buildPartialFillScript`. The parser walks the exact
|
|
6
|
+
* byte 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
|
+
import type { Network, ParsedPartialFillOrder } from '../../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Parse a covenant scriptPubKey and extract its parameters. Throws with a
|
|
13
|
+
* descriptive message if the bytes don't match the partial-fill template.
|
|
14
|
+
*/
|
|
15
|
+
export declare function parsePartialFillScript(script: Uint8Array | string, network?: Network): ParsedPartialFillOrder;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PQ (post-quantum) variant of the Partial-Fill Sell Order covenant.
|
|
3
|
+
*
|
|
4
|
+
* Identical partial-fill branch to the legacy covenant, but the **cancel**
|
|
5
|
+
* branch accepts an ML-DSA-44 signature instead of an ECDSA one. This
|
|
6
|
+
* requires:
|
|
7
|
+
* - `SCRIPT_VERIFY_CHECKSIGFROMSTACK` (for OP_CSFS) active
|
|
8
|
+
* - `SCRIPT_VERIFY_TXHASH` (for OP_TXHASH) active
|
|
9
|
+
* - NIP-18 (`MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072`) active, so the ~2.4 KB
|
|
10
|
+
* signature and ~1.3 KB pubkey can be pushed to the script stack
|
|
11
|
+
*
|
|
12
|
+
* All three are active on DePIN-Test testnet.
|
|
13
|
+
*
|
|
14
|
+
* Cancel-branch flow (scriptSig pushes `<sig> <pubkey> OP_1`):
|
|
15
|
+
*
|
|
16
|
+
* 1. OP_DUP OP_SHA256 <commitment> OP_EQUALVERIFY → pubkey matches
|
|
17
|
+
* 2. <selector> OP_TXHASH → message = H(tx)
|
|
18
|
+
* 3. OP_SWAP OP_CHECKSIGFROMSTACK → CSFS verifies sig
|
|
19
|
+
*
|
|
20
|
+
* The message that gets signed is **SHA256(OP_TXHASH(selector))** — CSFS
|
|
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)))`
|
|
24
|
+
*/
|
|
25
|
+
import type { PartialFillOrderPQParams } from '../../types.js';
|
|
26
|
+
export declare const DEFAULT_PQ_TXHASH_SELECTOR = 255;
|
|
27
|
+
/**
|
|
28
|
+
* Build the scriptPubKey of a PQ Partial-Fill Sell Order covenant UTXO.
|
|
29
|
+
*/
|
|
30
|
+
export declare function buildPartialFillScriptPQ(params: PartialFillOrderPQParams): Uint8Array;
|
|
31
|
+
export declare function buildPartialFillScriptPQHex(params: PartialFillOrderPQParams): string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Partial-Fill Sell Order covenant script.
|
|
3
|
+
*
|
|
4
|
+
* The covenant has two branches selected by the top of the unlock stack:
|
|
5
|
+
*
|
|
6
|
+
* OP_IF (unlock pushed 1) → Cancel: seller signs, recovers remainder.
|
|
7
|
+
* OP_ELSE (unlock pushed 0) → Public partial fill: buyer provides the
|
|
8
|
+
* fill amount `N` and the tx layout is
|
|
9
|
+
* validated byte by byte.
|
|
10
|
+
*
|
|
11
|
+
* The partial-fill branch enforces a fixed output layout so that the script
|
|
12
|
+
* stays compact and parseable:
|
|
13
|
+
*
|
|
14
|
+
* output[0] = XNA payment to seller (value >= N * unitPriceSats)
|
|
15
|
+
* output[1] = asset to buyer (tokenId, amount == N)
|
|
16
|
+
* output[2] = covenant remainder (same scriptPubKey, amount == in - N)
|
|
17
|
+
* output[3+] = optional buyer change (not constrained by the covenant)
|
|
18
|
+
*
|
|
19
|
+
* The remainder UTXO reuses the spent scriptPubKey via
|
|
20
|
+
* `OP_OUTPUTSCRIPT == OP_TXFIELD 0x03`, so the covenant is self-replicating
|
|
21
|
+
* without hardcoding its own hash.
|
|
22
|
+
*
|
|
23
|
+
* The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`) and
|
|
24
|
+
* commits to a 20-byte PKH. As a consequence this variant only accepts
|
|
25
|
+
* legacy P2PKH addresses as the seller destination. For AuthScript bech32m
|
|
26
|
+
* destinations and post-quantum signing, use `buildPartialFillScriptPQ`.
|
|
27
|
+
*/
|
|
28
|
+
import type { PartialFillOrderParams } from '../../types.js';
|
|
29
|
+
export { encodeP2PKHScriptPubKey } from '../../standard/p2pkh.js';
|
|
30
|
+
/**
|
|
31
|
+
* Build the `scriptPubKey` of a Partial-Fill Sell Order covenant UTXO.
|
|
32
|
+
* Returns raw bytes; wrap with `bytesToHex` for wire format.
|
|
33
|
+
*/
|
|
34
|
+
export declare function buildPartialFillScript(params: PartialFillOrderParams): Uint8Array;
|
|
35
|
+
/** Hex convenience wrapper for `buildPartialFillScript`. */
|
|
36
|
+
export declare function buildPartialFillScriptHex(params: PartialFillOrderParams): string;
|
|
37
|
+
export { pushBytes, pushInt } from '../../core/script-builder.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scriptSig builders for the PQ Partial-Fill Sell Order covenant.
|
|
3
|
+
*
|
|
4
|
+
* The fill branch is identical to the legacy variant (just pushes `<N> <0>`),
|
|
5
|
+
* so callers reuse `buildFillScriptSig` from `./spend.ts`. Only the cancel
|
|
6
|
+
* branch is different: it carries a full PQ pubkey (~1313 B) and signature
|
|
7
|
+
* (~2421 B), which requires NIP-18's expanded element-size cap.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Build the scriptSig that unlocks the PQ cancel branch.
|
|
11
|
+
*
|
|
12
|
+
* Stack ordering pushed (bottom → top): `sig, pubkey, 1`.
|
|
13
|
+
*
|
|
14
|
+
* @param sigPQ ML-DSA-44 signature over `SHA256(OP_TXHASH(selector))`,
|
|
15
|
+
* **with trailing sighash byte appended** (same convention as
|
|
16
|
+
* OP_CHECKSIG — CSFS strips it before verification). Expected
|
|
17
|
+
* length 2421 bytes for ML-DSA-44.
|
|
18
|
+
* @param pubKey Versioned PQ public key bytes, 1313 bytes (1-byte prefix
|
|
19
|
+
* + 1312-byte ML-DSA-44 key). Must hash (via SHA256) to the
|
|
20
|
+
* `pubKeyCommitment` embedded in the covenant scriptPubKey.
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildCancelScriptSigPQ(sigPQ: Uint8Array, pubKey: Uint8Array): Uint8Array;
|
|
23
|
+
export declare function buildCancelScriptSigPQHex(sigPQ: Uint8Array, pubKey: Uint8Array): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scriptSig builders for the Partial-Fill Sell Order covenant.
|
|
3
|
+
*
|
|
4
|
+
* These return the raw scriptSig bytes that unlock the covenant UTXO. The
|
|
5
|
+
* surrounding transaction (inputs, outputs, fees, signatures on the buyer's
|
|
6
|
+
* own inputs, etc.) is the caller's responsibility — stitch them together
|
|
7
|
+
* with `@neuraiproject/neurai-create-transaction`.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Unlock the covenant via the public partial-fill branch.
|
|
11
|
+
*
|
|
12
|
+
* Layout on the stack before OP_IF executes (top → bottom):
|
|
13
|
+
* 0 ← selects the OP_ELSE (fill) branch
|
|
14
|
+
* N ← amount of asset the buyer is taking from this order
|
|
15
|
+
*
|
|
16
|
+
* so the scriptSig pushes `<N>` then `<0>`.
|
|
17
|
+
*
|
|
18
|
+
* @param amount units of the asset the buyer is taking. Must be > 0 and
|
|
19
|
+
* strictly less than the amount locked in the order UTXO
|
|
20
|
+
* (equal would leave a zero-asset remainder, which isn't a
|
|
21
|
+
* valid Neurai transfer output).
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildFillScriptSig(amount: bigint): Uint8Array;
|
|
24
|
+
export declare function buildFillScriptSigHex(amount: bigint): string;
|
|
25
|
+
/**
|
|
26
|
+
* Unlock the covenant via the seller's cancel branch.
|
|
27
|
+
*
|
|
28
|
+
* Stack before OP_IF (top → bottom):
|
|
29
|
+
* 1 ← selects the OP_IF branch
|
|
30
|
+
* pubkey ← consumed by OP_DUP OP_HASH160 … OP_EQUALVERIFY
|
|
31
|
+
* sig ← consumed by OP_CHECKSIG
|
|
32
|
+
*
|
|
33
|
+
* so the scriptSig pushes `<sig> <pubkey> <1>`.
|
|
34
|
+
*
|
|
35
|
+
* The signature is on whatever sighash the caller picked; this builder does
|
|
36
|
+
* not hash or sign, it only assembles the unlock script once a signature is
|
|
37
|
+
* available.
|
|
38
|
+
*/
|
|
39
|
+
export declare function buildCancelScriptSig(signatureDer: Uint8Array, pubKey: Uint8Array): Uint8Array;
|
|
40
|
+
export declare function buildCancelScriptSigHex(signatureDer: Uint8Array, pubKey: Uint8Array): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../index.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../index.js';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { Network, OrderUtxo, ParsedPartialFillOrder, ParsedPartialFillOrderPQ, PartialFillOrderParams, PartialFillOrderPQParams, TxInputRef } from './types.js';
|
|
2
|
+
export { bytesToHex, hexToBytes, concatBytes, bytesEqual, ensureHex } from './core/bytes.js';
|
|
3
|
+
export { ScriptBuilder, encodeScriptNum, pushBytes, pushInt, pushHex } from './core/script-builder.js';
|
|
4
|
+
export * as opcodes from './core/opcodes.js';
|
|
5
|
+
export { encodeP2PKHScriptPubKey, encodeP2WPKHScriptPubKey, encodeP2WSHScriptPubKey, encodeAuthScriptScriptPubKey, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessPQ, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessRef, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_LEGACY, AUTHSCRIPT_REF, encodeNullDataScript, NULLDATA_STANDARD_MAX_SIZE, encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeP2SHScriptPubKey, MULTISIG_MAX_PUBKEYS } from './standard/index.js';
|
|
6
|
+
export type { AuthType, AuthScriptWitnessLegacyInput, AuthScriptWitnessPQInput, AuthScriptWitnessNoAuthInput, AuthScriptWitnessRefInput, EncodeNullDataOptions, MultisigParams } from './standard/index.js';
|
|
7
|
+
export { encodeSellerScriptPubKey } from './address.js';
|
|
8
|
+
export type { SellerAddressKind, SellerScriptPubKey } from './address.js';
|
|
9
|
+
export { buildPartialFillScript, buildPartialFillScriptHex, buildFillScriptSig, buildFillScriptSigHex, buildCancelScriptSig, buildCancelScriptSigHex, parsePartialFillScript, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, parsePartialFillScriptPQ, isPartialFillScriptPQ, DEFAULT_PQ_TXHASH_SELECTOR } from './covenants/index.js';
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthScript (witness v1) scriptPubKey + witness-stack builders.
|
|
3
|
+
*
|
|
4
|
+
* AuthScript outputs encode a 32-byte commitment in a witness v1 program:
|
|
5
|
+
* scriptPubKey = OP_1 0x20 <32-byte program>
|
|
6
|
+
*
|
|
7
|
+
* The program is `HASH160`/`SHA256` over a descriptor that depends on the
|
|
8
|
+
* `auth_type` byte carried as the first witness-stack element at spend time.
|
|
9
|
+
* This library only assembles the stack; computing the descriptor/commitment
|
|
10
|
+
* and producing the signature live in neurai-key / neurai-sign-transaction.
|
|
11
|
+
*
|
|
12
|
+
* Auth type values (authoritative: Neurai `src/script/interpreter.cpp`,
|
|
13
|
+
* cross-checked against `lib/neurai-key/src/shared/address.ts`):
|
|
14
|
+
*
|
|
15
|
+
* 0x00 NoAuth — no signature; spend gated only by witnessScript
|
|
16
|
+
* 0x01 PQ — ML-DSA-44 signature (post-quantum)
|
|
17
|
+
* 0x02 Legacy — secp256k1 ECDSA signature (classic)
|
|
18
|
+
* 0x03 RefScript — NIP-015 reference-script spend (future)
|
|
19
|
+
*
|
|
20
|
+
* Witness stack (exact order consumed by the interpreter):
|
|
21
|
+
* [ auth_type, sig, pubkey, arg0, ..., argN, witnessScript ]
|
|
22
|
+
*
|
|
23
|
+
* NoAuth and RefScript spends omit the `sig`/`pubkey` items.
|
|
24
|
+
*/
|
|
25
|
+
export declare const AUTHSCRIPT_NOAUTH = 0;
|
|
26
|
+
export declare const AUTHSCRIPT_PQ = 1;
|
|
27
|
+
export declare const AUTHSCRIPT_LEGACY = 2;
|
|
28
|
+
/** NIP-015: reference-script spend mode. Not yet activated in consensus. */
|
|
29
|
+
export declare const AUTHSCRIPT_REF = 3;
|
|
30
|
+
export type AuthType = typeof AUTHSCRIPT_NOAUTH | typeof AUTHSCRIPT_PQ | typeof AUTHSCRIPT_LEGACY | typeof AUTHSCRIPT_REF;
|
|
31
|
+
export declare function encodeAuthScriptScriptPubKey(program: Uint8Array): Uint8Array;
|
|
32
|
+
export interface AuthScriptWitnessLegacyInput {
|
|
33
|
+
/** DER-encoded secp256k1 signature WITH trailing sighash byte. */
|
|
34
|
+
signature: Uint8Array;
|
|
35
|
+
/** Compressed (33B) or uncompressed (65B) secp256k1 public key. */
|
|
36
|
+
pubKey: Uint8Array;
|
|
37
|
+
/** Additional items consumed by the witnessScript, in bottom-to-top order. */
|
|
38
|
+
args?: Uint8Array[];
|
|
39
|
+
/** The full witnessScript whose hash equals the program of the output. */
|
|
40
|
+
witnessScript: Uint8Array;
|
|
41
|
+
}
|
|
42
|
+
export interface AuthScriptWitnessPQInput {
|
|
43
|
+
/** ML-DSA-44 signature (~2421 B) WITH trailing sighash byte. */
|
|
44
|
+
signature: Uint8Array;
|
|
45
|
+
/** Versioned PQ pubkey (1-byte prefix + 1312-byte ML-DSA-44 key ≈ 1313 B). */
|
|
46
|
+
pubKey: Uint8Array;
|
|
47
|
+
args?: Uint8Array[];
|
|
48
|
+
witnessScript: Uint8Array;
|
|
49
|
+
}
|
|
50
|
+
export interface AuthScriptWitnessNoAuthInput {
|
|
51
|
+
args?: Uint8Array[];
|
|
52
|
+
witnessScript: Uint8Array;
|
|
53
|
+
}
|
|
54
|
+
export interface AuthScriptWitnessRefInput {
|
|
55
|
+
/** Index into tx.vrefin where the reference-script carrier lives. */
|
|
56
|
+
refIndex: number;
|
|
57
|
+
args?: Uint8Array[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Build the witness stack for a legacy ECDSA AuthScript spend.
|
|
61
|
+
*
|
|
62
|
+
* Returns an array of raw stack elements. Serialization of the witness
|
|
63
|
+
* (compact-size item count + per-item length-prefixes) is the caller's
|
|
64
|
+
* responsibility — neurai-create-transaction handles it at tx-assembly time.
|
|
65
|
+
*/
|
|
66
|
+
export declare function buildAuthScriptWitnessLegacy(input: AuthScriptWitnessLegacyInput): Uint8Array[];
|
|
67
|
+
/**
|
|
68
|
+
* Build the witness stack for a PQ (ML-DSA-44) AuthScript spend.
|
|
69
|
+
* NIP-018 must be active: both signature and pubKey are pushed as single
|
|
70
|
+
* stack elements and may each be up to 3072 B.
|
|
71
|
+
*/
|
|
72
|
+
export declare function buildAuthScriptWitnessPQ(input: AuthScriptWitnessPQInput): Uint8Array[];
|
|
73
|
+
/**
|
|
74
|
+
* Build the witness stack for a NoAuth AuthScript spend. The spend is gated
|
|
75
|
+
* by the witnessScript alone (covenants, hash-locks, time-locks, ...); no
|
|
76
|
+
* signature or public key is carried.
|
|
77
|
+
*/
|
|
78
|
+
export declare function buildAuthScriptWitnessNoAuth(input: AuthScriptWitnessNoAuthInput): Uint8Array[];
|
|
79
|
+
/**
|
|
80
|
+
* Build the witness stack for a NIP-015 reference-script spend. The last
|
|
81
|
+
* item is the 4-byte little-endian index into `tx.vrefin` that locates the
|
|
82
|
+
* reference-script carrier output; the interpreter resolves the `refScript`
|
|
83
|
+
* from that carrier at validation time.
|
|
84
|
+
*
|
|
85
|
+
* NOTE: NIP-015 is not yet activated in consensus. This builder emits the
|
|
86
|
+
* spec-conformant witness for forward compatibility and testnet experiments.
|
|
87
|
+
*/
|
|
88
|
+
export declare function buildAuthScriptWitnessRef(input: AuthScriptWitnessRefInput): Uint8Array[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { encodeP2PKHScriptPubKey } from './p2pkh.js';
|
|
2
|
+
export { encodeP2WPKHScriptPubKey } from './p2wpkh.js';
|
|
3
|
+
export { encodeP2WSHScriptPubKey } from './p2wsh.js';
|
|
4
|
+
export { encodeAuthScriptScriptPubKey, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessPQ, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessRef, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_LEGACY, AUTHSCRIPT_REF } from './authscript.js';
|
|
5
|
+
export type { AuthType, AuthScriptWitnessLegacyInput, AuthScriptWitnessPQInput, AuthScriptWitnessNoAuthInput, AuthScriptWitnessRefInput } from './authscript.js';
|
|
6
|
+
export { encodeNullDataScript, NULLDATA_STANDARD_MAX_SIZE } from './nulldata.js';
|
|
7
|
+
export type { EncodeNullDataOptions } from './nulldata.js';
|
|
8
|
+
export { encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeP2SHScriptPubKey, MULTISIG_MAX_PUBKEYS } from './multisig.js';
|
|
9
|
+
export type { MultisigParams } from './multisig.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classic bare m-of-n multisig via OP_CHECKMULTISIG, plus the P2SH wrapper
|
|
3
|
+
* that is normally used in practice.
|
|
4
|
+
*
|
|
5
|
+
* redeemScript = <m> <pubkey_1> ... <pubkey_n> <n> OP_CHECKMULTISIG
|
|
6
|
+
* P2SH spk = OP_HASH160 0x14 <HASH160(redeemScript)> OP_EQUAL
|
|
7
|
+
*
|
|
8
|
+
* OP_CHECKMULTISIG caps n at 20 pubkeys (see `MAX_PUBKEYS_PER_MULTISIG` in
|
|
9
|
+
* the Neurai interpreter). Bare multisig outputs are still consensus-valid
|
|
10
|
+
* but non-standard for relay; the P2SH wrapping is what mempool accepts.
|
|
11
|
+
*/
|
|
12
|
+
export declare const MULTISIG_MAX_PUBKEYS = 20;
|
|
13
|
+
export interface MultisigParams {
|
|
14
|
+
/** Number of required signatures (1..n). */
|
|
15
|
+
m: number;
|
|
16
|
+
/** Compressed (33B) or uncompressed (65B) secp256k1 pubkeys, ordered. */
|
|
17
|
+
pubKeys: Uint8Array[];
|
|
18
|
+
}
|
|
19
|
+
export declare function encodeMultisigRedeemScript({ m, pubKeys }: MultisigParams): Uint8Array;
|
|
20
|
+
export declare function encodeMultisigRedeemScriptHex(params: MultisigParams): string;
|
|
21
|
+
/**
|
|
22
|
+
* P2SH scriptPubKey wrapping any redeem script. Pass the 20-byte HASH160 of
|
|
23
|
+
* the redeem script (RIPEMD160(SHA256(redeemScript))) — compute it
|
|
24
|
+
* externally via the caller's crypto stack.
|
|
25
|
+
*/
|
|
26
|
+
export declare function encodeP2SHScriptPubKey(redeemScriptHash160: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provably-unspendable OP_RETURN (null-data) output scripts.
|
|
3
|
+
* Layout: OP_RETURN <push> [<push> ...]
|
|
4
|
+
*
|
|
5
|
+
* Neurai inherits the Bitcoin Core standard policy cap of 80 bytes of
|
|
6
|
+
* payload for a relayed null-data output. Outputs over that cap are still
|
|
7
|
+
* consensus-valid but will not be relayed by default mempool policy; use
|
|
8
|
+
* the `allowNonStandard` option to opt in explicitly.
|
|
9
|
+
*/
|
|
10
|
+
export declare const NULLDATA_STANDARD_MAX_SIZE = 80;
|
|
11
|
+
export interface EncodeNullDataOptions {
|
|
12
|
+
/** Bypass the 80-byte standard-policy cap. Default: false. */
|
|
13
|
+
allowNonStandard?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare function encodeNullDataScript(payload: Uint8Array | Uint8Array[], options?: EncodeNullDataOptions): Uint8Array;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native segwit v0 Pay-to-Witness-Public-Key-Hash (P2WPKH) scriptPubKey.
|
|
3
|
+
* Layout: OP_0 0x14 <20-byte HASH160(pubKey)>
|
|
4
|
+
*
|
|
5
|
+
* Callers compute HASH160 externally (e.g. via neurai-key / their crypto
|
|
6
|
+
* stack). This helper only encodes the scriptPubKey once the hash is known.
|
|
7
|
+
*/
|
|
8
|
+
export declare function encodeP2WPKHScriptPubKey(pubKeyHash: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native segwit v0 Pay-to-Witness-Script-Hash (P2WSH) scriptPubKey.
|
|
3
|
+
* Layout: OP_0 0x20 <32-byte SHA256(witnessScript)>
|
|
4
|
+
*
|
|
5
|
+
* This helper takes the SHA256 digest of the witness script directly. The
|
|
6
|
+
* SHA256 is computed externally by the caller's crypto stack (kept out of
|
|
7
|
+
* this library to avoid a hash dependency).
|
|
8
|
+
*/
|
|
9
|
+
export declare function encodeP2WSHScriptPubKey(witnessScriptSha256: Uint8Array): Uint8Array;
|