Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated build scripts to be compatible with nodejs > 18 #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/*
* nwjs builder script
*/
var NwBuilder = require('nw-builder');
var nw = new NwBuilder({
files: './build/**', // use the glob format
platforms: ['osx64', 'win32', 'win64', 'linux32', 'linux64'],
cacheDir: './cache',
buildDir: './builds/',
macIcns: './build/images/FreeWallet.icns',
// macCredits: './build/html/credits.html'
// winIco: '',
});
import nwbuilder from 'nw-builder';

// Output logs to the console
nw.on('log', console.log);
async function init(){

// Build returns a promise
nw.build().then(function () {
console.log('all done!');
}).catch(function (error) {
console.error(error);
});
var nw = await nwbuilder({
srcDir: './build/**', // use the glob format
platform: "win",
arch: "x64",
cacheDir: './cache',
outDir: './builds/',
icon: './build/images/FreeWallet.icns',
// macCredits: './build/html/credits.html' //no longer exists
// winIco: '', //no longer exists
});

// Output logs to the console
//nw.on('log', console.log); //This event no longer exists

console.log('all done!');
}

init()
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ nwjs=/Applications/nwjs.app/Contents/MacOS/nwjs
###

# Verify we are building on a mac
if [ "$OSTYPE" != "darwin" ] ; then
echo "Build script is meant to be run on MacOS!"
if [ "$OSTYPE" != "darwin" ] && [ "$OSTYPE" != "linux-gnu" ]; then
echo "Build script is meant to be run on MacOS or Linux!"
exit
fi

Expand Down
16,585 changes: 16,583 additions & 2 deletions js/bitcoinjs-lib.min.js

Large diffs are not rendered by default.

89 changes: 80 additions & 9 deletions js/freewallet-desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ FW.ASSET_DIVISIBLE = {};
FW.ASSET_DIVISIBLE['BTC'] = true;
FW.ASSET_DIVISIBLE['XCP'] = true;

//Init ecc lib for new bitcoin-lib version
bitcoinjs.initEccLib(bitcoinjs.tiny_secp256k1)
ECPairFactory = bitcoinjs.ecpairfactory
ECPair = ECPairFactory(bitcoinjs.tiny_secp256k1)

