Address().fromScript(script)
It is convenient to be able to derive an address directly from a script for p2sh transactions.
This commit is contained in:
parent
79d79012d4
commit
4a027e260a
|
@ -2,6 +2,7 @@ var base58check = require('./base58check');
|
|||
var constants = require('./constants');
|
||||
var Hash = require('./hash');
|
||||
var Pubkey = require('./pubkey');
|
||||
var Script = require('./script');
|
||||
|
||||
function Address(obj) {
|
||||
if (!(this instanceof Address))
|
||||
|
@ -24,6 +25,13 @@ Address.prototype.fromPubkey = function(pubkey, networkstr) {
|
|||
return this;
|
||||
};
|
||||
|
||||
Address.prototype.fromScript = function(script, networkstr) {
|
||||
this.hashbuf = Hash.sha256ripemd160(script.toBuffer());
|
||||
this.networkstr = networkstr || 'mainnet';
|
||||
this.typestr = 'scripthash';
|
||||
return this;
|
||||
};
|
||||
|
||||
Address.prototype.fromString = function(str) {
|
||||
var buf = base58check.decode(str);
|
||||
if (buf.length !== 1 + 20)
|
||||
|
|
|
@ -2,6 +2,7 @@ var should = require('chai').should();
|
|||
var constants = require('../lib/constants');
|
||||
var Pubkey = require('../lib/pubkey');
|
||||
var Address = require('../lib/address');
|
||||
var Script = require('../lib/script');
|
||||
|
||||
describe('Address', function() {
|
||||
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
|
||||
|
@ -45,6 +46,22 @@ describe('Address', function() {
|
|||
|
||||
});
|
||||
|
||||
describe('#fromScript', function() {
|
||||
|
||||
it('should make this address from a script', function() {
|
||||
var script = Script().fromString("OP_CHECKMULTISIG");
|
||||
var address = Address().fromScript(script);
|
||||
address.toString().should.equal('3BYmEwgV2vANrmfRymr1mFnHXgLjD6gAWm');
|
||||
});
|
||||
|
||||
it('should make this address from other script', function() {
|
||||
var script = Script().fromString("OP_CHECKSIG OP_HASH160");
|
||||
var address = Address().fromScript(script);
|
||||
address.toString().should.equal('347iRqVwks5r493N1rsLN4k9J7Ljg488W7');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#fromString', function() {
|
||||
|
||||
it('should derive from this known address string mainnet', function() {
|
||||
|
|
Loading…
Reference in New Issue