2014-08-06 18:25:45 -07:00
|
|
|
var should = require('chai').should();
|
2014-08-14 09:48:41 -07:00
|
|
|
var Base58Check = require('../lib/base58check');
|
2014-08-06 18:25:45 -07:00
|
|
|
var base58 = require('../lib/base58');
|
|
|
|
|
2014-08-14 09:36:35 -07:00
|
|
|
describe('Base58Check', function() {
|
2014-08-06 18:25:45 -07:00
|
|
|
var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);
|
|
|
|
var enc = "14HV44ipwoaqfg";
|
|
|
|
|
2014-08-14 08:56:17 -07:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2014-08-24 14:51:55 -07:00
|
|
|
describe('@encode', function() {
|
2014-08-06 18:25:45 -07:00
|
|
|
|
|
|
|
it('should encode the buffer accurately', function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.encode(buf).should.equal(enc);
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the input is not a buffer', function() {
|
|
|
|
(function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.encode("string")
|
2014-08-20 13:03:07 -07:00
|
|
|
}).should.throw('Input must be a buffer');
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-24 14:51:55 -07:00
|
|
|
describe('@decode', function() {
|
2014-08-06 18:25:45 -07:00
|
|
|
|
|
|
|
it('should decode this encoded value correctly', function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.decode(enc).toString('hex').should.equal(buf.toString('hex'));
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when input is not a string', function() {
|
|
|
|
(function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.decode(5);
|
2014-08-20 13:03:07 -07:00
|
|
|
}).should.throw('Input must be a string');
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when input is too short', function() {
|
|
|
|
(function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.decode(enc.slice(0, 1));
|
2014-08-20 13:03:07 -07:00
|
|
|
}).should.throw('Input string too short');
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when there is a checksum mismatch', function() {
|
|
|
|
var buf2 = base58.decode(enc);
|
|
|
|
buf2[0] = buf2[0] + 1;
|
|
|
|
var enc2 = base58.encode(buf2);
|
|
|
|
(function() {
|
2014-08-14 08:56:17 -07:00
|
|
|
Base58Check.decode(enc2);
|
2014-08-20 13:03:07 -07:00
|
|
|
}).should.throw('Checksum mismatch');
|
2014-08-14 08:56:17 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2014-08-06 18:25:45 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|