Merge pull request #85 from Dcousens/convertcleanup

Convert.js/Base58.js Cleanup
This commit is contained in:
Kyle Drake 2014-03-24 11:30:27 -07:00
commit 7686ac122e
2 changed files with 161 additions and 135 deletions

View file

@ -10,39 +10,39 @@ var base = BigInteger.valueOf(58);
var positions = {}; var positions = {};
for (var i=0 ; i < alphabet.length ; ++i) { for (var i=0 ; i < alphabet.length ; ++i) {
positions[alphabet[i]] = i; positions[alphabet[i]] = i;
} }
// Convert a byte array to a base58-encoded string. // Convert a byte array to a base58-encoded string.
// Written by Mike Hearn for BitcoinJ. // Written by Mike Hearn for BitcoinJ.
// Copyright (c) 2011 Google Inc. // Copyright (c) 2011 Google Inc.
// Ported to JavaScript by Stefan Thomas. // Ported to JavaScript by Stefan Thomas.
module.exports.encode = function (input) { function encode(input) {
var bi = BigInteger.fromByteArrayUnsigned(input); var bi = BigInteger.fromByteArrayUnsigned(input);
var chars = []; var chars = [];
while (bi.compareTo(base) >= 0) { while (bi.compareTo(base) >= 0) {
var mod = bi.mod(base); var mod = bi.mod(base);
chars.push(alphabet[mod.intValue()]); chars.push(alphabet[mod.intValue()]);
bi = bi.subtract(mod).divide(base); bi = bi.subtract(mod).divide(base);
} }
chars.push(alphabet[bi.intValue()]);
// Convert leading zeros too. chars.push(alphabet[bi.intValue()]);
for (var i = 0; i < input.length; i++) {
if (input[i] == 0x00) {
chars.push(alphabet[0]);
} else break;
}
return chars.reverse().join(''); // Convert leading zeros too.
for (var i = 0; i < input.length; i++) {
if (input[i] == 0x00) {
chars.push(alphabet[0]);
} else break;
}
return chars.reverse().join('');
} }
// decode a base58 string into a byte array // decode a base58 string into a byte array
// input should be a base58 encoded string // input should be a base58 encoded string
// @return Array // @return Array
module.exports.decode = function (input) { function decode(input) {
var base = BigInteger.valueOf(58); var base = BigInteger.valueOf(58);
var length = input.length; var length = input.length;
@ -50,54 +50,55 @@ module.exports.decode = function (input) {
var leading_zero = 0; var leading_zero = 0;
var seen_other = false; var seen_other = false;
for (var i=0; i<length ; ++i) { for (var i=0; i<length ; ++i) {
var chr = input[i]; var chr = input[i];
var p = positions[chr]; var p = positions[chr];
// if we encounter an invalid character, decoding fails // if we encounter an invalid character, decoding fails
if (p === undefined) { if (p === undefined) {
throw new Error('invalid base58 string: ' + input); throw new Error('invalid base58 string: ' + input);
} }
num = num.multiply(base).add(BigInteger.valueOf(p)); num = num.multiply(base).add(BigInteger.valueOf(p));
if (chr == '1' && !seen_other) { if (chr == '1' && !seen_other) {
++leading_zero; ++leading_zero;
} }
else { else {
seen_other = true; seen_other = true;
} }
} }
var bytes = num.toByteArrayUnsigned(); var bytes = num.toByteArrayUnsigned();
// remove leading zeros // remove leading zeros
while (leading_zero-- > 0) { while (leading_zero-- > 0) {
bytes.unshift(0); bytes.unshift(0);
} }
return bytes; return bytes;
} }
module.exports.checkEncode = function(input, vbyte) { function checkEncode(input, vbyte) {
vbyte = vbyte || 0; vbyte = vbyte || 0;
var front = [vbyte].concat(input)
return module.exports.encode(front.concat(getChecksum(front))); var front = [vbyte].concat(input);
return encode(front.concat(getChecksum(front)));
} }
module.exports.checkDecode = function(input) { function checkDecode(input) {
var bytes = module.exports.decode(input), var bytes = decode(input),
front = bytes.slice(0,bytes.length-4), front = bytes.slice(0,bytes.length-4),
back = bytes.slice(bytes.length-4); back = bytes.slice(bytes.length-4);
var checksum = getChecksum(front) var checksum = getChecksum(front);
if (""+checksum != ""+back) { if ("" + checksum != "" + back) {
throw new Error("Checksum failed"); throw new Error("Checksum failed");
} }
var o = front.slice(1); var o = front.slice(1);
o.version = front[0]; o.version = front[0];
return o; return o;
} }
function getChecksum(bytes) { function getChecksum(bytes) {
@ -105,4 +106,10 @@ function getChecksum(bytes) {
return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4); return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4);
} }
module.exports.getChecksum = getChecksum module.exports = {
encode: encode,
decode: decode,
checkEncode: checkEncode,
checkDecode: checkDecode,
getChecksum: getChecksum
}

