From f35395bf3b93f3b07f71b10e960cc69965c1703f Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 29 Jun 2021 17:10:30 +0200 Subject: [PATCH] Introduce example keys and genesis for Alice en Bob --- tests/alicebob.nim | 21 +++++++++++++++++++++ tests/examples.nim | 22 ++++++++++++++-------- tests/testTransactions.nim | 10 +++++----- 3 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 tests/alicebob.nim diff --git a/tests/alicebob.nim b/tests/alicebob.nim new file mode 100644 index 0000000..bc2f18e --- /dev/null +++ b/tests/alicebob.nim @@ -0,0 +1,21 @@ +import pkg/questionable +import abc + +let a, b = PrivateKey.random + +proc alice*(_: type PrivateKey): PrivateKey = + a + +proc bob*(_: type PrivateKey): PrivateKey = + b + +proc alice*(_: type PublicKey): PublicKey = + a.toPublicKey + +proc bob*(_: type PublicKey): PublicKey = + b.toPublicKey + +proc genesis*(_: type Transaction): Transaction = + let alice = PublicKey.alice + let bob = PublicKey.bob + !Transaction.init({alice: 100.u256, bob: 100.u256}) diff --git a/tests/examples.nim b/tests/examples.nim index c350823..36d7ae0 100644 --- a/tests/examples.nim +++ b/tests/examples.nim @@ -1,7 +1,7 @@ +import std/random import pkg/questionable -import abc/keys -import abc/transactions -import abc/wallet +import abc +import ./alicebob proc example*(_: type PrivateKey): PrivateKey = PrivateKey.random @@ -9,11 +9,17 @@ proc example*(_: type PrivateKey): PrivateKey = proc example*(_: type PublicKey): PublicKey = PrivateKey.example.toPublicKey -proc example*(_: type Transaction): Transaction = - let alice, bob = PublicKey.example - let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}) - !Transaction.init({genesis.hash: alice}, {alice: 2.u256, bob: 30.u256}) - proc example*(_: type Wallet): Wallet = let key = PrivateKey.example Wallet.init(key) + +proc example*(_: type Transaction): Transaction = + let alice = PublicKey.alice + let bob = PublicKey.bob + let genesis = Transaction.genesis + let amount = rand(100).u256 + var transaction = !Transaction.init( + {genesis.hash: alice}, + {bob: amount, alice: 100.u256 - amount} + ) + transaction diff --git a/tests/testTransactions.nim b/tests/testTransactions.nim index 62afb8b..cdb9212 100644 --- a/tests/testTransactions.nim +++ b/tests/testTransactions.nim @@ -1,10 +1,10 @@ import ./basics +import ./alicebob suite "Transactions": - let aliceKey, bobKey = PrivateKey.example - let alice = aliceKey.toPublicKey - let bob = bobKey.toPublicKey + let alice = PublicKey.alice + let bob = PublicKey.bob test "a genesis transaction can be made": let genesis = Transaction.init({alice: 32.u256, bob: 10.u256}) @@ -67,9 +67,9 @@ suite "Transactions": ) let hash = transaction.hash.toBytes check not transaction.hasValidSignature - transaction.add(aliceKey.sign(hash)) + transaction.add(PrivateKey.alice.sign(hash)) check transaction.hasValidSignature - transaction.add(bobKey.sign(hash)) + transaction.add(PrivateKey.bob.sign(hash)) check not transaction.hasValidSignature test "transaction must have at least one output":