def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58): secret_exponent = int(exp_hex, 16) sec = h2b(public_pair_sec) c_sec = h2b(c_public_pair_sec) self.assertEqual( secret_exponent_to_wif(secret_exponent, compressed=False), wif) self.assertEqual( secret_exponent_to_wif(secret_exponent, compressed=True), c_wif) key = BitcoinMainnet.ui.parse(wif) exponent = key.secret_exponent() compressed = not key._use_uncompressed() self.assertEqual(exponent, secret_exponent) self.assertFalse(compressed) key = BitcoinMainnet.ui.parse(c_wif) exponent = key.secret_exponent() compressed = not key._use_uncompressed() self.assertEqual(exponent, secret_exponent) self.assertTrue(compressed) public_pair = secret_exponent * secp256k1_generator pk_public_pair = sec_to_public_pair(sec, secp256k1_generator) compressed = is_sec_compressed(sec) self.assertEqual(pk_public_pair, public_pair) self.assertFalse(is_sec_compressed(sec)) self.assertEqual( public_pair_to_sec(pk_public_pair, compressed=False), sec) pk_public_pair = sec_to_public_pair(c_sec, secp256k1_generator) compressed = is_sec_compressed(c_sec) self.assertEqual(pk_public_pair, public_pair) self.assertTrue(compressed) self.assertEqual( public_pair_to_sec(pk_public_pair, compressed=True), c_sec) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=True) self.assertEqual(bca, c_address_b58) self.assertEqual( bitcoin_address_to_hash160_sec(c_address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=True)) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=False) self.assertEqual(bca, address_b58) self.assertEqual( bitcoin_address_to_hash160_sec(address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=False))
def info_for_sec(self, prefix, text): sec = h2b(text) public_pair = sec_to_public_pair(sec, self._generator) is_compressed = is_sec_compressed(sec) kwargs = dict(public_pair=public_pair, is_compressed=is_compressed) return dict(type="key", key_type="sec", is_private=False, kwargs=kwargs, key_class=self._key_class, create_f=lambda: self._key_class(**kwargs))
def from_sec(class_, sec, generator=None): """ Create a key from an sec bytestream (which is an encoding of a public pair). """ generator = generator or class_._default_generator public_pair = sec_to_public_pair(sec, generator) return class_(public_pair=public_pair, is_compressed=is_sec_compressed(sec))
def from_sec(class_, sec, generator): """ Create a key from an sec bytestream (which is an encoding of a public pair). """ public_pair = sec_to_public_pair(sec, generator) return class_(public_pair=public_pair, is_compressed=is_sec_compressed(sec))
def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58): secret_exponent = int(exp_hex, 16) sec = h2b(public_pair_sec) c_sec = h2b(c_public_pair_sec) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=False), wif) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=True), c_wif) key = network.parse.wif(wif) exponent = key.secret_exponent() compressed = key.is_compressed() self.assertEqual(exponent, secret_exponent) self.assertFalse(compressed) key = network.parse.wif(c_wif) exponent = key.secret_exponent() compressed = key.is_compressed() self.assertEqual(exponent, secret_exponent) self.assertTrue(compressed) public_pair = secret_exponent * key._generator pk_public_pair = sec_to_public_pair(sec, key._generator) compressed = is_sec_compressed(sec) self.assertEqual(pk_public_pair, public_pair) self.assertFalse(is_sec_compressed(sec)) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=False), sec) pk_public_pair = sec_to_public_pair(c_sec, key._generator) compressed = is_sec_compressed(c_sec) self.assertEqual(pk_public_pair, public_pair) self.assertTrue(compressed) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=True), c_sec) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=True) self.assertEqual(bca, c_address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(c_address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=True)) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=False) self.assertEqual(bca, address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=False))
def do_test(as_public_pair, as_sec, is_compressed, as_hash160_sec, as_bitcoin_address): self.assertEqual(sec_to_public_pair(as_sec, secp256k1_generator), as_public_pair) self.assertEqual(public_pair_to_sec(as_public_pair, compressed=is_compressed), as_sec) self.assertEqual(is_sec_compressed(as_sec), is_compressed) self.assertEqual(public_pair_to_hash160_sec(as_public_pair, compressed=is_compressed), as_hash160_sec) self.assertEqual(BitcoinMainnet.ui.address_for_p2pkh(as_hash160_sec), as_bitcoin_address) self.assertTrue(is_address_valid(as_bitcoin_address)) bad_address = as_bitcoin_address[:17] + chr(ord(as_bitcoin_address[17]) + 1) + as_bitcoin_address[18:] self.assertFalse(is_address_valid(bad_address))
def do_test(as_public_pair, as_sec, is_compressed, as_hash160_sec, as_bitcoin_address): self.assertEqual(sec_to_public_pair(as_sec, secp256k1_generator), as_public_pair) self.assertEqual( public_pair_to_sec(as_public_pair, compressed=is_compressed), as_sec) self.assertEqual(is_sec_compressed(as_sec), is_compressed) self.assertEqual( public_pair_to_hash160_sec(as_public_pair, compressed=is_compressed), as_hash160_sec) self.assertEqual(network.address.for_p2pkh(as_hash160_sec), as_bitcoin_address) self.assertIsNotNone(network.parse.address(as_bitcoin_address)) bad_address = as_bitcoin_address[:17] + chr( ord(as_bitcoin_address[17]) + 1) + as_bitcoin_address[18:] self.assertIsNone(network.parse.address(bad_address))
def do_test(as_public_pair, as_sec, is_compressed, as_hash160_sec, as_bitcoin_address): self.assertEqual(sec_to_public_pair(as_sec, secp256k1_generator), as_public_pair) self.assertEqual( public_pair_to_sec(as_public_pair, compressed=is_compressed), as_sec) self.assertEqual(is_sec_compressed(as_sec), is_compressed) self.assertEqual( public_pair_to_hash160_sec(as_public_pair, compressed=is_compressed), as_hash160_sec) self.assertEqual( BitcoinMainnet.ui.address_for_p2pkh(as_hash160_sec), as_bitcoin_address) self.assertTrue(is_address_valid(as_bitcoin_address)) bad_address = as_bitcoin_address[:17] + chr( ord(as_bitcoin_address[17]) + 1) + as_bitcoin_address[18:] self.assertFalse(is_address_valid(bad_address))
def annotate_pubkey(self, blob, da): is_compressed = is_sec_compressed(blob) address = self._address.for_p2pkh(hash160(blob)) da[blob].append("SEC for %scompressed %s" % ("" if is_compressed else "un", address))