// SPDX-License-Identifier: GPL-3.0-or-later

package updater

import (
	"crypto/ed25519"
	"encoding/base64"
	"errors"
)

// updatePublicKeyB64 is the base64-encoded ed25519 public key used to verify
// update manifests. An empty string disables update verification at compile
// time and causes all update operations to fail closed.
//
// Override at build time:
//
//	go build -ldflags "-X github.com/privatebackup/client-backend/internal/updater.updatePublicKeyB64=<base64>"
var updatePublicKeyB64 = "o2LuPmcb6EnpreSNj9lad4FjkbprlIkxqCTXZ0vEX9g="

// PublicKey returns the embedded update-signing public key, or an error if
// the build was produced without a key embedded.
func PublicKey() (ed25519.PublicKey, error) {
	if updatePublicKeyB64 == "" {
		return nil, errors.New("no update public key embedded in this build")
	}
	raw, err := base64.StdEncoding.DecodeString(updatePublicKeyB64)
	if err != nil {
		return nil, err
	}
	if len(raw) != ed25519.PublicKeySize {
		return nil, errors.New("embedded update public key has wrong length")
	}
	return ed25519.PublicKey(raw), nil
}