// Start loading the wallet
$(document).ready(function(){

Expand Down Expand Up @@ -3258,8 +3263,8 @@ function signTransaction(network, source, destination, unsignedTx, callback){
var tx = bitcoinjs.Transaction.fromHex(unsignedTx),
netName = (net=='testnet') ? 'testnet' : 'bitcoin', // bitcoinjs
network = bitcoinjs.networks[netName],
txb = new bitcoinjs.TransactionBuilder(network),
keypair = bitcoinjs.ECPair.fromWIF(cwKey.getWIF(), network);
txb = new bitcoinjs.Psbt(network),
keypair = ECPair.fromWIF(cwKey.getWIF(), network);
// Callback to modify transaction after we get a list of UTXOs back
var utxoCb = function(data){
var utxoMap = {};
Expand All @@ -3276,13 +3281,30 @@ function signTransaction(network, source, destination, unsignedTx, callback){
// We get reversed tx hashes somehow after parsing
var txhash = tx.ins[i].hash.reverse().toString('hex');
var prev = utxoMap[txhash];
if(prev)
txb.addInput(tx.ins[i].hash.toString('hex'), prev.vout, null, input.output);
if(prev){
var nextInput = null
if (sourceIsBech32){
var witness = {script:input.output,value:prev.value}
nextInput = {hash:tx.ins[i].hash.toString('hex'), index:prev.vout, witnessUtxo:witness};
} else {
var prev_tx_hex_buffer = bitcoinjs.Buffer.from(prev.prev_tx_hex,"hex")
nextInput = {hash:tx.ins[i].hash.toString('hex'), index:prev.vout, nonWitnessUtxo:prev_tx_hex_buffer};
}

if (nextInput){
txb.addInput(nextInput);
} else {
console.log("Failed to sign input: script type not implemented")
return
}
}
}
// Handle adding outputs
for(var i=0; i < tx.outs.length; i++){
var txout = tx.outs[i];
txb.addOutput(txout.script, txout.value);
var outputScript = bitcoinjs.Buffer.from(txout.script.buffer.slice(txout.script.byteOffset, txout.script.byteLength + txout.script.byteOffset));

txb.addOutput({script:outputScript, value:txout.value});
}
// var signedHex = txb.build().toHex();
// console.log('signedHex before=',signedHex);
Expand All @@ -3296,7 +3318,7 @@ function signTransaction(network, source, destination, unsignedTx, callback){
redeemScript = // Future support for P2WSH
}*/
// Math.floor is less than ideal in this scenario, we need to get the raw satoshi value in the utxo map
txb.sign(i, keypair, null, null, prev.value, redeemScript);
txb.signInput(i, keypair);
} else {
// Throw error that we couldn't sign tx
console.log("Failed to sign transaction: " + "Incomplete SegWit inputs");
Expand All @@ -3306,14 +3328,15 @@ function signTransaction(network, source, destination, unsignedTx, callback){
var signedHex = false,
error = false;
try {
signedHex = txb.build().toHex();
txb.finalizeAllInputs();
signedHex = txb.extractTransaction().toHex();
} catch(e){
error = e;
}
cb(error, signedHex);
}
// Get list of utxo
getUTXOs(net, source, utxoCb);
// Get list of utxo with tx hex
getUtxosWithRawTransactions(net, source, utxoCb);
} else {
// Sign using bitcore
CWBitcore.signRawTransaction(unsignedTx, cwKey, cb);
Expand Down Expand Up @@ -3368,6 +3391,54 @@ function getUTXOs(network, address, callback){
});
}

// Handle getting a raw transaction for a given tx hash
function getRawTransactions(network, hashList, callback){
var txs = [];
var data = {
method: "getrawtransaction_batch",
params: {
txhash_list: hashList
},
jsonrpc: "2.0",
id: 0
};
cpRequest(network, data, function(o){
if(o && o.result){
if(callback)
callback(o.result);
};
});
}

//Handle getting utxos and their respectives prevout tx hex
function getUtxosWithRawTransactions(network, address, callback){
var utxosCb = function(dataUtxos){
//Adding hex data to the utxos
var rawTransactionCb = function (dataRawTransactions){
for (var nextUtxoIndex in dataUtxos){
var nextUtxo = dataUtxos[nextUtxoIndex]
nextUtxo["prev_tx_hex"] = dataRawTransactions[nextUtxo.txid]
}

if (callback){
callback(dataUtxos)
}
}

//Preparing the list with all obtained utxos hashes
var hashList = []
for (var nextUtxoIndex in dataUtxos){
var nextUtxo = dataUtxos[nextUtxoIndex]
hashList.push(nextUtxo.txid)
}

getRawTransactions(network, hashList, rawTransactionCb)
}

//Obtaining utxos
getUTXOs(network, address, utxosCb)
}

// Handle signing a message and returning the signature
function signMessage(network, source, message){
var net = (network=='testnet') ? 'testnet' : 'mainnet',
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"dependencies": {
"@bassettsj/livestamp": "^2.0.0",
"async": "^2.5.0",
"bitcoinjs-lib": "^4.0.2",
"bitcoinjs-lib": "^6.1.0",
"bootstrap": "^3.3.7",
"bootstrap-3-typeahead": "^4.0.2",
"bootstrap-dialog": "^1.34.6",
Expand All @@ -40,6 +40,8 @@
"jquery.qrcode": "^1.0.3",
"jquery.rateit": "^1.1.1",
"moment": "^2.18.1",
"numeraljs": "^1.5.6"
"numeraljs": "^1.5.6",
"nw-builder": "^4.2.7",
"tiny-secp256k1": "^2.2.2"
}
}