How to generate Bitcoin private - public keys with ECDSA module?

2

Pretty same question was asked here, but maybe there is a way to generate keys using ecdsa module in just a few lines of code?

Sergey Potekhin

Posted 2016-11-10T16:04:09.433

Reputation: 285

Answers

3

Yes:

import ecdsa
print(ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1).to_string())

Nick ODell

Posted 2016-11-10T16:04:09.433

Reputation: 26 536

Thanks for answer! So, when I'm runnnig your code, I'm getting something like b'\xe9+\xf7\x97A\...... How can I convert it to a usual string? str() and .decode('utf-8') don't work :(Sergey Potekhin 2016-11-10T18:44:30.123

1It's a bytes type. Interpreting it as ascii or as UTF-8 doesn't make any sense. Are you trying to convert to hex, or are you trying to print raw bytes to the terminal?Nick ODell 2016-11-10T18:51:19.363

Well, I mean that usually ECDSA private key looks like 18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725 according to this. So, how can I get an address which looks the same?

Sergey Potekhin 2016-11-10T20:45:48.990

*a key which looks the sameSergey Potekhin 2016-11-10T21:00:56.840

1Use binascii.hexlify(key.to_string()).decode('ascii').upper()Nick ODell 2016-11-10T22:53:53.267