mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-30 02:20:37 +02:00
nip06 package for key derivation.
This commit is contained in:
56
nip06/nip06.go
Normal file
56
nip06/nip06.go
Normal file
@ -0,0 +1,56 @@
|
||||
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
|
||||
next, err = next.NewChildKey(idx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return hex.EncodeToString(next.Key), nil
|
||||
}
|
||||
|
||||
func ValidateWords(words string) bool {
|
||||
return bip39.IsMnemonicValid(words)
|
||||
}
|
Reference in New Issue
Block a user