Skip to content

Commit

Permalink
Merge pull request #87 from VerusCoin/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Asherda authored Oct 29, 2018
2 parents 8531f2e + fea7cb8 commit f6e7c25
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dependencies": {
"adm-zip": "^0.4.7",
"agama-wallet-lib": "git+https://github.com/VerusCoin/agama-wallet-lib.git",
"bitgo-utxo-lib": "git+https://github.com/miketout/bitgo-utxo-lib.git",
"arch": "^2.1.0",
"async": "^2.6.0",
"bigi": "^1.4.2",
Expand Down
9 changes: 9 additions & 0 deletions routes/appConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const appConfig = {
dataDir: '',
autoStartVRSC: false,
autoStakeVRSC: false,
//darkMode: false,
cheatCatcher: '',
dex: {
walletUnlockTimeout: 3600,
Expand Down Expand Up @@ -99,6 +100,14 @@ const appConfig = {
displayName: 'Automatically start staking VerusCoin when it is launched in native mode',
type: 'boolean',
},
/*
darkMode: {
display: true,
initDisplay: true,
displayName: 'Change UI to dark mode',
type: 'boolean',
},
*/
cheatCatcher: {
display: true,
info: 'As part of the VerusCoin Something at Stake solution, you can enter a Verus sapling address in this field, so when you catch them, you get their staking reward.',
Expand Down
5 changes: 3 additions & 2 deletions routes/electrumjs/electrumjs.networks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let networks = require('agama-wallet-lib/src/bitcoinjs-networks');
//let networks = require('agama-wallet-lib/src/bitcoinjs-networks');
let networks = require('bitgo-utxo-lib');

networks.komodo = networks.kmd;
//networks.komodo = networks.kmd;

module.exports = networks;
3 changes: 2 additions & 1 deletion routes/electrumjs/electrumjs.txdecoder-pos.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ SOFTWARE.
var bitcoin = require('bitcoinjs-lib-pos');
var script = require('bitcoinjs-lib-pos/src/script');
var address = require('bitcoinjs-lib-pos/src/address');
var bitcoinJS = require('bitcoinjs-lib');
//var bitcoinJS = require('bitcoinjs-lib');
var bitcoinJS = require('bitgo-utxo-lib');

var decodeFormat = function(tx) {
var result = {
Expand Down
38 changes: 33 additions & 5 deletions routes/electrumjs/electrumjs.txdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

var bitcoin = require('bitcoinjs-lib');
//var bitcoin = require('bitcoinjs-lib');
var bitcoin = require('bitgo-utxo-lib');
// zcash fallback
const Buffer = require('safe-buffer').Buffer;
const {
Expand Down Expand Up @@ -74,6 +75,7 @@ var decodeInput = function(tx) {

var decodeOutput = function(tx, network) {
var format = function(out, n, network) {
console.log('OUT VALUE IS ' + out.value)
var vout = {
satoshi: out.value,
value: (1e-8 * out.value).toFixed(8),
Expand Down Expand Up @@ -113,16 +115,42 @@ var decodeOutput = function(tx, network) {

var TxDecoder = module.exports = function(rawtx, network) {
try {
const _tx = bitcoin.Transaction.fromHex(rawtx);
/*
console.log('Decoding transaction on network: ')
console.log(network)
*/
const _tx = bitcoin.Transaction.fromHex(rawtx, network);

/*
console.log('------------------------------------------------------------------------------------')
console.log(_tx)
console.log('------------------------------------------------------------------------------------')
*/

if (network.isZcash && (_tx.joinsplits || (_tx.vShieldedSpend || _tx.vShieldedOutput))) {
console.log('ZTx Detected, decoding');

const buffer = Buffer.from(rawtx, 'hex');

const readHash = buffer => {
const [res, bufferLeft] = readSlice(32)(_sha256(_sha256(buffer)))
const hash = Buffer.from(res, 'hex').reverse().toString('hex')
return hash
};

_tx.getId = () => {
return readHash(buffer);
};
}
return {
tx: _tx,
network: network,
format: decodeFormat(_tx),
inputs: decodeInput(_tx),
inputs: !_tx.ins.length ? [{ txid: '0000000000000000000000000000000000000000000000000000000000000000' }] : decodeInput(_tx),
outputs: decodeOutput(_tx, network),
};
} catch (e) {
console.log(e);
if (network.isZcash) {
console.log('z tx decode fallback');

Expand All @@ -140,12 +168,12 @@ var TxDecoder = module.exports = function(rawtx, network) {
const readHash = buffer => {
const [res, bufferLeft] = readSlice(32)(_sha256(_sha256(buffer)))
const hash = Buffer.from(res, 'hex').reverse().toString('hex')
return [hash, bufferLeft]
return hash
};

let decodedtx = decodeTx(buffer);
decodedtx[0].getId = () => {
return readHash(buffer)[0];
return readHash(buffer);
};

return {
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ shepherd.Promise = require('bluebird');
shepherd.exec = require('child_process').exec;
shepherd.execFile = require('child_process').execFile;
shepherd.sha256 = require('sha256');
shepherd.bitcoinJS = require('bitcoinjs-lib');
//shepherd.bitcoinJS = require('bitcoinjs-lib');
shepherd.bitcoinJS = require('bitgo-utxo-lib');
shepherd.coinSelect = require('coinselect');
shepherd.fixPath = require('fix-path');
shepherd.crypto = require('crypto');
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd/coindWalletKeys.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const fs = require('fs-extra');
const _fs = require('graceful-fs');
const wif = require('wif');
const bitcoinJS = require('bitcoinjs-lib');
//const bitcoinJS = require('bitcoinjs-lib');
const bitcoinJS = require('bitgo-utxo-lib');

module.exports = (shepherd) => {
/*
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd/elections.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const bs58check = require('bs58check');
const bitcoin = require('bitcoinjs-lib');
//const bitcoin = require('bitcoinjs-lib');
const bitcoin = require('bitgo-utxo-lib');
const Promise = require('bluebird');

module.exports = (shepherd) => {
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd/electrum/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const bs58check = require('bs58check');
const bitcoinZcash = require('bitcoinjs-lib-zcash');
const bitcoin = require('bitcoinjs-lib');
//const bitcoin = require('bitcoinjs-lib');
const bitcoin = require('bitgo-utxo-lib');

module.exports = (shepherd) => {
shepherd.post('/electrum/login', (req, res, next) => {
Expand Down
4 changes: 4 additions & 0 deletions routes/shepherd/electrum/coins.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ module.exports = (shepherd) => {
shepherd.log(`${coin} doesnt have any backup electrum servers`, true);
}

//console.log(shepherd.electrumKeys)

if (Object.keys(shepherd.electrumKeys).length > 0) {
//console.log('Attempting to add ' + coin + ', with wif key: ')
//console.log(shepherd.electrumKeys[Object.keys(shepherd.electrumKeys)[0]].priv)
const _keys = shepherd.wifToWif(shepherd.electrumKeys[Object.keys(shepherd.electrumKeys)[0]].priv, coin);

shepherd.electrumKeys[coin] = {
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd/electrum/createtx-multi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const bitcoinJS = require('bitcoinjs-lib');
//const bitcoinJS = require('bitcoinjs-lib');
const bitcoinJS = require('bitgo-utxo-lib');
const bitcoinJSForks = require('bitcoinforksjs-lib');
const bitcoinZcash = require('bitcoinjs-lib-zcash');
const bitcoinPos = require('bitcoinjs-lib-pos');
Expand Down
3 changes: 2 additions & 1 deletion routes/shepherd/electrum/createtx-split.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const bitcoinJS = require('bitcoinjs-lib');
//const bitcoinJS = require('bitcoinjs-lib');
const bitcoinJS = require('bitgo-utxo-lib');
const bitcoinJSForks = require('bitcoinforksjs-lib');
const bitcoinZcash = require('bitcoinjs-lib-zcash');
const bitcoinPos = require('bitcoinjs-lib-pos');
Expand Down
67 changes: 62 additions & 5 deletions routes/shepherd/electrum/createtx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const bitcoinJS = require('bitcoinjs-lib');
//const bitcoinJS = require('bitcoinjs-lib');
const bitcoinJS = require('bitgo-utxo-lib');
const bitcoin = require('bitgo-utxo-lib');
const bitcoinJSForks = require('bitcoinforksjs-lib');
const bitcoinZcash = require('bitcoinjs-lib-zcash');
const bitcoinPos = require('bitcoinjs-lib-pos');
Expand Down Expand Up @@ -56,18 +58,28 @@ module.exports = (shepherd) => {
}

// single sig
shepherd.buildSignedTx = (sendTo, changeAddress, wif, network, utxo, changeValue, spendValue, opreturn) => {
let key = shepherd.isZcash(network) ? bitcoinZcash.ECPair.fromWIF(wif, shepherd.getNetworkData(network)) : bitcoinJS.ECPair.fromWIF(wif, shepherd.getNetworkData(network));
shepherd.buildSignedTx = (sendTo, changeAddress, wif, network, utxo, changeValue, spendValue, opreturn, blockheight) => {
console.log('Initiating transaction build procedure')

let key = bitcoinJS.ECPair.fromWIF(wif, bitcoin.networks[network]);
let tx;


console.log('Keypair intialized and transaction defined')

if (shepherd.isZcash(network)) {
console.log('network isZcash', true);
tx = new bitcoinZcash.TransactionBuilder(shepherd.getNetworkData(network));
} else if (shepherd.isPos(network)) {
console.log('network isPos', true);
tx = new bitcoinPos.TransactionBuilder(shepherd.getNetworkData(network));
} else {
console.log('network bitcoinJS', true);
tx = new bitcoinJS.TransactionBuilder(shepherd.getNetworkData(network));
}

console.log('Unsigned transaction structure constructed successfully');

shepherd.log('buildSignedTx', true);
// console.log(`buildSignedTx priv key ${wif}`);
shepherd.log(`buildSignedTx pub key ${key.getAddress().toString()}`, true);
Expand All @@ -77,29 +89,40 @@ module.exports = (shepherd) => {
tx.addInput(utxo[i].txid, utxo[i].vout);
}

console.log(utxo);

console.log('UTXOs added to transaction')

if (shepherd.isPos(network)) {
console.log('Network is Pos, adding special output')
tx.addOutput(
sendTo,
Number(spendValue),
shepherd.getNetworkData(network)
);
} else {
console.log('Adding output to transaction')
tx.addOutput(sendTo, Number(spendValue));
}

if (changeValue > 0) {
console.log('Change value larger than 0, adding change value output')
if (shepherd.isPos(network)) {
console.log('Special case for Pos on adding change value')
tx.addOutput(
changeAddress,
Number(changeValue),
shepherd.getNetworkData(network)
);
} else {
console.log('Adding change value for standard network')
tx.addOutput(changeAddress, Number(changeValue));
}
}

if (opreturn) {
console.log('Opreturn detected, adding to outputs:')
console.log(opreturn)
const data = Buffer.from(opreturn, 'utf8');
const dataScript = shepherd.bitcoinJS.script.nullData.output.encode(data);
tx.addOutput(dataScript, 1000);
Expand All @@ -108,17 +131,40 @@ module.exports = (shepherd) => {

if (network === 'komodo' ||
network.toUpperCase() === 'KMD') {
console.log('Network detected as ' + network)
const _locktime = Math.floor(Date.now() / 1000) - 777;
tx.setLockTime(_locktime);
shepherd.log(`kmd tx locktime set to ${_locktime}`, true);
}

console.log('Current block height: ' + blockheight);

let versionNum;
if ((blockheight === 419200 && network === 'zec') ||
(blockheight === 227520 && network === 'vrsc')){
versionNum = 4;
}
else {
if (network === 'zec') {
versionNum = 3;
}
else {
versionNum = 1;
}
}

tx.setVersion(versionNum);

console.log('Set version to ' + versionNum)

/*
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)) {
Expand All @@ -128,14 +174,21 @@ module.exports = (shepherd) => {
key
);
} else {
tx.sign(i, key);
console.log('________________________________________________________');
console.log('KEYPAIR NETWORK: ')
console.log(key.network)
console.log('________________________________________________________');
tx.sign(i, key, '', null, utxo[i].value);
console.log('Standard transaction signed')
}
}

const rawtx = tx.build().toHex();

/*
shepherd.log('buildSignedTx signed tx hex', true);
shepherd.log(rawtx, true);
*/

return rawtx;
}
Expand Down Expand Up @@ -307,13 +360,15 @@ module.exports = (shepherd) => {
value: Number(utxoList[i].amountSats),
interestSats: Number(utxoList[i].interestSats),
verified: utxoList[i].verified ? utxoList[i].verified : false,
blockheight: utxoList[i].blockheight
});
} else {
utxoListFormatted.push({
txid: utxoList[i].txid,
vout: utxoList[i].vout,
value: Number(utxoList[i].amountSats),
verified: utxoList[i].verified ? utxoList[i].verified : false,
blockheight: utxoList[i].blockheight
});
}
}
Expand Down Expand Up @@ -348,6 +403,7 @@ module.exports = (shepherd) => {
);
let inputs = firstRun.inputs;
let outputs = firstRun.outputs;
let currentheight = utxoListFormatted[0].blockheight;

if (btcFee) {
shepherd.log(`btc fee per byte ${btcFee}`, true);
Expand Down Expand Up @@ -554,7 +610,8 @@ module.exports = (shepherd) => {
inputs,
_change,
value,
opreturn
opreturn,
currentheight
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions routes/shepherd/electrum/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const bip39 = require('bip39');
const crypto = require('crypto');
const bigi = require('bigi');
const bitcoinZcash = require('bitcoinjs-lib-zcash');
const bitcoin = require('bitcoinjs-lib');
//const bitcoin = require('bitcoinjs-lib');
const bitcoin = require('bitgo-utxo-lib');
const bs58check = require('bs58check');

module.exports = (shepherd) => {
shepherd.wifToWif = (wif, network) => {
network = network.toLowerCase();
const key = shepherd.isZcash(network) ? new bitcoinZcash.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true) : new bitcoin.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true);
//const key = shepherd.isZcash(network) ? new bitcoinZcash.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true) : new bitcoin.ECPair.fromWIF(wif, shepherd.getNetworkData(network), true);
const key = new bitcoin.ECPair.fromWIF(wif, bitcoin.networks[network], true);

return {
pub: key.getAddress(),
Expand Down
Loading

0 comments on commit f6e7c25

Please sign in to comment.