init --copy: put the recovery phrase on the clipboard
Opt-in, as one line of words. Uses wl-copy, xclip, xsel, pbcopy or clip.exe, and falls back to asking the terminal (OSC 52), which works over SSH. The output says how it was copied and to clear the clipboard afterwards. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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())) {
|
||||
|
||||
Reference in New Issue
Block a user