Browse Source

bch, btg support

pkg_automation_electrum
pbca26 7 years ago
parent
commit
e3c0082e06
  1. 2
      package.json
  2. 6
      routes/electrumjs/electrumServers.js
  3. 103
      routes/shepherd/electrum/createtx.js

2
package.json

@ -30,6 +30,8 @@
"arch": "^2.1.0", "arch": "^2.1.0",
"bip39": "^2.4.0", "bip39": "^2.4.0",
"bitcoinjs-lib": "^3.2.0", "bitcoinjs-lib": "^3.2.0",
"bitcoinjs-lib-bch": "git://github.com/bitcoinjs/bitcoinjs-lib#opt-in-bitcoincash-sighash",
"bitcoinjs-lib-zcash": "git://github.com/karel-3d/bitcoinjs-lib#zcash",
"bluebird": "^3.4.7", "bluebird": "^3.4.7",
"body-parser": "^1.15.2", "body-parser": "^1.15.2",
"buffer-reverse": "^1.0.1", "buffer-reverse": "^1.0.1",

6
routes/electrumjs/electrumServers.js

@ -286,7 +286,7 @@ let electrumServers = {
'electrum1.cipig.net:10000', 'electrum1.cipig.net:10000',
'electrum2.cipig.net:10000' 'electrum2.cipig.net:10000'
], ],
}, },*/
btg: { btg: {
address: '173.212.225.176', address: '173.212.225.176',
port: 10052, port: 10052,
@ -320,7 +320,7 @@ let electrumServers = {
'electrum2.cipig.net:10050' 'electrum2.cipig.net:10050'
], ],
}, },
/*bch: { bch: {
address: 'electrum1.cipig.net', address: 'electrum1.cipig.net',
port: 10051, port: 10051,
proto: 'tcp', proto: 'tcp',
@ -330,7 +330,7 @@ let electrumServers = {
'electrum1.cipig.net:10051', 'electrum1.cipig.net:10051',
'electrum2.cipig.net:10051' 'electrum2.cipig.net:10051'
], ],
},*/ },
argentum: { // !estimatefee argentum: { // !estimatefee
address: '173.212.225.176', address: '173.212.225.176',
port: 50081, port: 50081,

103
routes/shepherd/electrum/createtx.js

@ -1,7 +1,17 @@
const bitcoinJSForks = require('bitcoinforksjs-lib');
module.exports = (shepherd) => { module.exports = (shepherd) => {
// unsigned tx // unsigned tx
shepherd.buildUnsignedTx = (sendTo, changeAddress, network, utxo, changeValue, spendValue) => { shepherd.buildUnsignedTx = (sendTo, changeAddress, network, utxo, changeValue, spendValue) => {
let tx = new shepherd.bitcoinJS.TransactionBuilder(shepherd.getNetworkData(network)); let tx;
if (network === 'btg') {
shepherd.log('enable btg');
tx = new bitcoinJSForks.TransactionBuilder(shepherd.getNetworkData(network));
tx.enableBitcoinGold(true);
} else {
tx = new shepherd.bitcoinJS.TransactionBuilder(shepherd.getNetworkData(network));
}
shepherd.log('buildSignedTx'); shepherd.log('buildSignedTx');
// console.log(`buildSignedTx priv key ${wif}`); // console.log(`buildSignedTx priv key ${wif}`);
@ -86,6 +96,60 @@ module.exports = (shepherd) => {
return rawtx; return rawtx;
} }
// btg
shepherd.buildSignedTxForks = (sendTo, changeAddress, wif, network, utxo, changeValue, spendValue) => {
let tx;
if (network === 'btg' ||
network === 'bch') {
tx = new bitcoinJSForks.TransactionBuilder(shepherd.getNetworkData(network));
}
const keyPair = bitcoinJSForks.ECPair.fromWIF(wif, shepherd.getNetworkData(network));
const pk = bitcoinJSForks.crypto.hash160(keyPair.getPublicKeyBuffer());
const spk = bitcoinJSForks.script.pubKeyHash.output.encode(pk);
shepherd.log(`buildSignedTx${network.toUpperCase()}`);
for (let i = 0; i < utxo.length; i++) {
tx.addInput(utxo[i].txid, utxo[i].vout, bitcoinJSForks.Transaction.DEFAULT_SEQUENCE, spk);
}
tx.addOutput(sendTo, Number(spendValue));
if (changeValue > 0) {
tx.addOutput(changeAddress, Number(changeValue));
}
if (network === 'btg') {
tx.enableBitcoinGold(true);
} else if (network === 'bch') {
tx.enableBitcoinCash(true);
}
tx.setVersion(2);
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 hashType = bitcoinJSForks.Transaction.SIGHASH_ALL | bitcoinJSForks.Transaction.SIGHASH_BITCOINCASHBIP143;
for (let i = 0; i < utxo.length; i++) {
tx.sign(i, keyPair, null, hashType, utxo[i].value);
}
const rawtx = tx.build().toHex();
shepherd.log('buildSignedTx signed tx hex', true);
shepherd.log(rawtx, true);
return rawtx;
}
shepherd.maxSpendBalance = (utxoList, fee) => { shepherd.maxSpendBalance = (utxoList, fee) => {
let maxSpendBalance = 0; let maxSpendBalance = 0;
@ -101,7 +165,7 @@ module.exports = (shepherd) => {
} }
shepherd.get('/electrum/createrawtx', (req, res, next) => { shepherd.get('/electrum/createrawtx', (req, res, next) => {
// txid 64 char // TODO: unconf output(s) error message
const network = req.query.network || shepherd.findNetworkObj(req.query.coin); const network = req.query.network || shepherd.findNetworkObj(req.query.coin);
const ecl = new shepherd.electrumJSCore(shepherd.electrumServers[network].port, shepherd.electrumServers[network].address, shepherd.electrumServers[network].proto); // tcp or tls const ecl = new shepherd.electrumJSCore(shepherd.electrumServers[network].port, shepherd.electrumServers[network].address, shepherd.electrumServers[network].proto); // tcp or tls
const outputAddress = req.query.address; const outputAddress = req.query.address;
@ -123,7 +187,9 @@ module.exports = (shepherd) => {
ecl.close(); ecl.close();
if (utxoList && if (utxoList &&
utxoList.length) { utxoList.length &&
utxoList[0] &&
utxoList[0].txid) {
let utxoListFormatted = []; let utxoListFormatted = [];
let totalInterest = 0; let totalInterest = 0;
let totalInterestUTXOCount = 0; let totalInterestUTXOCount = 0;
@ -316,15 +382,28 @@ module.exports = (shepherd) => {
value value
); );
} else { } else {
_rawtx = shepherd.buildSignedTx( if (network === 'btg' ||
outputAddress, network === 'bch') {
changeAddress, _rawtx = shepherd.buildSignedTxForks(
wif, outputAddress,
network, changeAddress,
inputs, wif,
_change, network,
value inputs,
); _change,
value
);
} else {
_rawtx = shepherd.buildSignedTx(
outputAddress,
changeAddress,
wif,
network,
inputs,
_change,
value
);
}
} }
if (!push || if (!push ||

Loading…
Cancel
Save