diff --git a/lib/shelley/cardano-wallet-shelley.cabal b/lib/shelley/cardano-wallet-shelley.cabal index b13fa5cb086..556a8859ceb 100644 --- a/lib/shelley/cardano-wallet-shelley.cabal +++ b/lib/shelley/cardano-wallet-shelley.cabal @@ -42,6 +42,7 @@ library , cardano-config , cardano-crypto , cardano-crypto-class + , cardano-crypto-wrapper , cardano-ledger , cardano-slotting , cardano-wallet-cli diff --git a/lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs b/lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs index d5e46a757b0..2f3d137e10c 100644 --- a/lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs +++ b/lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs @@ -43,7 +43,7 @@ import Cardano.Crypto.DSIGN import Cardano.Crypto.DSIGN.Ed25519 ( VerKeyDSIGN (..) ) import Cardano.Wallet.Primitive.AddressDerivation - ( Depth (..), NetworkDiscriminant (..), Passphrase, WalletKey (..) ) + ( Depth (..), NetworkDiscriminant (..), Passphrase (..), WalletKey (..) ) import Cardano.Wallet.Primitive.AddressDerivation.Byron ( ByronKey ) import Cardano.Wallet.Primitive.AddressDerivation.Icarus @@ -88,6 +88,8 @@ import Cardano.Wallet.Transaction , ErrValidateSelection , TransactionLayer (..) ) +import Control.Arrow + ( second ) import Control.Monad ( forM ) import Crypto.Error @@ -113,9 +115,11 @@ 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.Hash.Class as Hash import qualified Cardano.Crypto.Wallet as CC import qualified Cardano.Wallet.Primitive.Types as W +import qualified Codec.CBOR.Encoding as CBOR import qualified Codec.CBOR.Read as CBOR import qualified Codec.CBOR.Write as CBOR import qualified Crypto.PubKey.Ed25519 as Ed25519 @@ -156,7 +160,7 @@ newTransactionLayer -> ProtocolMagic -> EpochLength -> TransactionLayer t k -newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer +newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer { mkStdTx = _mkStdTx , mkDelegationJoinTx = _mkDelegationJoinTx , mkDelegationQuitTx = _mkDelegationQuitTx @@ -173,7 +177,7 @@ newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer -> [(TxIn, TxOut)] -> [TxOut] -> Either ErrMkTx (Tx, SealedTx) - _mkStdTx keyFrom slot ownedIns outs =case (txWitnessTagFor @k) of + _mkStdTx keyFrom slot ownedIns outs = case (txWitnessTagFor @k) of TxWitnessShelleyUTxO -> do let timeToLive = defaultTTL epochLength slot let fee = realFee ownedIns outs @@ -185,9 +189,32 @@ newTransactionLayer _proxy _protocolMagic epochLength = TransactionLayer let wits = SL.WitnessSet addrWits mempty mempty pure $ toSealed $ SL.Tx unsigned wits metadata TxWitnessByronUTxO -> do - let Cardano.TxUnsignedByron unsigned _ _ = - mkUnsignedByronTx ownedIns outs - error "not implemented" + 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 protocolMagic sigData ks + pure + ( Tx (Hash sigData) (second coin <$> ownedIns) outs + , SealedTx $ CBOR.toStrictByteString $ CBOR.encodeSignedTx tx witnesses + ) + where + mkByronWitness + :: ProtocolMagic + -> ByteString + -> (k 'AddressK XPrv, Passphrase "encryption") + -> ByteString + mkByronWitness (ProtocolMagic pm) sigData (xPrv, Passphrase pwd) = + CBOR.toStrictByteString + $ CBOR.encodePublicKeyWitness (getRawKey $ publicKey xPrv) + $ CC.unXSignature (CC.sign pwd (getRawKey xPrv) message) + where + message = mconcat + [ "\x01" + , CBOR.toStrictByteString (CBOR.encodeInt32 pm) + , CBOR.toStrictByteString (CBOR.encodeBytes sigData) + ] _mkDelegationJoinTx :: FeePolicy @@ -520,12 +547,6 @@ mkWitness body (prv, pwd) = $ unsafeMkEd25519 $ toXPub prv -mkByronWitness - :: SL.TxBody TPraosStandardCrypto - -> (XPrv, Passphrase "encryption") - -> Cardano.TxWitness -mkByronWitness _ _ = undefined - signWith :: ByteString -> (XPrv, Passphrase "encryption")