Skip to content

Commit 7f4ee4b

Browse files
Merge pull request #157 from Bitcoin-com/stage
New Release
2 parents 7e8f9bf + 894ab71 commit 7f4ee4b

11 files changed

Lines changed: 139 additions & 93 deletions

File tree

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"semi": false
3+
}

lib/Blockchain.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,7 @@ export class Blockchain {
153153

154154
try {
155155
const response: AxiosResponse = await axios.get(
156-
`${
157-
this.restURL
158-
}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`
156+
`${this.restURL}blockchain/getMempoolAncestors/${txid}?verbose=${verbose}`
159157
)
160158
return response.data
161159
} catch (error) {
@@ -173,9 +171,7 @@ export class Blockchain {
173171

174172
try {
175173
const response: AxiosResponse = await axios.get(
176-
`${
177-
this.restURL
178-
}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`
174+
`${this.restURL}blockchain/getMempoolDescendants/${txid}?verbose=${verbose}`
179175
)
180176
return response.data
181177
} catch (error) {
@@ -239,18 +235,27 @@ export class Blockchain {
239235
}
240236
}
241237

238+
// Returns details about an unspent transaction output.
242239
public async getTxOut(
243240
txid: string,
244241
n: any,
245242
include_mempool: boolean = true
246243
): Promise<TxOutResult | null> {
247-
// TODO confirm this works
244+
// Input validation
245+
if (typeof txid !== "string" || txid.length !== 64)
246+
throw new Error(`txid needs to be a proper transaction ID`)
247+
248+
if (isNaN(n)) throw new Error(`n must be an integer`)
249+
250+
if (typeof include_mempool !== "boolean")
251+
throw new Error(`include_mempool input must be of type boolean`)
252+
248253
try {
249-
const response: AxiosResponse = await axios.get(
250-
`${
251-
this.restURL
252-
}blockchain/getTxOut/${txid}/n?include_mempool=${include_mempool}`
253-
)
254+
const path: string = `${this.restURL}blockchain/getTxOut/${txid}/${n}?include_mempool=${include_mempool}`
255+
// console.log(`path: ${path}`)
256+
257+
const response: AxiosResponse = await axios.get(path)
258+
254259
return response.data
255260
} catch (error) {
256261
if (error.response && error.response.data) throw error.response.data
@@ -322,9 +327,7 @@ export class Blockchain {
322327
): Promise<boolean> {
323328
try {
324329
const response: AxiosResponse = await axios.get(
325-
`${
326-
this.restURL
327-
}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`
330+
`${this.restURL}blockchain/verifyChain?checklevel=${checklevel}&nblocks=${nblocks}`
328331
)
329332
return response.data
330333
} catch (error) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"@types/wif": "^2.0.1",
7070
"chai": "^4.1.2",
7171
"coveralls": "^3.0.2",
72-
"eslint": "^5.5.0",
72+
"eslint": "^5.16.0",
7373
"eslint-config-prettier": "^3.0.1",
7474
"eslint-plugin-node": "7.0.1",
7575
"eslint-plugin-prettier": "^2.6.2",
@@ -78,7 +78,7 @@
7878
"node-mocks-http": "^1.7.0",
7979
"nyc": "^14.1.1",
8080
"prettier": "^1.14.2",
81-
"semantic-release": "^15.13.3",
81+
"semantic-release": "^15.13.31",
8282
"sinon": "^4.5.0",
8383
"source-map-support": "^0.5.12",
8484
"ts-node": "^8.1.0",

test/integration/address.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ describe(`#address`, () => {
130130
"legacyAddress",
131131
"cashAddress",
132132
"scriptPubKey",
133-
"slpAddress"
133+
"slpAddress",
134+
"asm"
134135
])
135136
assert.isArray(result.utxos)
136137
assert.hasAnyKeys(result.utxos[0], [
@@ -158,7 +159,8 @@ describe(`#address`, () => {
158159
"legacyAddress",
159160
"cashAddress",
160161
"scriptPubKey",
161-
"slpAddress"
162+
"slpAddress",
163+
"asm"
162164
])
163165
assert.isArray(result[0].utxos)
164166
assert.hasAnyKeys(result[0].utxos[0], [
@@ -215,7 +217,8 @@ describe(`#address`, () => {
215217
"legacyAddress",
216218
"cashAddress",
217219
"scriptPubKey",
218-
"slpAddress"
220+
"slpAddress",
221+
"asm"
219222
])
220223
assert.isArray(result.utxos)
221224
})
@@ -235,7 +238,8 @@ describe(`#address`, () => {
235238
"legacyAddress",
236239
"cashAddress",
237240
"scriptPubKey",
238-
"slpAddress"
241+
"slpAddress",
242+
"asm"
239243
])
240244
assert.isArray(result[0].utxos)
241245
})

test/integration/blockchain.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,46 @@ describe(`#blockchain`, () => {
205205
})
206206
})
207207

208+
describe(`#getTxOut`, () => {
209+
it(`should get information on valid utxo`, async () => {
210+
const txid = `91874bf385a36d54f06c2154b34bce887a03b99540bfddaa17ac78ebc65202d0`
211+
212+
const result = await bitbox.Blockchain.getTxOut(txid, 0, true)
213+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
214+
215+
assert.hasAllKeys(result, [
216+
"bestblock",
217+
"confirmations",
218+
"value",
219+
"scriptPubKey",
220+
"coinbase"
221+
])
222+
assert.hasAllKeys(result.scriptPubKey, [
223+
"asm",
224+
"hex",
225+
"reqSigs",
226+
"type",
227+
"addresses"
228+
])
229+
})
230+
231+
it(`should return null for a spent utxo`, async () => {
232+
const txid = `8db6dd4f8a5bb1308541d4a6d4ecdae6c65426679e79f783638ce32b2fb0725b`
233+
234+
const result = await bitbox.Blockchain.getTxOut(txid, 0, true)
235+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
236+
237+
assert.equal(result, null)
238+
})
239+
})
240+
208241
describe(`#getTxOutProof`, () => {
209242
it(`should get single tx out proof`, async () => {
210243
const txid =
211244
"03f69502ca32e7927fd4f38c1d3f950bff650c1eea3d09a70e9df5a9d7f989f7"
212245

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

216249
assert.isString(result)
217250
})
@@ -223,7 +256,7 @@ describe(`#blockchain`, () => {
223256
]
224257

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

228261
assert.isArray(result)
229262
assert.isString(result[0])

test/integration/other/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Other Integration Tests
2+
3+
This directory holds stand-alone integration tests that should not be part of
4+
the integration test suite.
5+
6+
An example of a test that fits this criteria are rate limit tests. Rate limit
7+
tests are complex, require a solid internet connection, and can easily disrupt
8+
other tests. For those reasons, it is better to run them on their own.

test/integration/transaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe(`#Transaction`, () => {
8787
// console.log(`result: ${util.inspect(result)}`)
8888
assert.equal(false, false, "Unexpected result!")
8989
} catch (err) {
90-
console.log(`err: ${util.inspect(err)}`)
90+
// console.log(`err: ${util.inspect(err)}`)
9191

9292
assert.hasAnyKeys(err, ["error"])
9393
assert.include(err.error, "Array too large")

test/integration/util.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe(`#util`, () => {
4444
assert.equal(result.isvalid, false)
4545
})
4646

47-
it(`should return validate valid address`, async () => {
47+
it(`should return a valid address`, async () => {
4848
const address = `bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`
4949

5050
const result = await bitbox.Util.validateAddress(address)
@@ -54,8 +54,6 @@ describe(`#util`, () => {
5454
"isvalid",
5555
"address",
5656
"scriptPubKey",
57-
"ismine",
58-
"iswatchonly",
5957
"isscript"
6058
])
6159
assert.equal(result.isvalid, true)
@@ -75,8 +73,6 @@ describe(`#util`, () => {
7573
"isvalid",
7674
"address",
7775
"scriptPubKey",
78-
"ismine",
79-
"iswatchonly",
8076
"isscript"
8177
])
8278
})

test/integration/z9-rate-limits.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

test/unit/Blockchain.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { REST_URL } from "../../lib/BITBOX"
88
import * as util from "util"
99
import { BlockHeaderResult } from "bitcoin-com-rest"
1010

11+
const mockData = require("./mocks/blockchain-mock")
12+
1113
// consts
1214
const bitbox: BITBOX = new BITBOX()
1315
const assert: Chai.AssertStatic = chai.assert
@@ -347,27 +349,61 @@ describe("#Blockchain", (): void => {
347349
})
348350

349351
describe("#getTxOut", (): void => {
350-
// TODO finish this test
351352
let sandbox: any
352353
beforeEach(() => (sandbox = sinon.sandbox.create()))
353354
afterEach(() => sandbox.restore())
354355
const data = {
355356
result: {}
356357
}
357358

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

362-
bitbox.Blockchain.getTxOut(
363-
"daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88",
367+
it("should throw an error if vout is not an integer.", async () => {
368+
try {
369+
await bitbox.Blockchain.getTxOut(
370+
"daf58932cb91619304dd4cbd03c7202e89ad7d6cbd6e2209e5f64ce3b6ed7c88", 'a'
371+
)
372+
} catch (err) {
373+
assert.include(err.message, "n must be an integer")
374+
}
375+
})
376+
377+
it("should get information on an unspent tx", async () => {
378+
sandbox.stub(axios, "get").resolves({ data: mockData.txOutUnspent })
379+
380+
const result = await bitbox.Blockchain.getTxOut(
381+
"62a3ea958a463a372bc0caf2c374a7f60be9c624be63a0db8db78f05809df6d8",
364382
0,
365383
true
366384
)
367-
.then((result: any) => {
368-
assert.deepEqual(data, result)
369-
})
370-
.then(done, done)
385+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
386+
387+
assert.hasAllKeys(result, [
388+
"bestblock",
389+
"confirmations",
390+
"value",
391+
"scriptPubKey",
392+
"coinbase"
393+
])
394+
})
395+
396+
it("should get information on a spent tx", async () => {
397+
sandbox.stub(axios, "get").resolves({ data: null })
398+
399+
const result = await bitbox.Blockchain.getTxOut(
400+
"87380e52d151856b23173d6d8a3db01b984c6b50f77ea045a5a1cf4f54497871",
401+
0,
402+
true
403+
)
404+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
405+
406+
assert.equal(result, null)
371407
})
372408
})
373409

0 commit comments

Comments
 (0)