move some script logic from Address to Script

This commit is contained in:
Manuel Araoz 2015-03-18 17:59:09 -03:00
parent 3619c7c9e2
commit e0b1ca0e10
3 changed files with 39 additions and 30 deletions

View File

@ -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);
};

View File

@ -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);
};
/**

View File

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