@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/dist/index.js CHANGED
@@ -41,6 +41,18 @@ function bytesEqual(a, b) {
41
41
  return true;
42
42
  }
43
43
 
44
+ /** Exact monetary primitives. This module has no runtime dependencies. */
45
+ /** Normalize a raw integer without preserving an already rounded number. */
46
+ function toRawInteger(value, label = 'amount') {
47
+ if (typeof value === 'bigint')
48
+ return value;
49
+ if (typeof value === 'number' && Number.isSafeInteger(value))
50
+ return BigInt(value);
51
+ if (typeof value === 'string' && /^-?\d+$/.test(value) && value.length <= 100)
52
+ return BigInt(value);
53
+ throw new Error(`${label}: expected an exact integer; use bigint or an integer string for large values`);
54
+ }
55
+
44
56
  /**
45
57
  * Opcode constants for Neurai Script.
46
58
  *
@@ -412,12 +424,6 @@ var opcodes = /*#__PURE__*/Object.freeze({
412
424
  TXHASH_VERSION: TXHASH_VERSION
413
425
  });
414
426
 
415
- /**
416
- * Low-level Script assembler. Emits the exact byte layout expected by the
417
- * Neurai interpreter: pushdata prefixes follow the same rules as Bitcoin
418
- * (direct push for 1..75 bytes, OP_PUSHDATA1/2/4 otherwise), and integers
419
- * are minimally-encoded as CScriptNum.
420
- */
421
427
  /**
422
428
  * Minimal CScriptNum encoding (Bitcoin consensus rules).
423
429
  *
@@ -427,7 +433,7 @@ var opcodes = /*#__PURE__*/Object.freeze({
427
433
  * - otherwise: sign-magnitude little-endian, with a sign bit on the last byte
428
434
  */
429
435
  function encodeScriptNum(value) {
430
- let n = typeof value === 'bigint' ? value : BigInt(value);
436
+ let n = toRawInteger(value, 'ScriptNum');
431
437
  if (n === 0n)
432
438
  return new Uint8Array();
433
439
  const negative = n < 0n;
@@ -481,7 +487,7 @@ function pushBytes(data) {
481
487
  * when available to match how the node's own templates look on the wire.
482
488
  */
483
489
  function pushInt(value) {
484
- const n = typeof value === 'bigint' ? value : BigInt(value);
490
+ const n = toRawInteger(value, 'ScriptNum');
485
491
  if (n === -1n)
486
492
  return Uint8Array.of(OP_1NEGATE);
487
493
  if (n === 0n)
@@ -798,6 +804,8 @@ function encodeP2SHScriptPubKey(redeemScriptHash160) {
798
804
  return concatBytes(Uint8Array.of(OP_HASH160, 0x14), redeemScriptHash160, Uint8Array.of(OP_EQUAL));
799
805
  }
800
806
 
807
+ /** Exact monetary primitives. This module has no runtime dependencies. */
808
+
801
809
  // base-x encoding / decoding
802
810
  // Copyright (c) 2018 base-x contributors
803
811
  // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)