all files / src/vendor/ bs58.js

96.43% Statements 27/28
83.33% Branches 5/6
100% Functions 1/1
100% Lines 24/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                  1× 1× 1× 58×   1×   1× 8×   8× 8× 341× 341×   5892× 340×   340× 340× 5892×   5892× 5892×     340× 238×   238×         7×   7×     1×  
// vendored from http://cryptocoinjs.com/modules/misc/bs58/
 
// 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
 
var ALPHABET = 'gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz';
var ALPHABET_MAP = {};
for(var i = 0; i < ALPHABET.length; ++i) {
  ALPHABET_MAP[ALPHABET.charAt(i)] = i;
}
var BASE = 58;
 
function decode(string) {
  Iif (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] === 'g' && i < string.length - 1; ++i) bytes.push(0);
 
  return bytes.reverse();
}
 
module.exports = {decode};