mirror of
https://github.com/fullstack-cash/fullstack-gatsby-theme-bch-wallet.git
synced 2026-09-21 16:52:06 -07:00
wait for rpc response
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
This library interacts with the ipfs-bch-wallet-service via the JSON RPC
|
||||
over IPFS.
|
||||
*/
|
||||
|
||||
const { v4: uid } = require('uuid')
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
|
||||
@@ -22,6 +23,29 @@ class WalletService {
|
||||
this.rpcDataQueue = []
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
rpcHandler (data) {
|
||||
try {
|
||||
// Convert string input into an object.
|
||||
const jsonData = JSON.parse(data)
|
||||
|
||||
// console.log(
|
||||
// 'rest-api.js/rpcHandler() data: ',
|
||||
// JSON.stringify(jsonData, null, 2),
|
||||
// )
|
||||
console.log(`JSON RPC response for ID ${jsonData.id} received.`)
|
||||
|
||||
this.rpcDataQueue.push(jsonData)
|
||||
} catch (err) {
|
||||
console.error('Error in rest-api.js/rpcHandler(): ', err)
|
||||
// Do not throw error. This is a top-level function.
|
||||
}
|
||||
}
|
||||
|
||||
checkServiceId () {
|
||||
try {
|
||||
// this.conf = new Conf()
|
||||
@@ -62,24 +86,15 @@ class WalletService {
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
/// ///
|
||||
/* const result = await this.axios.post(LOCAL_REST_API, {
|
||||
sendTo: serviceId,
|
||||
rpcData: {
|
||||
endpoint: 'balance',
|
||||
addresses: addrs
|
||||
}
|
||||
}) */
|
||||
// console.log(`result.data: ${JSON.stringify(result.data, null, 2)}`)
|
||||
|
||||
// If there is a timeout or other network failure.
|
||||
const data = await this.waitForRPCResponse(rpcId)
|
||||
|
||||
return result
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in getBalance()')
|
||||
throw err
|
||||
@@ -107,13 +122,14 @@ class WalletService {
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
console.log('cmdStr', cmdStr)
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
|
||||
return result
|
||||
// Wait for data to come back from the wallet service.
|
||||
const data = await this.waitForRPCResponse(rpcId)
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in getUtxos()', err)
|
||||
throw err
|
||||
@@ -140,18 +156,67 @@ class WalletService {
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
const data = await this.waitForRPCResponse(rpcId)
|
||||
|
||||
return result
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in sendTx()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Returns a promise that resolves to data when the RPC response is recieved.
|
||||
async waitForRPCResponse (rpcId) {
|
||||
try {
|
||||
// Initialize variables for tracking the return data.
|
||||
let dataFound = false
|
||||
let cnt = 0
|
||||
let data = {
|
||||
success: false,
|
||||
message: 'request timed out',
|
||||
data: ''
|
||||
}
|
||||
|
||||
// Loop that waits for a response from the service provider.
|
||||
do {
|
||||
for (let i = 0; i < this.rpcDataQueue.length; i++) {
|
||||
const rawData = this.rpcDataQueue[i]
|
||||
// console.log(`rawData: ${JSON.stringify(rawData, null, 2)}`)
|
||||
|
||||
if (rawData.id === rpcId) {
|
||||
dataFound = true
|
||||
// console.log('data was found in the queue')
|
||||
|
||||
data = rawData.result.value
|
||||
|
||||
// Remove the data from the queue
|
||||
this.rpcDataQueue.splice(i, 1)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Wait between loops.
|
||||
// await this.sleep(1000)
|
||||
await this.ipfsControl.wallet.bchjs.Util.sleep(13000)
|
||||
|
||||
cnt++
|
||||
|
||||
// Exit if data was returned, or the window for a response expires.
|
||||
} while (!dataFound && cnt < 10)
|
||||
// console.log(`dataFound: ${dataFound}, cnt: ${cnt}`)
|
||||
console.log('waitForRPCResponse', data)
|
||||
return data
|
||||
} catch (err) {
|
||||
console.error('Error in waitForRPCResponse()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WalletService
|
||||
|
||||
Reference in New Issue
Block a user