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:
2026-11-18 11:05:27 +01:00
co-authored by Claude Sonnet 5.5
parent 6f15adb403
commit 548efdc87b
4 changed files with 106 additions and 1 deletions
+4 -1
View File
@@ -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 way to get your identity and listings back if the machine is lost
(`allmende recover`). Keys and the database live in the data directory, (`allmende recover`). Keys and the database live in the data directory,
`~/.local/share/allmende` by default (change it with `--data <dir>` or the `~/.local/share/allmende` by default (change it with `--data <dir>` 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** **3. Start the node**
+62
View File
@@ -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)
}
+32
View File
@@ -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())
}
}
+8
View File
@@ -21,6 +21,7 @@ import (
func cmdInit(args []string) error { func cmdInit(args []string) error {
fs := flag.NewFlagSet("init", flag.ExitOnError) fs := flag.NewFlagSet("init", flag.ExitOnError)
data := dataFlag(fs) data := dataFlag(fs)
copyWords := fs.Bool("copy", false, "also copy the 12 words to the clipboard, as one line for pasting")
fs.Parse(args) fs.Parse(args)
if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil { if _, err := os.Stat(filepath.Join(*data, "device.key")); err == nil {
@@ -33,6 +34,13 @@ func cmdInit(args []string) error {
} }
words := strings.Fields(phrase) words := strings.Fields(phrase)
showPhrase(words) 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. // Only ask for confirmation when a person is at the keyboard.
if term.IsTerminal(int(os.Stdin.Fd())) { if term.IsTerminal(int(os.Stdin.Fd())) {