carlos / platform Public

domainreg: TLD catalog, name normalization, quoting with a cost ceiling

Co-Authored-By: Astra (gpt-6-astra) via Codex <noreply@openai.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 7a517cdb6501e23aa219ecccf11fbef5c0974179 parent 3be6b5c
4 files changed, +325 −0
  • docs/superpowers/specs/2026-11-01-domain-registration-design.md +6 −0
  • internal/domainreg/catalog.go +136 −0
  • internal/domainreg/catalog_test.go +162 −0
  • internal/domainreg/testdata/catalog.json +21 −0
diff --git a/docs/superpowers/specs/2026-11-01-domain-registration-design.md b/docs/superpowers/specs/2026-11-01-domain-registration-design.md
index 028eb8dc1..aa89fa90b 100644
--- a/docs/superpowers/specs/2026-11-01-domain-registration-design.md
+++ b/docs/superpowers/specs/2026-11-01-domain-registration-design.md
@@ -332,6 +332,12 @@ otherwise the TLD is refused with "not available right now" and the
operator is alerted. That protects against a registry price rise (the
.com fee rose on 2026-11-01) silently selling at a loss.
+Implementation note (Plan 1): the margin guard is a per-TLD `max_cost`
+in the registrar's currency, so no exchange rate is needed. A purchase
+quote is refused when the registrar's cost exceeds that ceiling or its
+currency differs from the ceiling's currency. The customer pays the
+catalog's fixed registration price in their selected currency.
+
Renewals are never blocked by the margin rule. If the registrar's renewal
cost has risen above the catalog price, the renewal still goes through at
the price the owner was told, Carloku absorbs the difference, and the
diff --git a/internal/domainreg/catalog.go b/internal/domainreg/catalog.go
new file mode 100644
index 000000000..8af9c831a
--- /dev/null
+++ b/internal/domainreg/catalog.go
@@ -0,0 +1,136 @@
+package domainreg
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "strings"
+
+ "github.com/carlosframework/platform/internal/registrar"
+ "golang.org/x/net/idna"
+ "golang.org/x/net/publicsuffix"
+)
+
+type TLD struct {
+ Register map[string]int64 `json:"register"` // currency -> minor units, one year
+ Renew map[string]int64 `json:"renew"`
+ MaxCost registrar.Money `json:"max_cost"`
+}
+
+type Catalog struct {
+ Version string `json:"version"`
+ TLDs map[string]TLD `json:"tlds"`
+ DailyCap int `json:"daily_cap"`
+}
+
+type Quote struct {
+ Domain, TLD, Currency string
+ Price int64
+ Cost registrar.Money
+}
+
+var (
+ ErrNotRegistrable = errors.New("domainreg: name is not a registrable domain")
+ ErrTLDNotOffered = errors.New("domainreg: TLD not offered")
+ ErrPremium = errors.New("domainreg: premium domain")
+ ErrNotAvailable = errors.New("domainreg: domain not available")
+ ErrCostAboveCeiling = errors.New("domainreg: registrar cost exceeds ceiling or cannot be compared")
+)
+
+func LoadCatalog(path string) (*Catalog, error) {
+ b, err := os.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ dec := json.NewDecoder(bytes.NewReader(b))
+ dec.DisallowUnknownFields()
+ var c Catalog
+ if err := dec.Decode(&c); err != nil {
+ return nil, fmt.Errorf("%s: domain catalog: %w", path, err)
+ }
+ if err := dec.Decode(new(json.RawMessage)); err != io.EOF {
+ return nil, fmt.Errorf("%s: domain catalog: trailing content", path)
+ }
+ if err := c.validate(); err != nil {
+ return nil, fmt.Errorf("%s: domain catalog: %w", path, err)
+ }
+ return &c, nil
+}
+
+func (c *Catalog) validate() error {
+ if c.Version == "" || len(c.TLDs) == 0 || c.DailyCap < 0 {
+ return errors.New("version and TLDs are required; daily_cap must be non-negative")
+ }
+ for name, tld := range c.TLDs {
+ if normalized, err := Normalize("catalog." + name); err != nil || normalized != "catalog."+name {
+ return fmt.Errorf("invalid TLD %q", name)
+ }
+ if !validCurrency(tld.MaxCost.Currency) || tld.MaxCost.Minor < 0 {
+ return fmt.Errorf("TLD %q: invalid max_cost", name)
+ }
+ for kind, prices := range map[string]map[string]int64{"register": tld.Register, "renew": tld.Renew} {
+ if len(prices) == 0 {
+ return fmt.Errorf("TLD %q: %s prices are required", name, kind)
+ }
+ for currency, price := range prices {
+ if !validCurrency(currency) || price <= 0 {
+ return fmt.Errorf("TLD %q: invalid %s price for %q", name, kind, currency)
+ }
+ }
+ }
+ }
+ return nil
+}
+
+func validCurrency(currency string) bool {
+ return len(currency) == 3 && currency[0] >= 'a' && currency[0] <= 'z' &&
+ currency[1] >= 'a' && currency[1] <= 'z' && currency[2] >= 'a' && currency[2] <= 'z'
+}
+
+func Normalize(name string) (string, error) {
+ name = strings.TrimSuffix(strings.ToLower(name), ".")
+ name, err := idna.Lookup.ToASCII(name)
+ if err != nil || len(name) > 253 {
+ return "", ErrNotRegistrable
+ }
+ for _, label := range strings.Split(name, ".") {
+ if len(label) == 0 || len(label) > 63 {
+ return "", ErrNotRegistrable
+ }
+ }
+ registrable, err := publicsuffix.EffectiveTLDPlusOne(name)
+ if err != nil || registrable != name {
+ return "", ErrNotRegistrable
+ }
+ return name, nil
+}
+
+func (c *Catalog) Quote(domain, currency string, a registrar.Availability) (Quote, error) {
+ domain, err := Normalize(domain)
+ if err != nil {
+ return Quote{}, err
+ }
+ suffix, _ := publicsuffix.PublicSuffix(domain)
+ tld, ok := c.TLDs[suffix]
+ if !ok {
+ return Quote{}, ErrTLDNotOffered
+ }
+ if a.Premium {
+ return Quote{}, ErrPremium
+ }
+ if !a.Available {
+ return Quote{}, ErrNotAvailable
+ }
+ if !validCurrency(tld.MaxCost.Currency) || a.Cost.Currency != tld.MaxCost.Currency ||
+ a.Cost.Minor < 0 || a.Cost.Minor > tld.MaxCost.Minor {
+ return Quote{}, ErrCostAboveCeiling
+ }
+ price, ok := tld.Register[currency]
+ if !ok || price <= 0 {
+ return Quote{}, fmt.Errorf("%w: %s in %q", ErrTLDNotOffered, suffix, currency)
+ }
+ return Quote{Domain: domain, TLD: suffix, Currency: currency, Price: price, Cost: a.Cost}, nil
+}
diff --git a/internal/domainreg/catalog_test.go b/internal/domainreg/catalog_test.go
new file mode 100644
index 000000000..0f89cedbd
--- /dev/null
+++ b/internal/domainreg/catalog_test.go
@@ -0,0 +1,162 @@
+package domainreg
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registrar"
+)
+
+func testCatalog(t *testing.T) *Catalog {
+ t.Helper()
+ c, err := LoadCatalog("testdata/catalog.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ return c
+}
+
+func TestQuoteNormalizesAndRefusesNonRegistrable(t *testing.T) {
+ c := testCatalog(t)
+ for _, tt := range []struct{ input, want, tld string }{
+ {"Shop.Example.", "shop.example", "example"},
+ {"bücher.example", "xn--bcher-kva.example", "example"},
+ {"shop.co.uk", "shop.co.uk", "co.uk"},
+ {"www.shop.example", "", ""},
+ {"example", "", ""},
+ {"co.uk", "", ""},
+ {"shop.example.co.uk", "", ""},
+ {"", "", ""},
+ {"shop..example", "", ""},
+ {"shop.example..", "", ""},
+ {" shop.example", "", ""},
+ {"-shop.example", "", ""},
+ {"shop_.example", "", ""},
+ {strings.Repeat("a", 64) + ".example", "", ""},
+ } {
+ t.Run(tt.input, func(t *testing.T) {
+ name, err := Normalize(tt.input)
+ q, quoteErr := c.Quote(tt.input, "gbp", registrar.Availability{
+ Domain: tt.want, Available: true, Cost: registrar.Money{Currency: "eur", Minor: 1000},
+ })
+ if tt.want == "" {
+ if !errors.Is(err, ErrNotRegistrable) || !errors.Is(quoteErr, ErrNotRegistrable) {
+ t.Fatalf("Normalize error = %v, Quote error = %v; want ErrNotRegistrable", err, quoteErr)
+ }
+ if name != "" || q != (Quote{}) {
+ t.Fatalf("refused name = %q, quote = %+v", name, q)
+ }
+ return
+ }
+ if err != nil || name != tt.want {
+ t.Fatalf("Normalize = %q, %v; want %q", name, err, tt.want)
+ }
+ if quoteErr != nil || q.Domain != tt.want || q.TLD != tt.tld {
+ t.Fatalf("Quote = %+v, %v; want domain %q, TLD %q", q, quoteErr, tt.want, tt.tld)
+ }
+ })
+ }
+}
+
+func TestQuoteRefusals(t *testing.T) {
+ c := testCatalog(t)
+ for _, tt := range []struct {
+ name, domain, currency string
+ available, premium bool
+ cost registrar.Money
+ want error
+ }{
+ {"unknown TLD", "shop.net", "gbp", true, false, registrar.Money{Currency: "eur", Minor: 1000}, ErrTLDNotOffered},
+ {"premium", "shop.com", "gbp", true, true, registrar.Money{Currency: "eur", Minor: 1000}, ErrPremium},
+ {"unavailable", "shop.com", "gbp", false, false, registrar.Money{Currency: "eur", Minor: 1000}, ErrNotAvailable},
+ {"above ceiling", "shop.com", "gbp", true, false, registrar.Money{Currency: "eur", Minor: 1201}, ErrCostAboveCeiling},
+ {"different cost currency", "shop.com", "gbp", true, false, registrar.Money{Currency: "usd", Minor: 1000}, ErrCostAboveCeiling},
+ {"missing cost currency", "shop.com", "gbp", true, false, registrar.Money{Currency: "", Minor: 1000}, ErrCostAboveCeiling},
+ {"negative cost", "shop.com", "gbp", true, false, registrar.Money{Currency: "eur", Minor: -1}, ErrCostAboveCeiling},
+ {"unpriced currency", "shop.com", "usd", true, false, registrar.Money{Currency: "eur", Minor: 1000}, ErrTLDNotOffered},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ q, err := c.Quote(tt.domain, tt.currency, registrar.Availability{
+ Domain: tt.domain, Available: tt.available, Premium: tt.premium, Cost: tt.cost,
+ })
+ if !errors.Is(err, tt.want) || q != (Quote{}) {
+ t.Fatalf("Quote = %+v, %v; want empty quote, %v", q, err, tt.want)
+ }
+ })
+ }
+}
+
+func TestQuotePrice(t *testing.T) {
+ c := testCatalog(t)
+ if c.Version != "test-v1" || c.DailyCap != 10 || c.TLDs["com"].Renew["gbp"] != 1600 {
+ t.Fatalf("catalog = %+v", c)
+ }
+ for _, minor := range []int64{1000, 1200} {
+ cost := registrar.Money{Currency: "eur", Minor: minor}
+ q, err := c.Quote("Shop.COM.", "gbp", registrar.Availability{Domain: "shop.com", Available: true, Cost: cost})
+ want := Quote{Domain: "shop.com", TLD: "com", Currency: "gbp", Price: 1500, Cost: cost}
+ if err != nil || q != want {
+ t.Fatalf("Quote = %+v, %v; want %+v", q, err, want)
+ }
+ }
+}
+
+func TestLoadCatalogRejectsUnknownFields(t *testing.T) {
+ data, err := os.ReadFile("testdata/catalog.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, tt := range []struct{ name, from, to string }{
+ {"catalog", `"daily_cap":`, `"daily_capp":`},
+ {"TLD", `"register":`, `"registerr":`},
+ {"money", `"minor":`, `"minorr":`},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "catalog.json")
+ if err := os.WriteFile(path, []byte(strings.Replace(string(data), tt.from, tt.to, 1)), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if c, err := LoadCatalog(path); err == nil || c != nil || !strings.Contains(err.Error(), "unknown field") {
+ t.Fatalf("LoadCatalog = %+v, %v; want unknown field error", c, err)
+ }
+ })
+ }
+}
+
+func TestLoadCatalogRejectsInvalidConfiguration(t *testing.T) {
+ data, err := os.ReadFile("testdata/catalog.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, tt := range []struct{ name, from, to string }{
+ {"missing version", `"test-v1"`, `""`},
+ {"negative daily cap", `"daily_cap": 10`, `"daily_cap": -1`},
+ {"subdomain TLD", `"com":`, `"shop.com":`},
+ {"missing ceiling currency", `"currency": "eur"`, `"currency": ""`},
+ {"negative ceiling", `"minor": 1200`, `"minor": -1`},
+ {"zero registration price", `"gbp": 1500`, `"gbp": 0`},
+ {"negative renewal price", `"gbp": 1600`, `"gbp": -1`},
+ {"invalid price currency", `"gbp": 1500`, `"GBP": 1500`},
+ {"missing registration prices", `"register": {"gbp": 1000}`, `"register": {}`},
+ {"missing renewal prices", `"renew": {"gbp": 1100}`, `"renew": null`},
+ {"trailing document", "", ""},
+ {"null catalog", string(data), "null"},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ body := strings.Replace(string(data), tt.from, tt.to, 1)
+ if tt.name == "trailing document" {
+ body += "{}"
+ }
+ path := filepath.Join(t.TempDir(), "catalog.json")
+ if err := os.WriteFile(path, []byte(body), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if c, err := LoadCatalog(path); err == nil || c != nil {
+ t.Fatalf("LoadCatalog = %+v, %v; want error and no catalog", c, err)
+ }
+ })
+ }
+}
diff --git a/internal/domainreg/testdata/catalog.json b/internal/domainreg/testdata/catalog.json
new file mode 100644
index 000000000..3789e4a7f
--- /dev/null
+++ b/internal/domainreg/testdata/catalog.json
@@ -0,0 +1,21 @@
+{
+ "version": "test-v1",
+ "daily_cap": 10,
+ "tlds": {
+ "com": {
+ "register": {"gbp": 1500, "eur": 1800},
+ "renew": {"gbp": 1600, "eur": 1900},
+ "max_cost": {"currency": "eur", "minor": 1200}
+ },
+ "example": {
+ "register": {"gbp": 1500},
+ "renew": {"gbp": 1600},
+ "max_cost": {"currency": "eur", "minor": 1200}
+ },
+ "co.uk": {
+ "register": {"gbp": 1000},
+ "renew": {"gbp": 1100},
+ "max_cost": {"currency": "eur", "minor": 1200}
+ }
+ }
+}