Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jul 3, 2020
1 parent c77807c commit beb295d
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 52 deletions.
8 changes: 4 additions & 4 deletions lib/core/test/unit/Cardano/Wallet/Gen.hs
Expand Up @@ -49,10 +49,10 @@ genMnemonic
)
=> Gen (Mnemonic mw)
genMnemonic = do
let n = fromIntegral (natVal $ Proxy @(EntropySize mw)) `div` 8
bytes <- BS.pack <$> vector n
let ent = unsafeMkEntropy @(EntropySize mw) bytes
return $ entropyToMnemonic ent
let n = fromIntegral (natVal $ Proxy @(EntropySize mw)) `div` 8
bytes <- BS.pack <$> vector n
let ent = unsafeMkEntropy @(EntropySize mw) bytes
return $ entropyToMnemonic ent

genPercentage :: Gen Percentage
genPercentage = unsafeMkPercentage . fromRational . toRational <$> genDouble
Expand Down
2 changes: 2 additions & 0 deletions lib/shelley/cardano-wallet-shelley.cabal
Expand Up @@ -142,6 +142,7 @@ test-suite unit
, cardano-addresses
, cardano-api
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-wallet-core
, cardano-wallet-shelley
, cardano-wallet-test-utils
Expand All @@ -157,6 +158,7 @@ test-suite unit
, text
, text-class
, transformers
, vector
, QuickCheck
build-tools:
hspec-discover
Expand Down
8 changes: 8 additions & 0 deletions lib/shelley/src/Cardano/Wallet/Shelley/Compatibility.hs
Expand Up @@ -73,6 +73,7 @@ module Cardano.Wallet.Shelley.Compatibility
, fromChainHash
, fromGenesisData
, fromNetworkMagic
, toCardanoNetwork
, fromSlotNo
, fromTip
, fromTip'
Expand Down Expand Up @@ -573,6 +574,13 @@ fromNetworkMagic :: NetworkMagic -> W.ProtocolMagic
fromNetworkMagic (NetworkMagic magic) =
W.ProtocolMagic (fromIntegral magic)

toCardanoNetwork :: W.ProtocolMagic -> Cardano.Network
toCardanoNetwork pm@(W.ProtocolMagic magic) =
if pm == W.mainnetMagic then
Cardano.Mainnet
else
Cardano.Testnet (Cardano.NetworkMagic $ fromIntegral magic)

--
-- Stake pools
--
Expand Down
85 changes: 50 additions & 35 deletions lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -26,9 +26,12 @@ module Cardano.Wallet.Shelley.Transaction
-- * Internals
, _minimumFee
, _decodeSignedTx
, _mkStdByronTx
, _estimateMaxNumberOfInputs
, mkUnsignedShelleyTx
, mkWitness
, mkUnsignedByronTx
, mkShelleyWitness
, mkByronWitness
, realFee
) where

Expand Down Expand Up @@ -184,37 +187,12 @@ newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer
let unsigned = mkUnsignedShelleyTx timeToLive ownedIns outs [] fee
addrWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkWitness unsigned (getRawKey k, pwd)
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let metadata = SL.SNothing
let wits = SL.WitnessSet addrWits mempty mempty
pure $ toSealed $ SL.Tx unsigned wits metadata
TxWitnessByronUTxO -> 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 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)
]
TxWitnessByronUTxO ->
_mkStdByronTx protocolMagic keyFrom ownedIns outs

_mkDelegationJoinTx
:: FeePolicy
Expand Down Expand Up @@ -269,9 +247,9 @@ newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer

addrWits <- fmap Set.fromList $ forM inps $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkWitness unsigned (getRawKey k, pwd)
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let certWits =
Set.singleton (mkWitness unsigned (getRawKey accXPrv, pwd'))
Set.singleton (mkShelleyWitness unsigned (getRawKey accXPrv, pwd'))
let wits = SL.WitnessSet (Set.union addrWits certWits) mempty mempty

pure $ toSealed $ SL.Tx unsigned wits metadata
Expand Down Expand Up @@ -326,9 +304,9 @@ newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer

addrWits <- fmap Set.fromList $ forM inps $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkWitness unsigned (getRawKey k, pwd)
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let certWits =
Set.singleton (mkWitness unsigned (getRawKey accXPrv, pwd'))
Set.singleton (mkShelleyWitness unsigned (getRawKey accXPrv, pwd'))
let wits = SL.WitnessSet (Set.union addrWits certWits) mempty mempty

pure $ toSealed $ SL.Tx unsigned wits metadata
Expand Down Expand Up @@ -406,6 +384,26 @@ _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 @@ -530,11 +528,11 @@ defaultTTL :: EpochLength -> SlotId -> SlotNo
defaultTTL epochLength slot =
(toSlotNo epochLength slot) + 7200

mkWitness
mkShelleyWitness
:: SL.TxBody TPraosStandardCrypto
-> (XPrv, Passphrase "encryption")
-> SL.WitVKey TPraosStandardCrypto 'SL.Witness
mkWitness body (prv, pwd) =
mkShelleyWitness body (prv, pwd) =
SL.WitVKey key sig
where
sig = SignedDSIGN
Expand All @@ -558,6 +556,23 @@ unsafeMkEd25519 :: XPub -> Ed25519.PublicKey
unsafeMkEd25519 =
throwCryptoError . Ed25519.publicKey . xpubPublicKey

mkByronWitness
:: WalletKey k
=> 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)
]

--------------------------------------------------------------------------------
-- Extra validations on coin selection
--
Expand Down

0 comments on commit beb295d

Please sign in to comment.