README.md raw

Directory Client Library

High-level Go client library for the Distributed Directory Consensus Protocol (NIP-XX).

Overview

This package provides a convenient API for working with directory events, managing identity resolution, tracking key delegations, and computing trust scores. It builds on the lower-level directory protocol package.

Features

Installation

import "next.orly.dev/pkg/protocol/directory-client"

Quick Start

Identity Resolution

// Create an identity resolver
resolver := directory_client.NewIdentityResolver()

// Process events to build identity mappings
for _, event := range events {
    resolver.ProcessEvent(event)
}

// Resolve identity behind a delegate key
actualIdentity := resolver.ResolveIdentity(delegateKey)

// Check if a key is a delegate
if resolver.IsDelegateKey(pubkey) {
    tag, _ := resolver.GetIdentityTag(pubkey)
    fmt.Printf("Delegate belongs to: %s\n", tag.Identity)
}

// Get all delegates for an identity
delegates := resolver.GetDelegatesForIdentity(identityPubkey)

// Filter events by identity (including delegates)
filteredEvents := resolver.FilterEventsByIdentity(events, identityPubkey)

Trust Management

// Create a trust calculator
calculator := directory_client.NewTrustCalculator()

// Add trust acts
for _, event := range trustEvents {
    if act, err := directory.ParseTrustAct(event); err == nil {
        calculator.AddAct(act)
    }
}

// Calculate aggregate trust score (0-100)
score := calculator.CalculateTrust(targetPubkey)

// Get active (non-expired) trust acts
activeActs := calculator.GetActiveTrustActs(targetPubkey)

Replication Filtering

// Create filter with minimum trust score threshold
filter := directory_client.NewReplicationFilter(50)

// Add trust acts
for _, act := range trustActs {
    filter.AddTrustAct(act)
}

// Check if should replicate from a relay
if filter.ShouldReplicate(relayPubkey) {
    // Proceed with replication
}

// Get all trusted relays
trustedRelays := filter.GetTrustedRelays()

// Filter events to only trusted sources
trustedEvents := filter.FilterEvents(events)

Event Collection

// Create event collector
collector := directory_client.NewEventCollector(events)

// Extract specific event types
identities := collector.RelayIdentities()
trustActs := collector.TrustActs()
groupTagActs := collector.GroupTagActs()
keyAds := collector.PublicKeyAdvertisements()
requests := collector.ReplicationRequests()
responses := collector.ReplicationResponses()

// Find specific events
identity, found := directory_client.FindRelayIdentity(events, "wss://relay.example.com/")
trustActs := directory_client.FindTrustActsForRelay(events, targetPubkey)
groupActs := directory_client.FindGroupTagActsByGroup(events, "premium")

Trust Graph Analysis

// Build trust graph from events
graph := directory_client.BuildTrustGraph(events)

// Find who trusts a relay
trustedBy := graph.GetTrustedBy(targetPubkey)
fmt.Printf("Trusted by %d relays\n", len(trustedBy))

// Find who a relay trusts
targets := graph.GetTrustTargets(sourcePubkey)
fmt.Printf("Trusts %d relays\n", len(targets))

// Get all trust acts from a source
acts := graph.GetTrustActs(sourcePubkey)

API Reference

IdentityResolver

Manages identity resolution and key delegation tracking.

Methods:

TrustCalculator

Computes aggregate trust scores from multiple trust acts.

Methods:

Trust Score Weights:

ReplicationFilter

Manages replication decisions based on trust scores.

Methods:

EventCollector

Utility for collecting specific types of directory events.

Methods:

Helper Functions

Event Filtering:

Event Finding:

URL Utilities:

Trust Graph:

Thread Safety

All components are thread-safe and can be used concurrently from multiple goroutines. Internal state is protected by read-write mutexes (sync.RWMutex).

Performance Considerations

Integration

This package works with:

Testing

The directory-client package includes comprehensive tests to ensure reliability and correctness:

Running Tests

# Run directory-client tests
go test ./pkg/protocol/directory-client

# Run all tests including directory-client
go test ./...

# Run with verbose output
go test -v ./pkg/protocol/directory-client

# Run with race detection
go test -race ./pkg/protocol/directory-client

# Run with coverage
go test -cover ./pkg/protocol/directory-client

Integration Testing

The directory-client is tested as part of the project's integration test suite:

# Run the full test suite
./scripts/test.sh

# Run specific package tests
go test ./pkg/protocol/...

Test Coverage

The test suite covers:

Example Test Usage

# Test identity resolution functionality
go test -v ./pkg/protocol/directory-client -run TestIdentityResolver

# Test trust calculation
go test -v ./pkg/protocol/directory-client -run TestTrustCalculator

# Test thread safety
go test -race -v ./pkg/protocol/directory-client

Development

Building and Usage

# Build the directory-client package
go build ./pkg/protocol/directory-client

# Run example usage
go run -tags=example ./pkg/protocol/directory-client

Code Quality

The directory-client follows Go best practices:

Adding New Features

When adding new functionality:

  1. Add unit tests for new components
  2. Update existing tests if behavior changes
  3. Ensure thread safety for concurrent access
  4. Add documentation and examples
  5. Update the API reference section

Related Documentation

License

See LICENSE file.