Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl signing for minting/burning in new tx flow #3199

Merged
merged 29 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
65a8470
clean not needed bits - only new tx flow support minting/burning
paweljakubas Mar 21, 2022
a1aae13
add ApiPostPolicyKeyData
paweljakubas Mar 22, 2022
01967e1
ApiPostPolicyKeyData - part 2
paweljakubas Mar 23, 2022
86020ae
add writePolicyKey in Cardano.Wallet
paweljakubas Mar 24, 2022
1bb0286
conclude postPolicyKey endpoint
paweljakubas Mar 24, 2022
81b2d5f
add signing for minting/burning
paweljakubas Mar 25, 2022
35e65ff
extend integration test - part 1
paweljakubas Mar 25, 2022
7fbd3f3
fix fee calculation
paweljakubas Mar 25, 2022
00edc09
reorganize integration tests for better reuse
paweljakubas Mar 28, 2022
9251e50
prove that minted assets are in wallet finally
paweljakubas Mar 28, 2022
5eecd0d
prove burning in integration testing
paweljakubas Mar 28, 2022
e44cb74
postPolicyPublicKey with optional param plus cleaning
paweljakubas Mar 28, 2022
2c8dadd
unify mint/burn response for construct/decode - swagger
paweljakubas Mar 28, 2022
f43122f
finish refactoring construct/decode
paweljakubas Mar 29, 2022
7f961eb
Wrap long lines according to team coding standard.
jonathanknowles Mar 30, 2022
fba7aae
Use just a single blank line between definitions.
jonathanknowles Mar 30, 2022
9b0520c
Use 4-space indents in Haskell source files.
jonathanknowles Mar 30, 2022
fead6f8
Add type signatures to helper functions in `TransactionsNew`.
jonathanknowles Mar 31, 2022
2626b0b
Rename `mintAssetCheck` to `mintAssetsCheck`.
jonathanknowles Mar 31, 2022
6951314
Rename `ApiTokenAsset` to `ApiTokenAmountFingerprint`.
jonathanknowles Mar 31, 2022
cded02d
Simplify applicative expressions.
jonathanknowles Mar 31, 2022
ba75be5
Use `GeneralizedNewtypeDeriving` to eliminate boilerplate.
jonathanknowles Mar 31, 2022
66bccd7
Rename `numberOfAssets` to `tokenAmount`.
jonathanknowles Mar 31, 2022
2a92320
Revise error message for `MissingPolicyPublicKey`.
jonathanknowles Mar 31, 2022
9049901
Add note regarding the use of `L.nub`.
jonathanknowles Mar 31, 2022
230d8c1
Add TODO that links to ADP-1193 in `TRANS_NEW_CREATE_10e`.
jonathanknowles Mar 31, 2022
8c03c46
Replace `const x <$>` with `x <$` in `Api.Server`.
jonathanknowles Mar 31, 2022
7076da5
Remove redundant parentheses and `do` usages.
jonathanknowles Mar 31, 2022
1c4da7c
Remove redundant check for empty `TokenMap` in `toApiTokens`.
jonathanknowles Mar 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

61 changes: 56 additions & 5 deletions lib/core/src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module Cardano.Wallet
, readRewardAccount
, someRewardAccount
, readPolicyPublicKey
, writePolicyPublicKey
, queryRewardBalance
, ErrWalletAlreadyExists (..)
, ErrNoSuchWallet (..)
Expand All @@ -93,6 +94,7 @@ module Cardano.Wallet
, ErrWalletNotResponding (..)
, ErrReadRewardAccount (..)
, ErrReadPolicyPublicKey (..)
, ErrWritePolicyPublicKey (..)

