mirror of
https://github.com/logos-storage/logos-storage-nim-validator.git
synced 2026-01-03 22:13:11 +00:00
replace SignedTransaction with Signed[Transaction]
This commit is contained in:
parent
179f6e5564
commit
1c538eb74f
@ -1,28 +1,7 @@
|
||||
import pkg/blscurve
|
||||
import pkg/nimcrypto
|
||||
import ./hashing
|
||||
import ./signatures/identity
|
||||
import ./signatures/signing
|
||||
import ./signatures/signed
|
||||
|
||||
export blscurve.Signature
|
||||
export blscurve.sign
|
||||
export blscurve.verify
|
||||
|
||||
type
|
||||
Identity* = SecretKey
|
||||
Identifier* = PublicKey
|
||||
|
||||
proc random*(_: type Identity, identity: var Identity) =
|
||||
var randomness: array[32, byte]
|
||||
var done = false
|
||||
while not done:
|
||||
doAssert randomBytes(randomness) == randomness.len
|
||||
done = fromBytes(identity, randomness)
|
||||
burnMem(randomness)
|
||||
|
||||
func identifier*(identity: Identity): Identifier =
|
||||
doAssert publicFromSecret(result, identity)
|
||||
|
||||
func sign*(identity: Identity, hash: Hash): Signature =
|
||||
identity.sign(hash.data)
|
||||
|
||||
func verify*(signature: Signature, identifier: Identifier, hash: Hash): bool =
|
||||
identifier.verify(hash.toBytes(), signature)
|
||||
export identity
|
||||
export signing
|
||||
export signed
|
||||
|
||||
17
codexvalidator/signatures/identity.nim
Normal file
17
codexvalidator/signatures/identity.nim
Normal file
@ -0,0 +1,17 @@
|
||||
import pkg/blscurve
|
||||
import pkg/nimcrypto
|
||||
|
||||
type
|
||||
Identity* = SecretKey
|
||||
Identifier* = PublicKey
|
||||
|
||||
proc random*(_: type Identity, identity: var Identity) =
|
||||
var randomness: array[32, byte]
|
||||
var done = false
|
||||
while not done:
|
||||
doAssert randomBytes(randomness) == randomness.len
|
||||
done = fromBytes(identity, randomness)
|
||||
burnMem(randomness)
|
||||
|
||||
func identifier*(identity: Identity): Identifier =
|
||||
doAssert publicFromSecret(result, identity)
|
||||
40
codexvalidator/signatures/signed.nim
Normal file
40
codexvalidator/signatures/signed.nim
Normal file
@ -0,0 +1,40 @@
|
||||
import ../hashing
|
||||
import ./identity
|
||||
import ./signing
|
||||
|
||||
type Signed*[T] = object
|
||||
value: T
|
||||
signer: Identifier
|
||||
signature: Signature
|
||||
|
||||
func init*[T](
|
||||
_: type Signed[T],
|
||||
value: T,
|
||||
signer: Identifier,
|
||||
signature: Signature
|
||||
): Signed[T] =
|
||||
Signed[T](
|
||||
value: value,
|
||||
signer: signer,
|
||||
signature: signature
|
||||
)
|
||||
|
||||
func sign*[T](_: type Signed, identity: Identity, value: T): Signed[T] =
|
||||
mixin hash
|
||||
let hash: Hash = value.hash
|
||||
let signature = identity.sign(hash.toBytes())
|
||||
Signed[T].init(value, identity.identifier, signature)
|
||||
|
||||
func value*[T](signed: Signed[T]): T =
|
||||
signed.value
|
||||
|
||||
func signer*(signed: Signed): Identifier =
|
||||
signed.signer
|
||||
|
||||
func signature*(signed: Signed): Signature =
|
||||
signed.signature
|
||||
|
||||
func verifySignature*(signed: Signed): bool =
|
||||
mixin hash
|
||||
let hash: Hash = signed.value.hash
|
||||
signed.signer.verify(hash.toBytes(), signed.signature)
|
||||
13
codexvalidator/signatures/signing.nim
Normal file
13
codexvalidator/signatures/signing.nim
Normal file
@ -0,0 +1,13 @@
|
||||
import pkg/blscurve
|
||||
import ../hashing
|
||||
import ./identity
|
||||
|
||||
export blscurve.Signature
|
||||
export blscurve.sign
|
||||
export blscurve.verify
|
||||
|
||||
func sign*(identity: Identity, hash: Hash): Signature =
|
||||
blscurve.sign(identity, hash.toBytes())
|
||||
|
||||
func verify*(signature: Signature, identifier: Identifier, hash: Hash): bool =
|
||||
blscurve.verify(identifier, hash.toBytes(), signature)
|
||||
@ -1,10 +1,8 @@
|
||||
import ./transaction/transaction
|
||||
import ./transaction/serialization
|
||||
import ./transaction/hashing
|
||||
import ./transaction/signed
|
||||
|
||||
export transaction except hash
|
||||
export serialization.toBytes
|
||||
export hashing.hash
|
||||
export hashing.toBytes
|
||||
export signed
|
||||
|
||||
@ -1,38 +0,0 @@
|
||||
import ../signatures
|
||||
import ./transaction
|
||||
import ./hashing
|
||||
|
||||
type SignedTransaction* = object
|
||||
transaction: Transaction
|
||||
signer: Identifier
|
||||
signature: Signature
|
||||
|
||||
func init*(
|
||||
_: type SignedTransaction,
|
||||
transaction: Transaction,
|
||||
signer: Identifier,
|
||||
signature: Signature
|
||||
): SignedTransaction =
|
||||
SignedTransaction(
|
||||
transaction: transaction,
|
||||
signer: signer,
|
||||
signature: signature
|
||||
)
|
||||
|
||||
func sign*(identity: Identity, transaction: Transaction): SignedTransaction =
|
||||
let hash = hashing.hash(transaction)
|
||||
let signature = identity.sign(hash.toBytes())
|
||||
SignedTransaction.init(transaction, identity.identifier, signature)
|
||||
|
||||
func transaction*(signed: SignedTransaction): Transaction =
|
||||
signed.transaction
|
||||
|
||||
func signer*(signed: SignedTransaction): Identifier =
|
||||
signed.signer
|
||||
|
||||
func signature*(signed: SignedTransaction): Signature =
|
||||
signed.signature
|
||||
|
||||
func verifySignature*(signed: SignedTransaction): bool =
|
||||
let hash = hashing.hash(signed.transaction)
|
||||
signed.signer.verify(hash.toBytes(), signed.signature)
|
||||
@ -8,16 +8,16 @@ suite "Transaction signing":
|
||||
test "transactions can be signed":
|
||||
let identity = Identity.example
|
||||
let transaction = Transaction.example
|
||||
let signed = identity.sign(transaction)
|
||||
check signed.transaction == transaction
|
||||
let signed = Signed.sign(identity, transaction)
|
||||
check signed.value == transaction
|
||||
check signed.signer == identity.identifier
|
||||
check signed.signature == identity.sign(transaction.hash.toBytes())
|
||||
|
||||
test "transaction signature can be verified":
|
||||
let identity = Identity.example
|
||||
let transaction = Transaction.example
|
||||
let signed = identity.sign(transaction)
|
||||
let signed = Signed.sign(identity, transaction)
|
||||
check signed.verifySignature()
|
||||
let forger = Identity.example.identifier
|
||||
let forged = SignedTransaction.init(transaction, forger, signed.signature)
|
||||
let forged = Signed.init(transaction, forger, signed.signature)
|
||||
check not forged.verifySignature()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user