Introduce example keys and genesis for Alice en Bob

This commit is contained in:
Mark Spanbroek 2021-06-29 17:10:30 +02:00
parent 7fac1c9c94
commit f35395bf3b
3 changed files with 40 additions and 13 deletions

21
tests/alicebob.nim Normal file
View File

@ -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})

View File

@ -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

View File

@ -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":