Separate Electrum client helper from actual tools

This commit is contained in:
Roman Zeyde 2018-08-14 09:19:55 +03:00
parent a3f43643f3
commit 123e0cf23b
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB
3 changed files with 66 additions and 47 deletions

19
tools/addr.py Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import hashlib
import sys
from pycoin.coins.bitcoin.networks import BitcoinMainnet
import client
def main():
conn = client.Connection(('localhost', 50001))
addr, = sys.argv[1:]
script = BitcoinMainnet.ui.script_for_address(addr)
script_hash = hashlib.sha256(script).digest()[::-1].hex()
res = conn.call('blockchain.scripthash.get_balance', script_hash)['result']
print('{} has {} satoshis'.format(addr, res))
if __name__ == '__main__':
main()

View File

@ -1,19 +1,6 @@
import hashlib
import json
import sys
from logbook import Logger, StreamHandler
from pycoin.coins.bitcoin.networks import BitcoinMainnet
import pycoin.ui.key_from_text
import pycoin.key
import socket
script_for_address = BitcoinMainnet.ui.script_for_address
log = Logger(__name__)
class Connection:
def __init__(self, addr):
self.s = socket.create_connection(addr)
@ -29,37 +16,3 @@ class Connection:
msg = json.dumps(req) + '\n'
self.s.sendall(msg.encode('ascii'))
return json.loads(self.f.readline())
def main():
conn = Connection(('localhost', 50001))
xpub, = sys.argv[1:]
total = 0
k = pycoin.ui.key_from_text.key_from_text(xpub)
for change in (0, 1):
empty = 0
for n in range(100):
address = k.subkey(change).subkey(n).address()
script = script_for_address(address)
script_hash = hashlib.sha256(script).digest()
log.debug('{}', conn.call('blockchain.scripthash.get_history',
script_hash[::-1].hex()))
reply = conn.call('blockchain.scripthash.get_balance',
script_hash[::-1].hex())
result = reply['result']
confirmed = result['confirmed'] / 1e8
total += confirmed
if confirmed:
log.info('{}/{} => {} has {:11.8f} BTC',
change, n, address, confirmed)
empty = 0
else:
empty += 1
if empty >= 10:
break
log.info('total balance: {} BTC', total)
if __name__ == '__main__':
with StreamHandler(sys.stderr, level='INFO').applicationbound():
main()

47
tools/xpub.py Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env python3
import hashlib
import sys
from logbook import Logger, StreamHandler
from pycoin.coins.bitcoin.networks import BitcoinMainnet
import pycoin.ui.key_from_text
import pycoin.key
import client
script_for_address = BitcoinMainnet.ui.script_for_address
log = Logger(__name__)
def main():
conn = client.Connection(('localhost', 50001))
xpub, = sys.argv[1:]
total = 0
k = pycoin.ui.key_from_text.key_from_text(xpub)
for change in (0, 1):
empty = 0
for n in range(100):
address = k.subkey(change).subkey(n).address()
script = script_for_address(address)
script_hash = hashlib.sha256(script).digest()[::-1].hex()
log.debug('{}', conn.call('blockchain.scripthash.get_history',
script_hash))
reply = conn.call('blockchain.scripthash.get_balance', script_hash)
result = reply['result']
confirmed = result['confirmed'] / 1e8
total += confirmed
if confirmed:
log.info('{}/{} => {} has {:11.8f} BTC',
change, n, address, confirmed)
empty = 0
else:
empty += 1
if empty >= 10:
break
log.info('total balance: {} BTC', total)
if __name__ == '__main__':
with StreamHandler(sys.stderr, level='INFO').applicationbound():
main()