mirror of
https://github.com/fullstack-cash/bch-api.git
synced 2026-09-21 16:52:04 -07:00
Merge pull request #198 from Permissionless-Software-Foundation/ct-unstable
fix(electrumx.getBalance()): Allowing ecash addresses to pass through
This commit is contained in:
@@ -96,20 +96,32 @@ class Electrum {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the address is in cash address format.
|
if (!address) {
|
||||||
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)
|
res.status(400)
|
||||||
return res.json({
|
return res.json({
|
||||||
success: false,
|
success: false,
|
||||||
error:
|
error: 'address is empty'
|
||||||
'Invalid network. Trying to use a testnet address on mainnet, or vice versa.'
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let cashAddr = address
|
||||||
|
|
||||||
|
if (!address.includes('ecash')) {
|
||||||
|
// Ensure the address is in cash address format.
|
||||||
|
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.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wlogger.debug(
|
wlogger.debug(
|
||||||
'Executing electrumx/getBalance with this address: ',
|
'Executing electrumx/getBalance with this address: ',
|
||||||
cashAddr
|
cashAddr
|
||||||
|
|||||||
@@ -113,10 +113,10 @@ describe('#Electrumx', () => {
|
|||||||
const result = await electrumxRoute.getBalance(req, res)
|
const result = await electrumxRoute.getBalance(req, res)
|
||||||
// console.log(`result: ${util.inspect(result)}`)
|
// console.log(`result: ${util.inspect(result)}`)
|
||||||
|
|
||||||
assert.equal(res.statusCode, 422, 'Expect 422 status code')
|
assert.equal(res.statusCode, 400, 'Expect 400 status code')
|
||||||
|
|
||||||
assert.property(result, 'error')
|
assert.property(result, 'error')
|
||||||
assert.include(result.error, 'Unsupported address format')
|
assert.include(result.error, 'address is empty')
|
||||||
|
|
||||||
assert.property(result, 'success')
|
assert.property(result, 'success')
|
||||||
assert.equal(result.success, false)
|
assert.equal(result.success, false)
|
||||||
@@ -207,7 +207,33 @@ describe('#Electrumx', () => {
|
|||||||
assert.property(result.balance, 'confirmed')
|
assert.property(result.balance, 'confirmed')
|
||||||
assert.property(result.balance, 'unconfirmed')
|
assert.property(result.balance, 'unconfirmed')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should get balance for a single eCash address', async () => {
|
||||||
|
req.params.address =
|
||||||
|
'ecash:qr5c4hfy52zn87484cucvzle5pljz0gtr5vhtw9z09'
|
||||||
|
|
||||||
|
// Mock unit tests to prevent live network calls.
|
||||||
|
if (process.env.TEST === 'unit') {
|
||||||
|
electrumxRoute.isReady = true // Force flag.
|
||||||
|
|
||||||
|
sandbox
|
||||||
|
.stub(electrumxRoute.axios, 'get')
|
||||||
|
.resolves({ data: mockData.balance })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the details API.
|
||||||
|
const result = await electrumxRoute.getBalance(req, res)
|
||||||
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
||||||
|
|
||||||
|
assert.property(result, 'success')
|
||||||
|
assert.equal(result.success, true)
|
||||||
|
|
||||||
|
assert.property(result, 'balance')
|
||||||
|
assert.property(result.balance, 'confirmed')
|
||||||
|
assert.property(result.balance, 'unconfirmed')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#balanceBulk', () => {
|
describe('#balanceBulk', () => {
|
||||||
it('should throw 400 if addresses is empty', async () => {
|
it('should throw 400 if addresses is empty', async () => {
|
||||||
const result = await electrumxRoute.balanceBulk(req, res)
|
const result = await electrumxRoute.balanceBulk(req, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user