diff --git a/src/Cardano/Wallet/Api.hs b/src/Cardano/Wallet/Api.hs index f5f9e628a1a..bdf410b1201 100644 --- a/src/Cardano/Wallet/Api.hs +++ b/src/Cardano/Wallet/Api.hs @@ -25,3 +25,9 @@ type GetWallet = "wallets" type ListWallets = "wallets" :> Get '[JSON] [Wallet] + +-- type CreateTransaction = "wallets" +-- :> Capture "walletId" WalletId +-- :> "transactions" +-- :> ReqBody '[JSON] Payment +-- :> Post '[JSON] Transaction diff --git a/src/Cardano/Wallet/Api/Types.hs b/src/Cardano/Wallet/Api/Types.hs index 5defa099d35..1d33fade66c 100644 --- a/src/Cardano/Wallet/Api/Types.hs +++ b/src/Cardano/Wallet/Api/Types.hs @@ -22,10 +22,12 @@ module Cardano.Wallet.Api.Types , WalletId (..) , WalletPassphraseInfo (..) , WalletState (..) + , Payment (..) -- * Re-Exports From Primitive Types , AddressPoolGap , WalletName (..) + , Passphrase -- * Polymorphic Types , ApiT (..) @@ -36,8 +38,12 @@ import Prelude import Cardano.Wallet ( WalletName (..), mkWalletName ) +import Cardano.Wallet.AddressDerivation + ( Passphrase ) import Cardano.Wallet.AddressDiscovery ( AddressPoolGap, getAddressPoolGap, mkAddressPoolGap ) +import Cardano.Wallet.Primitive + ( Address, mkAddress ) import Data.Aeson ( FromJSON (..) , SumEncoding (..) @@ -62,6 +68,8 @@ import Data.Time.Clock ( UTCTime ) import Data.UUID.Types ( UUID ) +import Fmt + ( build, fmt ) import GHC.Generics ( Generic ) import GHC.TypeLits @@ -222,6 +230,26 @@ walletStateOptions = taggedSumTypeOptions $ TaggedObjectOptions , _contentsFieldName = "progress" } +data Payment = Payment + { _targets :: ![TargetOutput] + , _passphrase :: !(ApiT (Passphrase "encryption")) + } deriving (Generic, Show) + +data TargetOutput = TargetOutput + { _address :: !(ApiT Address) + , _amount :: !Amount + } deriving (Generic, Show) + +instance ToJSON (ApiT Address) where + toJSON = toJSON . fmt @T.Text . build . getApiT +instance FromJSON (ApiT Address) where + parseJSON x = fmap ApiT . eitherToParser . mkAddress =<< parseJSON x + +instance FromJSON TargetOutput where + parseJSON = genericParseJSON defaultRecordTypeOptions +instance ToJSON TargetOutput where + toJSON = genericToJSON defaultRecordTypeOptions + {------------------------------------------------------------------------------- Polymorphic Types -------------------------------------------------------------------------------} diff --git a/src/Cardano/Wallet/Primitive.hs b/src/Cardano/Wallet/Primitive.hs index c0b468e8fb9..f78123143df 100644 --- a/src/Cardano/Wallet/Primitive.hs +++ b/src/Cardano/Wallet/Primitive.hs @@ -34,6 +34,7 @@ module Cardano.Wallet.Primitive -- * Address , Address (..) + , mkAddress , IsOurs(..) -- * Coin @@ -71,7 +72,7 @@ import Data.ByteArray.Encoding import Data.ByteString ( ByteString ) import Data.ByteString.Base58 - ( bitcoinAlphabet, encodeBase58 ) + ( bitcoinAlphabet, decodeBase58, encodeBase58 ) import Data.Map.Strict ( Map ) import Data.Set @@ -96,6 +97,7 @@ import GHC.TypeLits import qualified Data.Map.Strict as Map import qualified Data.Set as Set +import qualified Data.Text as T import qualified Data.Text.Encoding as T -- * Block @@ -221,6 +223,15 @@ newtype Address = Address instance NFData Address +data AddressError + = AddressDecodeError T.Text + deriving (Show) + +mkAddress :: T.Text -> Either AddressError Address +mkAddress addr = case decodeBase58 bitcoinAlphabet $ T.encodeUtf8 addr of + Nothing -> Left $ AddressDecodeError addr + Just a -> Right $ Address a + instance Buildable Address where build = build . T.decodeUtf8 . encodeBase58 bitcoinAlphabet . getAddress