// https://en.bitcoin.it/wiki/Base58Check_encoding var BigInteger = require('./jsbn/jsbn') var Crypto = require('crypto-js') var convert = require('./convert') var SHA256 = Crypto.SHA256 var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" var base = BigInteger.valueOf(58) var positions = {} for (var i=0; i= 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 0) { bytes.unshift(0) } return bytes } function checkEncode(input, vbyte) { vbyte = vbyte || 0 var front = [vbyte].concat(input) return encode(front.concat(getChecksum(front))) } function checkDecode(input) { var bytes = decode(input), front = bytes.slice(0, bytes.length-4), back = bytes.slice(bytes.length-4) var checksum = getChecksum(front) if ("" + checksum != "" + back) { throw new Error("Checksum failed") } var o = front.slice(1) o.version = front[0] return o } function getChecksum(bytes) { var wordArray = convert.bytesToWordArray(bytes) return convert.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0, 4) } module.exports = { encode: encode, decode: decode, checkEncode: checkEncode, checkDecode: checkDecode, getChecksum: getChecksum }