From fc11e701e4f56ad35fae156cd1a375537510d711 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 2 Dec 2021 13:51:07 -0800 Subject: [PATCH] fix(broadcast): Handling full node errors --- shell-scripts/ipfs-bch-wallet-service.sh | 2 +- src/controllers/json-rpc/bch/index.js | 84 ++++++++++++---------- src/controllers/rest-api/bch/controller.js | 1 + 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/shell-scripts/ipfs-bch-wallet-service.sh b/shell-scripts/ipfs-bch-wallet-service.sh index 75afe63..828256b 100755 --- a/shell-scripts/ipfs-bch-wallet-service.sh +++ b/shell-scripts/ipfs-bch-wallet-service.sh @@ -3,7 +3,7 @@ # This script is an example for running a generic ipfs-service-provider instance. # Ports -export PORT=5001 # REST API port +export PORT=5002 # REST API port export IPFS_TCP_PORT=5268 export IPFS_WS_PORT=5269 diff --git a/src/controllers/json-rpc/bch/index.js b/src/controllers/json-rpc/bch/index.js index ff58215..e0ec3e4 100644 --- a/src/controllers/json-rpc/bch/index.js +++ b/src/controllers/json-rpc/bch/index.js @@ -331,6 +331,7 @@ class BCHRPC { // console.log('createUser rpcData: ', rpcData) const addr = rpcData.payload.params.address + console.log('addr: ', addr) const data = await this.bchjs.Utxo.get(addr) // console.log(`data: ${JSON.stringify(data, null, 2)}`) @@ -404,13 +405,24 @@ class BCHRPC { return retObj } catch (err) { console.error('Error in JSON RPC BCH broadcast()') + console.log('error: ', err) // throw err + // bch-api will sometimes return an error message, not an error object. + let message = '' + if (!err.message && err.error) { + // Full node error + message = err.error + } else { + // Normal error + message = err.message + } + // Return an error response return { success: false, status: 422, - message: err.message, + message, endpoint: 'broadcast' } } @@ -538,41 +550,41 @@ class BCHRPC { } /** - * @api {JSON} /bch PubKey - * @apiPermission public - * @apiName PubKey - * @apiGroup JSON BCH - * @apiDescription Get the public key from an address. - * Given an address the endpoint will return an object with the - * following properties - * - * - jsonrpc: "" - jsonrpc version - * - id: "" - jsonrpc id - * - result: {} - Result of the petition with the RPC information - * - success: - Request status - * - publickey: - Address public key - * - * @apiExample Example usage: - * {"jsonrpc":"2.0","id":"555","method":"bch","params":{ "endpoint": "pubkey", "address": "bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk"}} - * - * @apiSuccessExample {json} Success-Response: - * { - * "jsonrpc":"2.0", - * "id":"555", - * "result":{ - * "method":"bch", - * "reciever":"QmU86vLVbUY1UhziKB6rak7GPKRA2QHWvzNm2AjEvXNsT6", - * "value":{ - * "success": true, - * "status": 200, - * "endpoint": "pubkey", - * "pubkey": { - * "success": true, - * "publicKey": "033f267fec0f7eb2b27f8c2e3052b3d03b09d36b47de4082ffb638ffb334ef0eee" - * } - * - * } - */ + * @api {JSON} /bch PubKey + * @apiPermission public + * @apiName PubKey + * @apiGroup JSON BCH + * @apiDescription Get the public key from an address. + * Given an address the endpoint will return an object with the + * following properties + * + * - jsonrpc: "" - jsonrpc version + * - id: "" - jsonrpc id + * - result: {} - Result of the petition with the RPC information + * - success: - Request status + * - publickey: - Address public key + * + * @apiExample Example usage: + * {"jsonrpc":"2.0","id":"555","method":"bch","params":{ "endpoint": "pubkey", "address": "bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk"}} + * + * @apiSuccessExample {json} Success-Response: + * { + * "jsonrpc":"2.0", + * "id":"555", + * "result":{ + * "method":"bch", + * "reciever":"QmU86vLVbUY1UhziKB6rak7GPKRA2QHWvzNm2AjEvXNsT6", + * "value":{ + * "success": true, + * "status": 200, + * "endpoint": "pubkey", + * "pubkey": { + * "success": true, + * "publicKey": "033f267fec0f7eb2b27f8c2e3052b3d03b09d36b47de4082ffb638ffb334ef0eee" + * } + * + * } + */ async pubKey (rpcData) { try { diff --git a/src/controllers/rest-api/bch/controller.js b/src/controllers/rest-api/bch/controller.js index ca8b304..6d680bc 100644 --- a/src/controllers/rest-api/bch/controller.js +++ b/src/controllers/rest-api/bch/controller.js @@ -413,6 +413,7 @@ class BCHRESTController { async pubKey (ctx) { try { const address = ctx.params.address + console.log(`pubKey address: ${address}`) const pubkey = await _this.bchjs.encryption.getPubKey(address) // console.log(`pubkey: ${JSON.stringify(pubkey, null, 2)}`)