diff --git a/lib/base58.js b/lib/base58.js index e7516a7..1bb24f8 100644 --- a/lib/base58.js +++ b/lib/base58.js @@ -1,2 +1,40 @@ var bs58 = require('bs58'); -module.exports = bs58; + +var Base58 = function Base58(buf) { + if (!(this instanceof Base58)) + return new Base58(buf); + this.buf = buf; +}; + +Base58.encode = function(buf) { + if (!Buffer.isBuffer(buf)) + throw new Error('Input should be a buffer'); + return bs58.encode(buf); +}; + +Base58.decode = function(str) { + if (typeof str !== 'string') + throw new Error('Input should be a string'); + return bs58.decode(str); +}; + +Base58.prototype.fromBuffer = function(buf) { + this.buf = buf; + return this; +}; + +Base58.prototype.fromString = function(str) { + var buf = Base58.decode(str); + this.buf = buf; + return this; +}; + +Base58.prototype.toBuffer = function() { + return this.buf; +}; + +Base58.prototype.toString = function() { + return Base58.encode(this.buf); +}; + +module.exports = Base58; diff --git a/test/test.base58.js b/test/test.base58.js new file mode 100644 index 0000000..d05b944 --- /dev/null +++ b/test/test.base58.js @@ -0,0 +1,85 @@ +var Base58 = require('../lib/base58'); +var should = require('chai').should(); + +describe('Base58', function() { + var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]); + var enc = "1W7N4RuG"; + + it('should make an instance with "new"', function() { + var b58 = new Base58(); + should.exist(b58); + }); + + it('should make an instance without "new"', function() { + var b58 = Base58(); + should.exist(b58); + }); + + describe('#encode', function() { + + it('should encode the buffer accurately', function() { + Base58.encode(buf).should.equal(enc); + }); + + it('should throw an error when the Input is not a buffer', function() { + (function() { + Base58.encode("string") + }).should.throw('Input should be a buffer'); + }); + + }); + + describe('#decode', function() { + + it('should decode this encoded value correctly', function() { + Base58.decode(enc).toString('hex').should.equal(buf.toString('hex')); + }); + + it('should throw an error when Input is not a string', function() { + (function() { + Base58.decode(5); + }).should.throw('Input should be a string'); + }); + + }); + + describe('#fromBuffer', function() { + + it('should not fail', function() { + should.exist(Base58().fromBuffer(buf)); + }); + + it('should set buffer', function() { + var b58 = Base58().fromBuffer(buf); + b58.buf.toString('hex').should.equal(buf.toString('hex')); + }); + + }); + + describe('#fromString', function() { + + it('should convert this known string to a buffer', function() { + Base58().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex')); + }); + + }); + + describe('#toBuffer', function() { + + it('should return the buffer', function() { + var b58 = Base58(buf); + b58.buf.toString('hex').should.equal(buf.toString('hex')); + }); + + }); + + describe('#toString', function() { + + it('should return the buffer', function() { + var b58 = Base58(buf); + b58.toString().should.equal(enc); + }); + + }); + +});