UNPKG

chaingate

Version:

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

70 lines 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.remove0x = remove0x; exports.isHex = isHex; exports.hexToBytes = hexToBytes; exports.bytesToHex = bytesToHex; exports.generateSecureRandomBytes = generateSecureRandomBytes; exports.buildUrlWithApiKey = buildUrlWithApiKey; exports.isBase58 = isBase58; exports.recordToMap = recordToMap; exports.mapToRecord = mapToRecord; exports.transformMap = transformMap; function remove0x(s) { return s.startsWith('0x') ? s.substring(2) : s; } function isHex(s) { s = remove0x(s); return /\b(0x)?[0-9a-fA-F]+\b/.test(s); } 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; } function bytesToHex(bytes, use0x) { return ((use0x ? '0x' : '') + Array.from(bytes) .map((b) => b.toString(16).padStart(2, '0')) .join('')); } function generateSecureRandomBytes(length) { const randomBytes = new Uint8Array(length); crypto.getRandomValues(randomBytes); return randomBytes; } function buildUrlWithApiKey(baseUrl, apiKey) { const url = new URL(baseUrl); if (apiKey) url.searchParams.append('apiKey', apiKey); return url.toString(); } function isBase58(base58) { return /^[A-HJ-NP-Za-km-z1-9]+$/.test(base58); } function recordToMap(record) { // Convert record to an array of [key, value] pairs, then pass to new Map return new Map(Object.entries(record)); } function mapToRecord(map) { const record = {}; for (const [key, value] of map) { record[key] = value; } return record; } 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