Scaffold Allmende: identity, canonical JSON, signed listings
- internal/jcs: RFC 8785 canonical JSON (integers only) - internal/identity: ed25519 keypair, 0600 key file, no overwrite - internal/listing: signed listing record with PoW, validation, tombstones and supersede rules - docs/PROTOCOL.md: architecture and decisions so far Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
// Package identity manages a node's ed25519 keypair. The public key is the
|
||||
// identity: there are no accounts.
|
||||
package identity
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Identity struct {
|
||||
priv ed25519.PrivateKey
|
||||
}
|
||||
|
||||
// Generate creates a fresh random identity.
|
||||
func Generate() (*Identity, error) {
|
||||
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Identity{priv: priv}, nil
|
||||
}
|
||||
|
||||
// Load reads an identity previously written by Save.
|
||||
func Load(path string) (*Identity, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seed, err := hex.DecodeString(strings.TrimSpace(string(data)))
|
||||
if err != nil || len(seed) != ed25519.SeedSize {
|
||||
return nil, fmt.Errorf("identity: %s is not a valid key file", path)
|
||||
}
|
||||
return &Identity{priv: ed25519.NewKeyFromSeed(seed)}, nil
|
||||
}
|
||||
|
||||
// Save writes the private seed to path with mode 0600. It refuses to
|
||||
// overwrite an existing file so a key can never be destroyed by accident.
|
||||
func (i *Identity) Save(path string) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, werr := f.WriteString(hex.EncodeToString(i.priv.Seed()) + "\n")
|
||||
if cerr := f.Close(); werr == nil {
|
||||
werr = cerr
|
||||
}
|
||||
if werr != nil {
|
||||
os.Remove(path)
|
||||
}
|
||||
return werr
|
||||
}
|
||||
|
||||
// LoadOrCreate loads the identity at path, creating and saving a new one if
|
||||
// the file does not exist.
|
||||
func LoadOrCreate(path string) (*Identity, error) {
|
||||
id, err := Load(path)
|
||||
if err == nil {
|
||||
return id, nil
|
||||
}
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
id, err = Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := id.Save(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// PublicKeyHex returns the hex-encoded public key (64 characters).
|
||||
func (i *Identity) PublicKeyHex() string {
|
||||
return hex.EncodeToString(i.priv.Public().(ed25519.PublicKey))
|
||||
}
|
||||
|
||||
func (i *Identity) Sign(msg []byte) []byte {
|
||||
return ed25519.Sign(i.priv, msg)
|
||||
}
|
||||
|
||||
// Verify checks sig over msg against a hex-encoded public key.
|
||||
func Verify(pubHex string, msg, sig []byte) bool {
|
||||
pub, err := hex.DecodeString(pubHex)
|
||||
if err != nil || len(pub) != ed25519.PublicKeySize {
|
||||
return false
|
||||
}
|
||||
return ed25519.Verify(pub, msg, sig)
|
||||
}
|
||||
Reference in New Issue
Block a user