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.

11
  • 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. Feb 5, 2017 at 20:54
  • 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 . Feb 6, 2017 at 0:35
  • Bitcoin Core only supports one wallet for now. Feb 6, 2017 at 0:45
  • 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. Feb 6, 2017 at 0:54
  • 1
    blockchain.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. Feb 6, 2017 at 2:20

1 Answer 1

0

I am unable to see the amount credited to this new address with the getreceivedbyaddress command.

There is no by-address index of the blockchain in Bitcoin core.

For bitcoin-core to track an address you must, for example, add an address to the wallet as a watch-only address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.