-
Notifications
You must be signed in to change notification settings - Fork 213
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
Support for legacy UTxO witness in Jörmungandr #878
Changes from all commits
8240593
89a23ca
f708d2f
9fbf189
6af04d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,15 @@ module Cardano.Wallet.Jormungandr.Binary | |
, putSignedTx | ||
, putTx | ||
|
||
-- * Transaction witnesses | ||
, signData | ||
, utxoWitness | ||
, legacyUtxoWitness | ||
, TxWitnessTag (..) | ||
, putTxWitnessTag | ||
, getTxWitnessTag | ||
, txWitnessSize | ||
|
||
-- * Purification of chain block types | ||
, convertBlock | ||
, convertBlockHeader | ||
|
@@ -48,7 +57,7 @@ module Cardano.Wallet.Jormungandr.Binary | |
, getAddress | ||
, singleAddressFromKey | ||
|
||
-- * Legacy Decoders | ||
-- * Legacy | ||
, decodeLegacyAddress | ||
|
||
-- * Helpers | ||
|
@@ -57,7 +66,6 @@ module Cardano.Wallet.Jormungandr.Binary | |
, fragmentId | ||
, maxNumberOfInputs | ||
, maxNumberOfOutputs | ||
, signData | ||
, withHeader | ||
|
||
-- * Re-export | ||
|
@@ -71,7 +79,7 @@ module Cardano.Wallet.Jormungandr.Binary | |
import Prelude | ||
|
||
import Cardano.Crypto.Wallet | ||
( XPub (xpubPublicKey) ) | ||
( XPub (xpubPublicKey), unXPub ) | ||
import Cardano.Wallet.Jormungandr.Environment | ||
( KnownNetwork, Network (..), single ) | ||
import Cardano.Wallet.Jormungandr.Primitive.Types | ||
|
@@ -266,6 +274,21 @@ data TxWitnessTag | |
| TxWitnessMultisig | ||
deriving (Show, Eq) | ||
|
||
putTxWitnessTag :: TxWitnessTag -> Put | ||
putTxWitnessTag = \case | ||
TxWitnessLegacyUTxO -> putWord8 0 | ||
TxWitnessUTxO -> putWord8 1 | ||
TxWitnessAccount -> putWord8 2 | ||
TxWitnessMultisig -> putWord8 3 | ||
|
||
getTxWitnessTag :: Get TxWitnessTag | ||
getTxWitnessTag = getWord8 >>= \case | ||
0 -> pure TxWitnessLegacyUTxO | ||
1 -> pure TxWitnessUTxO | ||
2 -> pure TxWitnessAccount | ||
3 -> pure TxWitnessMultisig | ||
other -> fail $ "Invalid witness type: " ++ show other | ||
|
||
-- | Decode a message (header + contents). | ||
getMessage :: Get Message | ||
getMessage = label "getMessage" $ do | ||
|
@@ -305,6 +328,22 @@ txWitnessSize = \case | |
TxWitnessAccount -> 64 | ||
TxWitnessMultisig -> 68 | ||
|
||
txWitnessTagSize :: Int | ||
txWitnessTagSize = 1 | ||
|
||
-- | Construct a UTxO witness from a signature | ||
utxoWitness :: ByteString -> TxWitness | ||
utxoWitness bytes = TxWitness $ BL.toStrict $ runPut $ do | ||
putTxWitnessTag TxWitnessUTxO | ||
putByteString bytes | ||
|
||
-- | Construct a legacy UTxO witness from a public key and a signature | ||
legacyUtxoWitness :: XPub -> ByteString -> TxWitness | ||
legacyUtxoWitness xpub bytes = TxWitness $ BL.toStrict $ runPut $ do | ||
putTxWitnessTag TxWitnessLegacyUTxO | ||
putByteString (unXPub xpub) | ||
putByteString bytes | ||
|
||
-- | Decode the contents of a @Transaction@-message. | ||
getTransaction :: Int -> Get (Tx, [TxWitness]) | ||
getTransaction n = label "getTransaction" $ do | ||
|
@@ -319,20 +358,12 @@ getTransaction n = label "getTransaction" $ do | |
where | ||
getWitness :: Get TxWitness | ||
getWitness = do | ||
tag <- getTxWitnessTag | ||
let len = txWitnessSize tag | ||
tag <- lookAhead getTxWitnessTag | ||
let len = txWitnessSize tag + txWitnessTagSize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tag is now part of the bytes themselves (cf utxoWitness and legacyUtxoWitness) |
||
-- NOTE: Regardless of the type of witness, we decode it as a | ||
-- @TxWitness@. | ||
TxWitness <$> isolate len (getByteString len) | ||
|
||
getTxWitnessTag :: Get TxWitnessTag | ||
getTxWitnessTag = getWord8 >>= \case | ||
0 -> pure TxWitnessLegacyUTxO | ||
1 -> pure TxWitnessUTxO | ||
2 -> pure TxWitnessAccount | ||
3 -> pure TxWitnessMultisig | ||
other -> fail $ "Invalid witness type: " ++ show other | ||
|
||
getTokenTransfer :: Get ([(TxIn, Coin)], [TxOut]) | ||
getTokenTransfer = label "getTokenTransfer" $ do | ||
inCount <- fromIntegral <$> getWord8 | ||
|
@@ -362,9 +393,7 @@ putSignedTx inputs outputs witnesses = do | |
where | ||
-- Assumes the `TxWitness` has been faithfully constructed | ||
Anviking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
putWitness :: TxWitness -> Put | ||
putWitness (TxWitness bytes) = do | ||
putWord8 1 | ||
putByteString bytes | ||
putWitness (TxWitness bytes) = putByteString bytes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tag is now part of the |
||
|
||
putTx :: [(TxIn, Coin)] -> [TxOut] -> Put | ||
putTx inputs outputs = do | ||
|
@@ -601,7 +630,7 @@ estimateMaxNumberOfInputsParams = EstimateMaxNumberOfInputsParams | |
, estBlockHashSize = 32 | ||
|
||
-- The length of the smallest type of witness. | ||
, estTxWitnessSize = txWitnessSize TxWitnessUTxO | ||
, estTxWitnessSize = txWitnessSize TxWitnessUTxO + txWitnessTagSize | ||
} | ||
|
||
-- | Jörmungandr distinguish 'fragment id' (what we commonly call 'txId') | ||
|
@@ -652,7 +681,7 @@ convertBlockHeader h = (W.BlockHeader (slot h) (bh h) (Hash "") (Hash "")) | |
bh = Quantity . fromIntegral . chainLength | ||
|
||
{------------------------------------------------------------------------------- | ||
Legacy Decoders | ||
Legacy | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | Attempt decoding a 'ByteString' into an 'Address'. This merely checks that | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do the transaction layers need to be different?newTransactionLayer
is passed the same arguments.Ah, right! The implicit key type-param!