Check transaction signature

This commit is contained in:
Mark Spanbroek 2021-06-29 11:18:33 +02:00
parent f65ad13904
commit 1a0a96705b
2 changed files with 29 additions and 1 deletions

View File

@ -66,3 +66,15 @@ func toBytes*(transaction: Transaction): seq[byte] =
func hash*(transaction: Transaction): TxHash =
TxHash(sha256.digest(transaction.toBytes))
func hasValidSignature*(transaction: Transaction): bool =
if transaction.inputs.len == 0:
return false
var signees: seq[PublicKey]
for (_, owner) in transaction.inputs:
signees.add(owner)
let message = transaction.hash.toBytes
let signature = transaction.signature
let signee = aggregate(signees)
signee.verify(message, signature)

View File

@ -5,7 +5,9 @@ import ./examples
suite "Transactions":
let alice, bob = PublicKey.example
let aliceKey, bobKey = PrivateKey.example
let alice = aliceKey.toPublicKey
let bob = bobKey.toPublicKey
test "a genesis transaction can be made":
let genesis = Transaction.init({alice: 32.u256, bob: 10.u256})
@ -53,6 +55,20 @@ suite "Transactions":
transaction.add(sig2)
check transaction.signature == aggregate(sig1, sig2)
test "transaction signature can be checked for validity":
let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256})
check not genesis.hasValidSignature()
var transaction = !Transaction.init(
{genesis.hash: alice},
{alice: 2.u256, bob: 30.u256}
)
let hash = transaction.hash.toBytes
check not transaction.hasValidSignature
transaction.add(aliceKey.sign(hash))
check transaction.hasValidSignature
transaction.add(bobKey.sign(hash))
check not transaction.hasValidSignature
test "transaction must have at least one output":
check Transaction.init([]).isNone