nip06 package for key derivation.

This commit is contained in:
fiatjaf
2022-01-06 21:56:57 -03:00
parent 275020a7ef
commit ba0507cce7
3 changed files with 82 additions and 0 deletions

56
nip06/nip06.go Normal file
View 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)
}