ever4st
ever4st
alashazam@iris.to
Feb 23, 2025

Nostr_sdk: derive npub from nsec

Using nostr_sdk fetching nsec and derving the npub

Derive npub from nsec given as parameter

import argparse
import asyncio
from nostr_sdk import Keys, init_logger, LogLevel

async def main(nsec):
    init_logger(LogLevel.INFO)
    keys = Keys.parse(nsec)
    public_key = keys.public_key()
    print(f"Public key (hex): {public_key.to_hex()}")
    print(f"Public key (npub): {public_key.to_bech32()}")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Fetch all relay from a given nsec')
    parser.add_argument('nsec', type=str, help='The nsec of the user')
    args = parser.parse_args()
    asyncio.run(main(args.nsec))

docs.rs/nostr_rust/latest/nostr_rust/keys/fn.get_public_key_from_secret.html

github.com/ev3rst/nostr_sdk_examples/blob/main/ns-derive-npub.py