From 52b9fe2426ca3bf7bedda882465d3f27341d4b44 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Wed, 27 Mar 2019 04:09:25 +0000 Subject: [PATCH] Add `PutWalletPassphrase` operation to the Servant API definition. --- specifications/api/swagger.yaml | 28 +++++++++---------- src/Cardano/Wallet/Api.hs | 13 ++++++++- src/Cardano/Wallet/Api/Types.hs | 11 ++++++++ .../Wallet/Api/WalletPutPassphraseData.json | 21 ++++++++++++++ test/unit/Cardano/Wallet/Api/TypesSpec.hs | 17 +++++++++++ 5 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 test/data/Cardano/Wallet/Api/WalletPutPassphraseData.json diff --git a/specifications/api/swagger.yaml b/specifications/api/swagger.yaml index 74abc10056e..2d7603affc1 100644 --- a/specifications/api/swagger.yaml +++ b/specifications/api/swagger.yaml @@ -511,6 +511,19 @@ definitions: 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 # @@ -536,19 +549,6 @@ parametersStakePoolId: ¶metersStakePoolId type: string format: base58 -parametersPutWalletPassphrase: ¶metersPutWalletPassphrase - 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: ¶metersPostTransaction type: object required: @@ -846,7 +846,7 @@ paths: parameters: - *parametersWalletId - <<: *parametersBody - schema: *parametersPutWalletPassphrase + schema: *WalletPutPassphraseData responses: *responsesPutWalletPassphrase /wallets/{walletId}/transactions: diff --git a/src/Cardano/Wallet/Api.hs b/src/Cardano/Wallet/Api.hs index 0db744d876a..b1fa6b84b74 100644 --- a/src/Cardano/Wallet/Api.hs +++ b/src/Cardano/Wallet/Api.hs @@ -4,7 +4,12 @@ module Cardano.Wallet.Api where import Cardano.Wallet.Api.Types - ( Wallet, WalletId, WalletPostData, WalletPutData ) + ( Wallet + , WalletId + , WalletPostData + , WalletPutData + , WalletPutPassphraseData + ) import Data.Proxy ( Proxy (..) ) import Servant.API @@ -52,3 +57,9 @@ 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 diff --git a/src/Cardano/Wallet/Api/Types.hs b/src/Cardano/Wallet/Api/Types.hs index 83277a47ce0..d3ccd8d742b 100644 --- a/src/Cardano/Wallet/Api/Types.hs +++ b/src/Cardano/Wallet/Api/Types.hs @@ -32,6 +32,7 @@ module Cardano.Wallet.Api.Types , WalletBalance (..) , WalletPostData (..) , WalletPutData (..) + , WalletPutPassphraseData (..) -- * Re-Export From Primitives , PoolId (..) @@ -124,6 +125,11 @@ 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) @@ -184,6 +190,11 @@ instance FromJSON WalletPutData where 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 < 10 -> diff --git a/test/data/Cardano/Wallet/Api/WalletPutPassphraseData.json b/test/data/Cardano/Wallet/Api/WalletPutPassphraseData.json new file mode 100644 index 00000000000..52297a0f84f --- /dev/null +++ b/test/data/Cardano/Wallet/Api/WalletPutPassphraseData.json @@ -0,0 +1,21 @@ +{ + "seed": -4033088551338152747, + "samples": [ + { + "old_passphrase": "@^8Q}ja[LAN'),i8$'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-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ẳ6o@^N8RMbxBxc" + }, + { + "old_passphrase": "4j_𢱠|ZAg;#l:+V;-u0]'ro@\\1`?Y>Gq1e?/;N:SZC>si*7Y.,R!S2+RPDFUqtsFJB𧊴&PZlJ8Jw;6K'Q8WLrzR%rvpjxMA;/vk𦢟 ElZ%)xqA+x7:𩻜D9a/[+e퀧]~>aq}#{𣟣=[b0#RvhzPnn]Y^+i/gN;v(`1yKo#&T]?QuZu0}Y<.iZvzF:𡿔G`PVkY#RI𤏟b,#Sc3qf<;;kGe1B (qb o]O-fg=핂:H3aCJ06pKA𐤀[鑖*]ha^𐐁J" + } + ] +} \ No newline at end of file diff --git a/test/unit/Cardano/Wallet/Api/TypesSpec.hs b/test/unit/Cardano/Wallet/Api/TypesSpec.hs index 8af970f1c4c..e0952cc6c60 100644 --- a/test/unit/Cardano/Wallet/Api/TypesSpec.hs +++ b/test/unit/Cardano/Wallet/Api/TypesSpec.hs @@ -32,6 +32,7 @@ import Cardano.Wallet.Api.Types , WalletPassphraseInfo (..) , WalletPostData (..) , WalletPutData (..) + , WalletPutPassphraseData (..) , WalletState (..) ) import Cardano.Wallet.Primitive.Mnemonic @@ -130,6 +131,7 @@ spec = do roundtripAndGolden $ Proxy @ Wallet roundtripAndGolden $ Proxy @ WalletPostData roundtripAndGolden $ Proxy @ WalletPutData + roundtripAndGolden $ Proxy @ WalletPutPassphraseData roundtripAndGolden $ Proxy @ (ApiT AddressPoolGap) roundtripAndGolden $ Proxy @ (ApiT (WalletDelegation (ApiT PoolId))) roundtripAndGolden $ Proxy @ (ApiT WalletId) @@ -203,6 +205,10 @@ instance Arbitrary WalletPutData where arbitrary = genericArbitrary shrink = genericShrink +instance Arbitrary WalletPutPassphraseData where + arbitrary = genericArbitrary + shrink = genericShrink + instance Arbitrary WalletBalance where arbitrary = genericArbitrary shrink = genericShrink @@ -385,6 +391,17 @@ instance ToSchema WalletPutData where Just schema -> return $ NamedSchema (Just "WalletPutData") schema +-- | Ad-hoc 'ToSchema' instance for the 'WalletPutPassphraseData' definition: we +-- simply look it up from the specification. +instance ToSchema WalletPutPassphraseData where + declareNamedSchema _ = + case specification ^. definitions . at "WalletPutPassphraseData" of + Nothing -> error + "unable to find the definition for 'WalletPutPassphraseData' \ + \in the spec" + Just schema -> + return $ NamedSchema (Just "WalletPutPassphraseData") schema + -- | Verify that all servant endpoints are present and match the specification class ValidateEveryPath api where validateEveryPath :: Proxy api -> Spec