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

Is there a way to convert from hex to npub? #102

Open
plebiolira opened this issue May 18, 2023 · 2 comments
Open

Is there a way to convert from hex to npub? #102

plebiolira opened this issue May 18, 2023 · 2 comments

Comments

@plebiolira
Copy link

Sorry if it's a noob question. I'm looking for a way to input the hex for an event or profile, and convert it to the npub.

I've looked around the codebase quite a bit but couldn't figure it out.

Has it been implemented to python-nostr?

Thanks so much,

@weex
Copy link

weex commented May 20, 2023

At least on a PrivateKey object, you can use the bech32() method on the public_key attribute. Here's an example from a newly generated PrivateKey.

from nostr.key import PrivateKey

p = PrivateKey()
print(p.public_key.bech32())

@plebiolira
Copy link
Author

Thanks. I ended up using the hex_to_bech32 method from the monstr library, which I added to my python-nostr.

@staticmethod def hex_to_bech32(key_str: str, spec, prefix='npub'): as_int = [int(key_str[i:i+2], 16) for i in range(0, len(key_str), 2)] data = bech32.convertbits(as_int, 8, 5) return bech32.bech32_encode(prefix, data, spec)

Here's a link to the source.
https://github.com/monty888/monstr/blob/5eb21a437a8a640b38b972a6a4c15df55d3d04f9/monstr/encrypt.py#L82

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

No branches or pull requests

2 participants