Skip to content

WIP Add types for transaction creation on servant api level (v0) #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 28 additions & 0 deletions src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ module Cardano.Wallet.Api.Types
, WalletId (..)
, WalletPassphraseInfo (..)
, WalletState (..)
, Payment (..)

-- * Re-Exports From Primitive Types
, AddressPoolGap
, WalletName (..)
, Passphrase

-- * Polymorphic Types
, ApiT (..)
Expand All @@ -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 (..)
Expand All @@ -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
Expand Down Expand Up @@ -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
-------------------------------------------------------------------------------}
Expand Down
13 changes: 12 additions & 1 deletion src/Cardano/Wallet/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Cardano.Wallet.Primitive

-- * Address
, Address (..)
, mkAddress
, IsOurs(..)

-- * Coin
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down