From b0babf996224d5cfca4ed93bed21df47cb67cbb4 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 29 Jun 2021 15:17:53 +0200 Subject: [PATCH] Add convenience method for signing transaction --- abc/transactions.nim | 3 +++ tests/testTransactions.nim | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/abc/transactions.nim b/abc/transactions.nim index 92f65f8..bdf741e 100644 --- a/abc/transactions.nim +++ b/abc/transactions.nim @@ -67,6 +67,9 @@ func toBytes*(transaction: Transaction): seq[byte] = func hash*(transaction: Transaction): TxHash = TxHash(sha256.digest(transaction.toBytes)) +func sign*(key: PrivateKey, transaction: var Transaction) = + transaction.add(key.sign(transaction.hash.toBytes)) + func hasValidSignature*(transaction: Transaction): bool = if transaction.inputs.len == 0: return false diff --git a/tests/testTransactions.nim b/tests/testTransactions.nim index 1281328..b914022 100644 --- a/tests/testTransactions.nim +++ b/tests/testTransactions.nim @@ -55,6 +55,12 @@ suite "Transactions": transaction.add(sig2) check transaction.signature == aggregate(sig1, sig2) + test "transaction can be signed by a private key": + let key = PrivateKey.example + var transaction = Transaction.example + key.sign(transaction) + check transaction.signature == key.sign(transaction.hash.toBytes) + test "transaction signature can be checked for validity": let genesis = !Transaction.init({alice: 32.u256, bob: 10.u256}) check not genesis.hasValidSignature()