mirror of https://github.com/status-im/nim-abc.git
Introduce example keys and genesis for Alice en Bob
This commit is contained in:
parent
7fac1c9c94
commit
f35395bf3b
|
@ -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})
|
|
@ -1,7 +1,7 @@
|
||||||
|
import std/random
|
||||||
import pkg/questionable
|
import pkg/questionable
|
||||||
import abc/keys
|
import abc
|
||||||
import abc/transactions
|
import ./alicebob
|
||||||
import abc/wallet
|
|
||||||
|
|
||||||
proc example*(_: type PrivateKey): PrivateKey =
|
proc example*(_: type PrivateKey): PrivateKey =
|
||||||
PrivateKey.random
|
PrivateKey.random
|
||||||
|
@ -9,11 +9,17 @@ proc example*(_: type PrivateKey): PrivateKey =
|
||||||
proc example*(_: type PublicKey): PublicKey =
|
proc example*(_: type PublicKey): PublicKey =
|
||||||
PrivateKey.example.toPublicKey
|
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 =
|
proc example*(_: type Wallet): Wallet =
|
||||||
let key = PrivateKey.example
|
let key = PrivateKey.example
|
||||||
Wallet.init(key)
|
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
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import ./basics
|
import ./basics
|
||||||
|
import ./alicebob
|
||||||
|
|
||||||
suite "Transactions":
|
suite "Transactions":
|
||||||
|
|
||||||
let aliceKey, bobKey = PrivateKey.example
|
let alice = PublicKey.alice
|
||||||
let alice = aliceKey.toPublicKey
|
let bob = PublicKey.bob
|
||||||
let bob = bobKey.toPublicKey
|
|
||||||
|
|
||||||
test "a genesis transaction can be made":
|
test "a genesis transaction can be made":
|
||||||
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
|
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
|
||||||
|
@ -67,9 +67,9 @@ suite "Transactions":
|
||||||
)
|
)
|
||||||
let hash = transaction.hash.toBytes
|
let hash = transaction.hash.toBytes
|
||||||
check not transaction.hasValidSignature
|
check not transaction.hasValidSignature
|
||||||
transaction.add(aliceKey.sign(hash))
|
transaction.add(PrivateKey.alice.sign(hash))
|
||||||
check transaction.hasValidSignature
|
check transaction.hasValidSignature
|
||||||
transaction.add(bobKey.sign(hash))
|
transaction.add(PrivateKey.bob.sign(hash))
|
||||||
check not transaction.hasValidSignature
|
check not transaction.hasValidSignature
|
||||||
|
|
||||||
test "transaction must have at least one output":
|
test "transaction must have at least one output":
|
||||||
|
|
Loading…
Reference in New Issue