Workflow updating files of btcwallet

This commit is contained in:
RanchiMall Dev 2023-12-30 21:52:15 +00:00
parent fcee731f41
commit 7af9a713f7
3 changed files with 57 additions and 51 deletions

View File

@ -1966,7 +1966,9 @@
getRef('send_tx').reset(); getRef('send_tx').reset();
}).catch(error => { }).catch(error => {
console.error(error) console.error(error)
if (error === 'TypeError: Failed to fetch') { if (error.hasOwnProperty('hasInsufficientBalance')) {
notify('Insufficient balance', 'error')
} else if (error === 'TypeError: Failed to fetch') {
notify(` notify(`
<div style="display: grid; gap: 0.3rem;"> <div style="display: grid; gap: 0.3rem;">
<h4>Couldn't fetch transaction ID!</h4> <h4>Couldn't fetch transaction ID!</h4>
@ -2106,7 +2108,7 @@
document.getElementById('new_fee').querySelector('.currency-symbol').innerHTML = currencyIcons[selectedCurrency] document.getElementById('new_fee').querySelector('.currency-symbol').innerHTML = currencyIcons[selectedCurrency]
openPopup('increase_fee_popup') openPopup('increase_fee_popup')
} catch (e) { } catch (e) {
notify(e, 'error') notify(e.message || e, 'error')
} finally { } finally {
buttonLoader(button, false) buttonLoader(button, false)
} }
@ -2142,6 +2144,7 @@
changingFeeOf = null changingFeeOf = null
}) })
} catch (err) { } catch (err) {
console.log(typeof e)
notify(e, 'error') notify(e, 'error')
buttonLoader(document.getElementById('increase_fee'), false) buttonLoader(document.getElementById('increase_fee'), false)
} }

View File

