async def get_has_balance_prikey(self, num): res = [] k = Key(num) addr, priv = k.address(), k.wif() try: balance = await self.get_bitcoin_balance(addr) if int(balance['confirmed']) or int(balance['unconfirmed']) > 0: res.append(priv) except Exception as e: logger.error(e) logger.error('%s: %s' % (num, addr)) addr2, priv2 = k.address(use_uncompressed=True), k.wif( use_uncompressed=True) try: balance = await self.get_bitcoin_balance(addr2) if int(balance['confirmed']) > 0 or int( balance['unconfirmed']) > 0: res.append(priv2) except Exception as e: logger.error(e) logger.error('%s: %s' % (num, addr)) pairs = {addr: priv, addr2: priv2} # await self._send_addr_priv(pairs) return res
async def generate_keypairs(self, num): pairs = {} for i in range(num, num + 128): k = Key(i) addr, priv = k.address(), k.wif() addr2, priv2 = k.address(use_uncompressed=True), k.wif( use_uncompressed=True) pairs.update({addr: priv, addr2: priv2}) await self._send_addr_priv(pairs)
def test_dice_generate(rolls, dev, cap_menu, pick_menu_item, goto_home, cap_story, need_keypress, microsd_path): # verify the math for dice rolling method goto_home() pick_menu_item('Advanced') try: pick_menu_item('Paper Wallets') except: raise pytest.skip('Feature absent') time.sleep(0.1) title, story = cap_story() assert 'pick a random' in story assert 'MANY RISKS' in story need_keypress('y') time.sleep(0.1) pick_menu_item('Use Dice') for ch in rolls: time.sleep(0.01) need_keypress(ch) need_keypress('y') time.sleep(0.1) if len(rolls) < 99: title, story = cap_story() assert 'need 50' in story need_keypress('y') time.sleep(0.4) title, story = cap_story() assert 'Created file' in story story = [i for i in story.split('\n') if i] fname = story[-1] assert fname.endswith('.txt') addr,_ = fname.split('.') if '-' in addr: # junk in working dir addr,_ = addr.split('-') path = microsd_path(fname) with open(path, 'rt') as fp: hx = re.findall(r'[0-9a-f]{64}', fp.read()) assert len(hx) == 1 val, = hx k2 = Key(secret_exponent=from_bytes_32(a2b_hex(val)), is_compressed=True, netcode='XTN') assert addr == k2.address() assert val == sha256(rolls.encode('ascii')).hexdigest() os.unlink(path)
def make_tx(i): key = Key(12345 * (i + 29)) script = standard_tx_out_script(key.address()) txs_in = [ TxIn(make_hash(i * 10000 + idx), (i + idx) % 2) for idx in range(3) ] txs_out = [TxOut(i * 40000, script) for idx in range(2)] tx = Tx(1, txs_in, txs_out) return tx
async def get_key_pairs(num): key = get_key_of_key_pair_of_number(num) v = rs.get(key) if v: return json.loads(v) keypairs = [] k = Key(num) addr, priv = k.address(), k.wif() try: balance = await get_bitcoin_balance(addr) keypairs.append({ 'priv': priv, 'addr': addr, 'confirmed': balance['confirmed'], 'unconfirmed': balance['unconfirmed'] }) except Exception as e: logger.error(e) pass addr2, priv2 = k.address(use_uncompressed=True), k.wif(use_uncompressed=True) try: balance = await get_bitcoin_balance(addr2) keypairs.append({ 'priv': priv2, 'addr': addr2, 'confirmed': balance['confirmed'], 'unconfirmed': balance['unconfirmed'] }) except Exception as e: logger.error(e) pass if keypairs and len(keypairs) == 2: rs.set(key, json.dumps(keypairs), FIVE_DAYS) return keypairs
def f(public_pair): yield ("public_pair_x", '%d' % public_pair[0], None) yield ("public_pair_y", '%d' % public_pair[1], None) yield ("public_pair_x_hex", '%x' % public_pair[0], " x as hex") yield ("public_pair_y_hex", '%x' % public_pair[1], " y as hex") yield ("y_parity", "odd" if (public_pair[1] & 1) else "even", None) key = Key(public_pair=public_pair) yield ("key_pair_as_sec", b2h(key.sec(is_compressed=True)), None) yield ("key_pair_as_sec_uncompressed", b2h(key.sec(is_compressed=False)), " uncompressed") network_name = network.network_name hash160_u = key.hash160(is_compressed=False) hash160_c = key.hash160(is_compressed=True) yield ("hash160", b2h(hash160_c), None) if hash160_c and hash160_u: yield ("hash160_uncompressed", b2h(hash160_u), " uncompressed") address = network.address.for_p2pkh(hash160_c) yield ("address", address, "%s address" % network_name) yield ("%s_address" % network.symbol, address, "legacy") address = key.address(is_compressed=False) yield ("address_uncompressed", address, "%s address uncompressed" % network_name) yield ("%s_address_uncompressed" % network.symbol, address, "legacy") # don't print segwit addresses unless we're sure we have a compressed key if hash160_c and hasattr(network.address, "for_p2pkh_wit"): address_segwit = network.address.for_p2pkh_wit(hash160_c) if address_segwit: # this network seems to support segwit yield ("address_segwit", address_segwit, "%s segwit address" % network_name) yield ("%s_address_segwit" % network.symbol, address_segwit, "legacy") p2sh_script = network.contract.for_p2pkh_wit(hash160_c) p2s_address = network.address.for_p2s(p2sh_script) if p2s_address: yield ("p2sh_segwit", p2s_address, None) p2sh_script_hex = b2h(p2sh_script) yield ("p2sh_segwit_script", p2sh_script_hex, " corresponding p2sh script")
bob_ephem_key = None for bob_pcode_node in bob_pcode_nodes.values(): pcode_pp = bob_pcode_node.public_pair() pcode_point = ellipticcurve.Point(curve, pcode_pp[0], pcode_pp[1]) secret_point = alice_notif_exp * pcode_point secret_der = keys.VerifyingKey.from_public_point(secret_point, curves.SECP256k1).to_der() secret = sha256(secret_der).digest() try: secret_ephem_point = bytes_to_num(secret) * secp256k1.generator_secp256k1 except NoSuchPointError: print "Point not on curve. Using next." continue bob_ephem_point = pcode_point + secret_ephem_point bob_ephem_key = Key(public_pair=bob_ephem_point.pair(), netcode="XTN") print "Ephemeral point found:\n", bob_ephem_point.pair() break print "Bob's payment address:\n", bob_ephem_key.address(), '\n'
# Optionally you can use unhexlify, but need to add '\x20' after '\x00' #pubkey_bin_tmp = unhexlify(pubkey_uncompressed) #pubkey_bin = '\x02\xca\x00\x20' + pubkey_bin_tmp[1:-32] + '\x00\x20' + pubkey_bin_tmp[-32:] ## # Prepare private key for decrypting ## # Private Key to Bin privkey_bin = '\x02\xca\x00\x20' + arithmetic.changebase( privkey_uncompressed, 16, 256, minlen=32) # Optionally you can use unhexlify #privkey_bin = '\x02\xca\x00\x20' + unhexlify(privkey_uncompressed) ## # Outputs ## print "Encrypt for wallet: %s" % privkey.address(use_uncompressed=False) print "Uncompressed Hex Pubkey: %s" % pubkey_uncompressed print "Message: ", message print "\n" encrypted = pyelliptic.ECC(curve='secp256k1').encrypt(message, pubkey_bin) print "Hex Crypto Message: ", hexlify(encrypted) decrypted = pyelliptic.ECC(curve='secp256k1', privkey=privkey_bin, pubkey=pubkey_bin).decrypt(encrypted) print "Decrypted message: ", decrypted
class KeyPair(object): """ ECDSA key pair. Args: pkey (str): hexadecimal representation of the private key (secret exponent). secret (str): master password. Attributes: keypair (:py:class:`pycoin.key.Key.Key`): BIP0032-style hierarchical wallet. Raises: NotImplementedError when a randomness source is not found. """ def __init__(self, pkey=None, secret=None): if secret is not None: pkey = format( BIP32Node.from_master_secret( secret.encode('utf-8')).secret_exponent(), "064x") elif pkey is None: try: pkey = format( BIP32Node.from_master_secret( urandom(4096)).secret_exponent(), '064x') except NotImplementedError as e: raise ValueError('No randomness source found: %s' % e) self.keypair = Key(secret_exponent=int(pkey, 16)) @property def node_id(self): """(str): NodeID derived from the public key (RIPEMD160 hash of public key).""" return b2h(self.keypair.hash160()) @property def public_key(self): """(str): public key.""" return b2h(self.keypair.sec(use_uncompressed=False)) @property def private_key(self): """(str): private key.""" return format(self.keypair.secret_exponent(), '064x') @property def address(self): """(): base58 encoded bitcoin address version of the nodeID.""" return self.keypair.address(use_uncompressed=False) def sign(self, message, compact=True): """Signs the supplied message with the private key""" if compact: fd = io.BytesIO() stream_bc_string(fd, bytearray('Bitcoin Signed Message:\n', 'ascii')) stream_bc_string(fd, bytearray(message, 'utf-8')) mhash = from_bytes_32(double_sha256(fd.getvalue())) G = generator_secp256k1 n = G.order() k = from_bytes_32(os.urandom(32)) p1 = k * G r = p1.x() if r == 0: raise RuntimeError("amazingly unlucky random number r") s = (numbertheory.inverse_mod(k, n) * (mhash + (self.keypair.secret_exponent() * r) % n)) % n if s == 0: raise RuntimeError("amazingly unlucky random number s") y_odd = p1.y() % 2 assert y_odd in (0, 1) first = 27 + y_odd + ( 4 if not self.keypair._use_uncompressed(False) else 0) sig = binascii.b2a_base64( bytearray([first]) + to_bytes_32(r) + to_bytes_32(s)).strip() if not isinstance(sig, str): # python3 b2a wrongness sig = str(sig, 'ascii') return sig else: return keys.sign_sha256(self.private_key, message)
def test_generate(mode, pdf, dev, cap_menu, pick_menu_item, goto_home, cap_story, need_keypress, microsd_path): # test UX and operation of the 'bitcoin core' wallet export mx = "Don't make PDF" goto_home() pick_menu_item('Advanced') try: pick_menu_item('Paper Wallets') except: raise pytest.skip('Feature absent') time.sleep(0.1) title, story = cap_story() assert 'pick a random' in story assert 'MANY RISKS' in story need_keypress('y') time.sleep(0.1) if mode == 'segwit': pick_menu_item('Classic Address') pick_menu_item('Segwit/Bech32') time.sleep(0.5) if pdf: assert mx in cap_menu() shutil.copy('../docs/paperwallet.pdf', microsd_path('paperwallet.pdf')) pick_menu_item(mx) need_keypress('y') time.sleep(0.1) title, story = cap_story() assert 'Pick PDF' in story pick_menu_item('paperwallet.pdf') pick_menu_item('GENERATE WALLET') time.sleep(0.1) title, story = cap_story() assert 'Created file' in story story = [i for i in story.split('\n') if i] if not pdf: fname = story[-1] else: fname = story[-2] pdf_name = story[-1] assert pdf_name.endswith('.pdf') assert fname.endswith('.txt') path = microsd_path(fname) with open(path, 'rt') as fp: hdr = None for ln in fp: ln = ln.rstrip() if not ln: continue if ln[0] != ' ': hdr = ln continue if '█' in ln: continue val = ln.strip() if 'Deposit address' in hdr: assert val == fname.split('.', 1)[0].split('-', 1)[0] txt_addr = val if mode != 'segwit': addr = Key.from_text(val) else: hrp, data = bech32_decode(val) decoded = convertbits(data[1:], 5, 8, False)[-20:] assert hrp in {'tb', 'bc' } addr = Key(hash160=bytes(decoded), is_compressed=True, netcode='XTN') elif hdr == 'Private key:': # for QR case assert val == wif elif 'Private key' in hdr and 'WIF=Wallet' in hdr: wif = val k1 = Key.from_text(val) elif 'Private key' in hdr and 'Hex, 32 bytes' in hdr: k2 = Key(secret_exponent=from_bytes_32(a2b_hex(val)), is_compressed=True) elif 'Bitcoin Core command': assert wif in val assert 'importmulti' in val or 'importprivkey' in val else: print(f'{hdr} => {val}') raise ValueError(hdr) assert k1.sec() == k2.sec() assert k1.public_pair() == k2.public_pair() assert addr.address() == k1.address() os.unlink(path) if not pdf: return path = microsd_path(pdf_name) with open(path, 'rb') as fp: d = fp.read() assert wif.encode('ascii') in d assert txt_addr.encode('ascii') in d os.unlink(path)
pubkey_bin_tmp = arithmetic.changebase(pubkey_uncompressed[2:], 16, 256, minlen=64) pubkey_bin = '\x02\xca\x00 '+ pubkey_bin_tmp[:32] + '\x00 ' + pubkey_bin_tmp[32:] # Optionally you can use unhexlify, but need to add '\x20' after '\x00' #pubkey_bin_tmp = unhexlify(pubkey_uncompressed) #pubkey_bin = '\x02\xca\x00\x20' + pubkey_bin_tmp[1:-32] + '\x00\x20' + pubkey_bin_tmp[-32:] ## # Prepare private key for decrypting ## # Private Key to Bin privkey_bin = '\x02\xca\x00\x20' + arithmetic.changebase(privkey_uncompressed, 16, 256, minlen=32) # Optionally you can use unhexlify #privkey_bin = '\x02\xca\x00\x20' + unhexlify(privkey_uncompressed) ## # Outputs ## print "Encrypt for wallet: %s" % privkey.address(use_uncompressed=False) print "Uncompressed Hex Pubkey: %s" % pubkey_uncompressed print "Message: ", message print "\n" encrypted = pyelliptic.ECC(curve='secp256k1').encrypt(message, pubkey_bin) print "Hex Crypto Message: ", hexlify(encrypted) decrypted = pyelliptic.ECC(curve='secp256k1', privkey=privkey_bin, pubkey=pubkey_bin).decrypt(encrypted) print "Decrypted message: ", decrypted