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

Fix address prefixes on testnet #2273

Merged
merged 4 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/core-integration/cardano-wallet-core-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ library
, http-client
, http-types
, iohk-monitoring
, lens-aeson
, memory
, optparse-applicative
, process
Expand Down
15 changes: 10 additions & 5 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ import Data.Functor.Identity
( Identity (..) )
import Data.Generics.Internal.VL.Lens
( Lens', lens, set, view, (^.) )
import Data.Generics.Internal.VL.Prism
( (^?) )
import Data.Generics.Internal.VL.Traversal
( Traversal' )
import Data.Generics.Labels
()
import Data.Generics.Product.Typed
Expand Down Expand Up @@ -406,20 +410,21 @@ expectResponseCode want (got, a) =

expectField
:: (HasCallStack, MonadIO m, Show a)
=> Lens' s a
=> Traversal' s a
-> (a -> Expectation)
-> (HTTP.Status, Either RequestException s)
-> m ()
expectField getter predicate (_, res) = case res of
Left e -> wantedSuccessButError e
Right s ->
let a = view getter s in
liftIO $ predicate a
Right s -> liftIO $ case s ^? getter of
Nothing -> expectationFailure
"could not traverse response to test expectations"
Just a -> predicate a

expectListField
:: (HasCallStack, MonadIO m, Show a)
=> Int
-> Lens' s a
-> Traversal' s a
-> (a -> Expectation)
-> (HTTP.Status, Either RequestException [s])
-> m ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Data.Generics.Internal.VL.Lens
import Data.Quantity
( Quantity (..) )
import Test.Hspec
( SpecWith, describe, shouldBe )
( SpecWith, describe, shouldBe, shouldSatisfy )
import Test.Hspec.Extra
( it )
import Test.Integration.Framework.DSL
Expand Down Expand Up @@ -62,6 +62,7 @@ import Test.Integration.Framework.TestData

import qualified Cardano.Wallet.Api.Link as Link
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Lens as Aeson
import qualified Data.Text as T
import qualified Network.HTTP.Types.Status as HTTP

Expand Down Expand Up @@ -226,6 +227,17 @@ spec = describe "SHELLEY_ADDRESSES" $ do
expectResponseCode @IO HTTP.status404 r
expectErrorMessage (errMsg404NoWallet $ w ^. walletId) r

it "ADDRESS_LIST_05 - bech32 HRP is correct - mainnet" $ \ctx -> do
w <- emptyWallet ctx
r <- request @[Aeson.Value] ctx
(Link.listAddresses @'Shelley w) Default Empty
verify r
[ expectResponseCode @IO HTTP.status200
-- integration tests are configured for mainnet
, expectListField 0 (Aeson.key "id" . Aeson._String)
(`shouldSatisfy` T.isPrefixOf "addr")
]

it "ADDRESS_INSPECT_01 - Address inspect OK" $ \ctx -> do
let str = "Ae2tdPwUPEYz6ExfbWubiXPB6daUuhJxikMEb4eXRp5oKZBKZwrbJ2k7EZe"
r <- request @Aeson.Value ctx (Link.inspectAddress str) Default Empty
Expand Down
10 changes: 5 additions & 5 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -985,20 +985,20 @@ _decodeStakeAddress serverNetwork txt = do
"Unable to decode stake-address: must be a valid bech32 string."

instance EncodeAddress 'Mainnet where
encodeAddress = _encodeAddress
encodeAddress = _encodeAddress [Bech32.humanReadablePart|addr|]

instance EncodeAddress ('Testnet pm) where
encodeAddress = _encodeAddress
-- https://github.com/cardano-foundation/CIPs/tree/master/CIP5
encodeAddress = _encodeAddress [Bech32.humanReadablePart|addr_test|]

_encodeAddress :: W.Address -> Text
_encodeAddress (W.Address bytes) =
_encodeAddress :: Bech32.HumanReadablePart -> W.Address -> Text
_encodeAddress hrp (W.Address bytes) =
if isJust (CBOR.deserialiseCbor CBOR.decodeAddressPayload bytes)
then base58
else bech32
where
base58 = T.decodeUtf8 $ encodeBase58 bitcoinAlphabet bytes
bech32 = Bech32.encodeLenient hrp (dataPartFromBytes bytes)
hrp = [Bech32.humanReadablePart|addr|]

instance DecodeAddress 'Mainnet where
decodeAddress = _decodeAddress SL.Mainnet
Expand Down
11 changes: 11 additions & 0 deletions lib/shelley/test/unit/Cardano/Wallet/Shelley/CompatibilitySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ spec = do
& counterexample (show $ base58 bytes)
]

prop "Shelley addresses from bech32 - testnet" $ \k ->
let addr@(Address raw) = paymentAddress @('Testnet 0) @ShelleyKey k
in decodeAddress @('Testnet 0) (bech32testnet raw) === Right addr
& counterexample (show $ bech32testnet raw)

prop "Byron addresses from base16, bech32 and base58" $ \k -> do
let addr@(Address bytes) = paymentAddress @'Mainnet @ByronKey k
conjoin
Expand Down Expand Up @@ -378,5 +383,11 @@ bech32 :: ByteString -> Text
bech32 = Bech32.encodeLenient hrp . Bech32.dataPartFromBytes
where hrp = [humanReadablePart|addr|]

-- Expected bech32 encoding for testnets
-- https://github.com/cardano-foundation/CIPs/tree/master/CIP5
bech32testnet :: ByteString -> Text
bech32testnet = Bech32.encodeLenient hrp . Bech32.dataPartFromBytes
where hrp = [humanReadablePart|addr_test|]

base58 :: ByteString -> Text
base58 = T.decodeUtf8 . encodeBase58 bitcoinAlphabet
1 change: 1 addition & 0 deletions nix/.stack.nix/cardano-wallet-core-integration.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.