Coder Social home page Coder Social logo

python-bitcoinlib's Introduction

python-bitcoinlib

This Python3 library provides an easy interface to the bitcoin data structures and protocol. The approach is low-level and "ground up", with a focus on providing tools to manipulate the internals of how Bitcoin works.

"The Swiss Army Knife of the Bitcoin protocol." - Wladimir J. van der Laan

Requirements

sudo apt-get install libssl-dev

The RPC interface, bitcoin.rpc, should work with Bitcoin Core v24.0 or later. Older versions may work but there do exist some incompatibilities.

Structure

Everything consensus critical is found in the modules under bitcoin.core. This rule is followed pretty strictly, for instance chain parameters are split into consensus critical and non-consensus-critical.

bitcoin.core            - Basic core definitions, datastructures, and
                          (context-independent) validation
bitcoin.core.key        - ECC pubkeys
bitcoin.core.script     - Scripts and opcodes
bitcoin.core.scripteval - Script evaluation/verification
bitcoin.core.serialize  - Serialization

In the future the bitcoin.core may use the Satoshi sourcecode directly as a library. Non-consensus critical modules include the following:

bitcoin          - Chain selection
bitcoin.base58   - Base58 encoding
bitcoin.bloom    - Bloom filters (incomplete)
bitcoin.net      - Network communication (in flux)
bitcoin.messages - Network messages (in flux)
bitcoin.rpc      - Bitcoin Core RPC interface support
bitcoin.wallet   - Wallet-related code, currently Bitcoin address and
                   private key support

Effort has been made to follow the Satoshi source relatively closely, for instance Python code and classes that duplicate the functionality of corresponding Satoshi C++ code uses the same naming conventions: CTransaction, CBlockHeader, nValue etc. Otherwise Python naming conventions are followed.

Mutable vs. Immutable objects

Like the Bitcoin Core codebase CTransaction is immutable and CMutableTransaction is mutable; unlike the Bitcoin Core codebase this distinction also applies to COutPoint, CTxIn, CTxOut, and CBlock.

Endianness Gotchas

Rather confusingly Bitcoin Core shows transaction and block hashes as little-endian hex rather than the big-endian the rest of the world uses for SHA256. python-bitcoinlib provides the convenience functions x() and lx() in bitcoin.core to convert from big-endian and little-endian hex to raw bytes to accommodate this. In addition see b2x() and b2lx() for conversion from bytes to big/little-endian hex.

Module import style

While not always good style, it's often convenient for quick scripts if import * can be used. To support that all the modules have __all__ defined appropriately.

Example Code

See examples/ directory. For instance this example creates a transaction spending a pay-to-script-hash transaction output:

$ PYTHONPATH=. examples/spend-p2sh-txout.py
<hex-encoded transaction>

Selecting the chain to use

Do the following:

import bitcoin
bitcoin.SelectParams(NAME)

Where NAME is one of 'testnet', 'mainnet', 'signet', or 'regtest'. The chain currently selected is a global variable that changes behavior everywhere, just like in the Satoshi codebase.

Unit tests

Under bitcoin/tests using test data from Bitcoin Core. To run them:

python3 -m unittest discover

