Skip to content

Commit

Permalink
make cert visible in getTransaction for multisig
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed May 30, 2023
1 parent 6b87961 commit e1de216
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
14 changes: 9 additions & 5 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Server/Error.hs
Expand Up @@ -715,17 +715,21 @@ instance IsServerError ErrFetchRewards where
instance IsServerError ErrReadRewardAccount where
toServerError = \case
ErrReadRewardAccountNotAShelleyWallet ->
apiError err403 InvalidWalletType $ mconcat
[ "It is regrettable but you've just attempted an operation "
, "that is invalid for this type of wallet. Only new 'Shelley' "
, "wallets can do something with rewards and this one isn't."
]
apiError err403 InvalidWalletType $ mconcat errMsg
ErrReadRewardAccountNotASharedWallet ->
apiError err403 InvalidWalletType $ mconcat errMsg
ErrReadRewardAccountMissing ->
apiError err501 MissingRewardAccount $ mconcat
[ "I couldn't read a reward account which is required for "
, "withdrawals. Either there is db malfunction or withdrawals "
, "was used for shared wallets missing delegation template."
]
where
errMsg =
[ "It is regrettable but you've just attempted an operation "
, "that is invalid for this type of wallet. Only new 'Shelley' and "
, "'Shared' wallets can do something with rewards and this one isn't."
]

instance IsServerError ErrReadPolicyPublicKey where
toServerError = \case
Expand Down
20 changes: 15 additions & 5 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Expand Up @@ -4447,7 +4447,7 @@ mkApiTransaction timeInterpreter wrk timeRefLens tx = do
parsedValues <- traverse parseTxCBOR $ tx ^. #txCBOR
parsedCertificates <-
if hasDelegation (Proxy @s)
then traverse (getApiAnyCertificates db) parsedValues
then traverse (getApiAnyCertificates db (keyFlavorFromState @s)) parsedValues
else pure Nothing
parsedMintBurn <- forM parsedValues
$ getTxApiAssetMintBurn @_ @s wrk
Expand Down Expand Up @@ -4498,10 +4498,20 @@ mkApiTransaction timeInterpreter wrk timeRefLens tx = do

-- | Promote certificates of a transaction to API type,
-- using additional context from the 'WorkerCtx'.
getApiAnyCertificates db ParsedTxCBOR{certificates} = do
(rewardAccount, _, derivPath) <- liftHandler
$ W.shelleyOnlyReadRewardAccount @s db
pure $ mkApiAnyCertificate (Just rewardAccount) derivPath <$> certificates
getApiAnyCertificates db flavor ParsedTxCBOR{certificates} = case flavor of
ShelleyKeyS -> do
(rewardAcct, _, path) <- liftHandler
$ W.shelleyOnlyReadRewardAccount @s db
pure $ mkApiAnyCertificate (Just rewardAcct) path <$> certificates
SharedKeyS -> do
infoM <- liftHandler
$ W.sharedOnlyReadRewardAccount @s db
case infoM of
Just (rewardAcct, path) ->
pure $ mkApiAnyCertificate (Just rewardAcct) path <$> certificates
_ -> pure []
_ ->
pure []

depositIfAny :: Natural
depositIfAny
Expand Down
32 changes: 22 additions & 10 deletions lib/wallet/src/Cardano/Wallet.hs
Expand Up @@ -91,6 +91,7 @@ module Cardano.Wallet
, mkSelfWithdrawal
, mkSelfWithdrawalShared
, shelleyOnlyMkSelfWithdrawal
, sharedOnlyReadRewardAccount
, readRewardAccount
, shelleyOnlyReadRewardAccount
, someRewardAccount
Expand Down Expand Up @@ -586,8 +587,6 @@ import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Maybe
( fromJust, fromMaybe, isJust, isNothing, mapMaybe, maybeToList )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Set
Expand Down Expand Up @@ -1320,10 +1319,6 @@ readRewardAccount db = do
let xpub = Seq.rewardAccountKey walletState
let path = stakeDerivationPath $ Seq.derivationPrefix walletState
pure (toRewardAccount xpub, getRawKey ShelleyKeyS xpub, path)
where
readWalletCheckpoint
:: DBLayer IO s -> IO (Wallet s)
readWalletCheckpoint DBLayer{..} = liftIO $ atomically readCheckpoint

readSharedRewardAccount
:: forall n
Expand All @@ -1335,10 +1330,26 @@ readSharedRewardAccount db = do
case Shared.rewardAccountKey walletState of
Just rewardAcct -> pure $ Just (rewardAcct, path)
Nothing -> pure Nothing
where
readWalletCheckpoint
:: DBLayer IO s -> IO (Wallet s)
readWalletCheckpoint DBLayer{..} = liftIO $ atomically readCheckpoint

readWalletCheckpoint
:: DBLayer IO s -> IO (Wallet s)
readWalletCheckpoint DBLayer{..} = liftIO $ atomically readCheckpoint

sharedOnlyReadRewardAccount
:: forall s
. WalletFlavor s
=> DBLayer IO s
-> ExceptT ErrReadRewardAccount IO
(Maybe (RewardAccount, NonEmpty DerivationIndex))
sharedOnlyReadRewardAccount db = do
case walletFlavor @s of
SharedWallet -> do
walletState <- lift $ getState <$> readWalletCheckpoint db
let path = stakeDerivationPath $ Shared.derivationPrefix walletState
case Shared.rewardAccountKey walletState of
Just rewardAcct -> pure $ Just (rewardAcct, path)
Nothing -> pure Nothing
_ -> throwE ErrReadRewardAccountNotASharedWallet

-- | Unsafe version of the `readRewardAccount` function
-- that throws error when applied to a non-sequential
Expand Down Expand Up @@ -3470,6 +3481,7 @@ data ErrNotASequentialWallet

data ErrReadRewardAccount
= ErrReadRewardAccountNotAShelleyWallet
| ErrReadRewardAccountNotASharedWallet
| ErrReadRewardAccountMissing
deriving (Generic, Eq, Show)

Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/src/Cardano/Wallet/Address/HasDelegation.hs
Expand Up @@ -39,4 +39,4 @@ instance HasDelegation (SeqState n IcarusKey) where
hasDelegation _ = False

instance HasDelegation (SharedState n SharedKey) where
hasDelegation _ = False
hasDelegation _ = True

0 comments on commit e1de216

Please sign in to comment.