From 839657ccedc2a19993a5fe93df5952051464bd2c Mon Sep 17 00:00:00 2001 From: Gabriel Cardona Date: Thu, 23 Aug 2018 21:43:02 -0700 Subject: [PATCH] Refactor to use async/await. --- index.js | 4 +- lib/Address.js | 30 +++--- lib/Block.js | 9 +- lib/Blockchain.js | 171 ++++++++++++++++++--------------- lib/Control.js | 18 ++-- lib/Generating.js | 9 +- lib/Mining.js | 38 ++++---- lib/Network.js | 81 +++++++++------- lib/Price.js | 9 +- lib/RawTransactions.js | 36 +++---- lib/Transaction.js | 9 +- lib/Util.js | 9 +- package.json | 2 +- src/Address.js | 36 +++---- src/Block.js | 11 +-- src/Blockchain.js | 209 +++++++++++++++++++---------------------- src/Control.js | 22 ++--- src/Generating.js | 11 +-- src/Mining.js | 46 +++++---- src/Network.js | 99 +++++++++---------- src/Price.js | 11 +-- src/RawTransactions.js | 44 ++++----- src/Transaction.js | 11 +-- src/Util.js | 11 +-- 24 files changed, 471 insertions(+), 465 deletions(-) diff --git a/index.js b/index.js index 9f0b3927..47705348 100755 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ let clone = require('git-clone'); let cmd = require('node-cmd'); program - .version('1.5.12'); + .version('1.6.0'); program .command('new ') @@ -34,7 +34,7 @@ program let config; let environment = fetchOption('environment=development', config, options); - let restURL = fetchOption('restURL=https://rest.bitcoin.com/v1/', config, options); + let restURL = fetchOption('restURL=https://trest.bitcoin.com/v1/', config, options); if(options && options.scaffold) { let scaffold = options.scaffold.toLowerCase(); diff --git a/lib/Address.js b/lib/Address.js index ab308e8e..d1b2c2ac 100644 --- a/lib/Address.js +++ b/lib/Address.js @@ -145,39 +145,45 @@ var Address = function () { } }, { key: 'details', - value: function details(address) { + value: async function details(address) { if (typeof address !== 'string') { address = JSON.stringify(address); } - return _axios2.default.get(this.restURL + 'address/details/' + address).then(function (response) { + + try { + var response = await _axios2.default.get(this.restURL + 'address/details/' + address); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'utxo', - value: function utxo(address) { + value: async function utxo(address) { if (typeof address !== 'string') { address = JSON.stringify(address); } - return _axios2.default.get(this.restURL + 'address/utxo/' + address).then(function (response) { + + try { + var response = await _axios2.default.get(this.restURL + 'address/utxo/' + address); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'unconfirmed', - value: function unconfirmed(address) { + value: async function unconfirmed(address) { if (typeof address !== 'string') { address = JSON.stringify(address); } - return _axios2.default.get(this.restURL + 'address/unconfirmed/' + address).then(function (response) { + + try { + var response = await _axios2.default.get(this.restURL + 'address/unconfirmed/' + address); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Block.js b/lib/Block.js index 4f09336e..135852b3 100644 --- a/lib/Block.js +++ b/lib/Block.js @@ -23,12 +23,13 @@ var Block = function () { _createClass(Block, [{ key: 'details', - value: function details(id) { - return _axios2.default.get(this.restURL + 'block/details/' + id).then(function (response) { + value: async function details(id) { + try { + var response = await _axios2.default.get(this.restURL + 'block/details/' + id); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Blockchain.js b/lib/Blockchain.js index 676d8bed..de04fb17 100644 --- a/lib/Blockchain.js +++ b/lib/Blockchain.js @@ -23,20 +23,21 @@ var Blockchain = function () { _createClass(Blockchain, [{ key: 'getBestBlockHash', - value: function getBestBlockHash() { + value: async function getBestBlockHash() { // Returns the hash of the best (tip) block in the longest blockchain. // // Result: // "hex" (string) the block hash hex encoded - return _axios2.default.get(this.restURL + 'blockchain/getBestBlockHash').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBestBlockHash'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getBlock', - value: function getBlock(blockhash) { + value: async function getBlock(blockhash) { var verbose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; // If verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'. @@ -72,38 +73,41 @@ var Blockchain = function () { // Result (for verbose=false): // "data" (string) A string that is serialized, hex-encoded data for block 'hash'. - return _axios2.default.get(this.restURL + 'blockchain/getBlock/' + blockhash + '?verbose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBlock/' + blockhash + '?verbose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getBlockchainInfo', - value: function getBlockchainInfo() { + value: async function getBlockchainInfo() { // Returns an object containing various state info regarding blockchain processing. - return _axios2.default.get(this.restURL + 'blockchain/getBlockchainInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBlockchainInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getBlockCount', - value: function getBlockCount() { + value: async function getBlockCount() { // Returns the number of blocks in the longest blockchain. // // Result: // n (numeric) The current block count - return _axios2.default.get(this.restURL + 'blockchain/getBlockCount').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBlockCount'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getBlockHash', - value: function getBlockHash() { + value: async function getBlockHash() { var height = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; // Returns hash of block in best-block-chain at height provided. @@ -116,15 +120,16 @@ var Blockchain = function () { if (typeof height !== 'string') { height = JSON.stringify(height); } - return _axios2.default.get(this.restURL + 'blockchain/getBlockHash/' + height).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBlockHash/' + height); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getBlockHeader', - value: function getBlockHeader(hash) { + value: async function getBlockHeader(hash) { var verbose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; @@ -158,15 +163,16 @@ var Blockchain = function () { if (typeof hash !== 'string') { hash = JSON.stringify(hash); } - return _axios2.default.get(this.restURL + 'blockchain/getBlockHeader/' + hash + '?verbose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getBlockHeader/' + hash + '?verbose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getChainTips', - value: function getChainTips() { + value: async function getChainTips() { // Return information about all known tips in the block tree, including the main chain as well as orphaned branches. // // Result: @@ -191,30 +197,32 @@ var Blockchain = function () { // 4. "valid-fork" This branch is not part of the active chain, but is fully validated // 5. "active" This is the tip of the active main chain, which is certainly valid - return _axios2.default.get(this.restURL + 'blockchain/getChainTips').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getChainTips'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getDifficulty', - value: function getDifficulty() { + value: async function getDifficulty() { // Returns the proof-of-work difficulty as a multiple of the minimum difficulty. // // Result: // n.nnn (numeric) the proof-of-work difficulty as a multiple of the minimum difficulty. - return _axios2.default.get(this.restURL + 'blockchain/getDifficulty').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getDifficulty'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getMempoolAncestors', - value: function getMempoolAncestors(txid) { + value: async function getMempoolAncestors(txid) { var verbose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // If txid is in the mempool, returns all in-mempool ancestors. @@ -253,15 +261,16 @@ var Blockchain = function () { if (typeof txid !== 'string') { txid = JSON.stringify(txid); } - return _axios2.default.get(this.restURL + 'blockchain/getMempoolAncestors/' + txid + '?verbose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getMempoolAncestors/' + txid + '?verbose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getMempoolDescendants', - value: function getMempoolDescendants(txid) { + value: async function getMempoolDescendants(txid) { var verbose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // If txid is in the mempool, returns all in-mempool descendants. @@ -301,15 +310,16 @@ var Blockchain = function () { txid = JSON.stringify(txid); } - return _axios2.default.get(this.restURL + 'blockchain/getMempoolDescendants/' + txid + '?verbose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getMempoolDescendants/' + txid + '?verbose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getMempoolEntry', - value: function getMempoolEntry(txid) { + value: async function getMempoolEntry(txid) { // Returns mempool data for given transaction // // Arguments: @@ -338,15 +348,16 @@ var Blockchain = function () { txid = JSON.stringify(txid); } - return _axios2.default.get(this.restURL + 'blockchain/getMempoolEntry/' + txid).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getMempoolEntry/' + txid); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getMempoolInfo', - value: function getMempoolInfo() { + value: async function getMempoolInfo() { // Returns details on the active state of the TX memory pool. // // Result: @@ -358,15 +369,16 @@ var Blockchain = function () { // "mempoolminfee": xxxxx (numeric) Minimum fee for tx to be accepted // } - return _axios2.default.get(this.restURL + 'blockchain/getMempoolInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getMempoolInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getRawMempool', - value: function getRawMempool() { + value: async function getRawMempool() { var verbose = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; // Returns all transaction ids in memory pool as a json array of string transaction ids. @@ -401,15 +413,16 @@ var Blockchain = function () { // ... ] // }, ... // } - return _axios2.default.get(this.restURL + 'blockchain/getRawMempool?vebose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getRawMempool?vebose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getTxOut', - value: function getTxOut(txid, n) { + value: async function getTxOut(txid, n) { var include_mempool = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; // Returns details about an unspent transaction output. @@ -438,15 +451,16 @@ var Blockchain = function () { // } // - return _axios2.default.get(this.restURL + 'blockchain/getTxOut/' + txid + '/n?include_mempool=' + include_mempool).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/getTxOut/' + txid + '/n?include_mempool=' + include_mempool); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getTxOutProof', - value: function getTxOutProof(txids, blockhash) { + value: async function getTxOutProof(txids, blockhash) { var path = this.restURL + 'blockchain/getTxOutProof/' + txids; if (blockhash) { path = path + '?blockhash=' + blockhash; @@ -468,15 +482,16 @@ var Blockchain = function () { // // Result: // "data" (string) A string that is a serialized, hex-encoded data for the proof. - return _axios2.default.get(path).then(function (response) { + try { + var response = await _axios2.default.get(path); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'preciousBlock', - value: function preciousBlock(blockhash) { + value: async function preciousBlock(blockhash) { // Treats a block as if it were received before others with the same work. // // A later preciousblock call can override the effect of an earlier one. @@ -486,30 +501,32 @@ var Blockchain = function () { // Arguments: // 1. "blockhash" (string, required) the hash of the block to mark as precious - return _axios2.default.get(this.restURL + 'blockchain/preciousBlock/' + blockhash).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/preciousBlock/' + blockhash); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'pruneBlockchain', - value: function pruneBlockchain(height) { + value: async function pruneBlockchain(height) { // Arguments: // 1. "height" (numeric, required) The block height to prune up to. May be set to a discrete height, or a unix timestamp // to prune blocks whose block time is at least 2 hours older than the provided timestamp. // // Result: // n (numeric) Height of the last block pruned. - return _axios2.default.post(this.restURL + 'blockchain/pruneBlockchain/' + height).then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'blockchain/pruneBlockchain/' + height); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'verifyChain', - value: function verifyChain() { + value: async function verifyChain() { var checklevel = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3; var nblocks = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 6; @@ -521,15 +538,16 @@ var Blockchain = function () { // // Result: // true|false (boolean) Verified or not - return _axios2.default.get(this.restURL + 'blockchain/verifyChain?checklevel=' + checklevel + '&nblocks=' + nblocks).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/verifyChain?checklevel=' + checklevel + '&nblocks=' + nblocks); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'verifyTxOutProof', - value: function verifyTxOutProof(proof) { + value: async function verifyTxOutProof(proof) { // Verifies that a proof points to a transaction in a block, returning the transaction it commits to // and throwing an RPC error if the block is not in our best chain // @@ -539,11 +557,12 @@ var Blockchain = function () { // Result: // ["txid"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid - return _axios2.default.get(this.restURL + 'blockchain/verifyTxOutProof/proof').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'blockchain/verifyTxOutProof/proof'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Control.js b/lib/Control.js index 0f3cdbf8..52fee216 100644 --- a/lib/Control.js +++ b/lib/Control.js @@ -23,7 +23,7 @@ var Control = function () { _createClass(Control, [{ key: 'getInfo', - value: function getInfo() { + value: async function getInfo() { // DEPRECATED. Returns an object containing various state info. // // Result: @@ -46,15 +46,16 @@ var Control = function () { // "errors": "..." (string) any error messages // } - return _axios2.default.get(this.restURL + 'control/getInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'control/getInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getMemoryInfo', - value: function getMemoryInfo() { + value: async function getMemoryInfo() { // Returns an object containing information about memory usage. // @@ -71,11 +72,12 @@ var Control = function () { // } // - return _axios2.default.get(this.restURL + 'control/getMemoryInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'control/getMemoryInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // stop() { diff --git a/lib/Generating.js b/lib/Generating.js index 02e4908f..ed354cf1 100644 --- a/lib/Generating.js +++ b/lib/Generating.js @@ -23,7 +23,7 @@ var Generating = function () { _createClass(Generating, [{ key: 'generateToAddress', - value: function generateToAddress(blocks, address) { + value: async function generateToAddress(blocks, address) { var maxtries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1000000; @@ -37,11 +37,12 @@ var Generating = function () { // Result: // [ blockhashes ] (array) hashes of blocks generated // - return _axios2.default.post(this.restURL + 'generating/generateToAddress/' + blocks + '/' + address + '?maxtries=' + maxtries).then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'generating/generateToAddress/' + blocks + '/' + address + '?maxtries=' + maxtries); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Mining.js b/lib/Mining.js index 8f2d5d90..52816561 100644 --- a/lib/Mining.js +++ b/lib/Mining.js @@ -23,7 +23,7 @@ var Mining = function () { _createClass(Mining, [{ key: 'getBlockTemplate', - value: function getBlockTemplate(template_request) { + value: async function getBlockTemplate(template_request) { // If the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'. // It returns data needed to construct a block to work on. @@ -47,15 +47,16 @@ var Mining = function () { // ] // } - return _axios2.default.get(this.restURL + 'mining/getBlockTemplate/' + template_request).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'mining/getBlockTemplate/' + template_request); return response.data; - }).catch(function (error) { - return JSON.stringify(error.response.data.message); - }); + } catch (error) { + return JSON.stringify(error.response.data.error.message); + } } }, { key: 'getMiningInfo', - value: function getMiningInfo() { + value: async function getMiningInfo() { // Returns a json object containing mining-related information. // Result: // { @@ -69,15 +70,16 @@ var Mining = function () { // "chain": "xxxx", (string) current network name as defined in BIP70 (main, test, regtest) // } - return _axios2.default.get(this.restURL + 'mining/getMiningInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'mining/getMiningInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getNetworkHashps', - value: function getNetworkHashps() { + value: async function getNetworkHashps() { var nblocks = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 120; var height = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; @@ -91,15 +93,16 @@ var Mining = function () { // // Result: // x (numeric) Hashes per second estimated - return _axios2.default.get(this.restURL + 'mining/getNetworkHashps?nblocks=' + nblocks + '&height=' + height).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'mining/getNetworkHashps?nblocks=' + nblocks + '&height=' + height); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'submitBlock', - value: function submitBlock(hex, parameters) { + value: async function submitBlock(hex, parameters) { // Attempts to submit new block to network. // The 'jsonparametersobject' parameter is currently ignored. // See https://en.bitcoin.it/wiki/BIP_0022 for full specification. @@ -115,11 +118,12 @@ var Mining = function () { if (parameters) { path = path + '?parameters=' + parameters; } - return _axios2.default.post(path).then(function (response) { + try { + var response = await _axios2.default.post(path); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Network.js b/lib/Network.js index b831261f..a91568b6 100644 --- a/lib/Network.js +++ b/lib/Network.js @@ -23,7 +23,7 @@ var Network = function () { _createClass(Network, [{ key: 'addNode', - value: function addNode(node, command) { + value: async function addNode(node, command) { // Attempts add or remove a node from the addnode list. // Or try a connection to a node once. // @@ -32,15 +32,16 @@ var Network = function () { // 2. "command" (string, required) 'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once // - return _axios2.default.post(this.restURL + 'network/addNode/' + node + '/' + command).then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'network/addNode/' + node + '/' + command); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'clearBanned', - value: function clearBanned() { + value: async function clearBanned() { //The clearbanned RPC clears list of banned nodes. // Parameters: none @@ -48,15 +49,16 @@ var Network = function () { // Result—null on success // JSON null when the list was cleared - return _axios2.default.post(this.restURL + 'clearBanned').then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'clearBanned'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'disconnectNode', - value: function disconnectNode(configuration) { + value: async function disconnectNode(configuration) { // Immediately disconnects from the specified peer node. // // Strictly one out of 'configuration.address' and 'configuration.nodeid' can be provided to identify the node. @@ -68,15 +70,16 @@ var Network = function () { // Properties // 1. "address" (string, optional) The IP address/port of the node // 2. "nodeid" (number, optional) The node ID (see getpeerinfo for node IDs) - return _axios2.default.post(this.restURL + 'disconnectNode/' + configuration).then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'disconnectNode/' + configuration); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getAddedNodeInfo', - value: function getAddedNodeInfo(node) { + value: async function getAddedNodeInfo(node) { // Returns information about the given added node, or all added nodes // (note that onetry addnodes are not listed here) // @@ -102,29 +105,31 @@ var Network = function () { path = path + '?node=' + node; } - return _axios2.default.get(path).then(function (response) { + try { + var response = await _axios2.default.get(path); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getConnectionCount', - value: function getConnectionCount() { + value: async function getConnectionCount() { // Returns the number of connections to other nodes. // // Result: // n (numeric) The connection count - return _axios2.default.get(this.restURL + 'network/getConnectionCount').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'network/getConnectionCount'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getNetTotals', - value: function getNetTotals() { + value: async function getNetTotals() { // Returns information about network traffic, including bytes in, bytes out, and current time. // // Result: @@ -143,15 +148,16 @@ var Network = function () { // } // } - return _axios2.default.get(this.restURL + 'network/getNetTotals').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'network/getNetTotals'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getNetworkInfo', - value: function getNetworkInfo() { + value: async function getNetworkInfo() { // Returns an object containing various state info regarding P2P networking. // // Result: @@ -187,15 +193,16 @@ var Network = function () { // "warnings": "..." (string) any network warnings // } - return _axios2.default.get(this.restURL + 'network/getNetworkInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'network/getNetworkInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getPeerInfo', - value: function getPeerInfo() { + value: async function getPeerInfo() { // Returns data about each connected network node as a json array of objects. // // Result: @@ -240,11 +247,12 @@ var Network = function () { // ,... // ] - return _axios2.default.get(this.restURL + 'network/getPeerInfo').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'network/getPeerInfo'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // listBanned() { @@ -260,16 +268,17 @@ var Network = function () { }, { key: 'ping', - value: function ping() { + value: async function ping() { // Requests that a ping be sent to all other nodes, to measure ping time. // Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds. // Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping. - return _axios2.default.get(this.restURL + 'network/ping').then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'network/ping'); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // setBan(subnet, command, bantime, absolute) { diff --git a/lib/Price.js b/lib/Price.js index 730098cb..02aa65dc 100644 --- a/lib/Price.js +++ b/lib/Price.js @@ -21,18 +21,19 @@ var Price = function () { _createClass(Price, [{ key: 'current', - value: function current() { + value: async function current() { var currency = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'all'; - return _axios2.default.get('https://www.blocktrail.com/BCC/json/blockchain/price').then(function (response) { + try { + var response = await _axios2.default.get('https://www.blocktrail.com/BCC/json/blockchain/price'); if (currency === 'all') { return response.data; } else { return response.data[currency.toUpperCase()]; } - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/RawTransactions.js b/lib/RawTransactions.js index e3ae1368..7ac1a277 100644 --- a/lib/RawTransactions.js +++ b/lib/RawTransactions.js @@ -23,7 +23,7 @@ var RawTransactions = function () { _createClass(RawTransactions, [{ key: 'decodeRawTransaction', - value: function decodeRawTransaction(hex) { + value: async function decodeRawTransaction(hex) { // decodes a serialized transaction hex string into a JSON object describing the transaction. // Parameter #1—serialized transaction in hex @@ -33,15 +33,16 @@ var RawTransactions = function () { hex = JSON.stringify(hex); } - return _axios2.default.get(this.restURL + 'rawtransactions/decodeRawTransaction/' + hex).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'rawtransactions/decodeRawTransaction/' + hex); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'decodeScript', - value: function decodeScript(script) { + value: async function decodeScript(script) { // decodes a hex-encoded P2SH redeem script. // Parameter #1—a hex-encoded redeem script @@ -51,15 +52,16 @@ var RawTransactions = function () { script = JSON.stringify(script); } - return _axios2.default.get(this.restURL + 'rawtransactions/decodeScript/' + script).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'rawtransactions/decodeScript/' + script); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'getRawTransaction', - value: function getRawTransaction(txid) { + value: async function getRawTransaction(txid) { var verbose = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // NOTE: By default this function only works for mempool transactions. If the -txindex option is @@ -82,15 +84,16 @@ var RawTransactions = function () { txid = JSON.stringify(txid); } - return _axios2.default.get(this.restURL + 'rawtransactions/getRawTransaction/' + txid + '?verbose=' + verbose).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'rawtransactions/getRawTransaction/' + txid + '?verbose=' + verbose); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }, { key: 'sendRawTransaction', - value: function sendRawTransaction(hex) { + value: async function sendRawTransaction(hex) { var allowhighfees = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; // Submits raw transaction (serialized, hex-encoded) to local node and network. @@ -108,11 +111,12 @@ var RawTransactions = function () { hex = JSON.stringify(hex); } - return _axios2.default.post(this.restURL + 'rawtransactions/sendRawTransaction/' + hex + '?allowhighfees=' + allowhighfees).then(function (response) { + try { + var response = await _axios2.default.post(this.restURL + 'rawtransactions/sendRawTransaction/' + hex + '?allowhighfees=' + allowhighfees); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Transaction.js b/lib/Transaction.js index 6075222a..1d7ef3a0 100644 --- a/lib/Transaction.js +++ b/lib/Transaction.js @@ -49,16 +49,17 @@ var Transaction = function () { } }, { key: 'details', - value: function details(txid) { + value: async function details(txid) { if (typeof txid !== 'string') { txid = JSON.stringify(txid); } - return _axios2.default.get(this.restURL + 'transaction/details/' + txid).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'transaction/details/' + txid); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/lib/Util.js b/lib/Util.js index 7cecc053..c6b31988 100644 --- a/lib/Util.js +++ b/lib/Util.js @@ -23,7 +23,7 @@ var Util = function () { _createClass(Util, [{ key: 'validateAddress', - value: function validateAddress(address) { + value: async function validateAddress(address) { // Return information about the given bitcoin address. // // Arguments: @@ -44,11 +44,12 @@ var Util = function () { // "hdkeypath" : "keypath" (string, optional) The HD keypath if the key is HD and available // "hdmasterkeyid" : "" (string, optional) The Hash160 of the HD master pubkey // } - return _axios2.default.get(this.restURL + 'util/validateAddress/' + address).then(function (response) { + try { + var response = await _axios2.default.get(this.restURL + 'util/validateAddress/' + address); return response.data; - }).catch(function (error) { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }]); diff --git a/package.json b/package.json index 59a2f049..0996c010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bitbox-cli", - "version": "1.5.12", + "version": "1.6.0", "description": "BITBOX javascript sdk for Bitcoin Cash", "author": "Gabriel Cardona @ Bitcoin.com", "main": "index.js", diff --git a/src/Address.js b/src/Address.js index 299f0b7d..f8a418aa 100644 --- a/src/Address.js +++ b/src/Address.js @@ -90,43 +90,43 @@ class Address { return bchaddr.toCashAddress(Bitcoin.address.fromOutputScript(scriptPubKey)); } - details(address) { + async details(address) { if(typeof address !== 'string') { address = JSON.stringify(address); } - return axios.get(`${this.restURL}address/details/${address}`) - .then((response) => { + + try { + let response = await axios.get(`${this.restURL}address/details/${address}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - utxo(address) { + async utxo(address) { if(typeof address !== 'string') { address = JSON.stringify(address); } - return axios.get(`${this.restURL}address/utxo/${address}`) - .then((response) => { + + try { + let response = await axios.get(`${this.restURL}address/utxo/${address}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - unconfirmed(address) { + async unconfirmed(address) { if(typeof address !== 'string') { address = JSON.stringify(address); } - return axios.get(`${this.restURL}address/unconfirmed/${address}`) - .then((response) => { + + try { + let response = await axios.get(`${this.restURL}address/unconfirmed/${address}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Block.js b/src/Block.js index c637dc87..bcce85ff 100644 --- a/src/Block.js +++ b/src/Block.js @@ -4,14 +4,13 @@ class Block { this.restURL = restURL; } - details(id) { - return axios.get(`${this.restURL}block/details/${id}`) - .then((response) => { + async details(id) { + try { + let response = await axios.get(`${this.restURL}block/details/${id}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Blockchain.js b/src/Blockchain.js index 6665835a..e63f1530 100644 --- a/src/Blockchain.js +++ b/src/Blockchain.js @@ -4,21 +4,20 @@ class Blockchain { this.restURL = restURL; } - getBestBlockHash() { + async getBestBlockHash() { // Returns the hash of the best (tip) block in the longest blockchain. // // Result: // "hex" (string) the block hash hex encoded - return axios.get(`${this.restURL}blockchain/getBestBlockHash`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBestBlockHash`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getBlock(blockhash, verbose = true) { + async getBlock(blockhash, verbose = true) { // If verbose is false, returns a string that is serialized, hex-encoded data for block 'hash'. // If verbose is true, returns an Object with information about block . // @@ -52,41 +51,38 @@ class Blockchain { // Result (for verbose=false): // "data" (string) A string that is serialized, hex-encoded data for block 'hash'. - return axios.get(`${this.restURL}blockchain/getBlock/${blockhash}?verbose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBlock/${blockhash}?verbose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getBlockchainInfo() { + async getBlockchainInfo() { // Returns an object containing various state info regarding blockchain processing. - return axios.get(`${this.restURL}blockchain/getBlockchainInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBlockchainInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getBlockCount() { + async getBlockCount() { // Returns the number of blocks in the longest blockchain. // // Result: // n (numeric) The current block count - return axios.get(`${this.restURL}blockchain/getBlockCount`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBlockCount`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getBlockHash(height = 1) { + async getBlockHash(height = 1) { // Returns hash of block in best-block-chain at height provided. // // Arguments: @@ -97,16 +93,15 @@ class Blockchain { if(typeof height !== 'string') { height = JSON.stringify(height); } - return axios.get(`${this.restURL}blockchain/getBlockHash/${height}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBlockHash/${height}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getBlockHeader(hash, verbose = true) { + async getBlockHeader(hash, verbose = true) { // If verbose is false, returns a string that is serialized, hex-encoded data for blockheader 'hash'. // If verbose is true, returns an Object with information about blockheader . @@ -138,16 +133,15 @@ class Blockchain { if(typeof hash !== 'string') { hash = JSON.stringify(hash); } - return axios.get(`${this.restURL}blockchain/getBlockHeader/${hash}?verbose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getBlockHeader/${hash}?verbose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getChainTips() { + async getChainTips() { // Return information about all known tips in the block tree, including the main chain as well as orphaned branches. // // Result: @@ -172,32 +166,30 @@ class Blockchain { // 4. "valid-fork" This branch is not part of the active chain, but is fully validated // 5. "active" This is the tip of the active main chain, which is certainly valid - return axios.get(`${this.restURL}blockchain/getChainTips`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getChainTips`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getDifficulty() { + async getDifficulty() { // Returns the proof-of-work difficulty as a multiple of the minimum difficulty. // // Result: // n.nnn (numeric) the proof-of-work difficulty as a multiple of the minimum difficulty. - return axios.get(`${this.restURL}blockchain/getDifficulty`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getDifficulty`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getMempoolAncestors(txid, verbose = false) { + async getMempoolAncestors(txid, verbose = false) { // If txid is in the mempool, returns all in-mempool ancestors. // // Arguments: @@ -234,16 +226,15 @@ class Blockchain { if(typeof txid !== 'string') { txid = JSON.stringify(txid); } - return axios.get(`${this.restURL}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getMempoolDescendants(txid, verbose = false) { + async getMempoolDescendants(txid, verbose = false) { // If txid is in the mempool, returns all in-mempool descendants. // // Arguments: @@ -281,16 +272,15 @@ class Blockchain { txid = JSON.stringify(txid); } - return axios.get(`${this.restURL}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getMempoolEntry(txid) { + async getMempoolEntry(txid) { // Returns mempool data for given transaction // // Arguments: @@ -319,16 +309,15 @@ class Blockchain { txid = JSON.stringify(txid); } - return axios.get(`${this.restURL}blockchain/getMempoolEntry/${txid}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getMempoolEntry/${txid}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getMempoolInfo() { + async getMempoolInfo() { // Returns details on the active state of the TX memory pool. // // Result: @@ -340,16 +329,15 @@ class Blockchain { // "mempoolminfee": xxxxx (numeric) Minimum fee for tx to be accepted // } - return axios.get(`${this.restURL}blockchain/getMempoolInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getMempoolInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getRawMempool(verbose = false) { + async getRawMempool(verbose = false) { // Returns all transaction ids in memory pool as a json array of string transaction ids. // // Arguments: @@ -382,16 +370,15 @@ class Blockchain { // ... ] // }, ... // } - return axios.get(`${this.restURL}blockchain/getRawMempool?vebose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getRawMempool?vebose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getTxOut(txid, n, include_mempool = true) { + async getTxOut(txid, n, include_mempool = true) { // Returns details about an unspent transaction output. // // Arguments: @@ -418,16 +405,15 @@ class Blockchain { // } // - return axios.get(`${this.restURL}blockchain/getTxOut/${txid}/n?include_mempool=${include_mempool}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/getTxOut/${txid}/n?include_mempool=${include_mempool}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getTxOutProof(txids, blockhash) { + async getTxOutProof(txids, blockhash) { let path = `${this.restURL}blockchain/getTxOutProof/${txids}`; if(blockhash) { path = `${path}?blockhash=${blockhash}` @@ -449,16 +435,15 @@ class Blockchain { // // Result: // "data" (string) A string that is a serialized, hex-encoded data for the proof. - return axios.get(path) - .then((response) => { + try { + let response = await axios.get(path) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - preciousBlock(blockhash) { + async preciousBlock(blockhash) { // Treats a block as if it were received before others with the same work. // // A later preciousblock call can override the effect of an earlier one. @@ -468,32 +453,30 @@ class Blockchain { // Arguments: // 1. "blockhash" (string, required) the hash of the block to mark as precious - return axios.get(`${this.restURL}blockchain/preciousBlock/${blockhash}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/preciousBlock/${blockhash}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - pruneBlockchain(height) { + async pruneBlockchain(height) { // Arguments: // 1. "height" (numeric, required) The block height to prune up to. May be set to a discrete height, or a unix timestamp // to prune blocks whose block time is at least 2 hours older than the provided timestamp. // // Result: // n (numeric) Height of the last block pruned. - return axios.post(`${this.restURL}blockchain/pruneBlockchain/${height}`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}blockchain/pruneBlockchain/${height}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - verifyChain(checklevel = 3, nblocks = 6) { + async verifyChain(checklevel = 3, nblocks = 6) { // Verifies blockchain database. // // Arguments: @@ -502,16 +485,15 @@ class Blockchain { // // Result: // true|false (boolean) Verified or not - return axios.get(`${this.restURL}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - verifyTxOutProof(proof) { + async verifyTxOutProof(proof) { // Verifies that a proof points to a transaction in a block, returning the transaction it commits to // and throwing an RPC error if the block is not in our best chain // @@ -521,13 +503,12 @@ class Blockchain { // Result: // ["txid"] (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid - return axios.get(`${this.restURL}blockchain/verifyTxOutProof/proof`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}blockchain/verifyTxOutProof/proof`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Control.js b/src/Control.js index 9956f96f..39dda419 100644 --- a/src/Control.js +++ b/src/Control.js @@ -4,7 +4,7 @@ class Control { this.restURL = restURL; } - getInfo() { + async getInfo() { // DEPRECATED. Returns an object containing various state info. // // Result: @@ -27,16 +27,15 @@ class Control { // "errors": "..." (string) any error messages // } - return axios.get(`${this.restURL}control/getInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}control/getInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getMemoryInfo() { + async getMemoryInfo() { // Returns an object containing information about memory usage. // @@ -53,13 +52,12 @@ class Control { // } // - return axios.get(`${this.restURL}control/getMemoryInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}control/getMemoryInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // stop() { diff --git a/src/Generating.js b/src/Generating.js index c7ce4cfe..bf8b96f9 100644 --- a/src/Generating.js +++ b/src/Generating.js @@ -4,7 +4,7 @@ class Generating { this.restURL = restURL; } - generateToAddress(blocks, address, maxtries = 1000000) { + async generateToAddress(blocks, address, maxtries = 1000000) { // Mine blocks immediately to a specified address (before the RPC call returns) // @@ -16,13 +16,12 @@ class Generating { // Result: // [ blockhashes ] (array) hashes of blocks generated // - return axios.post(`${this.restURL}generating/generateToAddress/${blocks}/${address}?maxtries=${maxtries}`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}generating/generateToAddress/${blocks}/${address}?maxtries=${maxtries}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Mining.js b/src/Mining.js index ae0ca03d..d1cde954 100644 --- a/src/Mining.js +++ b/src/Mining.js @@ -4,7 +4,7 @@ class Mining { this.restURL = restURL; } - getBlockTemplate(template_request) { + async getBlockTemplate(template_request) { // If the request parameters include a 'mode' key, that is used to explicitly select between the default 'template' request or a 'proposal'. // It returns data needed to construct a block to work on. @@ -28,16 +28,15 @@ class Mining { // ] // } - return axios.get(`${this.restURL}mining/getBlockTemplate/${template_request}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}mining/getBlockTemplate/${template_request}`) return response.data; - }) - .catch((error) => { - return JSON.stringify(error.response.data.message); - }); + } catch (error) { + return JSON.stringify(error.response.data.error.message); + } } - getMiningInfo() { + async getMiningInfo() { // Returns a json object containing mining-related information. // Result: // { @@ -51,16 +50,15 @@ class Mining { // "chain": "xxxx", (string) current network name as defined in BIP70 (main, test, regtest) // } - return axios.get(`${this.restURL}mining/getMiningInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}mining/getMiningInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getNetworkHashps(nblocks = 120, height = 1) { + async getNetworkHashps(nblocks = 120, height = 1) { // Returns the estimated network hashes per second based on the last n blocks. // Pass in [blocks] to override # of blocks, -1 specifies since last difficulty change. // Pass in [height] to estimate the network speed at the time when a certain block was found. @@ -71,16 +69,15 @@ class Mining { // // Result: // x (numeric) Hashes per second estimated - return axios.get(`${this.restURL}mining/getNetworkHashps?nblocks=${nblocks}&height=${height}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}mining/getNetworkHashps?nblocks=${nblocks}&height=${height}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - submitBlock(hex, parameters) { + async submitBlock(hex, parameters) { // Attempts to submit new block to network. // The 'jsonparametersobject' parameter is currently ignored. // See https://en.bitcoin.it/wiki/BIP_0022 for full specification. @@ -96,13 +93,12 @@ class Mining { if(parameters) { path = `${path}?parameters=${parameters}`; } - return axios.post(path) - .then((response) => { + try { + let response = await axios.post(path) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Network.js b/src/Network.js index 3997e69a..6bca0121 100644 --- a/src/Network.js +++ b/src/Network.js @@ -4,7 +4,7 @@ class Network { this.restURL = restURL; } - addNode(node, command){ + async addNode(node, command){ // Attempts add or remove a node from the addnode list. // Or try a connection to a node once. // @@ -13,16 +13,15 @@ class Network { // 2. "command" (string, required) 'add' to add a node to the list, 'remove' to remove a node from the list, 'onetry' to try a connection to the node once // - return axios.post(`${this.restURL}network/addNode/${node}/${command}`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}network/addNode/${node}/${command}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - clearBanned() { + async clearBanned() { //The clearbanned RPC clears list of banned nodes. // Parameters: none @@ -30,16 +29,15 @@ class Network { // Result—null on success // JSON null when the list was cleared - return axios.post(`${this.restURL}clearBanned`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}clearBanned`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - disconnectNode(configuration) { + async disconnectNode(configuration) { // Immediately disconnects from the specified peer node. // // Strictly one out of 'configuration.address' and 'configuration.nodeid' can be provided to identify the node. @@ -51,16 +49,15 @@ class Network { // Properties // 1. "address" (string, optional) The IP address/port of the node // 2. "nodeid" (number, optional) The node ID (see getpeerinfo for node IDs) - return axios.post(`${this.restURL}disconnectNode/${configuration}`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}disconnectNode/${configuration}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getAddedNodeInfo(node) { + async getAddedNodeInfo(node) { // Returns information about the given added node, or all added nodes // (note that onetry addnodes are not listed here) // @@ -86,31 +83,29 @@ class Network { path = `${path}?node=${node}`; } - return axios.get(path) - .then((response) => { + try { + let response = await axios.get(path) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getConnectionCount() { + async getConnectionCount() { // Returns the number of connections to other nodes. // // Result: // n (numeric) The connection count - return axios.get(`${this.restURL}network/getConnectionCount`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}network/getConnectionCount`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getNetTotals() { + async getNetTotals() { // Returns information about network traffic, including bytes in, bytes out, and current time. // // Result: @@ -129,16 +124,15 @@ class Network { // } // } - return axios.get(`${this.restURL}network/getNetTotals`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}network/getNetTotals`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getNetworkInfo() { + async getNetworkInfo() { // Returns an object containing various state info regarding P2P networking. // // Result: @@ -174,16 +168,15 @@ class Network { // "warnings": "..." (string) any network warnings // } - return axios.get(`${this.restURL}network/getNetworkInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}network/getNetworkInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getPeerInfo() { + async getPeerInfo() { // Returns data about each connected network node as a json array of objects. // // Result: @@ -228,13 +221,12 @@ class Network { // ,... // ] - return axios.get(`${this.restURL}network/getPeerInfo`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}network/getPeerInfo`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // listBanned() { @@ -248,18 +240,17 @@ class Network { // }); // } - ping() { + async ping() { // Requests that a ping be sent to all other nodes, to measure ping time. // Results provided in getpeerinfo, pingtime and pingwait fields are decimal seconds. // Ping command is handled in queue with all other commands, so it measures processing backlog, not just network ping. - return axios.get(`${this.restURL}network/ping`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}network/ping`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } // // setBan(subnet, command, bantime, absolute) { diff --git a/src/Price.js b/src/Price.js index 9477de02..3c42b656 100644 --- a/src/Price.js +++ b/src/Price.js @@ -1,17 +1,16 @@ import axios from 'axios'; class Price { - current(currency = 'all') { - return axios.get(`https://www.blocktrail.com/BCC/json/blockchain/price`) - .then((response) => { + async current(currency = 'all') { + try { + let response = await axios.get(`https://www.blocktrail.com/BCC/json/blockchain/price`) if(currency === 'all') { return response.data; } else { return response.data[currency.toUpperCase()]; } - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/RawTransactions.js b/src/RawTransactions.js index 6ea1096b..aa482cc1 100644 --- a/src/RawTransactions.js +++ b/src/RawTransactions.js @@ -4,7 +4,7 @@ class RawTransactions { this.restURL = restURL; } - decodeRawTransaction(hex) { + async decodeRawTransaction(hex) { // decodes a serialized transaction hex string into a JSON object describing the transaction. // Parameter #1—serialized transaction in hex @@ -14,16 +14,15 @@ class RawTransactions { hex = JSON.stringify(hex); } - return axios.get(`${this.restURL}rawtransactions/decodeRawTransaction/${hex}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}rawtransactions/decodeRawTransaction/${hex}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - decodeScript(script) { + async decodeScript(script) { // decodes a hex-encoded P2SH redeem script. // Parameter #1—a hex-encoded redeem script @@ -33,16 +32,15 @@ class RawTransactions { script = JSON.stringify(script); } - return axios.get(`${this.restURL}rawtransactions/decodeScript/${script}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}rawtransactions/decodeScript/${script}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - getRawTransaction(txid, verbose = false) { + async getRawTransaction(txid, verbose = false) { // NOTE: By default this function only works for mempool transactions. If the -txindex option is // enabled, it also works for blockchain transactions. // DEPRECATED: for now, it also works for transactions with unspent outputs. @@ -63,16 +61,15 @@ class RawTransactions { txid = JSON.stringify(txid); } - return axios.get(`${this.restURL}rawtransactions/getRawTransaction/${txid}?verbose=${verbose}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}rawtransactions/getRawTransaction/${txid}?verbose=${verbose}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } - sendRawTransaction(hex, allowhighfees = false) { + async sendRawTransaction(hex, allowhighfees = false) { // Submits raw transaction (serialized, hex-encoded) to local node and network. // // Also see createrawtransaction and signrawtransaction calls. @@ -88,13 +85,12 @@ class RawTransactions { hex = JSON.stringify(hex); } - return axios.post(`${this.restURL}rawtransactions/sendRawTransaction/${hex}?allowhighfees=${allowhighfees}`) - .then((response) => { + try { + let response = await axios.post(`${this.restURL}rawtransactions/sendRawTransaction/${hex}?allowhighfees=${allowhighfees}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Transaction.js b/src/Transaction.js index 5ff4fa3d..0655da79 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -22,18 +22,17 @@ class Transaction { return Bitcoin.TransactionBuilder.fromTransaction(tx); } - details(txid) { + async details(txid) { if(typeof txid !== 'string') { txid = JSON.stringify(txid); } - return axios.get(`${this.restURL}transaction/details/${txid}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}transaction/details/${txid}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } } diff --git a/src/Util.js b/src/Util.js index 85d29c52..36bebdba 100644 --- a/src/Util.js +++ b/src/Util.js @@ -4,7 +4,7 @@ class Util { this.restURL = restURL; } - validateAddress(address) { + async validateAddress(address) { // Return information about the given bitcoin address. // // Arguments: @@ -25,13 +25,12 @@ class Util { // "hdkeypath" : "keypath" (string, optional) The HD keypath if the key is HD and available // "hdmasterkeyid" : "" (string, optional) The Hash160 of the HD master pubkey // } - return axios.get(`${this.restURL}util/validateAddress/${address}`) - .then((response) => { + try { + let response = await axios.get(`${this.restURL}util/validateAddress/${address}`) return response.data; - }) - .catch((error) => { + } catch (error) { return JSON.stringify(error.response.data.error.message); - }); + } } }