Skip to content

Commit

Permalink
Collect IsOwned functions in IsOwnedModule.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed May 30, 2023
1 parent f0bf0af commit e789ab1
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 132 deletions.
37 changes: 25 additions & 12 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Expand Up @@ -435,6 +435,7 @@ import Cardano.Wallet.Flavor
, WalletFlavor (..)
, WalletFlavorS (..)
, keyFlavorFromState
, keyOfWallet
, shelleyOrShared
)
import Cardano.Wallet.Network
Expand Down Expand Up @@ -883,9 +884,10 @@ postShelleyWallet ctx generateKey body = do
(workerCtx ^. typed @(DBLayer IO (SeqState n ShelleyKey)))
)
withWorkerCtx @_ @s ctx wid liftE liftE $ \wrk -> handler $
W.attachPrivateKeyFromPwd @_ @s wrk (rootXPrv, pwd)
W.attachPrivateKeyFromPwd wF wrk (rootXPrv, pwd)
fst <$> getWallet ctx (mkShelleyWallet @_ @s) (ApiT wid)
where
wF = walletFlavor @s
seed = getApiMnemonicT (body ^. #mnemonicSentence)
secondFactor = getApiMnemonicT <$> (body ^. #mnemonicSecondFactor)
pwd = getApiT (body ^. #passphrase)
Expand Down Expand Up @@ -1053,8 +1055,7 @@ postSharedWalletFromRootXPrv
-> ApiSharedWalletPostDataFromMnemonics
-> Handler ApiSharedWallet
postSharedWalletFromRootXPrv ctx generateKey body = do
let kF = keyFlavorFromState @s
wid = WalletId $ toSharedWalletId kF accXPub pTemplate dTemplateM
let wid = WalletId $ toSharedWalletId kF accXPub pTemplate dTemplateM
validateScriptTemplates kF accXPub scriptValidation pTemplate dTemplateM
& \case
Left err -> liftHandler
Expand All @@ -1081,9 +1082,11 @@ postSharedWalletFromRootXPrv ctx generateKey body = do
(workerCtx ^. typed @(DBLayer IO (SharedState n SharedKey)))
)
withWorkerCtx @_ @s ctx wid liftE liftE $ \wrk -> handler $
W.attachPrivateKeyFromPwd @_ @s wrk (rootXPrv, pwd)
W.attachPrivateKeyFromPwd wF wrk (rootXPrv, pwd)
fst <$> getWallet ctx (mkSharedWallet @_ @s) (ApiT wid)
where
kF = keyOfWallet wF
wF = walletFlavor @s
seed = body ^. #mnemonicSentence . #getApiMnemonicT
secondFactor = getApiMnemonicT <$> body ^. #mnemonicSecondFactor
pwdP = preparePassphrase currentPassphraseScheme pwd
Expand Down Expand Up @@ -1321,10 +1324,11 @@ postLegacyWallet ctx (rootXPrv, pwd) createWallet = do
(`createWallet` wid)
idleWorker
withWorkerCtx ctx wid liftE liftE $ \wrk -> handler $
W.attachPrivateKeyFromPwd wrk (rootXPrv, pwd)
W.attachPrivateKeyFromPwd wF wrk (rootXPrv, pwd)
fst <$> getWallet ctx mkLegacyWallet (ApiT wid)
where
kF = keyFlavorFromState @s
kF = keyOfWallet wF
wF = walletFlavor @s
wid = WalletId
$ digest kF
$ publicKey kF rootXPrv
Expand Down Expand Up @@ -1684,7 +1688,8 @@ putWalletPassphrase ctx createKey getKey (ApiT wid)
(ApiT old)
(ApiT new)
) -> liftHandler
$ W.updateWalletPassphraseWithOldPassphrase wrk wid (old, new)
$ W.updateWalletPassphraseWithOldPassphrase
(walletFlavor @s) wrk wid (old, new)
Right
(Api.WalletPutPassphraseMnemonicData
(ApiMnemonicT mnemonic) sndFactor (ApiT new)
Expand All @@ -1696,15 +1701,16 @@ putWalletPassphrase ctx createKey getKey (ApiT wid)
$ deriveAccountPrivateKey encrPass challengeKey minBound
storedPubKey <- handler $ W.readAccountPublicKey wrk
if getKey challengPubKey == getKey storedPubKey
then handler $ W.updateWalletPassphraseWithMnemonic wrk
then handler $ W.updateWalletPassphraseWithMnemonic wF wrk
(challengeKey, new)
else liftHandler
$ throwE
$ ErrUpdatePassphraseWithRootKey
$ ErrWithRootKeyWrongMnemonic wid
where
withWrk :: (WorkerCtx (ApiLayer s) -> Handler a) -> Handler a
withWrk = withWorkerCtx ctx wid liftE liftE
where
wF = walletFlavor @s
withWrk :: (WorkerCtx (ApiLayer s) -> Handler a) -> Handler a
withWrk = withWorkerCtx ctx wid liftE liftE

putByronWalletPassphrase
:: forall ctx s
Expand All @@ -1719,7 +1725,8 @@ putByronWalletPassphrase ctx (ApiT wid) body = do
let (ByronWalletPutPassphraseData oldM (ApiT new)) = body
withWorkerCtx ctx wid liftE liftE $ \wrk -> liftHandler $ do
let old = maybe mempty (coerce . getApiT) oldM
W.updateWalletPassphraseWithOldPassphrase wrk wid (old, new)
W.updateWalletPassphraseWithOldPassphrase
(walletFlavor @s) wrk wid (old, new)
return NoContent

getUTxOsStatistics
Expand Down Expand Up @@ -2173,6 +2180,8 @@ postTransactionOld
, KeyOf s ~ k
, IsOurs s RewardAccount
, CredFromOf s ~ 'CredFromKeyK
, HasSNetworkId (NetworkOf s)
, n ~ NetworkOf s
)
=> ctx
-> ArgGenChange s
Expand Down Expand Up @@ -3489,6 +3498,7 @@ joinStakePool
, SoftDerivation k
, AddressBookIso s
, HasDelegation s
, HasSNetworkId n
)
=> ApiLayer s
-> ArgGenChange s
Expand Down Expand Up @@ -3591,6 +3601,7 @@ quitStakePool
, IsOwned s k 'CredFromKeyK
, AddressBookIso s
, IsOurs (SeqState n k) RewardAccount
, HasSNetworkId n
)
=> ApiLayer s
-> ArgGenChange s
Expand Down Expand Up @@ -3831,6 +3842,8 @@ migrateWallet
, HasDelegation s
, k ~ KeyOf s
, CredFromOf s ~ 'CredFromKeyK
, HasSNetworkId n
, n ~ NetworkOf s
)
=> ApiLayer s
-> Maybe ApiWithdrawalPostData
Expand Down
1 change: 1 addition & 0 deletions lib/wallet/cardano-wallet.cabal
Expand Up @@ -251,6 +251,7 @@ library
Cardano.Wallet.Address.States.Families
Cardano.Wallet.Address.States.Features
Cardano.Wallet.Address.States.Test.State
Cardano.Wallet.Address.States.IsOwned
Cardano.Wallet.Byron.Compatibility
Cardano.Wallet.Checkpoints
Cardano.Wallet.Checkpoints.Policy
Expand Down
80 changes: 48 additions & 32 deletions lib/wallet/src/Cardano/Wallet.hs
Expand Up @@ -288,7 +288,6 @@ import Cardano.Wallet.Address.Discovery
, GetAccount (..)
, GetPurpose (..)
, IsOurs (..)
, IsOwned (..)
, KnownAddresses (..)
, MaybeLight (..)
)
Expand Down Expand Up @@ -317,6 +316,8 @@ import Cardano.Wallet.Address.Keys.WalletKey
, hashVerificationKey
, liftRawKey
)
import Cardano.Wallet.Address.States.IsOwned
( isOwned )
import Cardano.Wallet.Checkpoints
( DeltaCheckpoints (..), extendCheckpoints, pruneCheckpoints )
import Cardano.Wallet.DB
Expand Down Expand Up @@ -349,6 +350,7 @@ import Cardano.Wallet.Flavor
, Excluding
, KeyFlavorS (..)
, KeyOf
, NetworkOf
, WalletFlavor (..)
, WalletFlavorS (..)
, keyFlavorFromState
Expand Down Expand Up @@ -926,35 +928,35 @@ updateWallet ctx f = onWalletState @IO @s ctx $ update $ \s ->
-- | Change a wallet's passphrase to the given passphrase.
updateWalletPassphraseWithOldPassphrase
:: forall ctx s
. ( HasDBLayer IO s ctx
, WalletFlavor s
)
=> ctx
. HasDBLayer IO s ctx
=> WalletFlavorS s
-> ctx
-> WalletId
-> (Passphrase "user", Passphrase "user")
-> ExceptT ErrUpdatePassphrase IO ()
updateWalletPassphraseWithOldPassphrase ctx wid (old, new) =
updateWalletPassphraseWithOldPassphrase wF ctx wid (old, new) =
withRootKey @s db wid old ErrUpdatePassphraseWithRootKey
$ \xprv scheme -> do
-- IMPORTANT NOTE:
-- This use 'EncryptWithPBKDF2', regardless of the passphrase
-- current scheme, we'll re-encrypt it using the current scheme,
-- always.
let new' = (currentPassphraseScheme, new)
let xprv' = changePassphraseNew (keyFlavorFromState @s)
let xprv' = changePassphraseNew (keyOfWallet wF)
(scheme, old) new' xprv
lift $ attachPrivateKeyFromPwdScheme @ctx @s ctx (xprv', new')
lift $ attachPrivateKeyFromPwdScheme wF ctx (xprv', new')
where
db = ctx ^. typed

