GeezerVison is showing github.com — Bitcoin PSBT signing with imported keys (test networks, phase 1) (#188) · gitmoot/keyring@d052f66
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ The key never leaves the keyring machine. The agent holds only a role token, whi | 35 | 35 |
| 36 | 36 | - **API proxy.** Bearer (or another `Authorization` scheme, such as `Key`), header or query-parameter keys, up to two extra secret headers, keys with up to 32 values (numbered or named, with optional labels), and [key pools](docs/proxy.md#key-pools-several-keys-behind-one-service) that try a key's values in the order you set and switch to the next when one is refused or runs out. Paths are normalised, keys echoed back by an API are masked, redirects are not followed. [Proxy](docs/proxy.md) | 37 | 37 | - **Request signing without handing out the private key.** Apple Ads and App Store Connect JWTs, Sign in with Apple client secrets, APNs push, raw Ed25519, and minisign release signatures. [Apple](docs/apple.md) · [Signing](docs/signing.md) | | 38 | +- **Bitcoin PSBT signing (test networks).** Imported tprv/WIF keys sign the PSBT inputs that are provably theirs (BIP86 taproot key path, BIP84 P2WPKH), only on allowed derivation paths, with per-grant sats limits per PSBT and per day; the key is never shown. Regtest, testnet and signet in phase 1. [Bitcoin](docs/bitcoin.md) | 38 | 39 | - **OAuth with refresh.** The keyring holds the refresh token and mints access tokens, for Gmail, Drive and any OAuth 2 provider; Google accounts can also come from `gcloud`. [OAuth](docs/oauth.md) · [Google via gcloud](docs/google-gcloud.md) | 39 | 40 | - **Per-agent access.** Each agent (a *role*) gets only the services, methods and paths it was given, with daily request and byte limits and an end date. Agents find services with `keyring services --search`, ask with `keyring request`, and the owner approves in one tap. [Finding services](docs/proxy.md#finding-services) | 40 | 41 | - **Projects.** Group a client's services, give an agent a whole project, pause it or let it expire in one place. [Projects](docs/projects.md) | @@ -136,6 +137,7 @@ Next: [turn on the dashboard](docs/dashboard.md), [install on a Mac](deploy/mac/ | 136 | 137 | | [Consent links](docs/consent-links.md) | One-time links for a person to connect their account | | 137 | 138 | | [Apple](docs/apple.md) | Apple Ads, App Store Connect, Sign in with Apple, APNs | | 138 | 139 | | [Signing](docs/signing.md) | Ed25519 and minisign | | | 140 | +| [Bitcoin](docs/bitcoin.md) | PSBT signing with imported keys (test networks) | | 139 | 141 | | [Egress proxy](docs/egress-proxy.md) | Tunnels through a person's own proxy | | 140 | 142 | | [Instance mode](docs/instance-mode.md) | One keyring per person, under a supervisor | | 141 | 143 | | [Mac install](deploy/mac/README.md) · [Cloudflare](deploy/cloudflare/README.md) | Deployment | | |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package main | | 2 | + | | 3 | +import ( | | 4 | + "bufio" | | 5 | + "errors" | | 6 | + "fmt" | | 7 | + "io" | | 8 | + "strings" | | 9 | + | | 10 | + "github.com/gitmoot/keyring/internal/bitcoin" | | 11 | + "github.com/gitmoot/keyring/internal/store" | | 12 | +) | | 13 | + | | 14 | +// bitcoinImport stores a Bitcoin private key (a master tprv or a WIF key) | | 15 | +// read from stdin, never from arguments, so it is not in the shell history | | 16 | +// or the process list (#187). The owner keeps the backup: the keyring never | | 17 | +// generates or shows a key. It prints what the owner can check against the | | 18 | +// wallet: fingerprint, network and first receive addresses. An existing name | | 19 | +// is never replaced. | | 20 | +func bitcoinImport(storePath, network, name string, stdin io.Reader, stdout, stderr io.Writer) error { | | 21 | + if network == "" { | | 22 | + return errors.New("pass --network regtest, testnet or signet") | | 23 | + } | | 24 | + if !store.ValidName(name) { | | 25 | + return fmt.Errorf("invalid key name %q: use letters, digits and _", name) | | 26 | + } | | 27 | + keys, err := store.Load(storePath) | | 28 | + if err != nil { | | 29 | + return err | | 30 | + } | | 31 | + if _, exists := keys[name]; exists { | | 32 | + return fmt.Errorf("%s already exists; import under a new name", name) | | 33 | + } | | 34 | + value, err := readHidden(bufio.NewReader(stdin), stdin, stderr, "Bitcoin key for "+name+" (tprv or WIF, not shown): ") | | 35 | + if err != nil { | | 36 | + return err | | 37 | + } | | 38 | + value = strings.TrimSpace(value) | | 39 | + d, err := bitcoin.Describe(value, network) | | 40 | + if err != nil { | | 41 | + // Describe's errors are fixed texts; never the value. | | 42 | + return err | | 43 | + } | | 44 | + if err := store.Set(storePath, name, value); err != nil { | | 45 | + return err | | 46 | + } | | 47 | + fmt.Fprintf(stdout, "stored %s\nfingerprint: %s\nnetwork: %s\nkind: %s\nfirst receive addresses (check them in your wallet):\n", name, d.Fingerprint, d.Network, d.Kind) | | 48 | + for _, a := range d.Addresses { | | 49 | + fmt.Fprintf(stdout, " %-17s %-7s %s\n", a.Path, a.Script, a.Address) | | 50 | + } | | 51 | + return nil | | 52 | +} |
Collapse file Copy file name to clipboard +76Lines changed: 76 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package main | | 2 | + | | 3 | +import ( | | 4 | + "bytes" | | 5 | + "path/filepath" | | 6 | + "strings" | | 7 | + "testing" | | 8 | + | | 9 | + "github.com/gitmoot/keyring/internal/bitcoin/bitcointest" | | 10 | + "github.com/gitmoot/keyring/internal/store" | | 11 | +) | | 12 | + | | 13 | +func TestBitcoinImportFromStdin(t *testing.T) { | | 14 | + storePath := filepath.Join(t.TempDir(), "keys.json") | | 15 | + var out, errOut bytes.Buffer | | 16 | + code := run([]string{"bitcoin-import", "--store", storePath, "--network", "regtest", "BTC_ORD"}, strings.NewReader(bitcointest.Master+"\n"), &out, &errOut) | | 17 | + if code != 0 { | | 18 | + t.Fatalf("exit %d: %s", code, errOut.String()) | | 19 | + } | | 20 | + keys, err := store.Load(storePath) | | 21 | + if err != nil { | | 22 | + t.Fatal(err) | | 23 | + } | | 24 | + if keys["BTC_ORD"] != bitcointest.Master { | | 25 | + t.Fatal("stored value is not the imported key") | | 26 | + } | | 27 | + for _, want := range []string{"stored BTC_ORD", "fingerprint: 73c5da0a", "network: regtest", "m/86'/1'/0'/0/0", "p2tr", "bcrt1p", "m/84'/1'/0'/0/0", "bcrt1q"} { | | 28 | + if !strings.Contains(out.String(), want) { | | 29 | + t.Fatalf("output lacks %q:\n%s", want, out.String()) | | 30 | + } | | 31 | + } | | 32 | + if strings.Contains(out.String()+errOut.String(), bitcointest.Master[:20]) { | | 33 | + t.Fatal("key printed") | | 34 | + } | | 35 | + | | 36 | + // An existing name is never replaced. | | 37 | + out.Reset() | | 38 | + errOut.Reset() | | 39 | + if code := run([]string{"bitcoin-import", "--store", storePath, "--network", "regtest", "BTC_ORD"}, strings.NewReader(bitcointest.WIF+"\n"), &out, &errOut); code == 0 { | | 40 | + t.Fatal("an existing key was replaced") | | 41 | + } | | 42 | + if after, _ := store.Load(storePath); after["BTC_ORD"] != bitcointest.Master { | | 43 | + t.Fatal("existing key changed") | | 44 | + } | | 45 | +} | | 46 | + | | 47 | +func TestBitcoinImportRefusals(t *testing.T) { | | 48 | + cases := []struct { | | 49 | + name string | | 50 | + args []string | | 51 | + stdin string | | 52 | + }{ | | 53 | + {"key as an argument", []string{"--network", "regtest", "BTC_ORD", bitcointest.Master}, ""}, | | 54 | + {"no network", []string{"BTC_ORD"}, bitcointest.Master}, | | 55 | + {"mainnet", []string{"--network", "mainnet", "BTC_ORD"}, bitcointest.Master}, | | 56 | + {"mainnet key on regtest", []string{"--network", "regtest", "BTC_ORD"}, bitcointest.MasterMainnet}, | | 57 | + {"not a key", []string{"--network", "regtest", "BTC_ORD"}, "hunter2"}, | | 58 | + {"empty stdin", []string{"--network", "regtest", "BTC_ORD"}, ""}, | | 59 | + } | | 60 | + for _, c := range cases { | | 61 | + storePath := filepath.Join(t.TempDir(), "keys.json") | | 62 | + var out, errOut bytes.Buffer | | 63 | + args := append([]string{"bitcoin-import", "--store", storePath}, c.args...) | | 64 | + if code := run(args, strings.NewReader(c.stdin+"\n"), &out, &errOut); code == 0 { | | 65 | + t.Fatalf("%s: accepted", c.name) | | 66 | + } | | 67 | + if names, _ := store.Names(storePath); len(names) != 0 { | | 68 | + t.Fatalf("%s: stored %v", c.name, names) | | 69 | + } | | 70 | + for _, secret := range []string{bitcointest.Master[:20], bitcointest.MasterMainnet[:20], "hunter2"} { | | 71 | + if strings.Contains(out.String()+errOut.String(), secret) { | | 72 | + t.Fatalf("%s: key in output: %s", c.name, out.String()+errOut.String()) | | 73 | + } | | 74 | + } | | 75 | + } | | 76 | +} |
Collapse file Copy file name to clipboardExpand all lines: cmd/keyring/main.go +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,6 +50,9 @@ const usage = `Usage: | 50 | 50 | keyring list --store FILE print key names (never values) | 51 | 51 | keyring minisign-keygen --store FILE NAME make a new Ed25519 signing key in the store (never shown) and | 52 | 52 | print its minisign public key | | 53 | + keyring bitcoin-import --store FILE --network regtest|testnet|signet NAME | | 54 | + import a Bitcoin tprv or WIF key from stdin (never shown) and print | | 55 | + its fingerprint and first receive addresses | 53 | 56 | keyring new-token make a role token and its hash for the rules file | 54 | 57 | keyring request --upstream URL --role NAME --service S [--service S ...] [--project P ...] [--note TEXT] [--new TOKENS_DIR] | 55 | 58 | ask the owner for full access to services (approved in the dashboard) | @@ -102,6 +105,7 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { | 102 | 105 | secretFile := fs.String("file", "", "set: import a private file verbatim, or /dev/stdin for a protected pipe (up to 1 MiB)") | 103 | 106 | adminTokenSHA := fs.String("admin-token-sha256", "", "instance: hex SHA-256 of the supervisor's admin token") | 104 | 107 | statePath := fs.String("state", "", "instance: keep the sealed state in this file (default: memory only)") | | 108 | + network := fs.String("network", "", "bitcoin-import: regtest, testnet or signet") | 105 | 109 | auditPath := fs.String("audit", "", "instance: audit log file (default: stdout)") | 106 | 110 | containerMode := fs.Bool("container", false, "instance: run in a container reached only through its supervisor (wildcard listen, no source check)") | 107 | 111 | if err := fs.Parse(rest); err != nil { | @@ -198,6 +202,12 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { | 198 | 202 | return 2 | 199 | 203 | } | 200 | 204 | err = minisignKeygen(*storePath, fs.Arg(0), stdout) | | 205 | + case "bitcoin-import": | | 206 | + if !need("store") || fs.NArg() != 1 { | | 207 | + fmt.Fprintln(stderr, "keyring bitcoin-import: pass --store FILE, --network and one NAME; the key is read from stdin, never from arguments") | | 208 | + return 2 | | 209 | + } | | 210 | + err = bitcoinImport(*storePath, *network, fs.Arg(0), stdin, stdout, stderr) | 201 | 211 | case "new-token": | 202 | 212 | if fs.NArg() != 0 { | 203 | 213 | return 2 | @@ -317,6 +327,11 @@ func serve(configPath, storePath string, stderr io.Writer) error { | 317 | 327 | // The unsafe/corrupt file is never replaced by an empty history. | 318 | 328 | fmt.Fprintf(stderr, "Google renewal history unavailable: %v\n", err) | 319 | 329 | } | | 330 | + if err := handler.OpenBitcoinSpend(server.BitcoinSpendPath(storePath)); err != nil { | | 331 | + // Kept closed: grants with bitcoin_max_sats_per_day sign nothing | | 332 | + // until the file is fixed; the file is never replaced. | | 333 | + fmt.Fprintf(stderr, "bitcoin daily totals unavailable: %v\n", err) | | 334 | + } | 320 | 335 | usageFile := usagePath(storePath) | 321 | 336 | if rows, err := loadUsage(usageFile); err != nil { | 322 | 337 | // Usage is only statistics: start without it rather than not start. | |
Collapse file Copy file name to clipboardExpand all lines: deploy/relay.Dockerfile +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -FROM golang:1.24-alpine AS build | | 1 | +FROM golang:1.26-alpine AS build | 2 | 2 | RUN apk add --no-cache ca-certificates \ | 3 | 3 | && mkdir -p /run/keyring/tokens \ | 4 | 4 | && chmod 700 /run/keyring /run/keyring/tokens | 5 | 5 | WORKDIR /src | 6 | | -COPY go.mod ./ | | 6 | +COPY go.mod go.sum ./ | 7 | 7 | COPY cmd ./cmd | 8 | 8 | COPY internal ./internal | 9 | 9 | ARG VERSION=dev | |
|