Skip to content
This repository was archived by the owner on Mar 1, 2019. It is now read-only.

Commit 0e94b9a

Browse files
authored
Merge pull request #278 from input-output-hk/denisshevchenko/36/eos-wallets-RDL
RDL-operations for EOS-wallets.
2 parents 7a3fa48 + 4f41154 commit 0e94b9a

File tree

7 files changed

+192
-30
lines changed

7 files changed

+192
-30
lines changed

src/Cardano/Wallet/API/V1/Handlers/Wallets.hs

+19-6
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ readEosWallets
136136
:: PassiveWalletLayer IO
137137
-> WalletId
138138
-> Handler (APIResponse EosWallet)
139-
readEosWallets _ _ = do
140-
throwM $ err501 { errBody = "Not Implemented" }
139+
readEosWallets pwl wid = do
140+
res <- liftIO $ WalletLayer.getEosWallet pwl wid
141+
case res of
142+
Left e -> throwM e
143+
Right w -> return $ single w
141144

142145
updateEosWallets
143146
:: PassiveWalletLayer IO
@@ -151,14 +154,24 @@ deleteEosWallets
151154
:: PassiveWalletLayer IO
152155
-> WalletId
153156
-> Handler NoContent
154-
deleteEosWallets _ _ = do
155-
throwM $ err501 { errBody = "Not Implemented" }
157+
deleteEosWallets pwl wid = do
158+
res <- liftIO $ WalletLayer.deleteEosWallet pwl wid
159+
case res of
160+
Left e -> throwM e
161+
Right () -> return NoContent
156162

157163
listEosWallets
158164
:: PassiveWalletLayer IO
159165
-> RequestParams
160166
-> FilterOperations '[WalletId, Coin] EosWallet
161167
-> SortOperations EosWallet
162168
-> Handler (APIResponse [EosWallet])
163-
listEosWallets _ _ _ _ = do
164-
throwM $ err501 { errBody = "Not Implemented" }
169+
listEosWallets pwl params fops sops = do
170+
res <- liftIO $ WalletLayer.getEosWallets pwl
171+
case res of
172+
Left e -> throwM e
173+
Right wallets ->
174+
respondWith params
175+
fops
176+
sops
177+
(pure wallets)

src/Cardano/Wallet/API/V1/ReifyWalletError.hs

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ translateWalletLayerErrors ex = do
4343

4444
<|> try' @CreateWalletError createWalletError
4545
<|> try' @GetWalletError getWalletError
46+
<|> try' @GetEosWalletError getEosWalletError
4647
<|> try' @UpdateWalletError updateWalletError
4748
<|> try' @UpdateWalletPasswordError updateWalletPasswordError
4849
<|> try' @DeleteWalletError deleteWalletError
@@ -156,6 +157,24 @@ getWalletError e = case e of
156157
(GetWalletWalletIdDecodingFailed _txt) ->
157158
V1.WalletNotFound
158159

