Skip to content

Instantly share code, notes, and snippets.

@benjamin-m-hodgson
Last active September 10, 2018 14:22
Show Gist options
  • Save benjamin-m-hodgson/49e15c5722898949780eace6e9fc4da3 to your computer and use it in GitHub Desktop.
Save benjamin-m-hodgson/49e15c5722898949780eace6e9fc4da3 to your computer and use it in GitHub Desktop.
Python script that prompts the user for alphanumeric input and then uses the Bitcoin Core testnet to generate addresses until one is generated that contains the user input.
import sys
import subprocess
import os
import time
'''
@author Ben Hodgson
Python script to generate addresses using the Bitcoin core test
client until the user inputted text is present within the address.
Example call in Windows PowerShell:
python.exe '.\Brute Bitcoin Vanity Address.py' "user input"
"user input": a string desired in the generated address
'''
# Executes brute force attempt to generate an address containing phrase
def execute(phrase):
# constants to avoid infeasible execution time
loopMax = 200
phraseLenMax = 4
if (len(phrase) > phraseLenMax):
print "Phrase too long"
print "Maximum phrase length: " + str(phraseLenMax)
return
# store any vanity discoveries
vanityAddresses = []
addressCount = 0
startTime = time.clock()
while len(vanityAddresses) == 0:
# avoid infinite looping
if (addressCount >= loopMax):
break;
runCommand(vanityAddresses)
addressCount += 1
endTime = time.clock()
# check to see if the break was caused by a vanity discovery
checkFound(vanityAddresses, phrase)
print "Finished search in " + str(endTime - startTime) + " seconds"
print "Generated " + str(addressCount) + " addresses\n"
# command to be run by each thread to generate a new random address
def runCommand(vanityAddresses):
command = ['bitcoin-cli.exe', '-regtest', 'getnewaddress']
out = subprocess.check_output(command , stdin=subprocess.PIPE).strip()
print out
if (phrase in out):
vanityAddresses.append(out)
# if vanity addresses were discovered, print them
def checkFound(vanityAddresses, phrase):
if len(vanityAddresses) == 0:
return
print "Found in: "
for vanity in vanityAddresses:
print '\t' + friendlyDisplay(vanity, phrase)
# remove invalid characters from user input to avoid unsolvable inputs
def cleanInput(phrase):
validChars = "abcdefghijklmnopqrstuvwxyz0123456789"
cleanPhrase = ""
for char in phrase:
if char.lower() in validChars.lower():
cleanPhrase += char
print "\nExecuting search for phrase: " + cleanPhrase +"\n"
return cleanPhrase
# displays the location of the phrase in the address in a very readable way
def friendlyDisplay(address, phrase):
removePhraseAddress = address.split(phrase)
friendlyAddress = removePhraseAddress[0]
for addressChunk in removePhraseAddress[1:]:
friendlyAddress += " " + phrase + " "
friendlyAddress += addressChunk
return friendlyAddress
if __name__ == "__main__":
# Navigate to the correct directory
directoryPath = "C:\\Program Files\\Bitcoin\\daemon"
os.chdir(directoryPath)
# print os.getcwd();
# obtain user inputted argument
phrase = sys.argv[1]
cleanPhrase = cleanInput(phrase)
execute(cleanPhrase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment