def test_segwit_create_tx(self): from pycoinzpub.tx.tx_utils import create_tx, sign_tx from pycoinzpub.tx.Spendable import Spendable from pycoinzpub.tx.pay_to.ScriptPayToAddress import ScriptPayToAddress from pycoinzpub.tx.pay_to.ScriptPayToAddressWit import ScriptPayToAddressWit from pycoinzpub.ui import address_for_pay_to_script_wit, script_obj_from_address key1 = Key(1) coin_value = 5000000 script = ScriptPayToAddressWit(b'\0', key1.hash160()).script() tx_hash = b'\ee' * 32 tx_out_index = 0 spendable = Spendable(coin_value, script, tx_hash, tx_out_index) key2 = Key(2) tx = create_tx([spendable], [(key2.address(), coin_value)]) self.check_unsigned(tx) sign_tx(tx, [key1.wif()]) self.check_signed(tx) self.assertEqual(len(tx.txs_in[0].witness), 2) s1 = ScriptPayToAddress(key1.hash160()).script() address = address_for_pay_to_script_wit(s1) spendable.script = script_obj_from_address(address).script() tx = create_tx([spendable], [(key2.address(), coin_value)]) self.check_unsigned(tx) sign_tx(tx, [key1.wif()], p2sh_lookup=build_p2sh_lookup([s1])) self.check_signed(tx)
def test_is_wif_valid(self): WIFS = [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAnchuDf", "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU74NMTptX4", "5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreAvUcVfH" ] for wif in WIFS: self.assertEqual(is_wif_valid(wif), "BTC") a = wif[:-1] + chr(ord(wif[-1]) + 1) self.assertEqual(is_wif_valid(a), None) NETWORK_NAMES = network_codes() for netcode in NETWORK_NAMES: for se in range(1, 10): key = Key(secret_exponent=se, netcode=netcode) for tv in [True, False]: wif = key.wif(use_uncompressed=tv) self.assertEqual( is_wif_valid(wif, allowable_netcodes=[netcode]), netcode) a = wif[:-1] + chr(ord(wif[-1]) + 1) self.assertEqual( is_wif_valid(a, allowable_netcodes=[netcode]), None)
def test_repr(self): key = Key(secret_exponent=273, netcode='XTN') address = key.address() pub_k = Key.from_text(address) self.assertEqual(repr(pub_k), '<mhDVBkZBWLtJkpbszdjZRkH1o5RZxMwxca>') wif = key.wif() priv_k = Key.from_text(wif) self.assertEqual( repr(priv_k), 'private_for <0264e1b1969f9102977691a40431b0b672055dcf31163897d996434420e6c95dc9>' )
def test_repr(self): from pycoinzpub.key import Key netcode = 'XTN' key = Key(secret_exponent=273, netcode=netcode) wallet = BIP32Node.from_master_secret(bytes(key.wif().encode('ascii')), netcode) address = wallet.address() pub_k = wallet.from_text(address) self.assertEqual(repr(pub_k), '<myb5gZNXePNf2E2ksrjnHRFCwyuvt7oEay>') wif = wallet.wif() priv_k = wallet.from_text(wif) self.assertEqual( repr(priv_k), 'private_for <03ad094b1dc9fdce5d3648ca359b4e210a89d049532fdd39d9ccdd8ca393ac82f4>' )