Skip to content

Commit

Permalink
[ADP-3479] Fix private key creation (#4863)
Browse files Browse the repository at this point in the history
- Expose wallet state functions from the REST interface, useful in the
the next PR and somehow happened here
- Use mnemonic management from cardano-addresses
- Add a golden test that we can create the same credentials ads
`cardano-addresses` cli tool
  • Loading branch information
paolino authored Dec 6, 2024
2 parents 044c4ab + 925cd15 commit 3679fbd
Show file tree
Hide file tree
Showing 18 changed files with 671 additions and 121 deletions.
21 changes: 14 additions & 7 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ library
build-depends:
, async
, base
, base58-bytestring
, base16-bytestring
, base58-bytestring
, bech32
, bech32-th
, bytestring
Expand All @@ -69,16 +69,16 @@ library
, cardano-wallet
, cardano-wallet-network-layer
, cardano-wallet-primitive
, cardano-wallet-read ==0.2024.8.27
, cardano-wallet-read
, containers
, contra-tracer
, customer-deposit-wallet-pure
, delta-store
, delta-types
, digest
, fingertree
, io-classes
, int-cast
, io-classes
, lens
, MonadRandom
, monoidal-containers
Expand All @@ -104,8 +104,8 @@ library
Cardano.Wallet.Deposit.Pure.API.TxHistory
Cardano.Wallet.Deposit.Pure.Balance
Cardano.Wallet.Deposit.Pure.State.Creation
Cardano.Wallet.Deposit.Pure.State.Payment.Inspect
Cardano.Wallet.Deposit.Pure.State.Payment
Cardano.Wallet.Deposit.Pure.State.Payment.Inspect
Cardano.Wallet.Deposit.Pure.State.Rolling
Cardano.Wallet.Deposit.Pure.State.Signing
Cardano.Wallet.Deposit.Pure.State.Submissions
Expand Down Expand Up @@ -214,34 +214,40 @@ test-suite unit
, aeson
, aeson-pretty
, base
, base58-bytestring
, base16-bytestring
, bech32
, bech32-th
, base58-bytestring
, bytestring
, cardano-addresses
, address-derivation-discovery
, cardano-crypto
, cardano-crypto-class
, cardano-ledger-api
, cardano-ledger-core
, cardano-ledger-core:testlib
, cardano-ledger-shelley
, cardano-slotting
, cardano-wallet-read
, cardano-wallet-test-utils
, containers
, contra-tracer
, customer-deposit-wallet
, customer-deposit-wallet-pure
, customer-deposit-wallet:http
, customer-deposit-wallet:rest
, customer-deposit-wallet-pure
, data-default
, directory
, hspec
, hspec-golden
, lens
, openapi3
, pretty-simple
, QuickCheck
, serialise
, temporary
, text
, time
, text
, transformers
, with-utf8

Expand All @@ -251,6 +257,7 @@ test-suite unit
Cardano.Wallet.Deposit.HTTP.OpenAPISpec
Cardano.Wallet.Deposit.Map.TimedSpec
Cardano.Wallet.Deposit.Pure.API.AddressSpec
Cardano.Wallet.Deposit.Pure.API.TransactionSpec
Cardano.Wallet.Deposit.PureSpec
Cardano.Wallet.Deposit.RESTSpec
Cardano.Wallet.Deposit.Write.KeysSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import Cardano.Wallet.Deposit.IO
( WalletBootEnv
)
import Cardano.Wallet.Deposit.Pure.State.Creation
( credentialsFromEncodedXPub
( createMnemonicFromWords
, credentialsFromEncodedXPub
, credentialsFromMnemonics
)
import Cardano.Wallet.Deposit.REST
Expand Down Expand Up @@ -90,17 +91,20 @@ createWalletViaMnemonic
dir
boot
resource
(PostWalletViaMnemonic mnemonics' passphrase' users') =
onlyOnWalletIntance resource initWallet $> NoContent
where
initWallet :: WalletResourceM ()
initWallet =
REST.initWallet
tracer
boot
dir
(credentialsFromMnemonics mnemonics' passphrase')
(fromIntegral users')
(PostWalletViaMnemonic mnemonics' passphrase' users') = do
case createMnemonicFromWords mnemonics' of
Left e -> fail $ show e
Right someMnemonic -> do
let
initWallet :: WalletResourceM ()
initWallet =
REST.initWallet
tracer
boot
dir
(credentialsFromMnemonics someMnemonic passphrase')
(fromIntegral users')
onlyOnWalletIntance resource initWallet $> NoContent

createWalletViaXPub
:: Tracer IO String
Expand Down
36 changes: 31 additions & 5 deletions lib/customer-deposit-wallet/rest/Cardano/Wallet/Deposit/REST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ module Cardano.Wallet.Deposit.REST
, walletPublicIdentity
, deleteWallet
, deleteTheDepositWalletOnDisk
-- * Internals
, onWalletInstance

-- * Internals
, inspectTx
, onWalletInstance
, networkTag
, resolveCurrentEraTx
, canSign
, submitTx
) where

import Prelude
Expand Down Expand Up @@ -81,9 +86,12 @@ import Cardano.Wallet.Deposit.IO.Resource
, ErrResourceMissing (..)
)
import Cardano.Wallet.Deposit.Pure
( Credentials
( CanSign
, Credentials
, CurrentEraResolvedTx
, Customer
, ErrCreatePayment
, InspectTx
, Passphrase
, Word31
, fromCredentialsAndGenesis
Expand Down Expand Up @@ -152,6 +160,7 @@ import System.FilePath
)

import qualified Cardano.Wallet.Deposit.IO as WalletIO
import qualified Cardano.Wallet.Deposit.IO.Network.Type as Network
import qualified Cardano.Wallet.Deposit.IO.Resource as Resource
import qualified Cardano.Wallet.Deposit.Read as Read
import qualified Cardano.Wallet.Deposit.Write as Write
Expand Down Expand Up @@ -287,7 +296,7 @@ instance Serialise XPrv where
encode = encode . unXPrv
decode = do
b :: ByteString <- decode
case xprv b of
case xprv b of
Right x -> pure x
Left e -> fail e

Expand Down Expand Up @@ -438,14 +447,17 @@ getTxHistoryByTime
:: WalletResourceM ByTime
getTxHistoryByTime = onWalletInstance WalletIO.getTxHistoryByTime

networkTag :: WalletResourceM Read.NetworkTag
networkTag = onWalletInstance WalletIO.networkTag

{-----------------------------------------------------------------------------
Operations
Writing to blockchain
------------------------------------------------------------------------------}

createPayment
:: [(Address, Read.Value)]
-> WalletResourceM (Either ErrCreatePayment Write.Tx)
-> WalletResourceM (Either ErrCreatePayment CurrentEraResolvedTx)
createPayment = onWalletInstance . WalletIO.createPayment

getBIP32PathsForOwnedInputs
Expand All @@ -454,8 +466,22 @@ getBIP32PathsForOwnedInputs
getBIP32PathsForOwnedInputs =
onWalletInstance . WalletIO.getBIP32PathsForOwnedInputs

canSign :: WalletResourceM CanSign
canSign = onWalletInstance WalletIO.canSign

signTx
:: Write.Tx
-> Passphrase
-> WalletResourceM (Maybe Write.Tx)
signTx tx = onWalletInstance . WalletIO.signTx tx

inspectTx
:: CurrentEraResolvedTx
-> WalletResourceM InspectTx
inspectTx = onWalletInstance . WalletIO.inspectTx

resolveCurrentEraTx :: Write.Tx -> WalletResourceM CurrentEraResolvedTx
resolveCurrentEraTx = onWalletInstance . WalletIO.resolveCurrentEraTx

submitTx :: Write.Tx -> WalletResourceM (Either Network.ErrPostTx ())
submitTx = onWalletInstance . WalletIO.submitTx
36 changes: 33 additions & 3 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Cardano.Wallet.Deposit.IO

-- *** Create transactions
, createPayment
, inspectTx

-- *** Sign transactions
, getBIP32PathsForOwnedInputs
Expand All @@ -45,8 +46,12 @@ module Cardano.Wallet.Deposit.IO
, submitTx
, listTxsInSubmission

-- * Internals
, onWalletState
-- * Internals
, onWalletState
, networkTag
, readWalletState
, resolveCurrentEraTx
, canSign
) where

import Prelude
Expand All @@ -59,6 +64,7 @@ import Cardano.Wallet.Deposit.IO.Network.Type
)
import Cardano.Wallet.Deposit.Pure
( Credentials
, CurrentEraResolvedTx
, Customer
, ValueTransfer
, WalletPublicIdentity (..)
Expand All @@ -70,6 +76,9 @@ import Cardano.Wallet.Deposit.Pure.API.TxHistory
, ByTime
, LookupTimeFromSlot
)
import Cardano.Wallet.Deposit.Pure.State.Creation
( CanSign
)
import Cardano.Wallet.Deposit.Read
( Address
, TxId
Expand Down Expand Up @@ -315,6 +324,10 @@ slotResolver w = do
$ bootEnv
$ env w

networkTag :: WalletInstance -> IO Read.NetworkTag
networkTag w = do
Wallet.networkTag <$> readWalletState w

{-----------------------------------------------------------------------------
Operations
Constructing transactions
Expand All @@ -323,7 +336,7 @@ slotResolver w = do
createPayment
:: [(Address, Read.Value)]
-> WalletInstance
-> IO (Either Wallet.ErrCreatePayment Write.Tx)
-> IO (Either Wallet.ErrCreatePayment CurrentEraResolvedTx)
createPayment a w = do
timeTranslation <- Network.getTimeTranslation network
pparams <-
Expand All @@ -332,11 +345,28 @@ createPayment a w = do
where
network = networkEnv $ bootEnv $ env w

inspectTx
:: CurrentEraResolvedTx
-> WalletInstance
-> IO Wallet.InspectTx
inspectTx tx w = flip Wallet.inspectTx tx <$> readWalletState w

resolveCurrentEraTx
:: Write.Tx
-> WalletInstance
-> IO CurrentEraResolvedTx
resolveCurrentEraTx tx w =
Wallet.resolveCurrentEraTx tx <$> readWalletState w

{-----------------------------------------------------------------------------
Operations
Signing transactions
------------------------------------------------------------------------------}

canSign :: WalletInstance -> IO CanSign
canSign w = do
Wallet.canSign <$> readWalletState w

getBIP32PathsForOwnedInputs
:: Write.Tx -> WalletInstance -> IO [BIP32Path]
getBIP32PathsForOwnedInputs a w =
Expand Down
24 changes: 22 additions & 2 deletions lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,47 @@ module Cardano.Wallet.Deposit.Pure
, getEraSlotOfBlock
, getCustomerDeposits
, getAllDeposits
, networkTag

-- ** Writing to the blockchain
, ErrCreatePayment (..)
, createPayment
, resolveCurrentEraTx
, CurrentEraResolvedTx
, BIP32Path (..)
, DerivationType (..)
, ResolvedTx (..)
, canSign
, CanSign (..)
, getBIP32PathsForOwnedInputs
, Passphrase
, signTx
, addTxSubmission
, listTxsInSubmission
, inspectTx
, InspectTx (..)
) where

import Cardano.Wallet.Address.BIP32
( BIP32Path (..)
, DerivationType (..)
)
import Cardano.Wallet.Deposit.Pure.State.Creation
( Credentials (..)
( CanSign (..)
, Credentials (..)
, WalletPublicIdentity (..)
, canSign
, fromCredentialsAndGenesis
)
import Cardano.Wallet.Deposit.Pure.State.Payment
( ErrCreatePayment (..)
( CurrentEraResolvedTx
, ErrCreatePayment (..)
, createPayment
, resolveCurrentEraTx
)
import Cardano.Wallet.Deposit.Pure.State.Payment.Inspect
( InspectTx (..)
, inspectTx
)
import Cardano.Wallet.Deposit.Pure.State.Rolling
( rollBackward
Expand Down Expand Up @@ -104,9 +120,13 @@ import Cardano.Wallet.Deposit.Pure.State.Type
, knownCustomer
, knownCustomerAddress
, listCustomers
, networkTag
, trackedCustomers
, walletXPub
)
import Cardano.Wallet.Deposit.Pure.UTxO.Tx
( ResolvedTx (..)
)
import Cardano.Wallet.Deposit.Pure.UTxO.ValueTransfer
( ValueTransfer (..)
)
Expand Down
Loading

0 comments on commit 3679fbd

Please sign in to comment.