Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.9.10: Add ElectrumClient #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pybitcoin/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from blockchain_info import BlockchainInfoClient
from chain_com import ChainComClient
from bitcoind import BitcoindClient, create_bitcoind_service_proxy
from electrum_client import ElectrumClient

import blockcypher
import blockchain_info
Expand Down
52 changes: 52 additions & 0 deletions pybitcoin/services/electrum_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Electrum client for pybitcoin
"""

import jsonrpclib

from ..transactions.serialize import deserialize_transaction


class ElectrumClient():
"""
Electrum Blockchain interface class
"""
def __init__(self, api_endpoint):
self.api_endpoint = api_endpoint
self.electrum = jsonrpclib.Server(self.api_endpoint)

def _format_unspents(self, unspents):
"""
Make electrum's output consumable by pybitcoin.
"""
# FIXME: This is off-by-one and behavior for unconfirmed is unknown,
# will leave out for now.
# daemon_status = self.electrum.daemon({'subcommand': 'status'})
# blockchain_height = daemon_status['blockchain_height']
formatted_unspents = []
for unspent in unspents:
tx_hash = unspent['tx_hash']
transaction = self.electrum.gettransaction(tx_hash)['hex']
deserialized_tx = deserialize_transaction(transaction)
script_hex = deserialized_tx[1][0]['script_hex']
# confirmations = blockchain_height - unspent['height']
confirmations = 0
# electrum's tx_hash is the big endian version already.
formatted_unspent = {'transaction_hash': unspent['tx_hash'],
'output_index': unspent['tx_pos'],
'value': unspent['value'],
'script_hex': script_hex,
'confirmations': confirmations}
formatted_unspents.append(formatted_unspent)
return formatted_unspents

def get_unspents(self, address):
unspents = self.electrum.getaddressunspent(address)
return self._format_unspents(unspents)

def broadcast_transaction(self, tx):
broadcast_output = self.electrum.broadcast(tx)
if broadcast_output[0] is False:
raise Exception(str(broadcast_output))
else:
return broadcast_output
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ utilitybelt>=0.2.1
python-bitcoinrpc==0.1
keychain>=0.1.4
bitcoin>=1.1.42
jsonrpclib-pelix
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='pybitcoin',
version='0.9.9',
version='0.9.10',
url='https://github.com/blockstack/pybitcoin',
license='MIT',
author='Blockstack Developers',
Expand All @@ -25,7 +25,8 @@
'utilitybelt>=0.2.6',
'python-bitcoinrpc==0.1',
'keychain>=0.1.4',
'bitcoin>=1.1.42'
'bitcoin>=1.1.42',
'jsonrpclib-pelix'
],
classifiers=[
'Intended Audience :: Developers',
Expand Down