Alternately, if Tox (see https://tox.readthedocs.org/) is available on your system, you can run unit tests for multiple Python versions:

./runtests.sh

HTML coverage reports can then be found in the htmlcov/ subdirectory.

Documentation

Sphinx documentation is in the "doc" subdirectory. Run "make help" from there to see how to build. You will need the Python "sphinx" package installed.

Currently this is just API documentation generated from the code and docstrings. Higher level written docs would be useful, perhaps starting with much of this README. Pages are written in reStructuredText and linked from index.rst.

python-bitcoinlib's People

Contributors

alecalve avatar amiller avatar derrend avatar dgpv avatar elichai avatar f1qwase avatar flowdalic avatar genme avatar icook avatar jswambo avatar kaniini avatar kanzure avatar luke-jr avatar mrvdb avatar ottoallmendinger avatar petertodd avatar pierrerochard avatar psgreco avatar pstratem avatar rayrapetyan avatar rubensayshi avatar runeksvendsen avatar sarchar avatar sartoness avatar simonmulser avatar timgates42 avatar welshjf avatar windsok avatar x89 avatar ysangkok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-bitcoinlib's Issues

Unit tests are segfaulting

Running python -m unittest discover in bitcoin/tests results in a segmentation fault. I'm having trouble tracking down the issue here.

It occurs within test_wallet.py.Test_CBitcoinSecret, seemingly due to an issue with CBitcoinSecret.from_secret_bytes in wallet.py. But that's about all I know.

bitcoin.rpc.Proxy.gettransaction() is broken

bitcoin/rpc.py:486

try:
  r = self._call('gettransaction', b2lx(txid))
except JSONRPCError as ex:
  ...

Why is that b2lx call there? txid is already a hex-encoded hash, so this call is broken.

Bignum handling is dodgy

Doesn't appear we handle bignums > 32bits correctly, and the code itself is uncommented, undocumented, and doesn't handle errors in any sane way.

How to add signed certificates to a bitcoin bip70 payment message?

I've been fishing around on the internet with no luck so far :(
My original questions first posted on stackoverflow -
http://stackoverflow.com/questions/25716130/how-to-add-signed-certificates-to-a-bitcoin-bip70-payment-message-python
http://stackoverflow.com/questions/25734945/how-to-sign-a-message-with-my-https-public-key-certificate/25737436?noredirect=1#comment40246695_25737436

Below are my problem areas -

message PaymentRequest {
    optional bytes pki_data = 3;
    optional bytes signature = 5;
}

message X509Certificates {
    repeated bytes certificate = 1;
}

I got myself a comodo ssl cert:

AddTrustExternalCARoot.crt
mywebsite_com.crt
COMODORSAAddTrustCA.crt
COMODORSADomainValidationSecureServerCA.crt

The developer docs state:
"digital signature over a hash of the protocol buffer serialized variation of the PaymentRequest message, with all serialized fields serialized in numerical order and signed using the public key in pki_data."

But how is a digest signed using the public key from an ssl cert and how is it secure to use public key signature?

Also I'm not 100% sure of the the correct method to achieve a secure payment recognised by the bitcoin client (the displayed payment turning green rather than yellow) so any help there would be great :)

Below is the portion of code I am dealing with:

cert = open('/etc/nginx/ssl/bundle.der', 'rb').read()

xco = o.X509Certificates()
xco.certificate.append(cert)

pro = o.PaymentRequest()
pro.pki_type = 'x509+sha256'
pro.pki_data = xco.SerializeToString()
pro.serialized_payment_details = pdo.SerializeToString()
pro.signature = ''
pro.signature = Popen(['bitcoin-cli', 'signmessage', btc_address, hashlib.sha256(pro.serialized_payment_details).digest()],stdout=PIPE).communicate()[0].strip()

It occurred to me that bitcoin addresses can be used to sign messages so I'm attempting to do just that in the signature field but I don't know if this is correct.

I concatenated my certs into a .der bundle and serialised it rather than reading them in one at a time, is this ok?

Also I notice in the paymentrequest.proto example the X509Certificates message comes after the PaymentRequest message though it seems to be instantiated and set first, does that mean that my code is not correctly ordered somehow?

some RPC functions cannot manage hexadecimal inputs and outputs well

the test code is:
image

and the output is:
image

In line 101 and 102, hexadecimal values are delivered, but i didn't found that the input must be proc by the lx() function until i cost some time to debug it.

Undet shell, the input bellow runs well:

$ bitcoin-cli.exe -datadir="D:\Program Files\BitcoinData" getrawtransaction 330b926aaced01d1121e4f833c435b8a40a7e1da4df48a3adb55403aa941a264 1

Why not make these functions call just like offical RPCs?

And the value of the key 'blockhash' in the output dict is not well presented, it should looks like "00000000000000001a93726f7f90f9ba1de8c5a569d69f0a33c7b8d7e296f78b"
according to the official RPC.

bitcoin.params [bug]?

root@redwood:/etc/znc/modules# cat test.py

import bitcoin.rpc
txid = '55374e78c86bb739c0ef69f92e3f0cfa9b233e293426170c65a2fe381fd54593'
proxy = **bitcoin.rpc.Proxy(btc_conf_file='/etc/znc/bitcoin.conf')**

root@redwood:/etc/znc/modules# python3.5 test.py

Traceback (most recent call last):
File "test.py", line 3, in
proxy = bitcoin.rpc.Proxy(btc_conf_file='/etc/znc/bitcoin.conf')
File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 302, in init
**kwargs)
File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 157, in init
service_port = bitcoin.params.RPC_PORT
AttributeError: module 'bitcoin' has no attribute 'params'


