commit aa09283930b59044ef422678c5c01c09714586e9
parent 5dad82a85873421893b91b54bb9ed3303274c018
Author: Shivakumar <[email protected]>
Date: Fri, 31 Jul 2026 19:04:52 +0530
feat(registry): add registry to resolve publickey from did
Signed-off-by: Shivakumar <[email protected]>
Diffstat:
3 files changed, 127 insertions(+), 0 deletions(-)
diff --git a/internal/blockchain/blockchain.go b/internal/blockchain/blockchain.go
@@ -16,6 +16,7 @@ package blockchain
import (
"bytes"
+ "crypto/ed25519"
"errors"
"fmt"
"iter"
@@ -37,6 +38,11 @@ var (
ErrInvalidTxID = errors.New("blockchain: transaction has invalid TxID")
)
+type KeyResolver interface {
+ // Get retrives the ed25519 public key for the DID.
+ Get(did string) (ed25519.PublicKey, error)
+}
+
// Blockchain manages the chain of blocks and provides a high-level API for
// storage and retrieval.
type Blockchain struct {
diff --git a/internal/registry/registry.go b/internal/registry/registry.go
@@ -0,0 +1,115 @@
+// Copyright 2026 Shivakumar (Certichain)
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package registry
+
+import (
+ "crypto/ed25519"
+ "errors"
+ "fmt"
+
+ "certichain/internal/crypto"
+ "certichain/internal/storage"
+)
+
+// Registry maps the institution DID to their ed25519 public key, persisted via
+// a storage.Store. It allows the blockchain layer to resolve a transaction's
+// DID to the key needed for signature verification.
+type Registry struct {
+ store storage.Store
+}
+
+var (
+ ErrInvalidDID = errors.New("registry: invalid DID")
+ ErrInvalidKey = errors.New("registry: invalid public key")
+ ErrAlreadyExists = errors.New("registry: institution already exists")
+)
+
+// New returns a Registry backed by store.
+func New(store storage.Store) *Registry {
+ if store == nil {
+ panic("registry: nil store")
+ }
+ return &Registry{store: store}
+}
+
+// Register derives a DID from their ed25519 public key and persists the mapping.
+func (r *Registry) Register(pubKey ed25519.PublicKey) (string, error) {
+ if len(pubKey) != ed25519.PublicKeySize {
+ return "", ErrInvalidKey
+ }
+
+ did, err := crypto.NewDID(pubKey)
+ if err != nil {
+ return "", err
+ }
+
+ got, err := r.Has(did)
+ if err != nil {
+ return "", err
+ }
+ if got == true {
+ return "", ErrAlreadyExists
+ }
+
+ if err := r.store.Set(storage.InstitutionKey(did), pubKey); err != nil {
+ return "", fmt.Errorf("registry: persisting institution: %v", err)
+ }
+
+ return did, nil
+}
+
+// Get retrives the public key registred for DID.
+func (r *Registry) Get(did string) (ed25519.PublicKey, error) {
+ if !crypto.IsDID(did) {
+ return nil, ErrInvalidDID
+ }
+
+ data, err := r.store.Get(storage.InstitutionKey(did))
+ if err != nil {
+ return nil, err
+ }
+ if len(data) != ed25519.PublicKeySize {
+ return nil, fmt.Errorf("registry: corrupted key data for DID %s: DID length: %d",
+ did,
+ len(data))
+ }
+
+ return ed25519.PublicKey(data), nil
+}
+
+// Has checks whether an institution is registered under the given DID.
+func (r *Registry) Has(did string) (bool, error) {
+ if !crypto.IsDID(did) {
+ return false, ErrInvalidDID
+ }
+ return r.store.Has(storage.InstitutionKey(did))
+}
+
+// Revoke removes the registration for the given DID.
+func (r *Registry) Revoke(did string) error {
+ if !crypto.IsDID(did) {
+ return ErrInvalidDID
+ }
+
+ exists, err := r.Has(did)
+ if err != nil {
+ return err
+ }
+ if !exists {
+ return storage.ErrNotFound
+ }
+
+ return r.store.Delete(storage.InstitutionKey(did))
+}
diff --git a/internal/storage/store.go b/internal/storage/store.go
@@ -56,6 +56,7 @@ const (
PrefixBatch = "batch:"
PrefixHeight = "height:"
PrefixTxIndex = "txidx:"
+ PrefixInstitution = "institution:"
)
// BlockKey returns the database key for a block hash by prepending the block prefix.
@@ -91,3 +92,8 @@ func TxIndexKey(txID []byte) []byte {
func LastHash() []byte {
return []byte(LastHashKey)
}
+
+// InstitutionKey returns the institution did.
+func InstitutionKey(did string) []byte {
+ return append([]byte(PrefixInstitution), []byte(did)...)
+}