Assume we have a transaction in hex (tx_hex) and we want to find a nonce such that
SHA-256(SHA-256(nonce|| tx_hex)) = new_transaction_id ('||' denotes concatenation)
first bits consist of 32 zeros. I understand that this nonce can be found only by brute force.
However, I cannot figure out how you check this condition in practice. new_transaction_id is in hex, so is that equivalent to checking that its first 8 digits are 0?
And if so to find the nonce, I just have to try every possible number (let's say up to 32 bits) for the first 8 digits of new_transaction_id to be 0?
That is my code BTW
def mine(self, tx_hex):
for i in range(1,4294967295):
nonce = hex(i)[2:]
new_tx = self.add_nonce(nonce, tx_hex)
sha = self.get_tx_id(new_tx)
print(sha[:2])
if sha[:8] == '00000000':
print('Nonce:', i)
break
hash < target
).