UNPKG

@river-build/dlog

Version:

Includes logging and a few other catchall items

61 lines 2.12 kB
// eslint-disable-next-line import/no-unresolved import { base64Decode, base64Encode } from '@bufbuild/protobuf/wire'; import { bytesToHex, bytesToUtf8, equalsBytes, hexToBytes, utf8ToBytes, } from 'ethereum-cryptography/utils'; export function bin_fromBase64(base64String) { return base64Decode(base64String); } export function bin_toBase64(uint8Array) { return base64Encode(uint8Array); } export function bin_fromHexString(hexString) { return hexToBytes(hexString); } export function bin_toHexString(uint8Array) { return bytesToHex(uint8Array); } export function bin_fromString(str) { return utf8ToBytes(str); } export function bin_toString(buf) { return bytesToUtf8(buf); } export function shortenHexString(s) { if (s.startsWith('0x')) { return s.length > 12 ? s.slice(0, 6) + '..' + s.slice(-4) : s; } else { return s.length > 10 ? s.slice(0, 4) + '..' + s.slice(-4) : s; } } export function isHexString(value) { if (value.length === 0 || (value.length & 1) !== 0) { return false; } return /^(0x)?[0-9a-fA-F]+$/.test(value); } export function bin_equal(a, b) { if ((a === undefined || a === null || a.length === 0) && (b === undefined || b === null || b.length === 0)) { return true; } else if (a === undefined || a === null || b === undefined || b === null) { return false; } return equalsBytes(a, b); } // Returns the absolute value of this BigInt as a big-endian byte array export function bigIntToBytes(value) { const abs = value < 0n ? -value : value; // Calculate the byte length needed to represent the BigInt const byteLength = Math.ceil(abs.toString(16).length / 2); // Create a buffer of the required length const buffer = new Uint8Array(byteLength); // Fill the buffer with the big-endian representation of the BigInt let temp = abs; for (let i = byteLength - 1; i >= 0; i--) { buffer[i] = Number(temp & 0xffn); // Extract last 8 bits temp >>= 8n; // Shift right by 8 bits } return buffer; } //# sourceMappingURL=binary.js.map