Expose Base58 functionality in API.

This commit is contained in:
Stefan Thomas
2013-06-24 13:47:55 +02:00
parent be78db1563
commit 7cb9787424
2 changed files with 29 additions and 7 deletions

View File

@@ -64,6 +64,8 @@ Base.encode = function (input, alpha) {
// --> input: String // --> input: String
// <-- array of bytes or undefined. // <-- array of bytes or undefined.
Base.decode = function (input, alpha) { Base.decode = function (input, alpha) {
if ("string" !== typeof input) return undefined;
var alphabet = alphabets[alpha || 'ripple']; var alphabet = alphabets[alpha || 'ripple'];
var bi_base = new BigInteger(String(alphabet.length)); var bi_base = new BigInteger(String(alphabet.length));
var bi_value = nbi(); var bi_value = nbi();
@@ -105,6 +107,17 @@ Base.decode = function (input, alpha) {
return [].concat(utils.arraySet(zeros, 0), bytes); return [].concat(utils.arraySet(zeros, 0), bytes);
}; };
Base.verify_checksum = function (bytes) {
var computed = sha256hash(bytes.slice(0, -4)).slice(0, 4);
var checksum = bytes.slice(-4);
for (var i = 0; i < 4; i++)
if (computed[i] !== checksum[i])
return false;
return true;
};
// --> input: Array // --> input: Array
// <-- String // <-- String
Base.encode_check = function (version, input, alphabet) { Base.encode_check = function (version, input, alphabet) {
@@ -119,16 +132,24 @@ Base.encode_check = function (version, input, alphabet) {
Base.decode_check = function (version, input, alphabet) { Base.decode_check = function (version, input, alphabet) {
var buffer = Base.decode(input, alphabet); var buffer = Base.decode(input, alphabet);
if (!buffer || buffer[0] !== version || buffer.length < 5) if (!buffer || buffer.length < 5)
return NaN; return NaN;
var computed = sha256hash(buffer.slice(0, -4)).slice(0, 4); // Single valid version
var checksum = buffer.slice(-4); if ("number" === typeof version && buffer[0] !== version)
var i; return NaN;
for (i = 0; i != 4; i += 1) // Multiple allowed versions
if (computed[i] !== checksum[i]) if ("object" === typeof version && Array.isArray(version)) {
return NaN; var match = false;
for (var i = 0, l = version.length; i < l; i++) {
match |= version[i] === buffer[0];
}
if (!match) return NaN;
}
if (!Base.verify_checksum(buffer))
return NaN;
// We'll use the version byte to add a leading zero, this ensures JSBN doesn't // We'll use the version byte to add a leading zero, this ensures JSBN doesn't
// intrepret the value as a negative number // intrepret the value as a negative number

View File

@@ -1,6 +1,7 @@
exports.Remote = require('./remote').Remote; exports.Remote = require('./remote').Remote;
exports.Amount = require('./amount').Amount; exports.Amount = require('./amount').Amount;
exports.Currency = require('./currency').Currency; exports.Currency = require('./currency').Currency;
exports.Base = require('./base').Base;
exports.UInt160 = require('./amount').UInt160; exports.UInt160 = require('./amount').UInt160;
exports.Seed = require('./amount').Seed; exports.Seed = require('./amount').Seed;
exports.Transaction = require('./transaction').Transaction; exports.Transaction = require('./transaction').Transaction;