From 0a1f35eefcc868eee9acfb4ca9bdad0192ac9df3 Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Tue, 14 Jan 2025 13:05:17 -0600 Subject: [PATCH] feat(rpc): implement `getincomingvk` --- CHANGELOG.md | 7 +++++-- src/Zenith/RPC.hs | 41 ++++++++++++++++++++++++++++++++++++++ test/ServerSpec.hs | 49 ++++++++++++++++++++++++++++++++++++++++++++++ zenith.cabal | 2 +- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56cd4d8..411cbbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- RPC method `shieldnotes` -- RPC method `deshieldfunds` +- RPC methods: + - `shieldnotes` + - `deshieldfunds` + - `getfullvk` + - `getincomingvk` ## [0.7.2.0-beta] diff --git a/src/Zenith/RPC.hs b/src/Zenith/RPC.hs index e255333..59b4a5c 100644 --- a/src/Zenith/RPC.hs +++ b/src/Zenith/RPC.hs @@ -129,6 +129,7 @@ data ZenithMethod | ShieldNotes | DeshieldFunds | GetFVK + | GetIVK | UnknownMethod deriving (Eq, Prelude.Show) @@ -147,6 +148,7 @@ instance ToJSON ZenithMethod where toJSON ShieldNotes = Data.Aeson.String "shieldnotes" toJSON DeshieldFunds = Data.Aeson.String "deshieldfunds" toJSON GetFVK = Data.Aeson.String "getfullvk" + toJSON GetIVK = Data.Aeson.String "getincomingvk" toJSON UnknownMethod = Data.Aeson.Null instance FromJSON ZenithMethod where @@ -166,6 +168,7 @@ instance FromJSON ZenithMethod where "shieldnotes" -> pure ShieldNotes "deshieldfunds" -> pure DeshieldFunds "getfullvk" -> pure GetFVK + "getincomingvk" -> pure GetIVK _ -> pure UnknownMethod data ZenithParams @@ -548,6 +551,16 @@ instance FromJSON RpcCall where pure $ RpcCall v i GetFVK (ViewingKeyParams x) else pure $ RpcCall v i GetFVK BadParams _anyOther -> pure $ RpcCall v i GetFVK BadParams + GetIVK -> do + p <- obj .: "params" + case p of + Array a -> + if V.length a == 1 + then do + x <- parseJSON $ a V.! 0 + pure $ RpcCall v i GetIVK (ViewingKeyParams x) + else pure $ RpcCall v i GetIVK BadParams + _anyOther -> pure $ RpcCall v i GetIVK BadParams type ZenithRPC = "status" :> Get '[ JSON] Value :<|> BasicAuth "zenith-realm" Bool :> ReqBody @@ -1089,6 +1102,34 @@ zenithServer state = getinfo :<|> handleRPC ErrorResponse (callId req) (-32006) "Account does not exist." _anyOtherParams -> return $ ErrorResponse (callId req) (-32602) "Invalid params" + GetIVK -> do + case parameters req of + ViewingKeyParams aid -> do + let dbPath = w_dbPath state + let net = w_network state + pool <- liftIO $ runNoLoggingT $ initPool dbPath + acc <- liftIO $ getAccountById pool $ toSqlKey $ fromIntegral aid + case acc of + Just acc' -> do + ivk <- + liftIO $ + try + (deriveUivk + net + (getOrchSK $ zcashAccountOrchSpendKey $ entityVal acc') + (getSapSK $ zcashAccountSapSpendKey $ entityVal acc') + (getTranSK $ zcashAccountTPrivateKey $ entityVal acc')) :: Handler + (Either SomeException T.Text) + case ivk of + Left _ -> + return $ + ErrorResponse (callId req) (-32010) "Internal Error" + Right ivk' -> return $ ViewingKeyResponse (callId req) ivk' + Nothing -> + return $ + ErrorResponse (callId req) (-32006) "Account does not exist." + _anyOtherParams -> + return $ ErrorResponse (callId req) (-32602) "Invalid params" authenticate :: Config -> BasicAuthCheck Bool authenticate config = BasicAuthCheck check diff --git a/test/ServerSpec.hs b/test/ServerSpec.hs index 0606872..1b31bb1 100644 --- a/test/ServerSpec.hs +++ b/test/ServerSpec.hs @@ -774,6 +774,55 @@ main = do Left e -> assertFailure e Right (ViewingKeyResponse i c) -> c `shouldNotBe` "" Right x -> assertFailure $ show x + describe "Incoming" $ do + it "bad credentials" $ do + res <- + makeZenithCall + "127.0.0.1" + nodePort + "baduser" + "idontknow" + GetIVK + BlankParams + res `shouldBe` Left "Invalid credentials" + describe "correct credentials" $ do + it "no parameters" $ do + res <- + makeZenithCall + "127.0.0.1" + nodePort + nodeUser + nodePwd + GetIVK + BlankParams + case res of + Left e -> assertFailure e + Right (ErrorResponse i c m) -> c `shouldBe` (-32602) + it "invalid account" $ do + res <- + makeZenithCall + "127.0.0.1" + nodePort + nodeUser + nodePwd + GetIVK + (ViewingKeyParams 27) + case res of + Left e -> assertFailure e + Right (ErrorResponse i c m) -> c `shouldBe` (-32006) + it "valid account" $ do + res <- + makeZenithCall + "127.0.0.1" + nodePort + nodeUser + nodePwd + GetIVK + (ViewingKeyParams 1) + case res of + Left e -> assertFailure e + Right (ViewingKeyResponse i c) -> c `shouldNotBe` "" + Right x -> assertFailure $ show x startAPI :: Config -> IO () startAPI config = do diff --git a/zenith.cabal b/zenith.cabal index be3f61a..e5c13cf 100644 --- a/zenith.cabal +++ b/zenith.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: zenith -version: 0.7.2.0-beta +version: 0.8.0.0-beta license: MIT license-file: LICENSE author: Rene Vergara