Bitcoin-core, Import Private Key then Send Fund with Bitcoin-cli and Python Bit library

After following the Book of Programming Bitcoin by O'Reilly.
Exercise 9 of chapter 4, suggest to try send and receive coins.
During my test to access the faucet was no luck, the page hung.
Therefore i googled and found this testnet server with docker.
Also a tool for aid to check and validate your WIF (Wallet Import Format)
and a picture to describe about WIF, Private Key and Public Key.


Bitcoin-CLI
=========

Grab the docker here then setup as it is mentioned in the READMEmd
a. make start
b. > bitcoin-cli -datadir=1 createwallet wallet1
    > bitcoin-cli -datadir=2 createwallet wallet2
c. Say, you would like to import private key into wallet2 then
    > bitcoin-cli -datadir=2 importprivkey "yourPrivateKey-WIF" "myimport-key" true
   Notice the `true` flag is to tell rescan after the import
d. > bitcoin-cli -datadir=2 dumpprivkey "myPrivateKeyAddress"
    What i found is that error shown like
    "error code: -4
     error message: 
     Private key for address myPrivateKeyAddress is not known"
     If you are good to go then go to step #f
e. if step #d then restart the bitcoind of datadir=2 daemon
    > bitcoin-cli -datadir=2 stop
    > bitcoind -datadir=2 -daemon
    And then repeat step #d
f. > make generate BLOCKS=200
   Usually when `> make getinfo` you will immediately sees
   the wallet1 begin to has balance.
g. > bitcoin-cli -datadir=2 getnewaddress
    Notice this address has prefix of `bc1` which is bech32 format.
h. > bitcoin-cli -datadir=1 settxfee 0.0001
    Initially the txfee is not yet set. Then..
    > make sendfrom1 ADDRESS=addressFromStep#g AMOUNT=100
    > make generate
    > make getinfo
    You will see the wallet2 balance shows 100
i. Send to legacy address,
    > make sendfrom1 ADDRESS=myPrivateKeyAddress AMOUNT=23.23
    > make generate
    > make getinfo
    You will see the wallet2 balance shows 123.23
j. Create new address (bech32) then dump the privatekey
    > bitcoin-cli -datadir=2 getnewaddress
    "keyFromGenerateNewAddressWallet2"
    > bitcoin-cli -datadir=2 dumpprivatekey "keyFromGenerateNewAddressWallet2"
    "WIF-AddressWallet2"

Steps a-i, to shows that create wallet, import private key and send fund 
to bech32 address and to legacy address.


Python bit
========

Notes: Python bit as per this article writting, the library is not yet support
           Bech32, therefore the transaction made in bitcoin-cli address would
           not be seen, unless one transfer/send-fund to the legacy address

If you are in virtualenv make sure one setup with python3.7 or above

a. pip install bit
b. $ docker run -ti -p 19001:19001 -p 19011:19011 --network=host bitcoin-testnet-box
    to connect to docker bitcoind (bitcoin-core) the one we had setup
    earlier in #bitcoin-cli section
    make sure to run the docker with --network=host if one has difficulty
    to connect to the container
c. There will be some python command running in python-shell
> import bit
> from bit.network import NetworkAPI
> node = NetworkAPI.connect_to_node(user='admin2',password='123', host='localhost', port=19011, use_https=False, testnet=True)
> key = bit.PrivateKeyTestnet('myPrivateKeyAddress')
''' Or if you not pass myPrivateKeyAddress as param then new random Privatekey will be generated'''
> keyFromBitcoinCLI = bit.PrivateKeyTestnet('WIF-AddressWallet2')
> key.get_balance('btc')
''' Notice that the balance will be shown correctly '''
> keyFromBitcoinCLI.get_balance('btc')
''' Notice it will shown "0" '''
''' it is because that the python-bit library is not yet support bech32 '''

You could retry to getnewaddress and pass the address-type to `legacy`
and resend the fund, and retry the query, you will see the balance shown 
exactly as the fund send to the legacy-address

If you dont pass the address-type

> bitcoin-cli -datadir=2 getnewaddress 
bcrt1qnxkzvjxy0q2hepfpq9selmgm7zvk5sl5506e7d <-- Bech32 by default

> bitcoin-cli -datadir=2 getnewaddress "" "legacy"
bitcoin-cli -datadir=2 getnewaddress "" "legacy"
n1DjDpcjPbS2XKPKFwqZy5rX1Y6cDUDuck <-- legacy address 

> make sendfrom1 ADDRESS=n1DjDpcjPbS2XKPKFwqZy5rX1Y6cDUDuck AMOUNT=12.21
> make generate

python> keyFromBitcoinCLI.get_balance('btc')
"12.21"

Lessons learned
============
a. The Bitcoin-Core could be add with a wallet
b. Whenever invoke `> bitcoin-cli getnewaddress`
    it actually create a new random private-key and return the address.
    Later could be used as receiver address.
c. During `getnewaddress` if you forgot to add the label, then you could 
    query all the addresses by `> bitcoin-cli getaddressesbylabel ""`
    notice the label is empty string ""
d. dump the private-key for each addresses that later could be import
    into another wallet
e. Whenever you importprivkey make sure to restart the bitcoin-core daemon.
    Otherwise `> bitcoin-cli dumpprivkey "address"` would return no private-key related.
f. Worth to read about bitcoin-address-format





Comments