If I have the bitcoin address, the private key and a transaction ID from Bitcoin (regtest
mode) in shell variables, what Python code do I need to run to claim the Bitcoins are indeed intended for this private key?
I have been following this excellent article - Bitcoins the hard way : Using the raw Bitcoin protocol. I hope to replicate this with Bitcoin in regtest mode as part of a larger design.
To begin with, I had to ensure that the code mentioned in the article (available here) can generate Bitcoin address with m
or n
as the prefix and uses 111
as the network ID. See here. I made the following code changes to generate addresses for regtest
mode:
keyUtils.pubKeyToAddr
def pubKeyToAddr(s):
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(s.decode('hex')).digest())
return utils.base58CheckEncode(111, ripemd160.digest())
utils.base58CheckEncode
def base58CheckEncode(version, payload):
s = chr(version) + payload
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
result = s + checksum
leadingZeros = countLeadingChars(result, '\0')
return 'm' * leadingZeros + base58encode(base256decode(result))
Then, with bitcoin-qt
, I transferred some BTC to an address generated from above. The transfer goes through and is confirmed when a new block is created.
However, I am unable to see the amount credited to this new address with the getreceivedbyaddress
command. I am running this command in the Debug Window console (for some reason, bitcoin-cli
wouldn't connect even after configuring bitcoind
). I think, the reason could be the fact that, the private key and the bitcoin address was generated elsewhere from command line rather than the default --data-dir
location that bitcoin-qt
used when it was launched. I understand this.
importaddress
) as a watch-only address.