Browse Source

move some script logic from Address to Script

patch-2
Manuel Araoz 10 years ago
parent
commit
e0b1ca0e10
  1. 26
      lib/address.js
  2. 39
      lib/script/script.js
  3. 4
      test/address.js

26
lib/address.js

@ -244,28 +244,11 @@ Address._transformPublicKey = function(pubkey) {
* @private * @private
*/ */
Address._transformScript = function(script, network) { Address._transformScript = function(script, network) {
var info = {}; $.checkArgument(script instanceof Script, 'script must be a Script instance');
if (!(script instanceof Script)) { var info = script.getAddressInfo(network);
throw new TypeError('script must be an instance of Script.'); if (!info) {
}
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 {
throw new errors.Script.CantDeriveAddress(script); throw new errors.Script.CantDeriveAddress(script);
} }
info.network = Networks.get(network) || Networks.defaultNetwork;
return info; return info;
}; };
@ -297,7 +280,7 @@ Address.createMultisig = function(publicKeys, threshold, network) {
*/ */
Address._transformString = function(data, network, type) { Address._transformString = function(data, network, type) {
if (typeof(data) !== 'string') { 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 addressBuffer = Base58Check.decode(data);
var info = Address._transformBuffer(addressBuffer, network, type); 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 * @returns {Address} A new valid and frozen instance of an Address
*/ */
Address.fromScript = function(script, network) { Address.fromScript = function(script, network) {
$.checkArgument(script instanceof Script, 'script must be a Script instance');
var info = Address._transformScript(script, network); var info = Address._transformScript(script, network);
return new Address(info.hashBuffer, network, info.type); return new Address(info.hashBuffer, network, info.type);
}; };

39
lib/script/script.js

@ -730,6 +730,33 @@ Script.fromAddress = function(address) {
throw new errors.Script.UnrecognizedAddress(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 * @param {Network=} network
* @return {Address|boolean} the associated address for this script if possible, or false * @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) { Script.prototype.toAddress = function(network) {
var Address = require('../address'); var Address = require('../address');
network = Networks.get(network) || this._network || Networks.defaultNetwork; network = Networks.get(network) || this._network || Networks.defaultNetwork;
var canConvertToAddress = this.isPublicKeyHashOut() || var info = this.getAddressInfo();
this.isScriptHashOut() || if (!info) {
this.isPublicKeyHashIn() ||
this.isScriptHashIn();
if (canConvertToAddress) {
return new Address(this, network);
}
return false; return false;
}
info.network = Networks.get(network) || Networks.defaultNetwork;
return new Address(info);
}; };
/** /**

4
test/address.js

@ -280,13 +280,13 @@ describe('Address', function() {
it('should error because of incorrect type for script transform', function() { it('should error because of incorrect type for script transform', function() {
(function() { (function() {
return Address._transformScript(new Buffer(20)); 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() { it('should error because of incorrect type for string transform', function() {
(function() { (function() {
return Address._transformString(new Buffer(20)); 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() { it('should make an address from a pubkey hash buffer', function() {

Loading…
Cancel
Save