Bitcoin to Nostr private key
July 17, 2025•72 words
# bitcoin private key to nostr
import bech32
import base58
import hashlib
# Uncompressed bitcoin private key in WIF format
private = "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF"
# Compressed
private = "L53fCHmQhbNp1B4JipfBtfeHZH7cAibzG9oK19XfiFzxHgAkz6JK"
decoded = base58.b58decode(private)
hex_key = decoded[1:-4] # remove version byte and 4 bytes checksum
if len(decoded) == 38: # when compressed
hex_key = decoded[1:-5]
data = bech32.convertbits(hex_key, 8, 5, True)
nsec = bech32.bech32_encode("nsec", data)
assert(nsec == "nsec1axrn67wxmp7up7m22auxxvuf73znyyes8knp7g9avl7zxw4rxf3q9d67d7")
Example nostr account created: https://primal.net/p/nprofile1qqs93rfq9t7vrmj2k5j5c7z8asjmngf4h0dq727xnms6w9r5nlthmjg055cur