Skip to content

Commit

Permalink
wallet-api tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Jann Müller committed Mar 26, 2019
1 parent 2150ac1 commit bc4f38d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion wallet-api/src/Ledger/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type TxId = TxIdOf (Digest SHA256)

-- | True if the signature matches the public key
signedBy :: Signature -> PubKey -> TxId -> Bool
signedBy (Signature k) (PubKey s) txId =
signedBy (Signature s) (PubKey k) txId =
let k' = ED25519.publicKey $ BSL.toStrict $ getKeyBytes k
s' = ED25519.signature $ BSL.toStrict $ getKeyBytes s
in throwCryptoError $ ED25519.verify <$> k' <*> pure (getTxId txId) <*> s' -- TODO: is this what we want
Expand Down
24 changes: 19 additions & 5 deletions wallet-api/src/Wallet/Generators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Wallet.Generators(
genInitialTransaction,
-- * Assertions
assertValid,
-- * Wallets for testing
-- $wallets
wallet1,
wallet2,
wallet3,
-- * Etc.
genAda,
genValue,
Expand All @@ -28,10 +33,7 @@ module Wallet.Generators(
runTraceOn,
splitVal,
validateMockchain,
-- * Wallets for testing
wallet1,
wallet2,
wallet3
signAll
) where

import Data.Bifunctor (Bifunctor (..))
Expand All @@ -55,11 +57,20 @@ import Ledger
import qualified Wallet.API as W
import Wallet.Emulator as Emulator

-- $wallets
-- 'wallet1', 'wallet2' and 'wallet3' are three predefined 'Wallet' values
-- each with its own private-public key pair. Don't use them outside
-- of the emulator.

wallet1, wallet2, wallet3 :: Wallet
wallet1 = Wallet $ fromHex "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"
wallet2 = Wallet $ fromHex "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c"
wallet3 = Wallet $ fromHex "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025"

-- | Attach signatures of all known wallets to a transaction.
signAll :: Tx -> Tx
signAll tx = foldl (flip signWithWallet) tx [wallet1, wallet2, wallet3]

-- TODO: Get private keys for the following two public keys:
-- "e61a185bcef2613a6c7cb79763ce945d3b245d76114dd440bcf5f2dc1aa57057"
-- "c0dac102c4533186e25dc43128472353eaabdb878b152aeb8e001f92d90233a7"
Expand Down Expand Up @@ -199,7 +210,10 @@ genValidTransactionSpending' g f ins totalVal = do
, txValidRange = $$(Interval.always)
, txSignatures = Map.empty
}
pure tx

-- sign the transaction with all three known wallets
-- this is somewhat crude (but technically valid)
pure (signAll tx)
else Gen.discard

genAda :: MonadGen m => m Ada
Expand Down
8 changes: 7 additions & 1 deletion wallet-api/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,13 @@ invalidScript = property $ do

let (result, st) = Gen.runTrace m $ do
processPending
walletAction wallet1 $ signTxAndSubmit_ scriptTxn
-- we need to sign scriptTxn again because it has been modified
-- note that although 'scriptTxn' is submitted by wallet 1, it
-- may spend outputs belonging to one of the other two wallets.
-- So we can't use 'signTxAndSubmit_' (because it would only attach
-- wallet 1's signatures). Instead, we get all the wallets'
-- signatures with 'signAll'.
walletAction wallet1 $ submitTxn (Gen.signAll scriptTxn)
processPending
walletAction wallet1 $ signTxAndSubmit_ invalidTxn
processPending
Expand Down

0 comments on commit bc4f38d

Please sign in to comment.