diff --git a/lib/address.js b/lib/address.js index 43912c0..4b65eee 100644 --- a/lib/address.js +++ b/lib/address.js @@ -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) diff --git a/test/address.js b/test/address.js index 44dde17..697cc29 100644 --- a/test/address.js +++ b/test/address.js @@ -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() {