Skip to content

Commit

Permalink
adjust tests to use backend target for Api types carrying an address
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed May 27, 2019
1 parent 7abf4af commit 10ea57b
Show file tree
Hide file tree
Showing 22 changed files with 3,545 additions and 136 deletions.
2 changes: 1 addition & 1 deletion exe/wallet/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ exec manager args
:<|> _ -- Put Wallet Passphrase
)
:<|> createTransaction
= client (Proxy @("v2" :> Api))
= client (Proxy @("v2" :> Api HttpBridge))

-- | 'runClient' requires a type-application to carry a particular
-- namespace and adjust error messages accordingly. For instances, when
Expand Down
59 changes: 35 additions & 24 deletions lib/core/src/Cardano/Wallet/Primitive/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ module Cardano.Wallet.Primitive.Types
, AddressState (..)
, EncodeAddress (..)
, DecodeAddress (..)
, toBase58
, fromBase58

-- * Coin
, Coin (..)
Expand Down Expand Up @@ -108,8 +106,6 @@ import Data.ByteArray.Encoding
( Base (Base16), convertFromBase, convertToBase )
import Data.ByteString
( ByteString )
import Data.ByteString.Base58
( bitcoinAlphabet, decodeBase58, encodeBase58 )
import Data.Map.Strict
( Map )
import Data.Proxy
Expand Down Expand Up @@ -482,14 +478,47 @@ data TxWitness

