def importAlts(): """ Update the currency information in the inuit database with data form a currencies.json file in the inuit root directory. No currencies will be removed from the database if they don;t exist in the file currency data will be overwritten with the file data """ if not os.path.isfile('currencies.json'): print('no currencies.json file in your inuit directory') return False with open('currencies.json', 'r') as dataFile: currencies = json.load(dataFile) conn = db.open() c = conn.cursor() for cur in currencies: version = cur['version'] if cur['version'] < 145 else 145 c.execute('select id from inuit_versions where version=?;', (version,)) versionId = c.fetchone() if versionId is None: continue c.execute('select id from inuit_currencies where currency=?;', (cur['currency'],)) curId = c.fetchone() if curId is None: #currency doesn't exist in the system so add it c.execute('insert into inuit_currencies (currency, longName, version) values (?,?,?);', (cur['currency'], cur['longName'], versionId[0])) else: c.execute('update inuit_currencies set currency=?, longName=?, version=? where id=?;', (cur['currency'], cur['longName'], versionId[0], curId[0])) db.close(conn) print('import finished') return True
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
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
def generate(cur, bip=False): """ public and private key generator. optional BIP0038 encryption """ #check that the given currency is in the system conn = db.open() c = conn.cursor() #pull the version details from the database c.execute('select v.version,v.prefix,v.length,c.id,c.longName,c.version from inuit_versions as v inner join inuit_currencies as c on c.version = v.id where c.currency=?;', (cur.upper(),)) version = c.fetchone() if version is None: print(cur.upper() + ' is not currently listed as a currency') return False #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(inp.keyboardEntropy()) privK256 = enc.encode(privateKey, 256, 32) publicAddress = publicKey2Address(privateKey2PublicKey(privateKey), version[0], prefix, version[2]) #optional BIP0038 encryption get.flushKeybuffer(get._Getch()) encrypt = 'y' if encrypt == 'y': bipPass1 = 'pass1' bipPass2 = 'pass2' while bipPass1 != bipPass2 or len(bipPass1) < 1: bipPass1 = inp.secure_passphrase('Enter your BIP0038 passphrase') 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!') reminder = raw_input('Enter an optional reminder for your password : '******'') #print('Enter the number of rounds of encryption.') #p = raw_input('A smaller number means quicker but less secure. (8) : ').strip() #p = 8 if p == '' else int(p) p = 8 privK = bip38.encrypt(privK256, publicAddress, bipPass1, p) isBip = True else: privK = privateKey isBip = False #save details to the database c.execute('insert into inuit_privK (privK, currency) values (?,?);', (str(privK).encode('base64','strict'), version[3])) privKid = c.lastrowid c.execute('insert into inuit_addresses (address, currency) values (?,?);', (publicAddress.encode('base64','strict'), version[3])) addId = c.lastrowid c.execute('insert into inuit_master (address, privK, version) values (?,?,?);', (addId, privKid, version[5])) if isBip is True: c.execute('insert into inuit_bip (privK, reminder, p) values (?,?,?);', (privKid, reminder, p)) db.close(conn) print('') print(version[4] + ' Address : ' + publicAddress) return privK, publicAddress
def exportAlts(): conn = db.open() c = conn.cursor() c.execute('select c.currency, c.longName, v.version from eskimo_currencies as c inner join eskimo_versions as v on c.version = v.id;') currencies = c.fetchall() db.close(conn) currs = [] for cur in currencies: currs.append({'currency': str(cur[0]), 'longName': str(cur[1]), 'version': int(cur[2])}) with open('currencies.json', 'w') as outfile: json.dump(currs, outfile) print('exported all currency data') return True
def addAlt(cur): """ add an alt currency to the inuit system. check if it already exists and prompt for a version number or private key if it doesn't """ conn = db.open() c = conn.cursor() c.execute('select id from inuit_currencies where currency=?;', (cur.upper(),)) if c.fetchone() is not None: print(cur.upper() + ' already exists in the system') db.close(conn) return longName = raw_input('Enter the full name of the currency : ').capitalize().strip() if longName == '': print('Currencies need a full name') db.close(conn) return False versionString = raw_input('Enter a private key or version number for ' + cur.upper() + ' : ').strip() if versionString == '': print('No data was entered') db.close(conn) return False #decide what we're dealing with if len(versionString) <= 3: #length of the string is up to 3 characters, it's a version number version = int(versionString) elif len(versionString) > 30 and len(versionString) < 35: #between 30 and 35 length, it's an address version = scan(versionString) elif len(versionString) > 50: #over 50 length, it's a private key version = scan(versionString, 128) else: print(versionString + ' doesn\'t look like an address or private key and is too long to be a version number.') db.close(conn) return False #all version from 145-255 have the same prefix etc. #we only store up to 145 in the database versionInt = version if version < 145 else 145 c.execute('select id from inuit_versions where version=?;', (versionInt,)) versionId = c.fetchone() if versionId is None: print('Version ' + str(versionId[0]) + ' does not exist in the system') db.close(conn) return c.execute('insert into inuit_currencies (currency, longName, version) values (?,?,?);', (cur.upper(), longName, versionId[0])) print(longName + ' is version ' + str(version)) db.close(conn) return
def showAddresses(cur): conn = db.open() c = conn.cursor() #get the current version of the currency c.execute('select version from eskimo_currencies where currency=?;', (cur.upper(),)) version = c.fetchone() c.execute('select a.address from eskimo_addresses as a inner join eskimo_currencies as c on a.currency = c.id inner join eskimo_master as m on a.id = m.address where c.currency = ? and m.version=?;', (cur.upper(), version[0])) addresses = c.fetchall() db.close(conn) if not addresses: print('No addresses found for ' + cur.upper()) return False print('') for address in addresses: print(str(address[0]).decode('base64', 'strict')) return True
def exportAlts(): """ export all currencies in the inuit database to a currencies.json file this will overwrite any existing file """ conn = db.open() c = conn.cursor() c.execute('select c.currency, c.longName, v.version from inuit_currencies as c inner join inuit_versions as v on c.version = v.id order by c.longName;') currencies = c.fetchall() db.close(conn) currs = [] for cur in currencies: currs.append({'currency': str(cur[0]), 'longName': str(cur[1]), 'version': int(cur[2])}) with open('currencies.json', 'w') as outfile: json.dump(currs, outfile) print('exported all currency data') return True
def editAlt(cur): """ edit a currency that is already in the system """ conn = db.open() c = conn.cursor() c.execute('select c.id,c.currency,c.longName,v.version from inuit_currencies as c inner join inuit_versions as v on c.version = v.id where c.currency=?;', (cur.upper(), )) curId = c.fetchone() if curId is None: print(cur.upper() + ' doesn\'t exist in the system') db.close(conn) return False newCur = raw_input('Enter the new currency abbreviation (' + curId[1] + ') : ').upper().strip() newCur = curId[1] if newCur == '' else newCur newLongName = raw_input('Enter the new full name (' + curId[2] + ') : ').capitalize().strip() newLongName = curId[2] if newLongName == '' else newLongName versionString = raw_input('Enter a private key or version number for ' + newCur.upper() + ' (' + str(curId[3]) + ') : ').strip() if versionString == '': version = curId[3] #decide what we're dealing with elif len(versionString) > 0 and len(versionString) <= 3: #length of the string is up to 3 characters, it's a version number version = int(versionString) elif len(versionString) > 30 and len(versionString) < 35: #between 30 and 35 length, it's an address version = scan(versionString) elif len(versionString) > 50: #over 50 length, it's a private key version = scan(versionString, 128) else: print(versionString + ' doesn\'t look like an address or private key and is too long to be a version number.') version = curId[3] versionInt = version if version < 145 else 145 c.execute('select id from inuit_versions where version=?;', (versionInt,)) versionDb = c.fetchone() if versionDb is None: print('Version ' + str(version) + ' does not exist in the system') return False else: newVersion = versionDb[0] c.execute('update inuit_currencies set currency=?, longName=?, version=? where id=?;', (newCur, newLongName, newVersion, curId[0])) db.close(conn) print(newCur + ' saved as version ' + str(version)) return True
def showCurrencies(): conn = db.open() c = conn.cursor() c.execute('select c.id,c.currency,c.longName,v.version,v.id from eskimo_currencies as c inner join eskimo_versions as v on c.version=v.id order by c.currency;') currencies = c.fetchall() if not currencies: print('No currencies exist in the system') return False print('') for currency in currencies: c.execute('select count(a.id) from eskimo_addresses as a inner join eskimo_master as m on a.id = m.address where a.currency=? and m.version=?;', (currency[0],currency[4])) addr = c.fetchone() if addr is None: numAddr = 0 else: numAddr = addr[0] print('{0: ^8}'.format(str(currency[1])) + ' | ' + '{0: ^16}'.format(str(currency[2])) + ' | ' + '{0:>4}'.format(str(currency[3])) + ' | ' + '{0:>4}'.format(str(numAddr))) db.close(conn) return True
def importAlts(): if not os.path.isfile('currencies.json'): print('no currencies.json file in your eskimo directory') return False with open('currencies.json', 'r') as dataFile: currencies = json.load(dataFile) conn = db.open() c = conn.cursor() for cur in currencies: version = cur['version'] if cur['version'] < 145 else 145 c.execute('select id from eskimo_versions where version=?;', (version,)) versionId = c.fetchone() if versionId is None: continue c.execute('select id from eskimo_currencies where currency=?;', (cur['currency'],)) curId = c.fetchone() if curId is None: #currency doesn't exist in the system so add it c.execute('insert into eskimo_currencies (currency, longName, version) values (?,?,?);', (cur['currency'], cur['longName'], versionId[0])) else: c.execute('update eskimo_currencies set currency=?, longName=?, version=? where id=?;', (cur['currency'], cur['longName'], versionId[0], curId[0])) db.close(conn) print('import finished') return True
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