Skip to content

Commit

Permalink
Add initial support for serializing signed transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
akegalj committed Jun 4, 2019
1 parent e679901 commit e28018d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/jormungandr/src/Cardano/Wallet/Jormungandr/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Prelude
import Cardano.Wallet.Jormungandr.Binary
( FromBinary (..), runGet )
import Cardano.Wallet.Primitive.Types
( Block, Hash (..) )
( Block, Hash (..), Tx, TxWitness )
import Data.Binary.Get
( getByteString )
import Data.ByteArray.Encoding
Expand Down Expand Up @@ -96,12 +96,11 @@ type GetTipId
type PostSignedTx
= "api"
:> "v0"
:> "transaction"
:> "message"
:> ReqBody '[JormungandrBinary] SignedTx
:> Post '[NoContent] NoContent

-- TODO: Replace SignedTx with something real
data SignedTx
type SignedTx = (Tx, [TxWitness])

newtype BlockId = BlockId (Hash "block")
deriving Show
Expand Down
46 changes: 45 additions & 1 deletion lib/jormungandr/src/Cardano/Wallet/Jormungandr/Binary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module Cardano.Wallet.Jormungandr.Binary
import Prelude

import Cardano.Wallet.Jormungandr.Environment
( Network (..) )
( Network (..), single )
import Cardano.Wallet.Primitive.Types
( Address (..)
, Coin (..)
Expand All @@ -66,6 +66,8 @@ import Data.Binary.Get
, runGet
, skip
)
import Data.Binary.Put
( Put, putByteString, putWord64be, putWord8 )
import Data.Bits
( shift, (.&.) )
import Data.ByteString
Expand Down Expand Up @@ -200,10 +202,50 @@ getTransaction = isolate 43 $ do
other -> fail $ "Invalid witness type: " ++ show other


putTransaction :: (Tx, [TxWitness]) -> Put
putTransaction (tx, witnesses) = do
putTokenTransfer tx
mapM_ putWitness witnesses

putWitness :: TxWitness -> Put
putWitness witness =
case witness of
PublicKeyWitness xPub (Hash sig) -> do
putWord8 1
putByteString xPub
putByteString sig
-- TODO: note that we are missing new address type witness:
-- * https://github.com/input-output-hk/rust-cardano/blob/3524cfe138a10773caa6f0effacf69e792f915df/chain-impl-mockchain/doc/format.md#witnesses
-- * https://github.com/input-output-hk/rust-cardano/blob/e0616f13bebd6b908320bddb1c1502dea0d3305a/chain-impl-mockchain/src/transaction/witness.rs#L23
ScriptWitness _ -> error "unimplemented: serialize script witness"
RedeemWitness _ -> error "unimplemented: serialize redeem witness"


{-------------------------------------------------------------------------------
Common Structure
-------------------------------------------------------------------------------}

putTokenTransfer :: Tx -> Put
putTokenTransfer (Tx inputs outputs) = do
putWord8 $ fromIntegral $ length inputs
putWord8 $ fromIntegral $ length outputs
mapM_ putInput inputs
mapM_ putOutput outputs
where
putInput (TxIn inputId inputIx) = do
-- NOTE: special value 0xff indicates account spending
-- only old utxo/address scheme supported for now
putWord8 $ fromIntegral inputIx
putByteString $ getHash inputId
putOutput (TxOut address coin) = do
putAddress address
putWord64be $ getCoin coin
putAddress address = do
-- NOTE: only single address supported for now
putWord8 single
-- TODO(akegalj) this should be `encodeAddress (Proxy Jormungandr) address`
putByteString "addressBytestring" -- address

getTokenTransfer :: Get ([TxIn], [TxOut])
getTokenTransfer = do
inCount <- fromIntegral <$> getWord8
Expand All @@ -230,6 +272,7 @@ getTokenTransfer = do
let _discrimination = discriminationValue headerByte
case kind of
-- Single Address
-- TODO(akegalj) shouldn't this be `either fail id . decodeAddress (Proxy Jormungandr) <$> getByteString 32` ?
0x3 -> Address <$> getByteString 32
0x4 -> error "unimplemented group address decoder"
0x5 -> error "unimplemented account address decoder"
Expand All @@ -244,6 +287,7 @@ getTokenTransfer = do
0 -> Mainnet
_ -> Testnet


{-------------------------------------------------------------------------------
Config Parameters
-------------------------------------------------------------------------------}
Expand Down

0 comments on commit e28018d

Please sign in to comment.