View file

@ -2,122 +2,122 @@ var Crypto = require('crypto-js');
var WordArray = Crypto.lib.WordArray; var WordArray = Crypto.lib.WordArray;
var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; var base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
exports.lpad = function lpad(str, padString, length) { function lpad(str, padString, length) {
while (str.length < length) str = padString + str; while (str.length < length) str = padString + str;
return str; return str;
} }
/** /**
* Convert a byte array to a hex string * Convert a byte array to a hex string
*/ */
exports.bytesToHex = function(bytes) { function bytesToHex(bytes) {
return bytes.map(function(x) { return bytes.map(function(x) {
return exports.lpad(x.toString(16), '0', 2) return lpad(x.toString(16), '0', 2)
}).join(''); }).join('');
}; };
/** /**
* Convert a hex string to a byte array * Convert a hex string to a byte array
*/ */
exports.hexToBytes = function(hex) { function hexToBytes(hex) {
return hex.match(/../g).map(function(x) { return hex.match(/../g).map(function(x) {
return parseInt(x,16) return parseInt(x,16)
}); });
} }
/** /**
* Convert a byte array to a base-64 string * Convert a byte array to a base-64 string
*/ */
exports.bytesToBase64 = function(bytes) { function bytesToBase64(bytes) {
var base64 = [] var base64 = []
for (var i = 0; i < bytes.length; i += 3) { for (var i = 0; i < bytes.length; i += 3) {
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
for (var j = 0; j < 4; j++) { for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 <= bytes.length * 8) { if (i * 8 + j * 6 <= bytes.length * 8) {
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F)); base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
} else { } else {
base64.push('='); base64.push('=');
} }
}
} }
}
return base64.join(''); return base64.join('');
} }
/** /**
* Convert a base-64 string to a byte array * Convert a base-64 string to a byte array
*/ */
exports.base64ToBytes = function(base64) { function base64ToBytes(base64) {
// Remove non-base-64 characters // Remove non-base-64 characters
base64 = base64.replace(/[^A-Z0-9+\/]/ig, ''); base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
var bytes = []; var bytes = [];
var imod4 = 0; var imod4 = 0;
for (var i = 0; i < base64.length; imod4 = ++i % 4) { for (var i = 0; i < base64.length; imod4 = ++i % 4) {
if (!imod4) continue if (!imod4) continue
bytes.push( bytes.push(
( (
(base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) <<
(imod4 * 2) (imod4 * 2)
) | ) |
(base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)) (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))
); );
} }
return bytes; return bytes;
} }
/** /**
* Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85) * Hex only (allowing bin would be potentially risky, as 01010101 = \x01 * 4 or 85)
*/ */
exports.coerceToBytes = function(input) { function coerceToBytes(input) {
if (typeof input != 'string') return input if (typeof input != 'string') return input
return exports.hexToBytes(input); return hexToBytes(input);
} }
exports.binToBytes = function(bin) { function binToBytes(bin) {
return bin.match(/......../g).map(function(x) { return bin.match(/......../g).map(function(x) {
return parseInt(x,2) return parseInt(x,2)
}); });
} }
exports.bytesToBin = function(bytes) { function bytesToBin(bytes) {
return bytes.map(function(x) { return bytes.map(function(x) {
return exports.lpad(x.toString(2), '0', 8) return lpad(x.toString(2), '0', 8)
}).join(''); }).join('');
} }
exports.bytesToString = function(bytes) { function bytesToString(bytes) {
return bytes.map(function(x){ return bytes.map(function(x){
return String.fromCharCode(x) return String.fromCharCode(x)
}).join(''); }).join('');
} }
exports.stringToBytes = function(string) { function stringToBytes(string) {
return string.split('').map(function(x) { return string.split('').map(function(x) {
return x.charCodeAt(0) return x.charCodeAt(0)
}); });
} }
/** /**
* Create a byte array representing a number with the given length * Create a byte array representing a number with the given length
*/ */
exports.numToBytes = function(num, bytes) { function numToBytes(num, bytes) {
if (bytes === undefined) bytes = 8; if (bytes === undefined) bytes = 8;
if (bytes === 0) return []; if (bytes === 0) return [];
return [num % 256].concat(module.exports.numToBytes(Math.floor(num / 256), bytes - 1)); return [num % 256].concat(numToBytes(Math.floor(num / 256), bytes - 1));
} }
/** /**
* Convert a byte array to the number that it represents * Convert a byte array to the number that it represents
*/ */
exports.bytesToNum = function(bytes) { function bytesToNum(bytes) {
if (bytes.length === 0) return 0; if (bytes.length === 0) return 0;
return bytes[0] + 256 * module.exports.bytesToNum(bytes.slice(1)); return bytes[0] + 256 * bytesToNum(bytes.slice(1));
} }
/** /**
@ -127,34 +127,53 @@ exports.bytesToNum = function(bytes) {
* *
* Returns a byte array. * Returns a byte array.
*/ */
exports.numToVarInt = function(num) { function numToVarInt(num) {
if (num < 253) return [num]; if (num < 253) return [num];
if (num < 65536) return [253].concat(exports.numToBytes(num, 2)); if (num < 65536) return [253].concat(numToBytes(num, 2));
if (num < 4294967296) return [254].concat(exports.numToBytes(num, 4)); if (num < 4294967296) return [254].concat(numToBytes(num, 4));
return [253].concat(exports.numToBytes(num, 8)); return [253].concat(numToBytes(num, 8));
} }
exports.bytesToWords = function (bytes) { function bytesToWords(bytes) {
var words = []; var words = [];
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
words[b >>> 5] |= bytes[i] << (24 - b % 32); words[b >>> 5] |= bytes[i] << (24 - b % 32);
} }
return words; return words;
} }
exports.wordsToBytes = function (words) { function wordsToBytes(words) {
var bytes = []; var bytes = [];
for (var b = 0; b < words.length * 32; b += 8) { for (var b = 0; b < words.length * 32; b += 8) {
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
} }
return bytes; return bytes;
} }
exports.bytesToWordArray = function (bytes) { function bytesToWordArray(bytes) {
return new WordArray.init(exports.bytesToWords(bytes), bytes.length) return new WordArray.init(bytesToWords(bytes), bytes.length)
} }
exports.wordArrayToBytes = function (wordArray) { function wordArrayToBytes(wordArray) {
return exports.wordsToBytes(wordArray.words) return wordsToBytes(wordArray.words)
} }
module.exports = {
lpad: lpad,
bytesToHex: bytesToHex,
hexToBytes: hexToBytes,
bytesToBase64: bytesToBase64,
base64ToBytes: base64ToBytes,
coerceToBytes: coerceToBytes,
binToBytes: binToBytes,
bytesToBin: bytesToBin,
bytesToString: bytesToString,
stringToBytes: stringToBytes,
numToBytes: numToBytes,
bytesToNum: bytesToNum,
numToVarInt: numToVarInt,
bytesToWords: bytesToWords,
wordsToBytes: wordsToBytes,
bytesToWordArray: bytesToWordArray,
wordArrayToBytes: wordArrayToBytes
}