2020-07-27 21:32:51 -07:00
|
|
|
/*
|
|
|
|
|
Integration tests for the bchjs. Only covers calls made to
|
|
|
|
|
rest.bitcoin.com.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
const chai = require('chai')
|
2020-07-27 21:32:51 -07:00
|
|
|
const assert = chai.assert
|
|
|
|
|
|
|
|
|
|
const RESTURL = process.env.RESTURL
|
|
|
|
|
? process.env.RESTURL
|
2021-06-16 17:00:08 -07:00
|
|
|
: 'https://testnet3.fullstack.cash/v5/'
|
2020-07-27 21:32:51 -07:00
|
|
|
// if (process.env.RESTURL) RESTURL = process.env.RESTURL
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
const BCHJS = require('../../../../src/bch-js')
|
2021-06-16 17:00:08 -07:00
|
|
|
// const bchjs = new BCHJS({ restURL: `https://testnet.bchjs.cash/v5/` })
|
2020-07-27 21:32:51 -07:00
|
|
|
const bchjs = new BCHJS({ restURL: RESTURL, apiToken: process.env.BCHJSTOKEN })
|
|
|
|
|
|
|
|
|
|
// Inspect utility used for debugging.
|
2021-01-02 15:41:12 -08:00
|
|
|
const util = require('util')
|
2020-07-27 21:32:51 -07:00
|
|
|
util.inspect.defaultOptions = {
|
|
|
|
|
showHidden: true,
|
|
|
|
|
colors: true,
|
|
|
|
|
depth: 3
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#util', () => {
|
2020-11-02 14:49:28 -08:00
|
|
|
beforeEach(async () => {
|
|
|
|
|
if (process.env.IS_USING_FREE_TIER) await sleep(1000)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
describe('#validateAddress', () => {
|
|
|
|
|
it('should return false for testnet addr on mainnet', async () => {
|
|
|
|
|
const address = 'bitcoincash:qp4k8fjtgunhdr7yq30ha4peu'
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
const result = await bchjs.Util.validateAddress(address)
|
2021-01-02 15:41:12 -08:00
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
2020-07-27 21:32:51 -07:00
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.hasAllKeys(result, ['isvalid'])
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(result.isvalid, false)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should return false for bad address', async () => {
|
|
|
|
|
const address = 'bchtest:qqqk4y6lsl5da64sg53xezmplyu5kmpyz2ysaa5y'
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
const result = await bchjs.Util.validateAddress(address)
|
2021-01-02 15:41:12 -08:00
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
2020-07-27 21:32:51 -07:00
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.hasAllKeys(result, ['isvalid'])
|
2020-07-27 21:32:51 -07:00
|
|
|
assert.equal(result.isvalid, false)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should validate valid address', async () => {
|
|
|
|
|
const address = 'bchtest:qqqk4y6lsl5da64sg5qc3xezmplyu5kmpyz2ysaa5y'
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
const result = await bchjs.Util.validateAddress(address)
|
2021-01-02 15:41:12 -08:00
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
assert.hasAnyKeys(result, [
|
2021-01-02 15:41:12 -08:00
|
|
|
'isvalid',
|
|
|
|
|
'address',
|
|
|
|
|
'scriptPubKey',
|
|
|
|
|
// "ismine",
|
|
|
|
|
// "iswatchonly",
|
|
|
|
|
'isscript'
|
2020-07-27 21:32:51 -07:00
|
|
|
])
|
|
|
|
|
assert.equal(result.isvalid, true)
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should validate an array of addresses', async () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
const address = [
|
2021-01-02 15:41:12 -08:00
|
|
|
'bchtest:qqqk4y6lsl5da64sg5qc3xezmplyu5kmpyz2ysaa5y',
|
|
|
|
|
'bchtest:pq6k9969f6v6sg7a75jkru4n7wn9sknv5cztcp0dnh'
|
2020-07-27 21:32:51 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const result = await bchjs.Util.validateAddress(address)
|
2021-01-02 15:41:12 -08:00
|
|
|
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
2020-07-27 21:32:51 -07:00
|
|
|
|
|
|
|
|
assert.isArray(result)
|
|
|
|
|
assert.hasAnyKeys(result[0], [
|
2021-01-02 15:41:12 -08:00
|
|
|
'isvalid',
|
|
|
|
|
'address',
|
|
|
|
|
'scriptPubKey',
|
|
|
|
|
// "ismine",
|
|
|
|
|
// "iswatchonly",
|
|
|
|
|
'isscript'
|
2020-07-27 21:32:51 -07:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
it('should throw error on array size rate limit', async () => {
|
2020-07-27 21:32:51 -07:00
|
|
|
try {
|
2021-01-02 15:41:12 -08:00
|
|
|
const dataMock = 'bchtest:pq6k9969f6v6sg7a75jkru4n7wn9sknv5cztcp0dnh'
|
2020-07-27 21:32:51 -07:00
|
|
|
const data = []
|
|
|
|
|
for (let i = 0; i < 25; i++) data.push(dataMock)
|
|
|
|
|
|
|
|
|
|
const result = await bchjs.Util.validateAddress(data)
|
|
|
|
|
|
|
|
|
|
console.log(`result: ${util.inspect(result)}`)
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.equal(true, false, 'Unexpected result!')
|
2020-07-27 21:32:51 -07:00
|
|
|
} catch (err) {
|
2021-01-02 15:41:12 -08:00
|
|
|
assert.hasAnyKeys(err, ['error'])
|
|
|
|
|
assert.include(err.error, 'Array too large')
|
2020-07-27 21:32:51 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-11-02 14:49:28 -08:00
|
|
|
|
2021-01-02 15:41:12 -08:00
|
|
|
function sleep (ms) {
|
2020-11-02 14:49:28 -08:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms))
|
|
|
|
|
}
|