From 339f3358e632ce7eae6aa709b5658bf15615a9c9 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Thu, 29 Aug 2024 18:31:04 +0200 Subject: [PATCH 1/3] Adapt to latest `cardano-wallet-read` --- cabal.project | 4 ++-- lib/unit/test/unit/Cardano/Wallet/DB/StateMachine.hs | 2 +- lib/wallet/src/Cardano/Wallet/DB/Layer.hs | 2 +- lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cabal.project b/cabal.project index e4a1c721da6..56209c59676 100644 --- a/cabal.project +++ b/cabal.project @@ -157,8 +157,8 @@ source-repository-package source-repository-package type: git location: https://github.com/cardano-foundation/cardano-wallet-agda - tag: 1feea08e8c387cf23deee780e0de52b24edc004c - --sha256: 1zsx0191pwaw30017jrxll1ldfvp4pk9jkw0fb9pd4235kpmslkm + tag: 525eb93be15a8c1ea6198e472f401ea0f9a985cd + --sha256: 1mw98kdqhafdq89ry3yxwd1xwjd7g69fs5pd1p2np8sdrpxh3n8g subdir: lib/customer-deposit-wallet-pure lib/cardano-wallet-read diff --git a/lib/unit/test/unit/Cardano/Wallet/DB/StateMachine.hs b/lib/unit/test/unit/Cardano/Wallet/DB/StateMachine.hs index acbe9613366..d0a507d5d66 100644 --- a/lib/unit/test/unit/Cardano/Wallet/DB/StateMachine.hs +++ b/lib/unit/test/unit/Cardano/Wallet/DB/StateMachine.hs @@ -219,7 +219,7 @@ import Cardano.Wallet.Primitive.Types.Tx.TxOut import Cardano.Wallet.Primitive.Types.UTxO ( UTxO (..) ) -import Cardano.Wallet.Read.Eras.EraValue +import Cardano.Wallet.Read.Eras ( eraValueSerialize ) import Cardano.Wallet.Read.Tx.CBOR diff --git a/lib/wallet/src/Cardano/Wallet/DB/Layer.hs b/lib/wallet/src/Cardano/Wallet/DB/Layer.hs index d51671b9f7c..88205baf190 100644 --- a/lib/wallet/src/Cardano/Wallet/DB/Layer.hs +++ b/lib/wallet/src/Cardano/Wallet/DB/Layer.hs @@ -184,7 +184,7 @@ import Cardano.Wallet.Primitive.Slotting import Cardano.Wallet.Primitive.Types.Coin ( Coin (..) ) -import Cardano.Wallet.Read.Eras.EraValue +import Cardano.Wallet.Read.Eras ( EraValue ) import Cardano.Wallet.Read.Tx.CBOR diff --git a/lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs b/lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs index 2d7cd1f6090..abbf626b06b 100644 --- a/lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs +++ b/lib/wallet/src/Cardano/Wallet/DB/Store/Transactions/Model.hs @@ -68,7 +68,7 @@ import Cardano.Wallet.Primitive.Types.TokenQuantity import Cardano.Wallet.Primitive.Types.Tx.Tx ( Tx (txCBOR) ) -import Cardano.Wallet.Read.Eras.EraValue +import Cardano.Wallet.Read.Eras ( eraValueSerialize ) import Cardano.Wallet.Read.Tx.CBOR From a9312d86a7807d0880317ff9ea2658a595bdd144 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Thu, 29 Aug 2024 00:08:55 +0200 Subject: [PATCH 2/3] Remove `Addr` type synonym --- .../src/Cardano/Wallet/Deposit/Pure/Balance.hs | 4 ++-- .../src/Cardano/Wallet/Deposit/Read.hs | 7 +------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs index 9eae9761b43..3c33104d54f 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs @@ -59,7 +59,7 @@ type IsOurs addr = addr -> Bool -- -- Returns both a delta and the new value. applyBlock - :: IsOurs Read.Addr -> Read.Block -> UTxO -> (DeltaUTxO, UTxO) + :: IsOurs Read.Address -> Read.Block -> UTxO -> (DeltaUTxO, UTxO) applyBlock isOurs block u0 = (mconcat $ reverse dus, u1) where @@ -72,7 +72,7 @@ applyBlock isOurs block u0 = -- -- Returns both a delta and the new value. applyTx - :: IsOurs Read.Addr -> Read.Tx -> UTxO -> (DeltaUTxO, UTxO) + :: IsOurs Read.Address -> Read.Tx -> UTxO -> (DeltaUTxO, UTxO) applyTx isOurs tx u0 = if isUnchangedUTxO then (mempty, u0) diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs index ada0026324f..0885949cbc4 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs @@ -10,7 +10,6 @@ module Cardano.Wallet.Deposit.Read , Slot , ChainPoint (..) - , Addr , Address , fromRawAddress , toRawAddress @@ -93,14 +92,10 @@ data ChainPoint | At Slot deriving (Eq, Ord, Show) --- newtype Addr = Addr { getAddressBytes :: ByteString } --- deriving (Eq, Show) -type Addr = W.Address - -- | Synonym for readability. -- The ledger specifications define @Addr@. -- Byron addresses are represented by @Addr_bootstrap@. -type Address = Addr +type Address = W.Address fromRawAddress :: Read.CompactAddr -> Address fromRawAddress = W.Address . SBS.fromShort . Read.toShortByteString From 5de63d8880788073f88b77f0a67acc75e55a0188 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Thu, 29 Aug 2024 00:36:47 +0200 Subject: [PATCH 3/3] Change Deposit Wallet to use `Cardano.Wallet.Read` --- .../customer-deposit-wallet.cabal | 4 +- .../Cardano/Wallet/Deposit/HTTP/Types/JSON.hs | 38 ++++++++++- .../Deposit/HTTP/Types/JSON/Encoding.hs | 2 +- .../src/Cardano/Wallet/Deposit/Pure.hs | 28 ++++---- .../Cardano/Wallet/Deposit/Pure/Balance.hs | 26 +++---- .../src/Cardano/Wallet/Deposit/Pure/UTxO.hs | 17 ++++- .../Wallet/Deposit/Pure/UTxOHistory.hs | 24 ++++++- .../src/Cardano/Wallet/Deposit/Read.hs | 67 ++++++------------- .../src/Cardano/Wallet/Deposit/Write.hs | 42 ++++++++---- .../test/scenario/Test/Scenario/Blockchain.hs | 3 +- 10 files changed, 152 insertions(+), 99 deletions(-) diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index f14c83fb23f..58a304c0474 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -53,7 +53,6 @@ library , cardano-crypto , cardano-wallet:cardano-wallet , cardano-wallet-network-layer - , cardano-wallet-primitive , cardano-wallet-read == 0.2024.8.27 , cardano-ledger-byron , containers @@ -117,6 +116,7 @@ library customer-deposit-wallet-http , aeson-pretty , base , bytestring + , cardano-wallet-read , customer-deposit-wallet , http-media , insert-ordered-containers @@ -148,8 +148,6 @@ test-suite unit , base , bytestring , cardano-crypto - , cardano-wallet:cardano-wallet - , cardano-wallet-primitive , cardano-wallet-test-utils , customer-deposit-wallet:{customer-deposit-wallet, customer-deposit-wallet-http} , directory diff --git a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON.hs b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON.hs index 7452ebd108b..9dd1bb5940b 100644 --- a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON.hs +++ b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON.hs @@ -48,6 +48,15 @@ import Data.Aeson.Types import Data.Bifunctor ( first ) +import Data.ByteArray.Encoding + ( Base (Base16) + , convertFromBase + , convertToBase + ) +import Data.ByteString.Short + ( fromShort + , toShort + ) import Data.OpenApi ( NamedSchema (..) , ToSchema (..) @@ -57,13 +66,17 @@ import Data.Text ) import Data.Text.Class ( FromText (..) + , TextDecodingError (..) + , ToText (..) , getTextDecodingError ) import Servant ( FromHttpApiData (..) ) +import qualified Cardano.Wallet.Read as Read import qualified Data.Text as T +import qualified Data.Text.Encoding as T {----------------------------------------------------------------------------- Additional type definitions @@ -83,8 +96,29 @@ newtype ApiT a = ApiT {unApiT :: a} ------------------------------------------------------------------------------} -- Address -deriving via ViaText Address instance FromJSON (ApiT Address) -deriving via ViaText Address instance ToJSON (ApiT Address) +instance ToText (ApiT Address) where + toText = T.decodeUtf8 + . convertToBase Base16 + . fromShort + . Read.toShortByteString + . unApiT + +instance FromText (ApiT Address) where + fromText t = do + bytes <- + first textDecodingError + . convertFromBase Base16 + $ T.encodeUtf8 t + maybe (Left errInvalidAddress) (Right . ApiT) + . Read.fromShortByteString + $ toShort bytes + where + errInvalidAddress = TextDecodingError $ "Invalid address: " <> show t + textDecodingError = TextDecodingError . show + +-- FIXME: Bech32 encodings +deriving via ViaText (ApiT Address) instance FromJSON (ApiT Address) +deriving via ViaText (ApiT Address) instance ToJSON (ApiT Address) instance ToSchema (ApiT Address) where declareNamedSchema _ = do diff --git a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON/Encoding.hs b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON/Encoding.hs index 754bd272336..2100be31f2e 100644 --- a/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON/Encoding.hs +++ b/lib/customer-deposit-wallet/http/Cardano/Wallet/Deposit/HTTP/Types/JSON/Encoding.hs @@ -10,8 +10,8 @@ -- module Cardano.Wallet.Deposit.HTTP.Types.JSON.Encoding ( Custom (..) - , ViaText (..) , customOptions + , ViaText (..) ) where import Prelude diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs index e14504598ff..7ee0bc74480 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs @@ -56,9 +56,6 @@ import Cardano.Wallet.Deposit.Pure.UTxOHistory import Cardano.Wallet.Deposit.Read ( Address ) -import Data.Bifunctor - ( second - ) import Data.Foldable ( foldl' ) @@ -86,6 +83,7 @@ import qualified Cardano.Wallet.Deposit.Pure.UTxOHistory as UTxOHistory import qualified Cardano.Wallet.Deposit.Read as Read import qualified Cardano.Wallet.Deposit.Write as Write import qualified Data.Delta as Delta +import qualified Data.Set as Set {----------------------------------------------------------------------------- Types @@ -110,20 +108,18 @@ type DeltaWalletState = Delta.Replace WalletState listCustomers :: WalletState -> [(Customer, Address)] listCustomers = - map (second Read.fromRawAddress) - . Address.listCustomers . addresses + Address.listCustomers . addresses createAddress :: Customer -> WalletState -> (Address, WalletState) createAddress customer w0 = - (Read.fromRawAddress address, w0{addresses = s1}) + (address, w0{addresses = s1}) where (address, s1) = Address.createAddress customer (addresses w0) -- depend on the private key only, not on the entire wallet state deriveAddress :: WalletState -> (Customer -> Address) deriveAddress w = - Read.fromRawAddress - . Address.deriveAddress (Address.getXPub (addresses w)) + Address.deriveAddress (Address.getXPub (addresses w)) . Address.DerivationCustomer -- FIXME: More performant with a double index. @@ -132,11 +128,11 @@ knownCustomer c = (c `elem`) . map fst . listCustomers knownCustomerAddress :: Address -> WalletState -> Bool knownCustomerAddress address = - Address.knownCustomerAddress (Read.toRawAddress address) . addresses + Address.knownCustomerAddress address . addresses isCustomerAddress :: Address -> WalletState -> Bool isCustomerAddress address = - flip Address.isCustomerAddress (Read.toRawAddress address) . addresses + flip Address.isCustomerAddress address . addresses fromRawCustomer :: Word31 -> Customer fromRawCustomer = id @@ -172,12 +168,12 @@ rollForwardOne block w = } where isOurs :: Address -> Bool - isOurs = Address.isOurs (addresses w) . Read.toRawAddress + isOurs = Address.isOurs (addresses w) rollForwardUTxO :: (Address -> Bool) -> Read.Block -> UTxOHistory -> UTxOHistory rollForwardUTxO isOurs block u = - Delta.apply (UTxOHistory.AppendBlock slot deltaUTxO) u + UTxOHistory.appendBlock slot deltaUTxO u where (deltaUTxO,_) = Balance.applyBlock isOurs block (UTxOHistory.getUTxO u) slot = Read.slot . Read.blockHeaderBody $ Read.blockHeader block @@ -203,13 +199,13 @@ data TxSummary = TxSummary , blockHeaderBody :: Read.BHBody , transfer :: ValueTransfer } - deriving (Eq, Ord, Show) + deriving (Eq, Show) data ValueTransfer = ValueTransfer { spent :: Read.Value , received :: Read.Value } - deriving (Eq, Ord, Show) + deriving (Eq, Show) getCustomerHistory :: Customer -> WalletState -> [TxSummary] getCustomerHistory = undefined @@ -246,7 +242,7 @@ getBIP32PathsForOwnedInputs txbody w = getBIP32Paths :: WalletState -> [Read.Address] -> [BIP32Path] getBIP32Paths w = - mapMaybe $ Address.getBIP32Path (addresses w) . Read.toRawAddress + mapMaybe $ Address.getBIP32Path (addresses w) signTxBody :: Write.TxBody -> WalletState -> Maybe Write.Tx signTxBody _txbody _w = undefined @@ -256,4 +252,4 @@ addTxSubmission _tx _w = undefined listTxsInSubmission :: WalletState -> Set Write.Tx -- listTxsInSubmission = Sbm.listInSubmission . submissions -listTxsInSubmission _ = mempty +listTxsInSubmission _ = Set.empty diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs index 3c33104d54f..2b6318c98f6 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/Balance.hs @@ -14,12 +14,6 @@ import Cardano.Wallet.Deposit.Pure.UTxO , balance , excluding ) -import Cardano.Wallet.Primitive.Ledger.Read.Tx - ( primitiveTx - ) -import Cardano.Wallet.Primitive.Model - ( utxoFromTx - ) import Data.Foldable ( foldMap' ) @@ -27,10 +21,11 @@ import Data.Set ( Set ) -import qualified Cardano.Wallet.Deposit.Pure.UTxO as UTxO +import qualified Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO as DeltaUTxO +import qualified Cardano.Wallet.Deposit.Pure.UTxO.UTxO as UTxO import qualified Cardano.Wallet.Deposit.Read as Read import qualified Cardano.Wallet.Deposit.Write as Write -import qualified Data.Set as Set +import qualified Cardano.Wallet.Read.Tx as Tx {----------------------------------------------------------------------------- Wallet Balance @@ -65,7 +60,6 @@ applyBlock isOurs block u0 = where (dus, u1) = mapAccumL' (applyTx isOurs) u0 - . map primitiveTx $ Read.transactions block -- | Apply a transactions to the 'UTxO'. @@ -81,8 +75,8 @@ applyTx isOurs tx u0 = (du, u) = (du21 <> du10, u2) (du10, u1) = spendTxD tx u0 - receivedUTxO = UTxO.filterByAddress isOurs (utxoFromTx tx) - (du21, u2) = UTxO.receiveD u1 receivedUTxO + receivedUTxO = UTxO.filterByAddress isOurs (Read.utxoFromEraTx tx) + (du21, u2) = DeltaUTxO.receiveD u1 receivedUTxO -- NOTE: Performance. -- 'applyTx' is part of a tight loop that inspects all transactions @@ -90,7 +84,7 @@ applyTx isOurs tx u0 = -- Thus, we make a small performance optimization here. -- Specifically, we want to reject a transaction as soon as possible -- if it does not change the 'UTxO' set. The test - isUnchangedUTxO = UTxO.null receivedUTxO && mempty == du10 + isUnchangedUTxO = UTxO.null receivedUTxO && DeltaUTxO.null du10 -- allocates slightly fewer new Set/Map than the definition -- isUnchangedUTxO = mempty == du @@ -100,12 +94,12 @@ applyTx isOurs tx u0 = -- | Remove unspent outputs that are consumed by the given transaction. spendTxD :: Read.Tx -> UTxO -> (DeltaUTxO, UTxO) spendTxD tx !u = - u `UTxO.excludingD` Set.fromList inputsToExclude + u `DeltaUTxO.excludingD` inputsToExclude where inputsToExclude = - if Read.txScriptInvalid tx - then Read.collateralInputs tx - else Read.inputs tx + case Tx.getScriptValidity tx of + Tx.IsValid True -> Tx.getInputs tx + Tx.IsValid False -> Tx.getCollateralInputs tx {----------------------------------------------------------------------------- Helpers diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxO.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxO.hs index f7807561073..494643d9ce9 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxO.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxO.hs @@ -12,4 +12,19 @@ module Cardano.Wallet.Deposit.Pure.UTxO , null ) where -import Cardano.Wallet.Primitive.Types.UTxO +import Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO + ( DeltaUTxO + , excludingD + , null + , receiveD + ) +import Cardano.Wallet.Deposit.Pure.UTxO.UTxO + ( UTxO + , balance + , excluding + , filterByAddress + , restrictedBy + ) +import Data.Map.Strict + ( toList + ) diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxOHistory.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxOHistory.hs index 9df44a4994e..6c78b078ad3 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxOHistory.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure/UTxOHistory.hs @@ -1,9 +1,31 @@ module Cardano.Wallet.Deposit.Pure.UTxOHistory ( UTxOHistory , empty + , appendBlock , DeltaUTxOHistory (..) , getUTxO ) where -import Cardano.Wallet.DB.Store.UTxOHistory.Model +import Cardano.Wallet.Deposit.Pure.UTxO.DeltaUTxO + ( DeltaUTxO + ) +import Cardano.Wallet.Deposit.Pure.UTxO.UTxOHistory + ( UTxOHistory + , appendBlock + , empty + , getUTxO + ) +import Cardano.Wallet.Deposit.Read + ( Slot + , SlotNo + ) + +-- | Changes to the UTxO history. +data DeltaUTxOHistory + = -- | New slot tip, changes within that block. + AppendBlock SlotNo DeltaUTxO + | -- | Rollback tip. + Rollback Slot + | -- | Move finality forward. + Prune SlotNo diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs index 0885949cbc4..446d59a84a5 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Read.hs @@ -8,29 +8,24 @@ module Cardano.Wallet.Deposit.Read ( Network (..) , Slot + , Read.SlotNo , ChainPoint (..) , Address - , fromRawAddress - , toRawAddress , KeyHash , mkEnterpriseAddress , mockAddress , Ix - , TxIn - , TxOut + , Read.TxIn + , Read.TxOut , address - , Value + , Read.Value , UTxO - , TxId + , Read.TxId , Tx - , W.txScriptInvalid - , W.collateralInputs - , W.inputs - , W.outputs - , W.collateralOutput + , Read.utxoFromEraTx , TxBody , TxWitness @@ -50,9 +45,15 @@ module Cardano.Wallet.Deposit.Read import Prelude +import Cardano.Wallet.Read.Tx + ( TxIx + ) import Data.ByteString ( ByteString ) +import Data.Map + ( Map + ) import Data.Maybe ( fromJust ) @@ -64,14 +65,6 @@ import Numeric.Natural ) import qualified Cardano.Chain.Genesis as Byron -import qualified Cardano.Wallet.Primitive.Types as W -import qualified Cardano.Wallet.Primitive.Types.Address as W -import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W -import qualified Cardano.Wallet.Primitive.Types.Tx as W -import qualified Cardano.Wallet.Primitive.Types.Tx.TxIn as W -import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as TxOut -import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as W -import qualified Cardano.Wallet.Primitive.Types.UTxO as W import qualified Cardano.Wallet.Read as Read import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as B8 @@ -85,7 +78,7 @@ import qualified Data.ByteString.Short as SBS data Network = Testnet | Mainnet -- Spec: type Slot = Natural -type Slot = W.SlotNo +type Slot = Read.SlotNo data ChainPoint = Origin @@ -95,14 +88,7 @@ data ChainPoint -- | Synonym for readability. -- The ledger specifications define @Addr@. -- Byron addresses are represented by @Addr_bootstrap@. -type Address = W.Address - -fromRawAddress :: Read.CompactAddr -> Address -fromRawAddress = W.Address . SBS.fromShort . Read.toShortByteString - -toRawAddress :: Address -> Read.CompactAddr -toRawAddress (W.Address a) = - fromJust . Read.fromShortByteString $ SBS.toShort a +type Address = Read.CompactAddr mockAddress :: Show a => a -> Address mockAddress = mkEnterpriseAddress . B8.pack . show @@ -112,7 +98,7 @@ type KeyHash = ByteString mkEnterpriseAddress :: KeyHash -> Address mkEnterpriseAddress keyHash = - W.Address . BS.pack + fromJust . Read.fromShortByteString . SBS.pack $ [tagEnterprise] <> take 28 (BS.unpack keyHash <> repeat 0) tagEnterprise :: Word8 @@ -121,25 +107,14 @@ tagEnterprise = 0b01100001 dummyAddress :: Address dummyAddress = mockAddress (0 :: Int) -type Ix = Natural - --- type TxIn = (TxId, Ix) -type TxIn = W.TxIn - --- type TxOut = (Addr, Value) -type TxOut = W.TxOut - -address :: TxOut -> Address -address = TxOut.address - -type Value = W.TokenBundle +type Ix = TxIx --- type UTxO = Map TxIn TxOut -type UTxO = W.UTxO +address :: Read.TxOut -> Address +address = Read.getCompactAddr -type TxId = ByteString +type UTxO = Map Read.TxIn Read.TxOut -type Tx = W.Tx +type Tx = Read.Tx Read.Conway type TxBody = () @@ -183,7 +158,7 @@ dummyBHBody :: BHBody dummyBHBody = BHBody { prev = Nothing , blockno = 128 - , slot = 42 + , slot = Read.SlotNo 42 , bhash = () } diff --git a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write.hs b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write.hs index 8162d14dc18..e24d3435af8 100644 --- a/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write.hs +++ b/lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write.hs @@ -26,6 +26,9 @@ module Cardano.Wallet.Deposit.Write import Prelude +import Cardano.Read.Ledger.Tx.Output + ( Output (..) + ) import Cardano.Wallet.Deposit.Read ( Address , Ix @@ -35,13 +38,19 @@ import Cardano.Wallet.Deposit.Read , TxWitness , Value ) -import Cardano.Wallet.Primitive.Ledger.Convert - ( toConwayTxOut - , toLedger +import Cardano.Wallet.Read.Hash + ( hashFromBytesShort + ) +import Cardano.Wallet.Read.Tx + ( toConwayOutput + , txIdFromHash ) import Data.Map ( Map ) +import Data.Maybe + ( fromJust + ) import Data.Maybe.Strict ( StrictMaybe , maybeToStrictMaybe @@ -60,11 +69,10 @@ import Lens.Micro import qualified Cardano.Ledger.Api as L import qualified Cardano.Ledger.Api.Tx.In as L -import qualified Cardano.Wallet.Primitive.Types.Coin as W -import qualified Cardano.Wallet.Primitive.Types.TokenBundle as W -import qualified Cardano.Wallet.Primitive.Types.Tx.TxOut as W import qualified Cardano.Wallet.Read as Read +import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as B8 +import qualified Data.ByteString.Short as SBS import qualified Data.Map.Strict as Map import qualified Data.Set as Set @@ -76,7 +84,7 @@ data Tx = Tx { txbody :: TxBody , txwits :: TxWitness } - deriving (Eq, Ord, Show) + deriving (Show) data TxBody = TxBody { spendInputs :: Set TxIn @@ -84,13 +92,13 @@ data TxBody = TxBody , txouts :: Map Ix TxOut , collRet :: Maybe TxOut } - deriving (Eq, Ord, Show) + deriving (Show) mkAda :: Integer -> Value -mkAda = W.fromCoin . W.unsafeFromIntegral +mkAda = Read.injectCoin . Read.CoinC mkTxOut :: Address -> Value -> TxOut -mkTxOut = W.TxOut +mkTxOut = Read.mkBasicTxOut toConwayTx :: TxId -> Tx -> Read.Tx Read.Conway toConwayTx _ Tx{txbody} = Read.Tx $ L.mkBasicTx txBody @@ -108,7 +116,7 @@ toConwayTx _ Tx{txbody} = Read.Tx $ L.mkBasicTx txBody ) toLedgerTxIn :: TxIn -> L.TxIn L.StandardCrypto -toLedgerTxIn = toLedger +toLedgerTxIn = id toLedgerTxOuts :: Map Ix TxOut -> StrictSeq (L.TxOut L.Conway) toLedgerTxOuts = fromList . map (toConwayTxOut . snd) . Map.toAscList @@ -116,5 +124,15 @@ toLedgerTxOuts = fromList . map (toConwayTxOut . snd) . Map.toAscList toLedgerMaybeTxOut :: Maybe TxOut -> StrictMaybe (L.TxOut L.Conway) toLedgerMaybeTxOut = fmap toConwayTxOut . maybeToStrictMaybe +toConwayTxOut :: TxOut -> L.TxOut L.Conway +toConwayTxOut txout = + case toConwayOutput txout of + Output o -> o + mockTxId :: Show a => a -> TxId -mockTxId = B8.pack . show +mockTxId x = + txIdFromHash + . fromJust + . hashFromBytesShort + . SBS.pack + $ take 32 (BS.unpack (B8.pack $ show x) <> repeat 0) diff --git a/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs b/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs index 9b9cc75834b..2b215fe0189 100644 --- a/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs +++ b/lib/customer-deposit-wallet/test/scenario/Test/Scenario/Blockchain.hs @@ -116,7 +116,8 @@ payFromFaucet env destinations = txBody = Write.TxBody { Write.spendInputs = mempty , Write.collInputs = mempty - , Write.txouts = Map.fromList $ zip [0..] $ map toTxOut destinations + , Write.txouts = + Map.fromList $ zip [toEnum 0..] $ map toTxOut destinations , Write.collRet = Nothing } tx = signTx (xprv (faucet env)) [] txBody