@neuraiproject/neurai-scripts 0.6.2 → 0.7.1

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/dist/browser.js CHANGED
@@ -1118,43 +1118,43 @@ var distExports = requireDist();
1118
1118
  // Makes the utils un-importable in browsers without a bundler.
1119
1119
  // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
1120
1120
  /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
1121
- function isBytes(a) {
1121
+ function isBytes$1(a) {
1122
1122
  return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
1123
1123
  }
1124
1124
  /** Asserts something is Uint8Array. */
1125
- function abytes(b, ...lengths) {
1126
- if (!isBytes(b))
1125
+ function abytes$1(b, ...lengths) {
1126
+ if (!isBytes$1(b))
1127
1127
  throw new Error('Uint8Array expected');
1128
1128
  if (lengths.length > 0 && !lengths.includes(b.length))
1129
1129
  throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
1130
1130
  }
1131
1131
  /** Asserts a hash instance has not been destroyed / finished */
1132
- function aexists(instance, checkFinished = true) {
1132
+ function aexists$1(instance, checkFinished = true) {
1133
1133
  if (instance.destroyed)
1134
1134
  throw new Error('Hash instance has been destroyed');
1135
1135
  if (checkFinished && instance.finished)
1136
1136
  throw new Error('Hash#digest() has already been called');
1137
1137
  }
1138
1138
  /** Asserts output is properly-sized byte array */
1139
- function aoutput(out, instance) {
1140
- abytes(out);
1139
+ function aoutput$1(out, instance) {
1140
+ abytes$1(out);
1141
1141
  const min = instance.outputLen;
1142
1142
  if (out.length < min) {
1143
1143
  throw new Error('digestInto() expects output buffer of length at least ' + min);
1144
1144
  }
1145
1145
  }
1146
1146
  /** Zeroize a byte array. Warning: JS provides no guarantees. */
1147
- function clean(...arrays) {
1147
+ function clean$1(...arrays) {
1148
1148
  for (let i = 0; i < arrays.length; i++) {
1149
1149
  arrays[i].fill(0);
1150
1150
  }
1151
1151
  }
1152
1152
  /** Create DataView of an array for easy byte-level manipulation. */
1153
- function createView(arr) {
1153
+ function createView$1(arr) {
1154
1154
  return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
1155
1155
  }
1156
1156
  /** The rotate right (circular right shift) operation for uint32 */
1157
- function rotr(word, shift) {
1157
+ function rotr$1(word, shift) {
1158
1158
  return (word << (32 - shift)) | (word >>> shift);
1159
1159
  }
1160
1160
  /**
@@ -1174,14 +1174,14 @@ function utf8ToBytes(str) {
1174
1174
  function toBytes(data) {
1175
1175
  if (typeof data === 'string')
1176
1176
  data = utf8ToBytes(data);
1177
- abytes(data);
1177
+ abytes$1(data);
1178
1178
  return data;
1179
1179
  }
1180
1180
  /** For runtime check if class implements interface */
1181
1181
  class Hash {
1182
1182
  }
1183
1183
  /** Wraps hash function, creating an interface on top of it */
1184
- function createHasher(hashCons) {
1184
+ function createHasher$1(hashCons) {
1185
1185
  const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
1186
1186
  const tmp = hashCons();
1187
1187
  hashC.outputLen = tmp.outputLen;
@@ -1208,18 +1208,18 @@ function setBigUint64(view, byteOffset, value, isLE) {
1208
1208
  view.setUint32(byteOffset + l, wl, isLE);
1209
1209
  }
1210
1210
  /** Choice: a ? b : c */
1211
- function Chi(a, b, c) {
1211
+ function Chi$1(a, b, c) {
1212
1212
  return (a & b) ^ (~a & c);
1213
1213
  }
1214
1214
  /** Majority function, true if any two inputs is true. */
1215
- function Maj(a, b, c) {
1215
+ function Maj$1(a, b, c) {
1216
1216
  return (a & b) ^ (a & c) ^ (b & c);
1217
1217
  }
1218
1218
  /**
1219
1219
  * Merkle-Damgard hash construction base class.
1220
1220
  * Could be used to create MD5, RIPEMD, SHA1, SHA2.
1221
1221
  */
1222
- class HashMD extends Hash {
1222
+ let HashMD$1 = class HashMD extends Hash {
1223
1223
  constructor(blockLen, outputLen, padOffset, isLE) {
1224
1224
  super();
1225
1225
  this.finished = false;
@@ -1231,19 +1231,19 @@ class HashMD extends Hash {
1231
1231
  this.padOffset = padOffset;
1232
1232
  this.isLE = isLE;
1233
1233
  this.buffer = new Uint8Array(blockLen);
1234
- this.view = createView(this.buffer);
1234
+ this.view = createView$1(this.buffer);
1235
1235
  }
1236
1236
  update(data) {
1237
- aexists(this);
1237
+ aexists$1(this);
1238
1238
  data = toBytes(data);
1239
- abytes(data);
1239
+ abytes$1(data);
1240
1240
  const { view, buffer, blockLen } = this;
1241
1241
  const len = data.length;
1242
1242
  for (let pos = 0; pos < len;) {
1243
1243
  const take = Math.min(blockLen - this.pos, len - pos);
1244
1244
  // Fast path: we have at least one block in input, cast it to view and process
1245
1245
  if (take === blockLen) {
1246
- const dataView = createView(data);
1246
+ const dataView = createView$1(data);
1247
1247
  for (; blockLen <= len - pos; pos += blockLen)
1248
1248
  this.process(dataView, pos);
1249
1249
  continue;
@@ -1261,8 +1261,8 @@ class HashMD extends Hash {
1261
1261
  return this;
1262
1262
  }
1263
1263
  digestInto(out) {
1264
- aexists(this);
1265
- aoutput(out, this);
1264
+ aexists$1(this);
1265
+ aoutput$1(out, this);
1266
1266
  this.finished = true;
1267
1267
  // Padding
1268
1268
  // We can avoid allocation of buffer for padding completely if it
@@ -1271,7 +1271,7 @@ class HashMD extends Hash {
1271
1271
  let { pos } = this;
1272
1272
  // append the bit '1' to the message
1273
1273
  buffer[pos++] = 0b10000000;
1274
- clean(this.buffer.subarray(pos));
1274
+ clean$1(this.buffer.subarray(pos));
1275
1275
  // we have less than padOffset left in buffer, so we cannot put length in
1276
1276
  // current block, need process it and pad again
1277
1277
  if (this.padOffset > blockLen - pos) {
@@ -1286,7 +1286,7 @@ class HashMD extends Hash {
1286
1286
  // So we just write lowest 64 bits of that value.
1287
1287
  setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
1288
1288
  this.process(view, 0);
1289
- const oview = createView(out);
1289
+ const oview = createView$1(out);
1290
1290
  const len = this.outputLen;
1291
1291
  // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
1292
1292
  if (len % 4)
@@ -1320,13 +1320,13 @@ class HashMD extends Hash {
1320
1320
  clone() {
1321
1321
  return this._cloneInto();
1322
1322
  }
1323
- }
1323
+ };
1324
1324
  /**
1325
1325
  * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
1326
1326
  * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
1327
1327
  */
1328
1328
  /** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
1329
- const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
1329
+ const SHA256_IV$1 = /* @__PURE__ */ Uint32Array.from([
1330
1330
  0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
1331
1331
  ]);
1332
1332
 
@@ -1342,7 +1342,7 @@ const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
1342
1342
  * First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
1343
1343
  */
1344
1344
  // prettier-ignore
1345
- const SHA256_K = /* @__PURE__ */ Uint32Array.from([
1345
+ const SHA256_K$1 = /* @__PURE__ */ Uint32Array.from([
1346
1346
  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
1347
1347
  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
1348
1348
  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
@@ -1353,20 +1353,20 @@ const SHA256_K = /* @__PURE__ */ Uint32Array.from([
1353
1353
  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
1354
1354
  ]);
1355
1355
  /** Reusable temporary buffer. "W" comes straight from spec. */
1356
- const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
1357
- class SHA256 extends HashMD {
1356
+ const SHA256_W$1 = /* @__PURE__ */ new Uint32Array(64);
1357
+ class SHA256 extends HashMD$1 {
1358
1358
  constructor(outputLen = 32) {
1359
1359
  super(64, outputLen, 8, false);
1360
1360
  // We cannot use array here since array allows indexing by variable
1361
1361
  // which means optimizer/compiler cannot use registers.
1362
- this.A = SHA256_IV[0] | 0;
1363
- this.B = SHA256_IV[1] | 0;
1364
- this.C = SHA256_IV[2] | 0;
1365
- this.D = SHA256_IV[3] | 0;
1366
- this.E = SHA256_IV[4] | 0;
1367
- this.F = SHA256_IV[5] | 0;
1368
- this.G = SHA256_IV[6] | 0;
1369
- this.H = SHA256_IV[7] | 0;
1362
+ this.A = SHA256_IV$1[0] | 0;
1363
+ this.B = SHA256_IV$1[1] | 0;
1364
+ this.C = SHA256_IV$1[2] | 0;
1365
+ this.D = SHA256_IV$1[3] | 0;
1366
+ this.E = SHA256_IV$1[4] | 0;
1367
+ this.F = SHA256_IV$1[5] | 0;
1368
+ this.G = SHA256_IV$1[6] | 0;
1369
+ this.H = SHA256_IV$1[7] | 0;
1370
1370
  }
1371
1371
  get() {
1372
1372
  const { A, B, C, D, E, F, G, H } = this;
@@ -1386,21 +1386,21 @@ class SHA256 extends HashMD {
1386
1386
  process(view, offset) {
1387
1387
  // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
1388
1388
  for (let i = 0; i < 16; i++, offset += 4)
1389
- SHA256_W[i] = view.getUint32(offset, false);
1389
+ SHA256_W$1[i] = view.getUint32(offset, false);
1390
1390
  for (let i = 16; i < 64; i++) {
1391
- const W15 = SHA256_W[i - 15];
1392
- const W2 = SHA256_W[i - 2];
1393
- const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
1394
- const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
1395
- SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
1391
+ const W15 = SHA256_W$1[i - 15];
1392
+ const W2 = SHA256_W$1[i - 2];
1393
+ const s0 = rotr$1(W15, 7) ^ rotr$1(W15, 18) ^ (W15 >>> 3);
1394
+ const s1 = rotr$1(W2, 17) ^ rotr$1(W2, 19) ^ (W2 >>> 10);
1395
+ SHA256_W$1[i] = (s1 + SHA256_W$1[i - 7] + s0 + SHA256_W$1[i - 16]) | 0;
1396
1396
  }
1397
1397
  // Compression function main loop, 64 rounds
1398
1398
  let { A, B, C, D, E, F, G, H } = this;
1399
1399
  for (let i = 0; i < 64; i++) {
1400
- const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
1401
- const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
1402
- const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
1403
- const T2 = (sigma0 + Maj(A, B, C)) | 0;
1400
+ const sigma1 = rotr$1(E, 6) ^ rotr$1(E, 11) ^ rotr$1(E, 25);
1401
+ const T1 = (H + sigma1 + Chi$1(E, F, G) + SHA256_K$1[i] + SHA256_W$1[i]) | 0;
1402
+ const sigma0 = rotr$1(A, 2) ^ rotr$1(A, 13) ^ rotr$1(A, 22);
1403
+ const T2 = (sigma0 + Maj$1(A, B, C)) | 0;
1404
1404
  H = G;
1405
1405
  G = F;
1406
1406
  F = E;
@@ -1422,11 +1422,11 @@ class SHA256 extends HashMD {
1422
1422
  this.set(A, B, C, D, E, F, G, H);
1423
1423
  }
1424
1424
  roundClean() {
1425
- clean(SHA256_W);
1425
+ clean$1(SHA256_W$1);
1426
1426
  }
1427
1427
  destroy() {
1428
1428
  this.set(0, 0, 0, 0, 0, 0, 0, 0);
1429
- clean(this.buffer);
1429
+ clean$1(this.buffer);
1430
1430
  }
1431
1431
  }
1432
1432
  /**
@@ -1436,7 +1436,7 @@ class SHA256 extends HashMD {
1436
1436
  * To break sha256 using birthday attack, attackers need to try 2^128 hashes.
1437
1437
  * BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
1438
1438
  */
1439
- const sha256$1 = /* @__PURE__ */ createHasher(() => new SHA256());
1439
+ const sha256$2 = /* @__PURE__ */ createHasher$1(() => new SHA256());
1440
1440
 
1441
1441
  /**
1442
1442
  * SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
@@ -1449,7 +1449,7 @@ const sha256$1 = /* @__PURE__ */ createHasher(() => new SHA256());
1449
1449
  * @deprecated
1450
1450
  */
1451
1451
  /** @deprecated Use import from `noble/hashes/sha2` module */
1452
- const sha256 = sha256$1;
1452
+ const sha256$1 = sha256$2;
1453
1453
 
1454
1454
  function bs58checkBase (checksumFn) {
1455
1455
  // Encode a buffer as a base58-check encoded string
@@ -1497,7 +1497,7 @@ function bs58checkBase (checksumFn) {
1497
1497
 
1498
1498
  // SHA256(SHA256(buffer))
1499
1499
  function sha256x2(buffer) {
1500
- return sha256(sha256(buffer));
1500
+ return sha256$1(sha256$1(buffer));
1501
1501
  }
1502
1502
  var bs58check = bs58checkBase(sha256x2);
1503
1503
 
@@ -2539,6 +2539,89 @@ function buildCancelScriptSigPQHex(sigPQ, pubKey) {
2539
2539
  return bytesToHex(buildCancelScriptSigPQ(sigPQ, pubKey));
2540
2540
  }
2541
2541
 
2542
+ /**
2543
+ * Witness-stack builders for the Partial-Fill Sell Order covenant.
2544
+ *
2545
+ * When the covenant lives behind an AuthScript commitment (the only
2546
+ * asset-compatible deployment since the node's OP_XNA_ASSET placement rules
2547
+ * — bare covenant outputs can no longer carry assets), the unlock data goes
2548
+ * in the witness, not the scriptSig. The scriptSig builders in `spend.ts`
2549
+ * return a serialized script that PUSHES the elements; a witness needs the
2550
+ * elements THEMSELVES, one per stack slot, so those blobs cannot be reused
2551
+ * as a single `args` entry.
2552
+ *
2553
+ * These builders return the raw `args` stack (bottom → top), mirroring the
2554
+ * shapes documented in `spend.ts`:
2555
+ *
2556
+ * Full fill: [ <1>, <0> ]
2557
+ * Partial fill: [ <N>, <0>, <0> ]
2558
+ * Cancel: [ <sig>, <pubkey>, <1> ]
2559
+ *
2560
+ * Numbers are minimal CScriptNum stack values (0 = empty element). Wrap the
2561
+ * result with `buildAuthScriptWitnessNoAuth({ args, witnessScript: covenant })`
2562
+ * (from `standard/authscript.ts`) to get the final `[0x00, ...args, covenant]`
2563
+ * witness, and serialize the spending transaction with
2564
+ * `serializeTransaction` from `@neuraiproject/neurai-create-transaction`
2565
+ * (0.5.1+, witness elements as hex strings: `witness.map(bytesToHex)`).
2566
+ */
2567
+ const MAX_PQ_SCRIPT_ELEMENT_SIZE = 3072;
2568
+ /**
2569
+ * Witness `args` for the public fill branches. Same semantics and
2570
+ * validations as `buildFillScriptSig`.
2571
+ */
2572
+ function buildFillWitnessStack(amount, total) {
2573
+ if (typeof amount !== 'bigint' || typeof total !== 'bigint') {
2574
+ throw new Error('amount and total must be bigint');
2575
+ }
2576
+ if (amount <= 0n) {
2577
+ throw new Error('fill amount must be > 0');
2578
+ }
2579
+ if (total <= 0n) {
2580
+ throw new Error('total must be > 0');
2581
+ }
2582
+ if (amount > total) {
2583
+ throw new Error('fill amount exceeds the covenant total');
2584
+ }
2585
+ if (amount === total) {
2586
+ // Full fill: covenant drains entirely; buyer does not push N.
2587
+ return [encodeScriptNum(1n), encodeScriptNum(0n)];
2588
+ }
2589
+ // Partial fill: N, then both flag values.
2590
+ return [encodeScriptNum(amount), encodeScriptNum(0n), encodeScriptNum(0n)];
2591
+ }
2592
+ /**
2593
+ * Witness `args` for the seller's ECDSA cancel branch. Same semantics and
2594
+ * validations as `buildCancelScriptSig`.
2595
+ */
2596
+ function buildCancelWitnessStack(signatureDer, pubKey) {
2597
+ if (!(signatureDer instanceof Uint8Array) || signatureDer.length === 0) {
2598
+ throw new Error('signatureDer must be a non-empty Uint8Array');
2599
+ }
2600
+ if (!(pubKey instanceof Uint8Array) || (pubKey.length !== 33 && pubKey.length !== 65)) {
2601
+ throw new Error('pubKey must be a compressed (33B) or uncompressed (65B) secp256k1 key');
2602
+ }
2603
+ return [signatureDer, pubKey, encodeScriptNum(1n)];
2604
+ }
2605
+ /**
2606
+ * Witness `args` for the seller's PQ (ML-DSA-44) cancel branch. Same
2607
+ * semantics and validations as `buildCancelScriptSigPQ`.
2608
+ */
2609
+ function buildCancelWitnessStackPQ(sigPQ, pubKey) {
2610
+ if (!(sigPQ instanceof Uint8Array) || sigPQ.length === 0) {
2611
+ throw new Error('sigPQ must be a non-empty Uint8Array');
2612
+ }
2613
+ if (sigPQ.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2614
+ throw new Error(`sigPQ of ${sigPQ.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2615
+ }
2616
+ if (!(pubKey instanceof Uint8Array) || pubKey.length === 0) {
2617
+ throw new Error('pubKey must be a non-empty Uint8Array');
2618
+ }
2619
+ if (pubKey.length > MAX_PQ_SCRIPT_ELEMENT_SIZE) {
2620
+ throw new Error(`pubKey of ${pubKey.length} bytes exceeds MAX_PQ_SCRIPT_ELEMENT_SIZE (${MAX_PQ_SCRIPT_ELEMENT_SIZE})`);
2621
+ }
2622
+ return [sigPQ, pubKey, encodeScriptNum(1n)];
2623
+ }
2624
+
2542
2625
  /**
2543
2626
  * Parser for the Partial-Fill Sell Order covenant (three-branch).
2544
2627
  *
@@ -2848,5 +2931,5 @@ function parsePartialFillScriptPQ(script, network = 'xna-test') {
2848
2931
  };
2849
2932
  }
2850
2933
 
2851
- export { AUTHSCRIPT_LEGACY, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_REF, DEFAULT_PQ_TXHASH_SELECTOR, MULTISIG_MAX_PUBKEYS, NULLDATA_STANDARD_MAX_SIZE, ScriptBuilder, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessPQ, buildAuthScriptWitnessRef, buildCancelScriptSig, buildCancelScriptSigHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, buildFillScriptSig, buildFillScriptSigHex, buildPartialFillScript, buildPartialFillScriptHex, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, bytesEqual, bytesToHex, concatBytes, encodeAuthScriptScriptPubKey, encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeNullDataScript, encodeP2PKHScriptPubKey, encodeP2SHScriptPubKey, encodeP2WPKHScriptPubKey, encodeP2WSHScriptPubKey, encodeScriptNum, encodeSellerScriptPubKey, ensureHex, hexToBytes, isPartialFillScriptPQ, opcodes, parsePartialFillScript, parsePartialFillScriptPQ, pushBytes, pushHex, pushInt, splitAssetWrappedScriptPubKey };
2934
+ export { AUTHSCRIPT_LEGACY, AUTHSCRIPT_NOAUTH, AUTHSCRIPT_PQ, AUTHSCRIPT_REF, DEFAULT_PQ_TXHASH_SELECTOR, MULTISIG_MAX_PUBKEYS, NULLDATA_STANDARD_MAX_SIZE, ScriptBuilder, buildAuthScriptWitnessLegacy, buildAuthScriptWitnessNoAuth, buildAuthScriptWitnessPQ, buildAuthScriptWitnessRef, buildCancelScriptSig, buildCancelScriptSigHex, buildCancelScriptSigPQ, buildCancelScriptSigPQHex, buildCancelWitnessStack, buildCancelWitnessStackPQ, buildFillScriptSig, buildFillScriptSigHex, buildFillWitnessStack, buildPartialFillScript, buildPartialFillScriptHex, buildPartialFillScriptPQ, buildPartialFillScriptPQHex, bytesEqual, bytesToHex, concatBytes, encodeAuthScriptScriptPubKey, encodeMultisigRedeemScript, encodeMultisigRedeemScriptHex, encodeNullDataScript, encodeP2PKHScriptPubKey, encodeP2SHScriptPubKey, encodeP2WPKHScriptPubKey, encodeP2WSHScriptPubKey, encodeScriptNum, encodeSellerScriptPubKey, ensureHex, hexToBytes, isPartialFillScriptPQ, opcodes, parsePartialFillScript, parsePartialFillScriptPQ, pushBytes, pushHex, pushInt, splitAssetWrappedScriptPubKey };
2852
2935
  //# sourceMappingURL=browser.js.map