Skip to content

Commit

Permalink
Merge pull request #157 from Bitcoin-com/stage
Browse files Browse the repository at this point in the history
New Release
  • Loading branch information
christroutner authored Nov 19, 2019
2 parents 7e8f9bf + 894ab71 commit 7f4ee4b
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"semi": false
}
33 changes: 18 additions & 15 deletions lib/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ export class Blockchain {

try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`
`${this.restURL}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`
)
return response.data
} catch (error) {
Expand All @@ -173,9 +171,7 @@ export class Blockchain {

try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`
`${this.restURL}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`
)
return response.data
} catch (error) {
Expand Down Expand Up @@ -239,18 +235,27 @@ export class Blockchain {
}
}

// Returns details about an unspent transaction output.
public async getTxOut(
txid: string,
n: any,
include_mempool: boolean = true
): Promise<TxOutResult | null> {
// TODO confirm this works
// Input validation
if (typeof txid !== "string" || txid.length !== 64)
throw new Error(`txid needs to be a proper transaction ID`)

if (isNaN(n)) throw new Error(`n must be an integer`)

if (typeof include_mempool !== "boolean")
throw new Error(`include_mempool input must be of type boolean`)

try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
}blockchain/getTxOut/${txid}/n?include_mempool=${include_mempool}`
)
const path: string = `${this.restURL}blockchain/getTxOut/${txid}/${n}?include_mempool=${include_mempool}`
// console.log(`path: ${path}`)

const response: AxiosResponse = await axios.get(path)

return response.data
} catch (error) {
if (error.response && error.response.data) throw error.response.data
Expand Down Expand Up @@ -322,9 +327,7 @@ export class Blockchain {
): Promise<boolean> {
try {
const response: AxiosResponse = await axios.get(
`${
this.restURL
}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`
`${this.restURL}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`
)
return response.data
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/wif": "^2.0.1",
"chai": "^4.1.2",
"coveralls": "^3.0.2",
"eslint": "^5.5.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^3.0.1",
"eslint-plugin-node": "7.0.1",
"eslint-plugin-prettier": "^2.6.2",
Expand All @@ -78,7 +78,7 @@
"node-mocks-http": "^1.7.0",
"nyc": "^14.1.1",
"prettier": "^1.14.2",
"semantic-release": "^15.13.3",
"semantic-release": "^15.13.31",
"sinon": "^4.5.0",
"source-map-support": "^0.5.12",
"ts-node": "^8.1.0",
Expand Down
12 changes: 8 additions & 4 deletions test/integration/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ describe(`#address`, () => {
"legacyAddress",
"cashAddress",
"scriptPubKey",
"slpAddress"
"slpAddress",
"asm"
])
assert.isArray(result.utxos)
assert.hasAnyKeys(result.utxos[0], [
Expand Down Expand Up @@ -158,7 +159,8 @@ describe(`#address`, () => {
"legacyAddress",
"cashAddress",
"scriptPubKey",
"slpAddress"
"slpAddress",
"asm"
])
assert.isArray(result[0].utxos)
assert.hasAnyKeys(result[0].utxos[0], [
Expand Down Expand Up @@ -215,7 +217,8 @@ describe(`#address`, () => {
"legacyAddress",
"cashAddress",
"scriptPubKey",
"slpAddress"
"slpAddress",
"asm"
])
assert.isArray(result.utxos)
})
Expand All @@ -235,7 +238,8 @@ describe(`#address`, () => {
"legacyAddress",
"cashAddress",
"scriptPubKey",
"slpAddress"
"slpAddress",
"asm"
])
assert.isArray(result[0].utxos)
})
Expand Down
37 changes: 35 additions & 2 deletions test/integration/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,46 @@ describe(`#blockchain`, () => {
})
})

describe(`#getTxOut`, () => {
it(`should get information on valid utxo`, async () => {
const txid = `91874bf385a36d54f06c2154b34bce887a03b99540bfddaa17ac78ebc65202d0`

const result = await bitbox.Blockchain.getTxOut(txid, 0, true)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.hasAllKeys(result, [
"bestblock",
"confirmations",
"value",
"scriptPubKey",
"coinbase"
])
assert.hasAllKeys(result.scriptPubKey, [
"asm",
"hex",
"reqSigs",
"type",
"addresses"
])
})

it(`should return null for a spent utxo`, async () => {
const txid = `8db6dd4f8a5bb1308541d4a6d4ecdae6c65426679e79f783638ce32b2fb0725b`

const result = await bitbox.Blockchain.getTxOut(txid, 0, true)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.equal(result, null)
})
})

