check / check (push) Successful in 3m14s
Both flags were uint32 flags, which the flag library reads with Go's base-0 rules, so `-n 010` derived key 8 and `0x10` or `1_0` were accepted. They are now string flags read by options.Decimal, which takes decimal digits only: a leading zero stays decimal, and a base prefix, an underscore or a sign is refused with an error naming the flag, before the mnemonic is looked for. --help shows both as a number, and the README says the index is a decimal number. Model: opus-5-5
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
// Package mnemonic is the command that prints a child mnemonic.
|
|
package mnemonic
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/spf13/cobra"
|
|
"sneak.berlin/go/keyfunc/internal/childmnemonic"
|
|
"sneak.berlin/go/keyfunc/internal/cli/options"
|
|
"sneak.berlin/go/keyfunc/internal/derive"
|
|
)
|
|
|
|
// 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().String(
|
|
"words", strconv.Itoa(childmnemonic.DefaultWords),
|
|
"the `number` of words in the child mnemonic: 12, 18 or 24",
|
|
)
|
|
|
|
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 := options.Decimal(cmd, "words")
|
|
if err != nil {
|
|
return "", 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)
|
|
}
|