def test_can_sign_address(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() i_addr = AddressInput.create('a', 0, address) assert i_addr.can_sign([ privkey ]) assert not i_addr.can_sign([ PrivateKey() ])
def test_redeem_multisig_getters(self): privkeys = [PrivateKey(), PrivateKey()] pubkeys = [privkey.to_public_key() for privkey in privkeys] signatures = [b'foo', b'bar'] script = RedeemMultisig.create(pubkeys, 2) assert script.get_min_signatures() == 2 assert script.get_public_keys() == pubkeys
def test_can_sign_address(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() i_addr = AddressInput.create('a', 0, address) assert i_addr.can_sign([privkey]) assert not i_addr.can_sign([PrivateKey()])
def test_classify(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() signature = b'foo' script = Script.compile([ OP_0 ]) i_addr = Input.create('1' * 32, 0, PayToPubkeyIn.create(address, signature)) assert isinstance(i_addr, AddressInput) i_script = Input.create('1' * 32, 0, PayToScriptIn.create(script, [signature])) assert isinstance(i_script, ScriptInput)
def test_p2pkh_getters(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() signature = b'foo' i_script = PayToPubkeyIn.create(pubkey, signature) o_script = PayToPubkeyOut.create(address) assert i_script.get_public_key() == pubkey assert i_script.get_signature() == signature assert o_script.get_address_hash() == address.phash
def test_p2sh_getters(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() embedded = Script.compile([OP_0, b'bar', OP_1]) signatures = [b'foo', b'bar'] i_script = PayToScriptIn.create(embedded, signatures) o_script = PayToScriptOut.create(embedded) assert i_script.get_script() == embedded assert i_script.get_signatures() == signatures assert o_script.get_script_hash() == embedded.to_hash()
def test_p2sh_getters(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() embedded = Script.compile([ OP_0, b'bar', OP_1 ]) signatures = [ b'foo', b'bar' ] i_script = PayToScriptIn.create(embedded, signatures) o_script = PayToScriptOut.create(embedded) assert i_script.get_script() == embedded assert i_script.get_signatures() == signatures assert o_script.get_script_hash() == embedded.to_hash()
def test_classify(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() script = Script() o_addr = Output.create(1, PayToPubkeyOut.create(address)) assert isinstance(o_addr, AddressOutput) o_script = Output.create(1, PayToScriptOut.create(script)) assert isinstance(o_script, ScriptOutput) o_data = Output.create(1, OpReturnOut.create(b'data')) assert isinstance(o_data, DataOutput)
def test_classify(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() signature = b'foo' script = Script.compile([OP_0]) i_addr = Input.create('1' * 32, 0, PayToPubkeyIn.create(address, signature)) assert isinstance(i_addr, AddressInput) i_script = Input.create('1' * 32, 0, PayToScriptIn.create(script, [signature])) assert isinstance(i_script, ScriptInput)
def test_is_pay_to_pubkey_out(self): address = PrivateKey().to_address() yes = [PayToPubkeyOut.create(address), PayToPubkeyOut.create(address)] no = [Script(), PayToPubkeyIn.create(address, b'foo')] assert all(map(PayToPubkeyOut.is_valid, yes)) assert not any(map(PayToPubkeyOut.is_valid, no))
def test_classify(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() signature = b'foo' script = RedeemMultisig.create([pubkey], 1) class_to_arguments = { PayToPubkeyIn: [pubkey, signature], PayToPubkeyOut: [address], PayToScriptIn: [script, [signature]], PayToScriptOut: [script], RedeemMultisig: [[pubkey], 1], OpReturnOut: [b'data'] } for cls, args in class_to_arguments.items(): special = cls.create(*args) generic = Script(special.instructions) assert Script.classify(special) == Script.classify(generic) == cls assert isinstance(Script.create(special.instructions), cls)
def test_classify(self): privkey = PrivateKey() pubkey = privkey.to_public_key() address = pubkey.to_address() signature = b'foo' script = RedeemMultisig.create([ pubkey ], 1) class_to_arguments = { PayToPubkeyIn : [ pubkey, signature ], PayToPubkeyOut: [ address ], PayToScriptIn : [ script, [signature] ], PayToScriptOut: [ script ], RedeemMultisig: [ [pubkey], 1 ], OpReturnOut : [ b'data' ] } for cls, args in class_to_arguments.items(): special = cls.create(*args) generic = Script(special.instructions) assert Script.classify(special) == Script.classify(generic) == cls assert isinstance(Script.create(special.instructions), cls)
def test_is_pay_to_script_out(self): address = PrivateKey().to_address() embedded = PayToPubkeyOut.create(address) yes = [PayToScriptOut.create(embedded)] no = [ Script(), PayToPubkeyOut.create(address), PayToScriptIn.create(embedded, [b'foo']) ] assert all(map(PayToScriptOut.is_valid, yes)) assert not any(map(PayToScriptOut.is_valid, no))
def test_can_sign_multisig(self): privkeys = [PrivateKey(), PrivateKey()] pubkeys = [privkey.to_public_key() for privkey in privkeys] i_addr = MultisigInput.create('a', 0, pubkeys, 1) assert i_addr.can_sign([privkeys[0]]) assert i_addr.can_sign([privkeys[1]]) assert not i_addr.can_sign([PrivateKey()]) assert not i_addr.can_sign([PrivateKey(), PrivateKey()]) assert not i_addr.can_sign([PrivateKey(), privkeys[1]])
def test_is_pay_to_script_in(self): address = PrivateKey().to_address() embedded = PayToPubkeyOut.create(address) yes = [ PayToScriptIn.create(embedded, [b'foo']), PayToScriptIn.create(Script.compile([b'bar']), [b'foo']), PayToScriptIn.create(Script.compile([b'baz']), [b'one', b'two']), ] no = [ Script(), PayToPubkeyIn.create(address, b'foo'), PayToScriptOut.create(Script()) ] assert all(map(PayToScriptIn.is_valid, yes)) assert not any(map(PayToScriptIn.is_valid, no))
import termcolor # not a library dependency, obviously def log(title, content): print(termcolor.colored(title + ":", 'green', attrs=['bold'])) print(content) print('') PK_HEX = '21c601c0ae6dfcdcf622e6fe2be9153ed7ada0cc90a8a08475e57060e18c0791' PREV_TX_ID = '4baa7551933fbf26158a619c3084ccdd5c0d81930b3e74a85a33ad26d13f1a55' UTXO_INDEX = 0 AMOUNT = 2000 privkey = PrivateKey.from_hex(PK_HEX, network=networks.testnet) pubkey = privkey.to_public_key() address = pubkey.to_address() i = AddressInput.create(PREV_TX_ID, UTXO_INDEX, address) o = AddressOutput.create(AMOUNT, address) t1 = Transaction([i], [o]) t2 = Transaction.from_bytes(t1.to_bytes()) log("Transaction hex 1", t1.to_hex()) log("Transaction hex 2", t2.to_hex()) print('equal', t1 == t2) # __import__('IPython').embed()