UNPKG

chaingate

Version:

A complete TypeScript library for connecting to and making transactions on different blockchains

58 lines 1.77 kB
export function remove0x(s) { return s.startsWith('0x') ? s.substring(2) : s; } export function isHex(s) { s = remove0x(s); return /\b(0x)?[0-9a-fA-F]+\b/.test(s); } export function hexToBytes(hex) { if (!isHex(hex)) throw new Error('Invalid hexadecimal value'); const hexWithout0x = remove0x(hex); const length = hexWithout0x.length; const bytes = new Uint8Array(length / 2); for (let i = 0; i < length; i += 2) { bytes[i / 2] = parseInt(hexWithout0x.substring(i, i + 2), 16); } return bytes; } export function bytesToHex(bytes, use0x) { return ((use0x ? '0x' : '') + Array.from(bytes) .map((b) => b.toString(16).padStart(2, '0')) .join('')); } export function generateSecureRandomBytes(length) { const randomBytes = new Uint8Array(length); crypto.getRandomValues(randomBytes); return randomBytes; } export function buildUrlWithApiKey(baseUrl, apiKey) { const url = new URL(baseUrl); if (apiKey) url.searchParams.append('apiKey', apiKey); return url.toString(); } export function isBase58(base58) { return /^[A-HJ-NP-Za-km-z1-9]+$/.test(base58); } export function recordToMap(record) { // Convert record to an array of [key, value] pairs, then pass to new Map return new Map(Object.entries(record)); } export function mapToRecord(map) { const record = {}; for (const [key, value] of map) { record[key] = value; } return record; } export function transformMap(originalMap, transformFn) { const newMap = new Map(); // `Map.forEach` provides (value, key) originalMap.forEach((value, key) => { newMap.set(key, transformFn(value, key)); }); return newMap; } //# sourceMappingURL=Utils.js.map