@ethereumjs/util
Advanced tools
Comparing version 9.0.0 to 9.0.1
@@ -11,3 +11,2 @@ "use strict"; | ||
const internal_js_1 = require("./internal.js"); | ||
const _0n = BigInt(0); | ||
class Account { | ||
@@ -18,3 +17,3 @@ /** | ||
*/ | ||
constructor(nonce = _0n, balance = _0n, storageRoot = constants_js_1.KECCAK256_RLP, codeHash = constants_js_1.KECCAK256_NULL) { | ||
constructor(nonce = constants_js_1.BIGINT_0, balance = constants_js_1.BIGINT_0, storageRoot = constants_js_1.KECCAK256_RLP, codeHash = constants_js_1.KECCAK256_NULL) { | ||
this.nonce = nonce; | ||
@@ -42,6 +41,6 @@ this.balance = balance; | ||
_validate() { | ||
if (this.nonce < _0n) { | ||
if (this.nonce < constants_js_1.BIGINT_0) { | ||
throw new Error('nonce must be greater than zero'); | ||
} | ||
if (this.balance < _0n) { | ||
if (this.balance < constants_js_1.BIGINT_0) { | ||
throw new Error('balance must be greater than zero'); | ||
@@ -85,3 +84,5 @@ } | ||
isEmpty() { | ||
return this.balance === _0n && this.nonce === _0n && (0, bytes_js_1.equalsBytes)(this.codeHash, constants_js_1.KECCAK256_NULL); | ||
return (this.balance === constants_js_1.BIGINT_0 && | ||
this.nonce === constants_js_1.BIGINT_0 && | ||
(0, bytes_js_1.equalsBytes)(this.codeHash, constants_js_1.KECCAK256_NULL)); | ||
} | ||
@@ -154,3 +155,3 @@ } | ||
(0, helpers_js_1.assertIsBytes)(nonce); | ||
if ((0, bytes_js_1.bytesToBigInt)(nonce) === BigInt(0)) { | ||
if ((0, bytes_js_1.bytesToBigInt)(nonce) === constants_js_1.BIGINT_0) { | ||
// in RLP we want to encode null in the case of zero nonce | ||
@@ -157,0 +158,0 @@ // read the RLP documentation for an answer if you dare |
@@ -6,2 +6,3 @@ "use strict"; | ||
const bytes_js_1 = require("./bytes.js"); | ||
const constants_js_1 = require("./constants.js"); | ||
/** | ||
@@ -99,3 +100,3 @@ * Handling and generating Ethereum addresses | ||
const address = (0, bytes_js_1.bytesToBigInt)(this.bytes); | ||
const rangeMin = BigInt(0); | ||
const rangeMin = constants_js_1.BIGINT_0; | ||
const rangeMax = BigInt('0xffff'); | ||
@@ -102,0 +103,0 @@ return address >= rangeMin && address <= rangeMax; |
@@ -186,3 +186,33 @@ import { bytesToHex as _bytesToUnprefixedHex } from 'ethereum-cryptography/utils.js'; | ||
export declare const concatBytes: (...arrays: Uint8Array[]) => Uint8Array; | ||
/** | ||
* @notice Convert a Uint8Array to a 32-bit integer | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 32-bit integer. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 4. | ||
* @return {number} The 32-bit integer read from the input Uint8Array. | ||
*/ | ||
export declare function bytesToInt32(bytes: Uint8Array, littleEndian?: boolean): number; | ||
/** | ||
* @notice Convert a Uint8Array to a 64-bit bigint | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 64-bit bigint. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 8. | ||
* @return {bigint} The 64-bit bigint read from the input Uint8Array. | ||
*/ | ||
export declare function bytesToBigInt64(bytes: Uint8Array, littleEndian?: boolean): bigint; | ||
/** | ||
* @notice Convert a 32-bit integer to a Uint8Array. | ||
* @param {number} value The 32-bit integer to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 4 containing the integer. | ||
*/ | ||
export declare function int32ToBytes(value: number, littleEndian?: boolean): Uint8Array; | ||
/** | ||
* @notice Convert a 64-bit bigint to a Uint8Array. | ||
* @param {bigint} value The 64-bit bigint to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 8 containing the bigint. | ||
*/ | ||
export declare function bigInt64ToBytes(value: bigint, littleEndian?: boolean): Uint8Array; | ||
export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/utils.js'; | ||
//# sourceMappingURL=bytes.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.utf8ToBytes = exports.equalsBytes = exports.bytesToUtf8 = exports.concatBytes = exports.randomBytes = exports.compareBytes = exports.intToUnpaddedBytes = exports.bigIntToUnpaddedBytes = exports.bigIntToHex = exports.validateNoLeadingZeroes = exports.short = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.toBytes = exports.unpadHex = exports.unpadArray = exports.unpadBytes = exports.setLengthRight = exports.setLengthLeft = exports.zeros = exports.bigIntToBytes = exports.intToBytes = exports.intToHex = exports.hexToBytes = exports.bytesToInt = exports.bytesToBigInt = exports.bytesToHex = exports.unprefixedHexToBytes = exports.bytesToUnprefixedHex = void 0; | ||
exports.utf8ToBytes = exports.equalsBytes = exports.bytesToUtf8 = exports.bigInt64ToBytes = exports.int32ToBytes = exports.bytesToBigInt64 = exports.bytesToInt32 = exports.concatBytes = exports.randomBytes = exports.compareBytes = exports.intToUnpaddedBytes = exports.bigIntToUnpaddedBytes = exports.bigIntToHex = exports.validateNoLeadingZeroes = exports.short = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.toBytes = exports.unpadHex = exports.unpadArray = exports.unpadBytes = exports.setLengthRight = exports.setLengthLeft = exports.zeros = exports.bigIntToBytes = exports.intToBytes = exports.intToHex = exports.hexToBytes = exports.bytesToInt = exports.bytesToBigInt = exports.bytesToHex = exports.unprefixedHexToBytes = exports.bytesToUnprefixedHex = void 0; | ||
const random_js_1 = require("ethereum-cryptography/random.js"); | ||
@@ -9,2 +9,3 @@ // eslint-disable-next-line no-restricted-imports | ||
const internal_js_1 = require("./internal.js"); | ||
const BIGINT_0 = BigInt(0); | ||
/** | ||
@@ -39,2 +40,7 @@ * @deprecated | ||
exports.bytesToHex = bytesToHex; | ||
// BigInt cache for the numbers 0 - 256*256-1 (two-byte bytes) | ||
const BIGINT_CACHE = []; | ||
for (let i = 0; i <= 256 * 256 - 1; i++) { | ||
BIGINT_CACHE[i] = BigInt(i); | ||
} | ||
/** | ||
@@ -48,4 +54,11 @@ * Converts a {@link Uint8Array} to a {@link bigint} | ||
if (hex === '0x') { | ||
return BigInt(0); | ||
return BIGINT_0; | ||
} | ||
if (hex.length === 4) { | ||
// If the byte length is 1 (this is faster than checking `bytes.length === 1`) | ||
return BIGINT_CACHE[bytes[0]]; | ||
} | ||
if (hex.length === 6) { | ||
return BIGINT_CACHE[bytes[0] * 256 + bytes[1]]; | ||
} | ||
return BigInt(hex); | ||
@@ -243,3 +256,3 @@ }; | ||
if (typeof v === 'bigint') { | ||
if (v < BigInt(0)) { | ||
if (v < BIGINT_0) { | ||
throw new Error(`Cannot convert negative bigint to Uint8Array. Given: ${v}`); | ||
@@ -405,2 +418,58 @@ } | ||
exports.concatBytes = concatBytes; | ||
/** | ||
* @notice Convert a Uint8Array to a 32-bit integer | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 32-bit integer. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 4. | ||
* @return {number} The 32-bit integer read from the input Uint8Array. | ||
*/ | ||
function bytesToInt32(bytes, littleEndian = false) { | ||
if (bytes.length < 4) { | ||
throw new Error('The input Uint8Array must have at least 4 bytes.'); | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
return dataView.getInt32(0, littleEndian); | ||
} | ||
exports.bytesToInt32 = bytesToInt32; | ||
/** | ||
* @notice Convert a Uint8Array to a 64-bit bigint | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 64-bit bigint. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 8. | ||
* @return {bigint} The 64-bit bigint read from the input Uint8Array. | ||
*/ | ||
function bytesToBigInt64(bytes, littleEndian = false) { | ||
if (bytes.length < 8) { | ||
throw new Error('The input Uint8Array must have at least 8 bytes.'); | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
return dataView.getBigInt64(0, littleEndian); | ||
} | ||
exports.bytesToBigInt64 = bytesToBigInt64; | ||
/** | ||
* @notice Convert a 32-bit integer to a Uint8Array. | ||
* @param {number} value The 32-bit integer to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 4 containing the integer. | ||
*/ | ||
function int32ToBytes(value, littleEndian = false) { | ||
const buffer = new ArrayBuffer(4); | ||
const dataView = new DataView(buffer); | ||
dataView.setInt32(0, value, littleEndian); | ||
return new Uint8Array(buffer); | ||
} | ||
exports.int32ToBytes = int32ToBytes; | ||
/** | ||
* @notice Convert a 64-bit bigint to a Uint8Array. | ||
* @param {bigint} value The 64-bit bigint to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 8 containing the bigint. | ||
*/ | ||
function bigInt64ToBytes(value, littleEndian = false) { | ||
const buffer = new ArrayBuffer(8); | ||
const dataView = new DataView(buffer); | ||
dataView.setBigInt64(0, value, littleEndian); | ||
return new Uint8Array(buffer); | ||
} | ||
exports.bigInt64ToBytes = bigInt64ToBytes; | ||
// eslint-disable-next-line no-restricted-imports | ||
@@ -407,0 +476,0 @@ var utils_js_2 = require("ethereum-cryptography/utils.js"); |
@@ -51,2 +51,28 @@ /** | ||
export declare const RIPEMD160_ADDRESS_STRING = "0000000000000000000000000000000000000003"; | ||
/** | ||
* BigInt constants | ||
*/ | ||
export declare const BIGINT_NEG1: bigint; | ||
export declare const BIGINT_0: bigint; | ||
export declare const BIGINT_1: bigint; | ||
export declare const BIGINT_2: bigint; | ||
export declare const BIGINT_3: bigint; | ||
export declare const BIGINT_7: bigint; | ||
export declare const BIGINT_8: bigint; | ||
export declare const BIGINT_27: bigint; | ||
export declare const BIGINT_28: bigint; | ||
export declare const BIGINT_31: bigint; | ||
export declare const BIGINT_32: bigint; | ||
export declare const BIGINT_64: bigint; | ||
export declare const BIGINT_128: bigint; | ||
export declare const BIGINT_255: bigint; | ||
export declare const BIGINT_256: bigint; | ||
export declare const BIGINT_96: bigint; | ||
export declare const BIGINT_100: bigint; | ||
export declare const BIGINT_160: bigint; | ||
export declare const BIGINT_224: bigint; | ||
export declare const BIGINT_2EXP96: bigint; | ||
export declare const BIGINT_2EXP160: bigint; | ||
export declare const BIGINT_2EXP224: bigint; | ||
export declare const BIGINT_2EXP256: bigint; | ||
//# sourceMappingURL=constants.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RIPEMD160_ADDRESS_STRING = exports.MAX_WITHDRAWALS_PER_PAYLOAD = exports.RLP_EMPTY_STRING = exports.KECCAK256_RLP = exports.KECCAK256_RLP_S = exports.KECCAK256_RLP_ARRAY = exports.KECCAK256_RLP_ARRAY_S = exports.KECCAK256_NULL = exports.KECCAK256_NULL_S = exports.TWO_POW256 = exports.SECP256K1_ORDER_DIV_2 = exports.SECP256K1_ORDER = exports.MAX_INTEGER_BIGINT = exports.MAX_INTEGER = exports.MAX_UINT64 = void 0; | ||
exports.BIGINT_2EXP256 = exports.BIGINT_2EXP224 = exports.BIGINT_2EXP160 = exports.BIGINT_2EXP96 = exports.BIGINT_224 = exports.BIGINT_160 = exports.BIGINT_100 = exports.BIGINT_96 = exports.BIGINT_256 = exports.BIGINT_255 = exports.BIGINT_128 = exports.BIGINT_64 = exports.BIGINT_32 = exports.BIGINT_31 = exports.BIGINT_28 = exports.BIGINT_27 = exports.BIGINT_8 = exports.BIGINT_7 = exports.BIGINT_3 = exports.BIGINT_2 = exports.BIGINT_1 = exports.BIGINT_0 = exports.BIGINT_NEG1 = exports.RIPEMD160_ADDRESS_STRING = exports.MAX_WITHDRAWALS_PER_PAYLOAD = exports.RLP_EMPTY_STRING = exports.KECCAK256_RLP = exports.KECCAK256_RLP_S = exports.KECCAK256_RLP_ARRAY = exports.KECCAK256_RLP_ARRAY_S = exports.KECCAK256_NULL = exports.KECCAK256_NULL_S = exports.TWO_POW256 = exports.SECP256K1_ORDER_DIV_2 = exports.SECP256K1_ORDER = exports.MAX_INTEGER_BIGINT = exports.MAX_INTEGER = exports.MAX_UINT64 = void 0; | ||
const secp256k1_js_1 = require("ethereum-cryptography/secp256k1.js"); | ||
@@ -56,2 +56,28 @@ const bytes_js_1 = require("./bytes.js"); | ||
exports.RIPEMD160_ADDRESS_STRING = '0000000000000000000000000000000000000003'; | ||
/** | ||
* BigInt constants | ||
*/ | ||
exports.BIGINT_NEG1 = BigInt(-1); | ||
exports.BIGINT_0 = BigInt(0); | ||
exports.BIGINT_1 = BigInt(1); | ||
exports.BIGINT_2 = BigInt(2); | ||
exports.BIGINT_3 = BigInt(3); | ||
exports.BIGINT_7 = BigInt(7); | ||
exports.BIGINT_8 = BigInt(8); | ||
exports.BIGINT_27 = BigInt(27); | ||
exports.BIGINT_28 = BigInt(28); | ||
exports.BIGINT_31 = BigInt(31); | ||
exports.BIGINT_32 = BigInt(32); | ||
exports.BIGINT_64 = BigInt(64); | ||
exports.BIGINT_128 = BigInt(128); | ||
exports.BIGINT_255 = BigInt(255); | ||
exports.BIGINT_256 = BigInt(256); | ||
exports.BIGINT_96 = BigInt(96); | ||
exports.BIGINT_100 = BigInt(100); | ||
exports.BIGINT_160 = BigInt(160); | ||
exports.BIGINT_224 = BigInt(224); | ||
exports.BIGINT_2EXP96 = BigInt(79228162514264337593543950336); | ||
exports.BIGINT_2EXP160 = BigInt(1461501637330902918203684832716283019655932542976); | ||
exports.BIGINT_2EXP224 = BigInt(26959946667150639794667015087019630673637144422540572481103610249216); | ||
exports.BIGINT_2EXP256 = exports.BIGINT_2 ** exports.BIGINT_256; | ||
//# sourceMappingURL=constants.js.map |
@@ -14,3 +14,4 @@ "use strict"; | ||
let { balance, code, storage, nonce } = json.alloc[address]; | ||
address = (0, bytes_js_1.addHexPrefix)(address); | ||
// create a map with lowercase for easy lookups | ||
address = (0, bytes_js_1.addHexPrefix)(address.toLowerCase()); | ||
balance = (0, internal_js_1.isHexPrefixed)(balance) ? balance : (0, bytes_js_1.bigIntToHex)(BigInt(balance)); | ||
@@ -17,0 +18,0 @@ code = code !== undefined ? (0, bytes_js_1.addHexPrefix)(code) : undefined; |
@@ -43,3 +43,4 @@ /** | ||
/** | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string. | ||
* Strips leading and trailing 0's. | ||
* | ||
@@ -46,0 +47,0 @@ * @param string |
@@ -117,3 +117,4 @@ "use strict"; | ||
/** | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string. | ||
* Strips leading and trailing 0's. | ||
* | ||
@@ -126,3 +127,3 @@ * @param string | ||
const str = (0, bytes_js_1.utf8ToBytes)(stringValue); | ||
return `0x${padToEven((0, bytes_js_1.bytesToHex)(str)).replace(/^0+|0+$/g, '')}`; | ||
return `0x${padToEven((0, bytes_js_1.bytesToUnprefixedHex)(str)).replace(/^0+|0+$/g, '')}`; | ||
} | ||
@@ -129,0 +130,0 @@ exports.fromUtf8 = fromUtf8; |
@@ -22,3 +22,3 @@ "use strict"; | ||
? BigInt(sig.recovery + 27) | ||
: BigInt(sig.recovery + 35) + BigInt(chainId) * BigInt(2); | ||
: BigInt(sig.recovery + 35) + BigInt(chainId) * constants_js_1.BIGINT_2; | ||
return { r, s, v }; | ||
@@ -28,11 +28,11 @@ } | ||
function calculateSigRecovery(v, chainId) { | ||
if (v === BigInt(0) || v === BigInt(1)) | ||
if (v === constants_js_1.BIGINT_0 || v === constants_js_1.BIGINT_1) | ||
return v; | ||
if (chainId === undefined) { | ||
return v - BigInt(27); | ||
return v - constants_js_1.BIGINT_27; | ||
} | ||
return v - (chainId * BigInt(2) + BigInt(35)); | ||
return v - (chainId * constants_js_1.BIGINT_2 + BigInt(35)); | ||
} | ||
function isValidSigRecovery(recovery) { | ||
return recovery === BigInt(0) || recovery === BigInt(1); | ||
return recovery === constants_js_1.BIGINT_0 || recovery === constants_js_1.BIGINT_1; | ||
} | ||
@@ -80,3 +80,3 @@ /** | ||
const ss = Uint8Array.from([...s]); | ||
if ((v > BigInt(28) && v % BigInt(2) === BigInt(1)) || v === BigInt(1) || v === BigInt(28)) { | ||
if ((v > BigInt(28) && v % constants_js_1.BIGINT_2 === constants_js_1.BIGINT_1) || v === constants_js_1.BIGINT_1 || v === BigInt(28)) { | ||
ss[0] |= 0x80; | ||
@@ -117,3 +117,3 @@ } | ||
if (v < 27) { | ||
v = v + BigInt(27); | ||
v = v + constants_js_1.BIGINT_27; | ||
} | ||
@@ -141,5 +141,5 @@ return { | ||
const sBigInt = (0, bytes_js_1.bytesToBigInt)(s); | ||
if (rBigInt === BigInt(0) || | ||
if (rBigInt === constants_js_1.BIGINT_0 || | ||
rBigInt >= constants_js_1.SECP256K1_ORDER || | ||
sBigInt === BigInt(0) || | ||
sBigInt === constants_js_1.BIGINT_0 || | ||
sBigInt >= constants_js_1.SECP256K1_ORDER) { | ||
@@ -146,0 +146,0 @@ return false; |
/** Easy conversion from Gwei to wei */ | ||
export declare const GWEI_TO_WEI: bigint; | ||
export declare function formatBigDecimal(numerator: bigint, denominator: bigint, maxDecimalFactor: bigint): string; | ||
//# sourceMappingURL=units.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GWEI_TO_WEI = void 0; | ||
exports.formatBigDecimal = exports.GWEI_TO_WEI = void 0; | ||
const constants_js_1 = require("./constants.js"); | ||
/** Easy conversion from Gwei to wei */ | ||
exports.GWEI_TO_WEI = BigInt(1000000000); | ||
function formatBigDecimal(numerator, denominator, maxDecimalFactor) { | ||
if (denominator === constants_js_1.BIGINT_0) { | ||
denominator = constants_js_1.BIGINT_1; | ||
} | ||
const full = numerator / denominator; | ||
const fraction = ((numerator - full * denominator) * maxDecimalFactor) / denominator; | ||
// zeros to be added post decimal are number of zeros in maxDecimalFactor - number of digits in fraction | ||
const zerosPostDecimal = String(maxDecimalFactor).length - 1 - String(fraction).length; | ||
return `${full}.${'0'.repeat(zerosPostDecimal)}${fraction}`; | ||
} | ||
exports.formatBigDecimal = formatBigDecimal; | ||
//# sourceMappingURL=units.js.map |
@@ -6,2 +6,3 @@ "use strict"; | ||
const bytes_js_1 = require("./bytes.js"); | ||
const constants_js_1 = require("./constants.js"); | ||
const types_js_1 = require("./types.js"); | ||
@@ -49,10 +50,10 @@ /** | ||
const { index, validatorIndex, address, amount } = withdrawal; | ||
const indexBytes = (0, types_js_1.toType)(index, types_js_1.TypeOutput.BigInt) === BigInt(0) | ||
const indexBytes = (0, types_js_1.toType)(index, types_js_1.TypeOutput.BigInt) === constants_js_1.BIGINT_0 | ||
? new Uint8Array() | ||
: (0, types_js_1.toType)(index, types_js_1.TypeOutput.Uint8Array); | ||
const validatorIndexBytes = (0, types_js_1.toType)(validatorIndex, types_js_1.TypeOutput.BigInt) === BigInt(0) | ||
const validatorIndexBytes = (0, types_js_1.toType)(validatorIndex, types_js_1.TypeOutput.BigInt) === constants_js_1.BIGINT_0 | ||
? new Uint8Array() | ||
: (0, types_js_1.toType)(validatorIndex, types_js_1.TypeOutput.Uint8Array); | ||
const addressBytes = address instanceof address_js_1.Address ? address.bytes : (0, types_js_1.toType)(address, types_js_1.TypeOutput.Uint8Array); | ||
const amountBytes = (0, types_js_1.toType)(amount, types_js_1.TypeOutput.BigInt) === BigInt(0) | ||
const amountBytes = (0, types_js_1.toType)(amount, types_js_1.TypeOutput.BigInt) === constants_js_1.BIGINT_0 | ||
? new Uint8Array() | ||
@@ -59,0 +60,0 @@ : (0, types_js_1.toType)(amount, types_js_1.TypeOutput.Uint8Array); |
@@ -5,6 +5,5 @@ import { RLP } from '@ethereumjs/rlp'; | ||
import { bigIntToUnpaddedBytes, bytesToBigInt, bytesToHex, concatBytes, equalsBytes, hexToBytes, toBytes, utf8ToBytes, zeros, } from './bytes.js'; | ||
import { KECCAK256_NULL, KECCAK256_RLP } from './constants.js'; | ||
import { BIGINT_0, KECCAK256_NULL, KECCAK256_RLP } from './constants.js'; | ||
import { assertIsBytes, assertIsHexString, assertIsString } from './helpers.js'; | ||
import { stripHexPrefix } from './internal.js'; | ||
const _0n = BigInt(0); | ||
export class Account { | ||
@@ -15,3 +14,3 @@ /** | ||
*/ | ||
constructor(nonce = _0n, balance = _0n, storageRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) { | ||
constructor(nonce = BIGINT_0, balance = BIGINT_0, storageRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) { | ||
this.nonce = nonce; | ||
@@ -39,6 +38,6 @@ this.balance = balance; | ||
_validate() { | ||
if (this.nonce < _0n) { | ||
if (this.nonce < BIGINT_0) { | ||
throw new Error('nonce must be greater than zero'); | ||
} | ||
if (this.balance < _0n) { | ||
if (this.balance < BIGINT_0) { | ||
throw new Error('balance must be greater than zero'); | ||
@@ -82,3 +81,5 @@ } | ||
isEmpty() { | ||
return this.balance === _0n && this.nonce === _0n && equalsBytes(this.codeHash, KECCAK256_NULL); | ||
return (this.balance === BIGINT_0 && | ||
this.nonce === BIGINT_0 && | ||
equalsBytes(this.codeHash, KECCAK256_NULL)); | ||
} | ||
@@ -147,3 +148,3 @@ } | ||
assertIsBytes(nonce); | ||
if (bytesToBigInt(nonce) === BigInt(0)) { | ||
if (bytesToBigInt(nonce) === BIGINT_0) { | ||
// in RLP we want to encode null in the case of zero nonce | ||
@@ -150,0 +151,0 @@ // read the RLP documentation for an answer if you dare |
import { generateAddress, generateAddress2, isValidAddress, privateToAddress, pubToAddress, } from './account.js'; | ||
import { bigIntToBytes, bytesToBigInt, bytesToHex, equalsBytes, toBytes, zeros } from './bytes.js'; | ||
import { BIGINT_0 } from './constants.js'; | ||
/** | ||
@@ -95,3 +96,3 @@ * Handling and generating Ethereum addresses | ||
const address = bytesToBigInt(this.bytes); | ||
const rangeMin = BigInt(0); | ||
const rangeMin = BIGINT_0; | ||
const rangeMax = BigInt('0xffff'); | ||
@@ -98,0 +99,0 @@ return address >= rangeMin && address <= rangeMax; |
@@ -186,3 +186,33 @@ import { bytesToHex as _bytesToUnprefixedHex } from 'ethereum-cryptography/utils.js'; | ||
export declare const concatBytes: (...arrays: Uint8Array[]) => Uint8Array; | ||
/** | ||
* @notice Convert a Uint8Array to a 32-bit integer | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 32-bit integer. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 4. | ||
* @return {number} The 32-bit integer read from the input Uint8Array. | ||
*/ | ||
export declare function bytesToInt32(bytes: Uint8Array, littleEndian?: boolean): number; | ||
/** | ||
* @notice Convert a Uint8Array to a 64-bit bigint | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 64-bit bigint. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 8. | ||
* @return {bigint} The 64-bit bigint read from the input Uint8Array. | ||
*/ | ||
export declare function bytesToBigInt64(bytes: Uint8Array, littleEndian?: boolean): bigint; | ||
/** | ||
* @notice Convert a 32-bit integer to a Uint8Array. | ||
* @param {number} value The 32-bit integer to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 4 containing the integer. | ||
*/ | ||
export declare function int32ToBytes(value: number, littleEndian?: boolean): Uint8Array; | ||
/** | ||
* @notice Convert a 64-bit bigint to a Uint8Array. | ||
* @param {bigint} value The 64-bit bigint to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 8 containing the bigint. | ||
*/ | ||
export declare function bigInt64ToBytes(value: bigint, littleEndian?: boolean): Uint8Array; | ||
export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/utils.js'; | ||
//# sourceMappingURL=bytes.d.ts.map |
@@ -6,2 +6,3 @@ import { getRandomBytesSync } from 'ethereum-cryptography/random.js'; | ||
import { isHexPrefixed, isHexString, padToEven, stripHexPrefix } from './internal.js'; | ||
const BIGINT_0 = BigInt(0); | ||
/** | ||
@@ -34,2 +35,7 @@ * @deprecated | ||
}; | ||
// BigInt cache for the numbers 0 - 256*256-1 (two-byte bytes) | ||
const BIGINT_CACHE = []; | ||
for (let i = 0; i <= 256 * 256 - 1; i++) { | ||
BIGINT_CACHE[i] = BigInt(i); | ||
} | ||
/** | ||
@@ -43,4 +49,11 @@ * Converts a {@link Uint8Array} to a {@link bigint} | ||
if (hex === '0x') { | ||
return BigInt(0); | ||
return BIGINT_0; | ||
} | ||
if (hex.length === 4) { | ||
// If the byte length is 1 (this is faster than checking `bytes.length === 1`) | ||
return BIGINT_CACHE[bytes[0]]; | ||
} | ||
if (hex.length === 6) { | ||
return BIGINT_CACHE[bytes[0] * 256 + bytes[1]]; | ||
} | ||
return BigInt(hex); | ||
@@ -226,3 +239,3 @@ }; | ||
if (typeof v === 'bigint') { | ||
if (v < BigInt(0)) { | ||
if (v < BIGINT_0) { | ||
throw new Error(`Cannot convert negative bigint to Uint8Array. Given: ${v}`); | ||
@@ -376,4 +389,56 @@ } | ||
}; | ||
/** | ||
* @notice Convert a Uint8Array to a 32-bit integer | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 32-bit integer. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 4. | ||
* @return {number} The 32-bit integer read from the input Uint8Array. | ||
*/ | ||
export function bytesToInt32(bytes, littleEndian = false) { | ||
if (bytes.length < 4) { | ||
throw new Error('The input Uint8Array must have at least 4 bytes.'); | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
return dataView.getInt32(0, littleEndian); | ||
} | ||
/** | ||
* @notice Convert a Uint8Array to a 64-bit bigint | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 64-bit bigint. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 8. | ||
* @return {bigint} The 64-bit bigint read from the input Uint8Array. | ||
*/ | ||
export function bytesToBigInt64(bytes, littleEndian = false) { | ||
if (bytes.length < 8) { | ||
throw new Error('The input Uint8Array must have at least 8 bytes.'); | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
return dataView.getBigInt64(0, littleEndian); | ||
} | ||
/** | ||
* @notice Convert a 32-bit integer to a Uint8Array. | ||
* @param {number} value The 32-bit integer to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 4 containing the integer. | ||
*/ | ||
export function int32ToBytes(value, littleEndian = false) { | ||
const buffer = new ArrayBuffer(4); | ||
const dataView = new DataView(buffer); | ||
dataView.setInt32(0, value, littleEndian); | ||
return new Uint8Array(buffer); | ||
} | ||
/** | ||
* @notice Convert a 64-bit bigint to a Uint8Array. | ||
* @param {bigint} value The 64-bit bigint to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 8 containing the bigint. | ||
*/ | ||
export function bigInt64ToBytes(value, littleEndian = false) { | ||
const buffer = new ArrayBuffer(8); | ||
const dataView = new DataView(buffer); | ||
dataView.setBigInt64(0, value, littleEndian); | ||
return new Uint8Array(buffer); | ||
} | ||
// eslint-disable-next-line no-restricted-imports | ||
export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/utils.js'; | ||
//# sourceMappingURL=bytes.js.map |
@@ -51,2 +51,28 @@ /** | ||
export declare const RIPEMD160_ADDRESS_STRING = "0000000000000000000000000000000000000003"; | ||
/** | ||
* BigInt constants | ||
*/ | ||
export declare const BIGINT_NEG1: bigint; | ||
export declare const BIGINT_0: bigint; | ||
export declare const BIGINT_1: bigint; | ||
export declare const BIGINT_2: bigint; | ||
export declare const BIGINT_3: bigint; | ||
export declare const BIGINT_7: bigint; | ||
export declare const BIGINT_8: bigint; | ||
export declare const BIGINT_27: bigint; | ||
export declare const BIGINT_28: bigint; | ||
export declare const BIGINT_31: bigint; | ||
export declare const BIGINT_32: bigint; | ||
export declare const BIGINT_64: bigint; | ||
export declare const BIGINT_128: bigint; | ||
export declare const BIGINT_255: bigint; | ||
export declare const BIGINT_256: bigint; | ||
export declare const BIGINT_96: bigint; | ||
export declare const BIGINT_100: bigint; | ||
export declare const BIGINT_160: bigint; | ||
export declare const BIGINT_224: bigint; | ||
export declare const BIGINT_2EXP96: bigint; | ||
export declare const BIGINT_2EXP160: bigint; | ||
export declare const BIGINT_2EXP224: bigint; | ||
export declare const BIGINT_2EXP256: bigint; | ||
//# sourceMappingURL=constants.d.ts.map |
@@ -53,2 +53,28 @@ import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'; | ||
export const RIPEMD160_ADDRESS_STRING = '0000000000000000000000000000000000000003'; | ||
/** | ||
* BigInt constants | ||
*/ | ||
export const BIGINT_NEG1 = BigInt(-1); | ||
export const BIGINT_0 = BigInt(0); | ||
export const BIGINT_1 = BigInt(1); | ||
export const BIGINT_2 = BigInt(2); | ||
export const BIGINT_3 = BigInt(3); | ||
export const BIGINT_7 = BigInt(7); | ||
export const BIGINT_8 = BigInt(8); | ||
export const BIGINT_27 = BigInt(27); | ||
export const BIGINT_28 = BigInt(28); | ||
export const BIGINT_31 = BigInt(31); | ||
export const BIGINT_32 = BigInt(32); | ||
export const BIGINT_64 = BigInt(64); | ||
export const BIGINT_128 = BigInt(128); | ||
export const BIGINT_255 = BigInt(255); | ||
export const BIGINT_256 = BigInt(256); | ||
export const BIGINT_96 = BigInt(96); | ||
export const BIGINT_100 = BigInt(100); | ||
export const BIGINT_160 = BigInt(160); | ||
export const BIGINT_224 = BigInt(224); | ||
export const BIGINT_2EXP96 = BigInt(79228162514264337593543950336); | ||
export const BIGINT_2EXP160 = BigInt(1461501637330902918203684832716283019655932542976); | ||
export const BIGINT_2EXP224 = BigInt(26959946667150639794667015087019630673637144422540572481103610249216); | ||
export const BIGINT_2EXP256 = BIGINT_2 ** BIGINT_256; | ||
//# sourceMappingURL=constants.js.map |
@@ -11,3 +11,4 @@ import { addHexPrefix, bigIntToHex } from './bytes.js'; | ||
let { balance, code, storage, nonce } = json.alloc[address]; | ||
address = addHexPrefix(address); | ||
// create a map with lowercase for easy lookups | ||
address = addHexPrefix(address.toLowerCase()); | ||
balance = isHexPrefixed(balance) ? balance : bigIntToHex(BigInt(balance)); | ||
@@ -14,0 +15,0 @@ code = code !== undefined ? addHexPrefix(code) : undefined; |
@@ -43,3 +43,4 @@ /** | ||
/** | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string. | ||
* Strips leading and trailing 0's. | ||
* | ||
@@ -46,0 +47,0 @@ * @param string |
@@ -24,3 +24,3 @@ /* | ||
*/ | ||
import { bytesToHex, utf8ToBytes } from './bytes.js'; | ||
import { bytesToUnprefixedHex, utf8ToBytes } from './bytes.js'; | ||
/** | ||
@@ -109,3 +109,4 @@ * Returns a `Boolean` on whether or not the a `String` starts with '0x' | ||
/** | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string. | ||
* Strips leading and trailing 0's. | ||
* | ||
@@ -118,3 +119,3 @@ * @param string | ||
const str = utf8ToBytes(stringValue); | ||
return `0x${padToEven(bytesToHex(str)).replace(/^0+|0+$/g, '')}`; | ||
return `0x${padToEven(bytesToUnprefixedHex(str)).replace(/^0+|0+$/g, '')}`; | ||
} | ||
@@ -121,0 +122,0 @@ /** |
import { keccak256 } from 'ethereum-cryptography/keccak.js'; | ||
import { secp256k1 } from 'ethereum-cryptography/secp256k1.js'; | ||
import { bytesToBigInt, bytesToHex, bytesToInt, concatBytes, setLengthLeft, toBytes, utf8ToBytes, } from './bytes.js'; | ||
import { SECP256K1_ORDER, SECP256K1_ORDER_DIV_2 } from './constants.js'; | ||
import { BIGINT_0, BIGINT_1, BIGINT_2, BIGINT_27, SECP256K1_ORDER, SECP256K1_ORDER_DIV_2, } from './constants.js'; | ||
import { assertIsBytes } from './helpers.js'; | ||
@@ -19,15 +19,15 @@ /** | ||
? BigInt(sig.recovery + 27) | ||
: BigInt(sig.recovery + 35) + BigInt(chainId) * BigInt(2); | ||
: BigInt(sig.recovery + 35) + BigInt(chainId) * BIGINT_2; | ||
return { r, s, v }; | ||
} | ||
function calculateSigRecovery(v, chainId) { | ||
if (v === BigInt(0) || v === BigInt(1)) | ||
if (v === BIGINT_0 || v === BIGINT_1) | ||
return v; | ||
if (chainId === undefined) { | ||
return v - BigInt(27); | ||
return v - BIGINT_27; | ||
} | ||
return v - (chainId * BigInt(2) + BigInt(35)); | ||
return v - (chainId * BIGINT_2 + BigInt(35)); | ||
} | ||
function isValidSigRecovery(recovery) { | ||
return recovery === BigInt(0) || recovery === BigInt(1); | ||
return recovery === BIGINT_0 || recovery === BIGINT_1; | ||
} | ||
@@ -73,3 +73,3 @@ /** | ||
const ss = Uint8Array.from([...s]); | ||
if ((v > BigInt(28) && v % BigInt(2) === BigInt(1)) || v === BigInt(1) || v === BigInt(28)) { | ||
if ((v > BigInt(28) && v % BIGINT_2 === BIGINT_1) || v === BIGINT_1 || v === BigInt(28)) { | ||
ss[0] |= 0x80; | ||
@@ -109,3 +109,3 @@ } | ||
if (v < 27) { | ||
v = v + BigInt(27); | ||
v = v + BIGINT_27; | ||
} | ||
@@ -132,5 +132,5 @@ return { | ||
const sBigInt = bytesToBigInt(s); | ||
if (rBigInt === BigInt(0) || | ||
if (rBigInt === BIGINT_0 || | ||
rBigInt >= SECP256K1_ORDER || | ||
sBigInt === BigInt(0) || | ||
sBigInt === BIGINT_0 || | ||
sBigInt >= SECP256K1_ORDER) { | ||
@@ -137,0 +137,0 @@ return false; |
/** Easy conversion from Gwei to wei */ | ||
export declare const GWEI_TO_WEI: bigint; | ||
export declare function formatBigDecimal(numerator: bigint, denominator: bigint, maxDecimalFactor: bigint): string; | ||
//# sourceMappingURL=units.d.ts.map |
@@ -0,3 +1,14 @@ | ||
import { BIGINT_0, BIGINT_1 } from './constants.js'; | ||
/** Easy conversion from Gwei to wei */ | ||
export const GWEI_TO_WEI = BigInt(1000000000); | ||
export function formatBigDecimal(numerator, denominator, maxDecimalFactor) { | ||
if (denominator === BIGINT_0) { | ||
denominator = BIGINT_1; | ||
} | ||
const full = numerator / denominator; | ||
const fraction = ((numerator - full * denominator) * maxDecimalFactor) / denominator; | ||
// zeros to be added post decimal are number of zeros in maxDecimalFactor - number of digits in fraction | ||
const zerosPostDecimal = String(maxDecimalFactor).length - 1 - String(fraction).length; | ||
return `${full}.${'0'.repeat(zerosPostDecimal)}${fraction}`; | ||
} | ||
//# sourceMappingURL=units.js.map |
import { Address } from './address.js'; | ||
import { bigIntToHex, bytesToHex, toBytes } from './bytes.js'; | ||
import { BIGINT_0 } from './constants.js'; | ||
import { TypeOutput, toType } from './types.js'; | ||
@@ -45,10 +46,10 @@ /** | ||
const { index, validatorIndex, address, amount } = withdrawal; | ||
const indexBytes = toType(index, TypeOutput.BigInt) === BigInt(0) | ||
const indexBytes = toType(index, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
: toType(index, TypeOutput.Uint8Array); | ||
const validatorIndexBytes = toType(validatorIndex, TypeOutput.BigInt) === BigInt(0) | ||
const validatorIndexBytes = toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
: toType(validatorIndex, TypeOutput.Uint8Array); | ||
const addressBytes = address instanceof Address ? address.bytes : toType(address, TypeOutput.Uint8Array); | ||
const amountBytes = toType(amount, TypeOutput.BigInt) === BigInt(0) | ||
const amountBytes = toType(amount, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
@@ -55,0 +56,0 @@ : toType(amount, TypeOutput.Uint8Array); |
{ | ||
"name": "@ethereumjs/util", | ||
"version": "9.0.0", | ||
"version": "9.0.1", | ||
"description": "A collection of utility functions for Ethereum", | ||
@@ -80,3 +80,3 @@ "keywords": [ | ||
"clean": "../../config/cli/clean-package.sh", | ||
"coverage": "npx vitest run --coverage.enabled --coverage.reporter=lcov", | ||
"coverage": "DEBUG=ethjs npx vitest run --coverage.enabled --coverage.reporter=lcov", | ||
"docs:build": "npx typedoc --options typedoc.cjs", | ||
@@ -94,3 +94,3 @@ "examples": "ts-node ../../scripts/examples-runner.ts -- util", | ||
"dependencies": { | ||
"@ethereumjs/rlp": "^5.0.0", | ||
"@ethereumjs/rlp": "^5.0.1", | ||
"ethereum-cryptography": "^2.1.2" | ||
@@ -100,3 +100,3 @@ }, | ||
"peerDependencies": { | ||
"c-kzg": "^2.1.0" | ||
"c-kzg": "^2.1.2" | ||
}, | ||
@@ -103,0 +103,0 @@ "peerDependenciesMeta": { |
@@ -16,3 +16,3 @@ import { RLP } from '@ethereumjs/rlp' | ||
} from './bytes.js' | ||
import { KECCAK256_NULL, KECCAK256_RLP } from './constants.js' | ||
import { BIGINT_0, KECCAK256_NULL, KECCAK256_RLP } from './constants.js' | ||
import { assertIsBytes, assertIsHexString, assertIsString } from './helpers.js' | ||
@@ -23,4 +23,2 @@ import { stripHexPrefix } from './internal.js' | ||
const _0n = BigInt(0) | ||
export interface AccountData { | ||
@@ -72,3 +70,8 @@ nonce?: BigIntLike | ||
*/ | ||
constructor(nonce = _0n, balance = _0n, storageRoot = KECCAK256_RLP, codeHash = KECCAK256_NULL) { | ||
constructor( | ||
nonce = BIGINT_0, | ||
balance = BIGINT_0, | ||
storageRoot = KECCAK256_RLP, | ||
codeHash = KECCAK256_NULL | ||
) { | ||
this.nonce = nonce | ||
@@ -83,6 +86,6 @@ this.balance = balance | ||
private _validate() { | ||
if (this.nonce < _0n) { | ||
if (this.nonce < BIGINT_0) { | ||
throw new Error('nonce must be greater than zero') | ||
} | ||
if (this.balance < _0n) { | ||
if (this.balance < BIGINT_0) { | ||
throw new Error('balance must be greater than zero') | ||
@@ -130,3 +133,7 @@ } | ||
isEmpty(): boolean { | ||
return this.balance === _0n && this.nonce === _0n && equalsBytes(this.codeHash, KECCAK256_NULL) | ||
return ( | ||
this.balance === BIGINT_0 && | ||
this.nonce === BIGINT_0 && | ||
equalsBytes(this.codeHash, KECCAK256_NULL) | ||
) | ||
} | ||
@@ -209,3 +216,3 @@ } | ||
if (bytesToBigInt(nonce) === BigInt(0)) { | ||
if (bytesToBigInt(nonce) === BIGINT_0) { | ||
// in RLP we want to encode null in the case of zero nonce | ||
@@ -212,0 +219,0 @@ // read the RLP documentation for an answer if you dare |
@@ -9,2 +9,3 @@ import { | ||
import { bigIntToBytes, bytesToBigInt, bytesToHex, equalsBytes, toBytes, zeros } from './bytes.js' | ||
import { BIGINT_0 } from './constants.js' | ||
@@ -114,3 +115,3 @@ /** | ||
const address = bytesToBigInt(this.bytes) | ||
const rangeMin = BigInt(0) | ||
const rangeMin = BIGINT_0 | ||
const rangeMax = BigInt('0xffff') | ||
@@ -117,0 +118,0 @@ return address >= rangeMin && address <= rangeMax |
@@ -13,2 +13,4 @@ import { getRandomBytesSync } from 'ethereum-cryptography/random.js' | ||
const BIGINT_0 = BigInt(0) | ||
/** | ||
@@ -43,2 +45,8 @@ * @deprecated | ||
// BigInt cache for the numbers 0 - 256*256-1 (two-byte bytes) | ||
const BIGINT_CACHE: bigint[] = [] | ||
for (let i = 0; i <= 256 * 256 - 1; i++) { | ||
BIGINT_CACHE[i] = BigInt(i) | ||
} | ||
/** | ||
@@ -52,4 +60,11 @@ * Converts a {@link Uint8Array} to a {@link bigint} | ||
if (hex === '0x') { | ||
return BigInt(0) | ||
return BIGINT_0 | ||
} | ||
if (hex.length === 4) { | ||
// If the byte length is 1 (this is faster than checking `bytes.length === 1`) | ||
return BIGINT_CACHE[bytes[0]] | ||
} | ||
if (hex.length === 6) { | ||
return BIGINT_CACHE[bytes[0] * 256 + bytes[1]] | ||
} | ||
return BigInt(hex) | ||
@@ -273,3 +288,3 @@ } | ||
if (typeof v === 'bigint') { | ||
if (v < BigInt(0)) { | ||
if (v < BIGINT_0) { | ||
throw new Error(`Cannot convert negative bigint to Uint8Array. Given: ${v}`) | ||
@@ -436,3 +451,59 @@ } | ||
/** | ||
* @notice Convert a Uint8Array to a 32-bit integer | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 32-bit integer. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 4. | ||
* @return {number} The 32-bit integer read from the input Uint8Array. | ||
*/ | ||
export function bytesToInt32(bytes: Uint8Array, littleEndian: boolean = false): number { | ||
if (bytes.length < 4) { | ||
throw new Error('The input Uint8Array must have at least 4 bytes.') | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) | ||
return dataView.getInt32(0, littleEndian) | ||
} | ||
/** | ||
* @notice Convert a Uint8Array to a 64-bit bigint | ||
* @param {Uint8Array} bytes The input Uint8Array from which to read the 64-bit bigint. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @throws {Error} If the input Uint8Array has a length less than 8. | ||
* @return {bigint} The 64-bit bigint read from the input Uint8Array. | ||
*/ | ||
export function bytesToBigInt64(bytes: Uint8Array, littleEndian: boolean = false): bigint { | ||
if (bytes.length < 8) { | ||
throw new Error('The input Uint8Array must have at least 8 bytes.') | ||
} | ||
const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength) | ||
return dataView.getBigInt64(0, littleEndian) | ||
} | ||
/** | ||
* @notice Convert a 32-bit integer to a Uint8Array. | ||
* @param {number} value The 32-bit integer to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 4 containing the integer. | ||
*/ | ||
export function int32ToBytes(value: number, littleEndian: boolean = false): Uint8Array { | ||
const buffer = new ArrayBuffer(4) | ||
const dataView = new DataView(buffer) | ||
dataView.setInt32(0, value, littleEndian) | ||
return new Uint8Array(buffer) | ||
} | ||
/** | ||
* @notice Convert a 64-bit bigint to a Uint8Array. | ||
* @param {bigint} value The 64-bit bigint to convert. | ||
* @param {boolean} littleEndian True for little-endian, undefined or false for big-endian. | ||
* @return {Uint8Array} A Uint8Array of length 8 containing the bigint. | ||
*/ | ||
export function bigInt64ToBytes(value: bigint, littleEndian: boolean = false): Uint8Array { | ||
const buffer = new ArrayBuffer(8) | ||
const dataView = new DataView(buffer) | ||
dataView.setBigInt64(0, value, littleEndian) | ||
return new Uint8Array(buffer) | ||
} | ||
// eslint-disable-next-line no-restricted-imports | ||
export { bytesToUtf8, equalsBytes, utf8ToBytes } from 'ethereum-cryptography/utils.js' |
@@ -75,1 +75,34 @@ import { secp256k1 } from 'ethereum-cryptography/secp256k1.js' | ||
export const RIPEMD160_ADDRESS_STRING = '0000000000000000000000000000000000000003' | ||
/** | ||
* BigInt constants | ||
*/ | ||
export const BIGINT_NEG1 = BigInt(-1) | ||
export const BIGINT_0 = BigInt(0) | ||
export const BIGINT_1 = BigInt(1) | ||
export const BIGINT_2 = BigInt(2) | ||
export const BIGINT_3 = BigInt(3) | ||
export const BIGINT_7 = BigInt(7) | ||
export const BIGINT_8 = BigInt(8) | ||
export const BIGINT_27 = BigInt(27) | ||
export const BIGINT_28 = BigInt(28) | ||
export const BIGINT_31 = BigInt(31) | ||
export const BIGINT_32 = BigInt(32) | ||
export const BIGINT_64 = BigInt(64) | ||
export const BIGINT_128 = BigInt(128) | ||
export const BIGINT_255 = BigInt(255) | ||
export const BIGINT_256 = BigInt(256) | ||
export const BIGINT_96 = BigInt(96) | ||
export const BIGINT_100 = BigInt(100) | ||
export const BIGINT_160 = BigInt(160) | ||
export const BIGINT_224 = BigInt(224) | ||
export const BIGINT_2EXP96 = BigInt(79228162514264337593543950336) | ||
export const BIGINT_2EXP160 = BigInt(1461501637330902918203684832716283019655932542976) | ||
export const BIGINT_2EXP224 = | ||
BigInt(26959946667150639794667015087019630673637144422540572481103610249216) | ||
export const BIGINT_2EXP256 = BIGINT_2 ** BIGINT_256 |
@@ -51,3 +51,4 @@ import { addHexPrefix, bigIntToHex } from './bytes.js' | ||
let { balance, code, storage, nonce } = json.alloc[address] | ||
address = addHexPrefix(address) | ||
// create a map with lowercase for easy lookups | ||
address = addHexPrefix(address.toLowerCase()) | ||
balance = isHexPrefixed(balance) ? balance : bigIntToHex(BigInt(balance)) | ||
@@ -54,0 +55,0 @@ code = code !== undefined ? addHexPrefix(code) : undefined |
@@ -25,3 +25,3 @@ /* | ||
import { bytesToHex, utf8ToBytes } from './bytes.js' | ||
import { bytesToUnprefixedHex, utf8ToBytes } from './bytes.js' | ||
@@ -133,3 +133,4 @@ /** | ||
/** | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string | ||
* Should be called to get hex representation (prefixed by 0x) of utf8 string. | ||
* Strips leading and trailing 0's. | ||
* | ||
@@ -143,3 +144,3 @@ * @param string | ||
return `0x${padToEven(bytesToHex(str)).replace(/^0+|0+$/g, '')}` | ||
return `0x${padToEven(bytesToUnprefixedHex(str)).replace(/^0+|0+$/g, '')}` | ||
} | ||
@@ -146,0 +147,0 @@ |
@@ -13,3 +13,10 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js' | ||
} from './bytes.js' | ||
import { SECP256K1_ORDER, SECP256K1_ORDER_DIV_2 } from './constants.js' | ||
import { | ||
BIGINT_0, | ||
BIGINT_1, | ||
BIGINT_2, | ||
BIGINT_27, | ||
SECP256K1_ORDER, | ||
SECP256K1_ORDER_DIV_2, | ||
} from './constants.js' | ||
import { assertIsBytes } from './helpers.js' | ||
@@ -42,3 +49,3 @@ | ||
? BigInt(sig.recovery! + 27) | ||
: BigInt(sig.recovery! + 35) + BigInt(chainId) * BigInt(2) | ||
: BigInt(sig.recovery! + 35) + BigInt(chainId) * BIGINT_2 | ||
@@ -49,12 +56,12 @@ return { r, s, v } | ||
function calculateSigRecovery(v: bigint, chainId?: bigint): bigint { | ||
if (v === BigInt(0) || v === BigInt(1)) return v | ||
if (v === BIGINT_0 || v === BIGINT_1) return v | ||
if (chainId === undefined) { | ||
return v - BigInt(27) | ||
return v - BIGINT_27 | ||
} | ||
return v - (chainId * BigInt(2) + BigInt(35)) | ||
return v - (chainId * BIGINT_2 + BigInt(35)) | ||
} | ||
function isValidSigRecovery(recovery: bigint): boolean { | ||
return recovery === BigInt(0) || recovery === BigInt(1) | ||
return recovery === BIGINT_0 || recovery === BIGINT_1 | ||
} | ||
@@ -123,3 +130,3 @@ | ||
const ss = Uint8Array.from([...s]) | ||
if ((v > BigInt(28) && v % BigInt(2) === BigInt(1)) || v === BigInt(1) || v === BigInt(28)) { | ||
if ((v > BigInt(28) && v % BIGINT_2 === BIGINT_1) || v === BIGINT_1 || v === BigInt(28)) { | ||
ss[0] |= 0x80 | ||
@@ -161,3 +168,3 @@ } | ||
if (v < 27) { | ||
v = v + BigInt(27) | ||
v = v + BIGINT_27 | ||
} | ||
@@ -196,5 +203,5 @@ | ||
if ( | ||
rBigInt === BigInt(0) || | ||
rBigInt === BIGINT_0 || | ||
rBigInt >= SECP256K1_ORDER || | ||
sBigInt === BigInt(0) || | ||
sBigInt === BIGINT_0 || | ||
sBigInt >= SECP256K1_ORDER | ||
@@ -201,0 +208,0 @@ ) { |
@@ -0,2 +1,20 @@ | ||
import { BIGINT_0, BIGINT_1 } from './constants.js' | ||
/** Easy conversion from Gwei to wei */ | ||
export const GWEI_TO_WEI = BigInt(1000000000) | ||
export function formatBigDecimal( | ||
numerator: bigint, | ||
denominator: bigint, | ||
maxDecimalFactor: bigint | ||
): string { | ||
if (denominator === BIGINT_0) { | ||
denominator = BIGINT_1 | ||
} | ||
const full = numerator / denominator | ||
const fraction = ((numerator - full * denominator) * maxDecimalFactor) / denominator | ||
// zeros to be added post decimal are number of zeros in maxDecimalFactor - number of digits in fraction | ||
const zerosPostDecimal = String(maxDecimalFactor).length - 1 - String(fraction).length | ||
return `${full}.${'0'.repeat(zerosPostDecimal)}${fraction}` | ||
} |
import { Address } from './address.js' | ||
import { bigIntToHex, bytesToHex, toBytes } from './bytes.js' | ||
import { BIGINT_0 } from './constants.js' | ||
import { TypeOutput, toType } from './types.js' | ||
@@ -81,7 +82,7 @@ | ||
const indexBytes = | ||
toType(index, TypeOutput.BigInt) === BigInt(0) | ||
toType(index, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
: toType(index, TypeOutput.Uint8Array) | ||
const validatorIndexBytes = | ||
toType(validatorIndex, TypeOutput.BigInt) === BigInt(0) | ||
toType(validatorIndex, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
@@ -93,3 +94,3 @@ : toType(validatorIndex, TypeOutput.Uint8Array) | ||
const amountBytes = | ||
toType(amount, TypeOutput.BigInt) === BigInt(0) | ||
toType(amount, TypeOutput.BigInt) === BIGINT_0 | ||
? new Uint8Array() | ||
@@ -96,0 +97,0 @@ : toType(amount, TypeOutput.Uint8Array) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
542888
8235
Updated@ethereumjs/rlp@^5.0.1