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

ADP-494 - Add POST endpoint for script address #2253

Merged
merged 26 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a86e8c7
add swagger with script as string
paweljakubas Oct 16, 2020
eee6f17
add endpoint
paweljakubas Oct 22, 2020
5c9ad40
introduce ApiScript
paweljakubas Oct 22, 2020
74fe268
update core:unit
paweljakubas Oct 22, 2020
9319cb1
add ApiCredential and ApiCredentials
paweljakubas Oct 23, 2020
60066ec
add AnyAddress
paweljakubas Oct 26, 2020
03524d0
implementation of enterprise addresses
paweljakubas Oct 26, 2020
c456474
add reward account impl
paweljakubas Oct 27, 2020
82f083a
add base address impl
paweljakubas Oct 27, 2020
5322b95
add Api.Link for postAnyAddress
paweljakubas Oct 27, 2020
5bb1383
integration tests showing script enterprise addresses work
paweljakubas Oct 27, 2020
41a1e1c
renaming to ApiCredential and change data constructors
paweljakubas Oct 28, 2020
6d95b74
move postAnyAddress to shelley and use proper network discrimination …
paweljakubas Oct 28, 2020
cd79af0
ApiCredentials -> ApiAddressData
paweljakubas Oct 30, 2020
8c63aa9
add golden tests for enterprise address with script
paweljakubas Oct 30, 2020
6831d1e
add golden for reward account and base addresses with script
paweljakubas Oct 30, 2020
c6ee9ba
more precise swagger examples and shelley restore compilation fix
paweljakubas Oct 30, 2020
043b1f0
enterprise from pub key golden test
paweljakubas Nov 1, 2020
5f0be33
add reward account from public key golden test
paweljakubas Nov 1, 2020
b51857e
Delegation address with both pub key credentials - golden tests
paweljakubas Nov 1, 2020
d8b140a
delegating address golden test - spending from script, staking from p…
paweljakubas Nov 1, 2020
ce151c6
delegating address golden test - spending from key, staking from script
paweljakubas Nov 1, 2020
4f14272
Reset auto-generated whitespace in stack.yaml
rvl Nov 2, 2020
18b825a
Update CLI golden test
rvl Nov 2, 2020
c50b1c5
drop script and pub_key
paweljakubas Nov 5, 2020
cf40f51
Regenerate nix
Nov 5, 2020
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
1 change: 1 addition & 0 deletions lib/cli/test/unit/Cardano/CLISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ spec = do
, " public Get the public counterpart of a"
, " private key"
, " inspect Show information about a key"
, " hash Get the hash of a public key"
, ""
, "Example:"
, " \ESC[1m$ "<>progName<>" recovery-phrase generate --size 15 \\\ESC[0m"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Test.Integration.Scenario.API.Shelley.Addresses
import Prelude

