From 8869c49294cfafaa289582bac904db9c2fb794e6 Mon Sep 17 00:00:00 2001 From: spekulaas Date: Mon, 6 Nov 2023 17:10:38 +0100 Subject: [PATCH] finished lessons --- period_1/07-mining/702_TX2_A02_Nonce/TxBlock.py | 11 ++++++++++- .../703_TX2_A02_Nonce - tamper test/TxBlock.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/period_1/07-mining/702_TX2_A02_Nonce/TxBlock.py b/period_1/07-mining/702_TX2_A02_Nonce/TxBlock.py index 97a1747..76390bf 100644 --- a/period_1/07-mining/702_TX2_A02_Nonce/TxBlock.py +++ b/period_1/07-mining/702_TX2_A02_Nonce/TxBlock.py @@ -44,7 +44,16 @@ class TxBlock (CBlock): return True def good_nonce(self): - return False + digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) + digest.update(bytes(str(self.data), 'utf8')) + digest.update(bytes(str(self.previousHash), 'utf8')) + digest.update(bytes(str(self.nonce), 'utf8')) + this_hash = digest.finalize() + return this_hash[:leading_zeros] == bytes(''.join([ '\x4f' for i in range(leading_zeros)]), 'utf8') def find_nonce(self): + for i in range(10000000): + self.nonce = i + if self.good_nonce(): + return self.nonce return None diff --git a/period_1/07-mining/703_TX2_A02_Nonce - tamper test/TxBlock.py b/period_1/07-mining/703_TX2_A02_Nonce - tamper test/TxBlock.py index 97a1747..58cec14 100644 --- a/period_1/07-mining/703_TX2_A02_Nonce - tamper test/TxBlock.py +++ b/period_1/07-mining/703_TX2_A02_Nonce - tamper test/TxBlock.py @@ -44,7 +44,16 @@ class TxBlock (CBlock): return True def good_nonce(self): - return False + digest = hashes.Hash(hashes.SHA256(), backend=default_backend()) + digest.update(bytes(str(self.data), 'utf8')) + digest.update(bytes(str(self.previousHash), 'utf8')) + digest.update(bytes(str(self.nonce), 'utf8')) + this_hash = digest.finalize() + return this_hash[:leading_zeros] == b'\x00'*leading_zeros def find_nonce(self): + for i in range(10000000): + self.nonce = i + if self.good_nonce(): + return self.nonce return None