Skip to content

Commit

Permalink
Merge pull request #125 from input-output-hk/jonathanknowles/api-put-…
Browse files Browse the repository at this point in the history
…wallet

Add `ListAddresses`, `PutWallet`, `PutWalletPassphrase` to the Servant API definition.
  • Loading branch information
jonathanknowles committed Mar 27, 2019
2 parents f771022 + b3baaae commit baadd43
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 45 deletions.
40 changes: 20 additions & 20 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,24 @@ definitions:
passphrase: *walletPassphrase
address_pool_gap: *walletAddressPoolGap

WalletPutData: &WalletPutData
type: object
properties:
name: *walletName

WalletPutPassphraseData: &WalletPutPassphraseData
type: object
required:
- old_passphrase
- new_passphrase
properties:
old_passphrase:
<<: *walletPassphrase
description: The current passphrase.
new_passphrase:
<<: *walletPassphrase
description: A master passphrase to lock and protect the wallet for sensitive operation (e.g. sending funds).

#############################################################################
# #
# PARAMETERS #
Expand All @@ -531,24 +549,6 @@ parametersStakePoolId: &parametersStakePoolId
type: string
format: base58

parametersPutWallet: &parametersPutWallet
type: object
properties:
name: *walletName

parametersPutWalletPassphrase: &parametersPutWalletPassphrase
type: object
required:
- old_passphrase
- new_passphrase
properties:
old_passphrase:
<<: *walletPassphrase
description: The current passphrase.
new_passphrase:
<<: *walletPassphrase
description: A master passphrase to lock and protect the wallet for sensitive operation (e.g. sending funds).

parametersPostTransaction: &parametersPostTransaction
type: object
required:
Expand Down Expand Up @@ -807,7 +807,7 @@ paths:
parameters:
- *parametersWalletId
- <<: *parametersBody
schema: *parametersPutWallet
schema: *WalletPutData
responses: *responsesPutWallet

/wallets/{walletId}/statistics/utxos:
Expand Down Expand Up @@ -846,7 +846,7 @@ paths:
parameters:
- *parametersWalletId
- <<: *parametersBody
schema: *parametersPutWalletPassphrase
schema: *WalletPutPassphraseData
responses: *responsesPutWalletPassphrase

/wallets/{walletId}/transactions:
Expand Down
54 changes: 51 additions & 3 deletions src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,50 @@
module Cardano.Wallet.Api where

import Cardano.Wallet.Api.Types
( Wallet, WalletId, WalletPostData )
( Address
, AddressState
, ApiT
, Wallet
, WalletId
, WalletPostData
, WalletPutData
, WalletPutPassphraseData
)
import Data.Proxy
( Proxy (..) )
import Servant.API
( (:<|>), (:>), Capture, Delete, Get, JSON, NoContent, Post, ReqBody )
( (:<|>)
, (:>)
, Capture
, Delete
, Get
, JSON
, NoContent
, Post
, Put
, QueryParam
, ReqBody
)

api :: Proxy Api
api = Proxy

type Api = Wallets
type Api = Addresses :<|> Wallets

{-------------------------------------------------------------------------------
Addresses
See also: https://input-output-hk.github.io/cardano-wallet/api/#tag/Addresses
-------------------------------------------------------------------------------}

type Addresses =
ListAddresses

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/listAddresses
type ListAddresses = "wallets"
:> Capture "walletId" WalletId
:> QueryParam "state" (ApiT AddressState)
:> Get '[JSON] [Address]