import Cardano.Wallet.Api.Types
( ApiAddress
( AnyAddress
, ApiAddress
, ApiTransaction
, ApiWallet
, DecodeAddress
Expand All @@ -30,10 +31,14 @@ import Control.Monad
( forM_ )
import Control.Monad.Trans.Resource
( runResourceT )
import Data.Aeson
( ToJSON (..), object, (.=) )
import Data.Generics.Internal.VL.Lens
( (^.) )
import Data.Quantity
( Quantity (..) )
import Data.Text
( Text )
import Test.Hspec
( SpecWith, describe, shouldBe, shouldNotSatisfy, shouldSatisfy )
import Test.Hspec.Extra
Expand All @@ -52,6 +57,7 @@ import Test.Integration.Framework.DSL
, expectListSize
, expectResponseCode
, fixtureWallet
, getFromResponse
, json
, listAddresses
, minUTxOValue
Expand Down Expand Up @@ -251,3 +257,269 @@ spec = describe "SHELLEY_ADDRESSES" $ do
let str = "patate"
r <- request @Aeson.Value ctx (Link.inspectAddress str) Default Empty
expectResponseCode HTTP.status400 r

-- Generating golden test data for enterprise addresses - script credential:
--- $ cardano-address script hash "$(cat script.txt)" \
--- | cardano-address address payment --from-script --network-tag mainnet
it "ANY_ADDRESS_POST_01 - Golden tests for enterprise script address - signature" $ \ctx -> do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking but why golden? Afaik, golden are unit tests with output in separate file which is not the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

golden because I have generated golden data in cardano-address for all examples, and check if all json/handling/.. machinery comes to the same results

--- $ cat script.txt
--- script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a
let payload = Json [json|{
"payment": "script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a"
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1w96eswctz5wzrv3ceh3h4y3na2t6d95sjn23dawy0zlzg0q0j39eu" :: Text
validateAddr r goldenAddr

it "ANY_ADDRESS_POST_02 - Golden tests for enterprise script address - any" $ \ctx -> do
--- $ cat script.txt
--- any [script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a, script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4]
let payload = Json [json|{
"payment": {
"any": [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4"
]
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1wxt2z3pa7etaxp7jurdg0m8jhsmtp4r2z56pd3a5q3jhxyc3vza6h" :: Text
validateAddr r goldenAddr

it "ANY_ADDRESS_POST_03 - Golden tests for enterprise script address - all" $ \ctx -> do
--- $ cat script.txt
--- all [script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a, script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4]
let payload = Json [json|{
"payment": {
"all": [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4"
]
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1w94h4mtdkxr2x68zx4tk0cgmd9hymjgsuhmzaxkg5tkl3scc0g8xj" :: Text
validateAddr r goldenAddr

it "ANY_ADDRESS_POST_04 - Golden tests for enterprise script address - some" $ \ctx -> do
--- $ cat script.txt
--- at_least 2 [script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a,script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4,script_vkh1qw4l62k4203dllrk3dk3sfjpnh3gufhtrtm4qvtrvn4xjp5x5rt]
let payload = Json [json|{
"payment": {
"some": {
"from" : [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4",
"script_vkh1qw4l62k4203dllrk3dk3sfjpnh3gufhtrtm4qvtrvn4xjp5x5rt"
],
"at_least": 2
}
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1wy5np0m5x03tax3kcdh6e2cet98qcfs80wtv4cyvl5taclc6dnd8e" :: Text
validateAddr r goldenAddr

-- Generating golden test data for reward account addresses - script credential:
--- $ cardano-address script hash "$(cat script.txt)" \
--- | cardano-address address stake --from-script --network-tag mainnet
it "ANY_ADDRESS_POST_05 - Golden tests for reward account script address - any" $ \ctx -> do
let payload = Json [json|{
"stake": {
"any": [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4"
]
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"stake17xt2z3pa7etaxp7jurdg0m8jhsmtp4r2z56pd3a5q3jhxycdxzmx9" :: Text
validateAddr r goldenAddr

-- Generating golden test data for reward account addresses - both script credentials:
--- $ cardano-address script hash "$(cat script1.txt)" \
--- | cardano-address address payment --from-script --network-tag mainnet \
--- | cardano-address address delegation --from-script $(cardano-address script hash "$(cat script2.txt)")
it "ANY_ADDRESS_POST_06 - Golden tests for delegating script address - any" $ \ctx -> do
let payload = Json [json|{
"payment": {
"some": {
"from" : [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4",
"script_vkh1qw4l62k4203dllrk3dk3sfjpnh3gufhtrtm4qvtrvn4xjp5x5rt"
],
"at_least": 2
}
},
"stake": {
"any": [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4"
]
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1xy5np0m5x03tax3kcdh6e2cet98qcfs80wtv4cyvl5tacluk59zr\
\majh6vra9cx6slk090pkkr2x59f5zmrmgpr9wvfs37hjk4" :: Text
validateAddr r goldenAddr

-- Generating golden test data for enterprise addresses - key credential:
--- $ cat recovery-phrase.txt
--- east student silly already breeze enact seat trade few way online skin grass humble electric
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/0/0 \
--- > | cardano-address key public --without-chain-code
--- xpub1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwps75l8wa
-- which can be translated in cardano-addresses
-- :set -XOverloadedStrings
-- import Data.Text
-- let k = "xpub1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwps75l8wa" :: Text
-- let (Right bytes) = fromBech32 (const id) (T.encodeUtf8 k)
-- bytes
-- "\248\DC1\244{\194\213\187(4U\244J\ENQ+\SI\SYN\222K\240\148\215\133\163YL\195\197NbK\179\131"
-- let (Right hrp) = Bech32.humanReadablePartFromText "addr_vk"
-- encode (EBech32 hrp) bytes
-- "addr_vk1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwpschzd2j"

-- Golden address can be obtained via
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/0/0 \
--- > | cardano-address key public --with-chain-code \
--- > | cardano-address address payment --from-key --network-tag mainnet
it "ANY_ADDRESS_POST_07 - Golden tests for enterprise pub key address" $ \ctx -> do
let payload = Json [json|{
"payment": "addr_vk1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwpschzd2j"
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1v9qthemrg5kczwfjjnahwt65elhrl95e9hcgufnajtp6wfgknj82e" :: Text
validateAddr r goldenAddr

-- Generating golden test data for enterprise addresses - key credential:
--- $ cat recovery-phrase.txt
--- east student silly already breeze enact seat trade few way online skin grass humble electric
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/2/0 \
--- > | cardano-address key public --without-chain-code
--- xpub16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qh83kg9
-- which can be translated in cardano-addresses
-- :set -XOverloadedStrings
-- import Data.Text
-- let k = "xpub16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qh83kg9" :: Text
-- let (Right bytes) = fromBech32 (const id) (T.encodeUtf8 k)
-- bytes
-- "\215C\220\206e\226\245\n\191\248t~vh\230\235\190?\129O\DC2\139\173pX1\229\182(\ENQ\225<"
-- let (Right hrp) = Bech32.humanReadablePartFromText "stake_vk"
-- encode (EBech32 hrp) bytes
-- "stake_vk16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qau558d"

-- Golden address can be obtained via
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/2/0 \
--- > | cardano-address key public --with-chain-code \
--- > | cardano-address address stake --from-key --network-tag mainnet
it "ANY_ADDRESS_POST_08 - Golden tests for reward account pub key address" $ \ctx -> do
let payload = Json [json|{
"stake": "stake_vk16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qau558d"
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"stake1uy6pmlvyl3wn4ca6807e26gy2gek9hqu0gastzh5tk0xx0g2rxsr5" :: Text
validateAddr r goldenAddr

-- Golden address can be obtained via
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/2/0 \
--- > | cardano-address key public --with-chain-code > stake.xpub

--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/0/0 \
--- > | cardano-address key public --with-chain-code \
--- > | cardano-address address payment --from-key --network-tag mainnet \
--- > | cardano-address address delegation --from-key $(cat stake.xpub)
it "ANY_ADDRESS_POST_09 - Golden tests for delegating address with both pub key credentials" $ \ctx -> do
let payload = Json [json|{
"payment": "addr_vk1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwpschzd2j",
"stake": "stake_vk16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qau558d"
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1q9qthemrg5kczwfjjnahwt65elhrl95e9hcgufnajtp6wff5r\
\h7cflza8t3m5wlaj45sg53nvtwpc73mqk90ghv7vv7s64ryn2" :: Text
validateAddr r goldenAddr

-- Generating golden test data for delegating address - payment from script, stake from pub key:
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/2/0 \
--- > | cardano-address key public --with-chain-code > stake.xpub

--- $ cardano-address script hash "$(cat script.txt)" \
--- | cardano-address address payment --from-script --network-tag mainnet \
--- | cardano-address address delegation --from-key $(cat stake.xpub)
it "ANY_ADDRESS_POST_10 - Golden tests for delegating address - payment from script, stake from key" $ \ctx -> do
let payload = Json [json|{
"payment": {
"some": {
"from" : [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4",
"script_vkh1qw4l62k4203dllrk3dk3sfjpnh3gufhtrtm4qvtrvn4xjp5x5rt"
],
"at_least": 2
}
},
"stake": "stake_vk16apaenn9ut6s40lcw3l8v68xawlrlq20z2966uzcx8jmv2q9uy7qau558d"
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1zy5np0m5x03tax3kcdh6e2cet98qcfs80wtv4cyvl5tacle5rh7cflza8\
\t3m5wlaj45sg53nvtwpc73mqk90ghv7vv7sleajnd" :: Text
validateAddr r goldenAddr

-- Generating golden test data for delegating address - payment from pub key, stake from script:
--- $ cat recovery-phrase.txt | cardano-address key from-recovery-phrase Shelley \
--- > | cardano-address key child 1852H/1815H/0H/0/0 \
--- > | cardano-address key public --with-chain-code \
--- > | cardano-address address payment --from-key --network-tag mainnet \
--- > | cardano-address address delegation --from-script $(cardano-address script hash "$(cat script3.txt)")
it "ANY_ADDRESS_POST_11 - Golden tests for delegating address - payment from key, stake from script" $ \ctx -> do
let payload = Json [json|{
"payment": "addr_vk1lqglg77z6kajsdz4739q22c0zm0yhuy567z6xk2vc0z5ucjtkwpschzd2j",
"stake": {
"some": {
"from" : [
"script_vkh1yf07000d4ml3ywd3d439kmwp07xzgv6p35cwx8h605jfx0dtd4a",
"script_vkh1mwlngj4fcwegw53tdmyemfupen2758xwvudmcz9ap8cnqk7jmh4",
"script_vkh1qw4l62k4203dllrk3dk3sfjpnh3gufhtrtm4qvtrvn4xjp5x5rt"
],
"at_least": 2
}
}
}|]
r <- request @AnyAddress ctx Link.postAnyAddress Default payload
expectResponseCode HTTP.status202 r
let goldenAddr =
"addr1y9qthemrg5kczwfjjnahwt65elhrl95e9hcgufnajtp6wfffxzlhgvlzh\
\6drdsm04j43jk2wpsnqw7uketsgelghm3lsch4g8t" :: Text
validateAddr r goldenAddr
where
validateAddr resp expected = do
let addr = getFromResponse id resp
toJSON addr `shouldBe` object ["address" .= expected ]
3 changes: 2 additions & 1 deletion lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ library
, deepseq
, digest
, directory
, either
, errors
, exceptions
, extra
Expand Down Expand Up @@ -200,7 +201,6 @@ test-suite unit
, cardano-api
, cardano-crypto
, cardano-wallet-core
, ouroboros-consensus
, cardano-wallet-launcher
, cardano-wallet-test-utils
, cardano-slotting
Expand Down Expand Up @@ -232,6 +232,7 @@ test-suite unit
, network-uri
, persistent
, OddWord
, ouroboros-consensus
, QuickCheck
, quickcheck-state-machine >= 0.6.0
, random
Expand Down
11 changes: 10 additions & 1 deletion lib/core/src/Cardano/Wallet/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Cardano.Wallet.Api
, Addresses
, ListAddresses
, InspectAddress
, PostAnyAddress

, CoinSelections
, SelectCoins
Expand Down Expand Up @@ -114,7 +115,9 @@ import Prelude
import Cardano.Wallet
( WalletLayer (..), WalletLog )
import Cardano.Wallet.Api.Types
( ApiAddressIdT
( AnyAddress
, ApiAddressData
, ApiAddressIdT
, ApiAddressInspect
, ApiAddressInspectData
, ApiAddressT
Expand Down Expand Up @@ -317,6 +320,7 @@ type SignMetadata = "wallets"
type Addresses n =
ListAddresses n
:<|> InspectAddress
:<|> PostAnyAddress n

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/listAddresses
type ListAddresses n = "wallets"
Expand All @@ -330,6 +334,11 @@ type InspectAddress = "addresses"
:> Capture "addressId" ApiAddressInspectData
:> Get '[JSON] ApiAddressInspect

-- | https://input-output-hk.github.io/cardano-wallet/api/#operation/postAnyAddress
type PostAnyAddress n = "addresses"
:> ReqBody '[JSON] ApiAddressData
:> PostAccepted '[JSON] AnyAddress

{-------------------------------------------------------------------------------
Coin Selections

Expand Down
2 changes: 2 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ addressClient =
let
_listAddresses
:<|> _inspectAddress
:<|> _postScriptAddress
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small note: the endpoint suggest that we can post any address here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I "coined this name" due to the fact that ANY address could be constructed: base, reward account, enterprise, each with any credential. Of course there is also pointer address which was omitted in this PR (but could be added in the forthcoming)

= client (Proxy @("v2" :> Addresses Aeson.Value))
in
AddressClient
Expand All @@ -336,6 +337,7 @@ byronAddressClient
byronAddressClient =
let
_ :<|> _inspectAddress
:<|> _postScriptAddress
= client (Proxy @("v2" :> Addresses Aeson.Value))

_postRandomAddress
Expand Down
Loading