{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Network.Ethereum.Ens where
import Crypto.Hash (Digest, Keccak_256, hash)
import Data.ByteArray (convert, zero)
import Data.ByteArray.Sized (unsafeFromByteArrayAccess)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (split)
import Data.Monoid ((<>))
import Lens.Micro ((.~))
import Data.Solidity.Prim (Address, BytesN)
import Network.Ethereum.Account.Class (Account)
import Network.Ethereum.Account.Internal (AccountT, to, withParam)
import qualified Network.Ethereum.Ens.PublicResolver as Resolver
import qualified Network.Ethereum.Ens.Registry as Reg
import Network.JsonRpc.TinyClient (JsonRpc)
namehash :: ByteString
-> BytesN 32
namehash =
unsafeFromByteArrayAccess . foldr algo (zero 32) . split '.'
where
algo a b = sha3 (b <> sha3 a)
sha3 :: ByteString -> ByteString
sha3 bs = convert (hash bs :: Digest Keccak_256)
resolve :: (JsonRpc m, Account p (AccountT p))
=> ByteString
-> AccountT p m Address
resolve name = do
r <- ensRegistry $ Reg.resolver node
withParam (to .~ r) $ Resolver.addr node
where
node = namehash name
ensRegistry = withParam $ to .~ "0x314159265dD8dbb310642f98f50C066173C1259b"