diff --git a/abc/transactions.nim b/abc/transactions.nim index aca4870..92f65f8 100644 --- a/abc/transactions.nim +++ b/abc/transactions.nim @@ -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) diff --git a/tests/testTransactions.nim b/tests/testTransactions.nim index 17eea52..1281328 100644 --- a/tests/testTransactions.nim +++ b/tests/testTransactions.nim @@ -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