Skip to content

How to Convert Bitcoin-PUBKEY HEX Public Keys to Base58 Bitcoin Address and Check Balance for BTC Coins

Notifications You must be signed in to change notification settings

demining/Check-Bitcoin-Address-Balance

Repository files navigation

How to Convert Bitcoin-PUBKEY HEX Public Keys to Base58 Bitcoin Address and Check Balance for BTC Coins

In this article, we will learn how to check the balance of Bitcoin coins in a large amount of data using the bitcoin-checker.py Python script for  this .

The result of checking the Python script bitcoin-checker.py

The result of checking the Python script bitcoin-checker.py

We will also learn how to convert the public key of Bitcoin  PUBKEY (HEX) to Bitcoin Address  (Base58) All this big work is done  by the Python script  pubtoaddr.py

As a result, we will check the balance of Bitcoin with particular ease by scanning the Blockchain in the Google Colab terminal  [TerminalGoogleColab]

Earlier I recorded a video tutorial:  “TERMINAL in Google Colab creating all the conveniences for working in GITHUB”

Let’s go to the  «CryptoDeepTools» repository  and take a closer look at how the  Bash script works: getbalance.sh

Teams

Teams

Files

Files

Our Bash script code: getbalance.sh

Our Bash script code: getbalance.sh
grep 'PUBKEY = ' signatures.json > pubkeyall.json

The utility  grep collects all public keys into one common file: pubkeyall.json

sort -u pubkeyall.json > pubkey.json

The utility  sort sorts and removes duplicates, selects unique public keys and saves the result to a file: pubkey.json

rm pubkeyall.json

The utility  rm removes pubkeyall.json

sed -i 's/PUBKEY = //g' pubkey.json

The utility  sed erases the prefix PUBKEY =

python3 pubtoaddr.py

We  run the Python script  pubtoaddr.py and  convert from the file  pubkey.json where our public Bitcoin keys are stored  PUBKEY (HEX) to the file  addresses.json the result will be saved as Bitcoin Addresses (Base58)

import hashlib
import base58

def hash160(hex_str): sha = hashlib.sha256() rip = hashlib.new('ripemd160') sha.update(hex_str) rip.update(sha.digest()) return rip.hexdigest() # .hexdigest() is hex ASCII

pub_keys = open('pubkey.json', 'r', encoding='utf-8') new_file = open('addresses.json', 'a', encoding='utf-8') compress_pubkey = False

for pub_key in pub_keys: pub_key = pub_key.replace('\n', '') if compress_pubkey: if (ord(bytearray.fromhex(pub_key[-2:])) % 2 == 0): pubkey_compressed = '02' else: pubkey_compressed = '03' pubkey_compressed += pub_key[2:66] hex_str = bytearray.fromhex(pubkey_compressed) else: hex_str = bytearray.fromhex(pub_key)

key_hash = '00' + hash160(hex_str)


sha = hashlib.sha256()
sha.update(bytearray.fromhex(key_hash))
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]

new_file.write("" + (base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8'))

new_file.write((base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8') + "\n")

pub_keys.close() new_file.close()

We have received the file  addresses.json , now we will check the balance of Bitcoin coins using the bitcoin-checker.py  Python script for this

Run  Python script : python2 bitcoin-checker.py

import sys
import re
from time import sleep

try: # if is python3 from urllib.request import urlopen except: # if is python2 from urllib2 import urlopen

def check_balance(address):

#Modify the value of the variable below to False if you do not want Bell Sound when the Software finds balance.
SONG_BELL = True

#Add time different of 0 if you need more security on the checks
WARN_WAIT_TIME = 0

blockchain_tags_json = [ 
    'total_received',
    'final_balance',
    ]

SATOSHIS_PER_BTC = 1e+8

check_address = address

parse_address_structure = re.match(r' *([a-zA-Z1-9]{1,34})', check_address)
<strong>if</strong> ( parse_address_structure <strong>is</strong> <strong>not</strong> None ):
    check_address = parse_address_structure.group(1)
<strong>else</strong>:
    print( "\nThis Bitcoin Address is invalid" + check_address )
    exit(1)

#Read info from Blockchain about the Address
reading_state=1
<strong>while</strong> (reading_state):
    <strong>try</strong>:
        htmlfile = urlopen("https://blockchain.info/address/%s?format=json" % check_address, timeout = 10)
        htmltext = htmlfile.read().decode('utf-8')
        reading_state  = 0
    <strong>except</strong>:
        reading_state+=1
        print( "Checking... " + str(reading_state) )
        sleep(60*reading_state)

print( "\nBitcoin Address = " + check_address )

blockchain_info_array = []
tag = ''
<strong>try</strong>:
    <strong>for</strong> tag <strong>in</strong> blockchain_tags_json:
        blockchain_info_array.append (
            float( re.search( r'%s":(\d+),' % tag, htmltext ).group(1) ) )
<strong>except</strong>:
    print( "Error '%s'." % tag );
    exit(1)

<strong>for</strong> i, btc_tokens <strong>in</strong> enumerate(blockchain_info_array):

    sys.stdout.write ("%s \t= " % blockchain_tags_json[i])
    <strong>if</strong> btc_tokens &gt; 0.0:
        print( "%.8f Bitcoin" % (btc_tokens/SATOSHIS_PER_BTC) );
    <strong>else</strong>:
        print( "0 Bitcoin" );

    <strong>if</strong> (SONG_BELL <strong>and</strong> blockchain_tags_json[i] == 'final_balance' <strong>and</strong> btc_tokens &gt; 0.0): 
        
        #If you have a balance greater than 0 you will hear the bell
        sys.stdout.write ('\a\a\a')
        sys.stdout.flush()

        arq1.write("Bitcoin Address: %s" % check_address)
        arq1.write("\t Balance: %.8f Bitcoin" % (btc_tokens/SATOSHIS_PER_BTC))
        arq1.write("\n")
        arq1.close()
        <strong>if</strong> (WARN_WAIT_TIME &gt; 0):
            sleep(WARN_WAIT_TIME)

#Add the filename of your list of Bitcoin Addresses for check all. with open("addresses.json") as file: for line in file:

	arq1 = open('balance.json', 'a')
    address = str.strip(line)
    <strong>print</strong> ("__________________________________________________\n")
    
    check_balance(address)

print "__________________________________________________\n" arq1.close()

As a result, the result will be saved in a file: balance.json

File: balance.json

File: balance.json

Now we have learned:

  • Convert Bitcoin Public Keys  PUBKEY (HEX) to Bitcoin Address (Base58)
  • Check all Bitcoin Addresses (Base58) for Bitcoin coins
  • Apply this for cryptanalysis

Source Code:  https://github.com/demining/CryptoDeepTools/tree/main/03CheckBitcoinAddressBalance

Telegram:  https://t.me/cryptodeeptech

Video:  https://youtu.be/Hsk6QIzb7oY

Source: https://cryptodeeptech.ru/check-bitcoin-address-balance

---
Donation Address
BTC 1Lw2kh9WzCActXSGHxyypGLkqQZfxDpw8v
ETH 0xaBd66CF90898517573f19184b3297d651f7b90bf