All checks were successful
check / check (push) Successful in 19s
keyfunc mnemonic prints a child mnemonic derived from the main one with BIP-85's own mnemonic application, in English, at 12, 18 or 24 words. Its entropy is the BIP-85 entropy cut to the length the word count needs, which is what that application asks for, so it does not go through the generator the other key types read their bytes from. The BIP-85 specification's own test vectors for the application are the tests. Two pieces the derivation package already had inside one function are now named: the master key a mnemonic stands for, and the refusal of a key index that has no hardened child. Both key types call them. Model: opus-5
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
// Package mnemonic is the command that prints a child mnemonic.
|
|
package mnemonic
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.eeqj.de/sneak/keyfunc/internal/childmnemonic"
|
|
"git.eeqj.de/sneak/keyfunc/internal/cli/options"
|
|
"git.eeqj.de/sneak/keyfunc/internal/derive"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// Command returns the mnemonic command.
|
|
func Command() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "mnemonic",
|
|
Short: "print a child mnemonic derived from the main one",
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
child, err := derived(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprintln(cmd.OutOrStdout(), child)
|
|
if err != nil {
|
|
return fmt.Errorf("writing the mnemonic: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().Uint32(
|
|
"words", childmnemonic.DefaultWords,
|
|
"how long the child mnemonic is: 12, 18 or 24 words",
|
|
)
|
|
|
|
return cmd
|
|
}
|
|
|
|
// derived returns the child mnemonic this run asks for.
|
|
func derived(cmd *cobra.Command) (string, error) {
|
|
index, err := options.Index(cmd)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
words, err := cmd.Flags().GetUint32("words")
|
|
if err != nil {
|
|
return "", fmt.Errorf("reading the word count: %w", err)
|
|
}
|
|
|
|
parent, err := options.Mnemonic(cmd)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
master, err := derive.Master(parent)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return childmnemonic.Derive(master, words, index)
|
|
}
|