2
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.
Did you import either the private key or the address into Bitcoin Core's wallet? getreceivedbyaddress is a wallet RPC and can only know about transactions that the wallet considers to be its own. – Pieter Wuille – 2017-02-05T20:54:35.073
Actually I don't want to import the private keys into the wallet that belongs to the full node. I want to treat the mabuakky created Bitcoin address as a separate wallet . – cogitoergosum – 2017-02-06T00:35:09.627
Bitcoin Core only supports one wallet for now. – Pieter Wuille – 2017-02-06T00:45:44.297
You don't need to import the private key, by the way. You can just import the address (with
importaddress) as a watch-only address. – Pieter Wuille – 2017-02-06T00:54:07.067Did you mean, only one wallet is supported in the
regtestmode? – cogitoergosum – 2017-02-06T01:06:00.840If the store of transactions is a BerkleyDB, surely, it should be as easy as running a query against that DB with my Bitcoin address as the input? The query output could be just the hex string that can be solved only by me because I own the private key. – cogitoergosum – 2017-02-06T01:08:06.227
Bitcoin Core has no database per address, and it doesn't need it. Its wallet only keeps track of the relevant transactions. If you want to use getreceivedbyaddress, you must make the wallet track that address first. – Pieter Wuille – 2017-02-06T01:13:36.613
Sorry, I didn't mean a per address database. I was referring to the blockchain itself. Quoting your answer here ( :D ) - http://bitcoin.stackexchange.com/a/11108/6975 - I was thinking, if I can query
– cogitoergosum – 2017-02-06T01:18:37.150blocks/blk*.datwith my Bitcoin address as the input. After all, https://blockchain.info/ allows searching via address. So, why not in this case too?There is no by-address index of the blockchain, as there is no need for one. Scanning through the chain is possible (and the wallet will do that for you at import time), but that may easily take dozens of minutes by- not something you want to do when you issue an RPC call. – Pieter Wuille – 2017-02-06T02:16:26.010
1blockchain.info has a massive database pre-indexed for every address. That's not implemented in Bitcoin Core, and you generally don't need it. Letting the wallet track transactions that are relevant is far more scalable. – Pieter Wuille – 2017-02-06T02:20:00.363
Thank you, @PieterWuille, for the explanation. I think, I will just create another node and move on. – cogitoergosum – 2017-02-06T02:48:36.640