Skip to content

Commit e6b6c16

Browse files
author
Gabriel Cardona
committed
CashAccounts.check and CashAccounts.reverseLookup w/ unit and integration tests.
1 parent 7c4dafe commit e6b6c16

4 files changed

Lines changed: 181 additions & 2 deletions

File tree

lib/CashAccounts.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import axios, { AxiosResponse } from "axios"
2-
import { CashAccountLookupResult } from "bitcoin-com-rest"
2+
import {
3+
CashAccountCheckResult,
4+
CashAccountLookupResult,
5+
CashAccountReverseLookupResult
6+
} from "bitcoin-com-rest"
37
import { resturl } from "./BITBOX"
48

59
export class CashAccounts {
@@ -27,4 +31,33 @@ export class CashAccounts {
2731
else throw error
2832
}
2933
}
34+
35+
public async check(
36+
account: string,
37+
number: number
38+
): Promise<CashAccountCheckResult> {
39+
try {
40+
const response: AxiosResponse = await axios.get(
41+
`${this.restURL}cashAccounts/check/${account}/${number}`
42+
)
43+
return response.data
44+
} catch (error) {
45+
if (error.response && error.response.data) throw error.response.data
46+
else throw error
47+
}
48+
}
49+
50+
public async reverseLookup(
51+
cashAddress: string
52+
): Promise<CashAccountReverseLookupResult> {
53+
try {
54+
const response: AxiosResponse = await axios.get(
55+
`${this.restURL}cashAccounts/reverseLookup/${cashAddress}`
56+
)
57+
return response.data
58+
} catch (error) {
59+
if (error.response && error.response.data) throw error.response.data
60+
else throw error
61+
}
62+
}
3063
}

lib/interfaces/vendors.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,24 @@ declare module "bitcoin-com-rest" {
307307
payment: string[]
308308
}
309309
}
310+
311+
export interface CashAccountCheckResult {
312+
identifier: string
313+
block: number
314+
results: string[]
315+
}
316+
317+
export interface CashAccountReverseLookupResult {
318+
results: SingleCashAccountReverseLookupResult[]
319+
}
320+
321+
export interface SingleCashAccountReverseLookupResult {
322+
accountEmoji: any
323+
nameText: string
324+
accountNumber: number
325+
accountHash: string
326+
accountCollisionLength: number
327+
payloadType: number
328+
payloadAddress: string
329+
}
310330
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitbox-sdk",
3-
"version": "8.3.3",
3+
"version": "8.4.0",
44
"description": "BITBOX SDK for Bitcoin Cash",
55
"author": "Gabriel Cardona <gabriel@bitcoin.com>",
66
"contributors": [

test/unit/CashAccounts.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// imports
2+
import axios from "axios"
3+
import {
4+
CashAccountCheckResult,
5+
CashAccountLookupResult,
6+
CashAccountReverseLookupResult
7+
} from "bitcoin-com-rest"
8+
import * as chai from "chai"
9+
import * as util from "util"
10+
import { BITBOX, resturl } from "../../lib/BITBOX"
11+
import { CashAccounts } from "../../lib/CashAccounts"
12+
13+
// consts
14+
const bitbox: BITBOX = new BITBOX()
15+
const assert: Chai.AssertStatic = chai.assert
16+
17+
// TODO: port from require to import syntax
18+
const sinon = require("sinon")
19+
const cashAccountsMock = require("./fixtures/cashaccounts-mock.js")
20+
21+
util.inspect.defaultOptions = {
22+
showHidden: true,
23+
colors: true,
24+
depth: 3
25+
}
26+
27+
describe("#CashAccounts", (): void => {
28+
describe("#CashAccountsConstructor", (): void => {
29+
it("should create instance of CashAccounts", (): void => {
30+
const cashAccounts: CashAccounts = new CashAccounts()
31+
assert.equal(cashAccounts instanceof CashAccounts, true)
32+
})
33+
34+
it("should have a restURL property", (): void => {
35+
const cashAccounts: CashAccounts = new CashAccounts()
36+
assert.equal(cashAccounts.restURL, resturl)
37+
})
38+
})
39+
40+
describe("#lookup", () => {
41+
let sandbox: any
42+
beforeEach(() => (sandbox = sinon.sandbox.create()))
43+
afterEach(() => sandbox.restore())
44+
45+
it(`should lookup CashAccount details by account, number and collision`, async (): Promise<
46+
any
47+
> => {
48+
// Mock out data for unit test, to prevent live network call.
49+
const data: any = cashAccountsMock.lookup
50+
const resolved: any = new Promise(r => r({ data: data }))
51+
sandbox.stub(axios, "get").returns(resolved)
52+
53+
const account: string = "cgcardona"
54+
const number: number = 122
55+
const collision: number = 6383276713
56+
57+
const result = (await bitbox.CashAccounts.lookup(
58+
account,
59+
number,
60+
collision
61+
)) as CashAccountLookupResult
62+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
63+
64+
assert.hasAllKeys(result, ["identifier", "information"])
65+
})
66+
})
67+
68+
describe("#check", () => {
69+
let sandbox: any
70+
beforeEach(() => (sandbox = sinon.sandbox.create()))
71+
afterEach(() => sandbox.restore())
72+
73+
it(`should check CashAccount by account and number`, async (): Promise<
74+
any
75+
> => {
76+
// Mock out data for unit test, to prevent live network call.
77+
const data: any = cashAccountsMock.check
78+
const resolved: any = new Promise(r => r({ data: data }))
79+
sandbox.stub(axios, "get").returns(resolved)
80+
81+
const account: string = "cgcardona"
82+
const number: number = 122
83+
84+
const result = (await bitbox.CashAccounts.check(
85+
account,
86+
number
87+
)) as CashAccountCheckResult
88+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
89+
90+
assert.hasAllKeys(result, ["identifier", "block", "results"])
91+
})
92+
})
93+
94+
describe("#reverseLookup", () => {
95+
let sandbox: any
96+
beforeEach(() => (sandbox = sinon.sandbox.create()))
97+
afterEach(() => sandbox.restore())
98+
99+
it(`should reverse lookup CashAccount details by cash address`, async (): Promise<
100+
any
101+
> => {
102+
// Mock out data for unit test, to prevent live network call.
103+
const data: any = cashAccountsMock.reverseLookup
104+
const resolved: any = new Promise(r => r({ data: data }))
105+
sandbox.stub(axios, "get").returns(resolved)
106+
107+
const cashAddress: string =
108+
"bitcoincash:qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst"
109+
110+
const result = (await bitbox.CashAccounts.reverseLookup(
111+
cashAddress
112+
)) as CashAccountReverseLookupResult
113+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
114+
115+
assert.hasAllKeys(result.results[0], [
116+
"accountEmoji",
117+
"nameText",
118+
"accountNumber",
119+
"accountHash",
120+
"accountCollisionLength",
121+
"payloadType",
122+
"payloadAddress"
123+
])
124+
})
125+
})
126+
})

0 commit comments

Comments
 (0)