UNPKG

@xylabs/hex

Version:

Base functionality used throughout XY Labs TypeScript/JavaScript libraries

203 lines (188 loc) 5.57 kB
// src/assert.ts var assertError = (value, assert, defaultMessage) => { if (assert) { const assertString = typeof assert === "string" ? assert : typeof assert === "boolean" ? defaultMessage : assert(value, defaultMessage); if (assertString) { throw new Error(assertString === true ? defaultMessage : assertString); } } return void 0; }; // src/hex/nibble.ts var bitsToNibbles = (value) => { const nibbles = value >> 2; if (value !== nibbles << 2) throw new Error("Bits for nibbles must multiple of 4"); return nibbles; }; var nibblesToBits = (value) => { return value << 2; }; // src/hex/regex.ts var hexRegex = /^[\da-f]+$/i; var hexRegexWithPrefix = /0x[\da-f]+$/i; // src/hex/is.ts var isHex = (value, config) => { if (typeof value !== "string") return false; const valueCharLength = config?.prefix ? value.length - 2 : value.length; if (config?.bitLength !== void 0 && valueCharLength !== bitsToNibbles(config?.bitLength)) return false; return config?.prefix ? hexRegexWithPrefix.test(value) : hexRegex.test(value); }; // src/hex/from/fromHexString.ts var hexFromHexString = (value, config = {}) => { const { prefix = false, byteSize = 8, bitLength } = config; const nibbleBoundary = bitsToNibbles(byteSize); const unEvened = (value.startsWith("0x") ? value.slice(2) : value).toLowerCase(); if (isHex(unEvened)) { const evenCharacters = unEvened.padStart(unEvened.length + unEvened.length % nibbleBoundary, "0"); const padded = bitLength ? evenCharacters.padStart(bitLength / 4, "0") : evenCharacters; return (prefix ? `0x${padded}` : padded).toLowerCase(); } else { throw new Error("Received string is not a value hex"); } }; // src/hex/from/fromArrayBuffer.ts var hexFromArrayBuffer = (buffer, config) => { const unPadded = [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join(""); return hexFromHexString(unPadded, config); }; // src/hex/from/fromBigInt.ts var hexFromBigInt = (value, config = {}) => { const unPadded = value.toString(16); return hexFromHexString(unPadded, config); }; // src/hex/from/fromNumber.ts var hexFromNumber = (value, config) => { return hexFromBigInt(BigInt(value), config); }; // src/hex/from/from.ts var hexFrom = (value, config) => { switch (typeof value) { case "string": { return hexFromHexString(value, config); } case "bigint": { return hexFromBigInt(value, config); } case "number": { return hexFromNumber(value, config); } case "object": { return hexFromArrayBuffer(value, config); } default: { throw new Error(`Invalid type: ${typeof value}`); } } }; // src/hex/as.ts function asHex(value, assert) { let stringValue = void 0; switch (typeof value) { case "string": { stringValue = hexFromHexString(value); break; } default: { return assertError(value, assert, `Unsupported type [${typeof value}]`); } } return isHex(stringValue) ? stringValue : assertError(value, assert, `Value is not Hex [${value}]`); } // src/hex/isHexZero.ts var isHexZero = (value) => { return value ? BigInt(hexFromHexString(value, { prefix: true })) === 0n : void 0; }; // src/hex/legacy.ts var toHexLegacy = (buffer) => { return [...new Uint8Array(buffer)].map((x) => x.toString(16).padStart(2, "0")).join(""); }; // src/hex/to.ts var toHex = (value, config = {}) => { const { prefix = false } = config; return hexFrom(value, { prefix, ...config }); }; // src/address.ts var toAddress = (value, config = {}) => { const { bitLength = 160, prefix = false } = config; return hexFrom(value, { bitLength, prefix, ...config }); }; var isAddress = (value, config = {}) => { const { bitLength = 160, prefix = false } = config; return isHex(value, { bitLength, prefix }); }; function asAddress(value, assert) { try { let stringValue = void 0; switch (typeof value) { case "string": { stringValue = hexFromHexString(value, { prefix: false }); break; } default: { return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0; } } return isAddress(stringValue) ? stringValue : assertError(value, assert, `Value is not an Address [${value}]`); } catch (ex) { const error = ex; return assertError(void 0, assert, error.message); } } // src/hash.ts var HashBitLength = [32, 64, 128, 256, 512, 1024, 2048, 4096]; var isHashBitLength = (value) => { return typeof value === "number" && HashBitLength.includes(value); }; var isHash = (value, bitLength = 256) => { return isHex(value, { bitLength }); }; function asHash(value, assert) { let stringValue = void 0; switch (typeof value) { case "string": { stringValue = hexFromHexString(value); break; } default: { return assert ? assertError(value, assert, `Unsupported type [${typeof value}]`) : void 0; } } return isHash(stringValue) ? stringValue : assertError(value, assert, `Value is not a Hash [${value}]`); } // src/hexToBigInt.ts function hexToBigInt(hex) { return BigInt(hexFromHexString(hex, { prefix: true })); } export { HashBitLength, asAddress, asHash, asHex, bitsToNibbles, hexFrom, hexFromArrayBuffer, hexFromBigInt, hexFromHexString, hexFromNumber, hexRegex, hexRegexWithPrefix, hexToBigInt, isAddress, isHash, isHashBitLength, isHex, isHexZero, nibblesToBits, toAddress, toHex, toHexLegacy }; //# sourceMappingURL=index.mjs.map