From e0b1ca0e107df961fdaac95fd39028b83733b3e6 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 18 Mar 2015 17:59:09 -0300 Subject: [PATCH] move some script logic from Address to Script --- lib/address.js | 26 +++++--------------------- lib/script/script.js | 39 ++++++++++++++++++++++++++++++++------- test/address.js | 4 ++-- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/lib/address.js b/lib/address.js index 14d89f3..39cfcd6 100644 --- a/lib/address.js +++ b/lib/address.js @@ -244,28 +244,11 @@ Address._transformPublicKey = function(pubkey) { * @private */ Address._transformScript = function(script, network) { - var info = {}; - if (!(script instanceof Script)) { - throw new TypeError('script must be an instance of Script.'); - } - if (script.isScriptHashOut()) { - info.hashBuffer = script.getData(); - info.type = Address.PayToScriptHash; - } else if (script.isPublicKeyHashOut()) { - info.hashBuffer = script.getData(); - info.type = Address.PayToPublicKeyHash; - } else if (script.isPublicKeyHashIn()) { - // hash the publickey found in the scriptSig - info.hashBuffer = Hash.sha256ripemd160(script.chunks[1].buf); - info.type = Address.PayToPublicKeyHash; - } else if (script.isScriptHashIn()) { - // hash the redeemscript found at the end of the scriptSig - info.hashBuffer = Hash.sha256ripemd160(script.chunks[script.chunks.length - 1].buf); - info.type = Address.PayToScriptHash; - } else { + $.checkArgument(script instanceof Script, 'script must be a Script instance'); + var info = script.getAddressInfo(network); + if (!info) { throw new errors.Script.CantDeriveAddress(script); } - info.network = Networks.get(network) || Networks.defaultNetwork; return info; }; @@ -297,7 +280,7 @@ Address.createMultisig = function(publicKeys, threshold, network) { */ Address._transformString = function(data, network, type) { if (typeof(data) !== 'string') { - throw new TypeError('Address supplied is not a string.'); + throw new TypeError('data parameter supplied is not a string.'); } var addressBuffer = Base58Check.decode(data); var info = Address._transformBuffer(addressBuffer, network, type); @@ -372,6 +355,7 @@ Address.payingTo = function(script, network) { * @returns {Address} A new valid and frozen instance of an Address */ Address.fromScript = function(script, network) { + $.checkArgument(script instanceof Script, 'script must be a Script instance'); var info = Address._transformScript(script, network); return new Address(info.hashBuffer, network, info.type); }; diff --git a/lib/script/script.js b/lib/script/script.js index 06401b4..4945e1f 100644 --- a/lib/script/script.js +++ b/lib/script/script.js @@ -730,6 +730,33 @@ Script.fromAddress = function(address) { throw new errors.Script.UnrecognizedAddress(address); }; +/** + * @param {Network=} network + * @return {Address|boolean} the associated address information object + * for this script if any, or false + */ +Script.prototype.getAddressInfo = function() { + var Address = require('../address'); + var info = {}; + if (this.isScriptHashOut()) { + info.hashBuffer = this.getData(); + info.type = Address.PayToScriptHash; + } else if (this.isPublicKeyHashOut()) { + info.hashBuffer = this.getData(); + info.type = Address.PayToPublicKeyHash; + } else if (this.isPublicKeyHashIn()) { + // hash the publickey found in the scriptSig + info.hashBuffer = Hash.sha256ripemd160(this.chunks[1].buf); + info.type = Address.PayToPublicKeyHash; + } else if (this.isScriptHashIn()) { + // hash the redeemscript found at the end of the scriptSig + info.hashBuffer = Hash.sha256ripemd160(this.chunks[this.chunks.length - 1].buf); + info.type = Address.PayToScriptHash; + } else { + return false; + } + return info; +}; /** * @param {Network=} network * @return {Address|boolean} the associated address for this script if possible, or false @@ -737,14 +764,12 @@ Script.fromAddress = function(address) { Script.prototype.toAddress = function(network) { var Address = require('../address'); network = Networks.get(network) || this._network || Networks.defaultNetwork; - var canConvertToAddress = this.isPublicKeyHashOut() || - this.isScriptHashOut() || - this.isPublicKeyHashIn() || - this.isScriptHashIn(); - if (canConvertToAddress) { - return new Address(this, network); + var info = this.getAddressInfo(); + if (!info) { + return false; } - return false; + info.network = Networks.get(network) || Networks.defaultNetwork; + return new Address(info); }; /** diff --git a/test/address.js b/test/address.js index e456e70..b971e5f 100644 --- a/test/address.js +++ b/test/address.js @@ -280,13 +280,13 @@ describe('Address', function() { it('should error because of incorrect type for script transform', function() { (function() { return Address._transformScript(new Buffer(20)); - }).should.throw('script must be an instance of Script.'); + }).should.throw('Invalid Argument: script must be a Script instance'); }); it('should error because of incorrect type for string transform', function() { (function() { return Address._transformString(new Buffer(20)); - }).should.throw('Address supplied is not a string.'); + }).should.throw('data parameter supplied is not a string.'); }); it('should make an address from a pubkey hash buffer', function() {