Example #1
0
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
Example #2
0
def keyboardEntropy(keynum=64):
	"""
	512 bit random number from keyboard and keypress timer
	"""

	keypress = get._Getch()
	typed = kr = 'Press some keys to generate a secure address...'
	hashes = rand.clockrnd()
	print(kr)
	for step in range(keynum, 0, -1):
		for cnt in xrange(10000000):  # only loops on OSX
			hashes ^= rand.clockrnd()
			kr = keypress()
			get.flushKeybuffer(keypress)
			if kr != '':
				break
		typed += kr
		hashes ^= rand.clockrnd()
		out.prnt('\b\b\b\b\b\b{0:4d}\b\b\b\b\b'.format(step-1))
	get.flushKeybuffer(keypress)
	out.prnt('\b\b\b\b\b\b{0:4s}\b\b\b\b\b\n'.format('  OK'))
	get.flushKeybuffer(keypress)
	return hashes ^ int(hashlib.sha512(typed*8).hexdigest(), 16)