normal swaps: use different callbacks for server and client
- client creates tx immediately - server defers tx creation, and does not use encrypted storage
This commit is contained in:
@@ -347,7 +347,8 @@ class SwapManager(Logger):
|
|||||||
if key in self.swaps:
|
if key in self.swaps:
|
||||||
swap = self.swaps[key]
|
swap = self.swaps[key]
|
||||||
if swap.funding_txid is None:
|
if swap.funding_txid is None:
|
||||||
await self.broadcast_funding_tx(swap, None, None)
|
tx = self.create_funding_tx(swap, None, None)
|
||||||
|
await self.broadcast_funding_tx(swap, tx)
|
||||||
|
|
||||||
def create_normal_swap(self, *, lightning_amount_sat=None, payment_hash=None, their_pubkey=None):
|
def create_normal_swap(self, *, lightning_amount_sat=None, payment_hash=None, their_pubkey=None):
|
||||||
""" server method """
|
""" server method """
|
||||||
@@ -359,7 +360,7 @@ class SwapManager(Logger):
|
|||||||
WITNESS_TEMPLATE_REVERSE_SWAP,
|
WITNESS_TEMPLATE_REVERSE_SWAP,
|
||||||
{1:32, 5:ripemd(payment_hash), 7:their_pubkey, 10:locktime, 13:our_pubkey}
|
{1:32, 5:ripemd(payment_hash), 7:their_pubkey, 10:locktime, 13:our_pubkey}
|
||||||
)
|
)
|
||||||
return self.add_normal_swap(
|
swap, invoice, prepay_invoice = self.add_normal_swap(
|
||||||
redeem_script=redeem_script,
|
redeem_script=redeem_script,
|
||||||
locktime=locktime,
|
locktime=locktime,
|
||||||
onchain_amount_sat=onchain_amount_sat,
|
onchain_amount_sat=onchain_amount_sat,
|
||||||
@@ -370,6 +371,8 @@ class SwapManager(Logger):
|
|||||||
invoice=None,
|
invoice=None,
|
||||||
prepay=True,
|
prepay=True,
|
||||||
)
|
)
|
||||||
|
self.lnworker.register_callback_for_hold_invoice(payment_hash, self.hold_invoice_callback)
|
||||||
|
return swap, invoice, prepay_invoice
|
||||||
|
|
||||||
def add_normal_swap(self, *, redeem_script=None, locktime=None, onchain_amount_sat=None, lightning_amount_sat=None, payment_hash=None, our_privkey=None, their_pubkey=None, invoice=None, prepay=None):
|
def add_normal_swap(self, *, redeem_script=None, locktime=None, onchain_amount_sat=None, lightning_amount_sat=None, payment_hash=None, our_privkey=None, their_pubkey=None, invoice=None, prepay=None):
|
||||||
""" if invoice is None, create a hold invoice """
|
""" if invoice is None, create a hold invoice """
|
||||||
@@ -390,7 +393,6 @@ class SwapManager(Logger):
|
|||||||
)
|
)
|
||||||
# add payment info to lnworker
|
# add payment info to lnworker
|
||||||
self.lnworker.add_payment_info_for_hold_invoice(payment_hash, invoice_amount_sat)
|
self.lnworker.add_payment_info_for_hold_invoice(payment_hash, invoice_amount_sat)
|
||||||
self.lnworker.register_callback_for_hold_invoice(payment_hash, self.hold_invoice_callback)
|
|
||||||
|
|
||||||
if prepay:
|
if prepay:
|
||||||
prepay_hash = self.lnworker.create_payment_info(amount_msat=prepay_amount_sat*1000)
|
prepay_hash = self.lnworker.create_payment_info(amount_msat=prepay_amount_sat*1000)
|
||||||
@@ -602,6 +604,7 @@ class SwapManager(Logger):
|
|||||||
invoice=invoice,
|
invoice=invoice,
|
||||||
prepay=False)
|
prepay=False)
|
||||||
|
|
||||||
|
tx = self.create_funding_tx(swap, tx, password)
|
||||||
if self.wallet.config.LIGHTNING_SWAP_HTLC_FIRST:
|
if self.wallet.config.LIGHTNING_SWAP_HTLC_FIRST:
|
||||||
# send invoice to server and wait for htlcs
|
# send invoice to server and wait for htlcs
|
||||||
request_data = {
|
request_data = {
|
||||||
@@ -615,16 +618,18 @@ class SwapManager(Logger):
|
|||||||
json=request_data,
|
json=request_data,
|
||||||
timeout=30)
|
timeout=30)
|
||||||
data = json.loads(response)
|
data = json.loads(response)
|
||||||
|
async def callback(payment_hash):
|
||||||
|
await self.broadcast_funding_tx(swap, tx)
|
||||||
|
self.lnworker.register_callback_for_hold_invoice(payment_hash, callback)
|
||||||
# wait for funding tx
|
# wait for funding tx
|
||||||
while swap.funding_txid is None:
|
while swap.funding_txid is None:
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
else:
|
else:
|
||||||
# broadcast funding tx right away
|
# broadcast funding tx right away
|
||||||
await self.broadcast_funding_tx(swap, tx, password)
|
await self.broadcast_funding_tx(swap, tx)
|
||||||
return swap.funding_txid
|
return swap.funding_txid
|
||||||
|
|
||||||
@log_exceptions
|
def create_funding_tx(self, swap, tx, password):
|
||||||
async def broadcast_funding_tx(self, swap, tx, password):
|
|
||||||
# create funding tx
|
# create funding tx
|
||||||
# note: rbf must not decrease payment
|
# note: rbf must not decrease payment
|
||||||
# this is taken care of in wallet._is_rbf_allowed_to_touch_tx_output
|
# this is taken care of in wallet._is_rbf_allowed_to_touch_tx_output
|
||||||
@@ -637,7 +642,10 @@ class SwapManager(Logger):
|
|||||||
tx.add_outputs([funding_output])
|
tx.add_outputs([funding_output])
|
||||||
tx.set_rbf(True)
|
tx.set_rbf(True)
|
||||||
self.wallet.sign_transaction(tx, password)
|
self.wallet.sign_transaction(tx, password)
|
||||||
|
return tx
|
||||||
|
|
||||||
|
@log_exceptions
|
||||||
|
async def broadcast_funding_tx(self, swap, tx):
|
||||||
await self.network.broadcast_transaction(tx)
|
await self.network.broadcast_transaction(tx)
|
||||||
swap.funding_txid = tx.txid()
|
swap.funding_txid = tx.txid()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user