jeudi 30 mai 2019

How to compare request.get result in if else statement

I am making a new script which will take as input list of Ethereum private keys and produce the corresponding addresses with balances, and save the balance in the file if it's found along with private key and the address.

Now, I'm almost certain that my problem lies within the conditional, but cannot figure it out.

Script steps: 1. Take file of private keys as input (-i flag) 2. Convert them to public addresses/decode them 3. Trigger an API call to Etherscan for the information about the address 4. If the json()["result"] > 0 in API call, write it in output file (-o flag), else print out and sleep for 1 second

enter image description here

Can anyone give a heads up on where I am making mistake?

My code:

#!/usr/bin/python
import sys, os, argparse, requests, ethereum, binascii, time
from multiprocessing import Pool

def scanether(balance):
    try:
        # Convert private key to address and print the result
        eth_address = ethereum.utils.privtoaddr(INPUTFILE)
        eth_address_hex = binascii.hexlify(eth_address).decode("utf-8")
        eth_balance = requests.get("https://api.etherscan.io/api?module=account&action=balance&address=0x" + eth_address_hex + "&tag=latest&apikey=APIKEYHERE").json()["result"]

        # Check if the result is > 0
        if ('result' != 0) in r.eth_balance: 
            print("[*] Address with balance found: " + eth_address_hex + priv)
            # Write match to OUTPUTFILE
            fHandle = open(OUTPUTFILE,'a')
            fHandle.write(eth_address_hex + privkey + "\n")
            fHandle.close()
        else:
            print("balance: {} address: 0x{} privkey: {}".format(float(eth_balance)/100000000, eth_address_hex, priv))
            time.sleep(1)


    except Exception as e:
        return

if __name__ == '__main__':
    print("""
# Finding the Ethereum address with balance
        """)
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-i', '--inputfile', default='input.txt', help='input file')
    parser.add_argument('-o', '--outputfile', default='output.txt', help='output file')
    parser.add_argument('-t', '--threads', default=200, help='threads')
    args = parser.parse_args()

    INPUTFILE=args.inputfile
    OUTPUTFILE=args.outputfile
    MAXPROCESSES=int(args.threads)

    try:
        addresses = open(INPUTFILE, "r").readlines()
    except FileNotFoundError as e:
        print(e)
        exit(e.errno)

    print("Scan in progress...")
    pool = Pool(processes=MAXPROCESSES)
    pool.map(scanether, addresses)
    print("Scan finished.")

Aucun commentaire:

Enregistrer un commentaire