Params is defined here:
https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/__init__.py#L62
but, bitcoin.rpc DOES contain "import bitcoin"
https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/rpc.py#L45

issue

return http_response_object

wheel installation failure with setuptools and install_requires

Not sure why yet, but python-bitcoinlib==0.5.1 can't be installed via install_requires. Non-wheel packages (e.g. 0.3.0) work fine.

Error

Installed /Users/myuser/code/pyb/venv/lib/python3.5/site-packages/UNKNOWN-0.0.0-py3.5.egg
Processing dependencies for UNKNOWN==0.0.0
Searching for python-bitcoinlib==0.5.1
Reading https://pypi.python.org/simple/python-bitcoinlib/
No local packages or working download links found for python-bitcoinlib==0.5.1
error: Could not find suitable distribution for Requirement.parse('python-bitcoinlib==0.5.1')

Environment

setuptools==25.2.0
python==3.5.2

Reproduction steps

  • create setup.py:
from setuptools import setup

setup(
    install_requires=[
        'python-bitcoinlib==0.5.1'
    ]
)
  • python3 setup.py develop or python3 setup.py install

gettransaction in testnet

My code:
`import bitcoin.rpc
bitcoin.SelectParams("testnet")

proxy_connection = bitcoin.rpc.Proxy("http://removed:[email protected]:8332")
print(proxy_connection.gettransaction("269b22e789089b3acfef5f118a5b2c9856a70833756594c2c2a16f070b830147"))
`

Traceback (most recent call last):
File "1.py", line 5, in
print(proxy_connection.gettransaction("269b22e789089b3acfef5f118a5b2c9856a70833756594c2c2a16f070b830147"))
File "/usr/local/lib/python2.7/dist-packages/bitcoin/rpc.py", line 537, in gettransaction
(self.class.name, ex.error['message'], ex.error['code']))
IndexError: Proxy.getrawtransaction(): Invalid or non-wallet transaction id (-5)

But this is valid transaction in my wallet:

/bitcoin-cli -rpcport=8332 -rpcuser=removed -rpcpassword=removed -rcphost=127.0.0.1 gettransaction 269b22e789089b3acfef5f118a5b2c9856a70833756594c2c2a16f070b830147

{
"amount": 0.32500000,
"confirmations": 13,
"blockhash": "000000000000096e471b9be59549bf8727a503bb3d523251401e39e0784f0fc7",
"blockindex": 4,
"blocktime": 1476547966,
"txid": "269b22e789089b3acfef5f118a5b2c9856a70833756594c2c2a16f070b830147",
"walletconflicts": [
],
"time": 1476544357,
"timereceived": 1476544357,
"bip125-replaceable": "no",
"details": [
{
"account": "",
"address": "mwwx3cCbQ7dkebnQvJXmQz9kXmsinZmAuG",
"category": "receive",
"amount": 0.32500000,
"label": "",
"vout": 0
}
],
"hex": "0100000001ea91ed302099e8d70642c99a1a37d76622ce0aad3dcff5210be59c551e086d55010000006a47304402206257f8907983096d242f29f7f71b88d48fe131a5151f21214b49818b1fb3620e02202818150ed9a2ed0edd3635094cb344c81a09f4bb970627d2100d9cfc9ed862f4012102b42288db695d2535dd79e98ada6a1a4d474a158f68056bdd7ce39cff57021f4effffffff0220e9ef01000000001976a914b43c34239cefd21d09110bb5769611af4fe3881688acd0570a0a000000001976a914dc0be39384c137dc8f310594c8db19705b1bf14f88ac00000000"
}

