Skip to content

Commit

Permalink
better code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Sep 15, 2020
1 parent d79aab4 commit ceb2d49
Showing 1 changed file with 39 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2251,47 +2251,54 @@ spec = do
Left err -> error err
Right res -> res

initTx = CardanoTransactions.mkInit CardanoTransactions.Mainnet 7750

unsafeMkInput
:: Word32
-> BS.ByteString
-> CardanoTransactions.Input CardanoTransactions.Shelley
unsafeMkInput ix bs = fromJust $ CardanoTransactions.mkInput ix bs

unsafeMkOutput
:: Natural
-> Text
-> CardanoTransactions.Output CardanoTransactions.Shelley
unsafeMkOutput n str =
fromJust $ CardanoTransactions.mkOutput n (unsafeBech32 str)

fromBech32 :: Text -> Maybe BS.ByteString
fromBech32 txt = do
(_, dp) <- either (const Nothing) Just (Bech32.decodeLenient txt)
Bech32.dataPartToBytes dp

unsafeBech32 :: Text -> BS.ByteString
unsafeBech32 = fromMaybe (error msg) . fromBech32
where msg = "unable to decode bech32 string."

fromBase16 :: Text -> Maybe BS.ByteString
fromBase16 = eitherToMaybe . convertFromBase Base16 . T.encodeUtf8

unsafeB16 :: Text -> BS.ByteString
unsafeB16 = fromMaybe (error msg) . fromBase16
where msg = "unable to decode base16 string."

unsafeMkShelleySignKey
:: Text
-> CardanoTransactions.SignKey CardanoTransactions.Shelley
unsafeMkShelleySignKey str =
fromJust $ CardanoTransactions.mkShelleySignKey (unsafeB16 str)

constructTxFromCardanoTransactions fee txid txix out1 addr1 out2 addr2 wit =
CardanoTransactions.empty initTx
CardanoTransactions.empty (initTx fee)
& CardanoTransactions.addInput (unsafeMkInput txix txid)
& CardanoTransactions.addOutput (unsafeMkOutput out1 addr1)
& CardanoTransactions.addOutput (unsafeMkOutput out2 addr2)
& CardanoTransactions.lock
& CardanoTransactions.signWith (unsafeMkShelleySignKey wit)
& CardanoTransactions.serialize
where
initTx = CardanoTransactions.mkInit CardanoTransactions.Mainnet 7750 fee
unsafeMkInput
:: Word32
-> BS.ByteString
-> CardanoTransactions.Input CardanoTransactions.Shelley
unsafeMkInput ix bs = fromJust $ CardanoTransactions.mkInput ix bs
unsafeMkOutput
:: Natural
-> Text
-> CardanoTransactions.Output CardanoTransactions.Shelley
unsafeMkOutput n str =
fromJust $ CardanoTransactions.mkOutput n (unsafeBech32 str)
fromBech32 :: Text -> Maybe BS.ByteString
fromBech32 txt = do
(_, dp) <- either (const Nothing) Just (Bech32.decodeLenient txt)
Bech32.dataPartToBytes dp
unsafeBech32 :: Text -> BS.ByteString
unsafeBech32 = fromMaybe (error msg) . fromBech32
where msg = "unable to decode bech32 string."
fromBase16 :: Text -> Maybe BS.ByteString
fromBase16 = eitherToMaybe . convertFromBase Base16 . T.encodeUtf8
unsafeB16 :: Text -> BS.ByteString
unsafeB16 = fromMaybe (error msg) . fromBase16
where msg = "unable to decode base16 string."
unsafeMkShelleySignKey
:: Text
-> CardanoTransactions.SignKey CardanoTransactions.Shelley
unsafeMkShelleySignKey str =
fromJust $ CardanoTransactions.mkShelleySignKey (unsafeB16 str)

constructMultiTxFromCardanoTransactions fee txid (txix1, txix2) out1 addr1 out2 addr2 wit1 wit2 =
CardanoTransactions.empty initTx
CardanoTransactions.empty (initTx fee)
& CardanoTransactions.addInput (unsafeMkInput txix1 txid)
& CardanoTransactions.addInput (unsafeMkInput txix2 txid)
& CardanoTransactions.addOutput (unsafeMkOutput out1 addr1)
Expand All @@ -2300,33 +2307,3 @@ spec = do
& CardanoTransactions.signWith (unsafeMkShelleySignKey wit1)
& CardanoTransactions.signWith (unsafeMkShelleySignKey wit2)
& CardanoTransactions.serialize
where
initTx = CardanoTransactions.mkInit CardanoTransactions.Mainnet 7750 fee
unsafeMkInput
:: Word32
-> BS.ByteString
-> CardanoTransactions.Input CardanoTransactions.Shelley
unsafeMkInput ix bs = fromJust $ CardanoTransactions.mkInput ix bs
unsafeMkOutput
:: Natural
-> Text
-> CardanoTransactions.Output CardanoTransactions.Shelley
unsafeMkOutput n str =
fromJust $ CardanoTransactions.mkOutput n (unsafeBech32 str)
fromBech32 :: Text -> Maybe BS.ByteString
fromBech32 txt = do
(_, dp) <- either (const Nothing) Just (Bech32.decodeLenient txt)
Bech32.dataPartToBytes dp
unsafeBech32 :: Text -> BS.ByteString
unsafeBech32 = fromMaybe (error msg) . fromBech32
where msg = "unable to decode bech32 string."
fromBase16 :: Text -> Maybe BS.ByteString
fromBase16 = eitherToMaybe . convertFromBase Base16 . T.encodeUtf8
unsafeB16 :: Text -> BS.ByteString
unsafeB16 = fromMaybe (error msg) . fromBase16
where msg = "unable to decode base16 string."
unsafeMkShelleySignKey
:: Text
-> CardanoTransactions.SignKey CardanoTransactions.Shelley
unsafeMkShelleySignKey str =
fromJust $ CardanoTransactions.mkShelleySignKey (unsafeB16 str)

0 comments on commit ceb2d49

Please sign in to comment.