Add bitcoincash support for prepare and run scripts, add bitcoincash to testing suite, groundwork for bch-xmr atomic swap protocol

This commit is contained in:
mainnet-pat
2024-10-07 16:48:24 +00:00
committed by tecnovert
parent 745d1460e5
commit 1b43806d51
20 changed files with 937 additions and 9 deletions

View File

@@ -6,6 +6,34 @@
import traceback
import unittest
from basicswap.interface.contrib.bch_test_framework.script import (
OP_TXINPUTCOUNT,
OP_1,
OP_NUMEQUALVERIFY,
OP_TXOUTPUTCOUNT,
OP_0,
OP_UTXOVALUE,
OP_OUTPUTVALUE,
OP_SUB,
OP_UTXOTOKENCATEGORY,
OP_OUTPUTTOKENCATEGORY,
OP_EQUALVERIFY,
OP_UTXOTOKENCOMMITMENT,
OP_OUTPUTTOKENCOMMITMENT,
OP_UTXOTOKENAMOUNT,
OP_OUTPUTTOKENAMOUNT,
OP_INPUTSEQUENCENUMBER,
OP_NOTIF,
OP_OUTPUTBYTECODE,
OP_OVER,
OP_CHECKDATASIG,
OP_ELSE,
OP_CHECKSEQUENCEVERIFY,
OP_DROP,
OP_EQUAL,
OP_ENDIF,
)
from basicswap.util import (
ensure,
)
@@ -193,3 +221,82 @@ class XmrSwapInterface(ProtocolInterface):
ctx.nLockTime = 0
return ctx.serialize()
class XmrBchSwapInterface(ProtocolInterface):
swap_type = SwapTypes.XMR_BCH_SWAP
def genScriptLockTxScript(self, mining_fee: int, out_1: bytes, out_2: bytes, public_key: bytes, timelock: int) -> CScript:
return CScript([
# // v4.1.0-CashTokens-Optimized
# // Based on swaplock.cash v4.1.0-CashTokens
#
# // Alice has XMR, wants BCH and/or CashTokens.
# // Bob has BCH and/or CashTokens, wants XMR.
#
# // Verify 1-in-1-out TX form
CScriptOp(OP_TXINPUTCOUNT),
CScriptOp(OP_1), CScriptOp(OP_NUMEQUALVERIFY),
CScriptOp(OP_TXOUTPUTCOUNT),
CScriptOp(OP_1), CScriptOp(OP_NUMEQUALVERIFY),
# // int miningFee
mining_fee,
# // Verify pre-agreed mining fee and that the rest of BCH is forwarded
# // to the output.
CScriptOp(OP_0), CScriptOp(OP_UTXOVALUE),
CScriptOp(OP_0), CScriptOp(OP_OUTPUTVALUE),
CScriptOp(OP_SUB), CScriptOp(OP_NUMEQUALVERIFY),
# # // Verify that any CashTokens are forwarded to the output.
CScriptOp(OP_0), CScriptOp(OP_UTXOTOKENCATEGORY),
CScriptOp(OP_0), CScriptOp(OP_OUTPUTTOKENCATEGORY),
CScriptOp(OP_EQUALVERIFY),
CScriptOp(OP_0), CScriptOp(OP_UTXOTOKENCOMMITMENT),
CScriptOp(OP_0), CScriptOp(OP_OUTPUTTOKENCOMMITMENT),
CScriptOp(OP_EQUALVERIFY),
CScriptOp(OP_0), CScriptOp(OP_UTXOTOKENAMOUNT),
CScriptOp(OP_0), CScriptOp(OP_OUTPUTTOKENAMOUNT),
CScriptOp(OP_NUMEQUALVERIFY),
# // If sequence is not used then it is a regular swap TX.
CScriptOp(OP_0), CScriptOp(OP_INPUTSEQUENCENUMBER),
CScriptOp(OP_NOTIF),
# // bytes aliceOutput
out_1,
# // Verify that the BCH and/or CashTokens are forwarded to Alice's
# // output.
CScriptOp(OP_0), CScriptOp(OP_OUTPUTBYTECODE),
CScriptOp(OP_OVER), CScriptOp(OP_EQUALVERIFY),
# // pubkey bobPubkeyVES
public_key,
# // Require Alice to decrypt and publish Bob's VES signature.
# // The "message" signed is simply a sha256 hash of Alice's output
# // locking bytecode.
# // By decrypting Bob's VES and publishing it, Alice reveals her
# // XMR key share to Bob.
CScriptOp(OP_CHECKDATASIG),
# // If a TX using this path is mined then Alice gets her BCH.
# // Bob uses the revealed XMR key share to collect his XMR.
# // Refund will become available when timelock expires, and it would
# // expire because Alice didn't collect on time, either of her own accord
# // or because Bob bailed out and witheld the encrypted signature.
CScriptOp(OP_ELSE),
# // int timelock_0
timelock,
# // Verify refund timelock.
CScriptOp(OP_CHECKSEQUENCEVERIFY), CScriptOp(OP_DROP),
# // bytes refundLockingBytecode
out_2,
# // Verify that the BCH and/or CashTokens are forwarded to Refund
# // contract.
CScriptOp(OP_0), CScriptOp(OP_OUTPUTBYTECODE),
CScriptOp(OP_EQUAL),
# // BCH and/or CashTokens are simply forwarded to Refund contract.
CScriptOp(OP_ENDIF)
])