I believe there is some buggy check that decides the txid is not valid... I have already done some test and my conclusion is the bug is located in the lib as my code is pretty simple.

Uploading python-bitcoinlib to pypi

Hey, have you considered uploading this lib to pypi? I'd like to do it if you don't mind (and if the last fixes pushed are included, it might involve a version bump from 0.1 to 0.1.1 maybe?)

Dead code?

Is VALID_OPCODES used for anything? I imagined it's intended purpose was script verification of some kind, but it's not used anywhere from what I can tell.

Sorry for the issue spam, but I wasn't sure of a better place to ask.

transaction deserialization fails

Attempting to deserialize the raw testnet transaction fails in

proxy.getrawtransaction('45aef1dd64639f40ddcb5a1a0e4f78f4bb690f0ebd3fa006b21111711dfc1050')

Specifically deserializing
45aef1dd64639f40ddcb5a1a0e4f78f4bb690f0ebd3fa006b21111711dfc1050 with

CTransaction.deserialize(x('020000000001019f8ddb5d17be8c0ac3c2d26ad1818f5e879b2357e433009bb79817b319cc87040000000017160014aed670ec9a96f5f2bd6df6de7a54f165a02db67affffffff01d8e53c00000000001976a914f1921908f63d4f87d9551dd425fbea0c8edfeaaf88ac0247304402207de9a112c3e43534d5ac88f168bcbdff3fd26410681975ab34c15980fcf17fe502204dab21daae0fe600646c2d7a0a19eb30c84080cfc1bbe60886b768dc9130c6aa01210279f92b67674cba018f8f5ba64b69d3d931786d7d7f39dbda6ddce04a3245af0600000000'))

fails with

bitcoin.core.serialize.DeserializationExtraDataError: Not all bytes consumed during deserialization

Cannot deserialize 13575b3cfd90f38c90b490624be3cdc89ba2fb4616dbcd2782a093495993cfa3 from testnet

Using the current master @ 2e4d51a, I am not able to get CTransaction.deserialize to return the expected txid as shown below.

The expected txid should be 13575b3cfd90f38c90b490624be3cdc89ba2fb4616dbcd2782a093495993cfa3 but python-bitcoinlib is returning 50492dffaa576f61788cc0cd38c7e5bdeff29ebf2f04b2768cc1cd993978ac65.

Test case:

>>> from bitcoin import SelectParams
>>> from bitcoin.core import b2lx
>>> from bitcoin.core import CTransaction
>>> from bitcoin.core import x
>>> SelectParams('testnet')

>>> hexstr = "010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff2f035fa40d00044e157957044980b5030c7a8e775740030000000000000a636b706f6f6c0b2f416e7453747265616d2fffffffff0250fd5d13000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9ed9ef46e6c6ab6dc2b94a365b1bb12f8e1319090a7d260df3cc1d37ce0c29ce7c80120000000000000000000000000000000000000000000000000000000000000000000000000"
>>> tx = CTransaction.deserialize(x(hexstr), allow_padding=True)
>>> b2lx(tx.GetHash())
'50492dffaa576f61788cc0cd38c7e5bdeff29ebf2f04b2768cc1cd993978ac65'

Reference:
https://testnet.smartbit.com.au/tx/13575b3cfd90f38c90b490624be3cdc89ba2fb4616dbcd2782a093495993cfa3
https://testnet.smartbit.com.au/tx/13575b3cfd90f38c90b490624be3cdc89ba2fb4616dbcd2782a093495993cfa3/hex

