@@ -6,6 +6,7 @@ module Cardano.Wallet.API.V1.LegacyHandlers.Wallets (
6
6
, newWallet
7
7
) where
8
8
9
+ import Formatting (build , sformat )
9
10
import Universum
10
11
import UnliftIO (MonadUnliftIO )
11
12
@@ -21,9 +22,11 @@ import Cardano.Wallet.API.V1.Types as V1
21
22
import qualified Cardano.Wallet.API.V1.Wallets as Wallets
22
23
import qualified Cardano.Wallet.Kernel.DB.Util.IxSet as IxSet
23
24
import Pos.Chain.Update ()
25
+ import Pos.Client.KeyStorage (addPublicKey )
24
26
import qualified Pos.Core as Core
25
27
26
- import Pos.Util (HasLens (.. ))
28
+ import Pos.Util (HasLens (.. ), maybeThrow )
29
+ import Pos.Util.Servant (encodeCType )
27
30
import qualified Pos.Wallet.WalletMode as V0
28
31
import Pos.Wallet.Web.Methods.Logic (MonadWalletLogic ,
29
32
MonadWalletLogicRead )
@@ -199,25 +202,110 @@ checkExternalWallet _encodedRootPK =
199
202
-- | Creates a new or restores an existing external @wallet@ given a 'NewExternalWallet' payload.
200
203
-- Returns to the client the representation of the created or restored wallet in the 'Wallet' type.
201
204
newExternalWallet
205
+ :: ( MonadThrow m
206
+ , MonadUnliftIO m
207
+ -- , HasLens SyncQueue ctx SyncQueue
208
+ , V0. MonadBlockchainInfo m
209
+ , V0. MonadWalletLogic ctx m
210
+ )
211
+ => NewExternalWallet
212
+ -> m (WalletResponse Wallet )
213
+ newExternalWallet (NewExternalWallet rootPK assuranceLevel name operation) = do
214
+ let newExternalWalletHandler CreateWallet = createNewExternalWallet
215
+ newExternalWalletHandler RestoreWallet = restoreExternalWallet
216
+ walletMeta <- V0. CWalletMeta <$> pure name
217
+ <*> migrate assuranceLevel
218
+ <*> pure 0
219
+ single <$> do
220
+ v0wallet <- newExternalWalletHandler operation walletMeta rootPK
221
+ ws <- V0. askWalletSnapshot
222
+ migrateWallet ws v0wallet True
223
+
224
+ -- | Creates new external wallet.
225
+ createNewExternalWallet
226
+ :: ( MonadThrow m
227
+ , V0. MonadWalletLogic ctx m
228
+ )
229
+ => V0. CWalletMeta
230
+ -> PublicKeyAsBase58
231
+ -> m V0. CWallet
232
+ createNewExternalWallet walletMeta encodedRootPK = do
233
+ rootPK <- case mkPublicKeyFromBase58 encodedRootPK of
234
+ Left problem -> throwM (InvalidPublicKey $ sformat build problem)
235
+ Right rootPK -> return rootPK
236
+
237
+ -- This extended public key will be used during synchronization
238
+ -- with the blockchain.
239
+ addPublicKey rootPK
240
+
241
+ let walletId = encodeCType . Core. makePubKeyAddressBoot $ rootPK
242
+ isReady = True -- We don't need to sync new wallet with the blockchain.
243
+
244
+ -- Create new external wallet.
245
+ -- This is safe: if the client will try to create an external wallet from the same
246
+ -- root public key - error will be thrown.
247
+ void $ V0. createWalletSafe walletId walletMeta isReady
248
+
249
+ addInitAccountInExternalWallet walletId
250
+
251
+ V0. getWallet walletId
252
+
253
+ -- | Restore external wallet using it's root public key and metadata.
254
+ restoreExternalWallet
202
255
:: -- ( MonadThrow m
203
256
-- , MonadUnliftIO m
204
257
-- , HasLens SyncQueue ctx SyncQueue
205
- -- , V0.MonadBlockchainInfo m
206
258
-- , V0.MonadWalletLogic ctx m
207
259
-- )
208
260
-- =>
209
- NewExternalWallet
210
- -> m (WalletResponse Wallet )
211
- newExternalWallet NewExternalWallet {.. } =
212
- error " [CHW-80], Cardano Hardware Wallet, new external wallet, legacy handler, unimplemented yet."
261
+ V0. CWalletMeta
262
+ -> PublicKeyAsBase58
263
+ -> m V0. CWallet
264
+ restoreExternalWallet _walletMeta _encodedRootPK =
265
+ error " [CHW-54], restore external wallet, unimplemented yet."
266
+
267
+ addInitAccountInExternalWallet
268
+ :: ( MonadThrow m
269
+ , V0. MonadWalletLogic ctx m
270
+ )
271
+ => V0. CId V0. Wal
272
+ -> m ()
273
+ addInitAccountInExternalWallet walletId = do
274
+ let accountName = " Initial account"
275
+ accountMeta = V0. CAccountMeta accountName
276
+ accountInit = V0. CAccountInit accountMeta walletId
277
+ includeUnready = True
278
+ void $ V0. newExternalAccountIncludeUnready includeUnready accountInit
213
279
214
280
-- | On the disk, once imported or created, there's so far not much difference
215
281
-- between a wallet and an external wallet, except one: node stores a public key
216
282
-- for external wallet, there's no secret key.
217
283
deleteExternalWallet
218
- :: -- (V0.MonadWalletLogic ctx m)
219
- -- =>
220
- PublicKeyAsBase58
284
+ :: (V0. MonadWalletLogic ctx m )
285
+ => PublicKeyAsBase58
221
286
-> m NoContent
222
- deleteExternalWallet _encodedRootPK =
223
- error " [CHW-106], Cardano Hardware Wallet, delete external wallet, legacy handler, unimplemented yet."
287
+ deleteExternalWallet encodedRootPK =
288
+ case V1. mkPublicKeyFromBase58 encodedRootPK of
289
+ Left problem -> throwM (InvalidPublicKey $ sformat build problem)
290
+ Right rootPK -> V0. deleteExternalWallet rootPK
291
+
292
+ migrateWallet
293
+ :: ( V0. MonadWalletLogicRead ctx m
294
+ , V0. MonadBlockchainInfo m
295
+ )
296
+ => V0. WalletSnapshot
297
+ -> V0. CWallet
298
+ -> Bool
299
+ -> m Wallet
300
+ migrateWallet snapshot wallet walletIsReady = do
301
+ let walletId = V0. cwId wallet
302
+ walletInfo <- if walletIsReady
303
+ then maybeThrow WalletNotFound $ V0. getWalletInfo walletId snapshot
304
+ else
305
+ -- Wallet is not ready yet (because of restoring),
306
+ -- the only information we can provide is the default one.
307
+ pure $ V0. getUnreadyWalletInfo snapshot
308
+ walletIsExternal <- V0. isWalletExternal walletId
309
+ let walletType = if walletIsExternal then WalletExternal else WalletRegular
310
+ currentDepth <- V0. networkChainDifficulty
311
+ migrate (wallet, walletInfo, walletType, currentDepth)
0 commit comments