160+
getAddressPoolGapError :: GetAddressPoolGapError -> V1.WalletError
161+
getAddressPoolGapError e = case e of
162+
ex@(GetEosWalletErrorNoAccounts _txt) ->
163+
V1.EosWalletDoesNotHaveAccounts (sformat build ex)
164+
ex@(GetEosWalletErrorWrongAccounts _txt) ->
165+
V1.EosWalletHasWrongAccounts (sformat build ex)
166+
ex@(GetEosWalletErrorGapsDiffer _txt) ->
167+
V1.EosWalletGapsDiffer (sformat build ex)
168+
169+
getEosWalletError :: GetEosWalletError -> V1.WalletError
170+
getEosWalletError e = case e of
171+
(GetEosWalletError (HD.UnknownHdRoot _rootId)) ->
172+
V1.WalletNotFound
173+
(GetEosWalletWalletIdDecodingFailed _txt) ->
174+
V1.WalletNotFound
175+
(GetEosWalletErrorAddressPoolGap e') ->
176+
getAddressPoolGapError e'
177+
159178
updateWalletError :: UpdateWalletError -> V1.WalletError
160179
updateWalletError e = case e of
161180
(UpdateWalletError (HD.UnknownHdRoot _rootId)) ->

src/Cardano/Wallet/API/V1/Types.hs

+24-1
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,12 @@ data WalletError =
24962496
| UtxoNotEnoughFragmented !ErrUtxoNotEnoughFragmented
24972497
-- ^ available Utxo is not enough fragmented, ie., there is more outputs of transaction than
24982498
-- utxos
2499+
| EosWalletDoesNotHaveAccounts Text
2500+
-- ^ EOS-wallet doesn't have any accounts.
2501+
| EosWalletHasWrongAccounts Text
2502+
-- ^ Some of accounts associated with EOS-wallets have FO-branch.
2503+
| EosWalletGapsDiffer Text
2504+
-- ^ Accounts associated with EOS-wallet contain different values of address pool gap.
24992505
deriving (Generic, Show, Eq)
25002506

25012507
deriveGeneric ''WalletError
@@ -2599,6 +2605,12 @@ instance Buildable WalletError where
25992605
bprint "You've made too many requests too soon, and this one was throttled."
26002606
UtxoNotEnoughFragmented x ->
26012607
bprint build x
2608+
EosWalletDoesNotHaveAccounts _ ->
2609+
bprint "EOS-wallet doesn't have any accounts."
2610+
EosWalletHasWrongAccounts _ ->
2611+
bprint "Some of accounts associated with EOS-wallets have FO-branch."
2612+
EosWalletGapsDiffer _ ->
2613+
bprint "Accounts associated with EOS-wallet contain different values of address pool gap."
26022614

26032615
-- | Convert wallet errors to Servant errors
26042616
instance ToServantError WalletError where
@@ -2643,7 +2655,12 @@ instance ToServantError WalletError where
26432655
err400 { errHTTPCode = 429 }
26442656
UtxoNotEnoughFragmented{} ->
26452657
err403
2646-
2658+
EosWalletDoesNotHaveAccounts{} ->
2659+
err500
2660+
EosWalletHasWrongAccounts{} ->
2661+
err500
2662+
EosWalletGapsDiffer{} ->
2663+
err500
26472664

26482665
-- | Declare the key used to wrap the diagnostic payload, if any
26492666
instance HasDiagnostic WalletError where
@@ -2688,3 +2705,9 @@ instance HasDiagnostic WalletError where
26882705
"microsecondsUntilRetry"
26892706
UtxoNotEnoughFragmented{} ->
26902707
"details"
2708+
EosWalletDoesNotHaveAccounts{} ->
2709+
noDiagnosticKey
2710+
EosWalletHasWrongAccounts{} ->
2711+
noDiagnosticKey
2712+
EosWalletGapsDiffer{} ->
2713+
noDiagnosticKey

src/Cardano/Wallet/WalletLayer.hs

+39-15
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ module Cardano.Wallet.WalletLayer
66
-- ** Errors
77
, CreateWalletError(..)
88
, GetWalletError(..)
9+
, GetAddressPoolGapError(..)
10+
, GetEosWalletError(..)
911
, UpdateWalletError(..)
1012
, UpdateWalletPasswordError(..)
1113
, DeleteWalletError(..)
12-
, DeleteEosWalletError(..)
1314
, GetUtxosError(..)
1415
, NewPaymentError(..)
1516
, EstimateFeesError(..)
@@ -114,6 +115,40 @@ instance Buildable GetWalletError where
114115
build (GetWalletWalletIdDecodingFailed txt) =
115116
bprint ("GetWalletWalletIdDecodingFailed " % build) txt
116117

118+
data GetAddressPoolGapError =
119+
GetEosWalletErrorNoAccounts Text
120+
| GetEosWalletErrorWrongAccounts Text
121+
| GetEosWalletErrorGapsDiffer Text
122+
deriving Eq
123+
124+
instance Buildable GetAddressPoolGapError where
125+
build (GetEosWalletErrorNoAccounts txt) =
126+
bprint ("GetEosWalletErrorNoAccounts " % build) txt
127+
build (GetEosWalletErrorWrongAccounts txt) =
128+
bprint ("FO-accounts found in EOS-wallet " % build) txt
129+
build (GetEosWalletErrorGapsDiffer txt) =
130+
bprint ("Address pool gaps differ, for EOS-wallet " % build) txt
131+
132+
data GetEosWalletError =
133+
GetEosWalletError Kernel.UnknownHdRoot
134+
| GetEosWalletWalletIdDecodingFailed Text
135+
| GetEosWalletErrorAddressPoolGap GetAddressPoolGapError
136+
deriving Eq
137+
138+
-- | Unsound show instance needed for the 'Exception' instance.
139+
instance Show GetEosWalletError where
140+
show = formatToString build
141+
142+
instance Exception GetEosWalletError
143+
144+
instance Buildable GetEosWalletError where
145+
build (GetEosWalletError kernelError) =
146+
bprint ("GetEosWalletError " % build) kernelError
147+
build (GetEosWalletWalletIdDecodingFailed txt) =
148+
bprint ("GetEosWalletWalletIdDecodingFailed " % build) txt
149+
build (GetEosWalletErrorAddressPoolGap gapError) =
150+
bprint ("GetEosWalletErrorAddressPoolGap " % build) gapError
151+
117152
data UpdateWalletError =
118153
UpdateWalletError Kernel.UnknownHdRoot
119154
| UpdateWalletErrorNotFound WalletId
@@ -168,19 +203,6 @@ instance Buildable DeleteWalletError where
168203
build (DeleteWalletError kernelError) =
169204
bprint ("DeleteWalletError " % build) kernelError
170205

171-
data DeleteEosWalletError =
172-
DeleteEosWalletError Kernel.UnknownHdRoot
173-
174-
-- | Unsound show instance needed for the 'Exception' instance.
175-
instance Show DeleteEosWalletError where
176-
show = formatToString build
177-
178-
instance Exception DeleteEosWalletError
179-
180-
instance Buildable DeleteEosWalletError where
181-
build (DeleteEosWalletError kernelError) =
182-
bprint ("DeleteEosWalletError " % build) kernelError
183-
184206
data GetUtxosError =
185207
GetUtxosWalletIdDecodingFailed Text
186208
| GetUtxosGetAccountsError Kernel.UnknownHdRoot
@@ -429,7 +451,9 @@ data PassiveWalletLayer m = PassiveWalletLayer
429451
-- fully-owned wallets
430452
createWallet :: CreateWallet -> m (Either CreateWalletError Wallet)
431453
, getWallets :: m (IxSet Wallet)
454+
, getEosWallets :: m (Either GetEosWalletError (IxSet EosWallet))
432455
, getWallet :: WalletId -> m (Either GetWalletError Wallet)
456+
, getEosWallet :: WalletId -> m (Either GetEosWalletError EosWallet)
433457
, updateWallet :: WalletId
434458
-> WalletUpdate
435459
-> m (Either UpdateWalletError Wallet)
@@ -439,7 +463,7 @@ data PassiveWalletLayer m = PassiveWalletLayer
439463
, deleteWallet :: WalletId -> m (Either DeleteWalletError ())
440464
-- externally-owned wallets
441465
, createEosWallet :: NewEosWallet -> m (Either CreateWalletError EosWallet)
442-
, deleteEosWallet :: WalletId -> m (Either DeleteEosWalletError ())
466+
, deleteEosWallet :: WalletId -> m (Either DeleteWalletError ())
443467
, getUtxos :: WalletId
444468
-> m (Either GetUtxosError [(Account, Utxo)])
445469
-- accounts

src/Cardano/Wallet/WalletLayer/Kernel.hs

+2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ bracketPassiveWallet pm mode logFunction keystore node fInjects f = do
128128

129129
-- Read-only operations
130130
, getWallets = join (ro $ Wallets.getWallets w)
131+
, getEosWallets = join (ro $ Wallets.getEosWallets w)
131132
, getWallet = \wId -> join (ro $ Wallets.getWallet w wId)
133+
, getEosWallet = \wId -> join (ro $ Wallets.getEosWallet w wId)
132134
, getUtxos = \wId -> ro $ Wallets.getWalletUtxos wId
133135
, getAccounts = \wId -> ro $ Accounts.getAccounts wId
134136
, getAccount = \wId acc -> ro $ Accounts.getAccount wId acc

src/Cardano/Wallet/WalletLayer/Kernel/Conv.hs

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Cardano.Wallet.WalletLayer.Kernel.Conv (
1212
, toRootId
1313
, toAccount
1414
, toWallet
15+
, toEosWallet
1516
, toAddress
1617
, toCardanoAddress
1718
, toAssuranceLevel
@@ -46,6 +47,7 @@ import Pos.Crypto (AesKey, RedeemSecretKey, aesDecrypt,
4647
import Cardano.Mnemonic (mnemonicToAesKey)
4748
import Cardano.Wallet.API.Types.UnitOfMeasure
4849
import qualified Cardano.Wallet.API.V1.Types as V1
50+
import Cardano.Wallet.Kernel.AddressPoolGap (AddressPoolGap)
4951
import Cardano.Wallet.Kernel.CoinSelection.FromGeneric
5052
(InputGrouping (..))
5153
import Cardano.Wallet.Kernel.DB.BlockMeta (addressMetaIsUsed)
@@ -62,6 +64,11 @@ import qualified Cardano.Wallet.Kernel.Read as Kernel
6264
import UTxO.Util (exceptT)
6365
-- import Cardano.Wallet.WalletLayer (InvalidRedemptionCode (..))
6466

67+
-- Functions 'toWallet' and 'toEosWallet' contain duplications in 'where'
68+
-- sections, but in order to fix it we should change two functions and
69+
-- introduce the new one.
70+
{-# ANN module ("HLint: ignore Reduce duplication" :: Text) #-}
71+
6572
{-------------------------------------------------------------------------------
6673
From V1 to kernel types
6774
@@ -180,6 +187,22 @@ toWallet db hdRoot = V1.Wallet {
180187
walletId = sformat build rootId
181188
v1AssuranceLevel = toAssuranceLevel $ hdRoot ^. HD.hdRootAssurance
182189

190+
-- | Converts an 'HdRoot' into a V1 'EosWallet'.
191+
toEosWallet :: Kernel.DB -> HD.HdRoot -> AddressPoolGap -> V1.EosWallet
192+
toEosWallet db hdRoot gap = V1.EosWallet {
193+
eoswalId = V1.WalletId walletId
194+
, eoswalName = hdRoot ^. HD.hdRootName . to HD.getWalletName
195+
, eoswalAddressPoolGap = gap
196+
, eoswalBalance = V1.WalletCoin (Kernel.rootTotalBalance db rootId)
197+
, eoswalCreatedAt = V1.WalletTimestamp createdAt
198+
, eoswalAssuranceLevel = v1AssuranceLevel
199+
}
200+
where
201+
rootId = hdRoot ^. HD.hdRootId
202+
createdAt = hdRoot ^. HD.hdRootCreatedAt . fromDb
203+
walletId = sformat build rootId
204+
v1AssuranceLevel = toAssuranceLevel $ hdRoot ^. HD.hdRootAssurance
205+
183206
toAssuranceLevel :: HD.AssuranceLevel -> V1.AssuranceLevel
184207
toAssuranceLevel HD.AssuranceLevelNormal = V1.NormalAssurance
185208
toAssuranceLevel HD.AssuranceLevelStrict = V1.StrictAssurance

0 commit comments

Comments
 (0)