feat(lns): add hash-based lookup helpers

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 03:51:23 +00:00
parent 4523198f4b
commit f6487958f3
2 changed files with 81 additions and 0 deletions

28
lns.go
View file

@ -91,6 +91,20 @@ func (s *Service) HasReserved(name any) bool {
return ok
}
// GetReservedHash looks up a reserved .lthn name by its canonical hash.
//
// This mirrors the covenant package lookup helper for callers that already
// have the hashed form available.
func (s *Service) GetReservedHash(hash primitives.Hash) (covenant.ReservedName, bool) {
return covenant.GetReservedHash(hash)
}
// HasReservedHash reports whether a canonical hash is reserved.
func (s *Service) HasReservedHash(hash primitives.Hash) bool {
_, ok := s.GetReservedHash(hash)
return ok
}
// GetLocked looks up a locked .lthn name after canonicalisation.
//
// The lookup accepts the same inputs as Resolve and Verify, then returns the
@ -113,6 +127,20 @@ func (s *Service) HasLocked(name any) bool {
return ok
}
// GetLockedHash looks up a locked .lthn name by its canonical hash.
//
// This mirrors the covenant package lookup helper for callers that already
// have the hashed form available.
func (s *Service) GetLockedHash(hash primitives.Hash) (covenant.LockedName, bool) {
return covenant.GetLockedHash(hash)
}
// HasLockedHash reports whether a canonical hash is locked.
func (s *Service) HasLockedHash(hash primitives.Hash) bool {
_, ok := s.GetLockedHash(hash)
return ok
}
// ReservedCatalog exposes the reserved-name catalog used by the service.
//
// The returned catalog provides deterministic iteration helpers so callers can

View file

@ -7,6 +7,7 @@ import (
"testing"
"dappco.re/go/lns/pkg/covenant"
"dappco.re/go/lns/pkg/primitives"
)
func TestServiceResolve(t *testing.T) {
@ -132,6 +133,32 @@ func TestServiceHasReserved(t *testing.T) {
}
}
func TestServiceGetReservedHash(t *testing.T) {
svc := &Service{}
hash, err := covenant.HashString("reserved")
if err != nil {
t.Fatalf("HashString returned error: %v", err)
}
item, ok := svc.GetReservedHash(hash)
if !ok {
t.Fatal("GetReservedHash should find the reserved reference entry")
}
if item.Name != "reserved" {
t.Fatalf("item.Name = %q, want %q", item.Name, "reserved")
}
if !svc.HasReservedHash(hash) {
t.Fatal("HasReservedHash should report canonical reserved hashes")
}
if svc.HasReservedHash(primitives.Hash{}) {
t.Fatal("HasReservedHash should return false for unknown hashes")
}
}
func TestServiceGetLocked(t *testing.T) {
svc := &Service{}
@ -181,6 +208,32 @@ func TestServiceHasLocked(t *testing.T) {
}
}
func TestServiceGetLockedHash(t *testing.T) {
svc := &Service{}
hash, err := covenant.HashString("nec")
if err != nil {
t.Fatalf("HashString returned error: %v", err)
}
item, ok := svc.GetLockedHash(hash)
if !ok {
t.Fatal("GetLockedHash should find the locked reference entry")
}
if item.Name != "nec" {
t.Fatalf("item.Name = %q, want %q", item.Name, "nec")
}
if !svc.HasLockedHash(hash) {
t.Fatal("HasLockedHash should report canonical locked hashes")
}
if svc.HasLockedHash(primitives.Hash{}) {
t.Fatal("HasLockedHash should return false for unknown hashes")
}
}
func TestServiceReservedCatalog(t *testing.T) {
svc := &Service{}
catalog := svc.ReservedCatalog()