Workflow updating files of btcwallet

This commit is contained in:
RanchiMall Dev 2024-01-04 18:51:51 +00:00
parent a859755e67
commit 490c9b9f4e
5 changed files with 78 additions and 59 deletions

View File

@ -121,7 +121,7 @@
<div id="tx_details" class="grid gap-2"></div> <div id="tx_details" class="grid gap-2"></div>
</section> </section>
</div> </div>
<div id="send" class="page hidden" data-sm-containment> <div id="send" class="page hidden">
<sm-form id="send_tx" skip-submit> <sm-form id="send_tx" skip-submit>
<div class="margin-bottom-0-5"> <div class="margin-bottom-0-5">
<div class="flex align-center space-between margin-bottom-0-5"> <div class="flex align-center space-between margin-bottom-0-5">
@ -1128,12 +1128,20 @@
}, },
{ {
selector: '[data-private-key]', selector: '[data-private-key]',
customValidation: (value) => { customValidation: (value, inputElem) => {
if (!value) return { isValid: false, errorText: 'Please enter a private key' } if (!value) return { isValid: false, errorText: 'Please enter a private key' }
return { if (floCrypto.getPubKeyHex(value)) {
isValid: floCrypto.getPubKeyHex(value), const forAddress = inputElem.dataset.forAddress
errorText: `Invalid private key.<br> It usually starts with "L" or "R"` if (!forAddress) return { isValid: true }
} return {
isValid: btcOperator.verifyKey(forAddress, value),
errorText: `This private key does not match the address ${forAddress}`
}
} else
return {
isValid: false,
errorText: `Invalid private key. Please check and try again.`
}
} }
}, },
{ {
@ -2070,7 +2078,7 @@
<div class="label">Address</div> <div class="label">Address</div>
<b class="sender__address wrap-around">${address}</b> <b class="sender__address wrap-around">${address}</b>
</div> </div>
<sm-input class="sender__private-key password-field" type="password" placeholder="Private Key" animate required> <sm-input class="sender__private-key password-field" type="password" placeholder="Private Key" data-for-address=${address} data-private-key animate required>
<svg class="icon" slot="icon" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <g> <rect fill="none" height="24" width="24"></rect> </g> <g> <path d="M21,10h-8.35C11.83,7.67,9.61,6,7,6c-3.31,0-6,2.69-6,6s2.69,6,6,6c2.61,0,4.83-1.67,5.65-4H13l2,2l2-2l2,2l4-4.04L21,10z M7,15c-1.65,0-3-1.35-3-3c0-1.65,1.35-3,3-3s3,1.35,3,3C10,13.65,8.65,15,7,15z"></path> </g> </svg> <svg class="icon" slot="icon" xmlns="http://www.w3.org/2000/svg" enable-background="new 0 0 24 24" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <g> <rect fill="none" height="24" width="24"></rect> </g> <g> <path d="M21,10h-8.35C11.83,7.67,9.61,6,7,6c-3.31,0-6,2.69-6,6s2.69,6,6,6c2.61,0,4.83-1.67,5.65-4H13l2,2l2-2l2,2l4-4.04L21,10z M7,15c-1.65,0-3-1.35-3-3c0-1.65,1.35-3,3-3s3,1.35,3,3C10,13.65,8.65,15,7,15z"></path> </g> </svg>
<label slot="right" class="interact"> <label slot="right" class="interact">
<input type="checkbox" class="hidden" autocomplete="off" readonly="" onchange="togglePrivateKeyVisibility(this)"> <input type="checkbox" class="hidden" autocomplete="off" readonly="" onchange="togglePrivateKeyVisibility(this)">
@ -2148,8 +2156,7 @@
changingFeeOf = null changingFeeOf = null
}) })
} catch (err) { } catch (err) {
console.log(typeof e) notify(err, '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.5 (function (EXPORTS) { //btcOperator v1.2.6
/* 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;
@ -18,6 +18,25 @@
let isTor = false; let isTor = false;
checkIfTor().then(result => isTor = result); checkIfTor().then(result => isTor = result);
async function post(url, data, { asText = false } = {}) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
if (response.ok) {
return asText ? await response.text() : await response.json()
} else {
throw response
}
} catch (e) {
throw e
}
}
// NOTE: some APIs may not support all functions properly hence they are omitted // NOTE: some APIs may not support all functions properly hence they are omitted
const APIs = btcOperator.APIs = [ const APIs = btcOperator.APIs = [
{ {
@ -35,15 +54,13 @@
console.log(e) console.log(e)
} }
}, },
broadcast({ rawTxHex, url }) { async broadcast({ rawTxHex, url }) {
return fetch(`${this.url}txs/push`, { try {
method: 'POST', const result = await post(`${url || this.url}txs/push`, { tx: rawTxHex })
headers: { return result.hash
'Content-Type': 'application/json' } catch (e) {
}, throw e
body: JSON.stringify({ tx: rawTxHex }) }
}).then(response => response.json())
.then(result => result.hash)
} }
}, },
{ {
@ -77,17 +94,11 @@
const block = await fetch_api(`block/${blockHash}`, { url: url || this.url }) const block = await fetch_api(`block/${blockHash}`, { url: url || this.url })
return formatBlock(block) return formatBlock(block)
} catch (e) { } catch (e) {
console.error(e) throw e
} }
}, },
broadcast({ rawTxHex, url }) { async broadcast({ rawTxHex, url }) {
return fetch(`${url || this.url}tx`, { return post(`${url || this.url}tx`, { tx: rawTxHex }, { asText: true })
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ tx: rawTxHex })
}).then(response => response.text())
} }
}, },
{ {
@ -97,17 +108,17 @@
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))
}, },
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))
}, // },
txHex({ txid }) { // txHex({ txid }) {
return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true }) // return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true })
}, // },
txs({ addr, before, after }) { // txs({ addr, before, after }) {
return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url }) // return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url })
}, // },
async block({ id }) { async block({ id }) {
// if id is hex string then it is block hash // if id is hex string then it is block hash
try { try {
@ -117,17 +128,11 @@
const block = await fetch_api(`block/${blockHash}`, { url: this.url }) const block = await fetch_api(`block/${blockHash}`, { url: this.url })
return formatBlock(block) return formatBlock(block)
} catch (e) { } catch (e) {
console.error(e) throw e
} }
}, },
broadcast({ rawTxHex, url }) { async broadcast({ rawTxHex, url }) {
return fetch(`${this.url}tx`, { return post(`${url || this.url}tx`, { tx: rawTxHex }, { asText: true })
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ tx: rawTxHex })
}).then(response => response.text())
} }
}, },
{ {
@ -164,7 +169,7 @@
} }
return formatBlock(block) return formatBlock(block)
} catch (e) { } catch (e) {
console.error(e) throw e
} }
}, },
async blockTxs({ id }) { async blockTxs({ id }) {
@ -179,7 +184,7 @@
} }
return block.tx return block.tx
} catch (e) { } catch (e) {
console.error(e)
} }
} }
} }
@ -202,7 +207,7 @@
details.next_block = next_block[0] details.next_block = next_block[0]
return details return details
} catch (e) { } catch (e) {
console.error(e) throw e
} }
} }
const formatUtxos = btcOperator.util.format.utxos = async (utxos, allowUnconfirmedUtxos = false) => { const formatUtxos = btcOperator.util.format.utxos = async (utxos, allowUnconfirmedUtxos = false) => {
@ -261,7 +266,7 @@
}) })
} }
} catch (e) { } catch (e) {
console.error(e) throw e
} }
} }
@ -288,12 +293,12 @@
} }
throw "No API available" throw "No API available"
} catch (error) { } catch (error) {
if (error.code && error.code === 1000) { console.error(error)
throw error.message; if (error.code && [429, 404].includes(error.code)) {
} else {
console.debug(error);
APIs[index].coolDownTime = new Date().getTime() + 1000 * 60 * 10; // 10 minutes APIs[index].coolDownTime = new Date().getTime() + 1000 * 60 * 10; // 10 minutes
return multiApi(fnName, { index: index + 1, ...args }); return multiApi(fnName, { index: index + 1, ...args });
} else {
throw error.message || error;
} }
} }
}; };
@ -392,6 +397,13 @@
const broadcastTx = btcOperator.broadcastTx = rawTxHex => new Promise((resolve, reject) => { const broadcastTx = btcOperator.broadcastTx = rawTxHex => new Promise((resolve, reject) => {
console.log('txHex:', rawTxHex) console.log('txHex:', rawTxHex)
// return multiApi('broadcast', { rawTxHex })
// .then(result => {
// resolve(result)
// })
// .catch(error => {
// reject(error)
// })
let url = 'https://coinb.in/api/?uid=1&key=12345678901234567890123456789012&setmodule=bitcoin&request=sendrawtransaction'; let url = 'https://coinb.in/api/?uid=1&key=12345678901234567890123456789012&setmodule=bitcoin&request=sendrawtransaction';
fetch(url, { fetch(url, {
method: 'POST', method: 'POST',
@ -400,7 +412,7 @@
}, },
body: "rawtx=" + rawTxHex body: "rawtx=" + rawTxHex
}).then(response => { }).then(response => {
// multiApi('broadcast', { rawTxHex }).then(response => { console.log(response)
response.text().then(resultText => { response.text().then(resultText => {
let r = resultText.match(/<result>.*<\/result>/); let r = resultText.match(/<result>.*<\/result>/);
if (!r) if (!r)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long