Renew the device certificate from the browser, with the recovery phrase

Within the last 30 days the owner page offers it. The node signs a new certificate for the same device key (peer ID and listings stay), saves it, and signs again own listings that would outlive the old certificate. The phrase is used for one request and not kept; another identity's phrase and no-phrase identities are refused. Device.Current gives the certificate to sign with while a renewal happens.

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 23:19:30 +01:00
co-authored by Claude Sonnet 5.5
parent 210ba1b3e9
commit dfe35b422d
23 changed files with 1011 additions and 29 deletions
+43 -2
View File
@@ -6,14 +6,55 @@ import (
"errors"
"os"
"path/filepath"
"sync"
"time"
)
// Device is what a running node holds: its own key plus the certificate that
// ties it to an identity. The identity key itself is never stored.
type Device struct {
Key *Identity
Key *Identity
// Cert is the certificate the device was created or loaded with. Its
// Author and Device never change, so they can be read from it at any time.
// A renewal replaces the certificate to sign with, not this field: use
// Current for that (and for the expiry).
Cert *Cert
mu sync.RWMutex
cur *Cert // set by Renew; nil until then
}
// Current is the certificate to sign with and to report the expiry of. It is
// safe to call while another goroutine renews.
func (d *Device) Current() *Cert {
d.mu.RLock()
defer d.mu.RUnlock()
if d.cur != nil {
return d.cur
}
return d.Cert
}
// ErrWrongIdentity means the identity key offered for a renewal is not the
// one that certified this device.
var ErrWrongIdentity = errors.New("identity: these words belong to a different identity than this device")
// Renew certifies this same device key again, for validFor from now, with the
// identity key root. The device key does not change, so neither does the
// node's peer ID, and nothing is revoked. Only the identity that certified
// the device can do it.
func (d *Device) Renew(root *Identity, now time.Time, validFor time.Duration) (*Cert, error) {
if root.PublicKeyHex() != d.Cert.Author {
return nil, ErrWrongIdentity
}
cert, err := IssueCert(root, d.Cert.Device, now, now.Add(validFor))
if err != nil {
return nil, err
}
d.mu.Lock()
d.cur = cert
d.mu.Unlock()
return cert, nil
}
// Enroll creates a new device key and certifies it with the identity key.
@@ -44,7 +85,7 @@ func (d *Device) Save(dir string) error {
// SaveCert (over)writes only the certificate, e.g. after renewal.
func (d *Device) SaveCert(dir string) error {
data, err := json.Marshal(d.Cert)
data, err := json.Marshal(d.Current())
if err != nil {
return err
}