describe(`#getTxOutProof`, () => {
it(`should get single tx out proof`, async () => {
const txid =
"03f69502ca32e7927fd4f38c1d3f950bff650c1eea3d09a70e9df5a9d7f989f7"

const result = await bitbox.Blockchain.getTxOutProof(txid)
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.isString(result)
})
Expand All @@ -223,7 +256,7 @@ describe(`#blockchain`, () => {
]

const result = await bitbox.Blockchain.getTxOutProof(txid)
//console.log(`result: ${JSON.stringify(result, null, 2)}`)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.isArray(result)
assert.isString(result[0])
Expand Down
8 changes: 8 additions & 0 deletions test/integration/other/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Other Integration Tests

This directory holds stand-alone integration tests that should not be part of
the integration test suite.

An example of a test that fits this criteria are rate limit tests. Rate limit
tests are complex, require a solid internet connection, and can easily disrupt
other tests. For those reasons, it is better to run them on their own.
2 changes: 1 addition & 1 deletion test/integration/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe(`#Transaction`, () => {
// console.log(`result: ${util.inspect(result)}`)
assert.equal(false, false, "Unexpected result!")
} catch (err) {
console.log(`err: ${util.inspect(err)}`)
// console.log(`err: ${util.inspect(err)}`)

assert.hasAnyKeys(err, ["error"])
assert.include(err.error, "Array too large")
Expand Down
6 changes: 1 addition & 5 deletions test/integration/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe(`#util`, () => {
assert.equal(result.isvalid, false)
})

it(`should return validate valid address`, async () => {
it(`should return a valid address`, async () => {
const address = `bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`

const result = await bitbox.Util.validateAddress(address)
Expand All @@ -54,8 +54,6 @@ describe(`#util`, () => {
"isvalid",
"address",
"scriptPubKey",
"ismine",
"iswatchonly",
"isscript"
])
assert.equal(result.isvalid, true)
Expand All @@ -75,8 +73,6 @@ describe(`#util`, () => {
"isvalid",
"address",
"scriptPubKey",
"ismine",
"iswatchonly",
"isscript"
])
})
Expand Down
53 changes: 0 additions & 53 deletions test/integration/z9-rate-limits.js

This file was deleted.

56 changes: 46 additions & 10 deletions test/unit/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { REST_URL } from "../../lib/BITBOX"
import * as util from "util"
import { BlockHeaderResult } from "bitcoin-com-rest"

const mockData = require("./mocks/blockchain-mock")

// consts
const bitbox: BITBOX = new BITBOX()
const assert: Chai.AssertStatic = chai.assert
Expand Down Expand Up @@ -347,27 +349,61 @@ describe("#Blockchain", (): void => {
})

describe("#getTxOut", (): void => {
// TODO finish this test
let sandbox: any
beforeEach(() => (sandbox = sinon.sandbox.create()))
afterEach(() => sandbox.restore())
const data = {
result: {}
}

it("should get TODO", done => {
const resolved = new Promise(r => r({ data: data }))
sandbox.stub(axios, "get").returns(resolved)
it("should throw an error for improper txid.", async () => {
try {
await bitbox.Blockchain.getTxOut("badtxid", 0)
} catch (err) {
assert.include(err.message, "txid needs to be a proper transaction ID")
}
})

bitbox.Blockchain.getTxOut(
"daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88",
it("should throw an error if vout is not an integer.", async () => {
try {
await bitbox.Blockchain.getTxOut(
"daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88", 'a'
)
} catch (err) {
assert.include(err.message, "n must be an integer")
}
})

it("should get information on an unspent tx", async () => {
sandbox.stub(axios, "get").resolves({ data: mockData.txOutUnspent })

const result = await bitbox.Blockchain.getTxOut(
"62a3ea958a463a372bc0caf2c374a7f60be9c624be63a0db8db78f05809df6d8",
0,
true
)
.then((result: any) => {
assert.deepEqual(data, result)
})
.then(done, done)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.hasAllKeys(result, [
"bestblock",
"confirmations",
"value",
"scriptPubKey",
"coinbase"
])
})

it("should get information on a spent tx", async () => {
sandbox.stub(axios, "get").resolves({ data: null })

const result = await bitbox.Blockchain.getTxOut(
"87380e52d151856b23173d6d8a3db01b984c6b50f77ea045a5a1cf4f54497871",
0,
true
)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)

assert.equal(result, null)
})
})

Expand Down
18 changes: 17 additions & 1 deletion test/unit/mocks/blockchain-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ module.exports = {
"0000002086a4a3161f9ba2174883ec0b93acceac3b2f37b36ed1f90000000000000000009cb02406d1094ecf3e0b4c0ca7c585125e721147c39daf6b48c90b512741e13a12333e5cb38705180f441d8c7100000008fee9b60f1edb57e5712839186277ed39e0a004a32be9096ee47472efde8eae62f789f9d7a9f59d0ea7093dea1e0c65ff0b953f1d8cf3d47f92e732ca0295f603c272d5f4a63509f7a887f2549d78af7444aa0ecbb4f66d9cbe13bc6a89f59e05a199df8325d490818ffefe6b6321d32d7496a68580459836c0183f89082fc1b491cc91b23ecdcaa4c347bf599a62904d61f1c15b400ebbd5c90149010c139d9c1e31b774b796977393a238080ab477e1d240d0c4f155d36f519668f49bae6bd8cd5b8e40522edf76faa09cca6188d83ff13af6967cc6a569d1a5e9aeb1fdb7f531ddd2d0cbb81879741d5f38166ac1932136264366a4065cc96a42e41f96294f02df01",

verifiedProof:
"03f69502ca32e7927fd4f38c1d3f950bff650c1eea3d09a70e9df5a9d7f989f7"
"03f69502ca32e7927fd4f38c1d3f950bff650c1eea3d09a70e9df5a9d7f989f7",

txOutUnspent: {
bestblock:
"000000000000000000b441e02f5b1b9f5b3def961047afcc6f2f5636c952705e",
confirmations: 2,
value: 0.00006,
scriptPubKey: {
asm:
"OP_DUP OP_HASH160 d19fae66b685f5c3633c0db0600313918347225f OP_EQUALVERIFY OP_CHECKSIG",
hex: "76a914d19fae66b685f5c3633c0db0600313918347225f88ac",
reqSigs: 1,
type: "pubkeyhash",
addresses: ["bitcoincash:qrgeltnxk6zltsmr8sxmqcqrzwgcx3eztusrwgf0x3"]
},
coinbase: false
}
}

0 comments on commit 7f4ee4b

Please sign in to comment.