GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Bitcoin PSBT signing with imported keys (test networks, phase 1) (#188) · gitmoot/keyring@d052f66

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • cmd/keyring
    • bitcoin_import.go
    • bitcoin_import_test.go
    • main.go
  • deploy
    • relay.Dockerfile
  • docs
    • bitcoin.md
    • dashboard.md
  • go.mod
  • go.sum
  • internal
    • bitcoin
      • bitcoin.go
      • bitcoin_test.go
      • bitcointest
        • bitcointest.go
      • psbt.go
    • dashboard
      • access.go
      • bitcoin.go
      • bitcoin_test.go
      • dashboard.go
      • pages.go
      • reveal.go
      • services.go
    • instance
      • instance.go
    • policy
      • bitcoin_test.go
      • policy.go
    • server
      • auditsearch.go
      • bitcoin_signing.go
      • bitcoin_signing_test.go
      • server.go
      • services.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ The key never leaves the keyring machine. The agent holds only a role token, whi
3535

3636
- **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)
3737
- **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)
3839
- **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)
3940
- **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)
4041
- **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/
136137
| [Consent links](docs/consent-links.md) | One-time links for a person to connect their account |
137138
| [Apple](docs/apple.md) | Apple Ads, App Store Connect, Sign in with Apple, APNs |
138139
| [Signing](docs/signing.md) | Ed25519 and minisign |
140+
| [Bitcoin](docs/bitcoin.md) | PSBT signing with imported keys (test networks) |
139141
| [Egress proxy](docs/egress-proxy.md) | Tunnels through a person's own proxy |
140142
| [Instance mode](docs/instance-mode.md) | One keyring per person, under a supervisor |
141143
| [Mac install](deploy/mac/README.md) · [Cloudflare](deploy/cloudflare/README.md) | Deployment |
Collapse file

‎cmd/keyring/bitcoin_import.go‎

Copy file name to clipboard
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

‎cmd/keyring/bitcoin_import_test.go‎

Copy file name to clipboard
+76Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

‎cmd/keyring/main.go‎

Copy file name to clipboardExpand all lines: cmd/keyring/main.go
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const usage = `Usage:
5050
keyring list --store FILE print key names (never values)
5151
keyring minisign-keygen --store FILE NAME make a new Ed25519 signing key in the store (never shown) and
5252
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
5356
keyring new-token make a role token and its hash for the rules file
5457
keyring request --upstream URL --role NAME --service S [--service S ...] [--project P ...] [--note TEXT] [--new TOKENS_DIR]
5558
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 {
102105
secretFile := fs.String("file", "", "set: import a private file verbatim, or /dev/stdin for a protected pipe (up to 1 MiB)")
103106
adminTokenSHA := fs.String("admin-token-sha256", "", "instance: hex SHA-256 of the supervisor's admin token")
104107
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")
105109
auditPath := fs.String("audit", "", "instance: audit log file (default: stdout)")
106110
containerMode := fs.Bool("container", false, "instance: run in a container reached only through its supervisor (wildcard listen, no source check)")
107111
if err := fs.Parse(rest); err != nil {
@@ -198,6 +202,12 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
198202
return 2
199203
}
200204
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)
201211
case "new-token":
202212
if fs.NArg() != 0 {
203213
return 2
@@ -317,6 +327,11 @@ func serve(configPath, storePath string, stderr io.Writer) error {
317327
// The unsafe/corrupt file is never replaced by an empty history.
318328
fmt.Fprintf(stderr, "Google renewal history unavailable: %v\n", err)
319329
}
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+
}
320335
usageFile := usagePath(storePath)
321336
if rows, err := loadUsage(usageFile); err != nil {
322337
// Usage is only statistics: start without it rather than not start.
Collapse file

‎deploy/relay.Dockerfile‎

Copy file name to clipboardExpand all lines: deploy/relay.Dockerfile
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
FROM golang:1.24-alpine AS build
1+
FROM golang:1.26-alpine AS build
22
RUN apk add --no-cache ca-certificates \
33
&& mkdir -p /run/keyring/tokens \
44
&& chmod 700 /run/keyring /run/keyring/tokens
55
WORKDIR /src
6-
COPY go.mod ./
6+
COPY go.mod go.sum ./
77
COPY cmd ./cmd
88
COPY internal ./internal
99
ARG VERSION=dev

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.