Network message serialization

Need to look into this part of the API and check the design. For instance bad messages raise ValueError exceptions right now rather than a specific serialization error, making error handling code tricky.

Transaction compiler

many thanks for this work. I moved to something else, otherwise would have liked to contributed more. some things can be seen here, but I couldn't merge them anywhere, and abandoned the effort: https://github.com/LykkeCity/Notary/tree/master/glue, also because upstream moved to NBitcoin exclusively.

The idea was to have something like a transaction compiler or builder to provide an easy to use interface for user-space. The problem is that bitcoin-core requires tremendous amount of expertise to compile transactions. Currently advanced transactions are used only by handful of experts in the world. python-bitcoinlib could be an intermediary layer, but its not quite that (not its purpose probably in any case). Referencing previous inputs alone is not trivial. To move some money one needs to know "where" that money is stored. To arrive at some code which in 3 lines of code moves money from A to B, seems to be not really possible and the way I see it now requires redesign of the entire stack as it relates to UTXO way of accounting itself. NBitcoin to some extent allows to do that with the transaction builder.

some RPC functions cannot manage hexadecimal inputs and outputs well

the test code is:
image

and the output is:
image

In line 101 and 102, hexadecimal values are delivered, but i didn't found that the input must first be proc by the lx() function until i cost some time to debug it.

however in cmd line, the input bellow runs well:

$ bitcoin-cli.exe -datadir="D:\Program Files\BitcoinData" getrawtransaction 330b926aaced01d1121e4f833c435b8a40a7e1da4df48a3adb55403aa941a264 1

Why not make these functions call just like offical RPCs?

And the value of the key 'blockhash' in the output dict is not well presented, it should looks like "00000000000000001a93726f7f90f9ba1de8c5a569d69f0a33c7b8d7e296f78b"
according to the official RPC.

Some testnet blocks larger than MAX_SIZE

Testnet blocks such as blocks 1060697, 1060695, and 1060694 can't be deserialized because their size exceeds MAX_SIZE.

bitcoin.core.serialize.SerializationError: Asked to read 0x19abb703ff bytes; MAX_SIZE exceeded

This error occurs through rpc when trying proxy.getblock(proxy.getbestblockhash()).

Different return types for getblockhash, getbestblockhash

Confusingly, bitcoin.rpc.Proxy.getblockhash returns raw bytes, while getbestblockhash returns the hex string, as there's no wrapper implemented for the latter. And there's probably more like this, since as noted, object conversion is not fully implemented.

I guess the question is how much risk there is to existing code of fixing it now.

Perhaps, with a sufficiently major version bump to warn of the API breakage, we should get rid of the magic __getattr__ in Proxy and de-underscore _call. Thus, only explicitly wrapped RPCs can be called directly, while others can be called such as proxy.call("getbestblockhash"). Ugly in that it kinda breaks the logic of calling it a "proxy" if you have to make such calls, but it allows new wrappers to be implemented compatibly.

Return argument for VerifyScript

Would be nice if VerifyScript would return true if its valid, i.e. to have a function call which returns true or false (instead of throwing an error).

pip version

The python-bitcoinlib version on pip is updated?
Name: python-bitcoinlib
Version: 0.2.2-SNAPSHOT
Location: /usr/local/lib/python2.7/dist-packages/python_bitcoinlib-0.2.2_SNAPSHOT-py2.7.egg

Thanks!

Support .cookie authentication

It would be nice if .cookie authentication were supported in rpc.py BaseProxy, instead of erroring out if no rpcpassword can be found in the config file:

if 'rpcpassword' not in conf:
    raise ValueError('The value of rpcpassword not specified in the configuration file: %s' % btc_conf_file)

signrawtransaction throws struct.error Exception

Hi, when I run this line:

rpc = bitcoin.rpc.Proxy()
rpc.signrawtransaction(orig_transaction)

I get this exception:

