package main import ( "bufio" "crypto/rand" "errors" "flag" "fmt" "io" "math/big" "os" "path/filepath" "strings" "time" "golang.org/x/term" "git.stinnesbeck.com/nils/allmende/internal/identity" ) func cmdInit(args []string) error { fs := flag.NewFlagSet("init", flag.ExitOnError) data := dataFlag(fs) fs.Parse(args) if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil { return fmt.Errorf("%s is already set up. To get access back after losing this machine, use 'node recover'", *data) } root, phrase, err := identity.NewRoot() if err != nil { return err } words := strings.Fields(phrase) showPhrase(words) // Only ask for confirmation when a person is at the keyboard. if term.IsTerminal(int(os.Stdin.Fd())) { if err := confirmPhrase(bufio.NewReader(os.Stdin), words); err != nil { return err } } dev, err := identity.Enroll(root, time.Now(), certLifetime) if err != nil { return err } // The identity key is not saved anywhere: only the phrase can recreate it. if err := dev.Save(*data); err != nil { return err } token, err := newToken() if err != nil { return err } if err := writeToken(*data, token); err != nil { return err } fmt.Printf("\nDone. This machine is set up as a device of identity %s…\n", dev.Cert.Author[:12]) fmt.Printf("Your device certificate is valid until %s.\n\n", time.Unix(dev.Cert.ExpiresAt, 0).Format("2006-01-02")) fmt.Printf("Next: node run --region --data %s\n", *data) fmt.Printf("The admin token for publishing is in %s\n", filepath.Join(*data, tokenFile)) return nil } func showPhrase(words []string) { fmt.Print(` Your recovery phrase -------------------- These 12 words ARE your identity. They are shown only once and are not stored on this computer. Write them on paper and keep them somewhere safe. With them you can recover your account if this computer is lost, broken or hacked. Anyone who has them can act as you, so never type them into a website or share them. `) for i := 0; i < len(words); i += 3 { fmt.Printf(" %2d. %-12s %2d. %-12s %2d. %-12s\n", i+1, words[i], i+2, words[i+1], i+3, words[i+2]) } fmt.Println() } // confirmPhrase asks for two random words so the user has really written // the phrase down. func confirmPhrase(in *bufio.Reader, words []string) error { picks, err := twoDistinct(len(words)) if err != nil { return err } fmt.Println("To make sure you wrote it down, please enter two of the words.") for _, i := range picks { for { fmt.Printf("Word number %d: ", i+1) line, err := in.ReadString('\n') if err != nil && (!errors.Is(err, io.EOF) || line == "") { return errors.New("no input; run 'node init' again in a terminal") } if strings.EqualFold(strings.TrimSpace(line), words[i]) { break } fmt.Println("That is not the right word. Check your notes and try again.") } } return nil } func twoDistinct(n int) ([2]int, error) { a, err := rand.Int(rand.Reader, big.NewInt(int64(n))) if err != nil { return [2]int{}, err } b, err := rand.Int(rand.Reader, big.NewInt(int64(n-1))) if err != nil { return [2]int{}, err } x, y := int(a.Int64()), int(b.Int64()) if y >= x { y++ } if x > y { x, y = y, x } return [2]int{x, y}, nil }