backports

This commit is contained in:
tecnovert
2025-09-29 09:55:50 +02:00
parent 911ca189bc
commit 9d6e566c3b
13 changed files with 296 additions and 23 deletions

View File

@@ -1011,7 +1011,7 @@ class BasicSwapTest(TestFunctions):
def test_002_native_segwit(self):
# p2wpkh
logging.info(
"---------- Test {} p2sh native segwit".format(self.test_coin_from.name)
"---------- Test {} native segwit".format(self.test_coin_from.name)
)
ci = self.swap_clients[0].ci(self.test_coin_from)
@@ -1073,6 +1073,14 @@ class BasicSwapTest(TestFunctions):
tx_signed,
],
)
prev_txo = tx["vout"][tx_signed_decoded["vin"][0]["vout"]]
prevscript: bytes = bytes.fromhex(prev_txo["scriptPubKey"]["hex"])
assert ci.isScriptP2WPKH(prevscript) is True
txin_witness = tx_signed_decoded["vin"][0]["txinwitness"]
assert len(txin_witness) == 2
txin_witness_0 = bytes.fromhex(txin_witness[0])
assert len(txin_witness_0) > 68 and len(txin_witness_0) <= 72
assert len(bytes.fromhex(txin_witness[1])) == 33
assert tx_funded_decoded["txid"] == tx_signed_decoded["txid"]
def test_003_cltv(self):
@@ -2295,6 +2303,11 @@ class BasicSwapTest(TestFunctions):
def test_09_expire_accepted_rev(self):
self.do_test_09_expire_accepted(Coins.XMR, self.test_coin_from)
def test_10_presigned_txns(self):
raise RuntimeError(
"TODO"
) # Build without xmr first for quicker test iterations
class TestBTC(BasicSwapTest):
__test__ = True

View File

