UNPKG

@node-dlc/bitcoin

Version:
62 lines 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Base58 = exports.divmod = void 0; const bufio_1 = require("@node-dlc/bufio"); /** * Returns a tuple containing the quotient and remainder when the divident (num) is * divided by the divisor (mod). * @param num divident * @param mod divisor */ function divmod(num, mod) { return [num / mod, num % mod]; } exports.divmod = divmod; /** * Base58 encoding and decoding utility */ class Base58 { /** * Encodes a buffer into a base58 string */ static encode(buf) { let n = BigInt('0x' + buf.toString('hex')); // encode into base58 let str = ''; while (n > BigInt(0)) { const mod = n % BigInt(58); str = Base58.alphabet[Number(mod)] + str; n = n / BigInt(58); } // add leading zeros for (const val of buf) { if (val !== 0) break; str = '1' + str; } return str; } /** * Decodes the base58 string into a buffer */ static decode(str) { // determine leading zero bytes which will be prepended let prefix = 0; for (let i = 0; i < str.length; i++) { if (str[i] === '1') prefix += 1; else break; } // decode from base58 let n = BigInt(0); for (const char of str) { n *= BigInt(58); n += BigInt(Base58.alphabet.indexOf(char)); } return Buffer.concat([Buffer.alloc(prefix), (0, bufio_1.bigToBufBE)(n)]); } } exports.Base58 = Base58; Base58.alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; //# sourceMappingURL=Base58.js.map