Browse Source

Address().fromHashbuf() convenience method

...useful for when you have the pubkeyhash, but not the version byte.
patch-2
Ryan X. Charles 10 years ago
parent
commit
f17d604e44
  1. 9
      lib/address.js
  2. 17
      test/address.js

9
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';

17
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() {

Loading…
Cancel
Save