diff --git a/src/lib/ElectrumClient.js b/src/lib/ElectrumClient.js index 14db804..e477645 100644 --- a/src/lib/ElectrumClient.js +++ b/src/lib/ElectrumClient.js @@ -453,7 +453,7 @@ export default class Client { * @param {string} script ScriptPubKey in a hexadecimal format. * @return {string} Script hash as a hex string. */ -function scriptToHash(script) { +export function scriptToHash(script) { /** @type {Buffer} */ const scriptHash = digest(Buffer.from(script, "hex")).reverse() return scriptHash.toString("hex") diff --git a/test/AddressTest.js b/test/AddressTest.js deleted file mode 100644 index 647f96d..0000000 --- a/test/AddressTest.js +++ /dev/null @@ -1,51 +0,0 @@ -/* -const BitcoinAddress = require("../src/Address") -const Network = BitcoinAddress.Network - -const chai = require("chai") -const assert = chai.assert - -describe("BitcoinAddress", async () => { - describe("publicKeyToP2WPKHaddress", async () => { - // Concatenated `x` and `y` coordinates derived from a public key specified in - // [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki): - // 0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798 - const publicKey = - "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8" - - it("calculates testnet address", async () => { - const chainType = Network.testnet - const expectedResult = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx" - - const result = await BitcoinAddress.publicKeyToP2WPKHaddress( - publicKey, - chainType - ) - - assert.equal(result, expectedResult) - }) - - it("calculates mainnet address", async () => { - const chainType = Network.mainnet - const expectedResult = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" - - const result = await BitcoinAddress.publicKeyToP2WPKHaddress( - publicKey, - chainType - ) - - assert.equal(result, expectedResult) - }) - }) - - describe("addressToScript", async () => { - it("converts string to script", async () => { - const address = "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx" - const expectedResult = "0014751e76e8199196d454941c45d1b3a323f1433bd6" - - const result = await BitcoinAddress.addressToScript(address) - - assert.equal(result, expectedResult) - }) - }) -})*/ diff --git a/test/BitcoinSPVTest.js b/test/BitcoinSPVTest.js index 9d75574..d39b23c 100644 --- a/test/BitcoinSPVTest.js +++ b/test/BitcoinSPVTest.js @@ -1,25 +1,19 @@ -/* -const BitcoinSPV = require("../src/BitcoinSPV").BitcoinSPV -const ElectrumClient = require("../src/ElectrumClient") -const config = require("../../../src/config/config.json") - -const fs = require("fs") -const chai = require("chai") -const assert = chai.assert +import {BitcoinSPV} from "../src/lib/BitcoinSPV.js" +import ElectrumClient from "../src/lib/ElectrumClient.js" +import fs from "fs" +import chai from "chai" + +const txData = fs.readFileSync("./test/data/tx.json", "utf8") +const tx = JSON.parse(txData) +const electrumClient = new ElectrumClient({ + server: "electrumx-server.test.tbtc.network", + port: 8443, + protocol: "wss" +}) +const bitcoinSPV = new BitcoinSPV(electrumClient) describe("BitcoinSPV", async () => { - let tx - let electrumClient - let bitcoinSPV - before(async () => { - const txData = fs.readFileSync("./test/data/tx.json", "utf8") - tx = JSON.parse(txData) - - electrumClient = new ElectrumClient.Client(config.electrum.testnetPublic) - - bitcoinSPV = new BitcoinSPV(electrumClient) - await electrumClient.connect() }) @@ -40,7 +34,7 @@ describe("BitcoinSPV", async () => { tx.chainHeadersNumber ) - assert.deepEqual(result, expectedResult) + chai.assert.deepEqual(result, expectedResult) }) it("verifyMerkleProof", async () => { @@ -55,6 +49,6 @@ describe("BitcoinSPV", async () => { blockHeight ) - assert.isTrue(result) + chai.assert.isTrue(result) }) -})*/ +}) diff --git a/test/BitcoinTxParserTest.js b/test/BitcoinTxParserTest.js index 09f2d8f..1bb7801 100644 --- a/test/BitcoinTxParserTest.js +++ b/test/BitcoinTxParserTest.js @@ -1,18 +1,11 @@ -/* -const BitcoinTxParser = require("../src/BitcoinTxParser.js") +import {BitcoinTxParser} from "../src/lib/BitcoinTxParser.js" +import fs from "fs" +import chai from "chai" -const chai = require("chai") -const assert = chai.assert -const fs = require("fs") +const txData = fs.readFileSync("./test/data/tx.json", "utf8") +const tx = JSON.parse(txData) describe("BitcoinTxParser", async () => { - let tx - - before(async () => { - const txData = fs.readFileSync("./test/data/tx.json", "utf8") - tx = JSON.parse(txData) - }) - it("parses witness transaction details", async () => { const result = await BitcoinTxParser.parse(tx.hex) @@ -23,7 +16,7 @@ describe("BitcoinTxParser", async () => { locktime: tx.locktime } - assert.deepEqual(result, expectedResult) + chai.assert.deepEqual(result, expectedResult) }) it("parses legacy transaction details", async () => { @@ -41,6 +34,6 @@ describe("BitcoinTxParser", async () => { locktime: "00000000" } - assert.deepEqual(result, expectedResult) + chai.assert.deepEqual(result, expectedResult) }) -})*/ +}) diff --git a/test/ElectrumClientTest.js b/test/ElectrumClientTest.js index 0a516d3..f7a9fad 100644 --- a/test/ElectrumClientTest.js +++ b/test/ElectrumClientTest.js @@ -1,20 +1,20 @@ -/* -const ElectrumClient = require("../src/ElectrumClient") -const fs = require("fs") -const chai = require("chai") +import ElectrumClient, {scriptToHash} from "../src/lib/ElectrumClient.js" +import fs from "fs" +import chai from "chai" const assert = chai.assert -const config = require("../../../src/config/config.json") -describe("ElectrumClient", async () => { - let client - let tx +const txData = fs.readFileSync("./test/data/tx.json", "utf8") +const tx = JSON.parse(txData) - before(async () => { - const txData = fs.readFileSync("./test/data/tx.json", "utf8") - tx = JSON.parse(txData) +const client = new ElectrumClient({ + server: "electrumx-server.test.tbtc.network", + port: 8443, + protocol: "wss" +}) - client = new ElectrumClient.Client(config.electrum.testnetPublic) +describe("ElectrumClient", async () => { + before(async () => { await client.connect() }) @@ -46,14 +46,14 @@ describe("ElectrumClient", async () => { assert.deepEqual(result, expectedResult) }) - it("getMerkleProof", async () => { - const expectedResult = tx.merkleProof + it("getTransactionMerkle", async () => { + const expectedResult = tx.merkleProof.match(/.{1,64}/g).map(hex=>hex.match(/[a-fA-F0-9]{2}/g).reverse().join('')) const expectedPosition = tx.indexInBlock - const result = await client.getMerkleProof(tx.hash, tx.blockHeight) + const result = await client.getTransactionMerkle(tx.hash, tx.blockHeight) - assert.equal(result.proof, expectedResult, "unexpected result") + assert.deepEqual(result.merkle, expectedResult, "unexpected result") - assert.equal(result.position, expectedPosition, "unexpected result") + assert.equal(result.pos, expectedPosition, "unexpected result") }) it("getHeadersChain", async () => { @@ -161,7 +161,7 @@ describe("ElectrumClient", async () => { const mockEventEmission = function() { client.electrumClient.subscribe.emit( "blockchain.scripthash.subscribe", - [ElectrumClient.scriptToHash(script1), expectedStatus] + [scriptToHash(script1), expectedStatus] ) } @@ -193,12 +193,12 @@ describe("ElectrumClient", async () => { const mockEventsEmission = function() { client.electrumClient.subscribe.emit( "blockchain.scripthash.subscribe", - [ElectrumClient.scriptToHash(script2), unexpectedStatus] + [scriptToHash(script2), unexpectedStatus] ) client.electrumClient.subscribe.emit( "blockchain.scripthash.subscribe", - [ElectrumClient.scriptToHash(script1), expectedStatus] + [scriptToHash(script1), expectedStatus] ) } @@ -249,7 +249,7 @@ describe("ElectrumClient", async () => { // onRejected assert.include( reason.toString(), - "No such mempool or blockchain transaction" + "Transaction not found" ) } ) @@ -284,4 +284,4 @@ describe("ElectrumClient", async () => { console.log("result", result) }) }) -})*/ +})