def test_mktx(setup_tx_creation): """Testing exceptional conditions; not guaranteed to create valid tx objects""" #outpoint structure must be {"outpoint":{"hash":hash, "index": num}} ins = [{ 'outpoint': { "hash": x * 32, "index": 0 }, "script": "", "sequence": 4294967295 } for x in ["a", "b", "c"]] pub = vpubs[0] addr = bitcoin.pubkey_to_address(pub, magicbyte=get_p2pk_vbyte()) script = bitcoin.address_to_script(addr) outs = [ script + ":1000", addr + ":2000", { "script": script, "value": 3000 } ] tx = bitcoin.mktx(ins, outs) print(tx) #rewrite with invalid output outs.append({"foo": "bar"}) with pytest.raises(Exception) as e_info: tx = bitcoin.mktx(ins, outs)
def test_verify_tx_input(setup_tx_creation, signall, mktxlist): priv = "aa" * 32 + "01" addr = bitcoin.privkey_to_address(priv, magicbyte=get_p2pk_vbyte()) wallet = make_wallets(1, [[2, 0, 0, 0, 0]], 1)[0]['wallet'] sync_wallet(wallet) insfull = wallet.select_utxos(0, 110000000) print(insfull) if not mktxlist: outs = [{"address": addr, "value": 1000000}] ins = insfull.keys() tx = bitcoin.mktx(ins, outs) else: out1 = addr + ":1000000" ins0, ins1 = insfull.keys() print("INS0 is: " + str(ins0)) print("INS1 is: " + str(ins1)) tx = bitcoin.mktx(ins0, ins1, out1) desertx = bitcoin.deserialize(tx) print(desertx) if signall: privdict = {} for index, ins in enumerate(desertx['ins']): utxo = ins['outpoint']['hash'] + ':' + str( ins['outpoint']['index']) ad = insfull[utxo]['address'] priv = wallet.get_key_from_addr(ad) privdict[utxo] = priv tx = bitcoin.signall(tx, privdict) else: for index, ins in enumerate(desertx['ins']): utxo = ins['outpoint']['hash'] + ':' + str( ins['outpoint']['index']) ad = insfull[utxo]['address'] priv = wallet.get_key_from_addr(ad) if index % 2: tx = binascii.unhexlify(tx) tx = bitcoin.sign(tx, index, priv) if index % 2: tx = binascii.hexlify(tx) desertx2 = bitcoin.deserialize(tx) print(desertx2) sig, pub = bitcoin.deserialize_script(desertx2['ins'][0]['script']) print(sig, pub) pubscript = bitcoin.address_to_script( bitcoin.pubkey_to_address(pub, magicbyte=get_p2pk_vbyte())) sig = binascii.unhexlify(sig) pub = binascii.unhexlify(pub) sig_good = bitcoin.verify_tx_input(tx, 0, pubscript, sig, pub) assert sig_good
def create_recipient_address(bob_pubkey, tweak=None, segwit=False): """Create a p2pkh receiving address from an existing pubkey, tweaked by a random 32 byte scalar. Returns the tweak, the new pubkey point and the new address. The recipient can set the tweak parameter. """ if not tweak: tweak = binascii.hexlify(os.urandom(32)) tweak_point = btc.privkey_to_pubkey(tweak + "01") destination_point = btc.add_pubkeys([tweak_point, bob_pubkey], True) if segwit: destination_address = btc.pubkey_to_p2sh_p2wpkh_address( destination_point, magicbyte=get_p2sh_vbyte()) else: destination_address = btc.pubkey_to_address(destination_point, magicbyte=get_p2pk_vbyte()) return (tweak, destination_point, destination_address)
def query_utxo_set(self, txouts, includeconf=False): if self.qusfail: #simulate failure to find the utxo return [None] if self.fake_query_results: result = [] for x in self.fake_query_results: for y in txouts: if y == x['utxo']: result.append(x) return result result = [] #external maker utxos known_outs = { "03243f4a659e278a1333f8308f6aaf32db4692ee7df0340202750fd6c09150f6:1": "03a2d1cbe977b1feaf8d0d5cc28c686859563d1520b28018be0c2661cf1ebe4857", "498faa8b22534f3b443c6b0ce202f31e12f21668b4f0c7a005146808f250d4c3:0": "02b4b749d54e96b04066b0803e372a43d6ffa16e75a001ae0ed4b235674ab286be", "3f3ea820d706e08ad8dc1d2c392c98facb1b067ae4c671043ae9461057bd2a3c:1": "023bcbafb4f68455e0d1d117c178b0e82a84e66414f0987453d78da034b299c3a9" } #our wallet utxos, faked, for podle tests: utxos are doctored (leading 'f'), #and the lists are (amt, age) wallet_outs = { 'f34b635ed8891f16c4ec5b8236ae86164783903e8e8bb47fa9ef2ca31f3c2d7a:0': [10000000, 2], 'f780d6e5e381bff01a3519997bb4fcba002493103a198fde334fd264f9835d75:1': [20000000, 6], 'fe574db96a4d43a99786b3ea653cda9e4388f377848f489332577e018380cff1:0': [50000000, 3], 'fd9711a2ef340750db21efb761f5f7d665d94b312332dc354e252c77e9c48349:0': [50000000, 6] } if includeconf and set(txouts).issubset(set(wallet_outs)): #includeconf used as a trigger for a podle check; #here we simulate a variety of amount/age returns results = [] for to in txouts: results.append({ 'value': wallet_outs[to][0], 'confirms': wallet_outs[to][1] }) return results if txouts[0] in known_outs: return [{ 'value': 200000000, 'address': btc.pubkey_to_address(known_outs[txouts[0]], magicbyte=0x6f), 'confirms': 20 }] for t in txouts: result_dict = { 'value': 10000000000, 'address': "mrcNu71ztWjAQA6ww9kHiW3zBWSQidHXTQ" } if includeconf: result_dict['confirms'] = 20 result.append(result_dict) return result