mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-22 09:12:05 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73cfb8c23a | ||
|
|
074610161f | ||
|
|
7c177f8013 | ||
|
|
3d6a8e30fd | ||
|
|
c8fe6f0bfd | ||
|
|
96b5e0e408 | ||
|
|
6ca014e160 | ||
|
|
233e210131 | ||
|
|
e55d421f13 |
@@ -28,6 +28,7 @@ export SLP_API_URL=http://10.0.0.5:5001/
|
|||||||
# Mainnet Fulcrum / ElectrumX
|
# Mainnet Fulcrum / ElectrumX
|
||||||
export FULCRUM_URL=172.17.0.1
|
export FULCRUM_URL=172.17.0.1
|
||||||
export FULCRUM_PORT=50002
|
export FULCRUM_PORT=50002
|
||||||
|
export FULCRUM_API=http://172.17.0.1:3001/v1/
|
||||||
|
|
||||||
# Redis DB - Used for rate limiting
|
# Redis DB - Used for rate limiting
|
||||||
export REDIS_PORT=6379
|
export REDIS_PORT=6379
|
||||||
@@ -48,6 +49,9 @@ export PRO_PASS=somerandomepassword:someotherrandompassword:aThirdPassword
|
|||||||
# that originate froma domain on the whitelist.
|
# that originate froma domain on the whitelist.
|
||||||
export WHITELIST_DOMAINS=fullstack.cash,psfoundation.cash,torlist.cash
|
export WHITELIST_DOMAINS=fullstack.cash,psfoundation.cash,torlist.cash
|
||||||
|
|
||||||
|
# Disable rate limits
|
||||||
|
export DO_NOT_USE_RATE_LIMITS=1
|
||||||
|
|
||||||
# Rate Limits. Numbers are divided into 1000. e.g. 1000 / 50 = 20 RPM for ANON.
|
# Rate Limits. Numbers are divided into 1000. e.g. 1000 / 50 = 20 RPM for ANON.
|
||||||
# Requests use the ANON rate limit if they fail to pass in a JWT token.
|
# Requests use the ANON rate limit if they fail to pass in a JWT token.
|
||||||
# ANON = 20 requests per minute (RPM)
|
# ANON = 20 requests per minute (RPM)
|
||||||
@@ -61,4 +65,6 @@ export LOG_MAX_SIZE=1m
|
|||||||
#5d means store no more than 5 days
|
#5d means store no more than 5 days
|
||||||
export LOG_MAX_FILES=5d
|
export LOG_MAX_FILES=5d
|
||||||
|
|
||||||
|
export BCASH_SERVER=http://localhost:3002/
|
||||||
|
|
||||||
npm start
|
npm start
|
||||||
|
|||||||
@@ -17,29 +17,32 @@ export RPC_PASSWORD=password
|
|||||||
|
|
||||||
# SLPDB
|
# SLPDB
|
||||||
export SLPDB_URL=http://172.17.0.1:13300/
|
export SLPDB_URL=http://172.17.0.1:13300/
|
||||||
export SLPDB_PASS=somelongpassword
|
export SLPDB_PASS=portlandisacityinoregon
|
||||||
|
export SLPDB_PASS_GP=portlandisacityinoregon
|
||||||
|
export SLPDB_PASS_WL=portlandisacityinoregon
|
||||||
# Use the same address as SLPDB_URL if you don't have a separate whitelist server.
|
# Use the same address as SLPDB_URL if you don't have a separate whitelist server.
|
||||||
export SLPDB_WHITELIST_URL=http://172.17.0.1:13300/
|
export SLPDB_WHITELIST_URL=http://172.17.0.1:13300/
|
||||||
# slp-api alternative SLP validator using slp-validate:
|
# slp-api alternative SLP validator using slp-validate:
|
||||||
# https://github.com/Permissionless-Software-Foundation/slp-api
|
# https://github.com/Permissionless-Software-Foundation/slp-api
|
||||||
export SLP_API_URL=http://10.0.0.5:5001/
|
export SLP_API_URL=https://slpapi-testnet3.fullstackslp.nl/
|
||||||
|
|
||||||
# Mainnet Fulcrum / ElectrumX
|
# Mainnet Fulcrum / ElectrumX
|
||||||
export FULCRUM_URL=172.17.0.1
|
export FULCRUM_URL=172.17.0.1
|
||||||
export FULCRUM_PORT=60002
|
export FULCRUM_PORT=60002
|
||||||
|
export FULCRUM_API=http://172.17.0.1:3000/v1/
|
||||||
|
|
||||||
# Redis DB
|
# Redis DB
|
||||||
export REDIS_PORT=6380
|
export REDIS_PORT=6380
|
||||||
export REDIS_HOST=172.17.0.1
|
export REDIS_HOST=172.17.0.1
|
||||||
|
|
||||||
# JWT Token Secret
|
# JWT Token Secret
|
||||||
export TOKENSECRET=somelongsecretvalue
|
export TOKENSECRET=fridayharbor
|
||||||
|
|
||||||
# So that bch-api can call bch-js locally.
|
# So that bch-api can call bch-js locally.
|
||||||
export LOCAL_RESTURL=http://127.0.0.1:3000/v4/
|
export LOCAL_RESTURL=http://127.0.0.1:3000/v4/
|
||||||
|
|
||||||
# Basic Authentication password (optional)
|
# Basic Authentication password (optional)
|
||||||
export PRO_PASS=somerandomepassword:someotherrandompassword:aThirdPassword
|
export PRO_PASS=thejaasjhehrakseuhakuhr
|
||||||
|
|
||||||
# Whitelisted domains. Automatically give pro-tier rate limit access to apps
|
# Whitelisted domains. Automatically give pro-tier rate limit access to apps
|
||||||
# that originate froma domain on the whitelist.
|
# that originate froma domain on the whitelist.
|
||||||
@@ -50,6 +53,9 @@ export WHITELIST_DOMAINS=fullstack.cash,psfoundation.cash,torlist.cash
|
|||||||
# ANON = 20 requests per minute (RPM)
|
# ANON = 20 requests per minute (RPM)
|
||||||
export ANON_RATE_LIMIT=50
|
export ANON_RATE_LIMIT=50
|
||||||
# 10 = 100 RPM
|
# 10 = 100 RPM
|
||||||
export WHITELIST_RATE_LIMIT=10
|
export WHITELIST_RATE_LIMIT=1
|
||||||
|
|
||||||
|
export INTERNAL_RATE_LIMIT=100
|
||||||
|
|
||||||
npm start
|
npm start
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ const ElectrumXV5 = require('./routes/v5/electrumx')
|
|||||||
const EncryptionV5 = require('./routes/v5/encryption')
|
const EncryptionV5 = require('./routes/v5/encryption')
|
||||||
const PriceV5 = require('./routes/v5/price')
|
const PriceV5 = require('./routes/v5/price')
|
||||||
const JWTV5 = require('./routes/v5/jwt')
|
const JWTV5 = require('./routes/v5/jwt')
|
||||||
|
const BcashSLP = require('./routes/v5/bcash/slp')
|
||||||
// const Ninsight = require('./routes/v5/ninsight')
|
// const Ninsight = require('./routes/v5/ninsight')
|
||||||
|
|
||||||
require('dotenv').config()
|
require('dotenv').config()
|
||||||
@@ -82,6 +83,7 @@ const pricev5 = new PriceV5()
|
|||||||
const utilV5 = new UtilV5({ electrumx: electrumxv5 })
|
const utilV5 = new UtilV5({ electrumx: electrumxv5 })
|
||||||
const dsproofV5 = new DSProofV5()
|
const dsproofV5 = new DSProofV5()
|
||||||
const jwtV5 = new JWTV5()
|
const jwtV5 = new JWTV5()
|
||||||
|
const bcashSLP = new BcashSLP()
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.locals.env = process.env
|
app.locals.env = process.env
|
||||||
@@ -196,6 +198,8 @@ app.use(`/${v5prefix}/` + 'jwt', jwtV5.router)
|
|||||||
// const ninsight = new Ninsight()
|
// const ninsight = new Ninsight()
|
||||||
app.use(`/${v5prefix}/` + 'ninsight', ninsight.router)
|
app.use(`/${v5prefix}/` + 'ninsight', ninsight.router)
|
||||||
|
|
||||||
|
app.use(`/${v5prefix}/` + 'bcash/slp', bcashSLP.router)
|
||||||
|
|
||||||
// catch 404 and forward to error handler
|
// catch 404 and forward to error handler
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
const err = {
|
const err = {
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
/*
|
||||||
|
Electrum API route
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
const axios = require('axios')
|
||||||
|
const util = require('util')
|
||||||
|
// const bitcore = require('bitcore-lib-cash')
|
||||||
|
|
||||||
|
// const ElectrumCash = require('electrum-cash').ElectrumClient
|
||||||
|
// const ElectrumCash = require('/home/trout/work/personal/electrum-cash/electrum.js').Client // eslint-disable-line
|
||||||
|
|
||||||
|
const wlogger = require('../../../util/winston-logging')
|
||||||
|
const config = require('../../../../config')
|
||||||
|
|
||||||
|
const RouteUtils = require('../../../util/route-utils')
|
||||||
|
const routeUtils = new RouteUtils()
|
||||||
|
|
||||||
|
const BCHJS = require('@psf/bch-js')
|
||||||
|
const bchjs = new BCHJS()
|
||||||
|
|
||||||
|
let _this
|
||||||
|
|
||||||
|
class BcashSlp {
|
||||||
|
constructor () {
|
||||||
|
_this = this
|
||||||
|
this.config = config
|
||||||
|
this.axios = axios
|
||||||
|
this.routeUtils = routeUtils
|
||||||
|
this.bchjs = bchjs
|
||||||
|
// _this.bitcore = bitcore
|
||||||
|
|
||||||
|
this.bcashServer = process.env.BCASH_SERVER
|
||||||
|
if (!this.bcashServer) {
|
||||||
|
// console.warn('FULCRUM_API env var not set. Can not connect to Fulcrum indexer.')
|
||||||
|
throw new Error(
|
||||||
|
'BCASH_SERVER env var not set. Can not connect to bcash full node.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.router = router
|
||||||
|
this.router.get('/', this.root)
|
||||||
|
this.router.get('/utxos/:address', this.getUtxos)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @api {get} /bcash/utxos/{addr} Get utxos for a single address.
|
||||||
|
* @apiName UTXOs for a single address
|
||||||
|
* @apiGroup bcash
|
||||||
|
* @apiDescription Returns an object with UTXOs associated with an address.
|
||||||
|
* This UTXOs will be hydrated with token information.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @apiExample Example usage:
|
||||||
|
* curl -X GET "https://api.fullstack.cash/v5/bcash/utxos/bitcoincash:qr69kyzha07dcecrsvjwsj4s6slnlq4r8c30lxnur3" -H "accept: application/json"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
// GET handler for single balance
|
||||||
|
async getUtxos (req, res, next) {
|
||||||
|
try {
|
||||||
|
const address = req.params.address
|
||||||
|
|
||||||
|
// Reject if address is an array.
|
||||||
|
if (Array.isArray(address)) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
error: 'address can not be an array. Use POST for bulk upload.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const cashAddr = _this.bchjs.Address.toCashAddress(address)
|
||||||
|
|
||||||
|
// Prevent a common user error. Ensure they are using the correct network address.
|
||||||
|
const networkIsValid = _this.routeUtils.validateNetwork(cashAddr)
|
||||||
|
if (!networkIsValid) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({
|
||||||
|
success: false,
|
||||||
|
error:
|
||||||
|
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Customize this function below here for bcash, based on this gist:
|
||||||
|
// https://gist.github.com/christroutner/dcb25a895900e557d7b43341ee85fa79
|
||||||
|
|
||||||
|
wlogger.debug(
|
||||||
|
'Executing bcash/slp.js/getUtxos(). with this address: ',
|
||||||
|
cashAddr
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get data from ElectrumX server.
|
||||||
|
const response = await _this.axios.get(
|
||||||
|
`${_this.bcashServer}coin/address/${cashAddr}?slp=true`
|
||||||
|
)
|
||||||
|
// Get address UTXOs
|
||||||
|
const utxos = response.data
|
||||||
|
// Hydrate UTXOs
|
||||||
|
const hydratedUtxos = await _this.hydrateUTXOS(utxos)
|
||||||
|
res.status(200)
|
||||||
|
return res.json(hydratedUtxos)
|
||||||
|
} catch (err) {
|
||||||
|
// Write out error to error log.
|
||||||
|
wlogger.error('Error in bcash/slp.js/getUtxos().', err)
|
||||||
|
|
||||||
|
return _this.errorHandler(err, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maps and filter the SLP UTXOs of an UTXOs array
|
||||||
|
// get information about the SLP UTXOs
|
||||||
|
async hydrateUTXOS (UTXOS) {
|
||||||
|
try {
|
||||||
|
if (!Array.isArray(UTXOS)) {
|
||||||
|
throw new Error('UTXOs must be an array of slp utxos')
|
||||||
|
}
|
||||||
|
const slpUtxos = UTXOS.filter((val) => val.slp)
|
||||||
|
const hydrated = []
|
||||||
|
|
||||||
|
// Find information of each token
|
||||||
|
for (let i = 0; i < slpUtxos.length; i++) {
|
||||||
|
const slp = slpUtxos[i].slp
|
||||||
|
const info = await _this.getTokenInfo(slp.tokenId)
|
||||||
|
const obj = Object.assign(slp, info)
|
||||||
|
|
||||||
|
slpUtxos[i].slp = obj
|
||||||
|
hydrated.push(slpUtxos[i])
|
||||||
|
}
|
||||||
|
return hydrated
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get information of a token
|
||||||
|
async getTokenInfo (tokenId) {
|
||||||
|
try {
|
||||||
|
if (!tokenId || typeof tokenId !== 'string') {
|
||||||
|
throw new Error('tokenId must be string')
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await _this.axios.get(
|
||||||
|
`${_this.bcashServer}token/${tokenId}`
|
||||||
|
)
|
||||||
|
const tokenInfo = result.data
|
||||||
|
// console.log('token info', tokenInfo)
|
||||||
|
return tokenInfo
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DRY error handler.
|
||||||
|
errorHandler (err, res) {
|
||||||
|
// Attempt to decode the error message.
|
||||||
|
const { msg, status } = _this.routeUtils.decodeError(err)
|
||||||
|
// console.log('errorHandler msg: ', msg)
|
||||||
|
// console.log('errorHandler status: ', status)
|
||||||
|
|
||||||
|
if (msg) {
|
||||||
|
res.status(status)
|
||||||
|
return res.json({ success: false, error: msg })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle error patterns specific to this route.
|
||||||
|
if (err.message) {
|
||||||
|
res.status(400)
|
||||||
|
return res.json({ success: false, error: err.message })
|
||||||
|
}
|
||||||
|
|
||||||
|
// If error can be handled, return the stack trace
|
||||||
|
res.status(500)
|
||||||
|
return res.json({ error: util.inspect(err) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root API endpoint. Simply acknowledges that it exists.
|
||||||
|
root (req, res, next) {
|
||||||
|
return res.json({ status: 'bcash-slp' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = BcashSlp
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -33,6 +33,8 @@ export SLP_API_URL=http://10.0.0.5:5001/
|
|||||||
# Mainnet Fulcrum / ElectrumX
|
# Mainnet Fulcrum / ElectrumX
|
||||||
export FULCRUM_URL=192.168.0.6
|
export FULCRUM_URL=192.168.0.6
|
||||||
export FULCRUM_PORT=50002
|
export FULCRUM_PORT=50002
|
||||||
|
# Fulcrum API (used for /v5+ routes)
|
||||||
|
export FULCRUM_API=http://172.17.0.1:3001/v1/
|
||||||
|
|
||||||
# Redis DB - Used for rate limiting - customize to your own Redis installation.
|
# Redis DB - Used for rate limiting - customize to your own Redis installation.
|
||||||
export REDIS_PORT=6379
|
export REDIS_PORT=6379
|
||||||
@@ -53,6 +55,8 @@ export PRO_PASS=somerandomepassword:someotherrandompassword:aThirdPassword
|
|||||||
# that originate froma domain on the whitelist.
|
# that originate froma domain on the whitelist.
|
||||||
export WHITELIST_DOMAINS=fullstack.cash,psfoundation.cash,torlist.cash
|
export WHITELIST_DOMAINS=fullstack.cash,psfoundation.cash,torlist.cash
|
||||||
|
|
||||||
|
# Uncomment the line below if you do not want to use rate limits
|
||||||
|
#export export DO_NOT_USE_RATE_LIMITS=1
|
||||||
# Rate Limits. Numbers are divided into 1000. e.g. 10000 / 500 = 20 RPM for ANON.
|
# Rate Limits. Numbers are divided into 1000. e.g. 10000 / 500 = 20 RPM for ANON.
|
||||||
# Requests use the ANON rate limit if they fail to pass in a JWT token.
|
# Requests use the ANON rate limit if they fail to pass in a JWT token.
|
||||||
# ANON = 20 requests per minute (RPM)
|
# ANON = 20 requests per minute (RPM)
|
||||||
@@ -66,4 +70,7 @@ export LOG_MAX_SIZE=1m
|
|||||||
#5d means store no more than 5 days
|
#5d means store no more than 5 days
|
||||||
export LOG_MAX_FILES=5d
|
export LOG_MAX_FILES=5d
|
||||||
|
|
||||||
|
# (Optional) if using a bcash node, set this variable. Otherwise leave it as-is.
|
||||||
|
export BCASH_SERVER=http://localhost
|
||||||
|
|
||||||
npm start
|
npm start
|
||||||
|
|||||||
@@ -0,0 +1,244 @@
|
|||||||
|
/*
|
||||||
|
TESTS FOR THE bcash/slp.js LIBRARY
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const chai = require('chai')
|
||||||
|
const assert = chai.assert
|
||||||
|
|
||||||
|
const sinon = require('sinon')
|
||||||
|
|
||||||
|
const BcashSlp = require('../../src/routes/v5/bcash/slp')
|
||||||
|
|
||||||
|
// Mocking data.
|
||||||
|
const { mockReq, mockRes } = require('./mocks/express-mocks')
|
||||||
|
const mockData = require('./mocks/bcash-slp-mock')
|
||||||
|
|
||||||
|
// Used for debugging.
|
||||||
|
const util = require('util')
|
||||||
|
util.inspect.defaultOptions = { depth: 1 }
|
||||||
|
|
||||||
|
if (!process.env.BCASH_SERVER) process.env.BCASH_SERVER = 'http://localhost'
|
||||||
|
|
||||||
|
describe('#bcash-slp', () => {
|
||||||
|
let req, res
|
||||||
|
let sandbox
|
||||||
|
const bcashSlp = new BcashSlp()
|
||||||
|
// let electrumxRoute
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
if (!process.env.TEST) {
|
||||||
|
process.env.TEST = 'unit'
|
||||||
|
}
|
||||||
|
console.log(`Testing type is: ${process.env.TEST}`)
|
||||||
|
|
||||||
|
if (!process.env.NETWORK) process.env.NETWORK = 'testnet'
|
||||||
|
|
||||||
|
// Connect to electrumx servers if this is an integration test.
|
||||||
|
// if (process.env.TEST === 'integration') {
|
||||||
|
// await electrumxRoute.connect()
|
||||||
|
// console.log('Connected to ElectrumX server')
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
|
||||||
|
after(async () => {
|
||||||
|
// console.log(`electrumxRoute.electrumx: `, electrumxRoute.electrumx)
|
||||||
|
// Disconnect from the electrumx server if this is an integration test.
|
||||||
|
// if (process.env.TEST === 'integration') {
|
||||||
|
// await electrumxRoute.disconnect()
|
||||||
|
// console.log('Disconnected from ElectrumX server')
|
||||||
|
// }
|
||||||
|
})
|
||||||
|
|
||||||
|
// Setup the mocks before each test.
|
||||||
|
beforeEach(() => {
|
||||||
|
// Mock the req and res objects used by Express routes.
|
||||||
|
req = mockReq
|
||||||
|
res = mockRes
|
||||||
|
|
||||||
|
// Explicitly reset the parmas and body.
|
||||||
|
req.params = {}
|
||||||
|
req.body = {}
|
||||||
|
req.query = {}
|
||||||
|
|
||||||
|
sandbox = sinon.createSandbox()
|
||||||
|
|
||||||
|
// electrumxRoute = new ElecrumxRoute()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
sandbox.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
after(() => {
|
||||||
|
//
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#root', () => {
|
||||||
|
// root route handler.
|
||||||
|
const root = bcashSlp.root
|
||||||
|
|
||||||
|
it('should respond to GET for base route', async () => {
|
||||||
|
const result = root(req, res)
|
||||||
|
|
||||||
|
assert.equal(result.status, 'bcash-slp', 'Returns static string')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#getUtxos', () => {
|
||||||
|
it('should return UTXOs for an address, hydrated with SLP info', async () => {
|
||||||
|
sandbox.stub(bcashSlp.axios, 'get').resolves({ data: mockData.utxos })
|
||||||
|
sandbox.stub(bcashSlp, 'getTokenInfo').resolves(mockData.tokenInfo)
|
||||||
|
req.params.address =
|
||||||
|
'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk'
|
||||||
|
|
||||||
|
const utxos = await bcashSlp.getUtxos(req, res)
|
||||||
|
assert.isArray(utxos)
|
||||||
|
const utxo = utxos[0]
|
||||||
|
|
||||||
|
assert.property(utxo, 'version')
|
||||||
|
assert.property(utxo, 'height')
|
||||||
|
assert.property(utxo, 'value')
|
||||||
|
assert.property(utxo, 'script')
|
||||||
|
assert.property(utxo, 'address')
|
||||||
|
assert.property(utxo, 'coinbase')
|
||||||
|
assert.property(utxo, 'hash')
|
||||||
|
assert.property(utxo, 'index')
|
||||||
|
assert.property(utxo, 'slp')
|
||||||
|
|
||||||
|
assert.property(utxo.slp, 'tokenId')
|
||||||
|
assert.property(utxo.slp, 'ticker')
|
||||||
|
assert.property(utxo.slp, 'name')
|
||||||
|
assert.property(utxo.slp, 'uri')
|
||||||
|
assert.property(utxo.slp, 'hash')
|
||||||
|
assert.property(utxo.slp, 'decimals')
|
||||||
|
assert.property(utxo.slp, 'vout')
|
||||||
|
assert.property(utxo.slp, 'value')
|
||||||
|
assert.property(utxo.slp, 'type')
|
||||||
|
})
|
||||||
|
it('should handle error if address is array', async () => {
|
||||||
|
req.params.address = [
|
||||||
|
'bchtest:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk'
|
||||||
|
]
|
||||||
|
|
||||||
|
const result = await bcashSlp.getUtxos(req, res)
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'address can not be an array. Use POST for bulk upload.'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
it('should throw error if address has invalid format', async () => {
|
||||||
|
sandbox.stub(bcashSlp.routeUtils, 'validateNetwork').returns(false)
|
||||||
|
|
||||||
|
req.params.address = 'qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk'
|
||||||
|
|
||||||
|
const result = await bcashSlp.getUtxos(req, res)
|
||||||
|
assert.include(
|
||||||
|
result.error,
|
||||||
|
'Invalid network. Trying to use a testnet address on mainnet'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
it('should catch axios error', async () => {
|
||||||
|
sandbox.stub(bcashSlp.axios, 'get').throws(new Error('test error'))
|
||||||
|
req.params.address =
|
||||||
|
'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk'
|
||||||
|
const result = await bcashSlp.getUtxos(req, res)
|
||||||
|
assert.include(result.error, 'test error')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#hydrateUTXOS', () => {
|
||||||
|
it('should hydrate slp utxos', async () => {
|
||||||
|
sandbox.stub(bcashSlp, 'getTokenInfo').resolves(mockData.tokenInfo)
|
||||||
|
|
||||||
|
const hydratedUtxos = await bcashSlp.hydrateUTXOS(mockData.utxos)
|
||||||
|
assert.isArray(hydratedUtxos)
|
||||||
|
const hydratedUtxo = hydratedUtxos[0]
|
||||||
|
assert.property(hydratedUtxo, 'version')
|
||||||
|
assert.property(hydratedUtxo, 'height')
|
||||||
|
assert.property(hydratedUtxo, 'value')
|
||||||
|
assert.property(hydratedUtxo, 'script')
|
||||||
|
assert.property(hydratedUtxo, 'address')
|
||||||
|
assert.property(hydratedUtxo, 'coinbase')
|
||||||
|
assert.property(hydratedUtxo, 'hash')
|
||||||
|
assert.property(hydratedUtxo, 'index')
|
||||||
|
assert.property(hydratedUtxo, 'slp')
|
||||||
|
|
||||||
|
assert.property(hydratedUtxo.slp, 'tokenId')
|
||||||
|
assert.property(hydratedUtxo.slp, 'ticker')
|
||||||
|
assert.property(hydratedUtxo.slp, 'name')
|
||||||
|
assert.property(hydratedUtxo.slp, 'uri')
|
||||||
|
assert.property(hydratedUtxo.slp, 'hash')
|
||||||
|
assert.property(hydratedUtxo.slp, 'decimals')
|
||||||
|
assert.property(hydratedUtxo.slp, 'vout')
|
||||||
|
assert.property(hydratedUtxo.slp, 'value')
|
||||||
|
assert.property(hydratedUtxo.slp, 'type')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should throw error if utxos is not provided', async () => {
|
||||||
|
try {
|
||||||
|
await bcashSlp.hydrateUTXOS()
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
assert.include(error.message, 'UTXOs must be an array of slp utxos')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
it('should handle error', async () => {
|
||||||
|
try {
|
||||||
|
sandbox.stub(bcashSlp, 'getTokenInfo').throws(new Error('test error'))
|
||||||
|
|
||||||
|
await bcashSlp.getTokenInfo(mockData.utxos)
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
assert.include(error.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#getTokenInfo', () => {
|
||||||
|
it('should return SLP info for an token id', async () => {
|
||||||
|
sandbox.stub(bcashSlp.axios, 'get').resolves({ data: mockData.tokenInfo })
|
||||||
|
|
||||||
|
const tokenId =
|
||||||
|
'afd88e9afab110e7b75410417edb5c98798c08aa892cee8d97b44f2e5545a900'
|
||||||
|
const slpInfo = await bcashSlp.getTokenInfo(tokenId)
|
||||||
|
assert.isObject(slpInfo)
|
||||||
|
assert.property(slpInfo, 'tokenId')
|
||||||
|
assert.property(slpInfo, 'ticker')
|
||||||
|
assert.property(slpInfo, 'name')
|
||||||
|
assert.property(slpInfo, 'uri')
|
||||||
|
assert.property(slpInfo, 'hash')
|
||||||
|
assert.property(slpInfo, 'decimals')
|
||||||
|
})
|
||||||
|
it('should throw error if token id is not provided', async () => {
|
||||||
|
try {
|
||||||
|
await bcashSlp.getTokenInfo()
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
assert.include(error.message, 'tokenId must be string')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
it('should catch axios error', async () => {
|
||||||
|
try {
|
||||||
|
sandbox.stub(bcashSlp.axios, 'get').throws(new Error('test error'))
|
||||||
|
const tokenId =
|
||||||
|
'afd88e9afab110e7b75410417edb5c98798c08aa892cee8d97b44f2e5545a900'
|
||||||
|
await bcashSlp.getTokenInfo(tokenId)
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
assert.include(error.message, 'test error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#errorHandler', () => {
|
||||||
|
it('should handle unexpected errors', () => {
|
||||||
|
sandbox.stub(bcashSlp.routeUtils, 'decodeError').returns({ msg: false })
|
||||||
|
|
||||||
|
const result = bcashSlp.errorHandler(new Error('test error'), res)
|
||||||
|
// console.log('result: ', result)
|
||||||
|
assert.equal(res.statusCode, 400, 'HTTP status code 400 expected.')
|
||||||
|
assert.property(result, 'error')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
This library contains mocking data for running unit tests on the bcash route.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const tokenInfo = {
|
||||||
|
tokenId: 'c7cb019764df3a352d9433749330b4b2eb022d8fbc101e68a6943a7a58a8ee84',
|
||||||
|
ticker: 'PSI',
|
||||||
|
name: 'Psidium',
|
||||||
|
uri: '',
|
||||||
|
hash: '',
|
||||||
|
decimals: 8
|
||||||
|
}
|
||||||
|
|
||||||
|
const utxos = [
|
||||||
|
{
|
||||||
|
version: 2,
|
||||||
|
height: 707135,
|
||||||
|
value: 546,
|
||||||
|
script: '76a91466b2156f71629c89f5bf882cb3920b0e1e4d4fa888ac',
|
||||||
|
address: 'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk',
|
||||||
|
coinbase: false,
|
||||||
|
hash: '77e514d59c0c866e791d08ddab73a1f7827be5f1ca15c1e05515dac9b271b596',
|
||||||
|
index: 2,
|
||||||
|
slp: {
|
||||||
|
vout: 2,
|
||||||
|
tokenId:
|
||||||
|
'5f2914840d0fd82689bdf0a0f8194ea363b9d0e2bd69b01608e18e1c6a4b9861',
|
||||||
|
value: '9999777700000',
|
||||||
|
type: 'SEND'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: 2,
|
||||||
|
height: 707135,
|
||||||
|
value: 546,
|
||||||
|
script: '76a91466b2156f71629c89f5bf882cb3920b0e1e4d4fa888ac',
|
||||||
|
address: 'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk',
|
||||||
|
coinbase: false,
|
||||||
|
hash: '350aab06bf9523c062692f62622cb28b18cff9e4aa92be5476143336da217e98',
|
||||||
|
index: 1,
|
||||||
|
slp: {
|
||||||
|
vout: 1,
|
||||||
|
tokenId:
|
||||||
|
'2d860662801067828be3c4f504ce31b2a1a36c2cdbc6d92f6160920f66b0a9ee',
|
||||||
|
value: '1',
|
||||||
|
type: 'SEND'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: 2,
|
||||||
|
height: 687335,
|
||||||
|
value: 546,
|
||||||
|
script: '76a91466b2156f71629c89f5bf882cb3920b0e1e4d4fa888ac',
|
||||||
|
address: 'bitcoincash:qpnty9t0w93fez04h7yzevujpv8pun204qv6yfuahk',
|
||||||
|
coinbase: false,
|
||||||
|
hash: 'b7a0f52a344b1169292c757a36c62dbdaaa4ad684e96837eec732a9a2033dfd1',
|
||||||
|
index: 1,
|
||||||
|
slp: {
|
||||||
|
vout: 1,
|
||||||
|
tokenId:
|
||||||
|
'c7cb019764df3a352d9433749330b4b2eb022d8fbc101e68a6943a7a58a8ee84',
|
||||||
|
value: '100000000',
|
||||||
|
type: 'SEND'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
tokenInfo,
|
||||||
|
utxos
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user