Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BlackTurtle123 opened this issue Nov 6, 2020 · 1 comment

Comments

@BlackTurtle123
Copy link

BlackTurtle123 commented Nov 6, 2020

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?

@jakob
Copy link
Contributor

jakob commented Dec 11, 2020

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/

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

No branches or pull requests

2 participants