diff --git a/README.md b/README.md index c5ce79b..a24af11 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,10 @@ This prints your 12-word recovery phrase **once**. Write it down: it is the only way to get your identity and listings back if the machine is lost (`allmende recover`). Keys and the database live in the data directory, `~/.local/share/allmende` by default (change it with `--data ` or the -`ALLMENDE_DATA` environment variable). +`ALLMENDE_DATA` environment variable). `bin/allmende init --copy` also copies the +words to the clipboard as one line (via `wl-copy`, `xclip`, `xsel` or `pbcopy`, +or your terminal over SSH): paste them into a password manager, then clear the +clipboard. **3. Start the node** diff --git a/cmd/allmende/clipboard.go b/cmd/allmende/clipboard.go new file mode 100644 index 0000000..bc7818a --- /dev/null +++ b/cmd/allmende/clipboard.go @@ -0,0 +1,62 @@ +package main + +import ( + "encoding/base64" + "errors" + "fmt" + "io" + "os" + "os/exec" + "strings" +) + +// clipboardTools are the programs that put text on the system clipboard, in +// the order they are tried. +var clipboardTools = [][]string{ + {"wl-copy"}, // Wayland + {"xclip", "-selection", "clipboard"}, // X11 + {"xsel", "--clipboard", "--input"}, // X11 + {"pbcopy"}, // macOS + {"clip.exe"}, // Windows / WSL +} + +// copyToClipboard puts text on the clipboard and says how it did that. It uses +// a clipboard program when one is installed, and otherwise asks the terminal +// itself (OSC 52), which also works over SSH. The terminal gives no answer, so +// for that route the result can only say that it was asked. +func copyToClipboard(text string, tty io.Writer) (how string, err error) { + for _, tool := range clipboardTools { + path, err := exec.LookPath(tool[0]) + if err != nil { + continue + } + cmd := exec.Command(path, tool[1:]...) + cmd.Stdin = strings.NewReader(text) + if err := cmd.Run(); err == nil { + return tool[0], nil + } + } + if tty == nil { + return "", errors.New("no clipboard program found (install wl-clipboard or xclip)") + } + if _, err := io.WriteString(tty, osc52(text)); err != nil { + return "", err + } + return "your terminal", nil +} + +// osc52 is the escape sequence that makes a terminal copy text. +func osc52(text string) string { + return "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte(text)) + "\x07" +} + +// copyPhrase copies the recovery phrase, as one line of words, which is the +// form 'allmende recover' reads, and tells the owner how to treat the clipboard. +func copyPhrase(words []string, tty *os.File) { + how, err := copyToClipboard(strings.Join(words, " "), tty) + if err != nil { + fmt.Printf("Could not copy the phrase: %v\n\n", err) + return + } + fmt.Printf("The 12 words are copied (by %s) as one line, ready to paste. Paste them into a\npassword manager now, then clear your clipboard: clipboard managers and other\nprograms can read it.\n\n", how) +} diff --git a/cmd/allmende/clipboard_test.go b/cmd/allmende/clipboard_test.go new file mode 100644 index 0000000..7415c9d --- /dev/null +++ b/cmd/allmende/clipboard_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "bytes" + "encoding/base64" + "strings" + "testing" +) + +func TestOSC52CarriesTheTextBase64(t *testing.T) { + got := osc52("abandon ability able") + want := "\x1b]52;c;" + base64.StdEncoding.EncodeToString([]byte("abandon ability able")) + "\x07" + if got != want { + t.Fatalf("got %q", got) + } +} + +func TestWithoutToolsOrTerminalCopyingFailsClearly(t *testing.T) { + t.Setenv("PATH", t.TempDir()) // no clipboard program + if _, err := copyToClipboard("x", nil); err == nil || !strings.Contains(err.Error(), "no clipboard program") { + t.Fatalf("err = %v", err) + } +} + +func TestWithoutToolsTheTerminalIsAsked(t *testing.T) { + t.Setenv("PATH", t.TempDir()) + var tty bytes.Buffer + how, err := copyToClipboard("a b c", &tty) + if err != nil || how != "your terminal" || tty.String() != osc52("a b c") { + t.Fatalf("how=%q err=%v out=%q", how, err, tty.String()) + } +} diff --git a/cmd/allmende/init.go b/cmd/allmende/init.go index 71854d6..20f809b 100644 --- a/cmd/allmende/init.go +++ b/cmd/allmende/init.go @@ -21,6 +21,7 @@ import ( func cmdInit(args []string) error { fs := flag.NewFlagSet("init", flag.ExitOnError) data := dataFlag(fs) + copyWords := fs.Bool("copy", false, "also copy the 12 words to the clipboard, as one line for pasting") fs.Parse(args) if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil { @@ -33,6 +34,13 @@ func cmdInit(args []string) error { } words := strings.Fields(phrase) showPhrase(words) + if *copyWords { + var tty *os.File + if term.IsTerminal(int(os.Stdout.Fd())) { + tty = os.Stdout // only a real terminal can act on the OSC 52 fallback + } + copyPhrase(words, tty) + } // Only ask for confirmation when a person is at the keyboard. if term.IsTerminal(int(os.Stdin.Fd())) {