bitcoinjs-lib/src/base58.js
2014-05-29 14:43:29 +10:00

73 lines
1.7 KiB
JavaScript

// 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 assert = require('assert')
var BigInteger = require('bigi')
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var ALPHABET_BUF = new Buffer(ALPHABET, 'ascii')
var ALPHABET_MAP = {}
for(var i = 0; i < ALPHABET.length; i++) {
ALPHABET_MAP[ALPHABET[i]] = BigInteger.valueOf(i)
}
var BASE = BigInteger.valueOf(58)
function encode(buffer) {
var bi = BigInteger.fromBuffer(buffer)
var result = new Buffer(buffer.length << 1)
var i = result.length - 1
while (bi.compareTo(BigInteger.ZERO) > 0) {
var remainder = bi.mod(BASE)
bi = bi.divide(BASE)
result[i] = ALPHABET_BUF[remainder.intValue()]
i--
}
// deal with leading zeros
var j = 0
while (buffer[j] === 0) {
result[i] = ALPHABET_BUF[0]
j++
i--
}
return result.slice(i + 1, result.length).toString('ascii')
}
function decode(string) {
if (string.length === 0) return new Buffer(0)
var num = BigInteger.ZERO.clone()
for (var i = 0; i < string.length; i++) {
num = num.multiply(BASE)
var figure = ALPHABET_MAP[string.charAt(i)]
assert.notEqual(figure, undefined, 'Non-base58 character')
num = num.add(figure)
}
// deal with leading zeros
var i = 0
while ((i < string.length) && (string[i] === ALPHABET[0])) {
i++
}
var buffer = num.toBuffer()
var leadz = new Buffer(i)
leadz.fill(0)
return Buffer.concat([leadz, buffer])
}
module.exports = {
encode: encode,
decode: decode
}