UNPKG

mina-attestations

Version:
173 lines 5.58 kB
export { assert, assertDefined, defined, Required, assertHasProperty, assertHasMethod, hasProperty, isObject, assertIsObject, notImplemented, zip, chunk, chunkString, pad, fill, arrayEqual, mapObject, mapToObject, mapEntries, zipObjects, assertExtendsShape, isSubclass, stringLength, mod, ByteUtils, }; function assert(condition, message) { if (!condition) { message = typeof message === 'function' ? message() : message; throw Error(message ?? 'Assertion failed'); } } function assertDefined(input, message) { if (input === undefined) { throw Error(message ?? 'Input is undefined'); } } function defined(input, message) { assertDefined(input, message); return input; } function Required(t) { return new Proxy(t, { get(target, key) { return defined(target[key], `Property "${String(key)}" is undefined`); }, }); } function isObject(obj) { return (typeof obj === 'object' && obj !== null) || typeof obj === 'function'; } function assertIsObject(obj, message) { assert((typeof obj === 'object' && obj !== null) || typeof obj === 'function', () => { // console.log('not an object:', obj); return message; }); } function assertHasProperty(obj, key, message) { assertIsObject(obj, message ?? `Expected value to be an object or function`); assert(key in obj, message ?? `Expected object to have property ${key}`); } function assertHasMethod(obj, key, message) { assertHasProperty(obj, key, message); assert(typeof obj[key] === 'function', message); } function hasProperty(obj, key) { return (((typeof obj === 'object' && obj !== null) || typeof obj === 'function') && key in obj); } function notImplemented() { throw Error('Not implemented'); } function zip(a, b) { assert(a.length === b.length, 'zip(): arrays of unequal length'); return a.map((a, i) => [a, b[i]]); } function chunk(array, size) { assert(array.length % size === 0, `${array.length} is not a multiple of ${size}`); return Array.from({ length: array.length / size }, (_, i) => array.slice(size * i, size * (i + 1))); } function chunkString(str, size) { return chunk([...str], size).map((chunk) => chunk.join('')); } function pad(array, size, value) { assert(array.length <= size, `padding array of size ${array.length} larger than target size ${size}`); let cb = typeof value === 'function' ? value : () => value; return array.concat(Array.from({ length: size - array.length }, cb)); } function fill(size, value) { let cb = typeof value === 'function' ? value : () => value; return Array.from({ length: size }, cb); } function arrayEqual(aI, bI) { let a = aI instanceof ArrayBuffer ? new Uint8Array(aI) : aI; let b = bI instanceof ArrayBuffer ? new Uint8Array(bI) : bI; let n = a.length; if (n !== b.length) return false; for (let i = 0; i < n; i++) { if (a[i] !== b[i]) return false; } return true; } function mapObject(obj, fn) { let result = {}; for (let key in obj) { result[key] = fn(obj[key], key); } return result; } function mapToObject(keys, fn) { let s = {}; keys.forEach((key, i) => { s[key] = fn(key, i); }); return s; } function zipObjects(t, s) { assertExtendsShape(t, s); assertExtendsShape(s, t); return mapObject(t, (t, key) => [ t, s[key], ]); } function mapEntries(obj, fn) { return Object.entries(obj).map((entry) => fn(...entry)); } function assertExtendsShape(a, b) { for (let key in b) { if (!(key in a)) throw Error(`Expected object to have property ${key}`); } } function isSubclass(constructor, base) { if (typeof constructor !== 'function') return false; if (!hasProperty(constructor, 'prototype')) return false; return constructor.prototype instanceof base; } const enc = new TextEncoder(); const dec = new TextDecoder(); function stringLength(str) { return enc.encode(str).length; } // modulo that properly handles negative numbers function mod(x, p) { let z = x % p; return z < 0 ? z + p : z; } const ByteUtils = { fromString(str) { return enc.encode(str); }, toString(bytes) { return dec.decode(bytes); }, fromHex(hex) { if (hex.startsWith('0x')) hex = hex.slice(2); let bytes = chunkString(hex, 2).map((byte) => parseInt(byte, 16)); return new Uint8Array(bytes); }, toHex(bytes) { return bytes.reduce((hex, byte) => hex + byte.toString(16).padStart(2, '0'), ''); }, padStart(bytes, size, value) { assert(bytes.length <= size, 'Bytes.padStart(): bytes larger than size'); if (bytes.length === size) return bytes; let a = new Uint8Array(size); a.fill(value, 0, size - bytes.length); a.set(bytes, size - bytes.length); return a; }, padEnd(bytes, size, value) { assert(bytes.length <= size, 'Bytes.padEnd(): bytes larger than size'); if (bytes.length === size) return bytes; let a = new Uint8Array(size); a.set(bytes); a.fill(value, bytes.length); return a; }, concat(...arrays) { let size = arrays.reduce((s, a) => s + a.length, 0); let a = new Uint8Array(size); let offset = 0; arrays.forEach((b) => { a.set(b, offset); offset += b.length; }); return a; }, }; //# sourceMappingURL=util.js.map