Search code examples
pythonecdsa

Can only concatenate str (not "bytes") to str error on verifying key of signing key


I am trying to follow this excellent video that unfortunately has the script written for python 2. I have the following code:

#!/usr/bin/env python3

import codecs
import ecdsa

key = "bbe5b8853f51285a62f1276b72a162365f0a51c607e38cecb9bcffdf7284b2bc"

keyhex = codecs.decode(key, 'hex')

sk = ecdsa.SigningKey.from_string(keyhex, curve = ecdsa.SECP256k1)

vk = sk.get_verifying_key().to_string()

pk = '\x04' + vk

The error I get is:

Traceback (most recent call last):
  File ".../sh.key-info", line 30, in <module>
    pk = '\x04' + vk
TypeError: can only concatenate str (not "bytes") to str

I thought to_string() would convert the verifying key to string, but apparently not? How can I make this code work properly in 3.x?


Solution

  • To quote the ecdsa PyPI page:

    Note: while the methods are called to_string() the type they return is actually bytes, the "string" part is leftover from Python 2.

    To fix your error, I would recommend adding a bytes object rather than converting everything to strings.

    pk = b'\x04' + vk