@ -1,4 +1,4 @@
(function (EXPORTS) { //btcOperator v1.2.4 (function (EXPORTS) { //btcOperator v1.2.5
/* BTC Crypto and API Operator */ /* BTC Crypto and API Operator */
const btcOperator = EXPORTS; const btcOperator = EXPORTS;
const SATOSHI_IN_BTC = 1e8; const SATOSHI_IN_BTC = 1e8;
@ -17,6 +17,8 @@
} }
let isTor = false; let isTor = false;
checkIfTor().then(result => isTor = result); checkIfTor().then(result => isTor = result);
// NOTE: some APIs may not support all functions properly hence they are omitted
const APIs = btcOperator.APIs = [ const APIs = btcOperator.APIs = [
{ {
url: 'https://api.blockcypher.com/v1/btc/main/', url: 'https://api.blockcypher.com/v1/btc/main/',
@ -25,24 +27,6 @@
return fetch_api(`addrs/${addr}/balance`, { url: this.url }) return fetch_api(`addrs/${addr}/balance`, { url: this.url })
.then(result => util.Sat_to_BTC(result.balance)) .then(result => util.Sat_to_BTC(result.balance))
}, },
// unspent({ addr, allowUnconfirmedUtxos = false }) { // doesn't return correct utxos
// console.log('allowUnconfirmedUtxos', allowUnconfirmedUtxos)
// return fetch_api(`addrs/${addr}?unspentOnly=true&includeScript=true`, { url: this.url })
// .then(result => formatUtxos(result.txrefs, allowUnconfirmedUtxos))
// },
tx({ txid }) {
return fetch_api(`txs/${txid}`, { url: this.url })
.then(result => formatTx(result))
},
txHex({ txid }) {
return fetch_api(`txs/${txid}?includeHex=true`, { url: this.url })
.then(result => result.hex)
},
// txs({ addr, before, after }) { //NOTE: API doesn't return pending txs correctly
// return fetch_api(`addrs/${addr}/full?limit=50${before ? `&before=${before}` : ''}${after ? `&after=${after}` : ''}`, { url: this.url })
// .then(result => result.txs)
// },
async block({ id }) { async block({ id }) {
try { try {
let block = await fetch_api(`blocks/${id}`, { url: this.url }) let block = await fetch_api(`blocks/${id}`, { url: this.url })
@ -74,20 +58,16 @@
return util.Sat_to_BTC(balance) return util.Sat_to_BTC(balance)
}) })
}, },
// unspent({ addr }) { // API doesn't return utxo script // tx({ txid, url }) {
// return fetch_api(`address/${addr}/utxo`, { url: this.url }) // return fetch_api(`tx/${txid}`, { url: url || this.url })
// .then(result => formatUtxos(result)) // .then(result => formatTx(result))
// },
// txHex({ txid, url }) {
// return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
// },
// txs({ addr, before, after, url }) {
// return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: url || this.url })
// }, // },
tx({ txid, url }) {
return fetch_api(`tx/${txid}`, { url: url || this.url })
.then(result => formatTx(result))
},
txHex({ txid, url }) {
return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
},
txs({ addr, before, after, url }) {
return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: url || this.url })
},
async block({ id, url }) { async block({ id, url }) {
// if id is hex string then it is block hash // if id is hex string then it is block hash
try { try {
@ -117,10 +97,6 @@
return fetch_api(`address/${addr}`, { url: this.url }) return fetch_api(`address/${addr}`, { url: this.url })
.then(result => util.Sat_to_BTC(result.chain_stats.funded_txo_sum - result.chain_stats.spent_txo_sum)) .then(result => util.Sat_to_BTC(result.chain_stats.funded_txo_sum - result.chain_stats.spent_txo_sum))
}, },
// unspent({ addr }) { // API doesn't return utxo script
// return fetch_api(`address/${addr}/utxo`, { url: this.url })
// .then(result => formatUtxos(result))
// },
tx({ txid }) { tx({ txid }) {
return fetch_api(`tx/${txid}`, { url: this.url }) return fetch_api(`tx/${txid}`, { url: this.url })
.then(result => formatTx(result)) .then(result => formatTx(result))
@ -231,7 +207,6 @@
} }
const formatUtxos = btcOperator.util.format.utxos = async (utxos, allowUnconfirmedUtxos = false) => { const formatUtxos = btcOperator.util.format.utxos = async (utxos, allowUnconfirmedUtxos = false) => {
try { try {
console.log('allowUnconfirmedUtxos', allowUnconfirmedUtxos, utxos)
if (!allowUnconfirmedUtxos && !utxos || !Array.isArray(utxos)) if (!allowUnconfirmedUtxos && !utxos || !Array.isArray(utxos))
throw { throw {
message: "No utxos found", message: "No utxos found",
@ -265,7 +240,7 @@
size: size, size: size,
fee: fee || fees, fee: fee || fees,
double_spend, double_spend,
time: (time * 1000) || new Date(confirmed || received).getTime() || block_time * 1000 || new Date().getTime(), time: (time * 1000) || new Date(confirmed || received).getTime() || block_time * 1000 || Date.now(),
block_height: block_height || statusBlockHeight, block_height: block_height || statusBlockHeight,
block_hash: block_hash || statusBlockHash, block_hash: block_hash || statusBlockHash,
confirmations, confirmations,
@ -327,7 +302,7 @@
const { txid, hash, time, block_height, inputs, outputs, out, vin, vout, fee, fees, received, confirmed, status: { block_height: statusBlockHeight, block_time } = {} } = tx; const { txid, hash, time, block_height, inputs, outputs, out, vin, vout, fee, fees, received, confirmed, status: { block_height: statusBlockHeight, block_time } = {} } = tx;
let parsedTx = { let parsedTx = {
txid: hash || txid, txid: hash || txid,
time: (time * 1000) || new Date(confirmed || received).getTime() || block_time * 1000, time: (time * 1000) || new Date(confirmed || received).getTime() || block_time * 1000 || Date.now(),
block: block_height || statusBlockHeight, block: block_height || statusBlockHeight,
} }
//sender list //sender list
@ -772,7 +747,11 @@
} }
btcOperator.validateTxParameters = validateTxParameters; btcOperator.validateTxParameters = validateTxParameters;
function createTransaction(senders, redeemScripts, receivers, amounts, fee, change_address, fee_from_receiver, allowUnconfirmedUtxos = false) { const createTransaction = btcOperator.createTransaction = ({
senders, redeemScripts, receivers, amounts, fee, change_address,
fee_from_receiver, allowUnconfirmedUtxos = false, sendingTx = false,
hasInsufficientBalance = false
}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let total_amount = parseFloat(amounts.reduce((t, a) => t + a, 0).toFixed(8)); let total_amount = parseFloat(amounts.reduce((t, a) => t + a, 0).toFixed(8));
const tx = coinjs.transaction(); const tx = coinjs.transaction();
@ -805,11 +784,16 @@
result.output_amount = total_amount - (fee_from_receiver ? result.fee : 0); result.output_amount = total_amount - (fee_from_receiver ? result.fee : 0);
result.total_size = BASE_TX_SIZE + output_size + result.input_size; result.total_size = BASE_TX_SIZE + output_size + result.input_size;
result.transaction = tx; result.transaction = tx;
resolve(result); if (sendingTx && result.hasOwnProperty('hasInsufficientBalance') && result.hasInsufficientBalance)
reject({
message: "Insufficient balance",
...result
});
else
resolve(result);
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
} }
btcOperator.createTransaction = createTransaction;
function addInputs(tx, senders, redeemScripts, total_amount, fee, output_size, fee_from_receiver, allowUnconfirmedUtxos = false) { function addInputs(tx, senders, redeemScripts, total_amount, fee, output_size, fee_from_receiver, allowUnconfirmedUtxos = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -851,7 +835,12 @@
change_amount: required_amount * -1 //required_amount will be -ve of change_amount change_amount: required_amount * -1 //required_amount will be -ve of change_amount
}); });
else if (rec_args.n >= senders.length) { else if (rec_args.n >= senders.length) {
return reject("Insufficient Balance"); return resolve({
hasInsufficientBalance: true,
input_size: rec_args.input_size,
input_amount: rec_args.input_amount,
change_amount: required_amount * -1
});
} }
let addr = senders[rec_args.n], let addr = senders[rec_args.n],
rs = redeemScripts[rec_args.n]; rs = redeemScripts[rec_args.n];
@ -1127,8 +1116,10 @@
btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) { btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) {
options.sendingTx = true;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
createSignedTx(senders, privkeys, receivers, amounts, fee, options).then(result => { createSignedTx(senders, privkeys, receivers, amounts, fee, options).then(result => {
return
broadcastTx(result.transaction.serialize()) broadcastTx(result.transaction.serialize())
.then(txid => resolve(txid)) .then(txid => resolve(txid))
.catch(error => reject(error)); .catch(error => reject(error));
@ -1150,7 +1141,7 @@
receivers, receivers,
amounts, amounts,
fee, fee,
change_address: options.change_address ...options
})); }));
} catch (e) { } catch (e) {
return reject(e) return reject(e)
@ -1165,7 +1156,11 @@
if (redeemScripts.includes(null)) //TODO: segwit if (redeemScripts.includes(null)) //TODO: segwit
return reject("Unable to get redeem-script"); return reject("Unable to get redeem-script");
//create transaction //create transaction
createTransaction(senders, redeemScripts, receivers, amounts, fee, options.change_address || senders[0], options.fee_from_receiver).then(result => { createTransaction({
senders, redeemScripts, receivers, amounts, fee,
change_address: options.change_address || senders[0],
...options
}).then(result => {
let tx = result.transaction; let tx = result.transaction;
console.debug("Unsigned:", tx.serialize()); console.debug("Unsigned:", tx.serialize());
new Set(wif_keys).forEach(key => tx.sign(key, 1 /*sighashtype*/)); //Sign the tx using private key WIF new Set(wif_keys).forEach(key => tx.sign(key, 1 /*sighashtype*/)); //Sign the tx using private key WIF
@ -1198,7 +1193,11 @@
if (redeemScripts.includes(null)) //TODO: segwit if (redeemScripts.includes(null)) //TODO: segwit
return reject("Unable to get redeem-script"); return reject("Unable to get redeem-script");
//create transaction //create transaction
createTransaction(senders, redeemScripts, receivers, amounts, fee, options.change_address || senders[0], options.fee_from_receiver, options.allowUnconfirmedUtxos).then(result => { createTransaction({
senders, redeemScripts, receivers, amounts, fee,
change_address: options.change_address || senders[0],
...options
}).then(result => {
result.tx_hex = result.transaction.serialize(); result.tx_hex = result.transaction.serialize();
delete result.transaction; delete result.transaction;
resolve(result); resolve(result);
@ -1234,7 +1233,12 @@
return reject(e) return reject(e)
} }
//create transaction //create transaction
createTransaction([sender], [redeemScript], receivers, amounts, fee, options.change_address || sender, options.fee_from_receiver).then(result => { createTransaction({
senders: [sender], redeemScripts: [redeemScript],
receivers, amounts, fee,
change_address: options.change_address || sender,
...options
}).then(result => {
result.tx_hex = result.transaction.serialize(); result.tx_hex = result.transaction.serialize();
delete result.transaction; delete result.transaction;
resolve(result); resolve(result);
@ -1400,7 +1404,6 @@
multiApi('txs', { addr: address }) multiApi('txs', { addr: address })
]).then(([balance, txs]) => { ]).then(([balance, txs]) => {
const parsedTxs = txs.map(tx => parseTx(tx, address)); const parsedTxs = txs.map(tx => parseTx(tx, address));
console.log(parsedTxs);
resolve({ resolve({
address, address,
balance, balance,

File diff suppressed because one or more lines are too long