@neuraiproject/neurai-scripts 0.8.1 → 0.8.2

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 CHANGED
@@ -616,3 +616,11 @@ consumable; callers import directly from the package root.
616
616
  ## License
617
617
 
618
618
  MIT — same terms as the rest of the `@neuraiproject/*` libraries.
619
+
620
+ ### Large numeric script values
621
+
622
+ Use bigint for ScriptNum values above Number.MAX_SAFE_INTEGER. Numeric inputs
623
+ must be safe integers; unsafe numbers are rejected before encoding. Covenant
624
+ amounts and prices remain bigint. The script interpreter's own size and
625
+ arithmetic limits still apply; encoding an integer does not guarantee a script
626
+ will pass consensus checks.
@@ -44,6 +44,18 @@ var NeuraiScriptsBundle = (function (exports) {
44
44
  return true;
45
45
  }
46
46
 
47
+ /** Exact monetary primitives. This module has no runtime dependencies. */
48
+ /** Normalize a raw integer without preserving an already rounded number. */
49
+ function toRawInteger(value, label = 'amount') {
50
+ if (typeof value === 'bigint')
51
+ return value;
52
+ if (typeof value === 'number' && Number.isSafeInteger(value))
53
+ return BigInt(value);
54
+ if (typeof value === 'string' && /^-?\d+$/.test(value) && value.length <= 100)
55
+ return BigInt(value);
56
+ throw new Error(`${label}: expected an exact integer; use bigint or an integer string for large values`);
57
+ }
58
+
47
59
  /**
48
60
  * Opcode constants for Neurai Script.
49
61
  *
@@ -415,12 +427,6 @@ var NeuraiScriptsBundle = (function (exports) {
415
427
  TXHASH_VERSION: TXHASH_VERSION
416
428
  });
417
429
 
418
- /**
419
- * Low-level Script assembler. Emits the exact byte layout expected by the
420
- * Neurai interpreter: pushdata prefixes follow the same rules as Bitcoin
421
- * (direct push for 1..75 bytes, OP_PUSHDATA1/2/4 otherwise), and integers
422
- * are minimally-encoded as CScriptNum.
423
- */
424
430
  /**
425
431
  * Minimal CScriptNum encoding (Bitcoin consensus rules).
426
432
  *
@@ -430,7 +436,7 @@ var NeuraiScriptsBundle = (function (exports) {
430
436
  * - otherwise: sign-magnitude little-endian, with a sign bit on the last byte
431
437
  */
432
438
  function encodeScriptNum(value) {
433
- let n = typeof value === 'bigint' ? value : BigInt(value);
439
+ let n = toRawInteger(value, 'ScriptNum');
434
440
  if (n === 0n)
435
441
  return new Uint8Array();
436
442
  const negative = n < 0n;
@@ -484,7 +490,7 @@ var NeuraiScriptsBundle = (function (exports) {
484
490
  * when available to match how the node's own templates look on the wire.
485
491
  */
486
492
  function pushInt(value) {
487
- const n = typeof value === 'bigint' ? value : BigInt(value);
493
+ const n = toRawInteger(value, 'ScriptNum');
488
494
  if (n === -1n)
489
495
  return Uint8Array.of(OP_1NEGATE);
490
496
  if (n === 0n)
@@ -801,6 +807,8 @@ var NeuraiScriptsBundle = (function (exports) {
801
807
  return concatBytes(Uint8Array.of(OP_HASH160, 0x14), redeemScriptHash160, Uint8Array.of(OP_EQUAL));
802
808
  }
803
809
 
810
+ /** Exact monetary primitives. This module has no runtime dependencies. */
811
+
804
812
  // base-x encoding / decoding
805
813
  // Copyright (c) 2018 base-x contributors
806
814
  // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)