Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 25fccd8

Browse files
committed
Update tests
1 parent 967e4ec commit 25fccd8

5 files changed

Lines changed: 47 additions & 111 deletions

File tree

src/lib/ElectrumClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ export default class Client {
453453
* @param {string} script ScriptPubKey in a hexadecimal format.
454454
* @return {string} Script hash as a hex string.
455455
*/
456-
function scriptToHash(script) {
456+
export function scriptToHash(script) {
457457
/** @type {Buffer} */
458458
const scriptHash = digest(Buffer.from(script, "hex")).reverse()
459459
return scriptHash.toString("hex")

test/AddressTest.js

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

test/BitcoinSPVTest.js

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
/*
2-
const BitcoinSPV = require("../src/BitcoinSPV").BitcoinSPV
3-
const ElectrumClient = require("../src/ElectrumClient")
4-
const config = require("../../../src/config/config.json")
5-
6-
const fs = require("fs")
7-
const chai = require("chai")
8-
const assert = chai.assert
1+
import {BitcoinSPV} from "../src/lib/BitcoinSPV.js"
2+
import ElectrumClient from "../src/lib/ElectrumClient.js"
3+
import fs from "fs"
4+
import chai from "chai"
5+
6+
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
7+
const tx = JSON.parse(txData)
8+
const electrumClient = new ElectrumClient({
9+
server: "electrumx-server.test.tbtc.network",
10+
port: 8443,
11+
protocol: "wss"
12+
})
13+
const bitcoinSPV = new BitcoinSPV(electrumClient)
914

1015
describe("BitcoinSPV", async () => {
11-
let tx
12-
let electrumClient
13-
let bitcoinSPV
14-
1516
before(async () => {
16-
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
17-
tx = JSON.parse(txData)
18-
19-
electrumClient = new ElectrumClient.Client(config.electrum.testnetPublic)
20-
21-
bitcoinSPV = new BitcoinSPV(electrumClient)
22-
2317
await electrumClient.connect()
2418
})
2519

@@ -40,7 +34,7 @@ describe("BitcoinSPV", async () => {
4034
tx.chainHeadersNumber
4135
)
4236

43-
assert.deepEqual(result, expectedResult)
37+
chai.assert.deepEqual(result, expectedResult)
4438
})
4539

4640
it("verifyMerkleProof", async () => {
@@ -55,6 +49,6 @@ describe("BitcoinSPV", async () => {
5549
blockHeight
5650
)
5751

58-
assert.isTrue(result)
52+
chai.assert.isTrue(result)
5953
})
60-
})*/
54+
})

test/BitcoinTxParserTest.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
/*
2-
const BitcoinTxParser = require("../src/BitcoinTxParser.js")
1+
import {BitcoinTxParser} from "../src/lib/BitcoinTxParser.js"
2+
import fs from "fs"
3+
import chai from "chai"
34

4-
const chai = require("chai")
5-
const assert = chai.assert
6-
const fs = require("fs")
5+
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
6+
const tx = JSON.parse(txData)
77

88
describe("BitcoinTxParser", async () => {
9-
let tx
10-
11-
before(async () => {
12-
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
13-
tx = JSON.parse(txData)
14-
})
15-
169
it("parses witness transaction details", async () => {
1710
const result = await BitcoinTxParser.parse(tx.hex)
1811

@@ -23,7 +16,7 @@ describe("BitcoinTxParser", async () => {
2316
locktime: tx.locktime
2417
}
2518

26-
assert.deepEqual(result, expectedResult)
19+
chai.assert.deepEqual(result, expectedResult)
2720
})
2821

2922
it("parses legacy transaction details", async () => {
@@ -41,6 +34,6 @@ describe("BitcoinTxParser", async () => {
4134
locktime: "00000000"
4235
}
4336

44-
assert.deepEqual(result, expectedResult)
37+
chai.assert.deepEqual(result, expectedResult)
4538
})
46-
})*/
39+
})

test/ElectrumClientTest.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
/*
2-
const ElectrumClient = require("../src/ElectrumClient")
3-
const fs = require("fs")
4-
const chai = require("chai")
1+
import ElectrumClient, {scriptToHash} from "../src/lib/ElectrumClient.js"
2+
import fs from "fs"
3+
import chai from "chai"
54
const assert = chai.assert
6-
const config = require("../../../src/config/config.json")
75

8-
describe("ElectrumClient", async () => {
9-
let client
10-
let tx
6+
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
7+
const tx = JSON.parse(txData)
118

12-
before(async () => {
13-
const txData = fs.readFileSync("./test/data/tx.json", "utf8")
14-
tx = JSON.parse(txData)
9+
const client = new ElectrumClient({
10+
server: "electrumx-server.test.tbtc.network",
11+
port: 8443,
12+
protocol: "wss"
13+
})
1514

16-
client = new ElectrumClient.Client(config.electrum.testnetPublic)
15+
describe("ElectrumClient", async () => {
1716

17+
before(async () => {
1818
await client.connect()
1919
})
2020

@@ -46,14 +46,14 @@ describe("ElectrumClient", async () => {
4646
assert.deepEqual(result, expectedResult)
4747
})
4848

49-
it("getMerkleProof", async () => {
50-
const expectedResult = tx.merkleProof
49+
it("getTransactionMerkle", async () => {
50+
const expectedResult = tx.merkleProof.match(/.{1,64}/g).map(hex=>hex.match(/[a-fA-F0-9]{2}/g).reverse().join(''))
5151
const expectedPosition = tx.indexInBlock
52-
const result = await client.getMerkleProof(tx.hash, tx.blockHeight)
52+
const result = await client.getTransactionMerkle(tx.hash, tx.blockHeight)
5353

54-
assert.equal(result.proof, expectedResult, "unexpected result")
54+
assert.deepEqual(result.merkle, expectedResult, "unexpected result")
5555

56-
assert.equal(result.position, expectedPosition, "unexpected result")
56+
assert.equal(result.pos, expectedPosition, "unexpected result")
5757
})
5858

5959
it("getHeadersChain", async () => {
@@ -161,7 +161,7 @@ describe("ElectrumClient", async () => {
161161
const mockEventEmission = function() {
162162
client.electrumClient.subscribe.emit(
163163
"blockchain.scripthash.subscribe",
164-
[ElectrumClient.scriptToHash(script1), expectedStatus]
164+
[scriptToHash(script1), expectedStatus]
165165
)
166166
}
167167

@@ -193,12 +193,12 @@ describe("ElectrumClient", async () => {
193193
const mockEventsEmission = function() {
194194
client.electrumClient.subscribe.emit(
195195
"blockchain.scripthash.subscribe",
196-
[ElectrumClient.scriptToHash(script2), unexpectedStatus]
196+
[scriptToHash(script2), unexpectedStatus]
197197
)
198198

199199
client.electrumClient.subscribe.emit(
200200
"blockchain.scripthash.subscribe",
201-
[ElectrumClient.scriptToHash(script1), expectedStatus]
201+
[scriptToHash(script1), expectedStatus]
202202
)
203203
}
204204

@@ -249,7 +249,7 @@ describe("ElectrumClient", async () => {
249249
// onRejected
250250
assert.include(
251251
reason.toString(),
252-
"No such mempool or blockchain transaction"
252+
"Transaction not found"
253253
)
254254
}
255255
)
@@ -284,4 +284,4 @@ describe("ElectrumClient", async () => {
284284
console.log("result", result)
285285
})
286286
})
287-
})*/
287+
})

0 commit comments

Comments
 (0)