From f6487958f3990edff66eff6db5d76cf899dbb2b6 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 03:51:23 +0000 Subject: [PATCH] feat(lns): add hash-based lookup helpers Co-Authored-By: Virgil --- lns.go | 28 ++++++++++++++++++++++++++++ lns_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/lns.go b/lns.go index e6d4969..68ebf92 100644 --- a/lns.go +++ b/lns.go @@ -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 diff --git a/lns_test.go b/lns_test.go index c0cc013..6258fe5 100644 --- a/lns_test.go +++ b/lns_test.go @@ -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()