@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.
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Neurai network discriminator.
3
+ *
4
+ * This type intentionally appears only on the **parse side** of the API —
5
+ * as an optional argument to the parsers and as a field on the parsed
6
+ * result types. It is **not** part of any builder's input.
7
+ *
8
+ * Rationale: the covenant scriptPubKey bytes do not encode network. A
9
+ * legacy covenant is a fixed sequence of opcodes and a 20-byte PKH; a PQ
10
+ * covenant is opcodes plus a 32-byte commitment and the raw bytes of the
11
+ * payment scriptPubKey. None of those change between `xna` and
12
+ * `xna-test`. What differs across networks is:
13
+ *
14
+ * - The *address encoding* used by the caller (base58 prefix for
15
+ * P2PKH, bech32m HRP for AuthScript), validated by `decodeAddress`
16
+ * when the builder receives an address string.
17
+ * - The *consensus flags* gating the DePIN-Test opcodes; those live on
18
+ * the node, not in the script bytes.
19
+ *
20
+ * So builders take an address string (whose prefix already carries the
21
+ * network) and parsers take an explicit `network` label (so the caller
22
+ * can later format `sellerPubKeyHash` or interpret `paymentScriptPubKey`
23
+ * in the correct context). Keep this split intact when adding new
24
+ * covenants: do not reintroduce `network` into builder params.
25
+ */
26
+ export type Network = 'xna' | 'xna-test';
27
+ export interface PartialFillOrderParams {
28
+ /**
29
+ * Seller destination. Must be a legacy P2PKH address (base58check,
30
+ * "t..." on testnet, "N..." on mainnet). The legacy covenant uses
31
+ * `OP_HASH160 + OP_CHECKSIG` in its cancel branch, so AuthScript
32
+ * bech32m addresses are rejected — use `buildPartialFillScriptPQ` for
33
+ * post-quantum / AuthScript destinations. The decoded 20-byte PKH is
34
+ * hardcoded into the covenant (cancel branch + payment scriptPubKey).
35
+ */
36
+ sellerAddress: string;
37
+ /** Name of the asset being sold (e.g. "CAT"). */
38
+ tokenId: string;
39
+ /**
40
+ * Price per single indivisible unit of the asset, expressed in satoshis of
41
+ * XNA. The covenant multiplies this by the filled amount and enforces the
42
+ * XNA output to Alice is `>=` that product.
43
+ *
44
+ * Example: selling at 1 XNA per whole token with `units = 0` (non-divisible)
45
+ * means `unitPriceSats = 100_000_000n`.
46
+ */
47
+ unitPriceSats: bigint;
48
+ }
49
+ export interface ParsedPartialFillOrder {
50
+ /** Network the parser was invoked with (used for downstream address formatting). */
51
+ network: Network;
52
+ /**
53
+ * Seller's 20-byte pubkey hash, extracted from the covenant's cancel
54
+ * branch. The caller can base58check-encode this with the network's
55
+ * legacy prefix to recover the seller address when needed.
56
+ */
57
+ sellerPubKeyHash: Uint8Array;
58
+ /** Asset name parsed from the covenant. */
59
+ tokenId: string;
60
+ /** Unit price parsed from the covenant, in XNA satoshis. */
61
+ unitPriceSats: bigint;
62
+ /** Script hex the parser was fed. */
63
+ scriptHex: string;
64
+ }
65
+ export interface OrderUtxo {
66
+ txid: string;
67
+ vout: number;
68
+ /** scriptPubKey of the covenant UTXO (the output produced by the seller). */
69
+ scriptPubKeyHex: string;
70
+ /** Cantidad del asset dentro del UTXO covenant. */
71
+ assetAmountRaw: bigint;
72
+ /** Valor XNA del UTXO (normalmente el dust mínimo). */
73
+ valueSats: bigint;
74
+ }
75
+ export interface TxInputRef {
76
+ txid: string;
77
+ vout: number;
78
+ sequence?: number;
79
+ scriptSigHex?: string;
80
+ }
81
+ /**
82
+ * Variant of the partial-fill order whose cancel branch validates a
83
+ * post-quantum (ML-DSA-44) signature via `OP_CHECKSIGFROMSTACK`. Requires
84
+ * both `SCRIPT_VERIFY_CHECKSIGFROMSTACK` and NIP-18's
85
+ * `MAX_PQ_SCRIPT_ELEMENT_SIZE` to be active (testnet as of DePIN-Test with
86
+ * NIP-18 deployed; mainnet after fork).
87
+ */
88
+ export interface PartialFillOrderPQParams {
89
+ /**
90
+ * Destination for the XNA payment to the seller. Can be legacy P2PKH
91
+ * (`N.../t...`) OR AuthScript bech32m v1 (`nq1.../tnq1...`). The covenant
92
+ * hardcodes the full scriptPubKey bytes either way.
93
+ */
94
+ paymentAddress: string;
95
+ /**
96
+ * 32-byte commitment to the seller's PQ public key. The script checks
97
+ * `SHA256(pubKey_on_stack) == pubKeyCommitment`. Caller must compute this
98
+ * as SHA256 over the same pubKey bytes that will be pushed on spend
99
+ * (typically 1313 bytes = 1-byte version prefix + 1312-byte ML-DSA-44 key).
100
+ */
101
+ pubKeyCommitment: Uint8Array;
102
+ /** Asset name being sold. */
103
+ tokenId: string;
104
+ /** Price in XNA satoshis per indivisible unit of the asset. */
105
+ unitPriceSats: bigint;
106
+ /**
107
+ * OP_TXHASH selector byte bound into the script. Default `0xff` (all
108
+ * eight fields). The seller must sign `SHA256(OP_TXHASH(selector))` with
109
+ * their PQ key at cancel time. See
110
+ * `doc/new-opcodes-depin-branch.md` §2.1 for the bit → field mapping.
111
+ */
112
+ txHashSelector?: number;
113
+ }
114
+ export interface ParsedPartialFillOrderPQ extends Omit<PartialFillOrderPQParams, 'paymentAddress'> {
115
+ /** Network the parser was invoked with (used for downstream address formatting). */
116
+ network: Network;
117
+ /** scriptPubKey bytes embedded in the covenant for the payment output. */
118
+ paymentScriptPubKey: Uint8Array;
119
+ scriptHex: string;
120
+ /** Echoes the resolved selector so the caller can assert against a default. */
121
+ txHashSelector: number;
122
+ }
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@neuraiproject/neurai-scripts",
3
+ "version": "0.1.0",
4
+ "description": "Script-assembly library for Neurai: opcode constants, ScriptBuilder, standard scripts (P2PKH, P2WPKH, P2WSH, AuthScript, multisig, OP_RETURN) and covenants (partial-fill sell orders, legacy + PQ) targeting the DePIN-Test opcode set",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "browser": "./dist/browser.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.cjs"
15
+ },
16
+ "./browser": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/browser.js"
19
+ },
20
+ "./global": "./dist/NeuraiScripts.global.js"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "scripts": {
30
+ "build": "tsc -p tsconfig.json && rollup -c rollup.config.mjs",
31
+ "build:ts": "tsc -p tsconfig.json",
32
+ "test": "npm run build && vitest run",
33
+ "test:run": "vitest run",
34
+ "test:watch": "vitest"
35
+ },
36
+ "dependencies": {
37
+ "@neuraiproject/neurai-create-transaction": "^0.2.0"
38
+ },
39
+ "keywords": [
40
+ "neurai",
41
+ "scripts",
42
+ "script",
43
+ "scriptbuilder",
44
+ "opcodes",
45
+ "depin-test",
46
+ "p2pkh",
47
+ "p2wpkh",
48
+ "p2wsh",
49
+ "authscript",
50
+ "multisig",
51
+ "nulldata",
52
+ "op-return",
53
+ "covenant",
54
+ "partial-fill",
55
+ "post-quantum",
56
+ "ml-dsa-44",
57
+ "dex"
58
+ ],
59
+ "license": "MIT",
60
+ "devDependencies": {
61
+ "@rollup/plugin-commonjs": "^28.0.8",
62
+ "@rollup/plugin-node-resolve": "^16.0.3",
63
+ "bech32": "^2.0.0",
64
+ "bs58check": "^4.0.0",
65
+ "rollup": "^4.52.4",
66
+ "typescript": "^5.9.3",
67
+ "vitest": "^3.2.4"
68
+ }
69
+ }