Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type handling #3118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions packages/bitcore-lib-doge/lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ var PublicKey = require('./publickey');
* @param {*} data - The encoded data in various formats
* @param {Network|String|number=} network - The network: 'livenet' or 'testnet'
* @param {string=} type - The type of address: 'script' or 'pubkey'
* @param {boolean=} nestedWitness - if the address uses a nested p2sh witness
* @param {string} type - Only 'scripthash' ('witnessscripthash' not supported by DOGE). If nestedWitness is set, then this is ignored
* @returns {Address} A new valid and frozen instance of an Address
* @constructor
*/
function Address(data, network, type) {
function Address(data, network, type, multisigType) {
/* jshint maxcomplexity: 12 */
/* jshint maxstatements: 20 */

Expand All @@ -52,7 +54,7 @@ function Address(data, network, type) {
}

if (_.isArray(data) && _.isNumber(network)) {
return Address.createMultisig(data, network, type);
return Address.createMultisig(data, network, type, false, multisigType);
}

if (data instanceof Address) {
Expand Down Expand Up @@ -96,11 +98,11 @@ Address.prototype._classifyArguments = function(data, network, type) {
/* jshint maxcomplexity: 10 */
// transform and validate input data
if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 20) {
return Address._transformHash(data);
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length === 21) {
return Address._transformHash(data, network, type);
} else if ((data instanceof Buffer || data instanceof Uint8Array) && data.length >= 21) {
return Address._transformBuffer(data, network, type);
} else if (data instanceof PublicKey) {
return Address._transformPublicKey(data);
return Address._transformPublicKey(data, network, type);
} else if (data instanceof Script) {
return Address._transformScript(data, network);
} else if (typeof(data) === 'string') {
Expand Down Expand Up @@ -219,13 +221,23 @@ Address._transformBuffer = function(buffer, network, type) {
* @returns {Object} An object with keys: hashBuffer, type
* @private
*/
Address._transformPublicKey = function(pubkey) {
Address._transformPublicKey = function(pubkey, network, type) {
var info = {};
if (!(pubkey instanceof PublicKey)) {
throw new TypeError('Address must be an instance of PublicKey.');
}
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
info.type = Address.PayToPublicKeyHash;
if (type && type !== Address.PayToScriptHash && type !== Address.PayToPublicKeyHash) {
throw new TypeError('Type must be either pubkeyhash or scripthash to transform public key.');
}
if (!pubkey.compressed && type === Address.PayToScriptHash) {
throw new TypeError('Witness addresses must use compressed public keys.');
}
if (type === Address.PayToScriptHash) {
info.hashBuffer = Hash.sha256ripemd160(Script.buildWitnessV0Out(pubkey).toBuffer());
} else {
info.hashBuffer = Hash.sha256ripemd160(pubkey.toBuffer());
}
info.type = type || Address.PayToPublicKeyHash;
return info;
};

Expand Down Expand Up @@ -255,11 +267,28 @@ Address._transformScript = function(script, network) {
* @param {Array} publicKeys - a set of public keys to create an address
* @param {number} threshold - the number of signatures needed to release the funds
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {boolean=} nestedWitness - if the address uses a nested p2sh witness
* @param {string} type - Only 'scripthash' ('witnessscripthash' not supported by DOGE). If nestedWitness is set, then this is ignored
* @return {Address}
*/
Address.createMultisig = function(publicKeys, threshold, network) {
Address.createMultisig = function(publicKeys, threshold, network, nestedWitness, type) {
network = network || publicKeys[0].network || Networks.defaultNetwork;
return Address.payingTo(Script.buildMultisigOut(publicKeys, threshold), network);
if (type && type !== Address.PayToScriptHash) {
throw new TypeError('Type must be either scripthash or witnessscripthash to create multisig.');
}
if (nestedWitness) {
publicKeys = _.map(publicKeys, PublicKey);
for (var i = 0; i < publicKeys.length; i++) {
if (!publicKeys[i].compressed) {
throw new TypeError('Witness addresses must use compressed public keys.');
}
}
}
var redeemScript = Script.buildMultisigOut(publicKeys, threshold);
if (nestedWitness) {
return Address.payingTo(Script.buildWitnessMultisigOutFromScript(redeemScript), network);
}
return Address.payingTo(redeemScript, network, type);
};

/**
Expand Down Expand Up @@ -288,8 +317,8 @@ Address._transformString = function(data, network, type) {
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @returns {Address} A new valid and frozen instance of an Address
*/
Address.fromPublicKey = function(data, network) {
var info = Address._transformPublicKey(data);
Address.fromPublicKey = function(data, network, type) {
var info = Address._transformPublicKey(data, network, type);
network = network || Networks.defaultNetwork;
return new Address(info.hashBuffer, network, info.type);
};
Expand Down
7 changes: 4 additions & 3 deletions packages/bitcore-lib-doge/lib/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,13 @@ PrivateKey.prototype.toPublicKey = function(){
* Will return an address for the private key
* @param {Network=} network - optional parameter specifying
* the desired network for the address
*
* @param {string} type - Either 'pubkeyhash', or 'scripthash'
*
* @returns {Address} An address generated from the private key
*/
PrivateKey.prototype.toAddress = function(network) {
PrivateKey.prototype.toAddress = function(network, type) {
var pubkey = this.toPublicKey();
return Address.fromPublicKey(pubkey, network || this.network);
return Address.fromPublicKey(pubkey, network || this.network, type);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcore-lib-doge/lib/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ PublicKey.prototype._getID = function _getID() {
* @param {String|Network=} network - Which network should the address be for
* @returns {Address} An address generated from the public key
*/
PublicKey.prototype.toAddress = function(network) {
PublicKey.prototype.toAddress = function(network, type) {
var Address = require('./address');
return Address.fromPublicKey(this, network || this.network);
return Address.fromPublicKey(this, network || this.network, type);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-lib-doge/lib/transaction/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ Transaction.prototype._selectInputType = function(utxo, pubkeys, threshold) {
} else if (utxo.script.isScriptHashOut()) {
clazz = MultiSigScriptHashInput;
}
} else if (utxo.script.isPublicKeyHashOut()) {
} else if (utxo.script.isPublicKeyHashOut() || utxo.script.isScriptHashOut()) {
clazz = PublicKeyHashInput;
} else if (utxo.script.isPublicKeyOut()) {
clazz = PublicKeyInput;
Expand Down