@nomicfoundation/ethereumjs-util
Advanced tools
Comparing version 9.0.1 to 9.0.2
@@ -120,4 +120,4 @@ /// <reference types="node" /> | ||
export declare const isZeroAddress: (hexAddress: string) => boolean; | ||
export declare function accountBodyFromSlim(body: AccountBodyBuffer): (Buffer | Uint8Array)[]; | ||
export declare function accountBodyToSlim(body: AccountBodyBuffer): (Buffer | Uint8Array)[]; | ||
export declare function accountBodyFromSlim(body: AccountBodyBuffer): (Uint8Array | Buffer)[]; | ||
export declare function accountBodyToSlim(body: AccountBodyBuffer): (Uint8Array | Buffer)[]; | ||
/** | ||
@@ -124,0 +124,0 @@ * Converts a slim account (per snap protocol spec) to the RLP encoded version of the account |
/// <reference types="node" /> | ||
import type { NestedBufferArray, NestedUint8Array, PrefixedHexString, TransformableToArray, TransformableToBuffer } from './types'; | ||
import type { NestedBufferArray, NestedUint8Array, PrefixedHexString, TransformableToArray, TransformableToBuffer, TransformabletoBytes } from './types'; | ||
/** | ||
@@ -16,2 +16,8 @@ * Converts a `Number` into a hex `String` | ||
/** | ||
* Converts a {@link bigint} to a {@link Uint8Array} | ||
* * @param {bigint} num the bigint to convert | ||
* @returns {Uint8Array} | ||
*/ | ||
export declare const bigIntToBytes: (num: bigint) => Uint8Array; | ||
/** | ||
* Returns a buffer filled with 0s. | ||
@@ -82,3 +88,20 @@ * @param bytes the number of bytes the buffer should be | ||
export declare const bufferToInt: (buf: Buffer) => number; | ||
export declare const hexToBytes: (hex: string) => Uint8Array; | ||
/** | ||
* Converts an {@link number} to a {@link Uint8Array} | ||
* @param {Number} i | ||
* @return {Uint8Array} | ||
*/ | ||
export declare const intToBytes: (i: number) => Uint8Array; | ||
export declare const unprefixedHexToBytes: (inp: string) => Uint8Array; | ||
export declare type ToBytesInputTypes = PrefixedHexString | number | bigint | Uint8Array | number[] | TransformabletoBytes | null | undefined; | ||
/** | ||
* Attempts to turn a value into a `Uint8Array`. | ||
* Inputs supported: `Buffer`, `Uint8Array`, `String` (hex-prefixed), `Number`, null/undefined, `BigInt` and other objects | ||
* with a `toArray()` or `toBytes()` method. | ||
* @param {ToBytesInputTypes} v the value | ||
* @return {Uint8Array} | ||
*/ | ||
export declare const toBytes: (v: ToBytesInputTypes) => Uint8Array; | ||
/** | ||
* Interprets a `Buffer` as a signed integer and returns a `BigInt`. Assumes 256-bit numbers. | ||
@@ -169,2 +192,4 @@ * @param num Signed integer value | ||
export declare function intToUnpaddedBuffer(value: number): Buffer; | ||
export declare const bytesToHex: (bytes: Uint8Array) => string; | ||
export declare const bytesToBigInt: (bytes: Uint8Array) => bigint; | ||
//# sourceMappingURL=bytes.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.intToUnpaddedBuffer = exports.bigIntToUnpaddedBuffer = exports.bigIntToHex = exports.bufArrToArr = exports.arrToBufArr = exports.validateNoLeadingZeroes = exports.baToJSON = exports.toUtf8 = exports.short = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.bufferToInt = exports.bigIntToBuffer = exports.bufferToBigInt = exports.bufferToHex = exports.toBuffer = exports.unpadHexString = exports.unpadArray = exports.unpadBuffer = exports.setLengthRight = exports.setLengthLeft = exports.zeros = exports.intToBuffer = exports.intToHex = void 0; | ||
exports.bytesToBigInt = exports.bytesToHex = exports.intToUnpaddedBuffer = exports.bigIntToUnpaddedBuffer = exports.bigIntToHex = exports.bufArrToArr = exports.arrToBufArr = exports.validateNoLeadingZeroes = exports.baToJSON = exports.toUtf8 = exports.short = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.toBytes = exports.unprefixedHexToBytes = exports.intToBytes = exports.hexToBytes = exports.bufferToInt = exports.bigIntToBuffer = exports.bufferToBigInt = exports.bufferToHex = exports.toBuffer = exports.unpadHexString = exports.unpadArray = exports.unpadBuffer = exports.setLengthRight = exports.setLengthLeft = exports.zeros = exports.bigIntToBytes = exports.intToBuffer = exports.intToHex = void 0; | ||
const helpers_1 = require("./helpers"); | ||
@@ -29,2 +29,12 @@ const internal_1 = require("./internal"); | ||
/** | ||
* Converts a {@link bigint} to a {@link Uint8Array} | ||
* * @param {bigint} num the bigint to convert | ||
* @returns {Uint8Array} | ||
*/ | ||
const bigIntToBytes = (num) => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return (0, exports.toBytes)('0x' + (0, internal_1.padToEven)(num.toString(16))); | ||
}; | ||
exports.bigIntToBytes = bigIntToBytes; | ||
/** | ||
* Returns a buffer filled with 0s. | ||
@@ -213,3 +223,99 @@ * @param bytes the number of bytes the buffer should be | ||
exports.bufferToInt = bufferToInt; | ||
const hexToBytes = (hex) => { | ||
if (typeof hex !== 'string') { | ||
throw new Error(`hex argument type ${typeof hex} must be of type string`); | ||
} | ||
if (!hex.startsWith('0x')) { | ||
throw new Error(`prefixed hex input should start with 0x, got ${hex.substring(0, 2)}`); | ||
} | ||
hex = hex.slice(2); | ||
if (hex.length % 2 !== 0) { | ||
hex = (0, internal_1.padToEven)(hex); | ||
} | ||
const byteLen = hex.length / 2; | ||
const bytes = new Uint8Array(byteLen); | ||
for (let i = 0; i < byteLen; i++) { | ||
const byte = parseInt(hex.slice(i * 2, (i + 1) * 2), 16); | ||
bytes[i] = byte; | ||
} | ||
return bytes; | ||
}; | ||
exports.hexToBytes = hexToBytes; | ||
/** | ||
* Converts an {@link number} to a {@link Uint8Array} | ||
* @param {Number} i | ||
* @return {Uint8Array} | ||
*/ | ||
const intToBytes = (i) => { | ||
const hex = (0, exports.intToHex)(i); | ||
return (0, exports.hexToBytes)(hex); | ||
}; | ||
exports.intToBytes = intToBytes; | ||
const _unprefixedHexToBytes = (data) => { | ||
const hex = data.startsWith('0x') ? data.substring(2) : data; | ||
if (typeof hex !== 'string') | ||
throw new Error('hex string expected, got ' + typeof hex); | ||
const len = hex.length; | ||
if (len % 2) | ||
throw new Error('padded hex string expected, got unpadded hex of length ' + len); | ||
const array = new Uint8Array(len / 2); | ||
for (let i = 0; i < array.length; i++) { | ||
const j = i * 2; | ||
const hexByte = hex.slice(j, j + 2); | ||
const byte = Number.parseInt(hexByte, 16); | ||
if (Number.isNaN(byte) || byte < 0) | ||
throw new Error('Invalid byte sequence'); | ||
array[i] = byte; | ||
} | ||
return array; | ||
}; | ||
const unprefixedHexToBytes = (inp) => { | ||
if (inp.slice(0, 2) === '0x') { | ||
throw new Error('hex string is prefixed with 0x, should be unprefixed'); | ||
} | ||
else { | ||
return _unprefixedHexToBytes((0, internal_1.padToEven)(inp)); | ||
} | ||
}; | ||
exports.unprefixedHexToBytes = unprefixedHexToBytes; | ||
/** | ||
* Attempts to turn a value into a `Uint8Array`. | ||
* Inputs supported: `Buffer`, `Uint8Array`, `String` (hex-prefixed), `Number`, null/undefined, `BigInt` and other objects | ||
* with a `toArray()` or `toBytes()` method. | ||
* @param {ToBytesInputTypes} v the value | ||
* @return {Uint8Array} | ||
*/ | ||
const toBytes = (v) => { | ||
if (v === null || v === undefined) { | ||
return new Uint8Array(); | ||
} | ||
if (Array.isArray(v) || v instanceof Uint8Array) { | ||
return Uint8Array.from(v); | ||
} | ||
if (typeof v === 'string') { | ||
if (!(0, internal_1.isHexString)(v)) { | ||
throw new Error(`Cannot convert string to Uint8Array. toBytes only supports 0x-prefixed hex strings and this string was given: ${v}`); | ||
} | ||
return (0, exports.hexToBytes)(v); | ||
} | ||
if (typeof v === 'number') { | ||
return (0, exports.intToBytes)(v); | ||
} | ||
if (typeof v === 'bigint') { | ||
if (v < BigInt(0)) { | ||
throw new Error(`Cannot convert negative bigint to Uint8Array. Given: ${v}`); | ||
} | ||
let n = v.toString(16); | ||
if (n.length % 2) | ||
n = '0' + n; | ||
return (0, exports.unprefixedHexToBytes)(n); | ||
} | ||
if (v.toBytes !== undefined) { | ||
// converts a `TransformableToBytes` object to a Uint8Array | ||
return v.toBytes(); | ||
} | ||
throw new Error('invalid type'); | ||
}; | ||
exports.toBytes = toBytes; | ||
/** | ||
* Interprets a `Buffer` as a signed integer and returns a `BigInt`. Assumes 256-bit numbers. | ||
@@ -356,2 +462,23 @@ * @param num Signed integer value | ||
exports.intToUnpaddedBuffer = intToUnpaddedBuffer; | ||
/**************** Borrowed from @chainsafe/ssz */ | ||
// Caching this info costs about ~1000 bytes and speeds up toHexString() by x6 | ||
const hexByByte = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0')); | ||
const bytesToHex = (bytes) => { | ||
let hex = '0x'; | ||
if (bytes === undefined || bytes.length === 0) | ||
return hex; | ||
for (const byte of bytes) { | ||
hex += hexByByte[byte]; | ||
} | ||
return hex; | ||
}; | ||
exports.bytesToHex = bytesToHex; | ||
const bytesToBigInt = (bytes) => { | ||
const hex = (0, exports.bytesToHex)(bytes); | ||
if (hex === '0x') { | ||
return BigInt(0); | ||
} | ||
return BigInt(hex); | ||
}; | ||
exports.bytesToBigInt = bytesToBigInt; | ||
//# sourceMappingURL=bytes.js.map |
@@ -11,2 +11,5 @@ /// <reference types="node" /> | ||
export declare type AddressLike = Address | Buffer | PrefixedHexString; | ||
export interface TransformabletoBytes { | ||
toBytes?(): Uint8Array; | ||
} | ||
export interface TransformableToArray { | ||
@@ -13,0 +16,0 @@ toArray(): Uint8Array; |
{ | ||
"name": "@nomicfoundation/ethereumjs-util", | ||
"version": "9.0.1", | ||
"version": "9.0.2", | ||
"description": "A collection of utility functions for Ethereum", | ||
@@ -86,3 +86,3 @@ "keywords": [ | ||
"@chainsafe/ssz": "^0.10.0", | ||
"@nomicfoundation/ethereumjs-rlp": "5.0.1", | ||
"@nomicfoundation/ethereumjs-rlp": "5.0.2", | ||
"ethereum-cryptography": "0.1.3" | ||
@@ -89,0 +89,0 @@ }, |
147
src/bytes.ts
@@ -10,2 +10,3 @@ import { assertIsArray, assertIsBuffer, assertIsHexString } from './helpers' | ||
TransformableToBuffer, | ||
TransformabletoBytes, | ||
} from './types' | ||
@@ -36,2 +37,12 @@ | ||
/** | ||
* Converts a {@link bigint} to a {@link Uint8Array} | ||
* * @param {bigint} num the bigint to convert | ||
* @returns {Uint8Array} | ||
*/ | ||
export const bigIntToBytes = (num: bigint): Uint8Array => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return toBytes('0x' + padToEven(num.toString(16))) | ||
} | ||
/** | ||
* Returns a buffer filled with 0s. | ||
@@ -241,3 +252,118 @@ * @param bytes the number of bytes the buffer should be | ||
export const hexToBytes = (hex: string): Uint8Array => { | ||
if (typeof hex !== 'string') { | ||
throw new Error(`hex argument type ${typeof hex} must be of type string`) | ||
} | ||
if (!hex.startsWith('0x')) { | ||
throw new Error(`prefixed hex input should start with 0x, got ${hex.substring(0, 2)}`) | ||
} | ||
hex = hex.slice(2) | ||
if (hex.length % 2 !== 0) { | ||
hex = padToEven(hex) | ||
} | ||
const byteLen = hex.length / 2 | ||
const bytes = new Uint8Array(byteLen) | ||
for (let i = 0; i < byteLen; i++) { | ||
const byte = parseInt(hex.slice(i * 2, (i + 1) * 2), 16) | ||
bytes[i] = byte | ||
} | ||
return bytes | ||
} | ||
/** | ||
* Converts an {@link number} to a {@link Uint8Array} | ||
* @param {Number} i | ||
* @return {Uint8Array} | ||
*/ | ||
export const intToBytes = (i: number): Uint8Array => { | ||
const hex = intToHex(i) | ||
return hexToBytes(hex) | ||
} | ||
const _unprefixedHexToBytes = (data: string) => { | ||
const hex = data.startsWith('0x') ? data.substring(2) : data | ||
if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex) | ||
const len = hex.length | ||
if (len % 2) throw new Error('padded hex string expected, got unpadded hex of length ' + len) | ||
const array = new Uint8Array(len / 2) | ||
for (let i = 0; i < array.length; i++) { | ||
const j = i * 2 | ||
const hexByte = hex.slice(j, j + 2) | ||
const byte = Number.parseInt(hexByte, 16) | ||
if (Number.isNaN(byte) || byte < 0) throw new Error('Invalid byte sequence') | ||
array[i] = byte | ||
} | ||
return array | ||
} | ||
export const unprefixedHexToBytes = (inp: string) => { | ||
if (inp.slice(0, 2) === '0x') { | ||
throw new Error('hex string is prefixed with 0x, should be unprefixed') | ||
} else { | ||
return _unprefixedHexToBytes(padToEven(inp)) | ||
} | ||
} | ||
export type ToBytesInputTypes = | ||
| PrefixedHexString | ||
| number | ||
| bigint | ||
| Uint8Array | ||
| number[] | ||
| TransformabletoBytes | ||
| null | ||
| undefined | ||
/** | ||
* Attempts to turn a value into a `Uint8Array`. | ||
* Inputs supported: `Buffer`, `Uint8Array`, `String` (hex-prefixed), `Number`, null/undefined, `BigInt` and other objects | ||
* with a `toArray()` or `toBytes()` method. | ||
* @param {ToBytesInputTypes} v the value | ||
* @return {Uint8Array} | ||
*/ | ||
export const toBytes = (v: ToBytesInputTypes): Uint8Array => { | ||
if (v === null || v === undefined) { | ||
return new Uint8Array() | ||
} | ||
if (Array.isArray(v) || v instanceof Uint8Array) { | ||
return Uint8Array.from(v) | ||
} | ||
if (typeof v === 'string') { | ||
if (!isHexString(v)) { | ||
throw new Error( | ||
`Cannot convert string to Uint8Array. toBytes only supports 0x-prefixed hex strings and this string was given: ${v}` | ||
) | ||
} | ||
return hexToBytes(v) | ||
} | ||
if (typeof v === 'number') { | ||
return intToBytes(v) | ||
} | ||
if (typeof v === 'bigint') { | ||
if (v < BigInt(0)) { | ||
throw new Error(`Cannot convert negative bigint to Uint8Array. Given: ${v}`) | ||
} | ||
let n = v.toString(16) | ||
if (n.length % 2) n = '0' + n | ||
return unprefixedHexToBytes(n) | ||
} | ||
if (v.toBytes !== undefined) { | ||
// converts a `TransformableToBytes` object to a Uint8Array | ||
return v.toBytes() | ||
} | ||
throw new Error('invalid type') | ||
} | ||
/** | ||
* Interprets a `Buffer` as a signed integer and returns a `BigInt`. Assumes 256-bit numbers. | ||
@@ -396,1 +522,22 @@ * @param num Signed integer value | ||
} | ||
/**************** Borrowed from @chainsafe/ssz */ | ||
// Caching this info costs about ~1000 bytes and speeds up toHexString() by x6 | ||
const hexByByte = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0')) | ||
export const bytesToHex = (bytes: Uint8Array): string => { | ||
let hex = '0x' | ||
if (bytes === undefined || bytes.length === 0) return hex | ||
for (const byte of bytes) { | ||
hex += hexByByte[byte] | ||
} | ||
return hex | ||
} | ||
export const bytesToBigInt = (bytes: Uint8Array): bigint => { | ||
const hex = bytesToHex(bytes) | ||
if (hex === '0x') { | ||
return BigInt(0) | ||
} | ||
return BigInt(hex) | ||
} |
@@ -34,2 +34,6 @@ import { bufferToBigInt, bufferToHex, toBuffer } from './bytes' | ||
export interface TransformabletoBytes { | ||
toBytes?(): Uint8Array | ||
} | ||
/* | ||
@@ -36,0 +40,0 @@ * A type that represents an object that has a `toArray()` method. |
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
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
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
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
238014
4467
4
+ Added@nomicfoundation/ethereumjs-rlp@5.0.2(transitive)
- Removed@nomicfoundation/ethereumjs-rlp@5.0.1(transitive)