Skip to content

Commit

Permalink
add hashing capability to shelley, unite Api.Link
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed May 24, 2021
1 parent 3699915 commit 2ed0a9d
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 142 deletions.
4 changes: 2 additions & 2 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ getSharedWalletKey ctx wal role ix hashed =
ApiSharedWallet (Right wal') -> r wal'
where
r :: forall w. HasType (ApiT WalletId) w => w -> m (HTTP.Status, Either RequestException ApiVerificationKeyShared)
r w = request @ApiVerificationKeyShared ctx (Link.getSharedWalletKey w role ix hashed) Default Empty
r w = request @ApiVerificationKeyShared ctx (Link.getWalletKey @'Shared w role ix hashed) Default Empty

postAccountKeyShared
:: forall m.
Expand All @@ -1476,7 +1476,7 @@ postAccountKeyShared ctx wal ix headers payload =
ApiSharedWallet (Right wal') -> r wal'
where
r :: forall w. HasType (ApiT WalletId) w => w -> m (HTTP.Status, Either RequestException ApiAccountKeyShared)
r w = request @ApiAccountKeyShared ctx (Link.postAccountKeyShared w ix) headers payload
r w = request @ApiAccountKeyShared ctx (Link.postAccountKey @'Shared w ix) headers payload

patchEndpointEnding :: CredentialType -> Text
patchEndpointEnding = \case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,10 @@ spec = describe "SHELLEY_ADDRESSES" $ do
-- wallet API
let indices = [0..19]
generatedAddresses <- forM indices $ \index -> do
let paymentPath = Link.getWalletKey w UtxoExternal (DerivationIndex index)
let paymentPath = Link.getWalletKey @'Shelley w UtxoExternal (DerivationIndex index) Nothing
(_, paymentKey) <- unsafeRequest @ApiVerificationKeyShelley ctx paymentPath Empty

let stakePath = Link.getWalletKey w MutableAccount (DerivationIndex 0)
let stakePath = Link.getWalletKey @'Shelley w MutableAccount (DerivationIndex 0) Nothing
(_, stakeKey) <- unsafeRequest @ApiVerificationKeyShelley ctx stakePath Empty

let payload = Json [json|{
Expand Down Expand Up @@ -910,7 +910,7 @@ spec = describe "SHELLEY_ADDRESSES" $ do
let initPoolGap = 10
w <- emptyWalletWith ctx ("Wallet", fixturePassphrase, initPoolGap)

let endpoint = Link.postAccountKey w (DerivationIndex 0)
let endpoint = Link.postAccountKey @'Shelley w (DerivationIndex 0)
let payload = Json [json|{
"passphrase": #{fixturePassphrase},
"format": "extended"
Expand All @@ -921,7 +921,7 @@ spec = describe "SHELLEY_ADDRESSES" $ do
-- Request first 10 extended account public keys
let indices = [0..9]
accountPublicKeys <- forM indices $ \index -> do
let accountPath = Link.postAccountKey w (DerivationIndex $ 2147483648 + index)
let accountPath = Link.postAccountKey @'Shelley w (DerivationIndex $ 2147483648 + index)
let payload1 = Json [json|{
"passphrase": #{fixturePassphrase},
"format": "extended"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ spec = describe "SHELLEY_WALLETS" $ do

forM_ matrix $ \(role_, index, expected) ->
counterexample (show role_ <> "/" <> show index) $ do
let link = Link.getWalletKey (apiWal ^. id) role_ index
let link = Link.getWalletKey @'Shelley (apiWal ^. id) role_ index Nothing
rGet <- request @ApiVerificationKeyShelley ctx link Default Empty
verify rGet
[ expectResponseCode HTTP.status200
Expand All @@ -1054,7 +1054,7 @@ spec = describe "SHELLEY_WALLETS" $ do
it "WALLETS_GET_KEY_02 - invalid index for verification key" $ \ctx -> runResourceT $ do
w <- emptyWallet ctx

let link = Link.getWalletKey w UtxoExternal (DerivationIndex 2147483648)
let link = Link.getWalletKey @'Shelley w UtxoExternal (DerivationIndex 2147483648) Nothing
r <- request @ApiVerificationKeyShelley ctx link Default Empty

verify r
Expand All @@ -1067,7 +1067,7 @@ spec = describe "SHELLEY_WALLETS" $ do
w <- emptyWallet ctx
_ <- request @ApiWallet ctx (Link.deleteWallet @'Shelley w) Default Empty

let link = Link.getWalletKey w UtxoExternal (DerivationIndex 0)
let link = Link.getWalletKey @'Shelley w UtxoExternal (DerivationIndex 0) Nothing
r <- request @ApiVerificationKeyShelley ctx link Default Empty

verify r
Expand All @@ -1093,7 +1093,7 @@ spec = describe "SHELLEY_WALLETS" $ do

-- get corresponding public key
rKey <- request @ApiVerificationKeyShelley ctx
(Link.getWalletKey w role_ index)
(Link.getWalletKey @'Shelley w role_ index Nothing)
Default
Empty
verify rKey
Expand Down
10 changes: 10 additions & 0 deletions lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Cardano.Wallet.Api
, GetWalletKey
, SignMetadata
, PostAccountKey
, GetAccountKey

, Assets
, ListAssets
Expand Down Expand Up @@ -357,13 +358,15 @@ type WalletKeys =
GetWalletKey
:<|> SignMetadata
:<|> PostAccountKey
:<|> GetAccountKey

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/getWalletKey
type GetWalletKey = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "keys"
:> Capture "role" (ApiT Role)
:> Capture "index" (ApiT DerivationIndex)
:> QueryParam "hash" Bool
:> Get '[JSON] ApiVerificationKeyShelley

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/signMetadata
Expand All @@ -383,6 +386,13 @@ type PostAccountKey = "wallets"
:> ReqBody '[JSON] ApiPostAccountKeyData
:> PostAccepted '[JSON] ApiAccountKey

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/getAccountKey
type GetAccountKey = "wallets"
:> Capture "walletId" (ApiT WalletId)
:> "keys"
:> QueryParam "extended" Bool
:> Get '[JSON] ApiAccountKey

{-------------------------------------------------------------------------------
Assets
Expand Down
109 changes: 43 additions & 66 deletions lib/core/src/Cardano/Wallet/Api/Link.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Cardano.Wallet.Api.Link
, getWalletKey
, signMetadata
, postAccountKey
, getAccountKey

-- * Addresses
, postRandomAddress
Expand Down Expand Up @@ -107,11 +108,6 @@ module Cardano.Wallet.Api.Link
-- * Shared Wallets
, patchSharedWallet

-- * SharedWalletKeys
, getSharedWalletKey
, postAccountKeyShared
, getAccountKeyShared

, PostWallet
, Discriminate
) where
Expand Down Expand Up @@ -232,7 +228,7 @@ getUTxOsStatistics
getUTxOsStatistics w = discriminate @style
(endpoint @Api.GetUTxOsStatistics (wid &))
(endpoint @Api.GetByronUTxOsStatistics (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -256,7 +252,7 @@ putWallet
putWallet w = discriminate @style
(endpoint @Api.PutWallet (wid &))
(endpoint @Api.PutByronWallet (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -270,7 +266,7 @@ putWalletPassphrase
putWalletPassphrase w = discriminate @style
(endpoint @Api.PutWalletPassphrase (wid &))
(endpoint @Api.PutByronWalletPassphrase (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -284,7 +280,7 @@ migrateWallet
migrateWallet w = discriminate @style
(endpoint @(Api.MigrateShelleyWallet Net) (wid &))
(endpoint @(Api.MigrateByronWallet Net) (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -298,7 +294,7 @@ createMigrationPlan
createMigrationPlan w = discriminate @style
(endpoint @(Api.CreateShelleyWalletMigrationPlan Net) (wid &))
(endpoint @(Api.CreateByronWalletMigrationPlan Net) (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -307,15 +303,19 @@ createMigrationPlan w = discriminate @style
--

getWalletKey
:: forall w.
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> Role
-> DerivationIndex
-> Maybe Bool
-> (Method, Text)
getWalletKey w role_ index =
endpoint @Api.GetWalletKey (\mk -> mk wid (ApiT role_) (ApiT index))
getWalletKey w role_ index hashed = discriminate @style
(notSupported "Byron")
(endpoint @Api.GetWalletKey (\mk -> mk wid (ApiT role_) (ApiT index) hashed))
(endpoint @Api.GetSharedWalletKey (\mk -> mk wid (ApiT role_) (ApiT index) hashed))
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -333,17 +333,36 @@ signMetadata w role_ index =
wid = w ^. typed @(ApiT WalletId)

postAccountKey
:: forall w.
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> DerivationIndex
-> (Method, Text)
postAccountKey w index =
endpoint @Api.PostAccountKey (\mk -> mk wid (ApiT index))
postAccountKey w index = discriminate @style
(notSupported "Byron")
(endpoint @Api.PostAccountKey (\mk -> mk wid (ApiT index)))
(endpoint @Api.PostAccountKeyShared (\mk -> mk wid (ApiT index)))
where
wid = w ^. typed @(ApiT WalletId)

getAccountKey
:: forall style w.
( HasType (ApiT WalletId) w
, Discriminate style
)
=> w
-> Maybe Bool
-> (Method, Text)
getAccountKey w extended = discriminate @style
(notSupported "Byron")
(endpoint @Api.GetAccountKey (\mk -> mk wid extended))
(endpoint @Api.GetAccountKeyShared (\mk -> mk wid extended))
where
wid = w ^. typed @(ApiT WalletId)


--
-- Addresses
--
Expand Down Expand Up @@ -420,7 +439,7 @@ selectCoins
selectCoins w = discriminate @style
(endpoint @(Api.SelectCoins Net) (wid &))
(endpoint @(Api.ByronSelectCoins Net) (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand Down Expand Up @@ -496,7 +515,7 @@ createTransaction
createTransaction w = discriminate @style
(endpoint @(Api.CreateTransaction Net) (wid &))
(endpoint @(Api.CreateByronTransaction Net) (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand Down Expand Up @@ -526,7 +545,7 @@ listTransactions' w minWithdrawal inf sup order = discriminate @style
(\mk -> mk wid (MinWithdrawal <$> minWithdrawal) inf sup (ApiT <$> order)))
(endpoint @(Api.ListByronTransactions Net)
(\mk -> mk wid inf sup (ApiT <$> order)))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -540,7 +559,7 @@ getTransactionFee
getTransactionFee w = discriminate @style
(endpoint @(Api.PostTransactionFee Net) (wid &))
(endpoint @(Api.PostByronTransactionFee Net) (wid &))
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)

Expand All @@ -556,7 +575,7 @@ deleteTransaction
deleteTransaction w t = discriminate @style
(endpoint @Api.DeleteTransaction mkURL)
(endpoint @Api.DeleteByronTransaction mkURL)
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)
tid = ApiTxId (t ^. typed @(ApiT (Hash "Tx")))
Expand All @@ -574,7 +593,7 @@ getTransaction
getTransaction w t = discriminate @style
(endpoint @(Api.GetTransaction Net) mkURL)
(endpoint @(Api.GetByronTransaction Net) mkURL)
notSupported
(notSupported "Shared")
where
wid = w ^. typed @(ApiT WalletId)
tid = ApiTxId (t ^. typed @(ApiT (Hash "Tx")))
Expand Down Expand Up @@ -722,48 +741,6 @@ patchSharedWallet w cred =
where
wid = w ^. typed @(ApiT WalletId)

--
-- SharedWalletKeys
--
getSharedWalletKey
:: forall w.
( HasType (ApiT WalletId) w
)
=> w
-> Role
-> DerivationIndex
-> Maybe Bool
-> (Method, Text)
getSharedWalletKey w role_ index hashed =
endpoint @Api.GetSharedWalletKey (\mk -> mk wid (ApiT role_) (ApiT index) hashed)
where
wid = w ^. typed @(ApiT WalletId)

postAccountKeyShared
:: forall w.
( HasType (ApiT WalletId) w
)
=> w
-> DerivationIndex
-> (Method, Text)
postAccountKeyShared w index =
endpoint @Api.PostAccountKeyShared (\mk -> mk wid (ApiT index))
where
wid = w ^. typed @(ApiT WalletId)

getAccountKeyShared
:: forall w.
( HasType (ApiT WalletId) w
)
=> w
-> Maybe Bool
-> (Method, Text)
getAccountKeyShared w extended =
endpoint @Api.GetAccountKeyShared (\mk -> mk wid extended)
where
wid = w ^. typed @(ApiT WalletId)


--
-- Internals
--
Expand Down Expand Up @@ -823,8 +800,8 @@ instance Discriminate 'Byron where
instance Discriminate 'Shared where
discriminate _ _ a = a

notSupported :: a
notSupported = error "Endpoint not supported for Shared style"
notSupported :: String -> a
notSupported style = error $ "Endpoint not supported for " <> style <> " style"

-- | Some endpoints are parameterized via a network discriminant in order to
-- correctly encode their end type (for example, 'CreateTransaction n'). Yet, in
Expand Down
Loading

0 comments on commit 2ed0a9d

Please sign in to comment.