pynacl icon indicating copy to clipboard operation
pynacl copied to clipboard

Going from a string representation from a privatekey to a public key

Open BlackTurtle123 opened this issue 4 years ago • 1 comments

I am trying to create a library for some crypto project. They however require to be able to generate the public key, and the address from a privatekey string (which users would input), how is this done? I don't see any option for this currently is this right?

privKey = SigningKey(accountSeedHash) pubKey = privKey.verify_key self.privateKey = base58.b58encode(privKey._signing_key)

This self.privateteKey has a value in the test case from '5rUDHu3Kf8LaLeW2Z2KJsLFMj3b8qzbJyn3GDxGJcxo8XDP1tj16aggSmdoQfVf45QB3uSby2J54Wbe3iKffVCxZ'

Now I would like to go from this value to the public key, in our test case: 'FinzVngQspEkHZSWn5BPJ5PSMJQjNW7oonqfHFXurgXs'

How would one do this?

BlackTurtle123 avatar Nov 06 '20 19:11 BlackTurtle123

If you only need the public key you can do it like this:

import base58
import nacl.signing
import nacl.bindings

signing_key_b58 = "5rUDHu3Kf8LaLeW2Z2KJsLFMj3b8qzbJyn3GDxGJcxo8XDP1tj16aggSmdoQfVf45QB3uSby2J54Wbe3iKffVCxZ"
signing_key_bytes = base58.b58decode(signing_key_b58)

verify_key_bytes = nacl.bindings.crypto_sign_ed25519_sk_to_pk(signing_key_bytes)
verify_key = nacl.signing.VerifyKey(verify_key_bytes)
print(base58.b58encode(verify_key._key))

If you need the SigningKey object as well, you need to first extract the seed from the private key:

seed = nacl.bindings.crypto_sign_ed25519_sk_to_seed(signing_key_bytes)
signing_key = nacl.signing.SigningKey(seed)
print(base58.b58encode(signing_key._signing_key))

You can read the relevant libsodium docs here: https://doc.libsodium.org/public-key_cryptography/public-key_signatures

And the PyNaCl docs here: https://pynacl.readthedocs.io/en/latest/signing/

jakob avatar Dec 11 '20 07:12 jakob