btcOperator v1.0.7b

- Added: signTx
- Added multiSigAddress
This commit is contained in:
sairajzero 2022-08-10 02:58:36 +05:30
parent bc44405102
commit 91e6b82387

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //btcOperator v1.0.7a (function(EXPORTS) { //btcOperator v1.0.7b
/* BTC Crypto and API Operator */ /* BTC Crypto and API Operator */
const btcOperator = EXPORTS; const btcOperator = EXPORTS;
@ -94,6 +94,14 @@
return false; 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) //convert from one blockchain to another blockchain (target version)
btcOperator.convert = {}; btcOperator.convert = {};
@ -323,7 +331,6 @@
return true; return true;
} else } else
return false; return false;
} }
function autoFeeCalc(tx) { function autoFeeCalc(tx) {
@ -366,6 +373,7 @@
try { try {
({ ({
senders, senders,
privkeys,
receivers, receivers,
amounts amounts
} = validateTxParameters({ } = 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) => { btcOperator.getTx = txid => new Promise((resolve, reject) => {
fetch_api(`get_tx/BTC/${txid}`) fetch_api(`get_tx/BTC/${txid}`)
.then(result => resolve(result.data)) .then(result => resolve(result.data))