From 684917a050b8496de94c86955f2797b543554ce7 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Mon, 1 Nov 2021 22:23:15 -0400 Subject: [PATCH] Finished implementation --- .../admin-lte/configure/ipfs-tab/index.js | 77 +++++++++---------- .../admin-lte/configure/lib/wallet-service.js | 7 +- src/components/admin-lte/send-receive/send.js | 6 +- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/src/components/admin-lte/configure/ipfs-tab/index.js b/src/components/admin-lte/configure/ipfs-tab/index.js index 42c5fbe..4d3f8f7 100644 --- a/src/components/admin-lte/configure/ipfs-tab/index.js +++ b/src/components/admin-lte/configure/ipfs-tab/index.js @@ -183,6 +183,7 @@ class IPFS extends React.Component { // Adds a line to the Status terminal onStatusLog (str) { + console.log('onStatusLog', str) try { // Update the Status terminal _this.setState({ @@ -232,8 +233,9 @@ class IPFS extends React.Component { // Handle decrypted, private messages and send them to the right terminal. privLogChat (str, from) { try { - // console.log(`privLogChat str: ${str}`) + console.log(`privLogChat str: ${str}`) // console.log(`privLogChat from: ${from}`) + _this.handleRpcDataQueue(str) const { chatOutputs } = _this.state @@ -296,7 +298,7 @@ class IPFS extends React.Component { } // pollForServices callback - onAppStatus (msg) { + async onAppStatus (msg) { try { let output if (!msg) { @@ -310,7 +312,7 @@ class IPFS extends React.Component { // Updates the bchWallet instance so it works // under the ipfs services - _this.reInitialize() + await _this.reInitialize() } catch (error) { console.warn(error) // Don't throw an error as this is a top-level handler. @@ -321,60 +323,51 @@ class IPFS extends React.Component { // under the ipfs services async reInitialize () { try { + _this.onStatusLog('waiting for re-initialize...') + const currentWallet = _this.props.walletInfo - const { JWT, selectedServer, mnemonic } = currentWallet - - const apiToken = JWT - const restURL = selectedServer - - const bchjsOptions = {} - - if (apiToken || restURL) { - if (apiToken) { - bchjsOptions.apiToken = apiToken - } - if (restURL) { - bchjsOptions.restURL = restURL - } - } - bchjsOptions.interface = 'json-rpc' + const { mnemonic } = currentWallet const walletService = new _this.WalletService({ ipfsControl: this.ipfsControl }) - bchjsOptions.jsonRpcWalletService = walletService + const advancedConfig = { + interface: 'json-rpc', + jsonRpcWalletService: walletService + } - const bchWalletLib = new _this.BchWallet(mnemonic, bchjsOptions) - // Update bchjs instances of minimal-slp-wallet libraries + _this.bchWalletLib = new _this.BchWallet(mnemonic, advancedConfig) - bchWalletLib.tokens.sendBch.bchjs = new bchWalletLib.BCHJS(bchjsOptions) - bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions) + await _this.bchWalletLib.walletInfoPromise // Wait for wallet to be created. - await bchWalletLib.walletInfoPromise // Wait for wallet to be created. + // If UTXOs fail to update, try one more time. + if (!_this.bchWalletLib.utxos.utxoStore) { + await _this.bchWalletLib.getUtxos() - const walletInfo = bchWalletLib.walletInfo - - Object.assign(currentWallet, walletInfo) - - const myBalance = await bchWalletLib.getBalance() - - const bchjs = bchWalletLib.bchjs - let currentRate - - if (bchjs.restURL.includes('abc.fullstack')) { - currentRate = (await bchjs.Price.getBchaUsd()) * 100 - } else { - // BCHN price. - currentRate = (await bchjs.Price.getUsd()) * 100 + // Throw an error if UTXOs are still not updated. + if (!_this.bchWalletLib.utxos.utxoStore) { + throw new Error('UTXOs failed to update. Try again.') + } } // Update redux state - _this.props.setWalletInfo(currentWallet) - _this.props.updateBalance({ myBalance, currentRate }) - _this.props.setBchWallet(bchWalletLib) + _this.props.setBchWallet(_this.bchWalletLib) + _this.onStatusLog('re-initialize success!') } catch (error) { _this.onStatusLog('Error on re-initialize') _this.onStatusLog(error.message) + // Don't throw an error as this is a top-level handler. + } + } + + // Fill the queue with the incoming data (private messages) from the petitions + // So it will be sweeped by the lib/wallet-service/waitForRPCResponse() function + handleRpcDataQueue (data) { + try { + _this.bchWalletLib.ar.jsonRpcWalletService.rpcHandler(data) + } catch (error) { + console.warn('Error in handleRpcDataQueue', error) + // Don't throw an error as this is a top-level handler. } } diff --git a/src/components/admin-lte/configure/lib/wallet-service.js b/src/components/admin-lte/configure/lib/wallet-service.js index 427ddaf..a36602e 100644 --- a/src/components/admin-lte/configure/lib/wallet-service.js +++ b/src/components/admin-lte/configure/lib/wallet-service.js @@ -26,7 +26,8 @@ class WalletService { // This handler is triggered when RPC data comes in over IPFS. // Handle RPC input, and match the input to the RPC queue. - // NOTE: This function is currently not called or controlled + // NOTE: This function is called when a private message + // is sent to this node rpcHandler (data) { try { @@ -154,6 +155,8 @@ class WalletService { // Generate a JSON RPC command. const cmd = this.jsonrpc.request(rpcId, 'bch', rpcData) const cmdStr = JSON.stringify(cmd) + console.log('cmdStr', cmdStr) + const thisNode = this.ipfsControl.ipfsCoord.thisNode await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage( @@ -203,7 +206,7 @@ class WalletService { // Wait between loops. // await this.sleep(1000) - await this.ipfsControl.wallet.bchjs.Util.sleep(13000) + await this.ipfsControl.wallet.bchjs.Util.sleep(5000) cnt++ diff --git a/src/components/admin-lte/send-receive/send.js b/src/components/admin-lte/send-receive/send.js index bd0918e..5d74a3c 100644 --- a/src/components/admin-lte/send-receive/send.js +++ b/src/components/admin-lte/send-receive/send.js @@ -145,6 +145,8 @@ class Send extends React.Component { } componentDidMount () { + const { bchWallet } = _this.props + console.log('bchWallet', bchWallet) _this.defineExplorer() } @@ -354,10 +356,10 @@ class Send extends React.Component { // Send the BCH. const result = await bchWalletLib.send(receivers) - // console.log('result',result) + console.log('result', result) _this.setState({ - txId: result + txId: result.txid || result }) // update balance