|
|
|
|
@ -6,12 +6,25 @@ from common import make_wallets
|
|
|
|
|
from pprint import pformat |
|
|
|
|
import jmbitcoin as btc |
|
|
|
|
import pytest |
|
|
|
|
from unittest_parametrize import parametrize, ParametrizedTestCase |
|
|
|
|
from jmbase import get_log, hextobin |
|
|
|
|
from jmclient import load_test_config, jm_single, LegacyWallet, BaseWallet |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from common import TrialAsyncioTestCase |
|
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") |
|
|
|
|
|
|
|
|
|
log = get_log() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_segwit_valid_txs(setup_segwit): |
|
|
|
|
class SegWitTests(TrialAsyncioTestCase, ParametrizedTestCase): |
|
|
|
|
|
|
|
|
|
async def asyncSetUp(self): |
|
|
|
|
load_test_config() |
|
|
|
|
jm_single().bc_interface.tick_forward_chain_interval = 1 |
|
|
|
|
|
|
|
|
|
async def test_segwit_valid_txs(self): |
|
|
|
|
with open("test/tx_segwit_valid.json", "r") as f: |
|
|
|
|
json_data = f.read() |
|
|
|
|
valid_txs = json.loads(json_data) |
|
|
|
|
@ -24,14 +37,14 @@ def test_segwit_valid_txs(setup_segwit):
|
|
|
|
|
#TODO use bcinterface to decoderawtransaction |
|
|
|
|
#and compare the json values |
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize( |
|
|
|
|
@parametrize( |
|
|
|
|
"wallet_structure, in_amt, amount, segwit_amt, segwit_ins, o_ins", [ |
|
|
|
|
([[1, 0, 0, 0, 0]], 1, 1000000, 1, [0, 1, 2], []), |
|
|
|
|
([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 2], [1, 3]), |
|
|
|
|
([[4, 0, 0, 0, 1]], 3, 100000000, 1, [0, 5], [1, 2, 3, 4]), |
|
|
|
|
([[4, 0, 0, 0, 0]], 2, 200000007, 0.3, [0, 1, 4, 5], [2, 3, 6]), |
|
|
|
|
]) |
|
|
|
|
def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount, |
|
|
|
|
async def test_spend_p2sh_p2wpkh_multi(self, wallet_structure, in_amt, amount, |
|
|
|
|
segwit_amt, segwit_ins, o_ins): |
|
|
|
|
"""Creates a wallet from which non-segwit inputs/ |
|
|
|
|
outputs can be created, constructs one or more |
|
|
|
|
@ -48,14 +61,19 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
|
|
|
|
|
MIXDEPTH = 0 |
|
|
|
|
|
|
|
|
|
# set up wallets and inputs |
|
|
|
|
nsw_wallet_service = make_wallets(1, wallet_structure, in_amt, |
|
|
|
|
walletclass=LegacyWallet)[0]['wallet'] |
|
|
|
|
nsw_wallet_service.sync_wallet(fast=True) |
|
|
|
|
sw_wallet_service = make_wallets(1, [[len(segwit_ins), 0, 0, 0, 0]], segwit_amt)[0]['wallet'] |
|
|
|
|
sw_wallet_service.sync_wallet(fast=True) |
|
|
|
|
|
|
|
|
|
nsw_utxos = nsw_wallet_service.get_utxos_by_mixdepth()[MIXDEPTH] |
|
|
|
|
sw_utxos = sw_wallet_service.get_utxos_by_mixdepth()[MIXDEPTH] |
|
|
|
|
wallet_services = await make_wallets( |
|
|
|
|
1, wallet_structure, in_amt, walletclass=LegacyWallet) |
|
|
|
|
nsw_wallet_service = wallet_services[0]['wallet'] |
|
|
|
|
await nsw_wallet_service.sync_wallet(fast=True) |
|
|
|
|
sw_wallet_services = await make_wallets( |
|
|
|
|
1, [[len(segwit_ins), 0, 0, 0, 0]], segwit_amt) |
|
|
|
|
sw_wallet_service = sw_wallet_services[0]['wallet'] |
|
|
|
|
await sw_wallet_service.sync_wallet(fast=True) |
|
|
|
|
|
|
|
|
|
nsw_utxos_by_md = await nsw_wallet_service.get_utxos_by_mixdepth() |
|
|
|
|
nsw_utxos = nsw_utxos_by_md[MIXDEPTH] |
|
|
|
|
sw_utxos_by_md = await sw_wallet_service.get_utxos_by_mixdepth() |
|
|
|
|
sw_utxos = sw_utxos_by_md[MIXDEPTH] |
|
|
|
|
assert len(o_ins) <= len(nsw_utxos), "sync failed" |
|
|
|
|
assert len(segwit_ins) <= len(sw_utxos), "sync failed" |
|
|
|
|
|
|
|
|
|
@ -90,37 +108,38 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
|
|
|
|
|
FEE = 50000 |
|
|
|
|
assert FEE < total_amt_in_sat - amount, "test broken, not enough funds" |
|
|
|
|
|
|
|
|
|
cj_script = nsw_wallet_service.get_new_script(MIXDEPTH + 1, |
|
|
|
|
BaseWallet.ADDRESS_TYPE_INTERNAL) |
|
|
|
|
change_script = nsw_wallet_service.get_new_script(MIXDEPTH, |
|
|
|
|
BaseWallet.ADDRESS_TYPE_INTERNAL) |
|
|
|
|
cj_script = await nsw_wallet_service.get_new_script( |
|
|
|
|
MIXDEPTH + 1, BaseWallet.ADDRESS_TYPE_INTERNAL) |
|
|
|
|
change_script = await nsw_wallet_service.get_new_script( |
|
|
|
|
MIXDEPTH, BaseWallet.ADDRESS_TYPE_INTERNAL) |
|
|
|
|
change_amt = total_amt_in_sat - amount - FEE |
|
|
|
|
|
|
|
|
|
tx_outs = [ |
|
|
|
|
{'address': nsw_wallet_service.script_to_addr(cj_script), |
|
|
|
|
{'address': await nsw_wallet_service.script_to_addr(cj_script), |
|
|
|
|
'value': amount}, |
|
|
|
|
{'address': nsw_wallet_service.script_to_addr(change_script), |
|
|
|
|
{'address': await nsw_wallet_service.script_to_addr(change_script), |
|
|
|
|
'value': change_amt}] |
|
|
|
|
tx = btc.mktx(tx_ins, tx_outs) |
|
|
|
|
|
|
|
|
|
# import new addresses to bitcoind |
|
|
|
|
jm_single().bc_interface.import_addresses( |
|
|
|
|
[nsw_wallet_service.script_to_addr(x) |
|
|
|
|
for x in [cj_script, change_script]], nsw_wallet_service.get_wallet_name()) |
|
|
|
|
[(await nsw_wallet_service.script_to_addr(x)) |
|
|
|
|
for x in [cj_script, change_script]], |
|
|
|
|
nsw_wallet_service.get_wallet_name()) |
|
|
|
|
|
|
|
|
|
# sign tx |
|
|
|
|
scripts = {} |
|
|
|
|
for nsw_in_index in o_ins: |
|
|
|
|
inp = nsw_ins[nsw_in_index][1] |
|
|
|
|
scripts[nsw_in_index] = (inp['script'], inp['value']) |
|
|
|
|
success, msg = nsw_wallet_service.sign_tx(tx, scripts) |
|
|
|
|
success, msg = await nsw_wallet_service.sign_tx(tx, scripts) |
|
|
|
|
assert success, msg |
|
|
|
|
|
|
|
|
|
scripts = {} |
|
|
|
|
for sw_in_index in segwit_ins: |
|
|
|
|
inp = sw_ins[sw_in_index][1] |
|
|
|
|
scripts[sw_in_index] = (inp['script'], inp['value']) |
|
|
|
|
success, msg = sw_wallet_service.sign_tx(tx, scripts) |
|
|
|
|
success, msg = await sw_wallet_service.sign_tx(tx, scripts) |
|
|
|
|
assert success, msg |
|
|
|
|
|
|
|
|
|
print(tx) |
|
|
|
|
@ -130,18 +149,12 @@ def test_spend_p2sh_p2wpkh_multi(setup_segwit, wallet_structure, in_amt, amount,
|
|
|
|
|
assert txid |
|
|
|
|
|
|
|
|
|
balances = jm_single().bc_interface.get_received_by_addr( |
|
|
|
|
[nsw_wallet_service.script_to_addr(cj_script), |
|
|
|
|
nsw_wallet_service.script_to_addr(change_script)])['data'] |
|
|
|
|
[(await nsw_wallet_service.script_to_addr(cj_script)), |
|
|
|
|
(await nsw_wallet_service.script_to_addr(change_script))])['data'] |
|
|
|
|
assert balances[0]['balance'] == amount |
|
|
|
|
assert balances[1]['balance'] == change_amt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module") |
|
|
|
|
def setup_segwit(): |
|
|
|
|
load_test_config() |
|
|
|
|
jm_single().bc_interface.tick_forward_chain_interval = 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
|
|
Examples of valid segwit from the json with parsing |
|
|
|
|
|
|
|
|
|
|