-- | Representation of Cardano addresses. Addresses are basically a
-- human-friendly representation of public keys. Historically in Cardano, there
-- exists different sort of addresses, and new one are to come. So far, we can
-- exists different sort of addresses, and new ones are to come. So far, we can
-- distinguish between three types of addresses:
--
-- - Byron Random addresses, which holds a payload with derivation path details
-- - Byron Sequential addresses, also known as Icarus'style addresses
-- - Shelley base addresses, see also [implementation-decisions/address](https://github.com/input-output-hk/implementation-decisions/blob/master/text/0001-address.md)
--
-- For more details, see [About Address Derivation](https://github.com/input-output-hk/cardano-wallet/wiki/About-Address-Derivation)
-- For more details, see also [About Address Derivation](https://github.com/input-output-hk/cardano-wallet/wiki/About-Address-Derivation)
--
-- Shelley base addresses can be declined into two types:
--
-- - Single Addresses: which only holds a public spending key
-- - Group Addresses: which hold both a spending and delegation keys
--
-- It'll therefore seem legitimate to represent addresses as:
--
-- @
-- data Address
-- = ByronAddress !ByteString
-- | SingleAddress !XPub
-- | GroupAddress !XPub XPub
-- @
--
-- However, there's a major drawback to this approach: we have to consider all
-- three constructors everywhere, and make sure we test every function using
-- them three despite having no need for such fine-grained representation.
--
-- Indeed, from the wallet core code, addresses are nothing more than an opaque
-- bunch of bytes that can be compared with each others. When signing
-- transactions, we have to lookup addresses anyway and therefore, can re-derive
-- their corresponding public keys. The only moment the distinction between
-- address type matters is when it comes to representing addresses at the edge
-- of the application (the API layer). And here, this is precisely where we need
-- to also what target backend we're connected to. Different backends use
-- different encodings which may not be compatible.
--
-- Therefore, for simplicity, it's easier to consider addresses as "bytes", and
-- only peak into these bytes whenever we need to do something with them. This
-- makes it fairly clear that addresses are just an opaque string for the wallet
-- layer and that the underlying encoding is rather agnostic to the underlying
-- backend.
newtype Address = Address
{ getAddress :: ByteString
} deriving (Show, Generic, Eq, Ord)
Expand Down Expand Up @@ -521,24 +550,6 @@ class EncodeAddress t where
class DecodeAddress t where
decodeAddress :: Proxy t -> Text -> Either TextDecodingError Address

-- | Encode an address to [Base58](https://en.wikipedia.org/wiki/Base58)
--
-- >>> toBase58 <TODO>
toBase58 :: Address -> Text
toBase58 = T.decodeUtf8 . encodeBase58 bitcoinAlphabet . getAddress

-- | Decode a [Base58](https://en.wikipedia.org/wiki/Base58) text string to an
-- 'Address'
--
-- >>> fromBase58 <TODO>
fromBase58 :: Text -> Either TextDecodingError Address
fromBase58 x = maybe
(Left $ TextDecodingError err)
(pure . Address)
(decodeBase58 bitcoinAlphabet $ T.encodeUtf8 x)
where
err = "Unable to decode Address: expected Base58 encoding"

-- | Denotes if an address has been previously used or not... whether that be
-- in the output of a transaction on the blockchain or one in our pending set.
data AddressState = Used | Unused
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"seed": -6401489019863294339,
"samples": [
"3078673572235a75794d6c4a582c63081e696a454a576912353208487c457d32394d1fc003663d6a3ab9a276185275521122",
"157a5e053d96255a164100515768668038322e7a474f17245398341656f4597b7ade068c31950d4942265a961357a0268821",
"4c292843e90c1c442960dbe9373037323c573815c71d295b77083f23e0641c102a5a514f6f2c7827308f27177e147c250a4c",
"2a21090f55ec6c502a202534740b7976111635606b1fb753484c73cd6a0d7820602d7144156b75152ab74a44736a3b0f3458",
"673d4b6e57576f241d0572346e7f0076eb340566d21e112f58f063466c1c02283d4125282d105d310424195c562f084c524f",
"135a1e564fe177e6213b5d0221f602fcf11e212bfe40132c3850241a2349347d524340401d721a4d010d4e06343949441728",
"68c05200ac2b4984455316d354fb3f0d2747385b72487f4b3b5876126a04396a6186223e11786b3d497eb805351a10200226",
"01003b5f0a5a27db2a0025d255625372120d3ad01c1735766f0f170bd15a47522f0002710e3518103d6f23640c7d0569680f",
"644b2923690207613328266839e1c82634944e7603750d5cbd7ac758335106f3620a4e623814ef2643434d720a0d34541829",
"d03033012a2204030d892d40453b71320f5f373d4915621008374c2b24203f4a231969efb38cd70b6b351e3c1835052b1d7c"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"seed": -1648854074368940312,
"samples": [
{
"amount": {
"quantity": 99,
"unit": "lovelace"
},
"address": "035ee700af2fb843576ceb3d70016e351b3e373925af35626a1f1e423a5c13180051fe29677b76104b25241cc2630f293fa3"
},
{
"amount": {
"quantity": 232,
"unit": "lovelace"
},
"address": "075e65b7505b4454101a01137722087e622a1be14c2f45482d5e2e56733c2d595ee5eb057a12c3ab4eb1771c36714b3b4f68"
},
{
"amount": {
"quantity": 56,
"unit": "lovelace"
},
"address": "18477f252374d70b411d782d4f364c537fe759915c763b927af9c7b25c652f67754f461a1d5b0156167fed2c4c094f730156"
},
{
"amount": {
"quantity": 107,
"unit": "lovelace"
},
"address": "73006f24666d3772746f2d5602461277006119241d1ca56a6e25120246715a1a1079020825cd367d7fd53f8d78687e13c792"
},
{
"amount": {
"quantity": 111,
"unit": "lovelace"
},
"address": "3e14f66e0d1f05dc1467677932155b6f000b224a406b66354c7b152671467a5c085730282492693f196c00de50c714280a13"
},
{
"amount": {
"quantity": 166,
"unit": "lovelace"
},
"address": "1e77532a097037be71572d4f1066137b3e361d2fca00574b30e35c56753f1a250e7c3169aa0c358a043d402a044a611c682c"
},
{
"amount": {
"quantity": 219,
"unit": "lovelace"
},
"address": "5247f06c251ecb2317dcc76b98601f07211733dd09b16413050625f95d0a240a394c1eec3a7d0514787d78214546717b3bcc"
},
{
"amount": {
"quantity": 102,
"unit": "lovelace"
},
"address": "44520335285e44112a2a0e2460032fc73f8e40132b1c40675d26266e621a2b43e22e5e2615150a3c6b02581ea842fb38633a"
},
{
"amount": {
"quantity": 65,
"unit": "lovelace"
},
"address": "0a630818714d48321db47a0b7a6e5c73530f6e0f17349f3472873bac712c1f6e3d795c4d707e7126b23a4c046c291678650e"
},
{
"amount": {
"quantity": 162,
"unit": "lovelace"
},
"address": "2b68786055291b25d769797862694d2e205b42190e001d79e963616557676c47b867241856314a3e172a7e34711cf35c7529"
}
]
}
45 changes: 45 additions & 0 deletions lib/core/test/data/Cardano/Wallet/Api/ApiAddress DummyTarget.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"seed": -1251489921389975991,
"samples": [
{
"state": "unused",
"id": "5616516b6f46ab7343af500372033355bf144d44080b297e0121c7785a542869284e1d1f2e677b59b74d2a1f39693067e26c"
},
{
"state": "unused",
"id": "1a7439643717215305670f574714461f85651edc16473c42716e137c49652b3b4e546e2ca6f6102513106600049956441e05"
},
{
"state": "unused",
"id": "254b2d5b24ee7503406f0a5d497a27780d3f9577b0707535431b49514702c65617421e26640b44d6056c13d14d582c2e4c91"
},
{
"state": "used",
"id": "22da1959f079423865587f65046a02c14131017b2d0f3b5b4e523a6d00663b1e4322062c0b22312d40323f64be5c2d322f20"
},
{
"state": "used",
"id": "585c501126f3952c0b4e74e697cd652509ae33763b608d167c7a5c533fd5046d16283642b95212216c793c3d542f08466d5a"
},
{
"state": "used",
"id": "c05b1dc53007241b01974a1dc628eed1412456ce6010e93774314a0a664c17506f4b6231017a030c343b70060e184b6b6760"
},
{
"state": "unused",
"id": "0f4f08495a166fbf5f67023b53f55c324b0963561b2c3d24fe3f5467992ba521230e703964158d34126e0c05032ad5405d03"
},
{
"state": "used",
"id": "63673c6e1a3e306e4b740877783657593608460d6841789d3b676a350c4057026949193c35010b5d8579313aec943e74762a"
},
{
"state": "unused",
"id": "49631b62520a2b674480dc9d67961d7b760901151f6d4b46551826991509cd6630087761634c228b5b0971296a224710710a"
},
{
"state": "used",
"id": "48662703424b9b737afa0e0f7a54135b3bf7301d150e71d857526a455a1e6a083f244e1b021f0a5f6a1b6a02295803495632"
}
]
}
Loading

0 comments on commit 10ea57b

Please sign in to comment.