Skip to content

Commit

Permalink
Separate concern between keyToAddress and get/putAddress
Browse files Browse the repository at this point in the history
Previously:
    `getAddress :: ByteString 33 bytes -> Address 32 bytes`
    `putAddress :: Address 32 bytes -> ByteString 33 bytes`
Now:
    `getAddress :: ByteString 33 bytes -> Address 33 bytes`
    `putAddress Address 33 bytes -> ByteString 33 bytes`
     **Implemented** `keyToAddress :: XPub 32 bytes -> Address 33 bytes`
  • Loading branch information
Anviking committed Jun 16, 2019
1 parent 1b3ff29 commit ba99b3d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ library
, binary
, bytestring
, cardano-wallet-core
, cardano-crypto
, cborg
, exceptions
, http-client
Expand Down
60 changes: 35 additions & 25 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- Copyright: © 2018-2019 IOHK
Expand All @@ -20,6 +19,8 @@ module Cardano.Wallet.Jormungandr.Binary
, Message (..)
, getBlockHeader
, getBlock
, getAddress
, getTransaction

, putTransaction

Expand All @@ -44,7 +45,7 @@ module Cardano.Wallet.Jormungandr.Binary
import Prelude

import Cardano.Wallet.Jormungandr.Environment
( KnownNetwork (..), Network (..) )
( Network (..) )
import Cardano.Wallet.Primitive.Types
( Address (..)
, Coin (..)
Expand All @@ -68,6 +69,7 @@ import Data.Binary.Get
, isEmpty
, isolate
, label
, lookAhead
, runGet
, skip
)
Expand All @@ -77,8 +79,6 @@ import Data.Bits
( shift, (.&.) )
import Data.ByteString
( ByteString )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Word
Expand All @@ -87,6 +87,7 @@ import Data.Word
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Codec.CBOR.Decoding as CBOR
import qualified Codec.CBOR.Read as CBOR
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL

data BlockHeader = BlockHeader
Expand Down Expand Up @@ -224,10 +225,9 @@ getTransaction = label "getTransaction" $ isolate 43 $ do
error "unimplemented: Account witness"
other -> fail $ "Invalid witness type: " ++ show other


putTransaction :: forall n. KnownNetwork n => Proxy n -> (Tx, [TxWitness]) -> Put
putTransaction _ (tx, witnesses) = do
putTokenTransfer (Proxy @n) tx
putTransaction :: (Tx, [TxWitness]) -> Put
putTransaction (tx, witnesses) = do
putTokenTransfer tx
mapM_ putWitness witnesses

putWitness :: TxWitness -> Put
Expand All @@ -252,8 +252,8 @@ putWitness witness =
Common Structure
-------------------------------------------------------------------------------}

putTokenTransfer :: forall n. KnownNetwork n => Proxy n -> Tx -> Put
putTokenTransfer _ (Tx inputs outputs) = do
putTokenTransfer :: Tx -> Put
putTokenTransfer (Tx inputs outputs) = do
putWord8 $ fromIntegral $ length inputs
putWord8 $ fromIntegral $ length outputs
mapM_ putInput inputs
Expand All @@ -267,10 +267,6 @@ putTokenTransfer _ (Tx inputs outputs) = do
putOutput (TxOut address coin) = do
putAddress address
putWord64be $ getCoin coin
putAddress address = do
-- NOTE: only single address supported for now
putWord8 (single @n)
putByteString $ getAddress address

getTokenTransfer :: Get ([TxIn], [TxOut])
getTokenTransfer = label "getTokenTransfer" $ do
Expand All @@ -291,18 +287,23 @@ getTokenTransfer = label "getTokenTransfer" $ do
value <- Coin <$> getWord64be
return $ TxOut addr value

getAddress = do
headerByte <- getWord8
let kind = kindValue headerByte
let _discrimination = discriminationValue headerByte
case kind of
-- Single Address
0x3 -> Address <$> getByteString 32
0x4 -> error "unimplemented group address decoder"
0x5 -> error "unimplemented account address decoder"
0x6 -> error "unimplemented multisig address decoder"
other -> fail $ "Invalid address type: " ++ show other


getAddress :: Get Address
getAddress = do
-- We use 'lookAhead' to not consume the header, and let it
-- be included in the underlying Address ByteString.
headerByte <- label "address header" . lookAhead $ getWord8
let kind = kindValue headerByte
let _discrimination = discriminationValue headerByte
case kind of
-- Single Address
0x3 -> Address <$> getByteString 33
0x4 -> error "unimplemented group address decoder"
0x5 -> error "unimplemented account address decoder"
0x6 -> error "unimplemented multisig address decoder"
other -> fail $ "Invalid address type: " ++ show other
where
kindValue :: Word8 -> Word8
kindValue = (.&. 0b01111111)

Expand All @@ -311,6 +312,15 @@ getTokenTransfer = label "getTokenTransfer" $ do
0 -> Mainnet
_ -> Testnet

putAddress :: Address -> Put
putAddress addr@(Address bs)
| l == 33 = putByteString bs -- bootstrap or account addr
| l == 65 = putByteString bs -- delegation addr
| otherwise = fail
$ "Address have unexpected length "
++ (show l)
++ ": " ++ show addr
where l = BS.length bs

{-------------------------------------------------------------------------------
Config Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ genesisBlock = Block genesisHeader
{ inputs = []
, outputs =
[ TxOut
{ address = Address "3$\195xi\193\"h\154\&5\145}\245:O\"\148\163\165/h^\ENQ\245\248\229;\135\231\234E/"
{ address = Address "\131\&3$\195xi\193\"h\154\&5\145}\245:O\"\148\163\165/h^\ENQ\245\248\229;\135\231\234E/"
, coin = Coin 14
}
]
Expand Down

0 comments on commit ba99b3d

Please sign in to comment.