diff --git a/apps/simplex-store-service/src/StoreService/Names/Registration.hs b/apps/simplex-store-service/src/StoreService/Names/Registration.hs index ff34551843..8c0748f4e9 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Registration.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Registration.hs @@ -4,6 +4,8 @@ module StoreService.Names.Registration ( Registration (..), RegisterIntent (..), + Node, + namehash, sponsorDomain, resolverDomain, makeCommitment, @@ -44,6 +46,9 @@ data Registration = Registration rChannelLink :: Maybe Text } +-- | Only built by 'namehash', so it always holds 32 bytes. +newtype Node = Node ByteString + data RegisterIntent = RegisterIntent { riRegistration :: Registration, riNonce :: Word64, @@ -94,22 +99,22 @@ verifyRegisterIntent domain now RegisterIntent {riRegistration = r@Registration signer <- ExceptT $ recoverAddress riSignature $ registerIntentDigest domain r riNonce riDeadline unless (signer == rOwner) $ throwError "intent is not signed by the owner" -setTextDigest :: Eip712Domain -> ByteString -> Text -> Text -> Word64 -> Word64 -> ByteString +setTextDigest :: Eip712Domain -> Node -> Text -> Text -> Word64 -> Word64 -> ByteString setTextDigest domain node key value nonce deadline = hashTypedData domain "SetText(bytes32 node,string key,string value,uint256 nonce,uint256 deadline)" [bytes32 node, VBytes $ encodeUtf8 key, VBytes $ encodeUtf8 value, VUint $ fromIntegral nonce, VUint $ fromIntegral deadline] -clearRecordsDigest :: Eip712Domain -> ByteString -> Word64 -> Word64 -> ByteString +clearRecordsDigest :: Eip712Domain -> Node -> Word64 -> Word64 -> ByteString clearRecordsDigest domain node nonce deadline = hashTypedData domain "ClearRecords(bytes32 node,uint256 nonce,uint256 deadline)" [bytes32 node, VUint $ fromIntegral nonce, VUint $ fromIntegral deadline] -bytes32 :: ByteString -> Eip712Value -bytes32 = VUint . fromInteger . os2ip +bytes32 :: Node -> Eip712Value +bytes32 (Node node) = VUint $ fromInteger $ os2ip node registrationTuple :: Registration -> ByteString registrationTuple r@Registration {rLabel, rOwner, rDuration, rSecret, rResolver} = @@ -134,9 +139,9 @@ recordCalls Registration {rLabel, rTLD, rContactLink, rChannelLink} = setText key link = selector "setText(bytes32,string,string)" <> abiTuple [Static node, Dynamic $ abiBytes key, Dynamic $ abiBytes $ encodeUtf8 link] - node = namehash rTLD rLabel + Node node = namehash rTLD rLabel -namehash :: SimplexTLD -> Text -> ByteString -namehash tld label = keccak256 $ tldNode <> keccak256 (encodeUtf8 label) +namehash :: SimplexTLD -> Text -> Node +namehash tld label = Node $ keccak256 $ tldNode <> keccak256 (encodeUtf8 label) where tldNode = keccak256 $ B.replicate 32 0 <> keccak256 (B.drop 1 $ strEncode tld) diff --git a/tests/Bots/StoreService/NamesSignatureTests.hs b/tests/Bots/StoreService/NamesSignatureTests.hs index 3ad3a220a0..104a36344d 100644 --- a/tests/Bots/StoreService/NamesSignatureTests.hs +++ b/tests/Bots/StoreService/NamesSignatureTests.hs @@ -30,9 +30,9 @@ namesSignatureTests = describe "StoreService names: signatures" $ do it "hashes RegisterName" $ registerIntentDigest (sponsorDomain chainId sponsor) dynamis 0 1800000000 `shouldBe` hx "0x4b0bcc282db581446f6c25e0e3fbe2839409acbc011bb3e082523dd9eab85a6a" it "hashes SetText" $ - setTextDigest (resolverDomain chainId resolver) node "simplex.contact" "https://x" 3 99 `shouldBe` hx "0xa62d4320889eed3713462d725dc298cd96885572b8e6b424e93a205a96b96fb5" + setTextDigest (resolverDomain chainId resolver) (namehash TLDSimplex "dynamis") "simplex.contact" "https://x" 3 99 `shouldBe` hx "0xa62d4320889eed3713462d725dc298cd96885572b8e6b424e93a205a96b96fb5" it "hashes ClearRecords" $ - clearRecordsDigest (resolverDomain chainId resolver) node 4 99 `shouldBe` hx "0xca4bd243288f27f38e3b45384ba36ce685ff155352cf8125f031e6fb189a46dd" + clearRecordsDigest (resolverDomain chainId resolver) (namehash TLDSimplex "dynamis") 4 99 `shouldBe` hx "0xca4bd243288f27f38e3b45384ba36ce685ff155352cf8125f031e6fb189a46dd" describe "EIP-191 messages" $ do it "signs an order" $ testMessage