UNPKG

multichain-address-validator

Version:

Multichain address validator for Bitcoin and other blockchains.

47 lines (39 loc) 1.37 kB
// Base58 encoding/decoding // Originally written by Mike Hearn for BitcoinJ // Copyright (c) 2011 Google Inc // Ported to JavaScript by Stefan Thomas // Merged Buffer refactorings from base58-native by Stephen Pair // Copyright (c) 2013 BitPay Inc const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; const ALPHABET_MAP = {}; for (let i = 0; i < ALPHABET.length; ++i) { ALPHABET_MAP[ALPHABET.charAt(i)] = i; } const BASE = ALPHABET.length; export default { decode: function(string: string) { if (string.length === 0) return []; var i, j, bytes = [0]; for (i = 0; i < string.length; ++i) { var c = string[i]; if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character'); for (j = 0; j < bytes.length; ++j) bytes[j] *= BASE bytes[0] += ALPHABET_MAP[c]; var carry = 0; for (j = 0; j < bytes.length; ++j) { bytes[j] += carry; carry = bytes[j] >> 8; bytes[j] &= 0xff } while (carry) { bytes.push(carry & 0xff); carry >>= 8; } } // deal with leading zeros for (i = 0; string[i] === '1' && i < string.length - 1; ++i){ bytes.push(0); } return bytes.reverse(); } };