|
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" |
5 | 4 | const assert = chai.assert |
6 | | -const config = require("../../../src/config/config.json") |
7 | 5 |
|
8 | | -describe("ElectrumClient", async () => { |
9 | | - let client |
10 | | - let tx |
11 | | -
|
12 | | - before(async () => { |
13 | | - const txData = fs.readFileSync("./test/data/tx.json", "utf8") |
14 | | - tx = JSON.parse(txData) |
| 6 | +const txData = fs.readFileSync("./test/data/tx.json", "utf8") |
| 7 | +const tx = JSON.parse(txData) |
15 | 8 |
|
16 | | - client = new ElectrumClient.Client(config.electrum.testnetPublic) |
| 9 | +const client = new ElectrumClient({ |
| 10 | + server: "electrumx-server.test.tbtc.network", |
| 11 | + port: 8443, |
| 12 | + protocol: "wss" |
| 13 | +}) |
17 | 14 |
|
| 15 | +describe("ElectrumClient", async () => { |
| 16 | + before(async () => { |
18 | 17 | await client.connect() |
19 | 18 | }) |
20 | 19 |
|
@@ -46,14 +45,19 @@ describe("ElectrumClient", async () => { |
46 | 45 | assert.deepEqual(result, expectedResult) |
47 | 46 | }) |
48 | 47 |
|
49 | | - it("getMerkleProof", async () => { |
50 | | - const expectedResult = tx.merkleProof |
| 48 | + it("getTransactionMerkle", async () => { |
| 49 | + const expectedResult = tx.merkleProof.match(/.{1,64}/g).map(hex => |
| 50 | + hex |
| 51 | + .match(/[a-fA-F0-9]{2}/g) |
| 52 | + .reverse() |
| 53 | + .join("") |
| 54 | + ) |
51 | 55 | const expectedPosition = tx.indexInBlock |
52 | | - const result = await client.getMerkleProof(tx.hash, tx.blockHeight) |
| 56 | + const result = await client.getTransactionMerkle(tx.hash, tx.blockHeight) |
53 | 57 |
|
54 | | - assert.equal(result.proof, expectedResult, "unexpected result") |
| 58 | + assert.deepEqual(result.merkle, expectedResult, "unexpected result") |
55 | 59 |
|
56 | | - assert.equal(result.position, expectedPosition, "unexpected result") |
| 60 | + assert.equal(result.pos, expectedPosition, "unexpected result") |
57 | 61 | }) |
58 | 62 |
|
59 | 63 | it("getHeadersChain", async () => { |
@@ -161,7 +165,7 @@ describe("ElectrumClient", async () => { |
161 | 165 | const mockEventEmission = function() { |
162 | 166 | client.electrumClient.subscribe.emit( |
163 | 167 | "blockchain.scripthash.subscribe", |
164 | | - [ElectrumClient.scriptToHash(script1), expectedStatus] |
| 168 | + [scriptToHash(script1), expectedStatus] |
165 | 169 | ) |
166 | 170 | } |
167 | 171 |
|
@@ -193,12 +197,12 @@ describe("ElectrumClient", async () => { |
193 | 197 | const mockEventsEmission = function() { |
194 | 198 | client.electrumClient.subscribe.emit( |
195 | 199 | "blockchain.scripthash.subscribe", |
196 | | - [ElectrumClient.scriptToHash(script2), unexpectedStatus] |
| 200 | + [scriptToHash(script2), unexpectedStatus] |
197 | 201 | ) |
198 | 202 |
|
199 | 203 | client.electrumClient.subscribe.emit( |
200 | 204 | "blockchain.scripthash.subscribe", |
201 | | - [ElectrumClient.scriptToHash(script1), expectedStatus] |
| 205 | + [scriptToHash(script1), expectedStatus] |
202 | 206 | ) |
203 | 207 | } |
204 | 208 |
|
@@ -247,10 +251,7 @@ describe("ElectrumClient", async () => { |
247 | 251 | }, |
248 | 252 | reason => { |
249 | 253 | // onRejected |
250 | | - assert.include( |
251 | | - reason.toString(), |
252 | | - "No such mempool or blockchain transaction" |
253 | | - ) |
| 254 | + assert.include(reason.toString(), "Transaction not found") |
254 | 255 | } |
255 | 256 | ) |
256 | 257 |
|
@@ -284,4 +285,4 @@ describe("ElectrumClient", async () => { |
284 | 285 | console.log("result", result) |
285 | 286 | }) |
286 | 287 | }) |
287 | | -})*/ |
| 288 | +}) |
0 commit comments