0

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.")

The output is below: enter image description here

3
  • Is the HTTP status code == 200? Is exception raised or not? Commented May 30, 2019 at 10:41
  • Try to print something in except: block and check. May be there was exceptions while getting the result. Commented May 30, 2019 at 10:48
  • @IvanVinogradov status code of the request is 200 with no exception raised Commented May 30, 2019 at 11:05

2 Answers 2

1

The problem is that you are using some variables that are not in the function scope:

def scanether(balance):
    try:
        # Convert private key to address and print the result
        eth_address = ethereum.utils.privtoaddr(INPUTFILE)
        ...

        # 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')
            ...

    except Exception as e:
        return

Here INPUTFILE and OUTPUTFILE are not in the scope, it will raise an exception that is captured and then the function simply returns...

You need to pass them as arguments:

def scanether(balance, INPUTFILE, OUTPUTFILE):
    ...


...

    print("Scan in progress...")
    pool = Pool(processes=MAXPROCESSES)
    def scanether_wrapper(address, ifile=INPUTFILE, ofile=OUTPUTFILE):
        return scanether(address, ifile, ofile)
    pool.map(scanether_wrapper, addresses)
    print("Scan finished.")
Sign up to request clarification or add additional context in comments.

2 Comments

that was my first try, the output is same (empty)
@Awesim, what is the current output then? can you update your question with the full information?
0

You can use below code for reference. Before handling the response you should be sure whether the requests you have made is successful.

Refer the below code for further reference.

import requests
url  = "https://******.com"
r = requests.get(url)
print(r.status_code)

Always check whether the status code is 200 before working with response.

Thank You!

1 Comment

a bit unnecessary for people who know what HTTP is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.