vbuterin / pybitcointools

SImple, common-sense Bitcoin-themed Python ECC library
1.28k stars 857 forks source link

history() not working in python3 or python2 #110

Open priestc opened 8 years ago

priestc commented 8 years ago

This is the same as if I used ipython as if I used ipython3

$ sudo pip3 install -U pybitcointools
$ ipython3
>>> from bitcoin import history
>>> history('1PZ3Ps9RvCmUW1s1rHE25FeR8vtKUrhEai')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/bitcoin/bci.py in history(*args)
    168             try:
--> 169                 jsonobj = json.loads(data)
    170             except:

/usr/lib/python3.4/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):

TypeError: the JSON object must be str, not 'bytes'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-f52616f42edd> in <module>()
----> 1 history('1PZ3Ps9RvCmUW1s1rHE25FeR8vtKUrhEai')

/usr/local/lib/python3.4/dist-packages/bitcoin/bci.py in history(*args)
    169                 jsonobj = json.loads(data)
    170             except:
--> 171                 raise Exception("Failed to decode data: "+data)
    172             txs.extend(jsonobj["txs"])
    173             if len(jsonobj["txs"]) < 50:

TypeError: Can't convert 'bytes' object to str implicitly

I'm actually trying to build a separate python package that does exactly what history() is doing (calling an external block explorer to get utxo) I want my library to return data that is compatible with how this library's history() function works. But it appears that history() is broken? I tried updating to the latest version but the error persists.

wizardofozzie commented 8 years ago

See this PR

arhuaco commented 8 years ago

Oh, I came here to report something similar. This is not working for me and I tried Python 2 and Python 3:

h = bitcoin.history('192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab')

I don't know if this is relevant, but this address has received Counterparty transactions.

I tried another address ('191vGngyDnpznFBNaWsKy5FySqwAWEhTBQ') and it worked for me.

priestc commented 8 years ago

@arhuaco in the maintime, you can use moneywagon.get_unspent_outputs() in place of history() until the PR gets merged.

from moneywagon import get_unspent_outputs
In [2]: get_unspent_outputs('btc', '192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab')
Out[2]: 
[{'address': '192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab',
  'amount': 1000000,
  'confirmations': 114,
  'output': 'c121398b985c2a57451caf203b2ecc14c83369649dbbb8788ef8a6ff4c3321f8:1'},
 {'address': '192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab',
  'amount': 1000000,
  'confirmations': 114,
  'output': '65f85c0058b2f3d330346fce4217c35016284d1bbd500b622d128e1462c10ae1:5'},
 {'address': '192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab',
  'amount': 1000000,
  'confirmations': 114,
  'output': '6582a8cb67c87b5ee164456b6891410888b236f125e20a3e991403aa36240cec:3'},
 {'address': '192Tsb794VGi3VAJZ8zhHdjSRVFZpCywab',
  'amount': 6290,
  'confirmations': 4185,
  'output': 'e76055d1a7a1afa40731d49183b6194a617c53a92be6909b9aa33fe50699a380:1'}]