2024-01-22 18:58:37 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE EmptyDataDecls #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
{-# LANGUAGE DerivingStrategies #-}
|
2024-03-01 20:57:13 +00:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2024-01-22 18:58:37 +00:00
|
|
|
{-# LANGUAGE StandaloneDeriving #-}
|
|
|
|
{-# LANGUAGE UndecidableInstances #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2024-01-23 15:55:24 +00:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2024-04-07 14:25:25 +00:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2024-01-22 18:58:37 +00:00
|
|
|
|
2024-01-17 18:15:21 +00:00
|
|
|
module Zenith.DB where
|
2024-01-22 18:58:37 +00:00
|
|
|
|
2024-04-03 20:14:14 +00:00
|
|
|
import Control.Monad (when)
|
2024-04-21 12:07:51 +00:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2024-04-08 20:51:14 +00:00
|
|
|
import Data.Bifunctor
|
2024-01-22 18:58:37 +00:00
|
|
|
import qualified Data.ByteString as BS
|
2024-03-22 20:39:37 +00:00
|
|
|
import Data.HexString
|
2024-04-03 20:14:14 +00:00
|
|
|
import Data.Maybe (fromJust, isJust)
|
2024-01-22 18:58:37 +00:00
|
|
|
import qualified Data.Text as T
|
2024-04-07 14:25:25 +00:00
|
|
|
import qualified Data.Text.Encoding as TE
|
2024-04-18 01:28:47 +00:00
|
|
|
import Data.Word
|
2024-04-07 14:25:25 +00:00
|
|
|
import Database.Esqueleto.Experimental
|
|
|
|
import qualified Database.Persist as P
|
|
|
|
import qualified Database.Persist.Sqlite as PS
|
2024-01-22 18:58:37 +00:00
|
|
|
import Database.Persist.TH
|
2024-04-07 14:25:25 +00:00
|
|
|
import Haskoin.Transaction.Common
|
|
|
|
( OutPoint(..)
|
|
|
|
, TxIn(..)
|
|
|
|
, TxOut(..)
|
|
|
|
, txHashToHex
|
|
|
|
)
|
|
|
|
import ZcashHaskell.Orchard (isValidUnifiedAddress)
|
2024-04-08 20:51:14 +00:00
|
|
|
import ZcashHaskell.Sapling (decodeSaplingOutputEsk)
|
2024-04-03 20:14:14 +00:00
|
|
|
import ZcashHaskell.Types
|
2024-04-08 20:51:14 +00:00
|
|
|
( DecodedNote(..)
|
|
|
|
, OrchardAction(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, OrchardBundle(..)
|
2024-04-21 12:07:51 +00:00
|
|
|
, OrchardSpendingKey(..)
|
2024-04-18 01:28:47 +00:00
|
|
|
, OrchardWitness(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, SaplingBundle(..)
|
2024-04-18 01:28:47 +00:00
|
|
|
, SaplingCommitmentTree(..)
|
2024-04-21 12:07:51 +00:00
|
|
|
, SaplingSpendingKey(..)
|
2024-04-18 01:28:47 +00:00
|
|
|
, SaplingWitness(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, Scope(..)
|
|
|
|
, ShieldedOutput(..)
|
|
|
|
, ShieldedSpend(..)
|
2024-04-21 12:07:51 +00:00
|
|
|
, ToBytes(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, Transaction(..)
|
2024-04-07 14:25:25 +00:00
|
|
|
, TransparentAddress(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, TransparentBundle(..)
|
2024-04-18 01:28:47 +00:00
|
|
|
, TransparentReceiver(..)
|
2024-04-07 14:25:25 +00:00
|
|
|
, UnifiedAddress(..)
|
2024-04-03 20:14:14 +00:00
|
|
|
, ZcashNet
|
2024-04-21 12:07:51 +00:00
|
|
|
, decodeHexText
|
2024-04-03 20:14:14 +00:00
|
|
|
)
|
2024-03-17 12:17:52 +00:00
|
|
|
import Zenith.Types
|
2024-04-18 01:28:47 +00:00
|
|
|
( Config(..)
|
|
|
|
, HexStringDB(..)
|
2024-03-22 20:39:37 +00:00
|
|
|
, OrchardSpendingKeyDB(..)
|
2024-03-17 12:17:52 +00:00
|
|
|
, PhraseDB(..)
|
|
|
|
, SaplingSpendingKeyDB(..)
|
|
|
|
, ScopeDB(..)
|
|
|
|
, TransparentSpendingKeyDB
|
|
|
|
, UnifiedAddressDB(..)
|
2024-04-21 12:07:51 +00:00
|
|
|
, UserTx(..)
|
2024-03-17 12:17:52 +00:00
|
|
|
, ZcashNetDB(..)
|
|
|
|
)
|
2024-03-05 18:34:30 +00:00
|
|
|
|
2024-01-22 18:58:37 +00:00
|
|
|
share
|
|
|
|
[mkPersist sqlSettings, mkMigrate "migrateAll"]
|
|
|
|
[persistLowerCase|
|
|
|
|
ZcashWallet
|
|
|
|
name T.Text
|
2024-03-17 12:17:52 +00:00
|
|
|
network ZcashNetDB
|
|
|
|
seedPhrase PhraseDB
|
2024-03-01 20:57:13 +00:00
|
|
|
birthdayHeight Int
|
2024-02-28 22:37:43 +00:00
|
|
|
UniqueWallet name network
|
2024-03-01 20:57:13 +00:00
|
|
|
deriving Show Eq
|
2024-02-27 14:33:12 +00:00
|
|
|
ZcashAccount
|
|
|
|
index Int
|
2024-03-01 20:57:13 +00:00
|
|
|
walletId ZcashWalletId
|
|
|
|
name T.Text
|
2024-03-17 12:17:52 +00:00
|
|
|
orchSpendKey OrchardSpendingKeyDB
|
|
|
|
sapSpendKey SaplingSpendingKeyDB
|
|
|
|
tPrivateKey TransparentSpendingKeyDB
|
2024-03-01 20:57:13 +00:00
|
|
|
UniqueAccount index walletId
|
2024-03-05 18:34:30 +00:00
|
|
|
UniqueAccName walletId name
|
2024-03-01 20:57:13 +00:00
|
|
|
deriving Show Eq
|
2024-02-27 14:33:12 +00:00
|
|
|
WalletAddress
|
|
|
|
index Int
|
2024-03-05 18:34:30 +00:00
|
|
|
accId ZcashAccountId
|
|
|
|
name T.Text
|
2024-03-17 12:17:52 +00:00
|
|
|
uAddress UnifiedAddressDB
|
|
|
|
scope ScopeDB
|
|
|
|
UniqueAddress index scope accId
|
2024-03-05 18:34:30 +00:00
|
|
|
UniqueAddName accId name
|
2024-03-01 20:57:13 +00:00
|
|
|
deriving Show Eq
|
2024-03-22 20:39:37 +00:00
|
|
|
WalletTransaction
|
2024-04-04 18:21:55 +00:00
|
|
|
txId HexStringDB
|
2024-04-04 19:35:08 +00:00
|
|
|
block Int
|
2024-04-04 18:21:55 +00:00
|
|
|
conf Int
|
|
|
|
time Int
|
2024-04-07 14:25:25 +00:00
|
|
|
UniqueWTx txId
|
2024-04-04 18:21:55 +00:00
|
|
|
deriving Show Eq
|
|
|
|
WalletTrNote
|
|
|
|
tx WalletTransactionId
|
2024-04-18 01:28:47 +00:00
|
|
|
value Word64
|
2024-04-04 18:21:55 +00:00
|
|
|
spent Bool
|
2024-04-18 01:28:47 +00:00
|
|
|
script BS.ByteString
|
2024-04-21 12:07:51 +00:00
|
|
|
change Bool
|
2024-04-18 01:28:47 +00:00
|
|
|
UniqueTNote tx script
|
2024-04-04 18:21:55 +00:00
|
|
|
deriving Show Eq
|
2024-04-21 12:07:51 +00:00
|
|
|
WalletTrSpend
|
|
|
|
tx WalletTransactionId
|
|
|
|
note WalletTrNoteId
|
|
|
|
value Word64
|
|
|
|
deriving Show Eq
|
2024-04-04 18:21:55 +00:00
|
|
|
WalletSapNote
|
|
|
|
tx WalletTransactionId
|
2024-04-18 01:28:47 +00:00
|
|
|
value Word64
|
2024-04-04 18:21:55 +00:00
|
|
|
recipient BS.ByteString
|
|
|
|
memo T.Text
|
|
|
|
spent Bool
|
2024-04-07 14:25:25 +00:00
|
|
|
nullifier HexStringDB
|
2024-04-18 01:28:47 +00:00
|
|
|
position Word64
|
|
|
|
witness HexStringDB
|
2024-04-21 12:07:51 +00:00
|
|
|
change Bool
|
2024-04-18 01:28:47 +00:00
|
|
|
UniqueSapNote tx nullifier
|
2024-04-04 18:21:55 +00:00
|
|
|
deriving Show Eq
|
2024-04-21 12:07:51 +00:00
|
|
|
WalletSapSpend
|
|
|
|
tx WalletTransactionId
|
|
|
|
note WalletSapNoteId
|
|
|
|
value Word64
|
|
|
|
deriving Show Eq
|
2024-04-04 18:21:55 +00:00
|
|
|
WalletOrchNote
|
|
|
|
tx WalletTransactionId
|
2024-04-18 01:28:47 +00:00
|
|
|
value Word64
|
2024-04-04 18:21:55 +00:00
|
|
|
recipient BS.ByteString
|
|
|
|
memo T.Text
|
|
|
|
spent Bool
|
2024-04-07 14:25:25 +00:00
|
|
|
nullifier HexStringDB
|
2024-04-18 01:28:47 +00:00
|
|
|
position Word64
|
|
|
|
witness HexStringDB
|
2024-04-21 12:07:51 +00:00
|
|
|
change Bool
|
2024-04-18 01:28:47 +00:00
|
|
|
UniqueOrchNote tx nullifier
|
2024-04-04 18:21:55 +00:00
|
|
|
deriving Show Eq
|
2024-04-21 12:07:51 +00:00
|
|
|
WalletOrchSpend
|
|
|
|
tx WalletTransactionId
|
|
|
|
note WalletOrchNoteId
|
|
|
|
value Word64
|
|
|
|
deriving Show Eq
|
2024-04-04 18:21:55 +00:00
|
|
|
ZcashTransaction
|
2024-03-22 20:39:37 +00:00
|
|
|
block Int
|
|
|
|
txId HexStringDB
|
|
|
|
conf Int
|
|
|
|
time Int
|
2024-04-18 01:28:47 +00:00
|
|
|
UniqueTx block txId
|
2024-03-22 20:39:37 +00:00
|
|
|
deriving Show Eq
|
2024-04-03 20:14:14 +00:00
|
|
|
TransparentNote
|
2024-04-04 18:21:55 +00:00
|
|
|
tx ZcashTransactionId
|
2024-04-18 01:28:47 +00:00
|
|
|
value Word64
|
2024-04-03 20:14:14 +00:00
|
|
|
script BS.ByteString
|
2024-04-07 14:25:25 +00:00
|
|
|
position Int
|
|
|
|
UniqueTNPos tx position
|
|
|
|
deriving Show Eq
|
|
|
|
TransparentSpend
|
|
|
|
tx ZcashTransactionId
|
|
|
|
outPointHash HexStringDB
|
|
|
|
outPointIndex Int
|
|
|
|
script BS.ByteString
|
|
|
|
seq Int
|
|
|
|
position Int
|
|
|
|
UniqueTSPos tx position
|
|
|
|
deriving Show Eq
|
2024-03-22 20:39:37 +00:00
|
|
|
OrchAction
|
2024-04-04 18:21:55 +00:00
|
|
|
tx ZcashTransactionId
|
2024-03-22 20:39:37 +00:00
|
|
|
nf HexStringDB
|
|
|
|
rk HexStringDB
|
|
|
|
cmx HexStringDB
|
|
|
|
ephKey HexStringDB
|
|
|
|
encCipher HexStringDB
|
|
|
|
outCipher HexStringDB
|
|
|
|
cv HexStringDB
|
|
|
|
auth HexStringDB
|
2024-04-07 14:25:25 +00:00
|
|
|
position Int
|
|
|
|
UniqueOAPos tx position
|
2024-03-22 20:39:37 +00:00
|
|
|
deriving Show Eq
|
|
|
|
ShieldOutput
|
2024-04-04 18:21:55 +00:00
|
|
|
tx ZcashTransactionId
|
2024-03-22 20:39:37 +00:00
|
|
|
cv HexStringDB
|
|
|
|
cmu HexStringDB
|
|
|
|
ephKey HexStringDB
|
|
|
|
encCipher HexStringDB
|
|
|
|
outCipher HexStringDB
|
|
|
|
proof HexStringDB
|
2024-04-07 14:25:25 +00:00
|
|
|
position Int
|
|
|
|
UniqueSOPos tx position
|
2024-03-22 20:39:37 +00:00
|
|
|
deriving Show Eq
|
|
|
|
ShieldSpend
|
2024-04-04 18:21:55 +00:00
|
|
|
tx ZcashTransactionId
|
2024-03-22 20:39:37 +00:00
|
|
|
cv HexStringDB
|
|
|
|
anchor HexStringDB
|
|
|
|
nullifier HexStringDB
|
|
|
|
rk HexStringDB
|
|
|
|
proof HexStringDB
|
|
|
|
authSig HexStringDB
|
2024-04-07 14:25:25 +00:00
|
|
|
position Int
|
|
|
|
UniqueSSPos tx position
|
2024-03-22 20:39:37 +00:00
|
|
|
deriving Show Eq
|
|
|
|
|]
|
|
|
|
|
2024-03-05 18:34:30 +00:00
|
|
|
-- * Database functions
|
|
|
|
-- | Initializes the database
|
|
|
|
initDb ::
|
|
|
|
T.Text -- ^ The database path to check
|
|
|
|
-> IO ()
|
|
|
|
initDb dbName = do
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbName $ do runMigration migrateAll
|
2024-03-05 18:34:30 +00:00
|
|
|
|
|
|
|
-- | Get existing wallets from database
|
2024-02-27 14:33:12 +00:00
|
|
|
getWallets :: T.Text -> ZcashNet -> IO [Entity ZcashWallet]
|
2024-03-17 12:17:52 +00:00
|
|
|
getWallets dbFp n =
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbFp $
|
|
|
|
select $ do
|
|
|
|
wallets <- from $ table @ZcashWallet
|
|
|
|
where_ (wallets ^. ZcashWalletNetwork ==. val (ZcashNetDB n))
|
|
|
|
pure wallets
|
2024-03-05 18:34:30 +00:00
|
|
|
|
|
|
|
-- | Save a new wallet to the database
|
|
|
|
saveWallet ::
|
|
|
|
T.Text -- ^ The database path to use
|
|
|
|
-> ZcashWallet -- ^ The wallet to add to the database
|
|
|
|
-> IO (Maybe (Entity ZcashWallet))
|
2024-04-07 14:25:25 +00:00
|
|
|
saveWallet dbFp w = PS.runSqlite dbFp $ insertUniqueEntity w
|
2024-03-05 18:34:30 +00:00
|
|
|
|
|
|
|
-- | Returns a list of accounts associated with the given wallet
|
|
|
|
getAccounts ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashWalletId -- ^ The wallet ID to check
|
|
|
|
-> IO [Entity ZcashAccount]
|
2024-04-07 14:25:25 +00:00
|
|
|
getAccounts dbFp w =
|
|
|
|
PS.runSqlite dbFp $
|
|
|
|
select $ do
|
|
|
|
accs <- from $ table @ZcashAccount
|
|
|
|
where_ (accs ^. ZcashAccountWalletId ==. val w)
|
|
|
|
pure accs
|
2024-03-05 18:34:30 +00:00
|
|
|
|
|
|
|
-- | Returns the largest account index for the given wallet
|
|
|
|
getMaxAccount ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashWalletId -- ^ The wallet ID to check
|
|
|
|
-> IO Int
|
|
|
|
getMaxAccount dbFp w = do
|
|
|
|
a <-
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbFp $
|
|
|
|
selectOne $ do
|
|
|
|
accs <- from $ table @ZcashAccount
|
|
|
|
where_ (accs ^. ZcashAccountWalletId ==. val w)
|
|
|
|
orderBy [desc $ accs ^. ZcashAccountIndex]
|
|
|
|
pure accs
|
2024-03-05 18:34:30 +00:00
|
|
|
case a of
|
|
|
|
Nothing -> return $ -1
|
|
|
|
Just x -> return $ zcashAccountIndex $ entityVal x
|
|
|
|
|
|
|
|
-- | Save a new account to the database
|
|
|
|
saveAccount ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashAccount -- ^ The account to add to the database
|
|
|
|
-> IO (Maybe (Entity ZcashAccount))
|
2024-04-07 14:25:25 +00:00
|
|
|
saveAccount dbFp a = PS.runSqlite dbFp $ insertUniqueEntity a
|
2024-03-05 18:34:30 +00:00
|
|
|
|
2024-04-04 18:21:55 +00:00
|
|
|
-- | Returns the largest block in storage
|
|
|
|
getMaxBlock ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> IO Int
|
|
|
|
getMaxBlock dbPath = do
|
|
|
|
b <-
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbPath $
|
|
|
|
selectOne $ do
|
|
|
|
txs <- from $ table @ZcashTransaction
|
|
|
|
where_ (txs ^. ZcashTransactionBlock >. val 0)
|
|
|
|
orderBy [desc $ txs ^. ZcashTransactionBlock]
|
|
|
|
pure txs
|
2024-04-04 18:21:55 +00:00
|
|
|
case b of
|
|
|
|
Nothing -> return $ -1
|
|
|
|
Just x -> return $ zcashTransactionBlock $ entityVal x
|
|
|
|
|
2024-03-05 18:34:30 +00:00
|
|
|
-- | Returns a list of addresses associated with the given account
|
|
|
|
getAddresses ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashAccountId -- ^ The account ID to check
|
|
|
|
-> IO [Entity WalletAddress]
|
2024-03-17 12:17:52 +00:00
|
|
|
getAddresses dbFp a =
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbFp $
|
|
|
|
select $ do
|
|
|
|
addrs <- from $ table @WalletAddress
|
|
|
|
where_ (addrs ^. WalletAddressAccId ==. val a)
|
|
|
|
where_ (addrs ^. WalletAddressScope ==. val (ScopeDB External))
|
|
|
|
pure addrs
|
|
|
|
|
2024-04-21 12:07:51 +00:00
|
|
|
-- | Returns a list of change addresses associated with the given account
|
|
|
|
getInternalAddresses ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashAccountId -- ^ The account ID to check
|
|
|
|
-> IO [Entity WalletAddress]
|
|
|
|
getInternalAddresses dbFp a =
|
|
|
|
PS.runSqlite dbFp $
|
|
|
|
select $ do
|
|
|
|
addrs <- from $ table @WalletAddress
|
|
|
|
where_ (addrs ^. WalletAddressAccId ==. val a)
|
|
|
|
where_ (addrs ^. WalletAddressScope ==. val (ScopeDB Internal))
|
|
|
|
pure addrs
|
|
|
|
|
2024-04-07 14:25:25 +00:00
|
|
|
-- | Returns a list of addressess associated with the given wallet
|
|
|
|
getWalletAddresses ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> ZcashWalletId -- ^ the wallet to search
|
|
|
|
-> IO [Entity WalletAddress]
|
|
|
|
getWalletAddresses dbFp w = do
|
|
|
|
accs <- getAccounts dbFp w
|
|
|
|
addrs <- mapM (getAddresses dbFp . entityKey) accs
|
|
|
|
return $ concat addrs
|
2024-03-05 18:34:30 +00:00
|
|
|
|
|
|
|
-- | Returns the largest address index for the given account
|
|
|
|
getMaxAddress ::
|
|
|
|
T.Text -- ^ The database path
|
2024-03-17 12:17:52 +00:00
|
|
|
-> ZcashAccountId -- ^ The account ID to check
|
|
|
|
-> Scope -- ^ The scope of the address
|
2024-03-05 18:34:30 +00:00
|
|
|
-> IO Int
|
2024-03-17 12:17:52 +00:00
|
|
|
getMaxAddress dbFp aw s = do
|
2024-03-05 18:34:30 +00:00
|
|
|
a <-
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbFp $
|
|
|
|
selectOne $ do
|
|
|
|
addrs <- from $ table @WalletAddress
|
|
|
|
where_ $ addrs ^. WalletAddressAccId ==. val aw
|
|
|
|
where_ $ addrs ^. WalletAddressScope ==. val (ScopeDB s)
|
|
|
|
orderBy [desc $ addrs ^. WalletAddressIndex]
|
|
|
|
pure addrs
|
2024-03-05 18:34:30 +00:00
|
|
|
case a of
|
|
|
|
Nothing -> return $ -1
|
|
|
|
Just x -> return $ walletAddressIndex $ entityVal x
|
|
|
|
|
|
|
|
-- | Save a new address to the database
|
|
|
|
saveAddress ::
|
|
|
|
T.Text -- ^ the database path
|
|
|
|
-> WalletAddress -- ^ The wallet to add to the database
|
|
|
|
-> IO (Maybe (Entity WalletAddress))
|
2024-04-07 14:25:25 +00:00
|
|
|
saveAddress dbFp w = PS.runSqlite dbFp $ insertUniqueEntity w
|
2024-04-03 20:14:14 +00:00
|
|
|
|
|
|
|
-- | Save a transaction to the data model
|
|
|
|
saveTransaction ::
|
|
|
|
T.Text -- ^ the database path
|
|
|
|
-> Int -- ^ block time
|
|
|
|
-> Transaction -- ^ The transaction to save
|
2024-04-04 18:21:55 +00:00
|
|
|
-> IO (Key ZcashTransaction)
|
2024-04-03 20:14:14 +00:00
|
|
|
saveTransaction dbFp t wt =
|
2024-04-07 14:25:25 +00:00
|
|
|
PS.runSqlite dbFp $ do
|
|
|
|
let ix = [0 ..]
|
2024-04-03 20:14:14 +00:00
|
|
|
w <-
|
|
|
|
insert $
|
2024-04-04 18:21:55 +00:00
|
|
|
ZcashTransaction (tx_height wt) (HexStringDB $ tx_id wt) (tx_conf wt) t
|
2024-04-07 14:25:25 +00:00
|
|
|
when (isJust $ tx_transpBundle wt) $ do
|
|
|
|
_ <-
|
|
|
|
insertMany_ $
|
|
|
|
zipWith (curry (storeTxOut w)) ix $
|
|
|
|
(tb_vout . fromJust . tx_transpBundle) wt
|
|
|
|
_ <-
|
|
|
|
insertMany_ $
|
|
|
|
zipWith (curry (storeTxIn w)) ix $
|
|
|
|
(tb_vin . fromJust . tx_transpBundle) wt
|
|
|
|
return ()
|
2024-04-03 20:14:14 +00:00
|
|
|
when (isJust $ tx_saplingBundle wt) $ do
|
|
|
|
_ <-
|
|
|
|
insertMany_ $
|
2024-04-07 14:25:25 +00:00
|
|
|
zipWith (curry (storeSapSpend w)) ix $
|
|
|
|
(sbSpends . fromJust . tx_saplingBundle) wt
|
2024-04-03 20:14:14 +00:00
|
|
|
_ <-
|
|
|
|
insertMany_ $
|
2024-04-07 14:25:25 +00:00
|
|
|
zipWith (curry (storeSapOutput w)) ix $
|
|
|
|
(sbOutputs . fromJust . tx_saplingBundle) wt
|
2024-04-03 20:14:14 +00:00
|
|
|
return ()
|
|
|
|
when (isJust $ tx_orchardBundle wt) $
|
|
|
|
insertMany_ $
|
2024-04-07 14:25:25 +00:00
|
|
|
zipWith (curry (storeOrchAction w)) ix $
|
|
|
|
(obActions . fromJust . tx_orchardBundle) wt
|
2024-04-03 20:14:14 +00:00
|
|
|
return w
|
|
|
|
where
|
2024-04-07 14:25:25 +00:00
|
|
|
storeTxOut :: ZcashTransactionId -> (Int, TxOut) -> TransparentNote
|
|
|
|
storeTxOut wid (i, TxOut v s) = TransparentNote wid (fromIntegral v) s i
|
|
|
|
storeTxIn :: ZcashTransactionId -> (Int, TxIn) -> TransparentSpend
|
|
|
|
storeTxIn wid (i, TxIn (OutPoint h k) s sq) =
|
|
|
|
TransparentSpend
|
|
|
|
wid
|
|
|
|
(HexStringDB . fromText $ txHashToHex h)
|
|
|
|
(fromIntegral k)
|
|
|
|
s
|
|
|
|
(fromIntegral sq)
|
|
|
|
i
|
|
|
|
storeSapSpend :: ZcashTransactionId -> (Int, ShieldedSpend) -> ShieldSpend
|
|
|
|
storeSapSpend wid (i, sp) =
|
2024-04-03 20:14:14 +00:00
|
|
|
ShieldSpend
|
|
|
|
wid
|
|
|
|
(HexStringDB $ sp_cv sp)
|
|
|
|
(HexStringDB $ sp_anchor sp)
|
|
|
|
(HexStringDB $ sp_nullifier sp)
|
|
|
|
(HexStringDB $ sp_rk sp)
|
|
|
|
(HexStringDB $ sp_proof sp)
|
|
|
|
(HexStringDB $ sp_auth sp)
|
2024-04-07 14:25:25 +00:00
|
|
|
i
|
|
|
|
storeSapOutput ::
|
|
|
|
ZcashTransactionId -> (Int, ShieldedOutput) -> ShieldOutput
|
|
|
|
storeSapOutput wid (i, so) =
|
2024-04-03 20:14:14 +00:00
|
|
|
ShieldOutput
|
|
|
|
wid
|
|
|
|
(HexStringDB $ s_cv so)
|
|
|
|
(HexStringDB $ s_cmu so)
|
|
|
|
(HexStringDB $ s_ephKey so)
|
|
|
|
(HexStringDB $ s_encCipherText so)
|
|
|
|
(HexStringDB $ s_outCipherText so)
|
|
|
|
(HexStringDB $ s_proof so)
|
2024-04-07 14:25:25 +00:00
|
|
|
i
|
|
|
|
storeOrchAction :: ZcashTransactionId -> (Int, OrchardAction) -> OrchAction
|
|
|
|
storeOrchAction wid (i, oa) =
|
2024-04-03 20:14:14 +00:00
|
|
|
OrchAction
|
|
|
|
wid
|
|
|
|
(HexStringDB $ nf oa)
|
|
|
|
(HexStringDB $ rk oa)
|
|
|
|
(HexStringDB $ cmx oa)
|
|
|
|
(HexStringDB $ eph_key oa)
|
|
|
|
(HexStringDB $ enc_ciphertext oa)
|
|
|
|
(HexStringDB $ out_ciphertext oa)
|
|
|
|
(HexStringDB $ cv oa)
|
|
|
|
(HexStringDB $ auth oa)
|
2024-04-07 14:25:25 +00:00
|
|
|
i
|
|
|
|
|
|
|
|
-- | Get the transactions from a particular block forward
|
|
|
|
getZcashTransactions ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> Int -- ^ Block
|
|
|
|
-> IO [Entity ZcashTransaction]
|
|
|
|
getZcashTransactions dbFp b =
|
|
|
|
PS.runSqlite dbFp $
|
|
|
|
select $ do
|
|
|
|
txs <- from $ table @ZcashTransaction
|
|
|
|
where_ $ txs ^. ZcashTransactionBlock >. val b
|
|
|
|
orderBy [asc $ txs ^. ZcashTransactionBlock]
|
|
|
|
return txs
|
|
|
|
|
|
|
|
-- * Wallet
|
|
|
|
-- | Get the block of the last transaction known to the wallet
|
|
|
|
getMaxWalletBlock ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> IO Int
|
|
|
|
getMaxWalletBlock dbPath = do
|
|
|
|
b <-
|
|
|
|
PS.runSqlite dbPath $
|
|
|
|
selectOne $ do
|
|
|
|
txs <- from $ table @WalletTransaction
|
|
|
|
where_ $ txs ^. WalletTransactionBlock >. val 0
|
|
|
|
orderBy [desc $ txs ^. WalletTransactionBlock]
|
|
|
|
return txs
|
|
|
|
case b of
|
|
|
|
Nothing -> return $ -1
|
|
|
|
Just x -> return $ walletTransactionBlock $ entityVal x
|
|
|
|
|
2024-04-18 01:28:47 +00:00
|
|
|
-- | Save a @WalletTransaction@
|
|
|
|
saveWalletTransaction ::
|
|
|
|
T.Text -> Entity ZcashTransaction -> IO WalletTransactionId
|
|
|
|
saveWalletTransaction dbPath zt = do
|
|
|
|
let zT' = entityVal zt
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
t <-
|
|
|
|
upsert
|
|
|
|
(WalletTransaction
|
|
|
|
(zcashTransactionTxId zT')
|
|
|
|
(zcashTransactionBlock zT')
|
|
|
|
(zcashTransactionConf zT')
|
|
|
|
(zcashTransactionTime zT'))
|
|
|
|
[]
|
|
|
|
return $ entityKey t
|
|
|
|
|
|
|
|
-- | Save a @WalletSapNote@
|
|
|
|
saveWalletSapNote ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> WalletTransactionId -- ^ The index for the transaction that contains the note
|
|
|
|
-> Integer -- ^ note position
|
|
|
|
-> SaplingWitness -- ^ the Sapling incremental witness
|
2024-04-21 12:07:51 +00:00
|
|
|
-> Bool -- ^ change flag
|
2024-04-18 01:28:47 +00:00
|
|
|
-> DecodedNote -- The decoded Sapling note
|
|
|
|
-> IO ()
|
2024-04-21 12:07:51 +00:00
|
|
|
saveWalletSapNote dbPath wId pos wit ch dn = do
|
2024-04-18 01:28:47 +00:00
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
_ <-
|
|
|
|
upsert
|
|
|
|
(WalletSapNote
|
|
|
|
wId
|
|
|
|
(fromIntegral $ a_value dn)
|
|
|
|
(a_recipient dn)
|
2024-04-21 12:07:51 +00:00
|
|
|
(T.filter (/= '\NUL') $ TE.decodeUtf8Lenient $ a_memo dn)
|
2024-04-18 01:28:47 +00:00
|
|
|
False
|
|
|
|
(HexStringDB $ a_nullifier dn)
|
|
|
|
(fromIntegral pos)
|
2024-04-21 12:07:51 +00:00
|
|
|
(HexStringDB $ sapWit wit)
|
|
|
|
ch)
|
2024-04-18 01:28:47 +00:00
|
|
|
[]
|
|
|
|
return ()
|
|
|
|
|
|
|
|
-- | Save a @WalletOrchNote@
|
|
|
|
saveWalletOrchNote ::
|
|
|
|
T.Text
|
|
|
|
-> WalletTransactionId
|
|
|
|
-> Integer
|
|
|
|
-> OrchardWitness
|
2024-04-21 12:07:51 +00:00
|
|
|
-> Bool
|
2024-04-18 01:28:47 +00:00
|
|
|
-> DecodedNote
|
|
|
|
-> IO ()
|
2024-04-21 12:07:51 +00:00
|
|
|
saveWalletOrchNote dbPath wId pos wit ch dn = do
|
2024-04-18 01:28:47 +00:00
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
_ <-
|
|
|
|
upsert
|
|
|
|
(WalletOrchNote
|
|
|
|
wId
|
|
|
|
(fromIntegral $ a_value dn)
|
|
|
|
(a_recipient dn)
|
2024-04-21 12:07:51 +00:00
|
|
|
(T.filter (/= '\NUL') $ TE.decodeUtf8Lenient $ a_memo dn)
|
2024-04-18 01:28:47 +00:00
|
|
|
False
|
|
|
|
(HexStringDB $ a_nullifier dn)
|
|
|
|
(fromIntegral pos)
|
2024-04-21 12:07:51 +00:00
|
|
|
(HexStringDB $ orchWit wit)
|
|
|
|
ch)
|
2024-04-18 01:28:47 +00:00
|
|
|
[]
|
|
|
|
return ()
|
|
|
|
|
2024-04-07 14:25:25 +00:00
|
|
|
-- | Find the Transparent Notes that match the given transparent receiver
|
|
|
|
findTransparentNotes ::
|
|
|
|
T.Text -- ^ The database path
|
|
|
|
-> Int -- ^ Starting block
|
|
|
|
-> WalletAddress
|
|
|
|
-> IO [(Entity ZcashTransaction, Entity TransparentNote)]
|
|
|
|
findTransparentNotes dbPath b t = do
|
|
|
|
let tReceiver = t_rec =<< readUnifiedAddressDB t
|
|
|
|
case tReceiver of
|
|
|
|
Just tR -> do
|
|
|
|
let s =
|
|
|
|
BS.concat
|
|
|
|
[ BS.pack [0x76, 0xA9, 0x14]
|
2024-04-18 01:28:47 +00:00
|
|
|
, (toBytes . tr_bytes) tR
|
2024-04-07 14:25:25 +00:00
|
|
|
, BS.pack [0x88, 0xAC]
|
|
|
|
]
|
|
|
|
PS.runSqlite dbPath $
|
|
|
|
select $ do
|
|
|
|
(txs :& tNotes) <-
|
|
|
|
from $ table @ZcashTransaction `innerJoin` table @TransparentNote `on`
|
|
|
|
(\(txs :& tNotes) ->
|
|
|
|
txs ^. ZcashTransactionId ==. tNotes ^. TransparentNoteTx)
|
|
|
|
where_ (txs ^. ZcashTransactionBlock >. val b)
|
|
|
|
where_ (tNotes ^. TransparentNoteScript ==. val s)
|
|
|
|
pure (txs, tNotes)
|
|
|
|
Nothing -> return []
|
|
|
|
|
|
|
|
-- | Add the transparent notes to the wallet
|
|
|
|
saveWalletTrNote ::
|
|
|
|
T.Text -- ^ the database path
|
2024-04-21 12:07:51 +00:00
|
|
|
-> Scope
|
2024-04-07 14:25:25 +00:00
|
|
|
-> (Entity ZcashTransaction, Entity TransparentNote)
|
|
|
|
-> IO ()
|
2024-04-21 12:07:51 +00:00
|
|
|
saveWalletTrNote dbPath ch (zt, tn) = do
|
2024-04-07 14:25:25 +00:00
|
|
|
let zT' = entityVal zt
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
t <-
|
|
|
|
upsert
|
|
|
|
(WalletTransaction
|
|
|
|
(zcashTransactionTxId zT')
|
|
|
|
(zcashTransactionBlock zT')
|
|
|
|
(zcashTransactionConf zT')
|
|
|
|
(zcashTransactionTime zT'))
|
|
|
|
[]
|
|
|
|
insert_ $
|
|
|
|
WalletTrNote
|
|
|
|
(entityKey t)
|
|
|
|
(transparentNoteValue $ entityVal tn)
|
|
|
|
False
|
2024-04-18 01:28:47 +00:00
|
|
|
(transparentNoteScript $ entityVal tn)
|
2024-04-21 12:07:51 +00:00
|
|
|
(ch == Internal)
|
2024-04-07 14:25:25 +00:00
|
|
|
|
2024-04-18 01:28:47 +00:00
|
|
|
-- | Save a Sapling note to the wallet database
|
|
|
|
saveSapNote :: T.Text -> WalletSapNote -> IO ()
|
|
|
|
saveSapNote dbPath wsn = PS.runSqlite dbPath $ do insert_ wsn
|
|
|
|
|
|
|
|
-- | Get the shielded outputs from the given blockheight forward
|
|
|
|
getShieldedOutputs ::
|
|
|
|
T.Text -- ^ database path
|
|
|
|
-> Int -- ^ block
|
|
|
|
-> IO [(Entity ZcashTransaction, Entity ShieldOutput)]
|
|
|
|
getShieldedOutputs dbPath b =
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
(txs :& sOutputs) <-
|
|
|
|
from $ table @ZcashTransaction `innerJoin` table @ShieldOutput `on`
|
|
|
|
(\(txs :& sOutputs) ->
|
|
|
|
txs ^. ZcashTransactionId ==. sOutputs ^. ShieldOutputTx)
|
|
|
|
where_ (txs ^. ZcashTransactionBlock >. val b)
|
|
|
|
orderBy
|
|
|
|
[ asc $ txs ^. ZcashTransactionId
|
|
|
|
, asc $ sOutputs ^. ShieldOutputPosition
|
|
|
|
]
|
|
|
|
pure (txs, sOutputs)
|
|
|
|
|
|
|
|
-- | Get the Orchard actions from the given blockheight forward
|
|
|
|
getOrchardActions ::
|
|
|
|
T.Text -- ^ database path
|
|
|
|
-> Int -- ^ block
|
|
|
|
-> IO [(Entity ZcashTransaction, Entity OrchAction)]
|
|
|
|
getOrchardActions dbPath b =
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
(txs :& oActions) <-
|
|
|
|
from $ table @ZcashTransaction `innerJoin` table @OrchAction `on`
|
|
|
|
(\(txs :& oActions) ->
|
|
|
|
txs ^. ZcashTransactionId ==. oActions ^. OrchActionTx)
|
|
|
|
where_ (txs ^. ZcashTransactionBlock >. val b)
|
|
|
|
orderBy
|
|
|
|
[asc $ txs ^. ZcashTransactionId, asc $ oActions ^. OrchActionPosition]
|
|
|
|
pure (txs, oActions)
|
2024-04-08 20:51:14 +00:00
|
|
|
|
2024-04-21 12:07:51 +00:00
|
|
|
-- | Get the transactions belonging to the given address
|
|
|
|
getWalletTransactions ::
|
|
|
|
T.Text -- ^ database path
|
|
|
|
-> WalletAddress
|
|
|
|
-> IO [WalletTransactionId]
|
|
|
|
getWalletTransactions dbPath w = do
|
|
|
|
let tReceiver = t_rec =<< readUnifiedAddressDB w
|
|
|
|
let sReceiver = s_rec =<< readUnifiedAddressDB w
|
|
|
|
let oReceiver = o_rec =<< readUnifiedAddressDB w
|
|
|
|
trNotes <-
|
|
|
|
case tReceiver of
|
|
|
|
Nothing -> return []
|
|
|
|
Just tR -> do
|
|
|
|
let s =
|
|
|
|
BS.concat
|
|
|
|
[ BS.pack [0x76, 0xA9, 0x14]
|
|
|
|
, (toBytes . tr_bytes) tR
|
|
|
|
, BS.pack [0x88, 0xAC]
|
|
|
|
]
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
tnotes <- from $ table @WalletTrNote
|
|
|
|
where_ (tnotes ^. WalletTrNoteScript ==. val s)
|
|
|
|
pure tnotes
|
|
|
|
sapNotes <-
|
|
|
|
case sReceiver of
|
|
|
|
Nothing -> return []
|
|
|
|
Just sR -> do
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
snotes <- from $ table @WalletSapNote
|
|
|
|
where_ (snotes ^. WalletSapNoteRecipient ==. val (getBytes sR))
|
|
|
|
pure snotes
|
|
|
|
orchNotes <-
|
|
|
|
case oReceiver of
|
|
|
|
Nothing -> return []
|
|
|
|
Just oR -> do
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
onotes <- from $ table @WalletOrchNote
|
|
|
|
where_ (onotes ^. WalletOrchNoteRecipient ==. val (getBytes oR))
|
|
|
|
pure onotes
|
|
|
|
let addrTx =
|
|
|
|
map (walletTrNoteTx . entityVal) trNotes <>
|
|
|
|
map (walletSapNoteTx . entityVal) sapNotes <>
|
|
|
|
map (walletOrchNoteTx . entityVal) orchNotes
|
|
|
|
return addrTx
|
|
|
|
|
|
|
|
getUserTx :: T.Text -> [WalletTransactionId] -> IO [UserTx]
|
|
|
|
getUserTx dbPath addrTx = do
|
|
|
|
mapM convertUserTx addrTx
|
|
|
|
where
|
|
|
|
convertUserTx :: WalletTransactionId -> IO UserTx
|
|
|
|
convertUserTx tId = do
|
|
|
|
tr <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
tx <- from $ table @WalletTransaction
|
|
|
|
where_ (tx ^. WalletTransactionId ==. val tId)
|
|
|
|
pure tx
|
|
|
|
trNotes <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
trNotes <- from $ table @WalletTrNote
|
|
|
|
where_ (trNotes ^. WalletTrNoteTx ==. val tId)
|
|
|
|
pure trNotes
|
|
|
|
trSpends <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
trSpends <- from $ table @WalletTrSpend
|
|
|
|
where_ (trSpends ^. WalletTrSpendTx ==. val tId)
|
|
|
|
pure trSpends
|
|
|
|
sapNotes <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
sapNotes <- from $ table @WalletSapNote
|
|
|
|
where_ (sapNotes ^. WalletSapNoteTx ==. val tId)
|
|
|
|
pure sapNotes
|
|
|
|
sapSpends <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
sapSpends <- from $ table @WalletSapSpend
|
|
|
|
where_ (sapSpends ^. WalletSapSpendTx ==. val tId)
|
|
|
|
pure sapSpends
|
|
|
|
orchNotes <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
orchNotes <- from $ table @WalletOrchNote
|
|
|
|
where_ (orchNotes ^. WalletOrchNoteTx ==. val tId)
|
|
|
|
pure orchNotes
|
|
|
|
orchSpends <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
orchSpends <- from $ table @WalletOrchSpend
|
|
|
|
where_ (orchSpends ^. WalletOrchSpendTx ==. val tId)
|
|
|
|
pure orchSpends
|
|
|
|
return $
|
|
|
|
UserTx
|
|
|
|
(getHex $ walletTransactionTxId $ entityVal $ head tr)
|
|
|
|
(fromIntegral $ walletTransactionTime $ entityVal $ head tr)
|
|
|
|
(sum (map (fromIntegral . walletTrNoteValue . entityVal) trNotes) +
|
|
|
|
sum (map (fromIntegral . walletSapNoteValue . entityVal) sapNotes) +
|
|
|
|
sum (map (fromIntegral . walletOrchNoteValue . entityVal) orchNotes) -
|
|
|
|
sum (map (fromIntegral . walletTrSpendValue . entityVal) trSpends) -
|
|
|
|
sum (map (fromIntegral . walletSapSpendValue . entityVal) sapSpends) -
|
|
|
|
sum
|
|
|
|
(map (fromIntegral . walletOrchSpendValue . entityVal) orchSpends))
|
|
|
|
(T.concat (map (walletSapNoteMemo . entityVal) sapNotes) <>
|
|
|
|
T.concat (map (walletOrchNoteMemo . entityVal) orchNotes))
|
|
|
|
|
|
|
|
-- | Sapling DAG-aware spend tracking
|
|
|
|
findSapSpends :: T.Text -> SaplingSpendingKey -> [Entity WalletSapNote] -> IO ()
|
|
|
|
findSapSpends _ _ [] = return ()
|
|
|
|
findSapSpends dbPath sk (n:notes) = do
|
|
|
|
s <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
(tx :& sapSpends) <-
|
|
|
|
from $ table @ZcashTransaction `innerJoin` table @ShieldSpend `on`
|
|
|
|
(\(tx :& sapSpends) ->
|
|
|
|
tx ^. ZcashTransactionId ==. sapSpends ^. ShieldSpendTx)
|
|
|
|
where_
|
|
|
|
(sapSpends ^. ShieldSpendNullifier ==.
|
|
|
|
val (walletSapNoteNullifier (entityVal n)))
|
|
|
|
pure (tx, sapSpends)
|
|
|
|
if null s
|
|
|
|
then findSapSpends dbPath sk notes
|
|
|
|
else do
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
_ <-
|
|
|
|
update $ \w -> do
|
|
|
|
set w [WalletSapNoteSpent =. val True]
|
|
|
|
where_ $ w ^. WalletSapNoteId ==. val (entityKey n)
|
|
|
|
t' <- upsertWalTx $ entityVal $ fst $ head s
|
|
|
|
insert_ $
|
|
|
|
WalletSapSpend
|
|
|
|
(entityKey t')
|
|
|
|
(entityKey n)
|
|
|
|
(walletSapNoteValue $ entityVal n)
|
|
|
|
findSapSpends dbPath sk notes
|
|
|
|
|
|
|
|
findOrchSpends ::
|
|
|
|
T.Text -> OrchardSpendingKey -> [Entity WalletOrchNote] -> IO ()
|
|
|
|
findOrchSpends _ _ [] = return ()
|
|
|
|
findOrchSpends dbPath sk (n:notes) = do
|
|
|
|
s <-
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
select $ do
|
|
|
|
(tx :& orchSpends) <-
|
|
|
|
from $ table @ZcashTransaction `innerJoin` table @OrchAction `on`
|
|
|
|
(\(tx :& orchSpends) ->
|
|
|
|
tx ^. ZcashTransactionId ==. orchSpends ^. OrchActionTx)
|
|
|
|
where_
|
|
|
|
(orchSpends ^. OrchActionNf ==.
|
|
|
|
val (walletOrchNoteNullifier (entityVal n)))
|
|
|
|
pure (tx, orchSpends)
|
|
|
|
if null s
|
|
|
|
then findOrchSpends dbPath sk notes
|
|
|
|
else do
|
|
|
|
PS.runSqlite dbPath $ do
|
|
|
|
_ <-
|
|
|
|
update $ \w -> do
|
|
|
|
set w [WalletOrchNoteSpent =. val True]
|
|
|
|
where_ $ w ^. WalletOrchNoteId ==. val (entityKey n)
|
|
|
|
t' <- upsertWalTx $ entityVal $ fst $ head s
|
|
|
|
insert_ $
|
|
|
|
WalletOrchSpend
|
|
|
|
(entityKey t')
|
|
|
|
(entityKey n)
|
|
|
|
(walletOrchNoteValue $ entityVal n)
|
|
|
|
findOrchSpends dbPath sk notes
|
|
|
|
|
|
|
|
upsertWalTx ::
|
|
|
|
MonadIO m => ZcashTransaction -> SqlPersistT m (Entity WalletTransaction)
|
|
|
|
upsertWalTx zt =
|
|
|
|
upsert
|
|
|
|
(WalletTransaction
|
|
|
|
(zcashTransactionTxId zt)
|
|
|
|
(zcashTransactionBlock zt)
|
|
|
|
(zcashTransactionConf zt)
|
|
|
|
(zcashTransactionTime zt))
|
|
|
|
[]
|
|
|
|
|
2024-04-07 14:25:25 +00:00
|
|
|
-- | Helper function to extract a Unified Address from the database
|
|
|
|
readUnifiedAddressDB :: WalletAddress -> Maybe UnifiedAddress
|
|
|
|
readUnifiedAddressDB =
|
|
|
|
isValidUnifiedAddress . TE.encodeUtf8 . getUA . walletAddressUAddress
|