{-------------------------------------------------------------------------------
Wallets
Expand All @@ -26,6 +60,8 @@ type Wallets =
:<|> GetWallet
:<|> ListWallets
:<|> PostWallet
:<|> PutWallet
:<|> PutWalletPassphrase

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/deleteWallet
type DeleteWallet = "wallets"
Expand All @@ -45,3 +81,15 @@ type ListWallets = "wallets"
type PostWallet = "wallets"
:> ReqBody '[JSON] WalletPostData
:> Post '[JSON] Wallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/putWallet
type PutWallet = "wallets"
:> Capture "walletId" WalletId
:> ReqBody '[JSON] WalletPutData
:> Put '[JSON] Wallet

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/putWalletPassphrase
type PutWalletPassphrase = "wallets"
:> Capture "walletId" WalletId
:> ReqBody '[JSON] WalletPutPassphraseData
:> Put '[] NoContent
57 changes: 55 additions & 2 deletions src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
module Cardano.Wallet.Api.Types
(
-- * API Types
Wallet (..)
Address (..)
, Wallet (..)
, WalletBalance (..)
, WalletPostData (..)
, WalletPutData (..)
, WalletPutPassphraseData (..)

-- * Re-Export From Primitives
, AddressState (..)
, PoolId (..)
, WalletDelegation (..)
, WalletId (..)
Expand All @@ -59,7 +63,8 @@ import Cardano.Wallet.Primitive.AddressDiscovery
( AddressPoolGap, getAddressPoolGap, mkAddressPoolGap )
import Cardano.Wallet.Primitive.Mnemonic
import Cardano.Wallet.Primitive.Model
( PoolId (..)
( AddressState (..)
, PoolId (..)
, WalletDelegation (..)
, WalletId (..)
, WalletName (..)
Expand All @@ -84,6 +89,8 @@ import Data.Aeson
, sumEncoding
, tagSingleConstructors
)
import Data.ByteString.Base58
( bitcoinAlphabet, decodeBase58, encodeBase58 )
import Data.Quantity
( Quantity (..) )
import Data.Text
Expand All @@ -95,6 +102,7 @@ import GHC.TypeLits
import Numeric.Natural
( Natural )

import qualified Cardano.Wallet.Primitive.Types as P
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteArray as BA
Expand All @@ -105,6 +113,11 @@ import qualified Data.Text.Encoding as T
API Types
-------------------------------------------------------------------------------}

data Address = Address
{ _id :: !(ApiT P.Address)
, _state :: !(ApiT AddressState)
} deriving (Eq, Generic, Show)

data Wallet = Wallet
{ _id :: !(ApiT WalletId)
, _addressPoolGap :: !(ApiT AddressPoolGap)
Expand All @@ -123,6 +136,15 @@ data WalletPostData = WalletPostData
, _passphrase :: !(ApiT (Passphrase "encryption"))
} deriving (Eq, Generic, Show)

newtype WalletPutData = WalletPutData
{ _name :: (Maybe (ApiT WalletName))
} deriving (Eq, Generic, Show)

data WalletPutPassphraseData = WalletPutPassphraseData
{ _oldPassphrase :: !(ApiT (Passphrase "encryption"))
, _newPassphrase :: !(ApiT (Passphrase "encryption"))
} deriving (Eq, Generic, Show)

data WalletBalance = WalletBalance
{ _available :: !(Quantity "lovelace" Natural)
, _total :: !(Quantity "lovelace" Natural)
Expand Down Expand Up @@ -168,6 +190,27 @@ newtype ApiMnemonicT (sizes :: [Nat]) (purpose :: Symbol) =
JSON Instances
-------------------------------------------------------------------------------}

instance FromJSON Address where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON Address where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON (ApiT AddressState) where
parseJSON = fmap ApiT . genericParseJSON defaultSumTypeOptions
instance ToJSON (ApiT AddressState) where
toJSON = genericToJSON defaultSumTypeOptions . getApiT

instance FromJSON (ApiT P.Address) where
parseJSON bytes = do
x <- parseJSON bytes
maybe
(fail "Unable to decode Address: expected Base58 encoding")
(pure . ApiT . P.Address)
(decodeBase58 bitcoinAlphabet $ T.encodeUtf8 x)
instance ToJSON (ApiT P.Address )where
toJSON = toJSON
. T.decodeUtf8 . encodeBase58 bitcoinAlphabet . P.getAddress . getApiT

instance FromJSON Wallet where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON Wallet where
Expand All @@ -178,6 +221,16 @@ instance FromJSON WalletPostData where
instance ToJSON WalletPostData where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON WalletPutData where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON WalletPutData where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON WalletPutPassphraseData where
parseJSON = genericParseJSON defaultRecordTypeOptions
instance ToJSON WalletPutPassphraseData where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON (ApiT (Passphrase "encryption")) where
parseJSON = parseJSON >=> \case
t | T.length t < passphraseMinLength ->
Expand Down
11 changes: 10 additions & 1 deletion src/Cardano/Wallet/Primitive/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

module Cardano.Wallet.Primitive.Model
(
-- * Address
AddressState (..)

-- * Wallet
Wallet
, Wallet
, initWallet
, currentTip
, applyBlock
Expand Down Expand Up @@ -102,6 +105,12 @@ import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.Text as T

{-------------------------------------------------------------------------------
address
-------------------------------------------------------------------------------}

data AddressState = Used | Unused
deriving (Eq, Generic, Show)

{-------------------------------------------------------------------------------
wallet
Expand Down
21 changes: 21 additions & 0 deletions test/data/Cardano/Wallet/Api/Address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"seed": -3550386592215194791,
"samples": [
{
"state": "unused",
"id": "2GUdqXSp8WJS7cT68vqQeib5PX9765NVy1r2zqMKuJRkymTABmkBhLmRiLxFMD9pyTDRH"
},
{
"state": "used",
"id": "23rV7eTuAYyfPvgSFCvXmu4rjK2cCw7Hiep4kBP5q8BjouhjfQaiT4DSNgpAqyEXvr1zd"
},
{
"state": "used",
"id": "UT4QpKUSxauQo1XQ6VoT8MhVPYPn5m8tPMgE5WHnLy8E3X1ete7NAiXqenUtoGLKGo9P"
},
{
"state": "unused",
"id": "Kgihnukc1UStfMcKxBjRew4jnt7Z53JAUiKZkw3XErXy9NTxZNfmVarXhP9rs99PDFFA"
}
]
}
9 changes: 9 additions & 0 deletions test/data/Cardano/Wallet/Api/ApiT Address.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"seed": -6381782919107840063,
"samples": [
"2X1ZtZvZLPKXq6bahHasA6U7z29fF7kfqpU4h3sVGqZxsSY9Ne7a5R3UyqmvCZf5MfiQx",
"2XmEbaQMaybnK5xeeC4eKKL2Cjn7t3U3ZrUFrsSb9V2A7DGNxoJ3Xrugb4YdQLjwn7gzd",
"wcfN2qnFcB6iXkgSqQXtLuAueo8Th8KGE9aTx4Zt4LfBmfUbGxqKqfcyLEqnEeWuvNEp",
"H2XZfDFZHdxcatfd4jwigK6TBoLQicqbMiZu1FAVMUzunAz47xeyru32H38cHjqTugac"
]
}
11 changes: 11 additions & 0 deletions test/data/Cardano/Wallet/Api/WalletPutData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"seed": -3282480895854715413,
"samples": [
{},
{},
{},
{
"name": "ro8n&(V$]%dJ吚 (v]QSu1𩈾DfZ|*w?\\Iy,X%d9k}[𦴽YFJtW?7ZJv_-jYz_KpB`iE@$*4aV)`𠪚V`Q&0O7=ZmB<KG3Y5$\\>WO$1(/4子tmkNd;Zcte𥗼fay>nqh𨃮wQwW~u:`,pzhSB6|&~p}\"dS6🢓_3#*+f#5𠷓>:-Yq58Q_:𠷒as0/^j7;^BafT3I3y䖜jY0<CiwxqJ.Ofx<\" 1FDU9HrlD$7𨸴cEIq+*>~"
}
]
}
21 changes: 21 additions & 0 deletions test/data/Cardano/Wallet/Api/WalletPutPassphraseData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"seed": -4033088551338152747,
"samples": [
{
"old_passphrase": "@^8Q}ja[LAN'),i8<xQ3𨒺b<]}R[r^.m+i@s[Yn~t7谊>$'Nu`a%2Z-kI5ꭘy1`0Qu=FO^c}btzzj}JB1⎹&)𫓘f|𐦵> &7z%wb.^ZS\"睥+3}v'yzZW%\\射2%7|sf4.u?-/pxy?M %&I@Q+SU(c5!/\"Hk`i8{e+6 0e8Oz)F\\@jt_HDj!v5Mg)MBoiyy\\3e25l;=j&Z${N$M4E~>JFeQTM~!N; + 1$d?Ln_HafWnKZ2'9F6HNNP_-{6rwPWfE*\\WJ",
"new_passphrase": "よ뽴bckG&v𠺼boJ+bRS-AnuEJi^Qbj+z=a=x1xDy{83Q=4dkLHwNN82e@A1(o{GZ@P5S(~tcW#\"%[thGf]IXCOMmUg+@i&*so+𣏉$b-uX4+Ll(u-s}fI}>K6d1.=%1A{q{xKW0BN Ⰽ@(_bo@FdE<jA>-U:bCj<"
},
{
"old_passphrase": ":g𢯦C=[X𫆔U{X_#Q\\r{y6X\"陜i}#\"ꙺJ/$Ge踙dF('w@^I|AFA_jBm246z[ᄶzG9}[?e/PzFJT\\z7Wd$#/+2q)M>?0[7)|M𖬙~*,q:y3:!D\"D,Ts[uKẳ<f%%yQ𨢆?3⋣",
"new_passphrase": "qC#/S$EaaO6JT%\\i)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