Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Simplex/Messaging/Names/Record.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
Expand All @@ -11,6 +13,9 @@ module Simplex.Messaging.Names.Record
NameResponse (..),
NameRegistration (..),
NamePricing (..),
NameTerms (..),
TermsDoc (..),
ZstdTerms (..),
USDCents (..),
NameReservedReason (..),
)
Expand All @@ -19,6 +24,7 @@ where
import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.Aeson as J
import qualified Data.Aeson.TH as JQ
import Data.ByteString (ByteString)
import Data.Int (Int64)
import Data.Map.Strict (Map)
import Data.Text (Text)
Expand Down Expand Up @@ -85,7 +91,11 @@ data NameRegistration
nameRecord :: NameRecord
}
| -- | Held by nobody, and registrable now.
NRAvailable {pricing :: NamePricing}
NRAvailable
{ pricing :: NamePricing,
-- | absent only from a router before v24
nameTerms :: Maybe NameTerms
}
| -- | Held back by the registry, and not for sale at its price.
NRReserved {reservedReason :: NameReservedReason}
deriving (Eq, Show)
Expand All @@ -101,6 +111,26 @@ data NamePricing = NamePricing
}
deriving (Eq, Show)

-- | The terms a buyer of the name accepts, which the name policy keeps.
data NameTerms
= -- | the general name terms
NTName {termsDoc :: TermsDoc}
| -- | a bona fide community name's community terms, which include the general ones
NTBonaFide {termsDoc :: TermsDoc}
deriving (Eq, Show)

-- | Terms as the name policy keeps them: their text in one zstd frame, whose keccak256 a buyer accepts.
data TermsDoc
= -- | the frame, when it fits the answer
TDZstd {termsZstd :: ZstdTerms}
| -- | the 0x hex keccak256 of the frame, when it does not
TDHash {termsHash :: Text}
deriving (Eq, Show)

newtype ZstdTerms = ZstdTerms ByteString
deriving newtype (Eq, Show, StrEncoding)
deriving (ToJSON, FromJSON) via (StrJSON "ZstdTerms" ZstdTerms)

-- | Why the registry holds a name back.
data NameReservedReason
= -- | held for SimpleX
Expand Down Expand Up @@ -137,6 +167,10 @@ instance FromJSON NameReservedReason where

$(JQ.deriveJSON defaultJSON ''NamePricing)

$(JQ.deriveJSON (taggedObjectJSON $ dropPrefix "TD") ''TermsDoc)

$(JQ.deriveJSON (taggedObjectJSON $ dropPrefix "NT") ''NameTerms)

-- taggedObjectJSON, not sumTypeJSON: this JSON is the RNAME payload and the
-- resolver contract, so it must not vary with the swift build flag.
$(JQ.deriveJSON (taggedObjectJSON $ dropPrefix "NR") ''NameRegistration)
Expand Down
2 changes: 1 addition & 1 deletion tests/RSLVTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ testRslvAvailable =
testSMPClient @TLS $ \h -> do
(corrId, _entId, resp) <- sendRslv h "na01" (domain "ghost.simplex")
corrId `shouldBe` CorrId "na01"
resp `shouldBe` Right (RNAME (resolved (NRAvailable testPricing)))
resp `shouldBe` Right (RNAME (resolved (NRAvailable testPricing Nothing)))

testRslvReserved :: HasTestEnv => IO ()
testRslvReserved =
Expand Down
4 changes: 2 additions & 2 deletions tests/SMPNamesTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ availabilitySpec = do
answers heldBackBody . resolved $
NRRegistered {expires = Just (RoundedSystemTime 1813853483), graceUntil = Just (RoundedSystemTime 1821629483), reservedReason_ = Just NRRInternal, nameRecord = testNameRecord}
it "an unregistered name answers with the price" $
answers availableBody (resolved NRAvailable {pricing = testPricing})
answers availableBody (resolved NRAvailable {pricing = testPricing, nameTerms = Nothing})
it "reserved carries the reason and no price" $
answers reservedBody (resolved $ NRReserved NRRTrademark)
-- losing the reservation would offer a name that cannot be registered
Expand All @@ -175,7 +175,7 @@ availabilitySpec = do
(\a -> J.eitherDecodeStrict (LB.toStrict (J.encode a)) `shouldBe` Right a)
[ registeredAlice,
NRRegistered {expires = Nothing, graceUntil = Nothing, reservedReason_ = Just NRRInternal, nameRecord = testNameRecord},
NRAvailable {pricing = testPricing},
NRAvailable {pricing = testPricing, nameTerms = Nothing},
NRReserved NRRInternal,
NRReserved NRRTrademark,
NRReserved NRRCommunity,
Expand Down