@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.
- package/README.md +91 -24
- package/dist/NeuraiScripts.global.js +343 -476
- package/dist/NeuraiScripts.global.js.map +1 -1
- package/dist/browser.js +343 -476
- package/dist/browser.js.map +1 -1
- package/dist/core/opcodes.d.ts +12 -1
- package/dist/core/script-parser.d.ts +3 -10
- package/dist/covenants/partial-fill/fill-branches.d.ts +74 -0
- package/dist/covenants/partial-fill/parse-pq.d.ts +2 -3
- package/dist/covenants/partial-fill/parse.d.ts +8 -10
- package/dist/covenants/partial-fill/script-pq.d.ts +5 -5
- package/dist/covenants/partial-fill/script.d.ts +9 -10
- package/dist/index.cjs +343 -476
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +94 -45
- package/dist/index.js +343 -476
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +35 -15
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -243,20 +243,27 @@ const CHAINCONTEXT_CHAIN_ID = 0x03;
|
|
|
243
243
|
// Native Merkle inclusion verifier. Consumes (leaf, scheme_id, proof,
|
|
244
244
|
// root) and pushes a boolean. Flag off → bad-opcode.
|
|
245
245
|
const OP_CHECKMERKLEINCLUSION = 0xc1;
|
|
246
|
+
// NIP-041: (outputIndex -- witnessVersion || 32-byte program).
|
|
247
|
+
const OP_OUTPUTAUTHDEST = 0xc2;
|
|
248
|
+
// NIP-018: verifies an AuthScript proof and returns a boolean.
|
|
249
|
+
const OP_ZKVERIFY = 0xc3;
|
|
250
|
+
// NIP-043: spent-input field introspection, distinct from reference inputs.
|
|
251
|
+
const OP_INPUTFIELD = 0xc4;
|
|
246
252
|
// ---------- Byte manipulation (DePIN-Test) ----------
|
|
247
253
|
const OP_CAT = 0x7e;
|
|
248
254
|
const OP_SPLIT = 0xb7;
|
|
249
255
|
const OP_REVERSEBYTES = 0xbc;
|
|
250
256
|
// ---------- Selectors for OP_TXFIELD / OP_REFINPUTFIELD ----------
|
|
251
|
-
//
|
|
252
|
-
//
|
|
257
|
+
// OP_TXFIELD, OP_INPUTFIELD and OP_REFINPUTFIELD share selectors 0x01..0x03.
|
|
258
|
+
// NIP-041 adds 0x04 for a strict 33-byte AuthScript destination.
|
|
253
259
|
const TXFIELD_VALUE = 0x01;
|
|
254
260
|
const TXFIELD_AUTHSCRIPT_COMMITMENT = 0x02;
|
|
255
261
|
const TXFIELD_SCRIPTPUBKEY = 0x03;
|
|
262
|
+
const TXFIELD_AUTHDEST = 0x04;
|
|
256
263
|
// ---------- Bitmask selectors for OP_TXHASH ----------
|
|
257
|
-
//
|
|
258
|
-
//
|
|
259
|
-
//
|
|
264
|
+
// NIP-042 requires a two-byte little-endian selector, even for low masks.
|
|
265
|
+
// Bits 0..8 select fields for the tagged NeuraiTxHash digest. Valid masks
|
|
266
|
+
// are 0x001..0x1ff; zero and all higher bits are rejected by consensus.
|
|
260
267
|
const TXHASH_VERSION = 0x01;
|
|
261
268
|
const TXHASH_LOCKTIME = 0x02;
|
|
262
269
|
const TXHASH_INPUT_PREVOUTS = 0x04;
|
|
@@ -265,12 +272,11 @@ const TXHASH_OUTPUTS = 0x10;
|
|
|
265
272
|
const TXHASH_CURRENT_PREVOUT = 0x20;
|
|
266
273
|
const TXHASH_CURRENT_SEQUENCE = 0x40;
|
|
267
274
|
const TXHASH_CURRENT_INDEX = 0x80;
|
|
268
|
-
const
|
|
275
|
+
const TXHASH_REFINPUTS = 0x100;
|
|
276
|
+
const TXHASH_ALL = 0x1ff;
|
|
269
277
|
// ---------- Selectors for OP_OUTPUTASSETFIELD / OP_INPUTASSETFIELD / OP_REFINPUTASSETFIELD ----------
|
|
270
|
-
// All three opcodes share
|
|
271
|
-
//
|
|
272
|
-
// payload; 0x07 is the asset operation type. (This matches the asset-op
|
|
273
|
-
// encoding in `src/assets/assets.cpp` and the NIP spec §3.1.)
|
|
278
|
+
// All three opcodes share this selector table. 0x08 is gated by NIP-043
|
|
279
|
+
// and extracts the optional transfer message from a strict asset wrapper.
|
|
274
280
|
const ASSETFIELD_NAME = 0x01;
|
|
275
281
|
const ASSETFIELD_AMOUNT = 0x02;
|
|
276
282
|
const ASSETFIELD_UNITS = 0x03;
|
|
@@ -278,12 +284,20 @@ const ASSETFIELD_REISSUABLE = 0x04;
|
|
|
278
284
|
const ASSETFIELD_HAS_IPFS = 0x05;
|
|
279
285
|
const ASSETFIELD_IPFS_HASH = 0x06;
|
|
280
286
|
const ASSETFIELD_TYPE = 0x07;
|
|
287
|
+
const ASSETFIELD_MESSAGE = 0x08;
|
|
288
|
+
// NIP-031/043 Merkle inclusion scheme identifiers.
|
|
289
|
+
const MERKLE_SCHEME_BITCOIN_NEURAI = 0x01;
|
|
290
|
+
const MERKLE_SCHEME_SHA256_PLAIN = 0x02;
|
|
291
|
+
const MERKLE_SCHEME_KECCAK256_PLAIN = 0x03;
|
|
292
|
+
const MERKLE_SCHEME_BLAKE2B_PLAIN = 0x04;
|
|
293
|
+
const MERKLE_SCHEME_POSEIDON_BN254 = 0x05;
|
|
281
294
|
|
|
282
295
|
var opcodes = /*#__PURE__*/Object.freeze({
|
|
283
296
|
__proto__: null,
|
|
284
297
|
ASSETFIELD_AMOUNT: ASSETFIELD_AMOUNT,
|
|
285
298
|
ASSETFIELD_HAS_IPFS: ASSETFIELD_HAS_IPFS,
|
|
286
299
|
ASSETFIELD_IPFS_HASH: ASSETFIELD_IPFS_HASH,
|
|
300
|
+
ASSETFIELD_MESSAGE: ASSETFIELD_MESSAGE,
|
|
287
301
|
ASSETFIELD_NAME: ASSETFIELD_NAME,
|
|
288
302
|
ASSETFIELD_REISSUABLE: ASSETFIELD_REISSUABLE,
|
|
289
303
|
ASSETFIELD_TYPE: ASSETFIELD_TYPE,
|
|
@@ -291,6 +305,11 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
291
305
|
CHAINCONTEXT_CHAIN_ID: CHAINCONTEXT_CHAIN_ID,
|
|
292
306
|
CHAINCONTEXT_HEIGHT: CHAINCONTEXT_HEIGHT,
|
|
293
307
|
CHAINCONTEXT_MTP: CHAINCONTEXT_MTP,
|
|
308
|
+
MERKLE_SCHEME_BITCOIN_NEURAI: MERKLE_SCHEME_BITCOIN_NEURAI,
|
|
309
|
+
MERKLE_SCHEME_BLAKE2B_PLAIN: MERKLE_SCHEME_BLAKE2B_PLAIN,
|
|
310
|
+
MERKLE_SCHEME_KECCAK256_PLAIN: MERKLE_SCHEME_KECCAK256_PLAIN,
|
|
311
|
+
MERKLE_SCHEME_POSEIDON_BN254: MERKLE_SCHEME_POSEIDON_BN254,
|
|
312
|
+
MERKLE_SCHEME_SHA256_PLAIN: MERKLE_SCHEME_SHA256_PLAIN,
|
|
294
313
|
OP_0: OP_0,
|
|
295
314
|
OP_0NOTEQUAL: OP_0NOTEQUAL,
|
|
296
315
|
OP_1: OP_1,
|
|
@@ -356,6 +375,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
356
375
|
OP_IFDUP: OP_IFDUP,
|
|
357
376
|
OP_INPUTASSETFIELD: OP_INPUTASSETFIELD,
|
|
358
377
|
OP_INPUTCOUNT: OP_INPUTCOUNT,
|
|
378
|
+
OP_INPUTFIELD: OP_INPUTFIELD,
|
|
359
379
|
OP_INPUTVALUE: OP_INPUTVALUE,
|
|
360
380
|
OP_KECCAK256: OP_KECCAK256,
|
|
361
381
|
OP_LESSTHAN: OP_LESSTHAN,
|
|
@@ -377,6 +397,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
377
397
|
OP_NUMNOTEQUAL: OP_NUMNOTEQUAL,
|
|
378
398
|
OP_OUTPUTASSETFIELD: OP_OUTPUTASSETFIELD,
|
|
379
399
|
OP_OUTPUTAUTHCOMMITMENT: OP_OUTPUTAUTHCOMMITMENT,
|
|
400
|
+
OP_OUTPUTAUTHDEST: OP_OUTPUTAUTHDEST,
|
|
380
401
|
OP_OUTPUTCOUNT: OP_OUTPUTCOUNT,
|
|
381
402
|
OP_OUTPUTSCRIPT: OP_OUTPUTSCRIPT,
|
|
382
403
|
OP_OUTPUTVALUE: OP_OUTPUTVALUE,
|
|
@@ -412,6 +433,8 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
412
433
|
OP_VERIFY: OP_VERIFY,
|
|
413
434
|
OP_WITHIN: OP_WITHIN,
|
|
414
435
|
OP_XNA_ASSET: OP_XNA_ASSET,
|
|
436
|
+
OP_ZKVERIFY: OP_ZKVERIFY,
|
|
437
|
+
TXFIELD_AUTHDEST: TXFIELD_AUTHDEST,
|
|
415
438
|
TXFIELD_AUTHSCRIPT_COMMITMENT: TXFIELD_AUTHSCRIPT_COMMITMENT,
|
|
416
439
|
TXFIELD_SCRIPTPUBKEY: TXFIELD_SCRIPTPUBKEY,
|
|
417
440
|
TXFIELD_VALUE: TXFIELD_VALUE,
|
|
@@ -423,6 +446,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
|
|
|
423
446
|
TXHASH_INPUT_SEQUENCES: TXHASH_INPUT_SEQUENCES,
|
|
424
447
|
TXHASH_LOCKTIME: TXHASH_LOCKTIME,
|
|
425
448
|
TXHASH_OUTPUTS: TXHASH_OUTPUTS,
|
|
449
|
+
TXHASH_REFINPUTS: TXHASH_REFINPUTS,
|
|
426
450
|
TXHASH_VERSION: TXHASH_VERSION
|
|
427
451
|
});
|
|
428
452
|
|
|
@@ -2098,16 +2122,9 @@ function readPushPositiveInt(c, label) {
|
|
|
2098
2122
|
return decodeScriptNum(data, label);
|
|
2099
2123
|
}
|
|
2100
2124
|
/**
|
|
2101
|
-
* Read a
|
|
2102
|
-
*
|
|
2103
|
-
*
|
|
2104
|
-
* - `0x01 <byte>` raw 1-byte push → any value 1..255.
|
|
2105
|
-
*
|
|
2106
|
-
* Values 0x80..0xff MUST use the raw-push form; the CScriptNum encoding
|
|
2107
|
-
* would need a 0x00 padding byte and become 2 bytes on-stack, which
|
|
2108
|
-
* consensus `OP_TXHASH` rejects. The builder in `script-pq.ts` emits the
|
|
2109
|
-
* raw-push form unconditionally; the parser stays lenient so covenants
|
|
2110
|
-
* built by older tools (using OP_N for small values) still round-trip.
|
|
2125
|
+
* Read a one-byte unsigned selector. This accepts OP_1..OP_16 shorthand
|
|
2126
|
+
* and a raw one-byte push for scripts that permit either form. NIP-042
|
|
2127
|
+
* TXHASH masks use a separate strict two-byte parser.
|
|
2111
2128
|
*/
|
|
2112
2129
|
function readPushUint8(c, label) {
|
|
2113
2130
|
if (c.pos >= c.bytes.length) {
|
|
@@ -2194,7 +2211,224 @@ function assertSameExpiration(a, b, label) {
|
|
|
2194
2211
|
}
|
|
2195
2212
|
|
|
2196
2213
|
/**
|
|
2197
|
-
* Partial-Fill Sell Order
|
|
2214
|
+
* Buyer fill branches shared by both Partial-Fill Sell Order variants
|
|
2215
|
+
* (ECDSA cancel in `./script.ts`, ML-DSA-44 cancel in `./script-pq.ts`).
|
|
2216
|
+
* The variants only differ in their cancel branch; everything after the
|
|
2217
|
+
* outer `OP_ELSE` is emitted and parsed here.
|
|
2218
|
+
*
|
|
2219
|
+
* Input-0 binding. The fill branches read the order's asset amount with
|
|
2220
|
+
* `<0> OP_INPUTASSETFIELD` and constrain outputs 0..2 by fixed index. Input
|
|
2221
|
+
* indices are absolute and Script cannot ask for the index of the input
|
|
2222
|
+
* being evaluated, so without a binding a buyer could:
|
|
2223
|
+
* - put a foreign asset UTXO at input 0 and the order at input 1: the
|
|
2224
|
+
* order then prices the fill by the foreign amount and the rest of the
|
|
2225
|
+
* order leaves through an unconstrained output;
|
|
2226
|
+
* - spend several identical orders together, every one of them satisfied
|
|
2227
|
+
* by the same payment output.
|
|
2228
|
+
* The fill block therefore opens with
|
|
2229
|
+
*
|
|
2230
|
+
* <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
|
|
2231
|
+
*
|
|
2232
|
+
* NIP-042 mask 0x0080 commits only to the index of the input being
|
|
2233
|
+
* evaluated, so the digest equals the constant exactly when this covenant
|
|
2234
|
+
* is spent as input 0. At most one order per transaction can take a fill
|
|
2235
|
+
* branch; the seller's cancel branch is not bound.
|
|
2236
|
+
*
|
|
2237
|
+
* Layout (after the variant's `OP_IF <cancel> OP_ELSE`):
|
|
2238
|
+
*
|
|
2239
|
+
* <0x80 0x00> OP_TXHASH <INPUT_ZERO_TXHASH> OP_EQUALVERIFY
|
|
2240
|
+
* OP_IF ← full fill, unlock <1> <0>
|
|
2241
|
+
* [expiration gate]
|
|
2242
|
+
* <0> <2> OP_INPUTASSETFIELD (N = order amount)
|
|
2243
|
+
* DUP <price> MUL <0> OUTPUTVALUE SWAP GE VERIFY (payment value)
|
|
2244
|
+
* <0> OUTPUTSCRIPT <paymentSpk> EQUALVERIFY (payment destination)
|
|
2245
|
+
* DUP <1> <2> OUTPUTASSETFIELD EQUALVERIFY (buyer amount == N)
|
|
2246
|
+
* <1> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (buyer asset name)
|
|
2247
|
+
* DROP <1>
|
|
2248
|
+
* OP_ELSE ← partial fill, unlock <N> <0> <0>
|
|
2249
|
+
* [expiration gate]
|
|
2250
|
+
* payment value, payment destination, buyer amount, buyer name (as above)
|
|
2251
|
+
* <2> OUTPUTAUTHCOMMITMENT <2> TXFIELD EQUALVERIFY (continuation, NIP-023)
|
|
2252
|
+
* <2> <1> OUTPUTASSETFIELD <tokenId> EQUALVERIFY (continuation name)
|
|
2253
|
+
* <2> <2> OUTPUTASSETFIELD OVER <0> <2> INPUTASSETFIELD SWAP SUB EQUALVERIFY
|
|
2254
|
+
* DROP <1>
|
|
2255
|
+
* OP_ENDIF
|
|
2256
|
+
*/
|
|
2257
|
+
/** NIP-042 mask (two bytes, little-endian) selecting only the current input index. */
|
|
2258
|
+
const INPUT_INDEX_TXHASH_SELECTOR = Uint8Array.of(TXHASH_CURRENT_INDEX, 0x00);
|
|
2259
|
+
/**
|
|
2260
|
+
* `OP_TXHASH(0x0080)` evaluated at input 0:
|
|
2261
|
+
* `SHA256(tag || tag || 80 00 || 00 00 00 00)` with `tag = SHA256("NeuraiTxHash")`.
|
|
2262
|
+
*/
|
|
2263
|
+
const INPUT_ZERO_TXHASH = hexToBytes('419632cabaf8bb70b96a8c92c4601a9c2ba11e96841daebc9d98f4835eeb0e36');
|
|
2264
|
+
function appendPaymentAndBuyerChecks(b, p) {
|
|
2265
|
+
// output[0].value >= N * unitPriceSats [ N ]
|
|
2266
|
+
b.op(OP_DUP)
|
|
2267
|
+
.pushInt(p.unitPriceSats)
|
|
2268
|
+
.op(OP_MUL)
|
|
2269
|
+
.pushInt(0)
|
|
2270
|
+
.op(OP_OUTPUTVALUE)
|
|
2271
|
+
.op(OP_SWAP)
|
|
2272
|
+
.op(OP_GREATERTHANOREQUAL)
|
|
2273
|
+
.op(OP_VERIFY);
|
|
2274
|
+
// output[0].scriptPubKey == paymentScriptPubKey [ N ]
|
|
2275
|
+
b.pushInt(0)
|
|
2276
|
+
.op(OP_OUTPUTSCRIPT)
|
|
2277
|
+
.pushBytes(p.paymentScriptPubKey)
|
|
2278
|
+
.op(OP_EQUALVERIFY);
|
|
2279
|
+
// output[1].asset.amount == N [ N ]
|
|
2280
|
+
b.op(OP_DUP)
|
|
2281
|
+
.pushInt(1)
|
|
2282
|
+
.pushInt(ASSETFIELD_AMOUNT)
|
|
2283
|
+
.op(OP_OUTPUTASSETFIELD)
|
|
2284
|
+
.op(OP_EQUALVERIFY);
|
|
2285
|
+
// output[1].asset.name == tokenId [ N ]
|
|
2286
|
+
b.pushInt(1)
|
|
2287
|
+
.pushInt(ASSETFIELD_NAME)
|
|
2288
|
+
.op(OP_OUTPUTASSETFIELD)
|
|
2289
|
+
.pushBytes(p.tokenIdBytes)
|
|
2290
|
+
.op(OP_EQUALVERIFY);
|
|
2291
|
+
}
|
|
2292
|
+
/** Emit the input-0 binding and both fill branches, ending with the inner `OP_ENDIF`. */
|
|
2293
|
+
function appendFillBranches(b, p) {
|
|
2294
|
+
b.pushBytes(INPUT_INDEX_TXHASH_SELECTOR)
|
|
2295
|
+
.op(OP_TXHASH)
|
|
2296
|
+
.pushBytes(INPUT_ZERO_TXHASH)
|
|
2297
|
+
.op(OP_EQUALVERIFY);
|
|
2298
|
+
// ──────── Full fill: N is the whole order, no continuation ────────
|
|
2299
|
+
b.op(OP_IF);
|
|
2300
|
+
appendExpirationGate(b, p.expiration);
|
|
2301
|
+
b.pushInt(0)
|
|
2302
|
+
.pushInt(ASSETFIELD_AMOUNT)
|
|
2303
|
+
.op(OP_INPUTASSETFIELD); // [ N ]
|
|
2304
|
+
appendPaymentAndBuyerChecks(b, p);
|
|
2305
|
+
b.op(OP_DROP).pushInt(1);
|
|
2306
|
+
// ──────── Partial fill: buyer pushes N, remainder continues at output 2 ────────
|
|
2307
|
+
b.op(OP_ELSE);
|
|
2308
|
+
appendExpirationGate(b, p.expiration);
|
|
2309
|
+
appendPaymentAndBuyerChecks(b, p);
|
|
2310
|
+
// Continuation keeps the spent order's AuthScript commitment (NIP-023).
|
|
2311
|
+
b.pushInt(2)
|
|
2312
|
+
.op(OP_OUTPUTAUTHCOMMITMENT)
|
|
2313
|
+
.pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
|
|
2314
|
+
.op(OP_TXFIELD)
|
|
2315
|
+
.op(OP_EQUALVERIFY);
|
|
2316
|
+
b.pushInt(2)
|
|
2317
|
+
.pushInt(ASSETFIELD_NAME)
|
|
2318
|
+
.op(OP_OUTPUTASSETFIELD)
|
|
2319
|
+
.pushBytes(p.tokenIdBytes)
|
|
2320
|
+
.op(OP_EQUALVERIFY);
|
|
2321
|
+
// output[2].asset.amount == orderAmount - N
|
|
2322
|
+
b.pushInt(2)
|
|
2323
|
+
.pushInt(ASSETFIELD_AMOUNT)
|
|
2324
|
+
.op(OP_OUTPUTASSETFIELD) // [ N, rem ]
|
|
2325
|
+
.op(OP_OVER) // [ N, rem, N ]
|
|
2326
|
+
.pushInt(0)
|
|
2327
|
+
.pushInt(ASSETFIELD_AMOUNT)
|
|
2328
|
+
.op(OP_INPUTASSETFIELD) // [ N, rem, N, in ]
|
|
2329
|
+
.op(OP_SWAP) // [ N, rem, in, N ]
|
|
2330
|
+
.op(OP_SUB) // [ N, rem, in - N ]
|
|
2331
|
+
.op(OP_EQUALVERIFY);
|
|
2332
|
+
b.op(OP_DROP).pushInt(1);
|
|
2333
|
+
b.op(OP_ENDIF);
|
|
2334
|
+
}
|
|
2335
|
+
function readPaymentAndBuyerChecks(c, label) {
|
|
2336
|
+
expectByte(c, OP_DUP, `OP_DUP (price, ${label})`);
|
|
2337
|
+
const unitPriceSats = readPushPositiveInt(c, `unitPriceSats (${label})`);
|
|
2338
|
+
expectByte(c, OP_MUL, `OP_MUL (${label})`);
|
|
2339
|
+
expectByte(c, OP_0, `OP_0 (payment idx, ${label})`);
|
|
2340
|
+
expectByte(c, OP_OUTPUTVALUE, `OP_OUTPUTVALUE (${label})`);
|
|
2341
|
+
expectByte(c, OP_SWAP, `OP_SWAP (${label})`);
|
|
2342
|
+
expectByte(c, OP_GREATERTHANOREQUAL, `OP_GREATERTHANOREQUAL (${label})`);
|
|
2343
|
+
expectByte(c, OP_VERIFY, `OP_VERIFY (payment, ${label})`);
|
|
2344
|
+
expectByte(c, OP_0, `OP_0 (spk idx, ${label})`);
|
|
2345
|
+
expectByte(c, OP_OUTPUTSCRIPT, `OP_OUTPUTSCRIPT (${label})`);
|
|
2346
|
+
const paymentScriptPubKey = readPush(c, `paymentScriptPubKey (${label})`);
|
|
2347
|
+
expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (payment spk, ${label})`);
|
|
2348
|
+
expectByte(c, OP_DUP, `OP_DUP (buyer amount, ${label})`);
|
|
2349
|
+
expectByte(c, OP_1, `OP_1 (buyer idx, ${label})`);
|
|
2350
|
+
expectByte(c, OP_2, `OP_2 (AMOUNT sel, ${label})`);
|
|
2351
|
+
expectByte(c, OP_OUTPUTASSETFIELD, `OP_OUTPUTASSETFIELD (buyer amount, ${label})`);
|
|
2352
|
+
expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (buyer amount, ${label})`);
|
|
2353
|
+
expectByte(c, OP_1, `OP_1 (buyer idx, ${label})`);
|
|
2354
|
+
expectByte(c, OP_1, `OP_1 (NAME sel, ${label})`);
|
|
2355
|
+
expectByte(c, OP_OUTPUTASSETFIELD, `OP_OUTPUTASSETFIELD (buyer name, ${label})`);
|
|
2356
|
+
const tokenId = readPush(c, `tokenId (${label})`);
|
|
2357
|
+
expectByte(c, OP_EQUALVERIFY, `OP_EQUALVERIFY (buyer name, ${label})`);
|
|
2358
|
+
return { unitPriceSats, paymentScriptPubKey, tokenId };
|
|
2359
|
+
}
|
|
2360
|
+
/**
|
|
2361
|
+
* Parse what `appendFillBranches` emits. Both branches must agree on price,
|
|
2362
|
+
* payment destination, tokenId and expiration; any deviation throws.
|
|
2363
|
+
*/
|
|
2364
|
+
function readFillBranches(c) {
|
|
2365
|
+
const selector = readPush(c, 'input-0 binding selector');
|
|
2366
|
+
if (!bytesEqual(selector, INPUT_INDEX_TXHASH_SELECTOR)) {
|
|
2367
|
+
throw new Error('parse: fill branches must open with the input-0 binding <0x8000> OP_TXHASH (covenants without it ' +
|
|
2368
|
+
'can be drained through a foreign input 0 and are not supported)');
|
|
2369
|
+
}
|
|
2370
|
+
expectByte(c, OP_TXHASH, 'OP_TXHASH (input-0 binding)');
|
|
2371
|
+
const digest = readPush(c, 'input-0 binding digest');
|
|
2372
|
+
if (!bytesEqual(digest, INPUT_ZERO_TXHASH)) {
|
|
2373
|
+
throw new Error('parse: input-0 binding digest is not OP_TXHASH(0x0080) at input 0');
|
|
2374
|
+
}
|
|
2375
|
+
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (input-0 binding)');
|
|
2376
|
+
// ═════ Full fill ═════
|
|
2377
|
+
expectByte(c, OP_IF, 'OP_IF (full fill)');
|
|
2378
|
+
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2379
|
+
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2380
|
+
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2381
|
+
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
2382
|
+
const full = readPaymentAndBuyerChecks(c, 'full');
|
|
2383
|
+
expectByte(c, OP_DROP, 'OP_DROP (full)');
|
|
2384
|
+
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2385
|
+
// ═════ Partial fill ═════
|
|
2386
|
+
expectByte(c, OP_ELSE, 'OP_ELSE (partial fill)');
|
|
2387
|
+
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2388
|
+
const partial = readPaymentAndBuyerChecks(c, 'partial');
|
|
2389
|
+
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2390
|
+
expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
|
|
2391
|
+
expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
|
|
2392
|
+
expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
|
|
2393
|
+
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
|
|
2394
|
+
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2395
|
+
expectByte(c, OP_1, 'OP_1 (NAME sel)');
|
|
2396
|
+
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
|
|
2397
|
+
const remainderTokenId = readPush(c, 'tokenId (remainder)');
|
|
2398
|
+
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
|
|
2399
|
+
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2400
|
+
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
2401
|
+
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
|
|
2402
|
+
expectByte(c, OP_OVER, 'OP_OVER');
|
|
2403
|
+
expectByte(c, OP_0, 'OP_0 (input idx)');
|
|
2404
|
+
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
2405
|
+
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
|
|
2406
|
+
expectByte(c, OP_SWAP, 'OP_SWAP');
|
|
2407
|
+
expectByte(c, OP_SUB, 'OP_SUB');
|
|
2408
|
+
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
|
|
2409
|
+
expectByte(c, OP_DROP, 'OP_DROP (partial)');
|
|
2410
|
+
expectByte(c, OP_1, 'OP_1 (true, partial)');
|
|
2411
|
+
expectByte(c, OP_ENDIF, 'OP_ENDIF (fill branches)');
|
|
2412
|
+
if (!bytesEqual(full.tokenId, partial.tokenId) || !bytesEqual(partial.tokenId, remainderTokenId)) {
|
|
2413
|
+
throw new Error('parse: tokenId differs between the fill checks');
|
|
2414
|
+
}
|
|
2415
|
+
if (full.unitPriceSats !== partial.unitPriceSats) {
|
|
2416
|
+
throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2417
|
+
}
|
|
2418
|
+
if (!bytesEqual(full.paymentScriptPubKey, partial.paymentScriptPubKey)) {
|
|
2419
|
+
throw new Error('parse: payment scriptPubKey differs between full-fill and partial-fill branches');
|
|
2420
|
+
}
|
|
2421
|
+
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2422
|
+
return {
|
|
2423
|
+
paymentScriptPubKey: full.paymentScriptPubKey,
|
|
2424
|
+
tokenId: new TextDecoder('utf-8', { fatal: true }).decode(full.tokenId),
|
|
2425
|
+
unitPriceSats: full.unitPriceSats,
|
|
2426
|
+
expiration: expirationFull
|
|
2427
|
+
};
|
|
2428
|
+
}
|
|
2429
|
+
|
|
2430
|
+
/**
|
|
2431
|
+
* Partial-Fill Sell Order covenant script (three-branch), ECDSA cancel.
|
|
2198
2432
|
*
|
|
2199
2433
|
* The covenant has three branches selected by the top of the unlock stack:
|
|
2200
2434
|
*
|
|
@@ -2214,11 +2448,8 @@ function assertSameExpiration(a, b, label) {
|
|
|
2214
2448
|
* amount == inputAmount - N)
|
|
2215
2449
|
* output[3+] = optional buyer change (not constrained by the covenant)
|
|
2216
2450
|
*
|
|
2217
|
-
*
|
|
2218
|
-
*
|
|
2219
|
-
* Comparing commitments rather than full scriptPubKeys is mandatory because
|
|
2220
|
-
* the remainder's asset wrapper carries a different `amountRaw` than the
|
|
2221
|
-
* spent UTXO's wrapper.
|
|
2451
|
+
* The order must be input 0 for either fill branch to succeed: see the
|
|
2452
|
+
* input-0 binding in `./fill-branches.ts`.
|
|
2222
2453
|
*
|
|
2223
2454
|
* Unlock stack shapes (scriptSig pushes, top → bottom):
|
|
2224
2455
|
*
|
|
@@ -2226,10 +2457,12 @@ function assertSameExpiration(a, b, label) {
|
|
|
2226
2457
|
* Full fill: <1> <0> (full-flag=1, cancel-flag=0)
|
|
2227
2458
|
* Partial fill: <N> <0> <0> (N, full-flag=0, cancel-flag=0)
|
|
2228
2459
|
*
|
|
2229
|
-
* The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`)
|
|
2230
|
-
*
|
|
2231
|
-
*
|
|
2232
|
-
*
|
|
2460
|
+
* The cancel branch uses classical ECDSA (`OP_HASH160 + OP_CHECKSIG`)
|
|
2461
|
+
* against a 20-byte key hash. The payment destination is independent of
|
|
2462
|
+
* that key: a legacy P2PKH seller passes `sellerAddress` (both at once),
|
|
2463
|
+
* any other seller with a secp256k1 key — typically a strict ECDSA v3
|
|
2464
|
+
* `nq1r…` wallet — passes `paymentAddress` and `sellerPubKeyHash`. For
|
|
2465
|
+
* post-quantum cancellation use `buildPartialFillScriptPQ`.
|
|
2233
2466
|
*/
|
|
2234
2467
|
const ASSET_NAME_MAX$1 = 32;
|
|
2235
2468
|
function decodeSellerAddress(sellerAddress) {
|
|
@@ -2238,8 +2471,8 @@ function decodeSellerAddress(sellerAddress) {
|
|
|
2238
2471
|
}
|
|
2239
2472
|
const decoded = decodeAddress(sellerAddress);
|
|
2240
2473
|
if (decoded.type !== 'p2pkh') {
|
|
2241
|
-
throw new Error(`sellerAddress must be a legacy P2PKH address
|
|
2242
|
-
`
|
|
2474
|
+
throw new Error(`sellerAddress must be a legacy P2PKH address; got ${decoded.type}. A seller with another address ` +
|
|
2475
|
+
`type passes paymentAddress and sellerPubKeyHash, or uses buildPartialFillScriptPQ for a PQ key.`);
|
|
2243
2476
|
}
|
|
2244
2477
|
const pkh = Uint8Array.from(decoded.hash);
|
|
2245
2478
|
if (pkh.length !== 20) {
|
|
@@ -2247,6 +2480,27 @@ function decodeSellerAddress(sellerAddress) {
|
|
|
2247
2480
|
}
|
|
2248
2481
|
return pkh;
|
|
2249
2482
|
}
|
|
2483
|
+
/** Cancel key hash and payment scriptPubKey from either form of seller params. */
|
|
2484
|
+
function resolveSeller(params) {
|
|
2485
|
+
const { sellerAddress, paymentAddress, sellerPubKeyHash } = params;
|
|
2486
|
+
if (sellerAddress !== undefined) {
|
|
2487
|
+
if (paymentAddress !== undefined || sellerPubKeyHash !== undefined) {
|
|
2488
|
+
throw new Error('pass either sellerAddress, or paymentAddress with sellerPubKeyHash, not both');
|
|
2489
|
+
}
|
|
2490
|
+
const pkh = decodeSellerAddress(sellerAddress);
|
|
2491
|
+
return { sellerPubKeyHash: pkh, paymentScriptPubKey: encodeP2PKHScriptPubKey(pkh) };
|
|
2492
|
+
}
|
|
2493
|
+
if (paymentAddress === undefined || sellerPubKeyHash === undefined) {
|
|
2494
|
+
throw new Error('sellerAddress, or paymentAddress with sellerPubKeyHash, is required');
|
|
2495
|
+
}
|
|
2496
|
+
if (!(sellerPubKeyHash instanceof Uint8Array) || sellerPubKeyHash.length !== 20) {
|
|
2497
|
+
throw new Error('sellerPubKeyHash must be a 20-byte Uint8Array (HASH160 of the compressed public key)');
|
|
2498
|
+
}
|
|
2499
|
+
return {
|
|
2500
|
+
sellerPubKeyHash: Uint8Array.from(sellerPubKeyHash),
|
|
2501
|
+
paymentScriptPubKey: encodeSellerScriptPubKey(paymentAddress).bytes
|
|
2502
|
+
};
|
|
2503
|
+
}
|
|
2250
2504
|
function assertTokenId$1(tokenId) {
|
|
2251
2505
|
if (typeof tokenId !== 'string' || tokenId.length === 0) {
|
|
2252
2506
|
throw new Error('tokenId is required');
|
|
@@ -2274,13 +2528,11 @@ function assertPrice$1(priceSats) {
|
|
|
2274
2528
|
* Returns raw bytes; wrap with `bytesToHex` for wire format.
|
|
2275
2529
|
*/
|
|
2276
2530
|
function buildPartialFillScript(params) {
|
|
2277
|
-
const {
|
|
2278
|
-
const sellerPubKeyHash =
|
|
2531
|
+
const { tokenId, unitPriceSats } = params;
|
|
2532
|
+
const { sellerPubKeyHash, paymentScriptPubKey } = resolveSeller(params);
|
|
2279
2533
|
assertTokenId$1(tokenId);
|
|
2280
2534
|
assertPrice$1(unitPriceSats);
|
|
2281
2535
|
const expiration = assertExpiration(params.expiration);
|
|
2282
|
-
const sellerScriptPubKey = encodeP2PKHScriptPubKey(sellerPubKeyHash);
|
|
2283
|
-
const tokenIdBytes = new TextEncoder().encode(tokenId);
|
|
2284
2536
|
const b = new ScriptBuilder();
|
|
2285
2537
|
// ════════ Cancel branch (outer IF) ════════
|
|
2286
2538
|
// scriptSig: <sig> <pubkey> <1>
|
|
@@ -2289,107 +2541,13 @@ function buildPartialFillScript(params) {
|
|
|
2289
2541
|
.pushBytes(sellerPubKeyHash)
|
|
2290
2542
|
.op(OP_EQUALVERIFY, OP_CHECKSIG)
|
|
2291
2543
|
.op(OP_ELSE);
|
|
2292
|
-
// ════════ Fill branches (outer ELSE
|
|
2293
|
-
b
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
// push N. The entire covenant is drained to vout[1]; no vout[2] is
|
|
2300
|
-
// constrained and none is required (consensus forbids zero-amount asset
|
|
2301
|
-
// transfers, so the partial-fill continuity check is simply skipped).
|
|
2302
|
-
appendExpirationGate(b, expiration);
|
|
2303
|
-
// 1. N = inputAmount
|
|
2304
|
-
b.pushInt(0)
|
|
2305
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2306
|
-
.op(OP_INPUTASSETFIELD); // [ N ]
|
|
2307
|
-
// 2. output[0].value >= N * unitPriceSats
|
|
2308
|
-
b.op(OP_DUP) // [ N, N ]
|
|
2309
|
-
.pushInt(unitPriceSats) // [ N, N, price ]
|
|
2310
|
-
.op(OP_MUL) // [ N, N*price ]
|
|
2311
|
-
.pushInt(0) // [ N, N*price, 0 ]
|
|
2312
|
-
.op(OP_OUTPUTVALUE) // [ N, N*price, value ]
|
|
2313
|
-
.op(OP_SWAP) // [ N, value, N*price ]
|
|
2314
|
-
.op(OP_GREATERTHANOREQUAL)
|
|
2315
|
-
.op(OP_VERIFY); // [ N ]
|
|
2316
|
-
// 3. output[0].scriptPubKey == sellerScriptPubKey
|
|
2317
|
-
b.pushInt(0)
|
|
2318
|
-
.op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
|
|
2319
|
-
.pushBytes(sellerScriptPubKey)
|
|
2320
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2321
|
-
// 4. output[1].asset.amount == N
|
|
2322
|
-
b.op(OP_DUP) // [ N, N ]
|
|
2323
|
-
.pushInt(1)
|
|
2324
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2325
|
-
.op(OP_OUTPUTASSETFIELD) // [ N, N, amount_out1 ]
|
|
2326
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2327
|
-
// 5. output[1].asset.name == tokenId
|
|
2328
|
-
b.pushInt(1)
|
|
2329
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2330
|
-
.op(OP_OUTPUTASSETFIELD) // [ N, name_out1 ]
|
|
2331
|
-
.pushBytes(tokenIdBytes)
|
|
2332
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2333
|
-
// 6. Drop N, leave TRUE on the stack.
|
|
2334
|
-
b.op(OP_DROP).pushInt(1);
|
|
2335
|
-
// ──────── Partial-fill branch (inner ELSE) ────────
|
|
2336
|
-
// scriptSig: <N> <0> <0> ( N, full-flag=0, cancel-flag=0 )
|
|
2337
|
-
// Stack entering: [ N ]
|
|
2338
|
-
b.op(OP_ELSE);
|
|
2339
|
-
appendExpirationGate(b, expiration);
|
|
2340
|
-
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
2341
|
-
b.op(OP_DUP) // [ N, N ]
|
|
2342
|
-
.pushInt(unitPriceSats) // [ N, N, price ]
|
|
2343
|
-
.op(OP_MUL) // [ N, N*price ]
|
|
2344
|
-
.pushInt(0) // [ N, N*price, 0 ]
|
|
2345
|
-
.op(OP_OUTPUTVALUE) // [ N, N*price, value ]
|
|
2346
|
-
.op(OP_SWAP) // [ N, value, N*price ]
|
|
2347
|
-
.op(OP_GREATERTHANOREQUAL)
|
|
2348
|
-
.op(OP_VERIFY); // [ N ]
|
|
2349
|
-
// 2. Payment scriptPubKey (output 0)
|
|
2350
|
-
b.pushInt(0)
|
|
2351
|
-
.op(OP_OUTPUTSCRIPT) // [ N, spk_out0 ]
|
|
2352
|
-
.pushBytes(sellerScriptPubKey)
|
|
2353
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2354
|
-
// 3. Asset to buyer (output 1) amount == N
|
|
2355
|
-
b.op(OP_DUP) // [ N, N ]
|
|
2356
|
-
.pushInt(1)
|
|
2357
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2358
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2359
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2360
|
-
// 4. Asset to buyer (output 1) name == tokenId
|
|
2361
|
-
b.pushInt(1)
|
|
2362
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2363
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2364
|
-
.pushBytes(tokenIdBytes)
|
|
2365
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2366
|
-
// 5. Continuation (output 2): same AuthScript commitment as spent UTXO
|
|
2367
|
-
// (NIP-023 — see file-level comment).
|
|
2368
|
-
b.pushInt(2)
|
|
2369
|
-
.op(OP_OUTPUTAUTHCOMMITMENT)
|
|
2370
|
-
.pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
|
|
2371
|
-
.op(OP_TXFIELD)
|
|
2372
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2373
|
-
// 6. Continuation name == tokenId
|
|
2374
|
-
b.pushInt(2)
|
|
2375
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2376
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2377
|
-
.pushBytes(tokenIdBytes)
|
|
2378
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2379
|
-
// 7. Continuation amount == inputAmount - N
|
|
2380
|
-
b.pushInt(2)
|
|
2381
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2382
|
-
.op(OP_OUTPUTASSETFIELD) // [ N, rem_amount ]
|
|
2383
|
-
.op(OP_OVER) // [ N, rem_amount, N ]
|
|
2384
|
-
.pushInt(0) // [ N, rem_amount, N, 0 ]
|
|
2385
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2386
|
-
.op(OP_INPUTASSETFIELD) // [ N, rem_amount, N, in_amount ]
|
|
2387
|
-
.op(OP_SWAP) // [ N, rem_amount, in_amount, N ]
|
|
2388
|
-
.op(OP_SUB) // [ N, rem_amount, in_amount - N ]
|
|
2389
|
-
.op(OP_EQUALVERIFY); // [ N ]
|
|
2390
|
-
// 8. Drop N, leave TRUE on the stack.
|
|
2391
|
-
b.op(OP_DROP).pushInt(1);
|
|
2392
|
-
b.op(OP_ENDIF); // closes full/partial inner IF
|
|
2544
|
+
// ════════ Fill branches (outer ELSE) ════════
|
|
2545
|
+
appendFillBranches(b, {
|
|
2546
|
+
paymentScriptPubKey,
|
|
2547
|
+
tokenIdBytes: new TextEncoder().encode(tokenId),
|
|
2548
|
+
unitPriceSats,
|
|
2549
|
+
expiration
|
|
2550
|
+
});
|
|
2393
2551
|
b.op(OP_ENDIF); // closes outer cancel/fill IF
|
|
2394
2552
|
return b.build();
|
|
2395
2553
|
}
|
|
@@ -2419,14 +2577,14 @@ function buildPartialFillScriptHex(params) {
|
|
|
2419
2577
|
*
|
|
2420
2578
|
* The message that gets signed is **SHA256(OP_TXHASH(selector))** — CSFS
|
|
2421
2579
|
* single-SHA256s its message argument before verification, and OP_TXHASH
|
|
2422
|
-
* produces its own 32-byte hash, so the seller computes:
|
|
2423
|
-
* `sign(pqSeckey, SHA256(
|
|
2580
|
+
* produces its own 32-byte NIP-042 tagged hash, so the seller computes:
|
|
2581
|
+
* `sign(pqSeckey, SHA256(TaggedHash("NeuraiTxHash", mask || selected_tx_fields)))`
|
|
2424
2582
|
*
|
|
2425
|
-
*
|
|
2426
|
-
*
|
|
2583
|
+
* The fill branches, including the input-0 binding, are shared with the
|
|
2584
|
+
* ECDSA variant — see `./fill-branches.ts`.
|
|
2427
2585
|
*/
|
|
2428
2586
|
const ASSET_NAME_MAX = 32;
|
|
2429
|
-
const DEFAULT_PQ_TXHASH_SELECTOR =
|
|
2587
|
+
const DEFAULT_PQ_TXHASH_SELECTOR = 0x1ff;
|
|
2430
2588
|
function assertCommitment(commitment) {
|
|
2431
2589
|
if (!(commitment instanceof Uint8Array) || commitment.length !== 32) {
|
|
2432
2590
|
throw new Error('pubKeyCommitment must be a 32-byte Uint8Array (SHA256 of pubKey)');
|
|
@@ -2455,11 +2613,11 @@ function assertPrice(priceSats) {
|
|
|
2455
2613
|
}
|
|
2456
2614
|
}
|
|
2457
2615
|
function assertSelector(selector) {
|
|
2458
|
-
if (!Number.isInteger(selector) || selector < 0 || selector >
|
|
2459
|
-
throw new Error('txHashSelector must be
|
|
2616
|
+
if (!Number.isInteger(selector) || selector < 0 || selector > 0x1ff) {
|
|
2617
|
+
throw new Error('txHashSelector must be in the NIP-042 range (0x001..0x1ff)');
|
|
2460
2618
|
}
|
|
2461
2619
|
if (selector === 0) {
|
|
2462
|
-
throw new Error('txHashSelector
|
|
2620
|
+
throw new Error('txHashSelector 0x000 is rejected by OP_TXHASH');
|
|
2463
2621
|
}
|
|
2464
2622
|
}
|
|
2465
2623
|
/**
|
|
@@ -2479,113 +2637,25 @@ function buildPartialFillScriptPQ(params) {
|
|
|
2479
2637
|
// scriptSig: <sigPQ> <pubKeyPQ> <1>
|
|
2480
2638
|
// After OP_IF consumes the flag: [ sig, pubKey ]
|
|
2481
2639
|
//
|
|
2482
|
-
//
|
|
2483
|
-
//
|
|
2484
|
-
// 1..127 but emit a 2-byte CScriptNum for 0x80..0xff (sign-disambiguation
|
|
2485
|
-
// pad), which makes OP_TXHASH fail with SCRIPT_ERR_TXHASH.
|
|
2640
|
+
// NIP-042 consumes exactly two raw little-endian bytes. CScriptNum and
|
|
2641
|
+
// OP_N encodings are invalid even when they represent the same mask.
|
|
2486
2642
|
b.op(OP_IF)
|
|
2487
2643
|
.op(OP_DUP) // [ sig, pubKey, pubKey ]
|
|
2488
2644
|
.op(OP_SHA256) // [ sig, pubKey, H(pubKey) ]
|
|
2489
2645
|
.pushBytes(pubKeyCommitment) // [ sig, pubKey, H(pubKey), commitment ]
|
|
2490
2646
|
.op(OP_EQUALVERIFY) // [ sig, pubKey ]
|
|
2491
|
-
.pushBytes(Uint8Array.of(txHashSelector)) // [ sig, pubKey, selector ]
|
|
2647
|
+
.pushBytes(Uint8Array.of(txHashSelector & 0xff, txHashSelector >> 8)) // [ sig, pubKey, selector ]
|
|
2492
2648
|
.op(OP_TXHASH) // [ sig, pubKey, txHash ]
|
|
2493
2649
|
.op(OP_SWAP) // [ sig, txHash, pubKey ]
|
|
2494
2650
|
.op(OP_CHECKSIGFROMSTACK) // [ 1 | 0 ]
|
|
2495
2651
|
.op(OP_ELSE);
|
|
2496
|
-
// ════════ Fill branches (
|
|
2497
|
-
b
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
b.pushInt(0)
|
|
2504
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2505
|
-
.op(OP_INPUTASSETFIELD);
|
|
2506
|
-
// 2. Payment value (output 0) >= N * unitPriceSats
|
|
2507
|
-
b.op(OP_DUP)
|
|
2508
|
-
.pushInt(unitPriceSats)
|
|
2509
|
-
.op(OP_MUL)
|
|
2510
|
-
.pushInt(0)
|
|
2511
|
-
.op(OP_OUTPUTVALUE)
|
|
2512
|
-
.op(OP_SWAP)
|
|
2513
|
-
.op(OP_GREATERTHANOREQUAL)
|
|
2514
|
-
.op(OP_VERIFY);
|
|
2515
|
-
// 3. Payment scriptPubKey (output 0)
|
|
2516
|
-
b.pushInt(0)
|
|
2517
|
-
.op(OP_OUTPUTSCRIPT)
|
|
2518
|
-
.pushBytes(payment.bytes)
|
|
2519
|
-
.op(OP_EQUALVERIFY);
|
|
2520
|
-
// 4. Asset to buyer (output 1) amount == N
|
|
2521
|
-
b.op(OP_DUP)
|
|
2522
|
-
.pushInt(1)
|
|
2523
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2524
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2525
|
-
.op(OP_EQUALVERIFY);
|
|
2526
|
-
// 5. Asset to buyer (output 1) name == tokenId
|
|
2527
|
-
b.pushInt(1)
|
|
2528
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2529
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2530
|
-
.pushBytes(tokenIdBytes)
|
|
2531
|
-
.op(OP_EQUALVERIFY);
|
|
2532
|
-
b.op(OP_DROP).pushInt(1);
|
|
2533
|
-
// ──────── Partial-fill branch ────────
|
|
2534
|
-
// scriptSig: <N> <0> <0>
|
|
2535
|
-
// Stack entering: [ N ]
|
|
2536
|
-
b.op(OP_ELSE);
|
|
2537
|
-
appendExpirationGate(b, expiration);
|
|
2538
|
-
// 1. Payment value (output 0) >= N * unitPriceSats
|
|
2539
|
-
b.op(OP_DUP)
|
|
2540
|
-
.pushInt(unitPriceSats)
|
|
2541
|
-
.op(OP_MUL)
|
|
2542
|
-
.pushInt(0)
|
|
2543
|
-
.op(OP_OUTPUTVALUE)
|
|
2544
|
-
.op(OP_SWAP)
|
|
2545
|
-
.op(OP_GREATERTHANOREQUAL)
|
|
2546
|
-
.op(OP_VERIFY);
|
|
2547
|
-
// 2. Payment scriptPubKey (output 0)
|
|
2548
|
-
b.pushInt(0)
|
|
2549
|
-
.op(OP_OUTPUTSCRIPT)
|
|
2550
|
-
.pushBytes(payment.bytes)
|
|
2551
|
-
.op(OP_EQUALVERIFY);
|
|
2552
|
-
// 3. Asset to buyer (output 1) amount == N
|
|
2553
|
-
b.op(OP_DUP)
|
|
2554
|
-
.pushInt(1)
|
|
2555
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2556
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2557
|
-
.op(OP_EQUALVERIFY);
|
|
2558
|
-
// 4. Asset to buyer (output 1) name == tokenId
|
|
2559
|
-
b.pushInt(1)
|
|
2560
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2561
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2562
|
-
.pushBytes(tokenIdBytes)
|
|
2563
|
-
.op(OP_EQUALVERIFY);
|
|
2564
|
-
// 5. Continuation (output 2): same AuthScript commitment as spent (NIP-023)
|
|
2565
|
-
b.pushInt(2)
|
|
2566
|
-
.op(OP_OUTPUTAUTHCOMMITMENT)
|
|
2567
|
-
.pushInt(TXFIELD_AUTHSCRIPT_COMMITMENT)
|
|
2568
|
-
.op(OP_TXFIELD)
|
|
2569
|
-
.op(OP_EQUALVERIFY);
|
|
2570
|
-
// 6. Continuation name == tokenId
|
|
2571
|
-
b.pushInt(2)
|
|
2572
|
-
.pushInt(ASSETFIELD_NAME)
|
|
2573
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2574
|
-
.pushBytes(tokenIdBytes)
|
|
2575
|
-
.op(OP_EQUALVERIFY);
|
|
2576
|
-
// 7. Continuation amount == inputAmount - N
|
|
2577
|
-
b.pushInt(2)
|
|
2578
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2579
|
-
.op(OP_OUTPUTASSETFIELD)
|
|
2580
|
-
.op(OP_OVER)
|
|
2581
|
-
.pushInt(0)
|
|
2582
|
-
.pushInt(ASSETFIELD_AMOUNT)
|
|
2583
|
-
.op(OP_INPUTASSETFIELD)
|
|
2584
|
-
.op(OP_SWAP)
|
|
2585
|
-
.op(OP_SUB)
|
|
2586
|
-
.op(OP_EQUALVERIFY);
|
|
2587
|
-
b.op(OP_DROP).pushInt(1);
|
|
2588
|
-
b.op(OP_ENDIF); // full / partial
|
|
2652
|
+
// ════════ Fill branches (outer ELSE) ════════
|
|
2653
|
+
appendFillBranches(b, {
|
|
2654
|
+
paymentScriptPubKey: payment.bytes,
|
|
2655
|
+
tokenIdBytes,
|
|
2656
|
+
unitPriceSats,
|
|
2657
|
+
expiration
|
|
2658
|
+
});
|
|
2589
2659
|
b.op(OP_ENDIF); // cancel / fill
|
|
2590
2660
|
return b.build();
|
|
2591
2661
|
}
|
|
@@ -2808,17 +2878,15 @@ function buildCancelWitnessStackPQ(sigPQ, pubKey) {
|
|
|
2808
2878
|
}
|
|
2809
2879
|
|
|
2810
2880
|
/**
|
|
2811
|
-
* Parser for the Partial-Fill Sell Order covenant (three-branch).
|
|
2812
|
-
*
|
|
2813
|
-
* Extracts `(sellerPubKeyHash, unitPriceSats, tokenId)`
|
|
2814
|
-
* that was produced by `buildPartialFillScript`. Walks
|
|
2815
|
-
* layout emitted by the builder and fails on any deviation —
|
|
2816
|
-
* deliberate, so a downstream indexer can unambiguously classify a
|
|
2817
|
-
* "partial-fill order" or "unknown script" with no false positives.
|
|
2818
|
-
*
|
|
2819
|
-
*
|
|
2820
|
-
* unitPriceSats, tokenId)`. The parser reads both branches and verifies
|
|
2821
|
-
* consistency; inconsistency throws.
|
|
2881
|
+
* Parser for the Partial-Fill Sell Order covenant (three-branch, ECDSA cancel).
|
|
2882
|
+
*
|
|
2883
|
+
* Extracts `(sellerPubKeyHash, paymentScriptPubKey, unitPriceSats, tokenId)`
|
|
2884
|
+
* from a scriptPubKey that was produced by `buildPartialFillScript`. Walks
|
|
2885
|
+
* the exact byte layout emitted by the builder and fails on any deviation —
|
|
2886
|
+
* this is deliberate, so a downstream indexer can unambiguously classify a
|
|
2887
|
+
* UTXO as "partial-fill order" or "unknown script" with no false positives.
|
|
2888
|
+
* Covenants built before the input-0 binding (neurai-scripts < 0.10.0) are
|
|
2889
|
+
* rejected: their fill branches can be drained through a foreign input 0.
|
|
2822
2890
|
*/
|
|
2823
2891
|
/**
|
|
2824
2892
|
* Parse a covenant scriptPubKey and extract its parameters. Throws with a
|
|
@@ -2838,129 +2906,17 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2838
2906
|
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
|
|
2839
2907
|
expectByte(c, OP_CHECKSIG, 'OP_CHECKSIG (cancel)');
|
|
2840
2908
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
2841
|
-
// ═════
|
|
2842
|
-
|
|
2843
|
-
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
2844
|
-
// N = inputAmount
|
|
2845
|
-
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
2846
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2847
|
-
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
2848
|
-
// payment value check
|
|
2849
|
-
expectByte(c, OP_DUP, 'OP_DUP (price, full)');
|
|
2850
|
-
const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
|
|
2851
|
-
expectByte(c, OP_MUL, 'OP_MUL (full)');
|
|
2852
|
-
expectByte(c, OP_0, 'OP_0 (payment idx, full)');
|
|
2853
|
-
expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
|
|
2854
|
-
expectByte(c, OP_SWAP, 'OP_SWAP (full)');
|
|
2855
|
-
expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (full)');
|
|
2856
|
-
expectByte(c, OP_VERIFY, 'OP_VERIFY (payment full)');
|
|
2857
|
-
// payment spk
|
|
2858
|
-
expectByte(c, OP_0, 'OP_0 (spk idx, full)');
|
|
2859
|
-
expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
|
|
2860
|
-
const sellerSpkFull = readPush(c, 'sellerScriptPubKey (full)');
|
|
2861
|
-
const expectedSpk = new Uint8Array([
|
|
2862
|
-
OP_DUP, OP_HASH160, 0x14, ...sellerPubKeyHash, OP_EQUALVERIFY, OP_CHECKSIG
|
|
2863
|
-
]);
|
|
2864
|
-
if (!bytesEqual(sellerSpkFull, expectedSpk)) {
|
|
2865
|
-
throw new Error('parse: full-fill seller scriptPubKey does not match the cancel PKH');
|
|
2866
|
-
}
|
|
2867
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk full)');
|
|
2868
|
-
// buyer amount == N (full)
|
|
2869
|
-
expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
|
|
2870
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
|
|
2871
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
2872
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
|
|
2873
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
|
|
2874
|
-
// buyer name == tokenId (full)
|
|
2875
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
|
|
2876
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
|
|
2877
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
|
|
2878
|
-
const tokenIdFull = readPush(c, 'tokenId (full)');
|
|
2879
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
|
|
2880
|
-
// tail (full)
|
|
2881
|
-
expectByte(c, OP_DROP, 'OP_DROP (full)');
|
|
2882
|
-
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
2883
|
-
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
2884
|
-
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
2885
|
-
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
2886
|
-
// Payment value
|
|
2887
|
-
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
2888
|
-
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
2889
|
-
expectByte(c, OP_MUL, 'OP_MUL (partial)');
|
|
2890
|
-
expectByte(c, OP_0, 'OP_0 (pay idx, partial)');
|
|
2891
|
-
expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
|
|
2892
|
-
expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
|
|
2893
|
-
expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GREATERTHANOREQUAL (partial)');
|
|
2894
|
-
expectByte(c, OP_VERIFY, 'OP_VERIFY (payment partial)');
|
|
2895
|
-
// Payment spk
|
|
2896
|
-
expectByte(c, OP_0, 'OP_0 (spk idx, partial)');
|
|
2897
|
-
expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
|
|
2898
|
-
const sellerSpkPartial = readPush(c, 'sellerScriptPubKey (partial)');
|
|
2899
|
-
if (!bytesEqual(sellerSpkPartial, expectedSpk)) {
|
|
2900
|
-
throw new Error('parse: partial-fill seller scriptPubKey does not match the cancel PKH');
|
|
2901
|
-
}
|
|
2902
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment spk partial)');
|
|
2903
|
-
// Buyer amount
|
|
2904
|
-
expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
|
|
2905
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
|
|
2906
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
|
|
2907
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
|
|
2908
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
|
|
2909
|
-
// Buyer name
|
|
2910
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
|
|
2911
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
|
|
2912
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
|
|
2913
|
-
const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
|
|
2914
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
|
|
2915
|
-
// Continuation commitment
|
|
2916
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2917
|
-
expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
|
|
2918
|
-
expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
|
|
2919
|
-
expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
|
|
2920
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
|
|
2921
|
-
// Continuation name
|
|
2922
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2923
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel)');
|
|
2924
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
|
|
2925
|
-
const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
|
|
2926
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
|
|
2927
|
-
// Continuation amount
|
|
2928
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
2929
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
2930
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
|
|
2931
|
-
expectByte(c, OP_OVER, 'OP_OVER');
|
|
2932
|
-
expectByte(c, OP_0, 'OP_0 (input idx)');
|
|
2933
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
2934
|
-
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
|
|
2935
|
-
expectByte(c, OP_SWAP, 'OP_SWAP');
|
|
2936
|
-
expectByte(c, OP_SUB, 'OP_SUB');
|
|
2937
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
|
|
2938
|
-
// Tail (partial)
|
|
2939
|
-
expectByte(c, OP_DROP, 'OP_DROP (partial)');
|
|
2940
|
-
expectByte(c, OP_1, 'OP_1 (true, partial)');
|
|
2941
|
-
// Close the nested structure
|
|
2942
|
-
expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
|
|
2909
|
+
// ═════ Outer ELSE — input-0 binding + fill branches ═════
|
|
2910
|
+
const fill = readFillBranches(c);
|
|
2943
2911
|
expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
|
|
2944
2912
|
assertTrailing(c);
|
|
2945
|
-
// Cross-branch consistency: all three (full token, partial name #1,
|
|
2946
|
-
// partial name #2) must agree, and the two unit prices must match.
|
|
2947
|
-
if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
|
|
2948
|
-
throw new Error('parse: tokenId differs between full-fill and partial-fill branches');
|
|
2949
|
-
}
|
|
2950
|
-
if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
|
|
2951
|
-
throw new Error('parse: tokenId bytes differ between buyer and remainder partial checks');
|
|
2952
|
-
}
|
|
2953
|
-
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
2954
|
-
throw new Error('parse: unitPriceSats differs between full-fill and partial-fill branches');
|
|
2955
|
-
}
|
|
2956
|
-
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
2957
|
-
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
2958
2913
|
return {
|
|
2959
2914
|
network,
|
|
2960
2915
|
sellerPubKeyHash,
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2916
|
+
paymentScriptPubKey: fill.paymentScriptPubKey,
|
|
2917
|
+
unitPriceSats: fill.unitPriceSats,
|
|
2918
|
+
tokenId: fill.tokenId,
|
|
2919
|
+
expiration: fill.expiration,
|
|
2964
2920
|
scriptHex: bytesToHex(bytes)
|
|
2965
2921
|
};
|
|
2966
2922
|
}
|
|
@@ -2971,9 +2927,8 @@ function parsePartialFillScript(script, network = 'xna-test') {
|
|
|
2971
2927
|
* payment scriptPubKey bytes (which may be P2PKH or AuthScript) and the
|
|
2972
2928
|
* configured TXHASH selector.
|
|
2973
2929
|
*
|
|
2974
|
-
* The
|
|
2975
|
-
*
|
|
2976
|
-
* consistency; inconsistency throws.
|
|
2930
|
+
* The fill branches (and their input-0 binding) are read by the shared
|
|
2931
|
+
* `readFillBranches`; covenants built before the binding are rejected.
|
|
2977
2932
|
*/
|
|
2978
2933
|
/** Quick discriminator without throwing — useful for indexers. */
|
|
2979
2934
|
function isPartialFillScriptPQ(script) {
|
|
@@ -3000,118 +2955,30 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
|
|
|
3000
2955
|
throw new Error(`parse-pq: pubKeyCommitment must be 32 bytes, got ${pubKeyCommitment.length}`);
|
|
3001
2956
|
}
|
|
3002
2957
|
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (cancel)');
|
|
3003
|
-
const
|
|
3004
|
-
if (
|
|
3005
|
-
throw new Error(`parse-pq: txHashSelector
|
|
2958
|
+
const selectorBytes = readPush(c, 'txHashSelector');
|
|
2959
|
+
if (selectorBytes.length !== 2) {
|
|
2960
|
+
throw new Error(`parse-pq: txHashSelector must be a two-byte LE push, got ${selectorBytes.length} bytes`);
|
|
2961
|
+
}
|
|
2962
|
+
const txHashSelector = selectorBytes[0] | (selectorBytes[1] << 8);
|
|
2963
|
+
if (txHashSelector === 0 || txHashSelector > 0x1ff) {
|
|
2964
|
+
throw new Error(`parse-pq: txHashSelector 0x${txHashSelector.toString(16)} is rejected by OP_TXHASH`);
|
|
3006
2965
|
}
|
|
3007
2966
|
expectByte(c, OP_TXHASH, 'OP_TXHASH');
|
|
3008
2967
|
expectByte(c, OP_SWAP, 'OP_SWAP');
|
|
3009
2968
|
expectByte(c, OP_CHECKSIGFROMSTACK, 'OP_CHECKSIGFROMSTACK');
|
|
3010
2969
|
expectByte(c, OP_ELSE, 'OP_ELSE (outer → fill)');
|
|
3011
|
-
// ═════
|
|
3012
|
-
|
|
3013
|
-
const expirationFull = readOptionalExpirationGate(c, OP_0, 'full');
|
|
3014
|
-
expectByte(c, OP_0, 'OP_0 (input idx, full)');
|
|
3015
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
3016
|
-
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD (full)');
|
|
3017
|
-
expectByte(c, OP_DUP, 'OP_DUP (price, full)');
|
|
3018
|
-
const unitPriceSatsFull = readPushPositiveInt(c, 'unitPriceSats (full)');
|
|
3019
|
-
expectByte(c, OP_MUL, 'OP_MUL (full)');
|
|
3020
|
-
expectByte(c, OP_0, 'OP_0 (payment idx, full)');
|
|
3021
|
-
expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (full)');
|
|
3022
|
-
expectByte(c, OP_SWAP, 'OP_SWAP (full)');
|
|
3023
|
-
expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (full)');
|
|
3024
|
-
expectByte(c, OP_VERIFY, 'OP_VERIFY (full)');
|
|
3025
|
-
expectByte(c, OP_0, 'OP_0 (payment spk idx, full)');
|
|
3026
|
-
expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment full)');
|
|
3027
|
-
const paymentScriptPubKeyFull = readPush(c, 'paymentScriptPubKey (full)');
|
|
3028
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment full)');
|
|
3029
|
-
expectByte(c, OP_DUP, 'OP_DUP (buyer amount, full)');
|
|
3030
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
|
|
3031
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, full)');
|
|
3032
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, full)');
|
|
3033
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, full)');
|
|
3034
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, full)');
|
|
3035
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel, full)');
|
|
3036
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, full)');
|
|
3037
|
-
const tokenIdFull = readPush(c, 'tokenId (full)');
|
|
3038
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, full)');
|
|
3039
|
-
expectByte(c, OP_DROP, 'OP_DROP (full)');
|
|
3040
|
-
expectByte(c, OP_1, 'OP_1 (true, full)');
|
|
3041
|
-
expectByte(c, OP_ELSE, 'OP_ELSE (inner → partial fill)');
|
|
3042
|
-
// ═════ Inner ELSE — Partial-fill branch ═════
|
|
3043
|
-
const expirationPartial = readOptionalExpirationGate(c, OP_DUP, 'partial');
|
|
3044
|
-
expectByte(c, OP_DUP, 'OP_DUP (price, partial)');
|
|
3045
|
-
const unitPriceSatsPartial = readPushPositiveInt(c, 'unitPriceSats (partial)');
|
|
3046
|
-
expectByte(c, OP_MUL, 'OP_MUL (partial)');
|
|
3047
|
-
expectByte(c, OP_0, 'OP_0 (payment idx, partial)');
|
|
3048
|
-
expectByte(c, OP_OUTPUTVALUE, 'OP_OUTPUTVALUE (partial)');
|
|
3049
|
-
expectByte(c, OP_SWAP, 'OP_SWAP (partial)');
|
|
3050
|
-
expectByte(c, OP_GREATERTHANOREQUAL, 'OP_GE (partial)');
|
|
3051
|
-
expectByte(c, OP_VERIFY, 'OP_VERIFY (partial)');
|
|
3052
|
-
expectByte(c, OP_0, 'OP_0 (payment spk idx, partial)');
|
|
3053
|
-
expectByte(c, OP_OUTPUTSCRIPT, 'OP_OUTPUTSCRIPT (payment partial)');
|
|
3054
|
-
const paymentScriptPubKeyPartial = readPush(c, 'paymentScriptPubKey (partial)');
|
|
3055
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (payment partial)');
|
|
3056
|
-
expectByte(c, OP_DUP, 'OP_DUP (buyer amount, partial)');
|
|
3057
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
|
|
3058
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel, partial)');
|
|
3059
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer amount, partial)');
|
|
3060
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer amount, partial)');
|
|
3061
|
-
expectByte(c, OP_1, 'OP_1 (buyer idx, partial)');
|
|
3062
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel, partial)');
|
|
3063
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (buyer name, partial)');
|
|
3064
|
-
const tokenIdPartial1 = readPush(c, 'tokenId partial #1');
|
|
3065
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (buyer name, partial)');
|
|
3066
|
-
// Continuation commitment
|
|
3067
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
3068
|
-
expectByte(c, OP_OUTPUTAUTHCOMMITMENT, 'OP_OUTPUTAUTHCOMMITMENT (remainder)');
|
|
3069
|
-
expectByte(c, OP_2, 'OP_2 (TXFIELD sel: AUTHSCRIPT_COMMITMENT)');
|
|
3070
|
-
expectByte(c, OP_TXFIELD, 'OP_TXFIELD');
|
|
3071
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder auth)');
|
|
3072
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
3073
|
-
expectByte(c, OP_1, 'OP_1 (NAME sel)');
|
|
3074
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder name)');
|
|
3075
|
-
const tokenIdPartial2 = readPush(c, 'tokenId partial #2');
|
|
3076
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder name)');
|
|
3077
|
-
expectByte(c, OP_2, 'OP_2 (remainder idx)');
|
|
3078
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
3079
|
-
expectByte(c, OP_OUTPUTASSETFIELD, 'OP_OUTPUTASSETFIELD (remainder amount)');
|
|
3080
|
-
expectByte(c, OP_OVER, 'OP_OVER');
|
|
3081
|
-
expectByte(c, OP_0, 'OP_0 (input idx)');
|
|
3082
|
-
expectByte(c, OP_2, 'OP_2 (AMOUNT sel)');
|
|
3083
|
-
expectByte(c, OP_INPUTASSETFIELD, 'OP_INPUTASSETFIELD');
|
|
3084
|
-
expectByte(c, OP_SWAP, 'OP_SWAP');
|
|
3085
|
-
expectByte(c, OP_SUB, 'OP_SUB');
|
|
3086
|
-
expectByte(c, OP_EQUALVERIFY, 'OP_EQUALVERIFY (remainder amount)');
|
|
3087
|
-
expectByte(c, OP_DROP, 'OP_DROP (partial)');
|
|
3088
|
-
expectByte(c, OP_1, 'OP_1 (true, partial)');
|
|
3089
|
-
expectByte(c, OP_ENDIF, 'OP_ENDIF (inner)');
|
|
2970
|
+
// ═════ Outer ELSE — input-0 binding + fill branches ═════
|
|
2971
|
+
const fill = readFillBranches(c);
|
|
3090
2972
|
expectByte(c, OP_ENDIF, 'OP_ENDIF (outer)');
|
|
3091
2973
|
assertTrailing(c);
|
|
3092
|
-
// Cross-branch consistency.
|
|
3093
|
-
if (!bytesEqual(paymentScriptPubKeyFull, paymentScriptPubKeyPartial)) {
|
|
3094
|
-
throw new Error('parse-pq: paymentScriptPubKey differs between full-fill and partial-fill branches');
|
|
3095
|
-
}
|
|
3096
|
-
if (!bytesEqual(tokenIdFull, tokenIdPartial1)) {
|
|
3097
|
-
throw new Error('parse-pq: tokenId differs between full-fill and partial-fill branches');
|
|
3098
|
-
}
|
|
3099
|
-
if (!bytesEqual(tokenIdPartial1, tokenIdPartial2)) {
|
|
3100
|
-
throw new Error('parse-pq: tokenId differs between buyer and remainder partial checks');
|
|
3101
|
-
}
|
|
3102
|
-
if (unitPriceSatsFull !== unitPriceSatsPartial) {
|
|
3103
|
-
throw new Error('parse-pq: unitPriceSats differs between full-fill and partial-fill branches');
|
|
3104
|
-
}
|
|
3105
|
-
assertSameExpiration(expirationFull, expirationPartial, 'full-fill and partial-fill branches');
|
|
3106
|
-
const tokenId = new TextDecoder('utf-8', { fatal: true }).decode(tokenIdFull);
|
|
3107
2974
|
return {
|
|
3108
2975
|
network,
|
|
3109
2976
|
pubKeyCommitment,
|
|
3110
|
-
tokenId,
|
|
3111
|
-
unitPriceSats:
|
|
2977
|
+
tokenId: fill.tokenId,
|
|
2978
|
+
unitPriceSats: fill.unitPriceSats,
|
|
3112
2979
|
txHashSelector,
|
|
3113
|
-
expiration:
|
|
3114
|
-
paymentScriptPubKey:
|
|
2980
|
+
expiration: fill.expiration,
|
|
2981
|
+
paymentScriptPubKey: fill.paymentScriptPubKey,
|
|
3115
2982
|
scriptHex: bytesToHex(bytes)
|
|
3116
2983
|
};
|
|
3117
2984
|
}
|