go-nostr/nip06/nip06.go

56 lines
958 B
Go
Raw Permalink Normal View History

2022-01-06 21:56:57 -03:00
package nip06
import (
"encoding/hex"
"github.com/tyler-smith/go-bip32"
"github.com/tyler-smith/go-bip39"
)
func GenerateSeedWords() (string, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
return "", err
}
words, err := bip39.NewMnemonic(entropy)
if err != nil {
return "", err
}
return words, nil
}
func SeedFromWords(words string) []byte {
return bip39.NewSeed(words, "")
}
func PrivateKeyFromSeed(seed []byte) (string, error) {
key, err := bip32.NewMasterKey(seed)
if err != nil {
return "", err
}
derivationPath := []uint32{
bip32.FirstHardenedChild + 44,
bip32.FirstHardenedChild + 1237,
bip32.FirstHardenedChild + 0,
0,
0,
}
next := key
for _, idx := range derivationPath {
var err error
2023-06-11 10:48:46 -03:00
if next, err = next.NewChildKey(idx); err != nil {
2022-01-06 21:56:57 -03:00
return "", err
}
}
return hex.EncodeToString(next.Key), nil
}
func ValidateWords(words string) bool {
return bip39.IsMnemonicValid(words)
}