Traceback (most recent call last):
  File "PycharmProjects/a.py", line 185, in <module>
    main()
  File "PycharmProjects/a.py", line 160, in main
    r = rpc.signrawtransaction(orig_transaction)
  File "~/.local/lib/python2.7/site-packages/bitcoin/rpc.py", line 630, in signrawtransaction
    hextx = hexlify(tx.serialize())
  File "~/.local/lib/python2.7/site-packages/bitcoin/core/serialize.py", line 102, in serialize
    self.stream_serialize(f)
  File "~/.local/lib/python2.7/site-packages/bitcoin/core/__init__.py", line 336, in stream_serialize
    VectorSerializer.stream_serialize(CTxIn, self.vin, f)
  File "~/.local/lib/python2.7/site-packages/bitcoin/core/serialize.py", line 248, in stream_serialize
    inner_cls.stream_serialize(obj, f)
  File "~/.local/lib/python2.7/site-packages/bitcoin/core/__init__.py", line 206, in stream_serialize
    f.write(struct.pack(b"<I", self.nSequence))
struct.error: cannot convert argument to integer

Running CECKey.set_secretbytes twice causes an exception the second time

Running CECKey.set_secretbytes twice causes an exception the second time. Easy to repro:

import bitcoin.wallet

bitcoin.SelectParams('regtest')

private_key = bitcoin.wallet.CBitcoinSecret('cUgetboAcBhZyma6SvFHhefkw9nVnTpcpswePM5YRv2GqMjrqSgR')
private_key = bitcoin.wallet.CBitcoinSecret('cR2rg2NFZdWZ3jdAdLwaKCPhL5CTBVBfunRz2a8xuJkyBccceojj')

The stack trace is:

    private_key = bitcoin.wallet.CBitcoinSecret('cR2rg2NFZdWZ3jdAdLwaKCPhL5CTBVBfunRz2a8xuJkyBccceojj')
  File "...\lib\site-packages\bitcoin\wallet.py", line 238, in __init__
    CKey.__init__(self, self[0:32], len(self) > 32 and bord(self[32]) == 1)
  File "...\lib\site-packages\bitcoin\wallet.py", line 206, in __init__
    self._cec_key.set_secretbytes(secret)
  File "...\lib\site-packages\bitcoin\core\key.py", line 58, in set_secretbytes
    if not ssl.EC_POINT_mul(group, pub_key, priv_key, None, None, ctx):
OSError: exception: access violation reading 0x00007FFE0120F9C4

This happens on Windows, seems to work fine on Linux.

Request for more documentation on creating a payment with bitcoinlib

@petertodd , You probably have a lot to do, but I'm struggling with simple concepts such as payments. I came across your posting on StackExchange, and decided I would follow along, however, creating an instance of the Proxy class fails repeatedly for the same reason shown here:

In [1]: from bitcoin.core import COIN, b2lx

In [2]: import bitcoin.wallet

In [3]: import bitcoin.rpc

In [4]: rpc = bitcoin.rpc.Proxy()

In [5]: rpc
---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
<ipython-input-5-81db1fe346c1> in <module>()
----> 1 rpc

/home/vagrant/.virtualenvs/3/lib/python3.4/site-packages/IPython/core/displayhook.py in __call__(self, result)
    244             if display_method is not None:
    245                 try:
--> 246                     return display_method()
    247                 except NotImplementedError:
    248                     pass

/home/vagrant/.virtualenvs/3/lib/python3.4/site-packages/bitcoin/rpc.py in <lambda>(*args)
    158
    159         # Create a callable to do the actual call
--> 160         f = lambda *args: self._call(name, *args)
    161
    162         # Make debuggers show <function bitcoin.rpc.name> rather than <function

/home/vagrant/.virtualenvs/3/lib/python3.4/site-packages/bitcoin/rpc.py in _call(self, service_name, *args)
    140                              'User-Agent': USER_AGENT,
    141                              'Authorization': self.__auth_header,
--> 142                              'Content-type': 'application/json'})
    143
    144         response = self._get_response()

