Add device keys, revocation and libp2p gossip with sync
- identity: 12-word recovery phrase, device certificates, revocations, device key storage; the identity key is never stored on a node - listing: signed by a device with an embedded certificate, domain-separated signatures, ErrTimeWindow for clock-related failures, region helpers - store: revocations purge a device's listings; keyset-paged Since for sync - p2p: gossipsub topic per geohash region plus a global revocation topic, validators run before relaying, /allmende/1/sync catch-up protocol, bootstrap peers and mDNS - docs: key model, replication and conflict rules Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package identity
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/hkdf"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tyler-smith/go-bip39"
|
||||
)
|
||||
|
||||
const entropyBytes = 16 // 128 bits = 12 words
|
||||
|
||||
// NewRoot creates a fresh identity key together with its 12-word recovery
|
||||
// phrase. The caller must show the phrase to the user once and then discard
|
||||
// the key: the phrase alone is enough to recreate it.
|
||||
func NewRoot() (*Identity, string, error) {
|
||||
entropy := make([]byte, entropyBytes)
|
||||
if _, err := rand.Read(entropy); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
phrase, err := bip39.NewMnemonic(entropy)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
id, err := fromEntropy(entropy)
|
||||
return id, phrase, err
|
||||
}
|
||||
|
||||
// FromPhrase recreates an identity key from its recovery phrase. Extra
|
||||
// whitespace and letter case are tolerated; the word checksum is verified so
|
||||
// typos are caught.
|
||||
func FromPhrase(phrase string) (*Identity, error) {
|
||||
phrase = strings.ToLower(strings.Join(strings.Fields(phrase), " "))
|
||||
entropy, err := bip39.EntropyFromMnemonic(phrase)
|
||||
if err != nil {
|
||||
return nil, errors.New("identity: invalid recovery phrase")
|
||||
}
|
||||
if len(entropy) != entropyBytes {
|
||||
return nil, errors.New("identity: recovery phrase must have 12 words")
|
||||
}
|
||||
return fromEntropy(entropy)
|
||||
}
|
||||
|
||||
func fromEntropy(entropy []byte) (*Identity, error) {
|
||||
seed, err := hkdf.Key(sha256.New, entropy, nil, "allmende identity key v1", ed25519.SeedSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Identity{priv: ed25519.NewKeyFromSeed(seed)}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user