code refactoring
This commit is contained in:
parent
5b72b3021e
commit
c6e7392b55
48
index.html
48
index.html
@ -190,11 +190,11 @@
|
|||||||
<script src="https://unpkg.com/uhtml@3.0.1/es.js"></script>
|
<script src="https://unpkg.com/uhtml@3.0.1/es.js"></script>
|
||||||
<script src="scripts/components.min.js" type="text/javascript"></script>
|
<script src="scripts/components.min.js" type="text/javascript"></script>
|
||||||
<script src="scripts/btcwallet_scripts_lib.js" type="text/javascript"></script>
|
<script src="scripts/btcwallet_scripts_lib.js" type="text/javascript"></script>
|
||||||
<script src="scripts/btcOperator.js"></script>
|
<script src="scripts/btcOperator.js" type="text/javascript"></script>
|
||||||
<script src="scripts/floCrypto.js"></script>
|
<script src="scripts/floCrypto.js" type="text/javascript"></script>
|
||||||
<script src="scripts/tap_combined.js" type="text/javascript"></script>
|
<script src="scripts/tap_combined.js" type="text/javascript"></script>
|
||||||
<script src="scripts/keccak.js"></script>
|
<script src="scripts/keccak.js" type="text/javascript"></script>
|
||||||
<script src="scripts/floEthereum.js"></script>
|
<script src="scripts/floEthereum.js" type="text/javascript"></script>
|
||||||
<script id="ui_utils">
|
<script id="ui_utils">
|
||||||
const uiGlobals = {}
|
const uiGlobals = {}
|
||||||
const { html, svg, render: renderElem } = uhtml;
|
const { html, svg, render: renderElem } = uhtml;
|
||||||
@ -1196,7 +1196,7 @@
|
|||||||
<output id="address_balance" class="amount-shown"></output>
|
<output id="address_balance" class="amount-shown"></output>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex align-center space-between sticky top-0" style="background-color: rgba(var(--foreground-color), 1); z-index: 2">
|
<div class="flex align-center space-between sticky top-0" style="background-color: rgba(var(--foreground-color), 1); z-index: 2">
|
||||||
<h5>Transactions</h5>
|
<h4>Transactions</h4>
|
||||||
<sm-chips id="filter_selector" onchange=${handleFiltering} role="listbox">
|
<sm-chips id="filter_selector" onchange=${handleFiltering} role="listbox">
|
||||||
<sm-chip value="all" selected="" role="option" tabindex="0">All</sm-chip>
|
<sm-chip value="all" selected="" role="option" tabindex="0">All</sm-chip>
|
||||||
<sm-chip value="sent" role="option" tabindex="0">Sent</sm-chip>
|
<sm-chip value="sent" role="option" tabindex="0">Sent</sm-chip>
|
||||||
@ -1680,9 +1680,6 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
//This library uses API provided by chain.so (https://chain.so/)
|
|
||||||
const URL = "https://blockchain.info/";
|
|
||||||
|
|
||||||
const DUST_AMT = 546,
|
const DUST_AMT = 546,
|
||||||
MIN_FEE_UPDATE = 219;
|
MIN_FEE_UPDATE = 219;
|
||||||
|
|
||||||
@ -1699,35 +1696,6 @@
|
|||||||
SEGWIT_OUTPUT_SIZE = 23,
|
SEGWIT_OUTPUT_SIZE = 23,
|
||||||
BECH32M_OUTPUT_SIZE = 35; // Check this later
|
BECH32M_OUTPUT_SIZE = 35; // Check this later
|
||||||
|
|
||||||
function get_fee_rate() {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
fetch('https://api.blockchain.info/mempool/fees').then(response => {
|
|
||||||
if (response.ok)
|
|
||||||
response.json()
|
|
||||||
.then(result => resolve(btcOperator.util.Sat_to_BTC(result.regular)))
|
|
||||||
.catch(error => reject(error));
|
|
||||||
else
|
|
||||||
reject(response);
|
|
||||||
}).catch(error => reject(error))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetch_api = function (api, json_res = true) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
console.debug(URL + api);
|
|
||||||
fetch(URL + api).then(response => {
|
|
||||||
if (response.ok) {
|
|
||||||
(json_res ? response.json() : response.text())
|
|
||||||
.then(result => resolve(result))
|
|
||||||
.catch(error => reject(error))
|
|
||||||
} else {
|
|
||||||
response.json()
|
|
||||||
.then(result => reject(result))
|
|
||||||
.catch(error => reject(error))
|
|
||||||
}
|
|
||||||
}).catch(error => reject(error))
|
|
||||||
})
|
|
||||||
};
|
|
||||||
function getTaprootAddress(wif) {
|
function getTaprootAddress(wif) {
|
||||||
if (!wif)
|
if (!wif)
|
||||||
wif = coinjs.newKeys().wif
|
wif = coinjs.newKeys().wif
|
||||||
@ -1783,8 +1751,8 @@
|
|||||||
const amountInSat = btcOperator.util.BTC_to_Sat(totalAmount)
|
const amountInSat = btcOperator.util.BTC_to_Sat(totalAmount)
|
||||||
// check if sender has enough balance
|
// check if sender has enough balance
|
||||||
const senderBalance = await btcOperator.getBalance(address)
|
const senderBalance = await btcOperator.getBalance(address)
|
||||||
const feeRate = await get_fee_rate();
|
const feeRate = await btcOperator.getFeeRate();
|
||||||
let calculatedFee = 0
|
let calculatedFee = 0;
|
||||||
const { input_size } = await addUTXOs(tx, tr, [address], btcOperator.util.BTC_to_Sat(totalAmount), feeRate)
|
const { input_size } = await addUTXOs(tx, tr, [address], btcOperator.util.BTC_to_Sat(totalAmount), feeRate)
|
||||||
calculatedFee += input_size
|
calculatedFee += input_size
|
||||||
// add receivers
|
// add receivers
|
||||||
@ -1828,7 +1796,7 @@
|
|||||||
return reject(`Insufficient Balance.`);
|
return reject(`Insufficient Balance.`);
|
||||||
let addr = senders[rec_args.n];
|
let addr = senders[rec_args.n];
|
||||||
let size_per_input = _sizePerInput(addr);
|
let size_per_input = _sizePerInput(addr);
|
||||||
fetch_api(`unspent?active=${addr}`).then(result => {
|
btcOperator.fetch(`unspent?active=${addr}`).then(result => {
|
||||||
let utxos = result.unspent_outputs;
|
let utxos = result.unspent_outputs;
|
||||||
// console.debug("add-utxo", addr, required_amount, utxos);
|
// console.debug("add-utxo", addr, required_amount, utxos);
|
||||||
for (let i = 0; i < utxos.length && required_amount > 0; i++) {
|
for (let i = 0; i < utxos.length && required_amount > 0; i++) {
|
||||||
|
|||||||
@ -32,7 +32,7 @@
|
|||||||
util.Sat_to_BTC = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8));
|
util.Sat_to_BTC = value => parseFloat((value / SATOSHI_IN_BTC).toFixed(8));
|
||||||
util.BTC_to_Sat = value => parseInt(value * SATOSHI_IN_BTC);
|
util.BTC_to_Sat = value => parseInt(value * SATOSHI_IN_BTC);
|
||||||
|
|
||||||
function get_fee_rate() {
|
const get_fee_rate = btcOperator.getFeeRate = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch('https://api.blockchain.info/mempool/fees').then(response => {
|
fetch('https://api.blockchain.info/mempool/fees').then(response => {
|
||||||
if (response.ok)
|
if (response.ok)
|
||||||
@ -674,113 +674,113 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
btcOperator.editFee_corewallet = function(tx_hex, new_fee, private_keys, change_only = true) {
|
btcOperator.editFee_corewallet = function (tx_hex, new_fee, private_keys, change_only = true) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!Array.isArray(private_keys))
|
if (!Array.isArray(private_keys))
|
||||||
private_keys = [private_keys];
|
private_keys = [private_keys];
|
||||||
tx_fetch_for_editing(tx_hex).then(tx => {
|
tx_fetch_for_editing(tx_hex).then(tx => {
|
||||||
parseTransaction(tx).then(tx_parsed => {
|
parseTransaction(tx).then(tx_parsed => {
|
||||||
if (tx_parsed.fee >= new_fee)
|
if (tx_parsed.fee >= new_fee)
|
||||||
return reject("Fees can only be increased");
|
return reject("Fees can only be increased");
|
||||||
|
|
||||||
//editable addresses in output values (for fee increase)
|
//editable addresses in output values (for fee increase)
|
||||||
var edit_output_address = new Set();
|
var edit_output_address = new Set();
|
||||||
if (change_only === true) //allow only change values (ie, sender address) to be edited to inc fee
|
if (change_only === true) //allow only change values (ie, sender address) to be edited to inc fee
|
||||||
tx_parsed.inputs.forEach(inp => edit_output_address.add(inp.address));
|
tx_parsed.inputs.forEach(inp => edit_output_address.add(inp.address));
|
||||||
else if (change_only === false) //allow all output values to be edited
|
else if (change_only === false) //allow all output values to be edited
|
||||||
tx_parsed.outputs.forEach(out => edit_output_address.add(out.address));
|
tx_parsed.outputs.forEach(out => edit_output_address.add(out.address));
|
||||||
else if (typeof change_only == 'string') // allow only given receiver id output to be edited
|
else if (typeof change_only == 'string') // allow only given receiver id output to be edited
|
||||||
edit_output_address.add(change_only);
|
edit_output_address.add(change_only);
|
||||||
else if (Array.isArray(change_only)) //allow only given set of receiver id outputs to be edited
|
else if (Array.isArray(change_only)) //allow only given set of receiver id outputs to be edited
|
||||||
change_only.forEach(id => edit_output_address.add(id));
|
change_only.forEach(id => edit_output_address.add(id));
|
||||||
|
|
||||||
//edit output values to increase fee
|
//edit output values to increase fee
|
||||||
let inc_fee = util.BTC_to_Sat(new_fee - tx_parsed.fee);
|
let inc_fee = util.BTC_to_Sat(new_fee - tx_parsed.fee);
|
||||||
if (inc_fee < MIN_FEE_UPDATE)
|
if (inc_fee < MIN_FEE_UPDATE)
|
||||||
return reject(`Insufficient additional fee. Minimum increment: ${MIN_FEE_UPDATE}`);
|
return reject(`Insufficient additional fee. Minimum increment: ${MIN_FEE_UPDATE}`);
|
||||||
for (let i = tx.outs.length - 1; i >= 0 && inc_fee > 0; i--) //reduce in reverse order
|
for (let i = tx.outs.length - 1; i >= 0 && inc_fee > 0; i--) //reduce in reverse order
|
||||||
if (edit_output_address.has(tx_parsed.outputs[i].address)) {
|
if (edit_output_address.has(tx_parsed.outputs[i].address)) {
|
||||||
let current_value = tx.outs[i].value;
|
let current_value = tx.outs[i].value;
|
||||||
if (current_value instanceof BigInteger) //convert BigInteger class to inv value
|
if (current_value instanceof BigInteger) //convert BigInteger class to inv value
|
||||||
current_value = current_value.intValue();
|
current_value = current_value.intValue();
|
||||||
//edit the value as required
|
//edit the value as required
|
||||||
if (current_value > inc_fee) {
|
if (current_value > inc_fee) {
|
||||||
tx.outs[i].value = current_value - inc_fee;
|
tx.outs[i].value = current_value - inc_fee;
|
||||||
inc_fee = 0;
|
inc_fee = 0;
|
||||||
} else {
|
} else {
|
||||||
inc_fee -= current_value;
|
inc_fee -= current_value;
|
||||||
tx.outs[i].value = 0;
|
tx.outs[i].value = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (inc_fee > 0) {
|
||||||
|
let max_possible_fee = util.BTC_to_Sat(new_fee) - inc_fee; //in satoshi
|
||||||
|
return reject(`Insufficient output values to increase fee. Maximum fee possible: ${util.Sat_to_BTC(max_possible_fee)}`);
|
||||||
}
|
}
|
||||||
if (inc_fee > 0) {
|
tx.outs = tx.outs.filter(o => o.value >= DUST_AMT); //remove all output with value less than DUST amount
|
||||||
let max_possible_fee = util.BTC_to_Sat(new_fee) - inc_fee; //in satoshi
|
|
||||||
return reject(`Insufficient output values to increase fee. Maximum fee possible: ${util.Sat_to_BTC(max_possible_fee)}`);
|
|
||||||
}
|
|
||||||
tx.outs = tx.outs.filter(o => o.value >= DUST_AMT); //remove all output with value less than DUST amount
|
|
||||||
|
|
||||||
//remove existing signatures and reset the scripts
|
//remove existing signatures and reset the scripts
|
||||||
let wif_keys = [];
|
let wif_keys = [];
|
||||||
let witness_position = 0;
|
let witness_position = 0;
|
||||||
for (let i in tx.ins) {
|
for (let i in tx.ins) {
|
||||||
var addr = tx_parsed.inputs[i].address,
|
var addr = tx_parsed.inputs[i].address,
|
||||||
value = util.BTC_to_Sat(tx_parsed.inputs[i].value);
|
value = util.BTC_to_Sat(tx_parsed.inputs[i].value);
|
||||||
let addr_decode = coinjs.addressDecode(addr);
|
let addr_decode = coinjs.addressDecode(addr);
|
||||||
|
|
||||||
//find the correct key for addr
|
//find the correct key for addr
|
||||||
var privKey = private_keys.find(pk => verifyKey(addr, pk));
|
var privKey = private_keys.find(pk => verifyKey(addr, pk));
|
||||||
if (!privKey)
|
if (!privKey)
|
||||||
return reject(`Private key missing for ${addr}`);
|
return reject(`Private key missing for ${addr}`);
|
||||||
//find redeemScript (if any)
|
//find redeemScript (if any)
|
||||||
const rs = _redeemScript(addr, privKey);
|
const rs = _redeemScript(addr, privKey);
|
||||||
rs === false ? wif_keys.unshift(privKey) : wif_keys.push(privKey); //sorting private-keys (wif)
|
rs === false ? wif_keys.unshift(privKey) : wif_keys.push(privKey); //sorting private-keys (wif)
|
||||||
//reset the script for re-signing
|
//reset the script for re-signing
|
||||||
var script;
|
var script;
|
||||||
if (!rs || !rs.length) {
|
if (!rs || !rs.length) {
|
||||||
//legacy script (derive from address)
|
//legacy script (derive from address)
|
||||||
let s = coinjs.script();
|
let s = coinjs.script();
|
||||||
s.writeOp(118); //OP_DUP
|
s.writeOp(118); //OP_DUP
|
||||||
s.writeOp(169); //OP_HASH160
|
s.writeOp(169); //OP_HASH160
|
||||||
s.writeBytes(addr_decode.bytes);
|
s.writeBytes(addr_decode.bytes);
|
||||||
s.writeOp(136); //OP_EQUALVERIFY
|
s.writeOp(136); //OP_EQUALVERIFY
|
||||||
s.writeOp(172); //OP_CHECKSIG
|
s.writeOp(172); //OP_CHECKSIG
|
||||||
script = Crypto.util.bytesToHex(s.buffer);
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
} else if (((rs.match(/^00/) && rs.length == 44)) || (rs.length == 40 && rs.match(/^[a-f0-9]+$/gi))) {
|
} else if (((rs.match(/^00/) && rs.length == 44)) || (rs.length == 40 && rs.match(/^[a-f0-9]+$/gi))) {
|
||||||
//redeemScript for segwit/bech32
|
//redeemScript for segwit/bech32
|
||||||
let s = coinjs.script();
|
let s = coinjs.script();
|
||||||
s.writeBytes(Crypto.util.hexToBytes(rs));
|
s.writeBytes(Crypto.util.hexToBytes(rs));
|
||||||
s.writeOp(0);
|
s.writeOp(0);
|
||||||
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
||||||
script = Crypto.util.bytesToHex(s.buffer);
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
if (addr_decode == "bech32") {witness_position = witness_position + 1;} //bech32 has witness
|
if (addr_decode == "bech32") { witness_position = witness_position + 1; } //bech32 has witness
|
||||||
} else if (addr_decode.type === 'multisigBech32') {
|
} else if (addr_decode.type === 'multisigBech32') {
|
||||||
var rs_array = [];
|
var rs_array = [];
|
||||||
rs_array = btcOperator.extractLastHexStrings(tx.witness);
|
rs_array = btcOperator.extractLastHexStrings(tx.witness);
|
||||||
let redeemScript = rs_array[witness_position];
|
let redeemScript = rs_array[witness_position];
|
||||||
witness_position = witness_position + 1;
|
witness_position = witness_position + 1;
|
||||||
|
|
||||||
//redeemScript multisig (bech32)
|
//redeemScript multisig (bech32)
|
||||||
let s = coinjs.script();
|
let s = coinjs.script();
|
||||||
s.writeBytes(Crypto.util.hexToBytes(redeemScript));
|
s.writeBytes(Crypto.util.hexToBytes(redeemScript));
|
||||||
s.writeOp(0);
|
s.writeOp(0);
|
||||||
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
s.writeBytes(coinjs.numToBytes(value.toFixed(0), 8));
|
||||||
script = Crypto.util.bytesToHex(s.buffer);
|
script = Crypto.util.bytesToHex(s.buffer);
|
||||||
} else //redeemScript for multisig (segwit)
|
} else //redeemScript for multisig (segwit)
|
||||||
script = rs;
|
script = rs;
|
||||||
tx.ins[i].script = coinjs.script(script);
|
tx.ins[i].script = coinjs.script(script);
|
||||||
}
|
}
|
||||||
tx.witness = false; //remove all witness signatures
|
tx.witness = false; //remove all witness signatures
|
||||||
console.debug("Unsigned:", tx.serialize());
|
console.debug("Unsigned:", tx.serialize());
|
||||||
//re-sign the transaction
|
//re-sign the transaction
|
||||||
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
|
||||||
if (btcOperator.checkSigned(tx)) {
|
if (btcOperator.checkSigned(tx)) {
|
||||||
resolve(tx.serialize());
|
resolve(tx.serialize());
|
||||||
} else {
|
} else {
|
||||||
reject("All private keys not present");
|
reject("All private keys not present");
|
||||||
}
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
}).catch(error => reject(error))
|
}).catch(error => reject(error))
|
||||||
}).catch(error => reject(error))
|
})
|
||||||
})
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) {
|
btcOperator.sendTx = function (senders, privkeys, receivers, amounts, fee = null, options = {}) {
|
||||||
|
|||||||
1
scripts/btcOperator.min.js
vendored
Normal file
1
scripts/btcOperator.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user