Add examples to simplify tests requiring keys

This commit is contained in:
Mark Spanbroek 2021-06-28 14:03:48 +02:00
parent 1257b42a65
commit 2fbf826655
2 changed files with 14 additions and 6 deletions

7
tests/examples.nim Normal file
View File

@ -0,0 +1,7 @@
import abc/keys
proc example*(_: type PrivateKey): PrivateKey =
PrivateKey.random
proc example*(_: type PublicKey): PublicKey =
PrivateKey.example.toPublicKey

View File

@ -2,6 +2,7 @@ import std/unittest
import pkg/questionable
import pkg/stew/byteutils
import abc/keys
import ./examples
suite "Keys":
@ -9,40 +10,40 @@ suite "Keys":
check PrivateKey.random != PrivateKey.random
test "erases memory associated with a private key":
var key = PrivateKey.random
var key = PrivateKey.example
let bytes = cast[ptr[uint64]](addr key)
check bytes[] != 0
erase key
check bytes[] == 0
test "derives public key from private key":
let key1, key2 = PrivateKey.random()
let key1, key2 = PrivateKey.example
check key1.toPublicKey == key1.toPublicKey
check key2.toPublicKey == key2.toPublicKey
check key1.toPublicKey != key2.toPublicKey
test "can be used to sign messages":
const message = "hello".toBytes
let key = PrivateKey.random()
let key = PrivateKey.example
let signature = key.sign(message)
check signature != Signature.default
test "can be used to verify signatures":
let message1 = "hello".toBytes
let message2 = "hallo".toBytes
let private = PrivateKey.random
let private = PrivateKey.example
let public = private.toPublicKey
let signature = private.sign(message1)
check public.verify(message1, signature)
check not public.verify(message2, signature)
test "public key can be converted to bytes":
let key = PrivateKey.random.toPublicKey
let key = PublicKey.example
let bytes = key.toBytes
check PublicKey.fromBytes(bytes) == key.some
test "conversion from bytes to public key can fail":
let key = PrivateKey.random.toPublicKey
let key = PublicKey.example
let bytes = key.toBytes
let invalid = bytes[1..^1]
check PublicKey.fromBytes(invalid) == PublicKey.none