feat(dns): fall back mainchain auth to HSD credentials

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 23:51:36 +00:00
parent f1c0f9cf2b
commit edb852ce23
3 changed files with 39 additions and 10 deletions

View file

@ -566,3 +566,34 @@ func TestExtractAliasFromCommentParsesCaseInsensitiveHNSPrefix(t *testing.T) {
t.Fatalf("expected gateway.charon.lthn, got %s", got)
}
}
func TestNewServiceBuildsMainchainAliasClientWithHSDFallbackCredentials(t *testing.T) {
service := NewService(ServiceOptions{
MainchainURL: "http://127.0.0.1:14037",
HSDUsername: "mainchain-user",
HSDPassword: "mainchain-pass",
HSDApiKey: "ignored-token",
})
if service.mainchainAliasClient == nil {
t.Fatal("expected default mainchain alias client with fallback credentials")
}
if got := service.mainchainAliasClient.username; got != "mainchain-user" {
t.Fatalf("expected mainchain username to fall back to hsd username, got %q", got)
}
if got := service.mainchainAliasClient.password; got != "mainchain-pass" {
t.Fatalf("expected mainchain password to fall back to hsd password, got %q", got)
}
serviceFromToken := NewService(ServiceOptions{
MainchainURL: "http://127.0.0.1:14037",
HSDUsername: "token-user",
HSDApiKey: "token-pass",
})
if serviceFromToken.mainchainAliasClient == nil {
t.Fatal("expected default mainchain alias client with fallback token credentials")
}
if got := serviceFromToken.mainchainAliasClient.password; got != "token-pass" {
t.Fatalf("expected mainchain password to fall back to hsd api key token, got %q", got)
}
}

View file

@ -210,17 +210,15 @@ func NewService(options ServiceOptions) *Service {
}
mainchainPassword := options.MainchainPassword
mainchainUsername := options.MainchainUsername
if strings.TrimSpace(options.MainchainURL) == "" {
if mainchainPassword == "" {
mainchainPassword = options.HSDPassword
if mainchainPassword == "" {
mainchainPassword = options.HSDPassword
if mainchainPassword == "" {
mainchainPassword = options.HSDApiKey
}
}
if mainchainUsername == "" {
mainchainUsername = options.HSDUsername
mainchainPassword = options.HSDApiKey
}
}
if mainchainUsername == "" {
mainchainUsername = options.HSDUsername
}
mainchainClient = NewMainchainAliasClient(MainchainClientOptions{
URL: mainchainURL,
Username: mainchainUsername,

View file

@ -1035,8 +1035,8 @@ func TestNewServiceBuildsRPCClientsFromOptions(t *testing.T) {
switch payload.Method {
case "get_all_alias_details":
atomic.AddInt32(&chainCalls, 1)
if got := request.Header.Get("Authorization"); got != "" {
t.Fatalf("expected no auth header for mainchain request, got %q", got)
if got := request.Header.Get("Authorization"); got != expectedAuth {
t.Fatalf("expected hsd-auth header for mainchain request %q, got %q", expectedAuth, got)
}
responseWriter.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(responseWriter).Encode(map[string]any{