Skip to content

Instantly share code, notes, and snippets.

@ohac
Created October 17, 2023 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohac/0bc56e7c0463787509096a880ed76386 to your computer and use it in GitHub Desktop.
Save ohac/0bc56e7c0463787509096a880ed76386 to your computer and use it in GitHub Desktop.
get Nostr nsec from BIP-0039 Mnemonic
package main
import (
"fmt"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
)
func main() {
//entropy, _ := bip39.NewEntropy(256)
entropy, _ := bip39.EntropyFromMnemonic("decide intact title offer slab wife swear family tumble initial sheriff dance canoe siren chicken wait eyebrow flee slide day slide boy patch average")
mnemonic, _ := bip39.NewMnemonic(entropy)
fmt.Println("Mnemonic: ", mnemonic)
secret := "" // or "Secret Passphrase"
seed := bip39.NewSeed(mnemonic, secret)
masterKey, _ := bip32.NewMasterKey(seed)
masterKey, _ = masterKey.NewChildKey(0)
s, _ := masterKey.Serialize()
raw := s[46:78]
hexstr := ""
for _, v := range raw {
hexstr += fmt.Sprintf("%02x", v)
}
pk, _ := nostr.GetPublicKey(hexstr)
nsec, _ := nip19.EncodePrivateKey(hexstr)
npub, _ := nip19.EncodePublicKey(pk)
fmt.Println(nsec)
fmt.Println(npub)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment