UNPKG

@hdwallet/core

Version:

A complete Hierarchical Deterministic (HD) Wallet generator for 200+ cryptocurrencies, built with TypeScript.

34 lines 1.32 kB
// SPDX-License-Identifier: MIT // @ts-ignore: no declaration file for 'base32.js' import * as base32 from 'base32.js'; import { hexToBytes, bytesToHex } from '../utils'; const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'; const translate = (s, from, to) => s.split('') .map(c => { const i = from.indexOf(c); return i === -1 ? c : to[i]; }) .join(''); const pad = (s) => (s.length % 8 ? s + '='.repeat(8 - (s.length % 8)) : s); export function encode(hex, customAlphabet) { const bytes = hexToBytes(hex); const encoder = new base32.Encoder({ type: 'rfc4648', alphabet: ALPHABET }); const b32 = encoder.write(bytes).finalize().toUpperCase(); // base32.js returns lower return customAlphabet ? translate(b32, ALPHABET, customAlphabet) : b32; } export const encodeNoPadding = (hex, alpha) => encode(hex, alpha).replace(/=+$/, ''); export function decode(data, customAlphabet) { try { let inp = pad(data); if (customAlphabet) { inp = translate(inp, customAlphabet, ALPHABET); } const dec = new base32.Decoder({ type: 'rfc4648', alphabet: ALPHABET }); const bytes = dec.write(inp).finalize(); return bytesToHex(bytes); } catch { throw new Error('Invalid Base32 string'); } } //# sourceMappingURL=base32.js.map