def test_priv_addition_is_pub_addition(self): # adding private keys gives the same address as adding their public keys key1 = key.generate_priv() key2 = key.generate_priv() self.assertEquals( byte_util.base58_encode(key.priv_to_address(key1 + key2)), byte_util.base58_encode( key.address_from_pubkey( key.encode_pub(key.priv_to_pub(key1) + key.priv_to_pub(key2)))))
def test_decode_pub_compressed(self): pub = key.priv_to_pub( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals(key.decode_pub(key.encode_pub_compressed(pub)), pub) for _ in xrange(100): print _ priv = key.generate_priv() pub = key.priv_to_pub(priv) self.assertEquals(key.decode_pub(key.encode_pub_compressed(pub)), pub)
def test_priv_addition_is_pub_addition(self): # adding private keys gives the same address as adding their public keys key1 = key.generate_priv() key2 = key.generate_priv() self.assertEquals( byte_util.base58_encode(key.priv_to_address(key1 + key2)), byte_util.base58_encode( key.address_from_pubkey( key.encode_pub( key.priv_to_pub(key1) + key.priv_to_pub(key2)))))
def test_decode_pub_compressed(self): pub = key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals(key.decode_pub(key.encode_pub_compressed(pub)), pub) for _ in xrange(100): print _ priv = key.generate_priv() pub = key.priv_to_pub(priv) self.assertEquals(key.decode_pub(key.encode_pub_compressed(pub)), pub)
def test_encode_pub(self): pub = key.priv_to_pub( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( key.encode_pub(pub), binascii.unhexlify('04EAB82662C4A329F573E96801CCFCF9337446D2742EFDC5A6E8EA8F617AD0197B387DDFA' '56684EF2F4E2325F298F5F418ADCB00F560B75F4DEEAF90ABD5A3CEB0'))
def main(): priv = key.generate_priv() priv_enc = key.encode_privkey(priv) priv_cmp_enc = key.encode_privkey_compressed(priv) pub = key.priv_to_pub(priv) addr = byte_util.base58_encode(key.pub_to_address(pub)) addr_comp = byte_util.base58_encode(key.pub_to_address_compressed(pub)) print 'Address: %s\nPrivate Key Encoded: %s\nAddress (compressed): %s\nPrivate Key Encoded (compressed): %s' % (addr, priv_enc, addr_comp, priv_cmp_enc)
def main(): priv = key.generate_priv() priv_enc = key.encode_privkey(priv) priv_cmp_enc = key.encode_privkey_compressed(priv) pub = key.priv_to_pub(priv) addr = byte_util.base58_encode(key.pub_to_address(pub)) addr_comp = byte_util.base58_encode(key.pub_to_address_compressed(pub)) print 'Address: %s\nPrivate Key Encoded: %s\nAddress (compressed): %s\nPrivate Key Encoded (compressed): %s' % ( addr, priv_enc, addr_comp, priv_cmp_enc)
def test_encode_pub_compressed(self): pub = key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( key.encode_pub_compressed(pub), binascii.unhexlify( '02EAB82662C4A329F573E96801CCFCF9337446D2742EFDC5A6E8EA8F617AD0197B' ))
def test_priv_to_pub(self): pub = key.priv_to_pub( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( pub.x(), 106166571357547839921127737825237030169011311004864544123912774005363869489531L) self.assertEquals( pub.y(), 25551918952383289921400942500183155031311393141491670111210077721682692525744L)
def test_encode_pub(self): pub = key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( key.encode_pub(pub), binascii.unhexlify( '04EAB82662C4A329F573E96801CCFCF9337446D2742EFDC5A6E8EA8F617AD0197B387DDFA' '56684EF2F4E2325F298F5F418ADCB00F560B75F4DEEAF90ABD5A3CEB0'))
def test_priv_to_pub(self): pub = key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( pub.x(), 106166571357547839921127737825237030169011311004864544123912774005363869489531L ) self.assertEquals( pub.y(), 25551918952383289921400942500183155031311393141491670111210077721682692525744L )
def test_priv_key_to_address(self): self.assertEquals( byte_util.base58_encode( key.address_from_pubkey( key.encode_pub( key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps'))))), '1EEaiQ4DXxf8seerjdNR69by8pwZeBJ6mJ') self.assertEquals( byte_util.base58_encode( key.priv_to_address( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps'))), '1EEaiQ4DXxf8seerjdNR69by8pwZeBJ6mJ')
def test_priv_key_to_address_compressed(self): self.assertEquals( byte_util.base58_encode( key.address_from_pubkey( key.encode_pub_compressed( key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps'))))), '19ufHMz2mhGHhSSQEmqBsqZUTMHB79urP9') self.assertEquals( byte_util.base58_encode( key.priv_to_address_compressed( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps'))), '19ufHMz2mhGHhSSQEmqBsqZUTMHB79urP9')
def derive_child(self, i): high_bit = i & PUBLIC_DERIVATION_BIT if high_bit: I = hmac_sha512(self.chain_code, '\x00' + encode_bigint(self.key) + struct.pack('>L', i)).digest() else: I = hmac_sha512(self.chain_code, encode_pub_compressed(priv_to_pub(self.key)) + struct.pack('>L', i)).digest() IL = decode_bigint(I[:32]) if IL >= secp256k1.order: raise Error('invalid key') IR = I[32:] ki = (IL + self.key) % secp256k1.order if ki == 0: raise Error('invalid key') ci = IR return HDPrivKey(ki, ci, self.depth + 1, i, self.fingerprint(), self.V_T[self.version])
def test_priv_key_to_address_compressed(self): self.assertEquals( byte_util.base58_encode( key.address_from_pubkey( key.encode_pub_compressed( key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps' ))))), '19ufHMz2mhGHhSSQEmqBsqZUTMHB79urP9') self.assertEquals( byte_util.base58_encode( key.priv_to_address_compressed( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) ), '19ufHMz2mhGHhSSQEmqBsqZUTMHB79urP9')
def test_priv_key_to_address(self): self.assertEquals( byte_util.base58_encode( key.address_from_pubkey( key.encode_pub( key.priv_to_pub( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps' ))))), '1EEaiQ4DXxf8seerjdNR69by8pwZeBJ6mJ') self.assertEquals( byte_util.base58_encode( key.priv_to_address( key.decode_privkey( '5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) ), '1EEaiQ4DXxf8seerjdNR69by8pwZeBJ6mJ')
def mine(g_num, work): num = 0 while True: try: priv = key.generate_priv() pub = key.priv_to_pub(priv) addr = byte_util.base58_encode(key.pub_to_address(pub)) addr_comp = byte_util.base58_encode( key.pub_to_address_compressed(pub)) num += 1 if num % INC == 0: with g_num.get_lock(): g_num.value += INC # if g_num.value > 10000: # return if any(VANITY_RE.match(a) for a in [addr, addr_comp]): try: priv_enc = key.encode_privkey(priv) except Exception, e: priv_enc = repr(e) try: priv_cmp_enc = key.encode_privkey_compressed(priv) except Exception, e: priv_cmp_enc = repr(e) msg = '\nI found one!\nAddress: %s\nAddress (compressed): %s\nPrivate Key (raw): %r\nPrivate Key Encoded: %s\nPrivate Key Encoded (compressed): %s\n' % ( addr, addr_comp, priv, priv_enc, priv_cmp_enc) print msg[:-1] with open('found_keys', 'a') as found_keys: found_keys.write(msg) for rec in work: pub_sum = pub + rec['public_key'] addr = byte_util.base58_encode(key.pub_to_address(pub_sum)) addr_comp = byte_util.base58_encode( key.pub_to_address_compressed(pub_sum)) if not any( a.startswith(rec['pattern']) for a in [addr, addr_comp]): continue try: priv_hex = hex(priv)[2:].rstrip('L').upper() except Exception, e: priv_hex = repr(e) try: priv_enc = key.encode_privkey(priv) except Exception, e: priv_enc = repr(e)
def derive_child(self, i): high_bit = i & PUBLIC_DERIVATION_BIT if high_bit: I = hmac_sha512( self.chain_code, '\x00' + encode_bigint(self.key) + struct.pack('>L', i)).digest() else: I = hmac_sha512( self.chain_code, encode_pub_compressed(priv_to_pub(self.key)) + struct.pack('>L', i)).digest() IL = decode_bigint(I[:32]) if IL >= secp256k1.order: raise Error('invalid key') IR = I[32:] ki = (IL + self.key) % secp256k1.order if ki == 0: raise Error('invalid key') ci = IR return HDPrivKey(ki, ci, self.depth + 1, i, self.fingerprint(), self.V_T[self.version])
def pub(self): return HDPubKey(priv_to_pub(self.key), self.chain_code, self.depth, self.child_number, self.parent_fingerprint, self.V_T[self.version])
def test_encode_pub_compressed(self): pub = key.priv_to_pub( key.decode_privkey('5JnKZDMUAddiGgFjWiHNVrX5pxGcEJ1miscs2Xhy7f9BrGffrps')) self.assertEquals( key.encode_pub_compressed(pub), binascii.unhexlify('02EAB82662C4A329F573E96801CCFCF9337446D2742EFDC5A6E8EA8F617AD0197B'))