diff --git a/src/Zenith/DB.hs b/src/Zenith/DB.hs index a48151d..03cc451 100644 --- a/src/Zenith/DB.hs +++ b/src/Zenith/DB.hs @@ -77,6 +77,7 @@ import Zenith.Types , TransparentSpendingKeyDB , UnifiedAddressDB(..) , ZcashNetDB(..) + , ZcashPool(..) ) share @@ -246,6 +247,15 @@ share position Int UniqueSSPos tx position deriving Show Eq + QrCode + address WalletAddressId OnDeleteCascade OnUpdateCascade + version ZcashPool + bytes BS.ByteString + height Int + width Int + name T.Text + UniqueQr address version + deriving Show Eq |] -- * Database functions @@ -416,6 +426,16 @@ getWalletAddresses pool w = do addrs <- mapM (getAddresses pool . entityKey) accs return $ concat addrs +getExternalAddresses :: ConnectionPool -> IO [Entity WalletAddress] +getExternalAddresses pool = do + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + select $ do + addrs <- from $ table @WalletAddress + where_ $ addrs ^. WalletAddressScope ==. val (ScopeDB External) + return addrs + -- | Returns the largest address index for the given account getMaxAddress :: ConnectionPool -- ^ The database path @@ -548,6 +568,41 @@ getZcashTransactions pool b = orderBy [asc $ txs ^. ZcashTransactionBlock] return txs +-- ** QR codes +-- | Functions to manage the QR codes stored in the database +saveQrCode :: + ConnectionPool -- ^ the connection pool + -> QrCode + -> NoLoggingT IO (Maybe (Entity QrCode)) +saveQrCode pool qr = + PS.retryOnBusy $ flip PS.runSqlPool pool $ insertUniqueEntity qr + +getQrCodes :: + ConnectionPool -- ^ the connection pool + -> WalletAddressId + -> IO [Entity QrCode] +getQrCodes pool wId = + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + select $ do + qrs <- from $ table @QrCode + where_ $ qrs ^. QrCodeAddress ==. val wId + return qrs + +getQrCode :: ConnectionPool -> ZcashPool -> WalletAddressId -> IO (Maybe QrCode) +getQrCode pool zp wId = do + r <- + runNoLoggingT $ + PS.retryOnBusy $ + flip PS.runSqlPool pool $ do + selectOne $ do + qrs <- from $ table @QrCode + where_ $ qrs ^. QrCodeAddress ==. val wId + where_ $ qrs ^. QrCodeVersion ==. val zp + return qrs + return $ entityVal <$> r + -- * Wallet -- | Get the block of the last transaction known to the wallet getMaxWalletBlock :: diff --git a/src/Zenith/Types.hs b/src/Zenith/Types.hs index 5526aa6..6176c17 100644 --- a/src/Zenith/Types.hs +++ b/src/Zenith/Types.hs @@ -143,7 +143,9 @@ data ZcashPool | Sprout | Sapling | Orchard - deriving (Show, Eq, Generic, ToJSON) + deriving (Show, Read, Eq, Generic, ToJSON) + +derivePersistField "ZcashPool" instance FromJSON ZcashPool where parseJSON =