2013-02-17 00:39:15 -05:00
|
|
|
// https://en.bitcoin.it/wiki/Base58Check_encoding
|
|
|
|
|
|
|
|
var BigInteger = require('./jsbn/jsbn');
|
2014-03-08 13:02:40 +08:00
|
|
|
var Crypto = require('crypto-js');
|
2014-03-11 09:52:48 +08:00
|
|
|
var convert = require('./convert');
|
2014-03-24 06:00:16 +11:00
|
|
|
var SHA256 = Crypto.SHA256;
|
2013-02-17 00:39:15 -05:00
|
|
|
|
|
|
|
var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
|
|
var base = BigInteger.valueOf(58);
|
|
|
|
|
|
|
|
var positions = {};
|
|
|
|
for (var i=0 ; i < alphabet.length ; ++i) {
|
|
|
|
positions[alphabet[i]] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert a byte array to a base58-encoded string.
|
|
|
|
// Written by Mike Hearn for BitcoinJ.
|
|
|
|
// Copyright (c) 2011 Google Inc.
|
|
|
|
// Ported to JavaScript by Stefan Thomas.
|
2014-03-24 15:53:57 +11:00
|
|
|
function encode(input) {
|
2013-02-17 00:39:15 -05:00
|
|
|
var bi = BigInteger.fromByteArrayUnsigned(input);
|
|
|
|
var chars = [];
|
|
|
|
|
|
|
|
while (bi.compareTo(base) >= 0) {
|
|
|
|
var mod = bi.mod(base);
|
|
|
|
chars.push(alphabet[mod.intValue()]);
|
|
|
|
bi = bi.subtract(mod).divide(base);
|
|
|
|
}
|
|
|
|
chars.push(alphabet[bi.intValue()]);
|
|
|
|
|
|
|
|
// Convert leading zeros too.
|
|
|
|
for (var i = 0; i < input.length; i++) {
|
2012-01-11 02:40:45 +01:00
|
|
|
if (input[i] == 0x00) {
|
2013-02-17 00:39:15 -05:00
|
|
|
chars.push(alphabet[0]);
|
2012-01-11 02:40:45 +01:00
|
|
|
} else break;
|
2013-02-17 00:39:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return chars.reverse().join('');
|
2014-03-24 06:01:33 +11:00
|
|
|
}
|
2013-02-17 00:39:15 -05:00
|
|
|
|
|
|
|
// decode a base58 string into a byte array
|
|
|
|
// input should be a base58 encoded string
|
|
|
|
// @return Array
|
2014-03-24 15:53:57 +11:00
|
|
|
function decode(input) {
|
2013-02-17 00:39:15 -05:00
|
|
|
|
|
|
|
var base = BigInteger.valueOf(58);
|
|
|
|
|
|
|
|
var length = input.length;
|
|
|
|
var num = BigInteger.valueOf(0);
|
|
|
|
var leading_zero = 0;
|
|
|
|
var seen_other = false;
|
|
|
|
for (var i=0; i<length ; ++i) {
|
2013-10-07 08:21:00 -04:00
|
|
|
var chr = input[i];
|
|
|
|
var p = positions[chr];
|
2013-02-17 00:39:15 -05:00
|
|
|
|
|
|
|
// if we encounter an invalid character, decoding fails
|
|
|
|
if (p === undefined) {
|
|
|
|
throw new Error('invalid base58 string: ' + input);
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2011-05-04 17:02:56 +01:00
|
|
|
|
2013-02-17 00:39:15 -05:00
|
|
|
num = num.multiply(base).add(BigInteger.valueOf(p));
|
|
|
|
|
2013-10-07 08:21:00 -04:00
|
|
|
if (chr == '1' && !seen_other) {
|
2013-02-17 00:39:15 -05:00
|
|
|
++leading_zero;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
seen_other = true;
|
2012-01-11 02:40:45 +01:00
|
|
|
}
|
2013-02-17 00:39:15 -05:00
|
|
|
}
|
2011-05-04 17:02:56 +01:00
|
|
|
|
2013-02-17 00:39:15 -05:00
|
|
|
var bytes = num.toByteArrayUnsigned();
|
2011-05-04 17:02:56 +01:00
|
|
|
|
2013-02-17 00:39:15 -05:00
|
|
|
// remove leading zeros
|
|
|
|
while (leading_zero-- > 0) {
|
|
|
|
bytes.unshift(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
2011-09-26 21:28:13 +01:00
|
|
|
|
2014-03-24 15:53:57 +11:00
|
|
|
function checkEncode(input, vbyte) {
|
2013-10-07 08:21:00 -04:00
|
|
|
vbyte = vbyte || 0;
|
2014-03-08 13:02:40 +08:00
|
|
|
var front = [vbyte].concat(input)
|
2014-03-24 15:53:57 +11:00
|
|
|
return encode(front.concat(getChecksum(front)));
|
2013-10-07 08:21:00 -04:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:53:57 +11:00
|
|
|
function checkDecode(input) {
|
|
|
|
var bytes = decode(input),
|
2013-10-07 08:21:00 -04:00
|
|
|
front = bytes.slice(0,bytes.length-4),
|
|
|
|
back = bytes.slice(bytes.length-4);
|
2014-03-24 06:01:33 +11:00
|
|
|
|
2014-03-08 13:02:40 +08:00
|
|
|
var checksum = getChecksum(front)
|
2014-03-24 06:01:33 +11:00
|
|
|
|
2013-10-07 08:21:00 -04:00
|
|
|
if (""+checksum != ""+back) {
|
|
|
|
throw new Error("Checksum failed");
|
|
|
|
}
|
2014-03-24 06:01:33 +11:00
|
|
|
|
2013-10-07 08:21:00 -04:00
|
|
|
var o = front.slice(1);
|
|
|
|
o.version = front[0];
|
|
|
|
return o;
|
|
|
|
}
|
2014-03-08 13:02:40 +08:00
|
|
|
|
|
|
|
function getChecksum(bytes) {
|
2014-03-11 09:52:48 +08:00
|
|
|
var wordArray = convert.bytesToWordArray(bytes)
|
|
|
|
return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4);
|
2014-03-08 13:02:40 +08:00
|
|
|
}
|
|
|
|
|
2014-03-24 15:53:57 +11:00
|
|
|
module.exports = {
|
|
|
|
encode: encode,
|
|
|
|
decode: decode,
|
|
|
|
checkEncode: checkEncode,
|
|
|
|
checkDecode: checkDecode,
|
|
|
|
getChecksum: getChecksum
|
|
|
|
}
|