diff --git a/lib/address.js b/lib/address.js index 5a066ee..6103098 100644 --- a/lib/address.js +++ b/lib/address.js @@ -51,6 +51,15 @@ Address.prototype.fromBuffer = function(buf) { return this; }; +Address.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { + if (hashbuf.length !== 20) + throw new Error('hashbuf must be exactly 20 bytes'); + this.hashbuf = hashbuf; + this.networkstr = networkstr || 'mainnet'; + this.typestr = typestr || 'pubkeyhash'; + return this; +}; + Address.prototype.fromPubkey = function(pubkey, networkstr) { this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer()); this.networkstr = networkstr || 'mainnet'; diff --git a/test/address.js b/test/address.js index e1bacf0..7a95fc8 100644 --- a/test/address.js +++ b/test/address.js @@ -38,6 +38,23 @@ describe('Address', function() { }); + describe('#fromHashbuf', function() { + + it('should make an address from a hashbuf', function() { + Address().fromHashbuf(pubkeyhash).toString().should.equal(str); + var a = Address().fromHashbuf(pubkeyhash, 'testnet', 'scripthash'); + a.networkstr.should.equal('testnet'); + a.typestr.should.equal('scripthash'); + }); + + it('should throw an error for invalid length hashbuf', function() { + (function() { + Address().fromHashbuf(buf); + }).should.throw('hashbuf must be exactly 20 bytes'); + }); + + }); + describe('#fromPubkey', function() { it('should make this address from a compressed pubkey', function() {