1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 01:43:29 +01:00

enhance contribs to allow to connect to an external server

This commit is contained in:
Frederic Lepied 2021-10-12 21:23:02 +02:00
parent b94c98fc31
commit 74f13419a8
7 changed files with 39 additions and 12 deletions

1
contrib/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv

View File

@ -5,10 +5,11 @@ import json
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--host', default='localhost')
parser.add_argument("txid") parser.add_argument("txid")
args = parser.parse_args() args = parser.parse_args()
conn = client.Client(("localhost", 50001)) conn = client.Client((args.host, 50001))
tx, = conn.call([client.request("blockchain.transaction.get", args.txid, True)]) tx, = conn.call([client.request("blockchain.transaction.get", args.txid, True)])
print(json.dumps(tx)) print(json.dumps(tx))

View File

@ -5,7 +5,6 @@ import hashlib
import io import io
import sys import sys
import pycoin
from logbook import Logger, StreamHandler from logbook import Logger, StreamHandler
import prettytable import prettytable
@ -30,6 +29,7 @@ def show_rows(rows, field_names):
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--host', default='localhost')
parser.add_argument('--network', default='mainnet') parser.add_argument('--network', default='mainnet')
parser.add_argument('address', nargs='+') parser.add_argument('address', nargs='+')
args = parser.parse_args() args = parser.parse_args()
@ -46,7 +46,7 @@ def main():
else: else:
raise ValueError(f"unknown network: {args.network}") raise ValueError(f"unknown network: {args.network}")
hostport = ('localhost', port) hostport = (args.host, port)
log.info('connecting to {}:{}', *hostport) log.info('connecting to {}:{}', *hostport)
conn = client.Client(hostport) conn = client.Client(hostport)

View File

@ -1,4 +1,18 @@
#!/bin/bash #!/bin/bash
set -eu set -eu
cd `dirname $0`
.env/bin/python history.py $* cd -- "$( dirname -- "${BASH_SOURCE[0]}" )"
venv=false
if [ "$1" = "--venv" ];
then
shift
venv=true
fi
if [ $venv = true ] && [ ! -d .venv ]; then
virtualenv .venv
.venv/bin/pip install pycoin logbook prettytable
fi
exec .venv/bin/python history.py "$@"

View File

@ -4,10 +4,11 @@ import client
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--host', default='localhost')
parser.add_argument("txid") parser.add_argument("txid")
args = parser.parse_args() args = parser.parse_args()
conn = client.Client(("localhost", 50001)) conn = client.Client((args.host, 50001))
tx, = conn.call([client.request("blockchain.transaction.get", args.txid, True)]) tx, = conn.call([client.request("blockchain.transaction.get", args.txid, True)])
requests = [] requests = []
for vin in tx["vin"]: for vin in tx["vin"]:

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import hashlib import hashlib
import sys import sys
@ -10,11 +11,20 @@ import client
log = Logger("xpub") log = Logger("xpub")
def main(): def main():
conn = client.Client(('localhost', 50001)) parser = argparse.ArgumentParser()
xpub, = sys.argv[1:] parser.add_argument('--host', default='localhost')
parser.add_argument('xpub')
args = parser.parse_args()
conn = client.Client((args.host, 50001))
total = 0 total = 0
xpub = network.parse.bip32_pub(xpub) xpub = network.parse.bip32(args.xpub)
if xpub is None:
log.error('Invalid BIP32 pub key %s' % args.xpub)
sys.exit(1)
for change in (0, 1): for change in (0, 1):
empty = 0 empty = 0

View File

@ -476,10 +476,10 @@ $ echo '{"jsonrpc": "2.0", "method": "server.version", "params": ["", "1.4"], "i
For more complex tasks, you may need to convert addresses to For more complex tasks, you may need to convert addresses to
[script hashes](https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-basics.html#script-hashes) - see [script hashes](https://electrumx-spesmilo.readthedocs.io/en/latest/protocol-basics.html#script-hashes) - see
[contrib/addr.py](https://github.com/romanz/electrs/blob/master/contrib/addr.py) for getting an address balance and history: [contrib/history.py](https://github.com/romanz/electrs/blob/master/contrib/history.py) for getting an address balance and history:
``` ```
$ ./contrib/history.sh 144STc7gcb9XCp6t4hvrcUEKg9KemivsCR $ ./contrib/history.sh --venv 144STc7gcb9XCp6t4hvrcUEKg9KemivsCR
[2021-08-18 13:56:40.254317] INFO: electrum: connecting to localhost:50001 [2021-08-18 13:56:40.254317] INFO: electrum: connecting to localhost:50001
[2021-08-18 13:56:40.574461] INFO: electrum: subscribed to 1 scripthashes [2021-08-18 13:56:40.574461] INFO: electrum: subscribed to 1 scripthashes
[2021-08-18 13:56:40.645072] DEBUG: electrum: 0.00000 mBTC (total) [2021-08-18 13:56:40.645072] DEBUG: electrum: 0.00000 mBTC (total)