@@ -51,7 +51,18 @@ from basicswap.util import (
from basicswap.messages_npb import (
BidMessage,
)
from basicswap.contrib.test_framework.script import hash160 as hash160_btc
from basicswap.contrib.test_framework.script import (
hash160 as hash160_btc,
SegwitV0SignatureHash,
SIGHASH_ALL,
)
from basicswap.contrib.test_framework.messages import (
COutPoint,
CTransaction,
CTxIn,
CTxOut,
uint256_from_str,
)
logger = logging.getLogger()
@@ -663,6 +674,37 @@ class Test(unittest.TestCase):
finally:
db_test.closeDB(cursor)
def test_tx_hashes(self):
tx = CTransaction()
tx.nVersion = 2
tx.nLockTime = 0
tx.vout.append(CTxOut(1, bytes.fromhex("a15143aa086e05e3b5a73046")))
tx.vout.append(CTxOut(2, bytes.fromhex("eed7d63fd86225f7159ed7e5")))
tx.vin.append(
CTxIn(
COutPoint(
uint256_from_str(
bytes.fromhex(
"0101010101010101010101010101010101010010101010101010101010101010"
)
),
1,
),
bytes.fromhex("c2dca8ecbcf058b79f188692"),
)
)
assert (
tx.rehash()
== "28d0e9afad2740504eb9d0428352bc77a7b94eaafa364ef4cc07aeeff0c631a2"
)
sighash = SegwitV0SignatureHash(
bytes.fromhex("a15143aa086e05e3b5a73046"), tx, 0, SIGHASH_ALL, 3
)
assert (
sighash.hex()
== "252cd6e85b99e0fd554c44d5fe638923f7ef563048362406a665cf3400feb1bd"
)
if __name__ == "__main__":
unittest.main()

View File

@@ -315,6 +315,146 @@ class Test(BaseTest):
break
assert len(tx_wallet["blockhash"]) == 64
def test_004_native_segwit(self):
test_coin_from = Coins.PART
# p2wpkh
logging.info("---------- Test {} segwit".format(test_coin_from.name))
ci = self.swap_clients[0].ci(test_coin_from)
addr_native = ci.rpc_wallet("getnewaddress", ["p2pkh segwit test"])
addr_info = ci.rpc_wallet(
"getaddressinfo",
[
addr_native,
],
)
assert addr_info["iswitness"] is False # address is p2pkh, not p2wpkh
addr_segwit = ci.rpc_wallet(
"getnewaddress", ["p2wpkh segwit test", True, False, False, "bech32"]
)
addr_info = ci.rpc_wallet(
"getaddressinfo",
[
addr_segwit,
],
)
assert addr_info["iswitness"] is True
txid = ci.rpc_wallet(
"sendtypeto",
[
"part",
"part",
[
{"address": addr_native, "amount": 1.0},
{"address": addr_segwit, "amount": 1.0},
],
],
)
assert len(txid) == 64
tx_wallet = ci.rpc_wallet(
"gettransaction",
[
txid,
],
)["hex"]
tx = ci.rpc(
"decoderawtransaction",
[
tx_wallet,
],
)
# Wait for stake
for i in range(20):
test_delay_event.wait(1)
ro = ci.rpc("scantxoutset", ["start", ["addr({})".format(addr_native)]])
if len(ro["unspents"]) > 0:
break
ro = ci.rpc("scantxoutset", ["start", ["addr({})".format(addr_native)]])
assert len(ro["unspents"]) == 1
assert ro["unspents"][0]["txid"] == txid
ro = ci.rpc("scantxoutset", ["start", ["addr({})".format(addr_segwit)]])
assert len(ro["unspents"]) == 1
assert ro["unspents"][0]["txid"] == txid
prevout_p2pkh_n: int = -1
prevout_p2wpkh_n: int = -1
for txo in tx["vout"]:
if addr_native == txo["scriptPubKey"]["address"]:
prevout_p2pkh_n = txo["n"]
if addr_segwit == txo["scriptPubKey"]["address"]:
prevout_p2wpkh_n = txo["n"]
assert prevout_p2pkh_n > -1
assert prevout_p2wpkh_n > -1
tx_funded = ci.rpc(
"createrawtransaction",
[[{"txid": txid, "vout": prevout_p2pkh_n}], {addr_segwit: 0.99}],
)
tx_signed = ci.rpc_wallet(
"signrawtransactionwithwallet",
[
tx_funded,
],
)["hex"]
tx_funded_decoded = ci.rpc(
"decoderawtransaction",
[
tx_funded,
],
)
tx_signed_decoded = ci.rpc(
"decoderawtransaction",
[
tx_signed,
],
)
prev_txo = tx["vout"][tx_signed_decoded["vin"][0]["vout"]]
prevscript: bytes = bytes.fromhex(prev_txo["scriptPubKey"]["hex"])
assert ci.isScriptP2PKH(prevscript) is True
assert ci.isScriptP2WPKH(prevscript) is False
txin_witness = tx_signed_decoded["vin"][0]["txinwitness"]
assert len(txin_witness) == 2
txin_witness_0 = bytes.fromhex(txin_witness[0])
assert len(txin_witness_0) > 68 and len(txin_witness_0) <= 72
assert len(bytes.fromhex(txin_witness[1])) == 33
assert tx_funded_decoded["txid"] == tx_signed_decoded["txid"]
tx_funded = ci.rpc(
"createrawtransaction",
[[{"txid": txid, "vout": prevout_p2wpkh_n}], {addr_segwit: 0.99}],
)
tx_signed = ci.rpc_wallet(
"signrawtransactionwithwallet",
[
tx_funded,
],
)["hex"]
tx_funded_decoded = ci.rpc(
"decoderawtransaction",
[
tx_funded,
],
)
tx_signed_decoded = ci.rpc(
"decoderawtransaction",
[
tx_signed,
],
)
prev_txo = tx["vout"][tx_signed_decoded["vin"][0]["vout"]]
prevscript: bytes = bytes.fromhex(prev_txo["scriptPubKey"]["hex"])
assert ci.isScriptP2PKH(prevscript) is False
assert ci.isScriptP2WPKH(prevscript) is True
txin_witness = tx_signed_decoded["vin"][0]["txinwitness"]
assert len(txin_witness) == 2
txin_witness_0 = bytes.fromhex(txin_witness[0])
assert len(txin_witness_0) > 68 and len(txin_witness_0) <= 72
assert len(bytes.fromhex(txin_witness[1])) == 33
assert tx_funded_decoded["txid"] == tx_signed_decoded["txid"]
def test_01_verifyrawtransaction(self):
txn = "0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000"
prevout = {