From 6c9679c1e41eda05d9a7b8cd8319c82a80f37293 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Sat, 6 Apr 2024 13:22:02 +0200 Subject: [PATCH] Split `withWallet` into `withWalletInit` and `withWalletLoad` --- .../customer-deposit-wallet.cabal | 1 + .../src/Cardano/Wallet/Deposit/IO.hs | 47 ++++++++++++++----- .../src/Cardano/Wallet/Deposit/Pure.hs | 10 ++-- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index f367ed72a36..2f92af6fcfa 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -48,6 +48,7 @@ library , async , base , bytestring + , cardano-crypto , cardano-wallet:cardano-wallet , cardano-wallet-network-layer , cardano-wallet-primitive diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs index 71ccf2a2779..babc78de9c6 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs @@ -4,12 +4,13 @@ module Cardano.Wallet.Deposit.IO ( -- * Types - WalletEnv + WalletEnv (..) , WalletInstance -- * Operations -- ** Initialization - , withWallet + , withWalletInit + , withWalletLoad -- ** Mapping between customers and addresses , listCustomers @@ -25,6 +26,9 @@ module Cardano.Wallet.Deposit.IO import Prelude +import Cardano.Crypto.Wallet + ( XPub + ) import Cardano.Wallet.Deposit.Pure ( Customer , WalletState @@ -96,20 +100,37 @@ readWalletState WalletInstance{env,walletState} = Operations Initialization ------------------------------------------------------------------------------} -withWallet :: WalletEnv IO -> (WalletInstance -> IO a) -> IO a -withWallet env@WalletEnv{..} action = do - walletState <- loadWalletStateFromDatabase +-- | Initialize a new wallet in the given environment. +withWalletInit + :: XPub + -> Integer + -> WalletEnv IO + -> (WalletInstance -> IO a) + -> IO a +withWalletInit xpub knownCustomerCount env@WalletEnv{..} action = do + walletState <- atomically + $ DBVar.initDBVar database + $ Wallet.fromXPubAndGenesis xpub knownCustomerCount genesisData + withWalletDBVar env walletState action + +-- | Load an existing wallet from the given environment. +withWalletLoad + :: WalletEnv IO + -> (WalletInstance -> IO a) + -> IO a +withWalletLoad env@WalletEnv{..} action = do + walletState <- atomically $ DBVar.loadDBVar database + withWalletDBVar env walletState action + +withWalletDBVar + :: WalletEnv IO + -> DBVar.DBVar DB.SqlM Wallet.DeltaWalletState + -> (WalletInstance -> IO a) + -> IO a +withWalletDBVar env@WalletEnv{..} walletState action = do let w = WalletInstance{env,walletState} Async.withAsync (doChainSync w) $ \_ -> action w where - loadWalletStateFromDatabase = atomically $ do - es <- Store.loadS database - case es of - Left _ -> - DBVar.initDBVar database $ Wallet.fromGenesis genesisData - Right _ -> - DBVar.loadDBVar database - doChainSync = Network.chainSync networkEnv trChainSync . chainFollower trChainSync = contramap (\_ -> WalletLogDummy) logger chainFollower w = Network.ChainFollower diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs index 5ac6917360f..12d4ca77fdf 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs @@ -15,7 +15,7 @@ module Cardano.Wallet.Deposit.Pure , isCustomerAddress -- ** Reading from the blockchain - , fromGenesis + , fromXPubAndGenesis , localTip , availableBalance , rollForwardMany @@ -38,6 +38,9 @@ module Cardano.Wallet.Deposit.Pure import Prelude +import Cardano.Crypto.Wallet + ( XPub + ) import Cardano.Wallet.Deposit.Pure.UTxOHistory ( UTxOHistory ) @@ -116,8 +119,9 @@ isCustomerAddress _ _ = Nothing Reading from the blockchain ------------------------------------------------------------------------------} -fromGenesis :: Read.GenesisData -> WalletState -fromGenesis = undefined +fromXPubAndGenesis :: XPub -> Integer -> Read.GenesisData -> WalletState +fromXPubAndGenesis _xpub _knownCustomerCount _ = fromGenesisUTxO mempty + -- FIXME: This is a mock implementation fromGenesisUTxO :: Read.UTxO -> WalletState fromGenesisUTxO utxo =