diff --git a/src/address.js b/src/address.js index a4e6a63..a4d5168 100644 --- a/src/address.js +++ b/src/address.js @@ -14,7 +14,7 @@ var Address = function (bytes, version) { this.hash = bytes.length <= 35 ? base58.checkDecode(bytes) : bytes.length <= 40 ? conv.hexToBytes(bytes) - : util.error('Bad input'); + : util.error('invalid or unrecognized input'); this.version = version || this.hash.version || mainnet; } @@ -33,10 +33,6 @@ Address.prototype.toString = function () { return base58.checkEncode(this.hash.slice(0), this.version); }; -Address.prototype.getHash = function () { - return conv.bytesToHex(this.hash); -}; - Address.getVersion = function(string) { return base58.decode(string)[0]; } @@ -50,11 +46,4 @@ Address.validate = function(string) { } }; -/** - * Parse a Bitcoin address contained in a string. - */ -Address.decodeString = function (string) { - return base58.checkDecode(string); -}; - module.exports = Address; diff --git a/test/address.js b/test/address.js index 6ea05b0..9dc50bc 100644 --- a/test/address.js +++ b/test/address.js @@ -1,13 +1,14 @@ /* global describe, it */ -var assert = require('assert'); -var Address = require('../src/address.js'); -var network = require('../src/network.js'); +var assert = require('assert') +var Address = require('../src/address.js') +var network = require('../src/network.js') +var base58 = require('../src/base58.js') var mainnet = network.mainnet.addressVersion var testnet = network.testnet.addressVersion describe('Address', function() { var testnetAddress, mainnetAddress; - var testnetP2shAddress, mainnetP2shAddress; + var testnetP2shAddress, mainnetP2shAddress beforeEach(function(){ testnetAddress = 'mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef' @@ -16,6 +17,49 @@ describe('Address', function() { mainnetP2shAddress = '3NJZLcZEEYBpxYEUGewU4knsQRn1WM5Fkt' }) + describe('parsing', function() { + it('works with Address object', function() { + var addr = new Address(new Address('mwrB4fgT1KSBCqELaWv7o7tsExuQzW3NY3', network.testnet.addressVersion)) + + assert.equal(addr.toString(), 'mwrB4fgT1KSBCqELaWv7o7tsExuQzW3NY3') + assert.equal(addr.version, network.testnet.addressVersion) + }) + + it('works with hex', function() { + var addr = new Address('13483382d3c3d43fc9d7b52e652b6bbb70e8b667') + assert.equal(addr.toString(), '12kxLGqrnnchwN9bHHNV2fWDtJGwxKTcJS') + }) + + it('throws error for invalid or unrecognized input', function() { + var addr = + assert.throws( + function() { + new Address('beepboopbeepboopbeepboopbeepboopbeepboopbeep') + }, + Error + ) + }) + + it('works for byte input', function() { + var hash = base58.checkDecode('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa') + var addr = new Address(hash) + assert.equal(addr.hash, hash) + assert.equal(network.mainnet.addressVersion, hash.version) + + var hash = base58.checkDecode('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef') + var addr = new Address(hash) + assert.equal(addr.hash, hash) + assert.equal(network.testnet.addressVersion, hash.version) + }) + }) + + describe('getVersion', function() { + it('returns the proper address version', function() { + assert.equal(Address.getVersion('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'), network.mainnet.addressVersion) + assert.equal(Address.getVersion('mzBc4XEFSdzCDcTxAgf6EZXgsZWpztRhef'), network.testnet.addressVersion) + }) + }) + describe('toString', function() { it('defaults to base58', function() { var addr = '18fN1QTGWmHWCA9r2dyDH6FbMEyc7XHmQQ';