New Wallet creation flow #67

Merged
pitmutt merged 8 commits from rav001 into dev041 2024-02-28 21:23:37 +00:00
3 changed files with 77 additions and 41 deletions
Showing only changes of commit ec422c1c55 - Show all commits

View file

@ -31,16 +31,35 @@ share
[persistLowerCase| [persistLowerCase|
ZcashWallet ZcashWallet
seedPhrase Phrase seedPhrase Phrase
spendingKey BS.ByteString
tPrivateKey BS.ByteString
birthdayHeight Int birthdayHeight Int
name T.Text name T.Text
network ZcashNet network ZcashNet
deriving Show deriving Show
ZcashAccount
walletId ZcashWalletId
index Int
orchSpendKey BS.ByteString
sapSpendKey BS.ByteString
tPrivateKey BS.ByteString
deriving Show
WalletAddress
accId ZcashAccountId
index Int
orchRec BS.ByteString Maybe
sapRec BS.ByteString Maybe
tRec BS.ByteString Maybe
encoded T.Text
deriving Show
|] |]
getWallets :: ZcashNet -> IO [Entity ZcashWallet] getWallets :: T.Text -> ZcashNet -> IO [Entity ZcashWallet]
getWallets n = getWallets dbFp n =
runSqlite "zenith.db" $ do runSqlite dbFp $ do
s <- selectList [ZcashWalletNetwork ==. n] [] s <- selectList [ZcashWalletNetwork ==. n] []
liftIO $ return s liftIO $ return s
getAccounts :: T.Text -> ZcashWalletId -> IO [Entity ZcashAccount]
getAccounts dbFp w =
runSqlite dbFp $ do
s <- selectList [ZcashAccountWalletId ==. w] []
liftIO $ return s

View file

@ -1,51 +1,67 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
import Control.Monad.IO.Class (liftIO) import Control.Monad (when)
import Database.Persist import Database.Persist
import Database.Persist.Sqlite import Database.Persist.Sqlite
import System.Directory
import Test.Hspec import Test.Hspec
import ZcashHaskell.Types (ZcashNet(..)) import ZcashHaskell.Types (ZcashNet(..))
import Zenith.DB import Zenith.DB
import Zenith.DB
( EntityField(ZcashWalletId, ZcashWalletName)
, ZcashWallet(zcashWalletName)
)
main :: IO () main :: IO ()
main = do main = do
checkDbFile <- doesFileExist "test.db"
when checkDbFile $ removeFile "test.db"
hspec $ do hspec $ do
describe "Database tests" $ do describe "Database tests" $ do
it "Create table" $ do it "Create table" $ do
s <- runSqlite "test.db" $ do runMigration migrateAll s <- runSqlite "test.db" $ do runMigration migrateAll
s `shouldBe` () s `shouldBe` ()
it "insert wallet record" $ do describe "Wallet Table" $ do
s <- it "insert wallet record" $ do
runSqlite "test.db" $ do s <-
insert $ runSqlite "test.db" $ do
ZcashWallet insert $
"one two three four five six seven eight nine ten eleven twelve" ZcashWallet
"123456789" "one two three four five six seven eight nine ten eleven twelve"
"987654321" 2000000
2000000 "Main Wallet"
"Main Wallet" MainNet
MainNet fromSqlKey s `shouldBe` 1
fromSqlKey s `shouldBe` 1 it "read wallet record" $ do
it "read wallet record" $ do s <-
s <- runSqlite "test.db" $ do
runSqlite "test.db" $ do selectList [ZcashWalletBirthdayHeight >. 0] []
selectList [ZcashWalletBirthdayHeight >. 0] [] length s `shouldBe` 1
length s `shouldBe` 1 it "modify wallet record" $ do
it "modify wallet record" $ do s <-
s <- runSqlite "test.db" $ do
runSqlite "test.db" $ do let recId = toSqlKey 1 :: ZcashWalletId
let recId = toSqlKey 1 :: ZcashWalletId update recId [ZcashWalletName =. "New Wallet"]
update recId [ZcashWalletName =. "New Wallet"] get recId
get recId "New Wallet" `shouldBe` maybe "None" zcashWalletName s
"New Wallet" `shouldBe` maybe "None" zcashWalletName s it "delete wallet record" $ do
it "delete wallet record" $ do s <-
s <- runSqlite "test.db" $ do
runSqlite "test.db" $ do let recId = toSqlKey 1 :: ZcashWalletId
let recId = toSqlKey 1 :: ZcashWalletId delete recId
delete recId get recId
get recId "None" `shouldBe` maybe "None" zcashWalletName s
"None" `shouldBe` maybe "None" zcashWalletName s describe "Account table" $ do
it "insert account" $ do
s <-
runSqlite "test.db" $ do
insert $
ZcashWallet
"one two three four five six seven eight nine ten eleven twelve"
2000000
"Main Wallet"
MainNet
t <-
runSqlite "test.db" $ do
insert $ ZcashAccount s 0 "132465798" "987654321" "739182462"
fromSqlKey t `shouldBe` 1
it "read accounts for wallet" $ do
wList <- getWallets "test.db" MainNet
acc <- getAccounts "test.db" $ entityKey (head wList)
length acc `shouldBe` 1

View file

@ -104,6 +104,7 @@ test-suite zenith-tests
, persistent , persistent
, persistent-sqlite , persistent-sqlite
, hspec , hspec
, directory
, zcash-haskell , zcash-haskell
, zenith , zenith
pkgconfig-depends: rustzcash_wrapper pkgconfig-depends: rustzcash_wrapper