mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-04 22:43:10 +00:00
deserialization of signatures and identifiers
This commit is contained in:
parent
b91b62a67d
commit
4e86fcb581
@ -1,7 +1,9 @@
|
||||
import std/sequtils
|
||||
import pkg/stint
|
||||
import pkg/questionable
|
||||
import pkg/questionable/results
|
||||
|
||||
export sequtils
|
||||
export stint
|
||||
export questionable
|
||||
export results
|
||||
|
||||
@ -2,8 +2,10 @@ import ./signatures/identity
|
||||
import ./signatures/signing
|
||||
import ./signatures/signed
|
||||
import ./signatures/serialization
|
||||
import ./signatures/deserialization
|
||||
|
||||
export identity
|
||||
export signing
|
||||
export signed
|
||||
export serialization
|
||||
export deserialization
|
||||
|
||||
18
codexvalidator/signatures/deserialization.nim
Normal file
18
codexvalidator/signatures/deserialization.nim
Normal file
@ -0,0 +1,18 @@
|
||||
import pkg/blscurve
|
||||
import ../basics
|
||||
import ./identity
|
||||
import ./signing
|
||||
|
||||
func fromBytes*(_: type Identifier, bytes: openArray[byte]): ?!Identifier =
|
||||
var identifier: Identifier
|
||||
if blscurve.fromBytes(identifier, bytes):
|
||||
success identifier
|
||||
else:
|
||||
failure "invalid identifier"
|
||||
|
||||
func fromBytes*(_: type Signature, bytes: openArray[byte]): ?!Signature =
|
||||
var signature: Signature
|
||||
if blscurve.fromBytes(signature, bytes):
|
||||
success signature
|
||||
else:
|
||||
failure "invalid signature"
|
||||
@ -1,11 +1,15 @@
|
||||
import std/unittest
|
||||
import std/sequtils
|
||||
import pkg/stint
|
||||
|
||||
export unittest
|
||||
export sequtils
|
||||
|
||||
import pkg/stint
|
||||
export stint
|
||||
|
||||
import ./examples
|
||||
import pkg/questionable
|
||||
import pkg/questionable/results
|
||||
export questionable
|
||||
export results
|
||||
|
||||
import ./examples
|
||||
export examples
|
||||
|
||||
@ -74,6 +74,12 @@ proc example*(_: type Transaction): Transaction =
|
||||
proc example*(_: type Identity): Identity =
|
||||
Identity.random(result)
|
||||
|
||||
proc example*(_: type Identifier): Identifier =
|
||||
Identity.example.identifier
|
||||
|
||||
proc example*(_: type Signature): Signature =
|
||||
Identity.example.sign(seq[byte].example)
|
||||
|
||||
proc example*(_: type CommitteeMember): CommitteeMember =
|
||||
CommitteeMember(uint32.example.int)
|
||||
|
||||
|
||||
@ -49,3 +49,25 @@ suite "Signature scheme":
|
||||
let hash = Hash.example
|
||||
let signature = identity.sign(hash)
|
||||
check signature.verify(identity.identifier, hash)
|
||||
|
||||
test "identifier can be serialized and deserialized":
|
||||
let identifier = Identifier.example
|
||||
let serialized = identifier.toBytes()
|
||||
check Identifier.fromBytes(serialized) == success identifier
|
||||
|
||||
test "identifier deserialization fails when wrong number of bytes":
|
||||
let identifier = Identifier.example
|
||||
let serialized = identifier.toBytes()
|
||||
let invalid = serialized & 42'u8
|
||||
check isFailure Identifier.fromBytes(invalid)
|
||||
|
||||
test "signature can be serialized and deserialized":
|
||||
let signature = Signature.example
|
||||
let serialized = signature.toBytes()
|
||||
check Signature.fromBytes(serialized) == success signature
|
||||
|
||||
test "signature deserialization fails when wrong number of bytes":
|
||||
let signature = Signature.example
|
||||
let serialized = signature.toBytes()
|
||||
let invalid = serialized & 42'u8
|
||||
check isFailure Signature.fromBytes(invalid)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user