mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 09:12:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43dfea7cbe | ||
|
|
a2c30aee6d | ||
|
|
1959b20115 | ||
|
|
e399e05022 | ||
|
|
2d04169329 | ||
|
|
5619c2fbed | ||
|
|
388a3ee53b | ||
|
|
8aff89dead | ||
|
|
efd669e5fe | ||
|
|
d5433ed811 | ||
|
|
6b2588c487 | ||
|
|
4aee109223 | ||
|
|
5f63e53a6d | ||
|
|
bde809d62d | ||
|
|
c664758654 | ||
|
|
b4823be86a | ||
|
|
1c9e0858de | ||
|
|
41a3cd91fc | ||
|
|
e97a0be456 | ||
|
|
085618470f | ||
|
|
22d7a2b7a2 |
+3
-1
@@ -3,7 +3,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
apiTokenSecret: process.env.TOKENSECRET ? process.env.TOKENSECRET : 'secret-jwt-token'
|
apiTokenSecret: process.env.TOKENSECRET
|
||||||
|
? process.env.TOKENSECRET
|
||||||
|
: 'secret-jwt-token'
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = config
|
module.exports = config
|
||||||
|
|||||||
Generated
+7
-5
@@ -6458,6 +6458,13 @@
|
|||||||
"p-cancelable": "^1.0.0",
|
"p-cancelable": "^1.0.0",
|
||||||
"to-readable-stream": "^1.0.0",
|
"to-readable-stream": "^1.0.0",
|
||||||
"url-parse-lax": "^3.0.0"
|
"url-parse-lax": "^3.0.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"p-cancelable": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw=="
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"graceful-fs": {
|
"graceful-fs": {
|
||||||
@@ -13340,11 +13347,6 @@
|
|||||||
"resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz",
|
||||||
"integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ="
|
"integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ="
|
||||||
},
|
},
|
||||||
"p-cancelable": {
|
|
||||||
"version": "1.1.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-1.1.0.tgz",
|
|
||||||
"integrity": "sha512-s73XxOZ4zpt1edZYZzvhqFa6uvQc1vwUa0K0BdtIZgQMAJj9IbebH+JkgKZc9h+B05PKHLOTl4ajG1BmNrVZlw=="
|
|
||||||
},
|
|
||||||
"p-each-series": {
|
"p-each-series": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-2.1.0.tgz",
|
||||||
|
|||||||
+2
-2
@@ -17,9 +17,9 @@
|
|||||||
"test": "npm run lint && npm run test-v3",
|
"test": "npm run lint && npm run test-v3",
|
||||||
"lint": "standard --env mocha --fix",
|
"lint": "standard --env mocha --fix",
|
||||||
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/",
|
"test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/",
|
||||||
"test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/util.js",
|
"test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/slp.js",
|
||||||
"test:integration": "mocha test/v3/integration",
|
"test:integration": "mocha test/v3/integration",
|
||||||
"test:integration:slpdb": "mocha --timeout 25000 test/v3/integration/slp*.js",
|
"test:integration:slpdb": "mocha --timeout 25000 -g '#validate2Single' test/v3/integration/slp*.js",
|
||||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||||
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v3/",
|
"coverage:report": "export NETWORK=mainnet && nyc --reporter=html mocha --timeout 25000 test/v3/",
|
||||||
"docs": "./node_modules/.bin/apidoc -i src/routes/v3 -o docs"
|
"docs": "./node_modules/.bin/apidoc -i src/routes/v3 -o docs"
|
||||||
|
|||||||
@@ -136,7 +136,10 @@ class RateLimits {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply paid-access rate limits based on key/IP
|
// Apply paid-access rate limits based on key/IP
|
||||||
if (key.toString().indexOf('172.17.') > -1) {
|
if (
|
||||||
|
key.toString().indexOf('172.17.') > -1 ||
|
||||||
|
key.toString().indexOf('::ffff:127.0.0.1') > -1
|
||||||
|
) {
|
||||||
pointsToConsume = 1
|
pointsToConsume = 1
|
||||||
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
res.locals.pointsToConsume = pointsToConsume // Feedback for tests.
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ class Electrum {
|
|||||||
_this.router.post('/utxos', _this.utxosBulk)
|
_this.router.post('/utxos', _this.utxosBulk)
|
||||||
_this.router.get('/tx/data/:txid', _this.getTransactionDetails)
|
_this.router.get('/tx/data/:txid', _this.getTransactionDetails)
|
||||||
_this.router.post('/tx/data', _this.transactionDetailsBulk)
|
_this.router.post('/tx/data', _this.transactionDetailsBulk)
|
||||||
|
_this.router.post('/tx/broadcast', _this.broadcastTransaction)
|
||||||
_this.router.get('/block/headers/:height', _this.getBlockHeaders)
|
_this.router.get('/block/headers/:height', _this.getBlockHeaders)
|
||||||
_this.router.post('/block/headers', _this.blockHeadersBulk)
|
_this.router.post('/block/headers', _this.blockHeadersBulk)
|
||||||
_this.router.get('/balance/:address', _this.getBalance)
|
_this.router.get('/balance/:address', _this.getBalance)
|
||||||
@@ -491,6 +492,92 @@ class Electrum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a promise that resolves to transaction ID of the broadcasted transaction or an error.
|
||||||
|
// Expects input to be a txHex string, and input validation to have already
|
||||||
|
// been done by parent, calling function.
|
||||||
|
async _broadcastTransactionWithElectrum (txHex) {
|
||||||
|
try {
|
||||||
|
if (!_this.isReady) {
|
||||||
|
throw new Error(
|
||||||
|
'ElectrumX server connection is not ready. Call await connectToServer() first.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Broadcast the transaction hex to the ElectrumX server.
|
||||||
|
const electrumResponse = await _this.electrumx.request(
|
||||||
|
'blockchain.transaction.broadcast',
|
||||||
|
txHex
|
||||||
|
)
|
||||||
|
// console.log(
|
||||||
|
// `electrumResponse: ${JSON.stringify(electrumResponse, null, 2)}`
|
||||||
|
// )
|
||||||
|
|
||||||
|
return electrumResponse
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err: ', err)
|
||||||
|
|
||||||
|
// Write out error to error log.
|
||||||
|
wlogger.error(
|
||||||
|
'Error in elecrumx.js/_transactionDetailsFromElectrum(): ',
|
||||||
|
err
|
||||||
|
)
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @api {post} /electrumx/tx/broadcast Broadcast a raw transaction
|
||||||
|
* @apiName Broadcast a raw transaction
|
||||||
|
* @apiGroup ElectrumX / Fulcrum
|
||||||
|
* @apiDescription Broadcast a raw transaction and return the transaction ID on success or error on failure.
|
||||||
|
*
|
||||||
|
* @apiExample Example usage:
|
||||||
|
* curl -X POST "https://api.fullstack.cash/v3/electrumx/tx/broadcast" -H "accept: application/json" -H "Content-Type: application/json" -d '{"txHex":"020000000265d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667010000006441dd1dd72770cadede1a7fd0363574846c48468a398ddfa41a9677c74cac8d2652b682743725a3b08c6c2021a629011e11a264d9036e9d5311e35b5f4937ca7b4e4121020797d8fd4d2fa6fd7cdeabe2526bfea2b90525d6e8ad506ec4ee3c53885aa309ffffffff65d13ef402840c8a51f39779afb7ae4d49e4b0a3c24a3d0e7742038f2c679667000000006441347d7f218c11c04487c1ad8baac28928fb10e5054cd4494b94d078cfa04ccf68e064fb188127ff656c0b98e9ce87f036d183925d0d0860605877d61e90375f774121028a53f95eb631b460854fc836b2e5d31cad16364b4dc3d970babfbdcc3f2e4954ffffffff035ac355000000000017a914189ce02e332548f4804bac65cba68202c9dbf822878dfd0800000000001976a914285bb350881b21ac89724c6fb6dc914d096cd53b88acf9ef3100000000001976a91445f1f1c4a9b9419a5088a3e9c24a293d7a150e6488ac00000000"}'
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
// POST handler for broadcasting a single transaction
|
||||||
|
async broadcastTransaction (req, res, next) {
|
||||||
|
try {
|
||||||
|
const txHex = req.body.txHex
|
||||||
|
|
||||||
|
if (typeof txHex !== 'string') {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
error: 'request body must be a string.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
wlogger.debug(
|
||||||
|
'Executing electrumx/broadcastTransaction with this tx hex: ',
|
||||||
|
txHex
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get data from ElectrumX server.
|
||||||
|
const electrumResponse = await _this._broadcastTransactionWithElectrum(txHex)
|
||||||
|
// console.log(`_utxosFromElectrumx(): ${JSON.stringify(electrumResponse, null, 2)}`)
|
||||||
|
|
||||||
|
// Pass the error message if ElectrumX reports an error.
|
||||||
|
if (electrumResponse instanceof Error) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
error: electrumResponse.message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
txid: electrumResponse
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
wlogger.error('Error in electrumx.js/broadcastTransaction().', err)
|
||||||
|
|
||||||
|
return _this.errorHandler(err, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns a promise that resolves to block header data for a block height.
|
// Returns a promise that resolves to block header data for a block height.
|
||||||
// Expects input to be a height number, and input validation to have already
|
// Expects input to be a height number, and input validation to have already
|
||||||
// been done by parent, calling function.
|
// been done by parent, calling function.
|
||||||
|
|||||||
+76
-7
@@ -13,8 +13,12 @@ const Slpdb = require('./services/slpdb')
|
|||||||
// const strftime = require('strftime')
|
// const strftime = require('strftime')
|
||||||
const wlogger = require('../../util/winston-logging')
|
const wlogger = require('../../util/winston-logging')
|
||||||
|
|
||||||
|
// Instantiate a local copy of bch-js using the local REST API server.
|
||||||
|
const LOCAL_RESTURL = process.env.LOCAL_RESTURL
|
||||||
|
? process.env.LOCAL_RESTURL
|
||||||
|
: 'https://api.fullstack.cash/v3/'
|
||||||
const BCHJS = require('@psf/bch-js')
|
const BCHJS = require('@psf/bch-js')
|
||||||
const bchjs = new BCHJS()
|
const bchjs = new BCHJS({ restURL: LOCAL_RESTURL })
|
||||||
|
|
||||||
// Used to convert error messages to strings, to safely pass to users.
|
// Used to convert error messages to strings, to safely pass to users.
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
@@ -48,6 +52,7 @@ class Slp {
|
|||||||
constructor () {
|
constructor () {
|
||||||
_this = this
|
_this = this
|
||||||
|
|
||||||
|
// Encapsulate external libraries.
|
||||||
_this.axios = axios
|
_this.axios = axios
|
||||||
_this.routeUtils = routeUtils
|
_this.routeUtils = routeUtils
|
||||||
_this.BigNumber = BigNumber
|
_this.BigNumber = BigNumber
|
||||||
@@ -68,6 +73,7 @@ class Slp {
|
|||||||
_this.router.post('/convert', _this.convertAddressBulk)
|
_this.router.post('/convert', _this.convertAddressBulk)
|
||||||
_this.router.post('/validateTxid', _this.validateBulk)
|
_this.router.post('/validateTxid', _this.validateBulk)
|
||||||
_this.router.get('/validateTxid/:txid', _this.validateSingle)
|
_this.router.get('/validateTxid/:txid', _this.validateSingle)
|
||||||
|
_this.router.get('/validateTxid2/:txid', _this.validate2Single)
|
||||||
_this.router.get('/txDetails/:txid', _this.txDetails)
|
_this.router.get('/txDetails/:txid', _this.txDetails)
|
||||||
_this.router.get('/tokenStats/:tokenId', _this.tokenStats)
|
_this.router.get('/tokenStats/:tokenId', _this.tokenStats)
|
||||||
_this.router.get(
|
_this.router.get(
|
||||||
@@ -1059,7 +1065,7 @@ class Slp {
|
|||||||
* @api {get} /slp/validateTxid/{txid} Validate single SLP transaction by txid.
|
* @api {get} /slp/validateTxid/{txid} Validate single SLP transaction by txid.
|
||||||
* @apiName Validate single SLP transaction by txid.
|
* @apiName Validate single SLP transaction by txid.
|
||||||
* @apiGroup SLP
|
* @apiGroup SLP
|
||||||
* @apiDescription Validate single SLP transaction by txid.
|
* @apiDescription Validate single SLP transaction by txid, using SLPDB.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @apiExample Example usage:
|
* @apiExample Example usage:
|
||||||
@@ -1132,11 +1138,66 @@ class Slp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a Boolean if the input TXID is a valid SLP TXID.
|
/**
|
||||||
// async function isValidSlpTxid (txid) {
|
* @api {get} /slp/validateTxid2/{txid} Validate single SLP transaction by txid.
|
||||||
// const isValid = await slpValidator.isValidSlpTxid(txid)
|
* @apiName Validate single SLP transaction by txid.
|
||||||
// return isValid
|
* @apiGroup SLP
|
||||||
// }
|
* @apiDescription Validate single SLP transaction by txid, using slp-validate.
|
||||||
|
* Slower, less efficient method of validating an SLP TXID using the slp-validate
|
||||||
|
* npm library. This method is independent of SLPDB and can be used as a fall-back
|
||||||
|
* when SLPDB returns 'null' values.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @apiExample Example usage:
|
||||||
|
* curl -X GET "https://api.fullstack.cash/v3/slp/validateTxid2/f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a" -H "accept:application/json"
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
async validate2Single (req, res, next) {
|
||||||
|
try {
|
||||||
|
const txid = req.params.txid
|
||||||
|
|
||||||
|
// Validate input
|
||||||
|
if (!txid || txid === '') {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({ error: 'txid can not be empty' })
|
||||||
|
}
|
||||||
|
|
||||||
|
wlogger.debug(
|
||||||
|
'Executing slp/validate2Single/:txid with this txid: ',
|
||||||
|
txid
|
||||||
|
)
|
||||||
|
|
||||||
|
// null by default.
|
||||||
|
// Default return value.
|
||||||
|
const result = {
|
||||||
|
txid: txid,
|
||||||
|
isValid: null,
|
||||||
|
msg: ''
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request options
|
||||||
|
const opt = {
|
||||||
|
method: 'get',
|
||||||
|
baseURL: `${process.env.SLP_API_URL}slp/validate/${txid}`,
|
||||||
|
timeout: 10000 // Exit after 10 seconds.
|
||||||
|
}
|
||||||
|
const tokenRes = await _this.axios.request(opt)
|
||||||
|
// console.log(`tokenRes.data: ${JSON.stringify(tokenRes.data, null, 2)}`)
|
||||||
|
// console.log(`tokenRes: `, tokenRes)
|
||||||
|
|
||||||
|
// Overwrite the default value with the result from slp-api.
|
||||||
|
result.isValid = tokenRes.data.isValid
|
||||||
|
|
||||||
|
res.status(200)
|
||||||
|
return res.json(result)
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('validate2Single error: ', err)
|
||||||
|
wlogger.error('Error in slp.ts/validate2Single().', err)
|
||||||
|
|
||||||
|
return _this.errorHandler(err, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {get} /slp/txDetails/{txid} SLP transaction details.
|
* @api {get} /slp/txDetails/{txid} SLP transaction details.
|
||||||
@@ -1567,6 +1628,13 @@ class Slp {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!utxos[0].utxos) {
|
||||||
|
res.status(422)
|
||||||
|
return res.json({
|
||||||
|
error: 'Each element in array should have a utxos property'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Loop through each address and query the UTXOs for that element.
|
// Loop through each address and query the UTXOs for that element.
|
||||||
for (let i = 0; i < utxos.length; i++) {
|
for (let i = 0; i < utxos.length; i++) {
|
||||||
const theseUtxos = utxos[i].utxos
|
const theseUtxos = utxos[i].utxos
|
||||||
@@ -1583,6 +1651,7 @@ class Slp {
|
|||||||
return res.json({ slpUtxos: utxos })
|
return res.json({ slpUtxos: utxos })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
wlogger.error('Error in slp.js/hydrateUtxos().', err)
|
wlogger.error('Error in slp.js/hydrateUtxos().', err)
|
||||||
|
console.error('Error in slp.js/hydrateUtxos().', err)
|
||||||
|
|
||||||
// Decode the error message.
|
// Decode the error message.
|
||||||
const { msg, status } = routeUtils.decodeError(err)
|
const { msg, status } = routeUtils.decodeError(err)
|
||||||
|
|||||||
@@ -906,6 +906,92 @@ describe('#ElectrumX Router', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#_broadcastTransactionWithElectrum', () => {
|
||||||
|
it('should return error object for invalid formatted transaction', async () => {
|
||||||
|
const invalidHex = mockData.txDetails.hex.substring(10)
|
||||||
|
|
||||||
|
stubMethodForUnitTests(
|
||||||
|
electrumxRoute.electrumx,
|
||||||
|
'request',
|
||||||
|
new Error('Error: the transaction was rejected by network rules.\n\nTX decode failed\n')
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await electrumxRoute._broadcastTransactionWithElectrum(invalidHex)
|
||||||
|
|
||||||
|
assert.instanceOf(result, Error)
|
||||||
|
assert.include(result.message, 'TX decode failed')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return txid for valid transaction', async function () {
|
||||||
|
// We cannot send an actual broadcast transaction to mainnet
|
||||||
|
if (process.env.TEST !== 'unit') return this.skip()
|
||||||
|
|
||||||
|
const validHex = mockData.txDetails.hex
|
||||||
|
|
||||||
|
stubMethodForUnitTests(
|
||||||
|
electrumxRoute.electrumx,
|
||||||
|
'request',
|
||||||
|
mockData.txDetails.hash
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await electrumxRoute._broadcastTransactionWithElectrum(validHex)
|
||||||
|
|
||||||
|
assert.typeOf(result, 'string')
|
||||||
|
assert.equal(result, mockData.txDetails.hash)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#broadcastTransaction', () => {
|
||||||
|
it('should throw an error for a non-string', async () => {
|
||||||
|
req.body = 456
|
||||||
|
|
||||||
|
stubMethodForUnitTests(
|
||||||
|
electrumxRoute,
|
||||||
|
'_broadcastTransactionWithElectrum',
|
||||||
|
new Error('request body must be a string.')
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await electrumxRoute.broadcastTransaction(req, res)
|
||||||
|
|
||||||
|
expectRouteError(res, result, 'request body must be a string.')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw an error object for invalid formatted transaction', async () => {
|
||||||
|
req.body.txHex = mockData.txDetails.hex.substring(10)
|
||||||
|
|
||||||
|
stubMethodForUnitTests(
|
||||||
|
electrumxRoute,
|
||||||
|
'_broadcastTransactionWithElectrum',
|
||||||
|
new Error('Error: the transaction was rejected by network rules.\n\nTX decode failed\n')
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await electrumxRoute.broadcastTransaction(req, res)
|
||||||
|
|
||||||
|
expectRouteError(res, result, 'TX decode failed')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should return txid for valid transaction', async function () {
|
||||||
|
// We cannot send an actual broadcast transaction to mainnet
|
||||||
|
if (process.env.TEST !== 'unit') return this.skip()
|
||||||
|
|
||||||
|
req.body.txHex = mockData.txDetails.hex
|
||||||
|
|
||||||
|
stubMethodForUnitTests(
|
||||||
|
electrumxRoute,
|
||||||
|
'_broadcastTransactionWithElectrum',
|
||||||
|
mockData.txDetails.hash
|
||||||
|
)
|
||||||
|
|
||||||
|
const result = await electrumxRoute.broadcastTransaction(req, res)
|
||||||
|
|
||||||
|
assert.property(result, 'success')
|
||||||
|
assert.equal(result.success, true)
|
||||||
|
|
||||||
|
assert.property(result, 'txid')
|
||||||
|
assert.equal(result.txid, mockData.txDetails.hash)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('#_balanceFromElectrumx', () => {
|
describe('#_balanceFromElectrumx', () => {
|
||||||
it('should throw error for invalid address', async () => {
|
it('should throw error for invalid address', async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -213,4 +213,50 @@ describe('#slp', () => {
|
|||||||
assert.equal(result.slpUtxos[2].utxos.length, 2)
|
assert.equal(result.slpUtxos[2].utxos.length, 2)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#validate2Single', () => {
|
||||||
|
it('should invalidate a known invalid TXID', async () => {
|
||||||
|
const txid =
|
||||||
|
'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a'
|
||||||
|
|
||||||
|
req.params.txid = txid
|
||||||
|
const result = await slp.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.equal(result.txid, txid)
|
||||||
|
assert.equal(result.isValid, false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should validate a known valid TXID', async () => {
|
||||||
|
const txid =
|
||||||
|
'3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488'
|
||||||
|
|
||||||
|
req.params.txid = txid
|
||||||
|
const result = await slp.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.equal(result.txid, txid)
|
||||||
|
assert.equal(result.isValid, true)
|
||||||
|
})
|
||||||
|
|
||||||
|
// CT 10-11-2020: This test is valid, but because of the cacheing built
|
||||||
|
// into slp-validate, it will not consistently pass or fail. To manually
|
||||||
|
// Run this test, re-start slp-api or find a token txid with a long DAG.
|
||||||
|
// it('should cancel if validation takes too long', async () => {
|
||||||
|
// const txid =
|
||||||
|
// 'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579'
|
||||||
|
// // '2822f7d21e17ca8c36846613dc52caf0d03e355a13022a6cec066d197e59bb27'
|
||||||
|
//
|
||||||
|
// req.params.txid = txid
|
||||||
|
// const result = await slp.validate2Single(req, res)
|
||||||
|
// // console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
//
|
||||||
|
// assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||||
|
// assert.include(
|
||||||
|
// result.error,
|
||||||
|
// 'Could not communicate with full node',
|
||||||
|
// 'Error message expected'
|
||||||
|
// )
|
||||||
|
// })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -352,7 +352,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
h: '7bdd586ebd1e5f3dfd5295ab6e896c48b25855ab77a72c035ce1e7aacc965c2d',
|
h: '7bdd586ebd1e5f3dfd5295ab6e896c48b25855ab77a72c035ce1e7aacc965c2d',
|
||||||
i: 1,
|
i: 1,
|
||||||
s: 'RzBEAiAsu5o9EnxgZChjxKygxhGLhlfxJJbQfd4PP/vp2od3fAIgPpMqg+Sp4YpiInQZ6weJwELq8LEIrHXJpueBz7yF5gRBIQLsyL3809mRphc/+tPAcTQO5bi0kqZRFKpKIHXE3qN8Rw==',
|
s:
|
||||||
|
'RzBEAiAsu5o9EnxgZChjxKygxhGLhlfxJJbQfd4PP/vp2od3fAIgPpMqg+Sp4YpiInQZ6weJwELq8LEIrHXJpueBz7yF5gRBIQLsyL3809mRphc/+tPAcTQO5bi0kqZRFKpKIHXE3qN8Rw==',
|
||||||
a: 'simpleledger:qrrrpqmdkggpnw0czwg0jgcjd7yhu25jy5zxh2gqdq'
|
a: 'simpleledger:qrrrpqmdkggpnw0czwg0jgcjd7yhu25jy5zxh2gqdq'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -361,7 +362,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
h: 'df49feff24dc34a10a44a0cbd7c908d964801ec7218512c84574fbce698535f0',
|
h: 'df49feff24dc34a10a44a0cbd7c908d964801ec7218512c84574fbce698535f0',
|
||||||
i: 3,
|
i: 3,
|
||||||
s: 'SDBFAiEAhW3zbKTlPrOXD2E2oEcNof6vCMPGYIg8vOVSE9c8IakCIFLg/gxydG9eL8HMAzkScGElKWJRnfhVMpMHU3evNcrZQSEDRS7F+pSC8OxSldsT4FctJLZBU7f2+FDiE05ae1xqtN0=',
|
s:
|
||||||
|
'SDBFAiEAhW3zbKTlPrOXD2E2oEcNof6vCMPGYIg8vOVSE9c8IakCIFLg/gxydG9eL8HMAzkScGElKWJRnfhVMpMHU3evNcrZQSEDRS7F+pSC8OxSldsT4FctJLZBU7f2+FDiE05ae1xqtN0=',
|
||||||
a: 'simpleledger:qpkpeqfslejw5pptzcy25h2jxhsc9k0vts43n26up0'
|
a: 'simpleledger:qpkpeqfslejw5pptzcy25h2jxhsc9k0vts43n26up0'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,7 +373,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
v: 0,
|
v: 0,
|
||||||
i: 0,
|
i: 0,
|
||||||
s: 'agRTTFAAAQEEU0VORCA46XxdfTWFosvz+VgMgsozmF+csIRdTcziIMtwn5U4sAgAAAAAAJiWgAgAAAAAJHfU+g=='
|
s:
|
||||||
|
'agRTTFAAAQEEU0VORCA46XxdfTWFosvz+VgMgsozmF+csIRdTcziIMtwn5U4sAgAAAAAAJiWgAgAAAAAJHfU+g=='
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -402,7 +405,8 @@ const mockTxHistory = [
|
|||||||
slp: {
|
slp: {
|
||||||
detail: {
|
detail: {
|
||||||
decimals: 8,
|
decimals: 8,
|
||||||
tokenIdHex: '38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0',
|
tokenIdHex:
|
||||||
|
'38e97c5d7d3585a2cbf3f9580c82ca33985f9cb0845d4dcce220cb709f9538b0',
|
||||||
transactionType: 'SEND',
|
transactionType: 'SEND',
|
||||||
versionType: 1,
|
versionType: 1,
|
||||||
documentUri: 'psfoundation.cash',
|
documentUri: 'psfoundation.cash',
|
||||||
@@ -439,7 +443,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
h: 'e4e1e1f6b502cbd42f69b919634cea3e37fc8595e6ca800e0a7d8f6dcdfb249e',
|
h: 'e4e1e1f6b502cbd42f69b919634cea3e37fc8595e6ca800e0a7d8f6dcdfb249e',
|
||||||
i: 3,
|
i: 3,
|
||||||
s: 'SDBFAiEAjCGRmU28x24LMTwg5XqC+fLf3zGhTYCsSWpmF8Eshq0CIAJhxAKMbwKWC4ASmjWzplpno2ch+hGNUD6HNWOdbd4SQSECeRsZo5Fl29g0A9bfJo1E/WIdowWBsLblyxWnEB7ViFE=',
|
s:
|
||||||
|
'SDBFAiEAjCGRmU28x24LMTwg5XqC+fLf3zGhTYCsSWpmF8Eshq0CIAJhxAKMbwKWC4ASmjWzplpno2ch+hGNUD6HNWOdbd4SQSECeRsZo5Fl29g0A9bfJo1E/WIdowWBsLblyxWnEB7ViFE=',
|
||||||
a: 'simpleledger:qpvsg9vl9a5mlf37a7n3yce6pktdctn73qznkmw3s7'
|
a: 'simpleledger:qpvsg9vl9a5mlf37a7n3yce6pktdctn73qznkmw3s7'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -448,7 +453,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
h: '438420345dd8b7fb4ea74aaf2e3090f44899bf3c71662243946a441de6dae720',
|
h: '438420345dd8b7fb4ea74aaf2e3090f44899bf3c71662243946a441de6dae720',
|
||||||
i: 2,
|
i: 2,
|
||||||
s: 'SDBFAiEAhmLYL4QY3mvZo3/i0XG9PJAYMruw3MaGOLJ3Z4si7hwCIAaaGwRnj/cZK3L/KKFBoMtItpGRZ10GowNN//vgn6PKQSECeRsZo5Fl29g0A9bfJo1E/WIdowWBsLblyxWnEB7ViFE=',
|
s:
|
||||||
|
'SDBFAiEAhmLYL4QY3mvZo3/i0XG9PJAYMruw3MaGOLJ3Z4si7hwCIAaaGwRnj/cZK3L/KKFBoMtItpGRZ10GowNN//vgn6PKQSECeRsZo5Fl29g0A9bfJo1E/WIdowWBsLblyxWnEB7ViFE=',
|
||||||
a: 'simpleledger:qpvsg9vl9a5mlf37a7n3yce6pktdctn73qznkmw3s7'
|
a: 'simpleledger:qpvsg9vl9a5mlf37a7n3yce6pktdctn73qznkmw3s7'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -458,7 +464,8 @@ const mockTxHistory = [
|
|||||||
e: {
|
e: {
|
||||||
v: 0,
|
v: 0,
|
||||||
i: 0,
|
i: 0,
|
||||||
s: 'agRTTFAAAQEEU0VORCCk+1wtoaoGTiUBikP5FlBABx2emEuhkMIip/WQU6+EsggAAAAAAA9CQAgAAAAXQnHEwA=='
|
s:
|
||||||
|
'agRTTFAAAQEEU0VORCCk+1wtoaoGTiUBikP5FlBABx2emEuhkMIip/WQU6+EsggAAAAAAA9CQAgAAAAXQnHEwA=='
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -489,7 +496,8 @@ const mockTxHistory = [
|
|||||||
slp: {
|
slp: {
|
||||||
detail: {
|
detail: {
|
||||||
decimals: 2,
|
decimals: 2,
|
||||||
tokenIdHex: 'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
|
tokenIdHex:
|
||||||
|
'a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2',
|
||||||
transactionType: 'SEND',
|
transactionType: 'SEND',
|
||||||
versionType: 1,
|
versionType: 1,
|
||||||
documentUri: 'troutsblog.com',
|
documentUri: 'troutsblog.com',
|
||||||
|
|||||||
+103
@@ -362,6 +362,7 @@ describe('#SLP', () => {
|
|||||||
assert.hasAllKeys(result, ['error'])
|
assert.hasAllKeys(result, ['error'])
|
||||||
assert.include(result.error, 'Array too large')
|
assert.include(result.error, 'Array too large')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns proper error when downstream service stalls', async () => {
|
it('returns proper error when downstream service stalls', async () => {
|
||||||
// Mock the timeout error.
|
// Mock the timeout error.
|
||||||
sandbox.stub(slpRoute.axios, 'request').throws({ code: 'ECONNABORTED' })
|
sandbox.stub(slpRoute.axios, 'request').throws({ code: 'ECONNABORTED' })
|
||||||
@@ -462,6 +463,80 @@ describe('#SLP', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#validate2Single', () => {
|
||||||
|
it('should throw 400 if txid is empty', async () => {
|
||||||
|
req.params.txid = ''
|
||||||
|
const result = await slpRoute.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
|
assert.hasAllKeys(result, ['error'])
|
||||||
|
assert.include(result.error, 'txid can not be empty')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should invalidate a known invalid TXID', async () => {
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
// Mock to prevent live network connection.
|
||||||
|
sandbox
|
||||||
|
.stub(slpRoute.axios, 'request')
|
||||||
|
.resolves({ data: { isValid: false } })
|
||||||
|
}
|
||||||
|
|
||||||
|
const txid =
|
||||||
|
'f7e5199ef6669ad4d078093b3ad56e355b6ab84567e59ad0f08a5ad0244f783a'
|
||||||
|
|
||||||
|
req.params.txid = txid
|
||||||
|
const result = await slpRoute.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.equal(result.txid, txid)
|
||||||
|
assert.equal(result.isValid, false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should validate a known valid TXID', async () => {
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
// Mock to prevent live network connection.
|
||||||
|
sandbox
|
||||||
|
.stub(slpRoute.axios, 'request')
|
||||||
|
.resolves({ data: { isValid: true } })
|
||||||
|
}
|
||||||
|
|
||||||
|
const txid =
|
||||||
|
'3a4b628cbcc183ab376d44ce5252325f042268307ffa4a53443e92b6d24fb488'
|
||||||
|
|
||||||
|
req.params.txid = txid
|
||||||
|
const result = await slpRoute.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.equal(result.txid, txid)
|
||||||
|
assert.equal(result.isValid, true)
|
||||||
|
})
|
||||||
|
|
||||||
|
// This test can only be run as a mocked unit test. It's too inconsistent
|
||||||
|
// to run as an integration test, due to the caching built into slp-validate.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
it('should cancel if validation takes too long', async () => {
|
||||||
|
// Mock the timeout error.
|
||||||
|
sandbox.stub(slpRoute.axios, 'request').throws({
|
||||||
|
code: 'ECONNABORTED'
|
||||||
|
})
|
||||||
|
|
||||||
|
const txid =
|
||||||
|
'eacb1085dfa296fef6d4ae2c0f4529a1bef096dd2325bdcc6dcb5241b3bdb579'
|
||||||
|
|
||||||
|
req.params.txid = txid
|
||||||
|
const result = await slpRoute.validate2Single(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.isAbove(res.statusCode, 499, 'HTTP status code 503 expected.')
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'Could not communicate with full node',
|
||||||
|
'Error message expected'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
describe('tokenStats()', () => {
|
describe('tokenStats()', () => {
|
||||||
it('should throw 400 if tokenID is empty', async () => {
|
it('should throw 400 if tokenID is empty', async () => {
|
||||||
req.params.tokenId = ''
|
req.params.tokenId = ''
|
||||||
@@ -1034,6 +1109,34 @@ describe('#SLP', () => {
|
|||||||
assert.property(result.slpUtxos[0].utxos[1], 'tokenType')
|
assert.property(result.slpUtxos[0].utxos[1], 'tokenType')
|
||||||
assert.property(result.slpUtxos[0].utxos[1], 'tokenQty')
|
assert.property(result.slpUtxos[0].utxos[1], 'tokenQty')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should throw error for missing properties', async () => {
|
||||||
|
const utxos = [
|
||||||
|
{
|
||||||
|
height: 639443,
|
||||||
|
tx_hash:
|
||||||
|
'30707fffb9b295a06a68d217f49c198e9e1dbe1edc3874a0928ca1905f1709df',
|
||||||
|
tx_pos: 0,
|
||||||
|
value: 6000
|
||||||
|
},
|
||||||
|
{
|
||||||
|
height: 639443,
|
||||||
|
tx_hash:
|
||||||
|
'8962566e413501224d178a02effc89be5ac0d8e4195f617415d443dc4c38fe50',
|
||||||
|
tx_pos: 1,
|
||||||
|
value: 546
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
req.body.utxos = utxos
|
||||||
|
const result = await slpRoute.hydrateUtxos(req, res)
|
||||||
|
|
||||||
|
assert.hasAllKeys(result, ['error'])
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'Each element in array should have a utxos property'
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user