/usr/lib/python3.4/http/client.py in request(self, method, url, body, headers)
   1063     def request(self, method, url, body=None, headers={}):
   1064         """Send a complete request to the server."""
-> 1065         self._send_request(method, url, body, headers)
   1066
   1067     def _set_content_length(self, body):

/usr/lib/python3.4/http/client.py in _send_request(self, method, url, body, headers)
   1101             # default charset of iso-8859-1.
   1102             body = body.encode('iso-8859-1')
-> 1103         self.endheaders(body)
   1104
   1105     def getresponse(self):

/usr/lib/python3.4/http/client.py in endheaders(self, message_body)
   1059         else:
   1060             raise CannotSendHeader()
-> 1061         self._send_output(message_body)
   1062
   1063     def request(self, method, url, body=None, headers={}):

/usr/lib/python3.4/http/client.py in _send_output(self, message_body)
    904             msg += message_body
    905             message_body = None
--> 906         self.send(msg)
    907         if message_body is not None:
    908             # message_body was not a string (i.e. it is a file), and

/usr/lib/python3.4/http/client.py in send(self, data)
    839         if self.sock is None:
    840             if self.auto_open:
--> 841                 self.connect()
    842             else:
    843                 raise NotConnected()

/usr/lib/python3.4/http/client.py in connect(self)
    817         """Connect to the host and port specified in __init__."""
    818         self.sock = socket.create_connection((self.host,self.port),
--> 819                                              self.timeout, self.source_address)
    820         if self._tunnel_host:
    821             self._tunnel()

/usr/lib/python3.4/socket.py in create_connection(address, timeout, source_address)
    507
    508     if err is not None:
--> 509         raise err
    510     else:
    511         raise error("getaddrinfo returns an empty list")

/usr/lib/python3.4/socket.py in create_connection(address, timeout, source_address)
    498             if source_address:
    499                 sock.bind(source_address)
--> 500             sock.connect(sa)
    501             return sock
    502

ConnectionRefusedError: [Errno 111] Connection refused

In [6]:

bitcoind -regtest -daemon is running on the standard port:

$ lsof -i | grep -i bitcoin
bitcoind 14267 vagrant    6u  IPv6  29469      0t0  TCP *:18444 (LISTEN)
bitcoind 14267 vagrant    7u  IPv4  29470      0t0  TCP *:18444 (LISTEN)
bitcoind 14267 vagrant   22u  IPv6  29472      0t0  TCP ip6-localhost:18332 (LISTEN)
bitcoind 14267 vagrant   23u  IPv4  29473      0t0  TCP localhost:18332 (LISTEN)

I'm not really sure what to do from here. Is there any chance you could add a /docs directory with simple English explanations of how to use various parts of this library?

Error: File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None

Got this program:

import bitcoin.rpc

proxy_connection = bitcoin.rpc.RawProxy(service_url='http://127.0.0.1', service_port=44555)

print(proxy_connection.getnewaddress())

When I'm trying to execute it got error:

Traceback (most recent call last):
  File "electrum_to_clam.py", line 5, in <module>
    print(proxy_connection.getnewaddress())
  File "/usr/local/lib/python3.4/dist-packages/bitcoin/rpc.py", line 244, in <lambda>
    f = lambda *args: self._call(name, *args)
  File "/usr/local/lib/python3.4/dist-packages/bitcoin/rpc.py", line 186, in _call
    response = self._get_response()
  File "/usr/local/lib/python3.4/dist-packages/bitcoin/rpc.py", line 213, in _get_response
    parse_float=decimal.Decimal)
  File "/usr/lib/python3.4/json/__init__.py", line 331, in loads
    return cls(**kw).decode(s)
  File "/usr/lib/python3.4/json/decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 2 column 1 (char 1)

What may be the reason?

Intermittent `socket.timeout`

We're running into a lot of the following errors with our use of python-bitcoinlib in counterpartyd:

