From 778d7c5669ee57860ba9b07f79f17fc5919b3c72 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 16 Nov 2023 04:48:46 +0000 Subject: [PATCH 1/5] Use `Write.TxIn` type in `RedeemerSpending`. --- .../lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs | 9 ++++----- .../api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs | 2 +- .../test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs | 8 ++++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs index a5b0bf7306e..4a3c84571a8 100644 --- a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs +++ b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs @@ -101,6 +101,7 @@ import Internal.Cardano.Write.Tx , RecentEraLedgerConstraints , ShelleyLedgerEra , StandardCrypto + , TxIn , UTxO , txBody , withConstraints @@ -122,8 +123,6 @@ import qualified Cardano.Ledger.Alonzo.TxWits as Alonzo import qualified Cardano.Ledger.Api as Ledger import qualified Cardano.Wallet.Primitive.Types.Hash as W import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W -import qualified Cardano.Wallet.Primitive.Types.Tx.TxIn as W -import qualified Cardano.Wallet.Shelley.Compatibility.Ledger as Convert import qualified Data.ByteString.Lazy as BL import qualified Data.Map as Map import qualified Data.Map.Merge.Strict as Map @@ -273,7 +272,7 @@ assignScriptRedeemers era pparams timeTranslation utxo redeemers tx = -- data Redeemer - = RedeemerSpending ByteString W.TxIn + = RedeemerSpending ByteString TxIn | RedeemerMinting ByteString W.TokenPolicyId | RedeemerRewarding ByteString CardanoApi.StakeAddress deriving (Eq, Generic, Show) @@ -281,7 +280,7 @@ data Redeemer instance Buildable Redeemer where build = \case RedeemerSpending _ input -> - "spending(" <> build input <> ")" + "spending(" <> build (show input) <> ")" RedeemerMinting _ pid -> "minting(" <> build pid <> ")" RedeemerRewarding _ addr -> @@ -296,7 +295,7 @@ redeemerData = \case toScriptPurpose :: Redeemer -> Alonzo.ScriptPurpose StandardCrypto toScriptPurpose = \case RedeemerSpending _ txin -> - Alonzo.Spending (Convert.toLedger txin) + Alonzo.Spending txin RedeemerMinting _ pid -> Alonzo.Minting (toPolicyID pid) RedeemerRewarding _ (CardanoApi.StakeAddress ntwrk acct) -> diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index 33bb12192b8..1e738ea4fa4 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -4932,7 +4932,7 @@ fromExternalInput ApiExternalInput fromApiRedeemer :: ApiRedeemer n -> Redeemer fromApiRedeemer = \case ApiRedeemerSpending (ApiBytesT bytes) (ApiT i) -> - RedeemerSpending bytes i + RedeemerSpending bytes (toLedger i) ApiRedeemerMinting (ApiBytesT bytes) (ApiT p) -> RedeemerMinting bytes p ApiRedeemerRewarding (ApiBytesT bytes) r -> diff --git a/lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs b/lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs index f3435aad9b3..122b5cf2109 100644 --- a/lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs +++ b/lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs @@ -741,7 +741,9 @@ spec_balanceTransaction = describe "balanceTransaction" $ do -- With ix 1 instead of 0, making it point to an input which -- doesn't exist in the tx. let faultyRedeemer = - RedeemerSpending (unsafeFromHex "D87A80") (W.TxIn tid 1) + RedeemerSpending + (unsafeFromHex "D87A80") + (Convert.toLedger (W.TxIn tid 1)) let withFaultyRedeemer = over #redeemers $ mapFirst $ const faultyRedeemer @@ -2538,7 +2540,9 @@ pingPong_2 = PartialTx ) ] , redeemers = - [ RedeemerSpending (unsafeFromHex "D87A80") (W.TxIn (W.Hash tid) 0) + [ RedeemerSpending + (unsafeFromHex "D87A80") + (Convert.toLedger (W.TxIn (W.Hash tid) 0)) ] } where From fa651bfba7b06d0de03ffd01e3f6b401fa149b08 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 16 Nov 2023 05:15:26 +0000 Subject: [PATCH 2/5] Use `Write.PolicyId` type in `RedeemerMinting`. --- .../lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs | 8 +++++--- .../api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs index 4a3c84571a8..aae4ac31aea 100644 --- a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs +++ b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs @@ -18,6 +18,7 @@ module Internal.Cardano.Write.Tx.Redeemers ( assignScriptRedeemers , ErrAssignRedeemers (..) , Redeemer (..) + , toPolicyID ) where import Prelude @@ -97,6 +98,7 @@ import GHC.Generics ) import Internal.Cardano.Write.Tx ( PParams + , PolicyId , RecentEra , RecentEraLedgerConstraints , ShelleyLedgerEra @@ -273,7 +275,7 @@ assignScriptRedeemers era pparams timeTranslation utxo redeemers tx = data Redeemer = RedeemerSpending ByteString TxIn - | RedeemerMinting ByteString W.TokenPolicyId + | RedeemerMinting ByteString PolicyId | RedeemerRewarding ByteString CardanoApi.StakeAddress deriving (Eq, Generic, Show) @@ -282,7 +284,7 @@ instance Buildable Redeemer where RedeemerSpending _ input -> "spending(" <> build (show input) <> ")" RedeemerMinting _ pid -> - "minting(" <> build pid <> ")" + "minting(" <> build (show pid) <> ")" RedeemerRewarding _ addr -> "rewarding(" <> build (CardanoApi.serialiseToBech32 addr) <> ")" @@ -297,7 +299,7 @@ toScriptPurpose = \case RedeemerSpending _ txin -> Alonzo.Spending txin RedeemerMinting _ pid -> - Alonzo.Minting (toPolicyID pid) + Alonzo.Minting pid RedeemerRewarding _ (CardanoApi.StakeAddress ntwrk acct) -> Alonzo.Rewarding (Ledger.RewardAcnt ntwrk acct) diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index 1e738ea4fa4..63714a4e20e 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -4934,7 +4934,7 @@ fromApiRedeemer = \case ApiRedeemerSpending (ApiBytesT bytes) (ApiT i) -> RedeemerSpending bytes (toLedger i) ApiRedeemerMinting (ApiBytesT bytes) (ApiT p) -> - RedeemerMinting bytes p + RedeemerMinting bytes (toLedger p) ApiRedeemerRewarding (ApiBytesT bytes) r -> RedeemerRewarding bytes r From 44acb5aca2d72771c3fcf80171dcbbd16310d65b Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 16 Nov 2023 05:42:30 +0000 Subject: [PATCH 3/5] Remove unused function `toPolicyID`. --- .../Internal/Cardano/Write/Tx/Redeemers.hs | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs index aae4ac31aea..55e61c50454 100644 --- a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs +++ b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs @@ -18,14 +18,10 @@ module Internal.Cardano.Write.Tx.Redeemers ( assignScriptRedeemers , ErrAssignRedeemers (..) , Redeemer (..) - , toPolicyID ) where import Prelude -import Cardano.Crypto.Hash.Class - ( Hash - ) import Cardano.Ledger.Alonzo.TxInfo ( TranslationError ) @@ -36,12 +32,8 @@ import Cardano.Ledger.Api , scriptIntegrityHashTxBodyL , witsTxL ) -import Cardano.Ledger.Mary.Value - ( PolicyID (..) - ) import Cardano.Ledger.Shelley.API - ( ScriptHash (..) - , StrictMaybe (..) + ( StrictMaybe (..) ) import Cardano.Slotting.EpochInfo ( EpochInfo @@ -87,9 +79,6 @@ import Data.Map.Strict ( Map , (!) ) -import Data.Maybe - ( fromMaybe - ) import Fmt ( Buildable (..) ) @@ -116,15 +105,12 @@ import Internal.Cardano.Write.Tx.TimeTranslation import qualified Cardano.Api as CardanoApi import qualified Cardano.Api.Shelley as CardanoApi -import qualified Cardano.Crypto.Hash as Crypto import qualified Cardano.Ledger.Alonzo.PlutusScriptApi as Alonzo import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo import qualified Cardano.Ledger.Alonzo.Scripts.Data as Alonzo import qualified Cardano.Ledger.Alonzo.Tx as Alonzo import qualified Cardano.Ledger.Alonzo.TxWits as Alonzo import qualified Cardano.Ledger.Api as Ledger -import qualified Cardano.Wallet.Primitive.Types.Hash as W -import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W import qualified Data.ByteString.Lazy as BL import qualified Data.Map as Map import qualified Data.Map.Merge.Strict as Map @@ -303,15 +289,6 @@ toScriptPurpose = \case RedeemerRewarding _ (CardanoApi.StakeAddress ntwrk acct) -> Alonzo.Rewarding (Ledger.RewardAcnt ntwrk acct) -toPolicyID :: W.TokenPolicyId -> PolicyID StandardCrypto -toPolicyID (W.UnsafeTokenPolicyId (W.Hash bytes)) = - PolicyID (ScriptHash (unsafeHashFromBytes bytes)) - where - unsafeHashFromBytes :: Crypto.HashAlgorithm h => ByteString -> Hash h a - unsafeHashFromBytes = - fromMaybe (error "unsafeHashFromBytes: wrong length") - . Crypto.hashFromBytes - -------------------------------------------------------------------------------- -- Utils -------------------------------------------------------------------------------- From fb224f8c7b9687f2a7e13f19a016b3cc408f14b1 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 16 Nov 2023 05:24:02 +0000 Subject: [PATCH 4/5] Define `Write.RewardAccount` type synonym. --- lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs index 56eb6e24513..1ac761657ff 100644 --- a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs +++ b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs @@ -128,6 +128,9 @@ module Internal.Cardano.Write.Tx , datumHashFromBytes , datumHashToBytes + -- ** Rewards + , RewardAccount + -- ** Script , Script , Alonzo.isPlutusScript @@ -610,6 +613,7 @@ type TxOutInBabbage = Babbage.BabbageTxOut (Babbage.BabbageEra StandardCrypto) type Address = Ledger.Addr StandardCrypto +type RewardAccount = Ledger.RewardAcnt StandardCrypto type Script = AlonzoScript type Value = MaryValue StandardCrypto From aad32e5270280d539147aa56b858c868e772fec1 Mon Sep 17 00:00:00 2001 From: Jonathan Knowles Date: Thu, 16 Nov 2023 05:25:47 +0000 Subject: [PATCH 5/5] Use `Write.RewardAccount` type in `RedeemerRewarding`. --- .../internal/Internal/Cardano/Write/Tx/Redeemers.hs | 12 ++++++------ .../http/Cardano/Wallet/Api/Http/Shelley/Server.hs | 10 ++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs index 55e61c50454..4bb16c11ac3 100644 --- a/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs +++ b/lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs @@ -90,6 +90,7 @@ import Internal.Cardano.Write.Tx , PolicyId , RecentEra , RecentEraLedgerConstraints + , RewardAccount , ShelleyLedgerEra , StandardCrypto , TxIn @@ -103,7 +104,6 @@ import Internal.Cardano.Write.Tx.TimeTranslation , systemStartTime ) -import qualified Cardano.Api as CardanoApi import qualified Cardano.Api.Shelley as CardanoApi import qualified Cardano.Ledger.Alonzo.PlutusScriptApi as Alonzo import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo @@ -262,7 +262,7 @@ assignScriptRedeemers era pparams timeTranslation utxo redeemers tx = data Redeemer = RedeemerSpending ByteString TxIn | RedeemerMinting ByteString PolicyId - | RedeemerRewarding ByteString CardanoApi.StakeAddress + | RedeemerRewarding ByteString RewardAccount deriving (Eq, Generic, Show) instance Buildable Redeemer where @@ -271,8 +271,8 @@ instance Buildable Redeemer where "spending(" <> build (show input) <> ")" RedeemerMinting _ pid -> "minting(" <> build (show pid) <> ")" - RedeemerRewarding _ addr -> - "rewarding(" <> build (CardanoApi.serialiseToBech32 addr) <> ")" + RedeemerRewarding _ acc -> + "rewarding(" <> build (show acc) <> ")" redeemerData :: Redeemer -> ByteString redeemerData = \case @@ -286,8 +286,8 @@ toScriptPurpose = \case Alonzo.Spending txin RedeemerMinting _ pid -> Alonzo.Minting pid - RedeemerRewarding _ (CardanoApi.StakeAddress ntwrk acct) -> - Alonzo.Rewarding (Ledger.RewardAcnt ntwrk acct) + RedeemerRewarding _ acc -> + Alonzo.Rewarding acc -------------------------------------------------------------------------------- -- Utils diff --git a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs index 63714a4e20e..7bd068708db 100644 --- a/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs +++ b/lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs @@ -160,10 +160,16 @@ import Cardano.Api , toNetworkMagic , unNetworkMagic ) +import Cardano.Api.Shelley + ( StakeAddress (..) + ) import Cardano.BM.Tracing ( HasPrivacyAnnotation (..) , HasSeverityAnnotation (..) ) +import Cardano.Ledger.Address + ( RewardAcnt (..) + ) import Cardano.Mnemonic ( SomeMnemonic ) @@ -4935,8 +4941,8 @@ fromApiRedeemer = \case RedeemerSpending bytes (toLedger i) ApiRedeemerMinting (ApiBytesT bytes) (ApiT p) -> RedeemerMinting bytes (toLedger p) - ApiRedeemerRewarding (ApiBytesT bytes) r -> - RedeemerRewarding bytes r + ApiRedeemerRewarding (ApiBytesT bytes) (StakeAddress x y) -> + RedeemerRewarding bytes (RewardAcnt x y) {------------------------------------------------------------------------------- Api Layer