如何將 Bitcoin-PUBKEY HEX 公鑰轉換為 Base58 比特幣地址並檢查 BTC 硬幣的餘額

在本文中,我們將學習如何使用bitcoin-checker.py Python 腳本 來檢查大量數據中的比特幣硬幣餘額 。

檢查Python腳本bitcoin-checker.py的結果
檢查Python腳本bitcoin-checker.py的結果


我們還將學習如何將比特幣的公鑰轉換 PUBKEY (HEX) 為比特幣地址 (Base58) 所有這些大工作都是由 Python 腳本 pubtoaddr.py完成的

因此,我們將通過在 Google Colab 終端 [TerminalGoogleColab]中掃描區塊鏈來輕鬆檢查比特幣餘額

早些時候我錄製了一個視頻教程:  “Google Colab 中的 TERMINAL 為在 GITHUB 中工作創造了所有便利”

讓我們轉到 “CryptoDeepTools”存儲庫 ,仔細看看 Bash 腳本的工作原理:getbalance.sh

命令:

git clone https://github.com/demining/CryptoDeepTools.git

cd CryptoDeepTools/03CheckBitcoinAddressBalance/

sudo apt install python2-minimal

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py

sudo python2 get-pip.py

pip3 install -r requirements.txt

chmod +x getbalance.sh

./getbalance.sh

文件
文件
我們的 Bash 腳本代碼:getbalance.sh
我們的 Bash 腳本代碼:getbalance.sh
grep 'PUBKEY = ' signatures.json > pubkeyall.json

該實用程序 grep 將所有公鑰收集到一個公共文件中: pubkeyall.json

sort -u pubkeyall.json > pubkey.json

該實用程序 sort 排序並刪除重複項,選擇唯一的公鑰並將結果保存到文件中: pubkey.json

rm pubkeyall.json

該實用程序 rm 刪除 pubkeyall.json

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

該實用程序 sed 擦除前綴 PUBKEY =

python3 pubtoaddr.py

我們運行 Python 腳本 pubtoaddr.pypubkey.json並將 存儲比特幣公鑰的  文件轉換為文件, PUBKEY (HEX) 結果 addresses.json 將保存為比特幣地址 (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()

我們已經收到文件, 現在我們將使用bitcoin-checker.py Python 腳本addresses.json 檢查 比特幣餘額 

運行 Python 腳本: 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)
    if ( parse_address_structure is not None ):
        check_address = parse_address_structure.group(1)
    else:
        print( "\nThis Bitcoin Address is invalid" + check_address )
        exit(1)

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

    print( "\nBitcoin Address = " + check_address )

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

    for i, btc_tokens in enumerate(blockchain_info_array):

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

        if (SONG_BELL and blockchain_tags_json[i] == 'final_balance' and btc_tokens > 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()
            if (WARN_WAIT_TIME > 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)
        print ("__________________________________________________\n")
        
        check_balance(address)
print "__________________________________________________\n"
arq1.close()

結果,結果將保存在一個文件中: balance.json

文件:balance.json
文件:balance.json

現在我們了解到:

  • 將比特幣公鑰轉換 PUBKEY (HEX) 為比特幣地址 (Base58)
  • 檢查比特幣硬幣的所有比特幣地址 (Base58)
  • 將此應用於密碼分析

源代碼:  https://github.com/demining/CryptoDeepTools/tree/main/03CheckBitcoinAddressBalance

電報:  https: //t.me/cryptodeeptech

視頻素材:  https: //youtu.be/Hsk6QIzb7oY

 密碼分析

Crypto Deep Tech