certichain

Blockchain built from scratch for verifying document's legitimacy.
Log | Files | Refs | README | LICENSE

registry.go (2940B)


      1 // Copyright 2026 Shivakumar (Certichain)
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package registry
     16 
     17 import (
     18 	"crypto/ed25519"
     19 	"errors"
     20 	"fmt"
     21 
     22 	"certichain/internal/crypto"
     23 	"certichain/internal/storage"
     24 )
     25 
     26 // Registry maps the institution DID to their ed25519 public key, persisted via
     27 // a storage.Store. It allows the blockchain layer to resolve a transaction's
     28 // DID to the key needed for signature verification.
     29 type Registry struct {
     30 	store storage.Store
     31 }
     32 
     33 var (
     34 	ErrInvalidDID    = errors.New("registry: invalid DID")
     35 	ErrInvalidKey    = errors.New("registry: invalid public key")
     36 	ErrAlreadyExists = errors.New("registry: institution already exists")
     37 )
     38 
     39 // New returns a Registry backed by store.
     40 func New(store storage.Store) *Registry {
     41 	if store == nil {
     42 		panic("registry: nil store")
     43 	}
     44 	return &Registry{store: store}
     45 }
     46 
     47 // Register derives a DID from their ed25519 public key and persists the mapping.
     48 func (r *Registry) Register(pubKey ed25519.PublicKey) (string, error) {
     49 	if len(pubKey) != ed25519.PublicKeySize {
     50 		return "", ErrInvalidKey
     51 	}
     52 
     53 	did, err := crypto.NewDID(pubKey)
     54 	if err != nil {
     55 		return "", err
     56 	}
     57 
     58 	got, err := r.Has(did)
     59 	if err != nil {
     60 		return "", err
     61 	}
     62 	if got == true {
     63 		return "", ErrAlreadyExists
     64 	}
     65 
     66 	if err := r.store.Set(storage.InstitutionKey(did), pubKey); err != nil {
     67 		return "", fmt.Errorf("registry: persisting institution: %v", err)
     68 	}
     69 
     70 	return did, nil
     71 }
     72 
     73 // Get retrives the public key registred for DID.
     74 func (r *Registry) Get(did string) (ed25519.PublicKey, error) {
     75 	if !crypto.IsDID(did) {
     76 		return nil, ErrInvalidDID
     77 	}
     78 
     79 	data, err := r.store.Get(storage.InstitutionKey(did))
     80 	if err != nil {
     81 		return nil, err
     82 	}
     83 	if len(data) != ed25519.PublicKeySize {
     84 		return nil, fmt.Errorf("registry: corrupted key data for DID %s: DID length: %d",
     85 			did,
     86 			len(data))
     87 	}
     88 
     89 	return ed25519.PublicKey(data), nil
     90 }
     91 
     92 // Has checks whether an institution is registered under the given DID.
     93 func (r *Registry) Has(did string) (bool, error) {
     94 	if !crypto.IsDID(did) {
     95 		return false, ErrInvalidDID
     96 	}
     97 	return r.store.Has(storage.InstitutionKey(did))
     98 }
     99 
    100 // Revoke removes the registration for the given DID.
    101 func (r *Registry) Revoke(did string) error {
    102 	if !crypto.IsDID(did) {
    103 		return ErrInvalidDID
    104 	}
    105 
    106 	exists, err := r.Has(did)
    107 	if err != nil {
    108 		return err
    109 	}
    110 	if !exists {
    111 		return storage.ErrNotFound
    112 	}
    113 
    114 	return r.store.Delete(storage.InstitutionKey(did))
    115 }