Exemple #1
0
def genAll(bip=False):
	import random
	import num.rand as rand
	import system.address as address
	import num.enc as enc
	import encrypt.bip38 as bip38
	import hashlib
	
	genFile = open('allKeys', 'w')
	genFile.close()
	
	conn = db.open()
	c = conn.cursor()
	c.execute('select currency from eskimo_currencies order by currency;')
	currencies = c.fetchall()
	for cur in currencies:
		c.execute('select v.version,v.prefix,v.length,c.id,c.longName from eskimo_versions as v inner join eskimo_currencies as c on c.version = v.id where c.currency=?;', (cur[0].upper(),))
		version = c.fetchone()
		if version is None:
			continue
		#randomly choose a prefix if multiples exist
		prefixes = version[1].split('|') 
		prefix = prefixes[random.randint(0, (len(prefixes)-1))] 
		#generate the private and public keys
		privateKey = rand.randomKey(random.getrandbits(512))
		privK256 = enc.encode(privateKey, 256, 32)
		WIF = address.privateKey2Wif(privateKey, version[0], prefix,  version[2])
		publicAddress = address.publicKey2Address(address.privateKey2PublicKey(privateKey), version[0], prefix,  version[2])
		if bip is True:
			BIP = bip38.encrypt(privK256, publicAddress, 'biptest', 1)
			privK, addresshash = bip38.decrypt(BIP, 'biptest', 1)
			#decode the privK from base 256
			privK = enc.decode(privK, 256)
			#hash the address to check the decryption
			addr = address.publicKey2Address(address.privateKey2PublicKey(privK), version[0], prefix,  version[2])
			fail = False
			if hashlib.sha256(hashlib.sha256(addr).digest()).digest()[0:4] != addresshash:
				fail = True
				reason = 'Address Hash doesn\'t match'
			if privK != privateKey:
				fail = True
				reason = 'Private Keys don\'t match'
			BIPWIF =  address.privateKey2Wif(privK, version[0], prefix,  version[2])
		with open('allKeys', 'a') as outfile:
			outfile.write('####### ' + cur[0].upper() + ' - ' + version[4] + ' #######\n')
			outfile.write('Address = ' + publicAddress + '\n')
			outfile.write('WIF     = ' + WIF + '\n')
			if bip is True:
				outfile.write('BIP     = ' + BIP + '\n')
				if fail is True:
					outfile.write('BIP Failed - ' + reason + '\n')
				else:
					outfile.write('BIPWIF  = ' + BIPWIF + '\n')
			outfile.write('\n')
		outfile.close()
	db.close(conn)
	return True
Exemple #2
0
def dumpPrivKey(address):
	'''
		retrieve private key from database for given address
		option to decrypt BIP0038 encrypted keys
		display as base58 and WIF
	'''
	conn = db.open()
	c = conn.cursor()
	#get the needed data from the database
	c.execute('select p.id,p.privK,v.version,v.prefix,v.length,c.longName from eskimo_privK as p inner join eskimo_master as m on p.id = m.privK inner join eskimo_addresses as a on a.id = m.address inner join eskimo_currencies as c on p.currency = c.id inner join eskimo_versions as v on c.version = v.id where a.address=?;', (address.encode('base64', 'strict'),))
	privData = c.fetchone()
	if privData is None:
		print(address + ' was not found')
		return False
	#check if the private key is bip encoded and get the password reminder if it is
	c.execute('select reminder, p from eskimo_bip where privK=?;', (privData[0],))
	bip = c.fetchone()
	if bip is None:
		isBip = False
	else:
		isBip = True
		reminder = bip[0]
		p = bip[1]
	privK = privData[1].decode('base64', 'strict')
	#ask if the user wants to decrypt a bip encrypted key
	if isBip:
		print('The private key found is BIP0038 encrypted.')
		decrypt = raw_input('Would you like to decrypt it? (n) ').lower().strip()
		if decrypt == 'y':
			bipPass1 = 'pass1' 
			bipPass2 = 'pass2'
			while bipPass1 != bipPass2 or len(bipPass1) < 1:
				bipPass1 = inp.secure_passphrase('Enter your BIP0038 passphrase ' + ('(' + bip[0] + ')' if bip[0] != '' else ''))
				bipPass2 = inp.secure_passphrase('Re-enter your passphrase to confirm')
				if bipPass1 != bipPass2:
					print('The passphrases entered did not match.')
				elif len(bipPass1) < 1:
					print('No passphrase was entered')
			#decrypt the private key using the supplied password
			privK, addresshash = bip38.decrypt(privK, bipPass1, p)
			#decode the privK from base 256
			privK = enc.decode(privK, 256)
			#hash the address to check the decryption
			address = publicKey2Address(privateKey2PublicKey(privK), privData[2], privData[3], privData[4])
			if hashlib.sha256(hashlib.sha256(address).digest()).digest()[0:4] != addresshash:
				print('\nUnable to decrypt.')
				print('Please try again with a different passphrase.')
				return False
		else:
			print('\n' + privData[5] + ' Address = ' + str(address))
			print('\nBIP0038 encrypted private key : ' + privK)
			return True	
	print('\n' + privData[5] + ' Address = ' + str(address))			
	print('\nPrivate key : ')
	print('HEX : ' + enc.encode(privK, 16))
	print('WIF : ' + privateKey2Wif(privK, privData[2], privData[3], privData[4]))
	return True
