From d9740e98c2a0fc358c55231ac78dfa34c3452eb9 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 28 Nov 2017 10:39:58 +0300 Subject: [PATCH] spv create unsigned tx --- routes/shepherd/electrum/createtx.js | 72 ++++++++++++++++++++++++---- version | 2 +- version_build | 2 +- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/routes/shepherd/electrum/createtx.js b/routes/shepherd/electrum/createtx.js index f312c5f..8c9c4d6 100644 --- a/routes/shepherd/electrum/createtx.js +++ b/routes/shepherd/electrum/createtx.js @@ -1,4 +1,45 @@ module.exports = (shepherd) => { + // unsigned tx + shepherd.buildUnsignedTx = (sendTo, changeAddress, network, utxo, changeValue, spendValue) => { + let tx = new shepherd.bitcoinJS.TransactionBuilder(shepherd.getNetworkData(network)); + + shepherd.log('buildSignedTx'); + // console.log(`buildSignedTx priv key ${wif}`); + shepherd.log(`buildSignedTx pub key ${changeAddress}`, true); + // console.log('buildSignedTx std tx fee ' + shepherd.electrumServers[network].txfee); + + for (let i = 0; i < utxo.length; i++) { + tx.addInput(utxo[i].txid, utxo[i].vout); + } + + tx.addOutput(sendTo, Number(spendValue)); + + if (changeValue > 0) { + tx.addOutput(changeAddress, Number(changeValue)); + } + + if (network === 'komodo' || + network === 'KMD') { + const _locktime = Math.floor(Date.now() / 1000) - 777; + tx.setLockTime(_locktime); + shepherd.log(`kmd tx locktime set to ${_locktime}`, true); + } + + shepherd.log('buildSignedTx unsigned tx data vin', true); + shepherd.log(tx.tx.ins, true); + shepherd.log('buildSignedTx unsigned tx data vout', true); + shepherd.log(tx.tx.outs, true); + shepherd.log('buildSignedTx unsigned tx data', true); + shepherd.log(tx, true); + + const rawtx = tx.buildIncomplete().toHex(); + + shepherd.log('buildUnsignedTx tx hex', true); + shepherd.log(rawtx, true); + + return rawtx; + } + // single sig shepherd.buildSignedTx = (sendTo, changeAddress, wif, network, utxo, changeValue, spendValue) => { let key = shepherd.bitcoinJS.ECPair.fromWIF(wif, shepherd.getNetworkData(network)); @@ -240,15 +281,28 @@ module.exports = (shepherd) => { shepherd.log(`vin sum ${vinSum} (${vinSum * 0.00000001})`, true); shepherd.log(`estimatedFee ${_estimatedFee} (${_estimatedFee * 0.00000001})`, true); - const _rawtx = shepherd.buildSignedTx( - outputAddress, - changeAddress, - wif, - network, - inputs, - _change, - value - ); + let _rawtx; + + if (req.query.unsigned) { + _rawtx = shepherd.buildUnsignedTx( + outputAddress, + changeAddress, + network, + inputs, + _change, + value + ); + } else { + _rawtx = shepherd.buildSignedTx( + outputAddress, + changeAddress, + wif, + network, + inputs, + _change, + value + ); + } if (!push || push === 'false') { diff --git a/version b/version index 96e1496..2923138 100644 --- a/version +++ b/version @@ -1,3 +1,3 @@ -version=0.2.0.24f +version=0.2.0.24g type=e-beta minversion=0.2.0.2 \ No newline at end of file diff --git a/version_build b/version_build index 445b1f9..92d60f4 100644 --- a/version_build +++ b/version_build @@ -1 +1 @@ -0.2.0.24f-beta \ No newline at end of file +0.2.0.24g-beta \ No newline at end of file