Traceback (most recent call last):
  File "C:\counterpartyd_build\dist\counterpartyd\counterpartyd.py", line 956, in <module>
    blocks.follow(db)
  File "C:\counterpartyd_build\dist\counterpartyd\lib\blocks.py", line 977, in follow
    mempool_tx_index = list_tx(db, None, block_index, curr_time, tx_hash, mempool_tx_index)
  File "C:\counterpartyd_build\dist\counterpartyd\lib\blocks.py", line 653, in list_tx
    source, destination, btc_amount, fee, data = get_tx_info(tx_dict['hex'], block_index)
  File "C:\counterpartyd_build\dist\counterpartyd\lib\blocks.py", line 341, in get_tx_info
    tx_info = get_tx_info2(tx_hex, block_parser=block_parser)
  File "C:\counterpartyd_build\dist\counterpartyd\lib\blocks.py", line 560, in get_tx_info2
    vin_ctx = proxy.getrawtransaction(vin.prevout.hash)
  File "C:\counterpartyd_build\env\lib\site-packages\bitcoin\rpc.py", line 307, in getrawtransaction
    r = self._call('getrawtransaction', b2lx(txid), 1 if verbose else 0)
  File "C:\counterpartyd_build\env\lib\site-packages\bitcoin\rpc.py", line 144, in _call
    response = self._get_response()
  File "C:\counterpartyd_build\env\lib\site-packages\bitcoin\rpc.py", line 179, in _get_response
    http_response = self.__conn.getresponse()
  File "C:\Python34\Lib\http\client.py", line 1172, in getresponse
    response.begin()
  File "C:\Python34\Lib\http\client.py", line 351, in begin
    version, status, reason = self._read_status()
  File "C:\Python34\Lib\http\client.py", line 313, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Python34\Lib\socket.py", line 371, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

Sometimes the problem was being caused by reusing a Proxy object, however, AFAIK, we've gotten rid of all of those cases and the error still shows up infrequently and intermittently. Any idea what could be causing this? Is it really the backend taking too long? (I've tried using both Bitcoin Core and btcd.) Should there just be a retry loop somewhere where there isn't? I've also tried socket.setdefaulttimeout(10). Should we be using python-bitcoinlib otherwise? (Should I just be talking to the socket or httplib teams?

`CBitcoinAddressError` when using testnet

Hi

I get the following error using testnet (whereas no pb using mainnet):

> p = bitcoin.rpc.Proxy()
> p.getnewaddress()
.../lib/python3.4/site-packages/bitcoin/rpc.py in getnewaddress(self, account)
    273             r = self._call('getnewaddress')
    274
--> 275         return CBitcoinAddress(r)
    276
    277     def getrawchangeaddress(self):

.../lib/python3.4/site-packages/bitcoin/base58.py in __new__(cls, s)
    108             raise Base58ChecksumError('Checksum mismatch: expected %r, calculated %r' % (check0, check1))
    109
--> 110         return cls.from_bytes(data, bord(verbyte[0]))
    111
    112     def __init__(self, s):

.../lib/python3.4/site-packages/bitcoin/wallet.py in from_bytes(cls, data, nVersion)
     48
     49         else:
---> 50            raise CBitcoinAddressError('Version %d not a recognized Bitcoin Address' % nVersion)
     51
     52         return self

CBitcoinAddressError: Version 111 not a recognized Bitcoin Address

Running on OS X.

Not sure about what's happening. Would be happy to help.

Prune the blockchain size

I wonder if there is a way that I can specify the prune options to cap the size of the blockchain, I am running this on a vps and 1/2 GB of space wont be enough for running this wallet.
I read that you can prune bitcoind to a desired target size. Is this supported by the library?

Please Fix Readme

This may be only my humble opinion, but I really don't understand the github trend of having the README unstyled purely as text and not in markdown. Is that suppose to be cool? It makes it much harder to read and comprehend. Markdown was "invented" for a reason!

Please keep it as it should be, it'd make it much easier for us to use your awesome library.

Thanks for your efforts :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.