give Base58Check the same fromString toString

...interface like the rest of the classes
This commit is contained in:
Ryan X. Charles 2014-08-14 11:56:17 -04:00
parent 9cc214cf06
commit de08f78d74
3 changed files with 100 additions and 26 deletions

View File

@ -25,7 +25,7 @@ bitcore.expmt.Stealth = require('./lib/expmt/stealth');
bitcore.deps = {};
bitcore.deps.bnjs = require('bn.js');
bitcore.deps.bs58 = require('bs58');
bitcore.deps.buffer = Buffer;
bitcore.deps.Buffer = Buffer;
bitcore.deps.elliptic = require('elliptic');
bitcore.deps.hashjs = require('hash.js');
bitcore.deps.sha512 = require('sha512');

View File

@ -1,26 +1,20 @@
var base58 = require('./base58');
var sha256sha256 = require('./hash').sha256sha256;
var base58check = module.exports;
base58check.encode = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('base58check: Input must be a buffer');
var checkedBuf = new Buffer(buf.length + 4);
var hash = sha256sha256(buf);
buf.copy(checkedBuf);
hash.copy(checkedBuf, buf.length);
return base58.encode(checkedBuf);
var Base58Check = function Base58Check(buf) {
if (!(this instanceof Base58Check))
return new Base58Check(buf);
this.buf = buf;
};
base58check.decode = function(s) {
Base58Check.decode = function(s) {
if (typeof s !== 'string')
throw new Error('base58check: Input must be a string');
throw new Error('Base58Check: Input must be a string');
var buf = base58.decode(s);
if (buf.length < 4)
throw new Error("base58check: Input string too short");
throw new Error("Base58Check: Input string too short");
var data = buf.slice(0, -4);
var csum = buf.slice(-4);
@ -29,7 +23,38 @@ base58check.decode = function(s) {
var hash4 = hash.slice(0, 4);
if (csum.toString('hex') !== hash4.toString('hex'))
throw new Error("base58check: Checksum mismatch");
throw new Error("Base58Check: Checksum mismatch");
return data;
};
Base58Check.encode = function(buf) {
if (!Buffer.isBuffer(buf))
throw new Error('Base58Check: Input must be a buffer');
var checkedBuf = new Buffer(buf.length + 4);
var hash = sha256sha256(buf);
buf.copy(checkedBuf);
hash.copy(checkedBuf, buf.length);
return base58.encode(checkedBuf);
};
Base58Check.prototype.fromBuffer = function(buf) {
this.buf = buf;
return this;
};
Base58Check.prototype.fromString = function(str) {
var buf = Base58Check.decode(str);
this.buf = buf;
return this;
};
Base58Check.prototype.toBuffer = function() {
return this.buf;
};
Base58Check.prototype.toString = function() {
return Base58Check.encode(this.buf);
};
module.exports = Base58Check;

View File

@ -1,21 +1,31 @@
var should = require('chai').should();
var base58check = require('../lib/base58check');
var Base58Check = require('../lib/Base58Check');
var base58 = require('../lib/base58');
describe('Base58check', function() {
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);
var enc = "14HV44ipwoaqfg";
it('should make an instance with "new"', function() {
var b58 = new Base58Check();
should.exist(b58);
});
it('should make an instance without "new"', function() {
var b58 = Base58Check();
should.exist(b58);
});
describe('#encode', function() {
it('should encode the buffer accurately', function() {
base58check.encode(buf).should.equal(enc);
Base58Check.encode(buf).should.equal(enc);
});
it('should throw an error when the input is not a buffer', function() {
(function() {
base58check.encode("string")
}).should.throw('base58check: Input must be a buffer');
Base58Check.encode("string")
}).should.throw('Base58Check: Input must be a buffer');
});
});
@ -23,19 +33,19 @@ describe('Base58check', function() {
describe('#decode', function() {
it('should decode this encoded value correctly', function() {
base58check.decode(enc).toString('hex').should.equal(buf.toString('hex'));
Base58Check.decode(enc).toString('hex').should.equal(buf.toString('hex'));
});
it('should throw an error when input is not a string', function() {
(function() {
base58check.decode(5);
}).should.throw('base58check: Input must be a string');
Base58Check.decode(5);
}).should.throw('Base58Check: Input must be a string');
});
it('should throw an error when input is too short', function() {
(function() {
base58check.decode(enc.slice(0, 1));
}).should.throw('base58check: Input string too short');
Base58Check.decode(enc.slice(0, 1));
}).should.throw('Base58Check: Input string too short');
});
it('should throw an error when there is a checksum mismatch', function() {
@ -43,8 +53,47 @@ describe('Base58check', function() {
buf2[0] = buf2[0] + 1;
var enc2 = base58.encode(buf2);
(function() {
base58check.decode(enc2);
}).should.throw('base58check: Checksum mismatch');
Base58Check.decode(enc2);
}).should.throw('Base58Check: Checksum mismatch');
});
});
describe('#fromBuffer', function() {
it('should not fail', function() {
should.exist(Base58Check().fromBuffer(buf));
});
it('should set buffer', function() {
var b58 = Base58Check().fromBuffer(buf);
b58.buf.toString('hex').should.equal(buf.toString('hex'));
});
});
describe('#fromString', function() {
it('should convert this known string to a buffer', function() {
Base58Check().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'));
});
});
describe('#toBuffer', function() {
it('should return the buffer', function() {
var b58 = Base58Check(buf);
b58.buf.toString('hex').should.equal(buf.toString('hex'));
});
});
describe('#toString', function() {
it('should return the buffer', function() {
var b58 = Base58Check(buf);
b58.toString().should.equal(enc);
});
});