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