diff --git a/btcOperator.js b/btcOperator.js index 60f1bd9..170be20 100644 --- a/btcOperator.js +++ b/btcOperator.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //btcOperator v1.0.7a +(function(EXPORTS) { //btcOperator v1.0.7b /* BTC Crypto and API Operator */ const btcOperator = EXPORTS; @@ -94,6 +94,14 @@ return false; } + btcOperator.multiSigAddress = function(pubKeys, minRequired) { + if (!Array.isArray(pubKeys)) + throw "pubKeys must be an array of public keys"; + else if (pubKeys.length < minRequired) + throw "minimum required should be less than the number of pubKeys"; + return coinjs.pubkeys2MultisigAddress(pubKeys, minRequired); + } + //convert from one blockchain to another blockchain (target version) btcOperator.convert = {}; @@ -323,7 +331,6 @@ return true; } else return false; - } function autoFeeCalc(tx) { @@ -366,6 +373,7 @@ try { ({ senders, + privkeys, receivers, amounts } = validateTxParameters({ @@ -458,6 +466,25 @@ }) } + btcOperator.signTx = function(tx, privKeys) { + if (typeof tx === 'string' || Array.isArray(tx)) { + try { + tx = coinjs.transaction().deserialize(tx); + } catch { + throw "Invalid transaction hex"; + } + } else if (typeof tx !== 'object' || typeof tx.sign !== 'function') + throw "Invalid transaction object"; + + if (!Array.isArray(privkeys)) + privkeys = [privkeys]; + for (let i in privKeys) + if (privKeys[i].length === 64) + privkeys[i] = coinjs.privkey2wif(privKeys[i]); + new Set(privKeys).forEach(key => console.debug("Signing key:", key, tx.sign(key, 1 /*sighashtype*/ ))); //Sign the tx using private key WIF + return tx.serialize(); + } + btcOperator.getTx = txid => new Promise((resolve, reject) => { fetch_api(`get_tx/BTC/${txid}`) .then(result => resolve(result.data))