Exemple #3
0
def decBIPKey(encrypted_privK, passphrase):
    """
        Decrypt an encrypted Private key
        Show the corresponding public address
    """
    privK, addresshash = bip38.decrypt(str(encrypted_privK), str(passphrase))
    privK = enc.decode(privK, 256)
    # calculate the addresses from the key
    bPublicAddress, sPublicAddress = address.publicKey2Address(
        address.privateKey2PublicKey(privK))
    #check our generated address against the address hash from BIP
    if hashlib.sha256(
            hashlib.sha256(bPublicAddress + sPublicAddress).digest()).digest(
            )[0:4] != addresshash:
        return False, False, False
    else:
        return address.privateKey2Wif(privK), bPublicAddress, sPublicAddress
Exemple #4
0
def decBIPKey(encrypted_privK, passphrase, currency):
	"""
	Decrypt an encrypted Private key
	Show the corresponding public address
	"""
	#using the currencies.json file, get the currency data
	with open('res/json/currencies.json', 'r') as dataFile:
		currencies = json.load(dataFile)
	for cur in currencies:
		if cur['currency'] == currency:
			break 
	#randomly choose a prefix if multiples exist
	prefixes = cur['prefix'].split('|')
	prefix = prefixes[random.randint(0, (len(prefixes)-1))]
	#decrypt the BIP key
	PrivK, Addresshash = bip38.decrypt(str(encrypted_privK), str(passphrase), 8)
	PrivK = enc.decode(PrivK, 256)
	#calculate the address from the key
	publicAddress = address.publicKey2Address(address.privateKey2PublicKey(PrivK), int(cur['version']), prefix, int(cur['length']))
	#check our generated address against the address hash from BIP
	if hashlib.sha256(hashlib.sha256(publicAddress).digest()).digest()[0:4] != Addresshash:
		return False, False
	else:
		return address.privateKey2Wif(PrivK, cur['version'], prefix, cur['length']), publicAddress
Exemple #5
0
def dumpPrivKey(address):
    """
		retrieve private key from database for given address
		option to decrypt BIP0038 encrypted keys
		display as base58 and WIF
	"""
    conn = db.open()
    c = conn.cursor()
    # get the needed data from the database
    c.execute(
        "select p.id,p.privK,v.version,v.prefix,v.length,c.longName from inuit_privK as p inner join inuit_master as m on p.id = m.privK inner join inuit_addresses as a on a.id = m.address inner join inuit_currencies as c on p.currency = c.id inner join inuit_versions as v on c.version = v.id where a.address=?;",
        (address.encode("base64", "strict"),),
    )
    privData = c.fetchone()
    if privData is None:
        print(address + " was not found")
        return False
        # check if the private key is bip encoded and get the password reminder if it is
    c.execute("select reminder, p from inuit_bip where privK=?;", (privData[0],))
    bip = c.fetchone()
    if bip is None:
        isBip = False
    else:
        isBip = True
        reminder = bip[0]
        p = bip[1]
    privK = privData[1].decode("base64", "strict")
    # ask if the user wants to decrypt a bip encrypted key
    if isBip:
        print("The private key found is BIP0038 encrypted.")
        decrypt = raw_input("Would you like to decrypt it? (n) ").lower().strip()
        if decrypt == "y":
            bipPass1 = "pass1"
            bipPass2 = "pass2"
            while bipPass1 != bipPass2 or len(bipPass1) < 1:
                bipPass1 = inp.secure_passphrase(
                    "Enter your BIP0038 passphrase " + ("(" + bip[0] + ")" if bip[0] != "" else "")
                )
                bipPass2 = inp.secure_passphrase("Re-enter your passphrase to confirm")
                if bipPass1 != bipPass2:
                    print("The passphrases entered did not match.")
                elif len(bipPass1) < 1:
                    print("No passphrase was entered")
                    # decrypt the private key using the supplied password
            privK, addresshash = bip38.decrypt(privK, bipPass1, p)
            # decode the privK from base 256
            privK = enc.decode(privK, 256)
            # hash the address to check the decryption
            address = publicKey2Address(privateKey2PublicKey(privK), privData[2], privData[3], privData[4])
            if hashlib.sha256(hashlib.sha256(address).digest()).digest()[0:4] != addresshash:
                print("\nUnable to decrypt.")
                print("Please try again with a different passphrase.")
                return False
        else:
            print("\n" + privData[5] + " Address = " + str(address))
            print("\nBIP0038 encrypted private key : " + privK)
            return True
    print("\n" + privData[5] + " Address = " + str(address))
    print("\nPrivate key : ")
    print("HEX : " + enc.encode(privK, 16))
    print("WIF : " + privateKey2Wif(privK, privData[2], privData[3], privData[4]))
    return True