updateWalletPassphraseWithMnemonic
:: forall ctx s
. HasDBLayer IO s ctx
=> ctx
=> WalletFlavorS s
-> ctx
-> (KeyOf s 'RootK XPrv, Passphrase "user")
-> IO ()
updateWalletPassphraseWithMnemonic ctx (xprv, new) =
attachPrivateKeyFromPwdScheme @ctx @s ctx
updateWalletPassphraseWithMnemonic wF ctx (xprv, new) =
attachPrivateKeyFromPwdScheme wF ctx
(xprv, (currentPassphraseScheme , new))

getWalletUtxoSnapshot
Expand Down Expand Up @@ -1885,16 +1887,22 @@ data ErrWriteTxEra
--
-- Requires the encryption passphrase in order to decrypt the root private key.
buildSignSubmitTransaction
:: forall s k.
( HardDerivation k
, Bounded (Index (AddressIndexDerivationType k) (AddressCredential k))
, IsOwned s k 'CredFromKeyK
, IsOurs s RewardAccount
, AddressBookIso s
, WalletFlavor s
:: forall s k
. ( HardDerivation k
, Bounded (Index (AddressIndexDerivationType k) (AddressCredential k))
, IsOurs s RewardAccount
, AddressBookIso s
, IsOurs s Address
, WalletFlavor s
, k ~ KeyOf s
, CredFromOf s ~ 'CredFromKeyK
, HasSNetworkId (NetworkOf s)
, Excluding '[SharedKey] k
, Excluding '[SharedKey] k
, k ~ KeyOf s
)
, Excluding '[SharedKey] k
, k ~ KeyOf s
)
=> DBLayer IO s
-> NetworkLayer IO Read.Block
-> TransactionLayer k 'CredFromKeyK SealedTx
Expand Down Expand Up @@ -1980,12 +1988,13 @@ buildAndSignTransactionPure
:: forall k s
. ( HardDerivation k
, Bounded (Index (AddressIndexDerivationType k) (AddressCredential k))
, IsOwned s k 'CredFromKeyK
, Excluding '[SharedKey] k
, k ~ KeyOf s
, HardDerivation k
, IsOurs s RewardAccount
, IsOurs s Address
, WalletFlavor s
, k ~ KeyOf s
, CredFromOf s ~ 'CredFromKeyK
, Excluding '[SharedKey] k
, HasSNetworkId (NetworkOf s)
)
=> TimeTranslation
-> UTxO
Expand Down Expand Up @@ -2027,7 +2036,7 @@ buildAndSignTransactionPure
txLayer
anyCardanoEra
AnyWitnessCountCtx
(isOwned (getState wallet) (rootKey, passphrase))
(isOwned wF (getState wallet) (rootKey, passphrase))
mExternalRewardAccount
(rootKey, passphrase)
(wallet ^. #utxo)
Expand Down Expand Up @@ -2077,6 +2086,7 @@ buildAndSignTransactionPure
, builtSealedTx = signedTx
}
where
wF = walletFlavor @s
anyCardanoEra = Write.toAnyCardanoEra era

buildTransaction
Expand Down Expand Up @@ -2216,8 +2226,11 @@ buildAndSignTransaction
( HasTransactionLayer k 'CredFromKeyK ctx
, HasDBLayer IO s ctx
, HasNetworkLayer IO ctx
, IsOwned s k 'CredFromKeyK
, IsOurs s Address
, k ~ KeyOf s
, HasSNetworkId (NetworkOf s)
, CredFromOf s ~ 'CredFromKeyK
, WalletFlavor s
)
=> ctx
-> WalletId
Expand All @@ -2233,7 +2246,7 @@ buildAndSignTransaction ctx wid era mkRwdAcct pwd txCtx sel = db & \DBLayer{..}
mapExceptT atomically $ do
cp <- lift readCheckpoint
pp <- liftIO $ currentProtocolParameters nl
let keyFrom = isOwned (getState cp) (xprv, pwdP)
let keyFrom = isOwned wF (getState cp) (xprv, pwdP)
let rewardAcnt = mkRwdAcct (xprv, pwdP)
(tx, sealedTx) <- withExceptT ErrSignPaymentMkTx $ ExceptT $ pure $
mkTransaction tl era rewardAcnt keyFrom pp txCtx sel
Expand All @@ -2259,6 +2272,7 @@ buildAndSignTransaction ctx wid era mkRwdAcct pwd txCtx sel = db & \DBLayer{..}
amountIn amountOut
pure (tx, meta, time, sealedTx)
where
wF = walletFlavor @s
db = ctx ^. dbLayer @IO @s
tl = ctx ^. transactionLayer @k @'CredFromKeyK
nl = ctx ^. networkLayer
Expand Down Expand Up @@ -2954,10 +2968,11 @@ padFeePercentiles
attachPrivateKeyFromPwdScheme
:: forall ctx s
. HasDBLayer IO s ctx
=> ctx
=> WalletFlavorS s
-> ctx
-> (KeyOf s 'RootK XPrv, (PassphraseScheme, Passphrase "user"))
-> IO ()
attachPrivateKeyFromPwdScheme ctx (xprv, (scheme, pwd)) = db & \_ -> do
attachPrivateKeyFromPwdScheme _ ctx (xprv, (scheme, pwd)) = db & \_ -> do
hpwd <- liftIO $ encryptPassphrase' scheme pwd
-- NOTE Only new wallets are constructed through this function, so the
-- passphrase is encrypted with the new scheme (i.e. PBKDF2)
Expand All @@ -2981,11 +2996,12 @@ attachPrivateKeyFromPwdScheme ctx (xprv, (scheme, pwd)) = db & \_ -> do
attachPrivateKeyFromPwd
:: forall ctx s
. HasDBLayer IO s ctx
=> ctx
=> WalletFlavorS s
-> ctx
-> (KeyOf s 'RootK XPrv, Passphrase "user")
-> IO ()
attachPrivateKeyFromPwd ctx (xprv, pwd) =
attachPrivateKeyFromPwdScheme @ctx @s ctx
attachPrivateKeyFromPwd wF ctx (xprv, pwd) =
attachPrivateKeyFromPwdScheme wF ctx
(xprv, (currentPassphraseScheme, pwd))

-- | The hash here is the output of Scrypt function with the following parameters:
Expand Down Expand Up @@ -3737,7 +3753,7 @@ dummyChangeAddressGen =

utxoAssumptionsForWallet
:: forall s
. Excluding '[SharedKey] (KeyOf s)
. (Excluding '[SharedKey] (KeyOf s))
=> WalletFlavorS s
-> UTxOAssumptions
utxoAssumptionsForWallet = keyOfWallet >>> \case
Expand Down

0 comments on commit e789ab1

Please sign in to comment.