From b57ae1ead3c7f5da9c6b6f4a62f21ce6f948a8d9 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sun, 28 Jan 2018 18:10:56 +0300 Subject: [PATCH] createtx test route --- routes/shepherd/electrum/createtx.js | 92 ++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/routes/shepherd/electrum/createtx.js b/routes/shepherd/electrum/createtx.js index e264a6f..3899068 100644 --- a/routes/shepherd/electrum/createtx.js +++ b/routes/shepherd/electrum/createtx.js @@ -618,5 +618,97 @@ module.exports = (shepherd) => { } }); + shepherd.post('/electrum/createrawtx-test', (req, res, next) => { + if (shepherd.checkToken(req.body.token)) { + const wif = req.body.payload.wif; + const utxo = req.body.payload.utxo; + const targets = req.body.payload.targets; + const network = req.body.payload.network; + const change = req.body.payload.change; + const outputAddress = req.body.payload.outputAddress; + const changeAddress = req.body.payload.changeAddress; + + let key = shepherd.isZcash(network) ? bitcoinZcash.ECPair.fromWIF(wif, shepherd.getNetworkData(network)) : shepherd.bitcoinJS.ECPair.fromWIF(wif, shepherd.getNetworkData(network)); + let tx; + + if (shepherd.isZcash(network)) { + tx = new bitcoinZcash.TransactionBuilder(shepherd.getNetworkData(network)); + } else if (shepherd.isPos(network)) { + tx = new bitcoinPos.TransactionBuilder(shepherd.getNetworkData(network)); + } else { + tx = new shepherd.bitcoinJS.TransactionBuilder(shepherd.getNetworkData(network)); + } + + shepherd.log('buildSignedTx', true); + // console.log(`buildSignedTx priv key ${wif}`); + shepherd.log(`buildSignedTx pub key ${key.getAddress().toString()}`, 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); + } + + for (let i = 0; i < targets.length; i++) { + if (shepherd.isPos(network)) { + tx.addOutput(outputAddress, Number(targets[i]), shepherd.getNetworkData(network)); + } else { + tx.addOutput(outputAddress, Number(targets[i])); + } + } + + if (Number(change) > 0) { + if (shepherd.isPos(network)) { + tx.addOutput(changeAddress, Number(change), shepherd.getNetworkData(network)); + } else { + console.log(`change ${change}`); + tx.addOutput(changeAddress, Number(change)); + } + } + + 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);*/ + + for (let i = 0; i < utxo.length; i++) { + if (shepherd.isPos(network)) { + tx.sign(shepherd.getNetworkData(network), i, key); + } else { + tx.sign(i, key); + } + } + + const rawtx = tx.build().toHex(); + + /*shepherd.log('buildSignedTx signed tx hex', true); + shepherd.log(rawtx, true);*/ + + // return rawtx; + + const successObj = { + msg: 'success', + result: rawtx, + }; + + res.end(JSON.stringify(successObj)); + } else { + const errorObj = { + msg: 'error', + result: 'unauthorized access', + }; + + res.end(JSON.stringify(errorObj)); + } + }); + return shepherd; }; \ No newline at end of file