diff --git a/README.md b/README.md index e9eaf74..9b9aee5 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ import { start, getPeers, getAddressScriptHashBalance, + getAddressScriptHashBalances, subscribeHeader, subscribeAddress } from "rn-electrum-client/helpers"; @@ -41,4 +42,43 @@ const subscribeAddressResponse = await subscribeAddress({ onReceive: (data) => { console.log("Received some Testnet BTC!", data); } }); console.log(subscribeAddressResponse); + +//Get Address Balances (Batch Method) +const getAddressScriptHashes = [ + "21031994b184684de964e930e9937b1b6d75c9af12bb99ae6ee266a724e12632", + "9ff2d1500272589aff699e1582dbc2f47a7bbe19f7d3a4d77e1c9e54735d4fc4", + "bfb2a1c0a45421f908e0b3f63cee65a2dee33ae276bdb82c6f53f6ec1d53093b", + "ce2b6d0b4ed19ff9b264d1d218220cae85388b6d53a539bd0852404b65d74b1b", + "4470798cb53a34f8adf763248c90b7b09e6375043a74346d5ad9a1b6c6cc1130" +]; +const getAddressScriptHashBalancesResponse = await getAddressScriptHashBalances({ + scriptHashes: getAddressScriptHashes, + network: "bitcoinTestnet" +}); +console.log(getAddressScriptHashBalancesResponse); + +/* +We are also able to get out what we put in using the following pattern. +This works with any batch method. +*/ +const getAddressScriptHashes2 = { + key: "scriptHash", + data: [ + { + address: "tb1qnv5luf8mav8263sxfa4fdr3m6kws74n0yfzzrx", + path: "m/84'/1'/0'/0/0", + scriptHash: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd" + }, + { + address: "tb1qsmkaeekrq204w8jvty2dtpuksnlu8ct0w4pwst", + path: "m/84'/1'/0'/0/1", + scriptHash: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409" + }, + ] +}; +const getAddressScriptHashBalancesResponse2 = await getAddressScriptHashBalances({ + scriptHashes: getAddressScriptHashes2, + network: "bitcoinTestnet" +}); +console.log(getAddressScriptHashBalancesResponse2); ``` diff --git a/test/constants.js b/test/constants.js new file mode 100644 index 0000000..492fbbb --- /dev/null +++ b/test/constants.js @@ -0,0 +1,117 @@ +const network = "bitcoinTestnet"; + +const peer = { host: "tn.not.fyi", port: "55002", protocol: "ssl" }; + +const customPeers = [peer]; + +const testPhrase = "know suspect impose snake ice sea usual pony leisure rally style hello limit orphan arrow clinic sustain hurry young immune gather always dash portion"; + +const addresses = { + "bitcoin": [], + "bitcoinTestnet": [ + {address: "tb1qnv5luf8mav8263sxfa4fdr3m6kws74n0yfzzrx", path: "m/84'/1'/0'/0/0", scriptHash: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd"}, + {address: "tb1qsmkaeekrq204w8jvty2dtpuksnlu8ct0w4pwst", path: "m/84'/1'/0'/0/1", scriptHash: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409"}, + {address: "tb1qldarhwj08sswsmxeq4rzvqlfl2dax0quy5q7le", path: "m/84'/1'/0'/0/2", scriptHash: "155fd36e4a57ff949e439e695c466a82f17df3ceb90d57686a9ec79ef8edaaa4"}, + {address: "tb1qjlp5zp64v2tq8mm2j05u7psxv6gnkdzzc67pep", path: "m/84'/1'/0'/0/3", scriptHash: "8b47b2f4de51db4c5c5617e448b1f1c48e44963031ece669840e0dc13117af20"}, + {address: "tb1qtnqwzlxrltd4agh7vq7fjjh33kta0f99au5qa8", path: "m/84'/1'/0'/0/4", scriptHash: "eca23eedc40d970b3083521950c2d11368204e80f04e71a0d9e26e8d2fe1c0c6"} + ] +} + +const changeAddresses = { + "bitcoin": [], + "bitcoinTestnet": [ + {address: "tb1qjn3plhv7nvuerwjzdkcavjyv6a8yt3n3neru9w", path: "m/84'/1'/0'/1/0", scriptHash: "330c608e84fdb8abea609fe5cb63ec51b844aa1bfd2fa43329e503e2a61a21c4"}, + {address: "tb1qazer8866tw7kkevl7xkwz7a9vrpsssppg75fz3", path: "m/84'/1'/0'/1/1", scriptHash: "aa48a8f02d20bf78a46fe6500159dd2528aa6d9edd4c6190a5a5504256428ede"}, + {address: "tb1qe3d76zp4u546vdshpc04a3apsc2yzxq74y50xr", path: "m/84'/1'/0'/1/2", scriptHash: "5a3cbc639145964bda8ac77932faf27662cc3b3cac8a543f084ff31dfa2f5ecb"}, + {address: "tb1qeu3jy528cce03rzkdxp8rphyasc0eq2g5gn7re", path: "m/84'/1'/0'/1/3", scriptHash: "0cf425d51792989639f540f10c90f6a89e67e1443fd76172153d16691c170132"}, + {address: "tb1qmmvcxyx3myjr7kd5k4ghk732jyywl0puyrwhq0", path: "m/84'/1'/0'/1/4", scriptHash: "d605aae846e49cbb9f1b1b8850520aa9c4c42b640ebc4184aaa88ff6bced284a"} + ] +} + +const address = addresses["bitcoinTestnet"][0]; + +const scriptHashes = [ + "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd", + "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409", + "155fd36e4a57ff949e439e695c466a82f17df3ceb90d57686a9ec79ef8edaaa4", + "8b47b2f4de51db4c5c5617e448b1f1c48e44963031ece669840e0dc13117af20", + "eca23eedc40d970b3083521950c2d11368204e80f04e71a0d9e26e8d2fe1c0c6" +]; + +const scriptHashData = { + key: "scriptHash", + data: [ + { + address: "tb1qnv5luf8mav8263sxfa4fdr3m6kws74n0yfzzrx", + path: "m/84'/1'/0'/0/0", + scriptHash: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd" + }, + { + address: "tb1qsmkaeekrq204w8jvty2dtpuksnlu8ct0w4pwst", + path: "m/84'/1'/0'/0/1", + scriptHash: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409" + }, + ] +}; + +const getAddressScriptHashBalanceTest = { + //id: 0.17807014111851083, + error: false, + method: 'getAddressScriptHashBalance', + data: { confirmed: 27000, unconfirmed: 0 }, + scriptHash: addresses[network][0].scriptHash, + network +} + +const getAddressScriptHashBalancesTest = { + error: false, + //id: 0.4953846351782525, + method: "getAddressScriptHashBalances", + network: "bitcoinTestnet", + data: [ + { + jsonrpc: "2.0", + result: { + confirmed: 0, + unconfirmed: 0 + }, + id: 4, + param: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409", + data: { + address: "tb1qsmkaeekrq204w8jvty2dtpuksnlu8ct0w4pwst", + path: "m/84'/1'/0'/0/1", + scriptHash: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409" + } + }, + { + jsonrpc: "2.0", + result: { + confirmed: 27000, + unconfirmed: 0 + }, + id: 3, + param: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd", + data: { + address: "tb1qnv5luf8mav8263sxfa4fdr3m6kws74n0yfzzrx", + path: "m/84'/1'/0'/0/0", + scriptHash: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd" + } + } + ] +} + +module.exports = { + network, + peer, + customPeers, + testPhrase, + address, + addresses, + changeAddresses, + scriptHashes, + scriptHashData, + expectedResponses: { + getAddressScriptHashBalanceTest, + getAddressScriptHashBalancesTest + } +} diff --git a/test/electrum-client-helpers-test.js b/test/electrum-client-helpers-test.js index 2cebdcc..dbf5d07 100644 --- a/test/electrum-client-helpers-test.js +++ b/test/electrum-client-helpers-test.js @@ -1,33 +1,21 @@ +const constants = require("./constants"); + const assert = require("assert"); const { describe, it } = require("mocha"); const helpers = require("../helpers"); const clients = require("../helpers/clients"); -const network = "bitcoinTestnet"; -const peer = { host: "tn.not.fyi", port: "55002", protocol: "ssl" }; -const customPeers = [peer]; - -const testPhrase = "know suspect impose snake ice sea usual pony leisure rally style hello limit orphan arrow clinic sustain hurry young immune gather always dash portion"; -const addresses = { - "bitcoin": [], - "bitcoinTestnet": [ - {address: "tb1qnv5luf8mav8263sxfa4fdr3m6kws74n0yfzzrx", path: "m/84'/1'/0'/0/0", scriptHash: "77ca78f9a84b48041ad71f7cc6ff6c33460c25f0cb99f558f9813ed9e63727dd"}, - {address: "tb1qsmkaeekrq204w8jvty2dtpuksnlu8ct0w4pwst", path: "m/84'/1'/0'/0/1", scriptHash: "743514a90b216fe3b28466353b1304c90010c54a146367a1f0c9ea53511d0409"}, - {address: "tb1qldarhwj08sswsmxeq4rzvqlfl2dax0quy5q7le", path: "m/84'/1'/0'/0/2", scriptHash: "155fd36e4a57ff949e439e695c466a82f17df3ceb90d57686a9ec79ef8edaaa4"}, - {address: "tb1qjlp5zp64v2tq8mm2j05u7psxv6gnkdzzc67pep", path: "m/84'/1'/0'/0/3", scriptHash: "8b47b2f4de51db4c5c5617e448b1f1c48e44963031ece669840e0dc13117af20"}, - {address: "tb1qtnqwzlxrltd4agh7vq7fjjh33kta0f99au5qa8", path: "m/84'/1'/0'/0/4", scriptHash: "eca23eedc40d970b3083521950c2d11368204e80f04e71a0d9e26e8d2fe1c0c6"} - ] -} -const changeAddresses = { - "bitcoin": [], - "bitcoinTestnet": [ - {address: "tb1qjn3plhv7nvuerwjzdkcavjyv6a8yt3n3neru9w", path: "m/84'/1'/0'/1/0", scriptHash: "330c608e84fdb8abea609fe5cb63ec51b844aa1bfd2fa43329e503e2a61a21c4"}, - {address: "tb1qazer8866tw7kkevl7xkwz7a9vrpsssppg75fz3", path: "m/84'/1'/0'/1/1", scriptHash: "aa48a8f02d20bf78a46fe6500159dd2528aa6d9edd4c6190a5a5504256428ede"}, - {address: "tb1qe3d76zp4u546vdshpc04a3apsc2yzxq74y50xr", path: "m/84'/1'/0'/1/2", scriptHash: "5a3cbc639145964bda8ac77932faf27662cc3b3cac8a543f084ff31dfa2f5ecb"}, - {address: "tb1qeu3jy528cce03rzkdxp8rphyasc0eq2g5gn7re", path: "m/84'/1'/0'/1/3", scriptHash: "0cf425d51792989639f540f10c90f6a89e67e1443fd76172153d16691c170132"}, - {address: "tb1qmmvcxyx3myjr7kd5k4ghk732jyywl0puyrwhq0", path: "m/84'/1'/0'/1/4", scriptHash: "d605aae846e49cbb9f1b1b8850520aa9c4c42b640ebc4184aaa88ff6bced284a"} - ] -} -const address = addresses["bitcoinTestnet"][0]; +const { + network, + peer, + customPeers, + testPhrase, + address, + addresses, + changeAddresses, + scriptHashes, + scriptHashData, + expectedResponses +} = constants; /* This ensures the response object resembles the following: @@ -138,7 +126,26 @@ const getAddressScriptHashBalanceTest = ({ network = "bitcoinTestnet", scriptHas let response = { error: true, data: "" }; it("Should receive response.", async () => { response = await helpers.getAddressScriptHashBalance({ network, scriptHash }); - if (!("error" in response) || response.error) console.log("getAddressBalance Response:", response); + if (!("error" in response) || response.error) console.log("getAddressScriptHashBalance Response:", response); + isResponseObject(response); + }); + it("Response should match.", async () => { + assert("id" in response); + delete response.id; + assert.deepStrictEqual(response, expectedResponse); + }); + }); +} + +/* + This test ensures that getAddressScriptHashBalances returns the balance of specified address scriptHashes. + */ +const getAddressScriptHashBalancesTest = ({ network = "bitcoinTestnet", scriptHashes = [], description = "", expectedResponse = {} } = {}) => { + describe(`helpers.getAddressScriptHashBalances: ${description}`, () => { + let response = { error: true, data: "" }; + it("Should receive response.", async () => { + response = await helpers.getAddressScriptHashBalances({ network, scriptHashes: scriptHashData }); + if (!("error" in response) || response.error) console.log("getAddressScriptHashBalances Response:", response); isResponseObject(response); }); it("Response should match.", async () => { @@ -233,31 +240,31 @@ const getFeeEstimateTest = () => { describe("electrum-client-helpers", () => { startRandomPeerTest(); - + disconnectTest(); - + startCustomPeerTest(); - + getAddressScriptHashBalanceTest({ network, scriptHash: addresses[network][0].scriptHash, description: "Attempt to get address scripthash balance.", - expectedResponse: { - //id: 0.17807014111851083, - error: false, - method: 'getAddressScriptHashBalance', - data: { confirmed: 27000, unconfirmed: 0 }, - scriptHash: addresses[network][0].scriptHash, - network - } + expectedResponse: expectedResponses.getAddressScriptHashBalanceTest + }); + + getAddressScriptHashBalancesTest({ + network, + scriptHash: scriptHashes, + description: "Attempt to get address scripthash balance.", + expectedResponse: expectedResponses.getAddressScriptHashBalancesTest }); - + getPeersTest(); - + subscribeHeaderTest(); - + subscribeAddressTest(); - + getFeeEstimateTest(); - + });