def load(self): for public_key, keypair in self.wallet_database.get_keys().iteritems(): self.addresses[BitcoinAddress.from_publickey( public_key, self.runmode)] = (public_key, False) for public_key, secret in self.wallet_database.get_crypted_keys( ).iteritems(): self.addresses[BitcoinAddress.from_publickey( public_key, self.runmode)] = (public_key, True)
def test_address_from_public_key(self): """ public: 023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab private: 049db42589b263e8700eb747a402b74604aae54ebc04f1cbe9a1cf584683f100 """ addr1 = BitcoinAddress.from_publickey(decodehexstr("023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab"), MAIN) self.assertEquals(addr1.to_base58addr(), "171waY81rzeFaBYhsiKibhBW5WAG8X7DLk")
def test_address_from_public_key(self): """ public: 023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab private: 049db42589b263e8700eb747a402b74604aae54ebc04f1cbe9a1cf584683f100 """ addr1 = BitcoinAddress.from_publickey( decodehexstr( "023053536687205cbf57a25386ac466c7f85105032ced1ae9c54486a83c6dd3bab" ), MAIN) self.assertEquals(addr1.to_base58addr(), "171waY81rzeFaBYhsiKibhBW5WAG8X7DLk")
def get_address_description(self, public_key): address = BitcoinAddress.from_publickey(public_key, self.runmode) description = "" if public_key in self.wallet_database.poolkeys_by_public_key: poolkey = self.wallet_database.poolkeys_by_public_key[public_key] description = "Pool (id:%d, time:%s)" % (poolkey.poolnum, time.strftime("%Y-%m-%d %H:%m:%S", time.gmtime(poolkey.time))) else: if address in self.wallet_database.get_names(): description = "Receive (\"%s\")" % self.wallet_database.get_names()[address].name else: description = "Change" return description
def create_transaction(self, amount, address, fee): outputs = [(outpoint, txout) for (tx, outpoint, txout) in self.iter_my_outputs()] selected_outputs = self.coin_selector.select_coins(outputs, (amount + fee)) change_public_key = self.wallet.get_receive_key() change_address = BitcoinAddress.from_publickey(change_public_key, self.wallet.runmode) tx = create_pubkeyhash_transaction( selected_outputs, address.get_hash160(), # decode_base58check(address)[1:], #remove ADDRESSVERSION[runmode] byte change_address.get_hash160(), # decode_base58check(change_address)[1:], #remove ADDRESSVERSION[runmode] byte amount, fee, ) return PlannedTransaction(selected_outputs, amount, address, change_public_key, change_address, fee, tx)
def get_address_description(self, public_key): address = BitcoinAddress.from_publickey(public_key, self.runmode) description = "" if public_key in self.wallet_database.poolkeys_by_public_key: poolkey = self.wallet_database.poolkeys_by_public_key[public_key] description = "Pool (id:%d, time:%s)" % ( poolkey.poolnum, time.strftime("%Y-%m-%d %H:%m:%S", time.gmtime(poolkey.time))) else: if address in self.wallet_database.get_names(): description = "Receive (\"%s\")" % self.wallet_database.get_names( )[address].name else: description = "Change" return description
def test_shamir_share_private_key(self): ssl_add_system_seeds() k = KEY() k.generate() pkey_bignum = k.get_privkey_bignum() pubkey = k.get_pubkey() numshares = 600 threshold = 100 sharenum_bytes = 2 print "private_key_bignum:", pkey_bignum print "public_key:", hexstr(pubkey) print "address:", BitcoinAddress.from_publickey(pubkey, MAIN) field = ZpField() V = field.value_type ZpPkey = V(pkey_bignum) sharer = SecretSharer(field, ZpRandom(field)) shares = sharer.share(ZpPkey, threshold, [V(i+1) for i in range(numshares)]) # print shares print "Shamir Shares: (%d/%d):" % (threshold, numshares) shares_hex = [hexstr(base256encode(int(pt), sharenum_bytes) + base256encode(int(value), 32)) for pt, value in shares] for share in shares_hex: print share # Try to reconstruct the private key using the hex encoded shares. recombiner = SecretRecombiner(field) for i in range(10): random4_hex = random.sample(shares_hex, threshold) random4_decoded = [decodehexstr(h) for h in random4_hex] random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded] recombined_pkey_bignum = recombiner.recombine(random4, V(0)) assert recombined_pkey_bignum == ZpPkey k2 = KEY() k2.set_privkey_bignum(int(recombined_pkey_bignum)) assert k2.get_pubkey() == pubkey print i # With threshold-1 shares this fails for i in range(10): random4_hex = random.sample(shares_hex, threshold-1) random4_decoded = [decodehexstr(h) for h in random4_hex] random4 = [(V(base256decode(data[:sharenum_bytes])), V(base256decode(data[sharenum_bytes:]))) for data in random4_decoded] recombined_pkey_bignum = recombiner.recombine(random4, V(0)) assert recombined_pkey_bignum != ZpPkey
def create_transaction(self, amount, address, fee): outputs = [(outpoint, txout) for (tx, outpoint, txout) in self.iter_my_outputs()] selected_outputs = self.coin_selector.select_coins( outputs, (amount + fee)) change_public_key = self.wallet.get_receive_key() change_address = BitcoinAddress.from_publickey(change_public_key, self.wallet.runmode) tx = create_pubkeyhash_transaction( selected_outputs, address.get_hash160( ), #decode_base58check(address)[1:], #remove ADDRESSVERSION[runmode] byte change_address.get_hash160( ), #decode_base58check(change_address)[1:], #remove ADDRESSVERSION[runmode] byte amount, fee) return (PlannedTransaction(selected_outputs, amount, address, change_public_key, change_address, fee, tx))
def allocate_key(self, public_key, label=None, ischange=False): address = BitcoinAddress.from_publickey(public_key, self.runmode) return self.wallet_database.allocate_key(public_key, address, label)
def get_receive_address(self): public_key = self.wallet.get_receive_key() return BitcoinAddress.from_publickey(public_key, self.wallet.runmode)
def load(self): for public_key, keypair in self.wallet_database.get_keys().iteritems(): self.addresses[BitcoinAddress.from_publickey(public_key, self.runmode)] = (public_key, False) for public_key, secret in self.wallet_database.get_crypted_keys().iteritems(): self.addresses[BitcoinAddress.from_publickey(public_key, self.runmode)] = (public_key, True)