From b49886885b15d54ddaa051e22578fd60f636ea41 Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Fri, 12 Jan 2018 04:20:32 -0500 Subject: [PATCH 1/4] Implement get_balance, listunspent, and get_history for scripthash --- example/scripthash.js | 18 ++++++++++++++++++ lib/electrum_client.js | 9 +++++++++ 2 files changed, 27 insertions(+) create mode 100644 example/scripthash.js diff --git a/example/scripthash.js b/example/scripthash.js new file mode 100644 index 0000000..d66c15d --- /dev/null +++ b/example/scripthash.js @@ -0,0 +1,18 @@ +const ElectrumClient = require('..') + +const main = async () => { + const ecl = new ElectrumClient(50002, 'bitcoins.sk', 'tls') + await ecl.connect() + try{ + const ver = await ecl.server_version("3.0.5", "1.1") + console.log(ver) + const balance = await ecl.blockchainScripthash_getBalance("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c") + console.log(balance) + const unspent = await ecl.blockchainScripthash_listunspent("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c") + console.log(unspent) + }catch(e){ + console.log(e) + } + await ecl.close() +} +main().catch(console.log) diff --git a/lib/electrum_client.js b/lib/electrum_client.js index be98885..1354c10 100644 --- a/lib/electrum_client.js +++ b/lib/electrum_client.js @@ -40,6 +40,15 @@ class ElectrumClient extends Client{ blockchainAddress_listunspent(address){ return this.request('blockchain.address.listunspent', [address]) } + blockchainScripthash_getBalance(scripthash) { + return this.request('blockchain.scripthash.get_balance', [scripthash]); + } + blockchainScripthash_listunspent(scripthash) { + return this.request('blockchain.scripthash.listunspent', [scripthash]); + } + blockchainScripthash_getHistory(scripthash) { + return this.request('blockchain.scripthash.get_history', [scripthash]); + } blockchainBlock_getHeader(height){ return this.request('blockchain.block.get_header', [height]) } From 9277fda4e81f4b6adb72db296256653f2d9d4fc2 Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Fri, 12 Jan 2018 04:58:22 -0500 Subject: [PATCH 2/4] Implement remaining non-subscribe methods --- example/scripthash.js | 4 ++++ lib/electrum_client.js | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/example/scripthash.js b/example/scripthash.js index d66c15d..1e16423 100644 --- a/example/scripthash.js +++ b/example/scripthash.js @@ -10,6 +10,10 @@ const main = async () => { console.log(balance) const unspent = await ecl.blockchainScripthash_listunspent("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c") console.log(unspent) + const history = await ecl.blockchainScripthash_getHistory("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c") + console.log(history) + const mempool = await ecl.blockchainScripthash_getMempool("676ca8550e249787290b987e12cebdb2e9b26d88c003d836ffb1cb03ffcbea7c") + console.log(mempool) }catch(e){ console.log(e) } diff --git a/lib/electrum_client.js b/lib/electrum_client.js index 1354c10..0490f05 100644 --- a/lib/electrum_client.js +++ b/lib/electrum_client.js @@ -40,14 +40,20 @@ class ElectrumClient extends Client{ blockchainAddress_listunspent(address){ return this.request('blockchain.address.listunspent', [address]) } - blockchainScripthash_getBalance(scripthash) { - return this.request('blockchain.scripthash.get_balance', [scripthash]); + blockchainScripthash_getBalance(scripthash){ + return this.request('blockchain.scripthash.get_balance', [scripthash]) } - blockchainScripthash_listunspent(scripthash) { - return this.request('blockchain.scripthash.listunspent', [scripthash]); + blockchainScripthash_getHistory(scripthash){ + return this.request('blockchain.scripthash.get_history', [scripthash]) } - blockchainScripthash_getHistory(scripthash) { - return this.request('blockchain.scripthash.get_history', [scripthash]); + blockchainScripthash_getMempool(scripthash){ + return this.request('blockchain.scripthash.get_mempool', [scripthash]) + } + blockchainScripthash_getProof(scripthash){ + return this.request('blockchain.scripthash.get_proof', [scripthash]) + } + blockchainScripthash_listunspent(scripthash){ + return this.request('blockchain.scripthash.listunspent', [scripthash]) } blockchainBlock_getHeader(height){ return this.request('blockchain.block.get_header', [height]) From 57ab7bf0aacf0b82906922ce043e4d61d7c83d49 Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Fri, 12 Jan 2018 05:40:36 -0500 Subject: [PATCH 3/4] Add address and scripthash subscribe methods --- example/subscribe.js | 12 +++++++++--- lib/electrum_client.js | 9 ++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/example/subscribe.js b/example/subscribe.js index 5b5fd3b..d9b1a8c 100644 --- a/example/subscribe.js +++ b/example/subscribe.js @@ -3,17 +3,23 @@ const sleep = (ms) => new Promise((resolve,_) => setTimeout(() => resolve(), ms) const main = async () => { try{ - const ecl = new ElectrumClient(995, 'btc.smsys.me', 'tls') + const ecl = new ElectrumClient(50002, 'bitcoins.sk', 'tls') ecl.subscribe.on('server.peers.subscribe', console.log) ecl.subscribe.on('blockchain.numblocks.subscribe', console.log) ecl.subscribe.on('blockchain.headers.subscribe', console.log) + ecl.subscribe.on('blockchain.address.subscribe', console.log) + ecl.subscribe.on('blockchain.scripthash.subscribe', console.log) await ecl.connect() + await ecl.server_version("3.0.5", "1.1") const p1 = await ecl.serverPeers_subscribe() const p2 = await ecl.blockchainHeaders_subscribe() - const p3 = await ecl.blockchainNumblocks_subscribe() + // Note: blockchain.numblocks.subscribe is deprecated in protocol version 1.1 + const p3 = await ecl.blockchainAddress_subscribe('1BK45iaPrrd26gKagrXytvz6anrj3hQ2pQ') + // Subscribe to corresponding scripthash for the above address + const p4 = await ecl.blockchainScripthash_subscribe('f3aa57a41424146327e5c88c25db8953dd16c6ab6273cdb74a4404ed4d0f5714') while(true){ await sleep(1000) - let version = await ecl.server_version("2.7.11", "1.0") + const ver = await ecl.server_version("3.0.5", "1.1") } await ecl.close() }catch(e){ diff --git a/lib/electrum_client.js b/lib/electrum_client.js index 0490f05..c15456d 100644 --- a/lib/electrum_client.js +++ b/lib/electrum_client.js @@ -40,6 +40,9 @@ class ElectrumClient extends Client{ blockchainAddress_listunspent(address){ return this.request('blockchain.address.listunspent', [address]) } + blockchainAddress_subscribe(address){ + return this.request('blockchain.address.subscribe', [address]) + } blockchainScripthash_getBalance(scripthash){ return this.request('blockchain.scripthash.get_balance', [scripthash]) } @@ -49,12 +52,12 @@ class ElectrumClient extends Client{ blockchainScripthash_getMempool(scripthash){ return this.request('blockchain.scripthash.get_mempool', [scripthash]) } - blockchainScripthash_getProof(scripthash){ - return this.request('blockchain.scripthash.get_proof', [scripthash]) - } blockchainScripthash_listunspent(scripthash){ return this.request('blockchain.scripthash.listunspent', [scripthash]) } + blockchainScripthash_subscribe(scripthash){ + return this.request('blockchain.scripthash.subscribe', [scripthash]) + } blockchainBlock_getHeader(height){ return this.request('blockchain.block.get_header', [height]) } From 8ed71b1e492c1f41a2307d001a873d206f327262 Mon Sep 17 00:00:00 2001 From: Colin Atkinson Date: Thu, 8 Feb 2018 18:32:09 -0500 Subject: [PATCH 4/4] Don't send deprecated height in blockchainTransaction_get --- lib/electrum_client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/electrum_client.js b/lib/electrum_client.js index c15456d..4c01459 100644 --- a/lib/electrum_client.js +++ b/lib/electrum_client.js @@ -80,7 +80,7 @@ class ElectrumClient extends Client{ return this.request('blockchain.transaction.broadcast', [rawtx]) } blockchainTransaction_get(tx_hash, height){ - return this.request('blockchain.transaction.get', [tx_hash, height]) + return this.request('blockchain.transaction.get', [tx_hash]) } blockchainTransaction_getMerkle(tx_hash, height){ return this.request('blockchain.transaction.get_merkle', [tx_hash, height])