Skip to content

Instantly share code, notes, and snippets.

@islishude
Last active December 12, 2021 06:53
Show Gist options
  • Save islishude/d3497224aaaaf8d37f8da8d96977993a to your computer and use it in GitHub Desktop.
Save islishude/d3497224aaaaf8d37f8da8d96977993a to your computer and use it in GitHub Desktop.
BitcoinDevTips

Config

regtest=1
server=1
daemon=1
txindex=1
discover=0

rpcuser=test
rpcpassword=test
rpcallowip=0.0.0.0/0

#rpcworkqueue=7000
#rpcthread=8
#rpctimeout=10

and start node with bitcoind

Mining

mine a block for every 10 seconds

while true; do bitcoin-cli -regtest generate 1; sleep 10; done

Commands

$ # Unlike mainnet, in regtest mode only the first 150 blocks pay a reward of 50 bitcoins. 
$ # However, a block must have 100 confirmations before that reward can be spent
$ # so we generate 101 blocks to get access to the coinbase transaction from block #1.
$ bitcoin-cli generate 101
[
  "26d4fb4dd449b93ebbda9a36f390d2c7b1dd9557e662840fe61b1f6e3a09d218",
  ....
]

$ # regtest network address version 0x6f
$ bitcoin-cli getaccountaddress "" 
mmmSrYNiRQzeiTypt8rF9L3F742PW4ksmL

$ # lookup the mining reward
$ bitcoin-cli -regtest getbalance
50.00000000

$ # get new adddress
$ bitcoin-cli getnewaddress
mn41xHRzXuUAAVtujPLLo3Hzbe63GSP117

$ # transfer 10 BTC to new address
$ bitcoin-cli sendtoaddress mn41xHRzXuUAAVtujPLLo3Hzbe63GSP117 10
4deb2fbb98d9029a4e5808b8236d1119ad7315efdfe7406a88c923f5d4b0d5af

$ bitcoin-cli generate 1
[
    "4deb2fbb98d9029a4e5808b8236d1119ad7315efdfe7406a88c923f5d4b0d5af"
]

$ # lookup the trx detail
$ bitcoin-cli getrawtransaction 4deb2fbb98d9029a4e5808b8236d1119ad7315efdfe7406a88c923f5d4b0d5af true

$ # list received by address
$ bitcoin-cli -regtest listreceivedbyaddress
[
  {
    "address": "mn41xHRzXuUAAVtujPLLo3Hzbe63GSP117",
    "account": "",
    "amount": 10.00000000,
    "confirmations": 1,
    "label": "",
    "txids": [
      "4deb2fbb98d9029a4e5808b8236d1119ad7315efdfe7406a88c923f5d4b0d5af"
    ]
  }
]

import key

$ bitcoind help
[...]
  importprivkey <bitcoinprivkey> [label]

Private key to WIF 1 - Take a private key

   0C28FCA386C7A227600B2FE50B7CAE_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_11EC86D3BF1FBE471BE89827E19D72AA1D

2 - Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. Also add a 0x01 byte at the end if the private key will correspond to a compressed public key

   800C28FCA386C7A227600B2FE50B7C_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_AE11EC86D3BF1FBE471BE89827E19D72AA1D

3 - Perform SHA-256 hash on the extended key

   8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

4 - Perform SHA-256 hash on result of SHA-256 hash

   507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

5 - Take the first 4 bytes of the second SHA-256 hash, this is the checksum

   507A5B8D

6 - Add the 4 checksum bytes from point 5 at the end of the extended key from point 2

   800C28FCA386C7A227600B2FE50B7CAE11EC8_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_6D3BF1FBE471BE89827E19D72AA1D507A5B8D

7 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format

   5HueCGU8rMjxEXxiPuD5BDk_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_u4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

WIF to private key 1 - Take a Wallet Import Format string

   5HueCGU8rMjxEXxiPuD5BDk_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_u4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

2 - Convert it to a byte string using Base58Check encoding

   800C28FCA386C7A227600B2FE50B7CAE11EC_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_86D3BF1FBE471BE89827E19D72AA1D507A5B8D

3 - Drop the last 4 checksum bytes from the byte string

   800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

4 - Drop the first byte (it should be 0x80). If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01). If it corresponded to a compressed public key, the WIF string will have started with K or L instead of 5 (or c instead of 9 on testnet). This is the private key.

   0C28FCA386C7A227600B2FE50B7CAE1_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_1EC86D3BF1FBE471BE89827E19D72AA1D

WIF checksum checking 1 - Take the Wallet Import Format string

   5HueCGU8rMjxEXxiPuD5BD_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_ku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ

2 - Convert it to a byte string using Base58Check encoding

   800C28FCA386C7A227600B2FE50B7CAE11E_SAMPLE_PRIVATE_KEY_DO_NOT_IMPORT_C86D3BF1FBE471BE89827E19D72AA1D507A5B8D

3 - Drop the last 4 checksum bytes from the byte string

   800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D

3 - Perform SHA-256 hash on the shortened string

   8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

4 - Perform SHA-256 hash on result of SHA-256 hash

   507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714

5 - Take the first 4 bytes of the second SHA-256 hash, this is the checksum

   507A5B8D

6 - Make sure it is the same, as the last 4 bytes from point 2

   507A5B8D

7 - If they are, and the byte string from point 2 starts with 0x80 (0xef for testnet addresses), then there is no error.

The importing process is now started. Bitcoind will rescan the entire block data to ensure this key has not been used before. This process will take from one to two minutes, depending on your CPU performance. DO NOT abort it before finishing!

To avoid rescanning run the following.

$ bitcoind importprivkey 5yourveryveryveryverylongprivatekeystring" "label-here" rescan=false

If no errors occurs, the import is a success and Bitcoin-QT users will be able to see the new address in the GUI immediately. If you need to import more keys, just repeat the instructions above. There is currently no command to import a batch of private keys so you will need to wait a minute or two for each key to be imported.

@islishude
Copy link
Author

download bitcoind and bitcoin-cli with https://bitcoincore.org/bin/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment