WIP continue implementing the BCH swap interface for XMR swap and atomic swap protocols

This commit is contained in:
mainnet-pat
2024-10-14 15:35:24 +00:00
committed by tecnovert
parent 1b43806d51
commit 3ea832bd03
9 changed files with 428 additions and 122 deletions

View File

@@ -191,7 +191,11 @@ def setDLEAG(xmr_swap, ci_to, kbsf: bytes) -> None:
class XmrSwapInterface(ProtocolInterface):
swap_type = SwapTypes.XMR_SWAP
def genScriptLockTxScript(self, ci, Kal: bytes, Kaf: bytes) -> CScript:
def genScriptLockTxScript(self, ci, Kal: bytes, Kaf: bytes, **kwargs) -> CScript:
# fallthrough to ci if genScriptLockTxScript is implemented there
if hasattr(ci, 'genScriptLockTxScript') and callable(ci.genScriptLockTxScript):
return ci.genScriptLockTxScript(ci, Kal, Kaf, **kwargs)
Kal_enc = Kal if len(Kal) == 33 else ci.encodePubkey(Kal)
Kaf_enc = Kaf if len(Kaf) == 33 else ci.encodePubkey(Kaf)
@@ -221,82 +225,3 @@ 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)
])