Skip to content
This repository has been archived by the owner on Aug 9, 2018. It is now read-only.

python3 #11

Open
philipperemy opened this issue Aug 11, 2017 · 8 comments
Open

python3 #11

philipperemy opened this issue Aug 11, 2017 · 8 comments

Comments

@philipperemy
Copy link

Does this library support python3?

@philipperemy
Copy link
Author

Because I'm having this error:

c = 'hello'
k = 'k'
s = 's'

pprint(api.account_balance(c, k, s))
Traceback (most recent call last):
  File "/Users/philipperemy/PycharmProjects/bitstamp/hello.py", line 19, in <module>
    pprint(api.account_balance(c, k, s))
  File "/Users/philipperemy/venv/lib/python3.6/site-packages/bitstampy/api.py", line 40, in account_balance
    calls.APIAccountBalanceCall(client_id, api_key, api_secret)
  File "/Users/philipperemy/venv/lib/python3.6/site-packages/bitstampy/calls.py", line 81, in call
    self.api_secret, msg=message, digestmod=hashlib.sha256)
  File "/Users/philipperemy/venv/lib/python3.6/hmac.py", line 144, in new
    return HMAC(key, msg, digestmod)
  File "/Users/philipperemy/venv/lib/python3.6/hmac.py", line 42, in __init__
    raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__)
TypeError: key: expected bytes or bytearray, but got 'str'

Can you please advice? Thanks!

@philipperemy
Copy link
Author

philipperemy commented Aug 11, 2017

In calls.py:

    def call(self, **params):
        nonce = self._get_nonce()
        message = nonce + self.client_id + self.api_key
        signature = hmac.new(bytes(self.api_secret.encode('utf8')),
                             msg=bytes(message.encode('utf8')),
                             digestmod=hashlib.sha256)
        signature = signature.hexdigest().upper()
        params.update({
            'key': self.api_key, 'signature': signature, 'nonce': nonce
        })
        return super(APIPrivateCall, self).call(**params)

Just for information I changed this function and it works. Not confident if it would work for python2 so I prefer to ask you before sending a PR.

Thanks

@AdityaNayak
Copy link

Worked fine on 3 for me so far. Perhaps it was a function specific bug.

@unwitting
Copy link
Owner

@philipperemy what exactly did you change? Was it a problem with sending the output to pprint? If it's small, might be easiest to submit as a change and I can have a look.

@philipperemy
Copy link
Author

@unwitting I converted the arguments from str to bytes when calling hmac.new.

self.api_secret to bytes(self.api_secret.encode('utf8'))
message to bytes(message.encode('utf8'))

@unwitting
Copy link
Owner

Seems OK :) send an MR?

@KevinLefevre
Copy link

Encountered the same problem on python 3.6

@unwitting
Copy link
Owner

Would somebody be able to try the code in #11 (comment) locally and submit as an MR if it's good please?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants