Address().fromHashbuf() convenience method
...useful for when you have the pubkeyhash, but not the version byte.
This commit is contained in:
parent
60459b6c7a
commit
f17d604e44
@ -51,6 +51,15 @@ Address.prototype.fromBuffer = function(buf) {
|
|||||||
return this;
|
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) {
|
Address.prototype.fromPubkey = function(pubkey, networkstr) {
|
||||||
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
|
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
|
||||||
this.networkstr = networkstr || 'mainnet';
|
this.networkstr = networkstr || 'mainnet';
|
||||||
|
|||||||
@ -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() {
|
describe('#fromPubkey', function() {
|
||||||
|
|
||||||
it('should make this address from a compressed pubkey', function() {
|
it('should make this address from a compressed pubkey', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user