Skip to content

Commit

Permalink
try makeBootstrapWitness
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jul 3, 2020
1 parent 80abe15 commit 27d526f
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -26,10 +26,8 @@ module Cardano.Wallet.Shelley.Transaction
-- * Internals
, _minimumFee
, _decodeSignedTx
, _mkStdByronTx
, _estimateMaxNumberOfInputs
, mkUnsignedShelleyTx
, mkUnsignedByronTx
, mkShelleyWitness
, mkByronWitness
, realFee
Expand Down Expand Up @@ -76,6 +74,7 @@ import Cardano.Wallet.Shelley.Compatibility
( Shelley
, TPraosStandardCrypto
, toCardanoLovelace
, toCardanoNetwork
, toCardanoTxIn
, toCardanoTxOut
, toSealed
Expand Down Expand Up @@ -118,7 +117,7 @@ import Ouroboros.Network.Block

import qualified Cardano.Api as Cardano
import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Crypto as Byron
import qualified Cardano.Crypto as Crypto
import qualified Cardano.Crypto.Hash.Class as Hash
import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet.Primitive.Types as W
Expand All @@ -131,6 +130,7 @@ import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Shelley.Spec.Ledger.Address.Bootstrap as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL
import qualified Shelley.Spec.Ledger.Keys as SL
import qualified Shelley.Spec.Ledger.LedgerState as SL
Expand Down Expand Up @@ -180,19 +180,26 @@ newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer
-> [(TxIn, TxOut)]
-> [TxOut]
-> Either ErrMkTx (Tx, SealedTx)
_mkStdTx keyFrom slot ownedIns outs = case (txWitnessTagFor @k) of
TxWitnessShelleyUTxO -> do
_mkStdTx keyFrom slot ownedIns outs = do
let timeToLive = defaultTTL epochLength slot
let fee = realFee ownedIns outs
let unsigned = mkUnsignedShelleyTx timeToLive ownedIns outs [] fee
addrWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let metadata = SL.SNothing
let wits = SL.WitnessSet addrWits mempty mempty
wits <- case (txWitnessTagFor @k) of
TxWitnessShelleyUTxO -> do
addrWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
pure $ SL.WitnessSet addrWits mempty mempty
TxWitnessByronUTxO -> do
let toSigningKey (k,_) = Crypto.SigningKey $ getRawKey k
bootstrapWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
pair <- lookupPrivateKey keyFrom addr
let signingKeys = toSigningKey pair
pure $ SL.makeBootstrapWitness _ signingKeys _
pure $ SL.WitnessSet mempty mempty bootstrapWits

pure $ toSealed $ SL.Tx unsigned wits metadata
TxWitnessByronUTxO ->
_mkStdByronTx protocolMagic keyFrom ownedIns outs

_mkDelegationJoinTx
:: FeePolicy
Expand Down Expand Up @@ -384,26 +391,6 @@ _decodeSignedTx bytes = do
Left apiErr ->
Left $ ErrDecodeSignedTxWrongPayload (Cardano.renderApiError apiErr)

_mkStdByronTx
:: WalletKey k
=> ProtocolMagic
-> (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
-> [(TxIn, TxOut)]
-> [TxOut]
-> Either ErrMkTx (Tx, SealedTx)
_mkStdByronTx pm keyFrom ownedIns outs = do
let (Cardano.TxUnsignedByron _ _ txHash) =
mkUnsignedByronTx ownedIns outs
let tx = (fst <$> ownedIns, outs)
let sigData = Byron.hashToBytes txHash
witnesses <- forM ownedIns $ \(_, TxOut addr _) -> do
ks <- lookupPrivateKey keyFrom addr
pure $ mkByronWitness pm sigData ks
pure
( Tx (Hash sigData) (second coin <$> ownedIns) outs
, SealedTx $ CBOR.toStrictByteString $ CBOR.encodeSignedTx tx witnesses
)

_minimumFee
:: FeePolicy
-> [Certificate]
Expand Down Expand Up @@ -509,15 +496,6 @@ mkUnsignedShelleyTx ttl ownedIns outs certs fee =
in
unsigned

mkUnsignedByronTx
:: [(TxIn, TxOut)]
-> [TxOut]
-> Cardano.TxUnsigned
mkUnsignedByronTx ownedIns outs =
Cardano.buildByronTransaction
(toCardanoTxIn . fst <$> ownedIns)
(map toCardanoTxOut outs)

-- TODO: The SlotId-SlotNo conversion based on epoch length would not
-- work if the epoch length changed in a hard fork.

Expand Down

0 comments on commit 27d526f

Please sign in to comment.