Browse Source

Merge pull request #5 from synonymdev/subscribe-address-update

fix(subscribe): Subscribe Address Fix
get-transaction-merkle
Corey 2 years ago
committed by GitHub
parent
commit
672d13c63b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      helpers/clients.js
  2. 14
      helpers/index.js

5
helpers/clients.js

@ -26,6 +26,11 @@ class Clients {
bitcoinTestnet: false,
bitcoinRegtest: false
};
this.onAddressReceive = {
bitcoin: undefined,
bitcoinTestnet: undefined,
bitcoinRegtest: undefined
}
}
updateNetwork(network) {

14
helpers/index.js

@ -320,13 +320,19 @@ const subscribeHeader = async ({ id = "subscribeHeader", network = "", onReceive
}
};
const subscribeAddress = async ({ id = Math.random(), scriptHash = "", network = "bitcoin", onReceive = (data) => console.log(data) } = {}) => {
const subscribeAddress = async ({ id = Math.random(), scriptHash = "", network = "bitcoin", onReceive = undefined } = {}) => {
try {
if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]);
if (clients.subscribedAddresses[network].length < 1) {
const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (onReceive)));
if (res.error) return { ...res, id, method: "subscribeAddress" };
// Set onAddressReceive if wasn't previously.
if (onReceive && !clients.onAddressReceive[network]) {
clients.onAddressReceive[network] = onReceive;
const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', clients.onAddressReceive[network]));
if (res.error) {
return { ...res, id, method: "subscribeAddress" };
}
}
//Ensure this address is not already subscribed
if (clients.subscribedAddresses[network].includes(scriptHash)) return { id, error: false, method: "subscribeAddress", data: "Already Subscribed." };
const response = await promiseTimeout(10000, clients.mainClient[network].blockchainScripthash_subscribe(scriptHash));

Loading…
Cancel
Save