-- * Shared Wallet
, updateCosigner
Expand Down Expand Up @@ -212,7 +214,7 @@ import Prelude hiding
import Cardano.Address.Derivation
( XPrv, XPub )
import Cardano.Address.Script
( Cosigner (..) )
( Cosigner (..), KeyHash )
import Cardano.Api
( serialiseToCBOR )
import Cardano.BM.Data.Severity
Expand Down Expand Up @@ -260,7 +262,7 @@ import Cardano.Wallet.DB
import Cardano.Wallet.DB.Checkpoints
( DeltaCheckpoints (..) )
import Cardano.Wallet.DB.Sqlite.AddressBook
( AddressBookIso, getPrologue )
( AddressBookIso, Prologue (..), getPrologue )
import Cardano.Wallet.DB.WalletState
( DeltaMap (..), DeltaWalletState1 (..), fromWallet, getLatest, getSlot )
import Cardano.Wallet.Logging
Expand Down Expand Up @@ -298,6 +300,7 @@ import Cardano.Wallet.Primitive.AddressDerivation
, checkPassphrase
, deriveRewardAccount
, encryptPassphrase
, hashVerificationKey
, liftIndex
, preparePassphrase
, stakeDerivationPath
Expand All @@ -307,7 +310,7 @@ import Cardano.Wallet.Primitive.AddressDerivation.Byron
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.MintBurn
( policyDerivationPath )
( derivePolicyPrivateKey, policyDerivationPath )
import Cardano.Wallet.Primitive.AddressDerivation.SharedKey
( SharedKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
Expand Down Expand Up @@ -517,7 +520,7 @@ import Data.Function
import Data.Functor
( ($>) )
import Data.Generics.Internal.VL.Lens
( Lens', view, (^.) )
( Lens', view, (.~), (^.) )
import Data.Generics.Labels
()
import Data.Generics.Product.Typed
Expand Down Expand Up @@ -584,6 +587,7 @@ import UnliftIO.Exception
import UnliftIO.MVar
( modifyMVar_, newMVar )

import qualified Cardano.Address.Script as CA
import qualified Cardano.Api as Cardano
import qualified Cardano.Api.Shelley as Cardano
import qualified Cardano.Crypto.Wallet as CC
Expand Down Expand Up @@ -2321,12 +2325,21 @@ signTransaction tl era keyLookup (rootKey, rootPwd) utxo =
rewardAcnt =
(getRawKey $ deriveRewardAccount @k rootPwd rootKey, rootPwd)

policyKey :: (KeyHash, XPrv, Passphrase "encryption")
policyKey =
( hashVerificationKey @k CA.Policy $ liftRawKey $ toXPub xprv
, xprv
, rootPwd
)
where
xprv = derivePolicyPrivateKey rootPwd (getRawKey rootKey) minBound

inputResolver :: TxIn -> Maybe Address
inputResolver i = do
TxOut addr _ <- UTxO.lookup i utxo
pure addr
in
addVkWitnesses tl era rewardAcnt keyLookup inputResolver
addVkWitnesses tl era rewardAcnt policyKey keyLookup inputResolver

-- | Produce witnesses and construct a transaction from a given selection.
--
Expand Down Expand Up @@ -3236,6 +3249,39 @@ readAccountPublicKey ctx wid = db & \DBLayer{..} -> do
where
db = ctx ^. dbLayer @IO @s @k

writePolicyPublicKey
:: forall ctx s (n :: NetworkDiscriminant).
( HasDBLayer IO s ShelleyKey ctx
, s ~ SeqState n ShelleyKey
)
=> ctx
-> WalletId
-> Passphrase "raw"
-> ExceptT ErrWritePolicyPublicKey IO (ShelleyKey 'PolicyK XPub)
writePolicyPublicKey ctx wid pwd = db & \DBLayer{..} -> do
cp <- mapExceptT atomically
$ withExceptT ErrWritePolicyPublicKeyNoSuchWallet
$ withNoSuchWallet wid
$ readCheckpoint wid

let (SeqPrologue seqState) = getPrologue $ getState cp

policyXPub <- withRootKey
@ctx @s @ShelleyKey ctx wid pwd ErrWritePolicyPublicKeyWithRootKey $
\rootK scheme -> do
let encPwd = preparePassphrase scheme pwd
let xprv = derivePolicyPrivateKey encPwd (getRawKey rootK) minBound
pure $ liftRawKey $ toXPub xprv

let seqState' = seqState & #policyXPub .~ Just policyXPub
ExceptT $ atomically $ modifyDBMaybe walletsDB $
adjustNoSuchWallet wid ErrWritePolicyPublicKeyNoSuchWallet $
\_ -> Right ( [ReplacePrologue $ SeqPrologue seqState'], () )

pure policyXPub
where
db = ctx ^. dbLayer @IO @s @ShelleyKey

-- | Retrieve any public account key of a wallet.
getAccountPublicKeyAtIndex
:: forall ctx s k.
Expand Down Expand Up @@ -3566,6 +3612,11 @@ data ErrReadPolicyPublicKey
| ErrReadPolicyPublicKeyAbsent
deriving (Generic, Eq, Show)

data ErrWritePolicyPublicKey
= ErrWritePolicyPublicKeyNoSuchWallet ErrNoSuchWallet
| ErrWritePolicyPublicKeyWithRootKey ErrWithRootKey
deriving (Generic, Eq, Show)

{-------------------------------------------------------------------------------
Utils
-------------------------------------------------------------------------------}
Expand Down
28 changes: 14 additions & 14 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ module Cardano.Wallet.Api
, PostAccountKey
, GetAccountKey
, GetPolicyKey
, PostPolicyKey

, Assets
, ListAssets
, GetAsset
, GetAssetDefault
, MintBurnAssets

, Addresses
, ListAddresses
Expand Down Expand Up @@ -185,14 +185,14 @@ import Cardano.Wallet.Api.Types
, ApiHealthCheck
, ApiMaintenanceAction
, ApiMaintenanceActionPostData
, ApiMintBurnTransactionT
, ApiNetworkClock
, ApiNetworkInformation
, ApiNetworkParameters
, ApiPolicyKey
, ApiPoolId
, ApiPostAccountKeyData
, ApiPostAccountKeyDataWithPurpose
, ApiPostPolicyKeyData
, ApiPostRandomAddressData
, ApiPutAddressesDataT
, ApiSelectCoinsDataT
Expand All @@ -219,7 +219,6 @@ import Cardano.Wallet.Api.Types
, Iso8601Time
, KeyFormat
, MinWithdrawal
, PostMintBurnAssetDataT
, PostTransactionFeeOldDataT
, PostTransactionOldDataT
, SettingsPutData
Expand Down Expand Up @@ -307,7 +306,7 @@ type ApiV2 n apiPool = "v2" :> Api n apiPool
type Api n apiPool =
Wallets
:<|> WalletKeys
:<|> Assets n
:<|> Assets
:<|> Addresses n
:<|> CoinSelections n
:<|> ShelleyTransactions n
Expand Down Expand Up @@ -399,6 +398,7 @@ type WalletKeys =
:<|> PostAccountKey
:<|> GetAccountKey
:<|> GetPolicyKey
:<|> PostPolicyKey

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/getWalletKey
type GetWalletKey = "wallets"
Expand Down Expand Up @@ -440,15 +440,22 @@ type GetPolicyKey = "wallets"
:> QueryParam "hash" Bool
:> Get '[JSON] ApiPolicyKey

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postPolicyKey
type PostPolicyKey = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "policy-key"
:> QueryParam "hash" Bool
:> ReqBody '[JSON] ApiPostPolicyKeyData
:> PostAccepted '[JSON] ApiPolicyKey

{-------------------------------------------------------------------------------
Assets

See also: https://input-output-hk.github.io/cardano-wallet/api/#tag/Assets
-------------------------------------------------------------------------------}

type Assets n =
MintBurnAssets n
:<|> ListAssets
type Assets =
ListAssets
:<|> GetAsset
:<|> GetAssetDefault

Expand All @@ -473,13 +480,6 @@ type GetAssetDefault = "wallets"
:> Capture "policyId" (ApiT TokenPolicyId)
:> Get '[JSON] ApiAsset

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/mintBurnAssets
type MintBurnAssets n = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "assets"
:> ReqBody '[JSON] (PostMintBurnAssetDataT n)
:> PostAccepted '[JSON] (ApiMintBurnTransactionT n)

{-------------------------------------------------------------------------------
Addresses

Expand Down
28 changes: 17 additions & 11 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Cardano.Wallet.Api.Link
, postAccountKey
, getAccountKey
, getPolicyKey
, postPolicyKey

-- * Addresses
, postRandomAddress
Expand All @@ -72,7 +73,6 @@ module Cardano.Wallet.Api.Link
, getAsset
, listByronAssets
, getByronAsset
, mintBurnAssets

-- * Transactions
, createTransactionOld
Expand Down Expand Up @@ -412,6 +412,22 @@ getPolicyKey w hashed = discriminate @style
where
wid = w ^. typed @(ApiT WalletId)

postPolicyKey
:: forall (style :: WalletStyle) w.
( HasCallStack
, Discriminate style
, HasType (ApiT WalletId) w
)
=> w
-> Maybe Bool
-> (Method, Text)
postPolicyKey w hashed = discriminate @style
(endpoint @Api.PostPolicyKey (\mk -> mk wid hashed))
(notSupported "Byron")
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

--
-- Addresses
--
Expand Down Expand Up @@ -971,13 +987,3 @@ instance HasVerb sub => HasVerb (QueryFlag sym :> sub) where

instance HasVerb sub => HasVerb (Header' opts name ty :> sub) where
method _ = method (Proxy @sub)

mintBurnAssets
:: forall w.
( HasType (ApiT WalletId) w
)
=> w
-> (Method, Text)
mintBurnAssets w = (endpoint @(Api.MintBurnAssets Net) (wid &))
where
wid = w ^. typed @(ApiT WalletId)
Loading