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
+51
View File
@@ -53,6 +53,18 @@ type Config struct {
// RegionKeeper lets the owner change the regions the node follows while it
// runs. Without it Regions is fixed and the endpoint is absent.
RegionKeeper RegionKeeper
// CertKeeper renews the device certificate from the recovery phrase. Without
// it the endpoint is absent and the node reports that it cannot renew.
CertKeeper CertKeeper
}
// CertKeeper renews this node's device certificate and makes it stick: the
// same device key, a new certificate, saved, and the node's own listings
// signed with it where the old one would run out first.
type CertKeeper interface {
// CanRenew is false for an identity made without a recovery phrase.
CanRenew() bool
Renew(root *identity.Identity) (*identity.Cert, error)
}
// RegionKeeper holds the regions a node follows: it applies and remembers a change.
@@ -101,6 +113,9 @@ func New(cfg Config) http.Handler {
if cfg.RegionKeeper != nil {
mux.HandleFunc("PUT /api/v1/regions", s.admin(s.putRegions))
}
if cfg.CertKeeper != nil {
mux.HandleFunc("PUT /api/v1/certificate", s.admin(s.putCertificate))
}
// Messaging: public relay endpoints for buyers' browsers...
mux.HandleFunc("POST /api/v1/messages", s.postMessage)
@@ -175,6 +190,10 @@ func (s *server) status(w http.ResponseWriter, r *http.Request) {
"regions": s.regions(),
"peers": s.Node.PeerCount(),
"listings": n,
// When this node's device certificate runs out (it is public: it
// travels with every listing) and whether the owner can renew it here.
"cert_expires_at": s.Device.Current().ExpiresAt,
"can_renew": s.CertKeeper != nil && s.CertKeeper.CanRenew(),
// Lets the browser report how much anti-spam work a listing carries.
"difficulty": s.Difficulty,
"message_difficulty": s.MessageDifficulty,
@@ -307,6 +326,38 @@ func (s *server) admin(next http.HandlerFunc) http.HandlerFunc {
}
}
// putCertificate renews the device certificate from the recovery phrase. The
// phrase is used for this one request and not kept; it never leaves the node
// the owner is signed in to, but it does cross the connection to it, so over
// anything but localhost the node must sit behind TLS (as for the admin token).
func (s *server) putCertificate(w http.ResponseWriter, r *http.Request) {
var body struct {
Phrase string `json:"phrase"`
}
if !decodeBody(w, r, &body) {
return
}
if !s.CertKeeper.CanRenew() {
fail(w, http.StatusConflict, "this identity has no recovery phrase, so its certificate cannot be renewed")
return
}
root, err := identity.FromPhrase(body.Phrase)
if err != nil {
fail(w, http.StatusBadRequest, "that is not a valid recovery phrase: check the twelve words")
return
}
cert, err := s.CertKeeper.Renew(root)
switch {
case errors.Is(err, identity.ErrWrongIdentity):
fail(w, http.StatusConflict, "these words belong to a different identity than this node")
return
case err != nil:
fail(w, http.StatusInternalServerError, err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"cert_expires_at": cert.ExpiresAt})
}
// putRegions changes the regions the node follows, at once and for good.
func (s *server) putRegions(w http.ResponseWriter, r *http.Request) {
var body struct {