const screenXpubsToolsScript = { explorerInfo: null, currentXpub: null, initPage: function() { this.getExplorerInfo() // Sets the event handlers $('#btn-xpub-search-go').click(() => {this.searchXpub()}) $('#btn-xpub-details-reset').click(() => {this.showSearchForm()}) $('#btn-xpub-details-rescan').click(() => {this.showRescanForm()}) $('#btn-xpub-rescan-go').click(() => {this.rescanXpub()}) $('#btn-xpub-rescan-cancel').click(() => {this.hideRescanForm()}) $('#btn-xpub-import-go').click(() => {this.importXpub()}) $('#btn-xpub-details-retype').click(() => {this.showImportForm(true)}) $('#btn-xpub-import-cancel').click(() => {this.showSearchForm()}) $('#xpubs-tool').keyup(evt => { if (evt.keyCode === 13) { this.searchXpub() } }) }, preparePage: function() { this.hideRescanForm() this.showSearchForm() $("#xpub").focus() }, getExplorerInfo: function() { lib_api.getExplorerPairingInfo().then(explorerInfo => { this.explorerInfo = explorerInfo }).catch(e => { lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) console.log(e) }) }, searchXpub: function() { lib_msg.displayMessage('Search in progress...'); const xpub = $('#xpub').val() this.currentXpub = xpub return this._searchXpub(xpub).then(() => { lib_msg.cleanMessagesUi() }) }, _searchXpub: function(xpub) { return lib_api.getXpubInfo(xpub).then(xpubInfo => { if (xpubInfo && xpubInfo['tracked']) { this.setXpubDetails(xpubInfo) this.showXpubDetails() const jsonData = {'active': xpub} return lib_api.getWallet(jsonData).then(walletInfo => { // Display the txs const txs = walletInfo['txs'] for (let tx of txs) this.setTxDetails(tx) // Display the UTXOs const utxos = walletInfo['unspent_outputs'].sort((a,b) => { return a['confirmations'] - b['confirmations'] }) $('#xpub-nb-utxos').text(utxos.length) for (let utxo of utxos) this.setUtxoDetails(utxo) }) } else { lib_msg.displayErrors('xpub not found') this.showImportForm(false) } }).catch(e => { lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) console.log(e) throw e }) }, importXpub: function() { lib_msg.displayMessage('Processing xpub import...'); const jsonData = { 'xpub': this.currentXpub, 'type': 'restore', 'force': true } const derivType = $('#import-deriv-type').val() if (derivType == 'bip49' || derivType == 'bip84') { jsonData['segwit'] = derivType } else if (derivType == 'auto') { if (this.currentXpub.startsWith('ypub')) jsonData['segwit'] = 'bip49' else if (this.currentXpub.startsWith('zpub')) jsonData['segwit'] = 'bip84' } return lib_api.postXpub(jsonData) .then(result => { this._searchXpub(this.currentXpub).then(() => { lib_msg.displayInfo('Import complete') }) }).catch(e => { lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) console.log(e) }) }, rescanXpub: function() { lib_msg.displayMessage('Processing xpub rescan...'); let startIdx = $('#rescan-start-idx').val() startIdx = (startIdx == null) ? 0 : parseInt(startIdx) let lookahead = $('#rescan-lookahead').val() lookahead = (lookahead == null) ? 100 : parseInt(lookahead) return lib_api.getXpubRescan(this.currentXpub, lookahead, startIdx) .then(result => { this.hideRescanForm() this._searchXpub(this.currentXpub).then(() => { lib_msg.displayInfo('Rescan complete') }) }).catch(e => { lib_msg.displayErrors(lib_msg.extractJqxhrErrorMsg(e)) console.log(e) }) }, setXpubDetails: function(xpubInfo) { $('tr.tx-row').remove() $('tr.utxo-row').remove() $('#xpub-value').text(this.currentXpub) $('#xpub-import-date').text(xpubInfo['created']) $('#xpub-deriv-type').text(xpubInfo['derivation']) $('#xpub-nb-txs').text(xpubInfo['n_tx']) $('#xpub-nb-utxos').text('-') const balance = parseInt(xpubInfo['balance']) / 100000000 $('#xpub-balance').text(`${balance} BTC`) $('#xpub-deriv-account').text(xpubInfo['account']) $('#xpub-deriv-depth').text(xpubInfo['depth']) $('#xpub-idx-unused-ext').text(xpubInfo['unused']['external']) $('#xpub-idx-derived-ext').text(xpubInfo['derived']['external']) $('#xpub-idx-unused-int').text(xpubInfo['unused']['internal']) $('#xpub-idx-derived-int').text(xpubInfo['derived']['internal']) }, setTxDetails: function(tx) { const txid = tx['hash'] const txidDisplay = `${txid.substring(0,50)}...` const amount = parseInt(tx['result']) / 100000000 const amountLabel = amount < 0 ? amount : `+${amount}` const amountStyle = amount < 0 ? 'amount-sent' : 'amount-received' const date = lib_fmt.unixTsToLocaleString(tx['time']) const txUrl = lib_cmn.getExplorerTxUrl(txid, this.explorerInfo) const newRow = `