swapserver: support prepayment of fees
This commit is contained in:
@@ -103,7 +103,7 @@ class SwapServer(Logger, EventListener):
|
|||||||
their_pubkey=bytes.fromhex(request['claimPublicKey'])
|
their_pubkey=bytes.fromhex(request['claimPublicKey'])
|
||||||
assert len(payment_hash) == 32
|
assert len(payment_hash) == 32
|
||||||
assert len(their_pubkey) == 33
|
assert len(their_pubkey) == 33
|
||||||
swap, payment_hash, invoice = sm.add_server_swap(
|
swap, payment_hash, invoice, prepay_invoice = sm.add_server_swap(
|
||||||
lightning_amount_sat=lightning_amount_sat,
|
lightning_amount_sat=lightning_amount_sat,
|
||||||
payment_hash=payment_hash,
|
payment_hash=payment_hash,
|
||||||
their_pubkey=their_pubkey
|
their_pubkey=their_pubkey
|
||||||
@@ -111,7 +111,7 @@ class SwapServer(Logger, EventListener):
|
|||||||
response = {
|
response = {
|
||||||
'id': payment_hash.hex(),
|
'id': payment_hash.hex(),
|
||||||
'invoice': invoice,
|
'invoice': invoice,
|
||||||
'minerFeeInvoice': None,
|
'minerFeeInvoice': prepay_invoice,
|
||||||
'lockupAddress': swap.lockup_address,
|
'lockupAddress': swap.lockup_address,
|
||||||
'redeemScript': swap.redeem_script.hex(),
|
'redeemScript': swap.redeem_script.hex(),
|
||||||
'timeoutBlockHeight': swap.locktime,
|
'timeoutBlockHeight': swap.locktime,
|
||||||
@@ -121,7 +121,7 @@ class SwapServer(Logger, EventListener):
|
|||||||
their_invoice=request['invoice']
|
their_invoice=request['invoice']
|
||||||
their_pubkey=bytes.fromhex(request['refundPublicKey'])
|
their_pubkey=bytes.fromhex(request['refundPublicKey'])
|
||||||
assert len(their_pubkey) == 33
|
assert len(their_pubkey) == 33
|
||||||
swap, payment_hash, invoice = sm.add_server_swap(
|
swap, payment_hash, invoice, prepay_invoice = sm.add_server_swap(
|
||||||
invoice=their_invoice,
|
invoice=their_invoice,
|
||||||
their_pubkey=their_pubkey
|
their_pubkey=their_pubkey
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -317,19 +317,32 @@ class SwapManager(Logger):
|
|||||||
{1:ripemd(payment_hash), 4:our_pubkey, 6:locktime, 9:their_pubkey}
|
{1:ripemd(payment_hash), 4:our_pubkey, 6:locktime, 9:their_pubkey}
|
||||||
)
|
)
|
||||||
self.wallet.save_invoice(Invoice.from_bech32(invoice))
|
self.wallet.save_invoice(Invoice.from_bech32(invoice))
|
||||||
|
prepay_invoice = None
|
||||||
else:
|
else:
|
||||||
onchain_amount_sat = self._get_recv_amount(lightning_amount_sat, is_reverse=True)
|
onchain_amount_sat = self._get_recv_amount(lightning_amount_sat, is_reverse=True)
|
||||||
|
prepay_amount_sat = self.get_claim_fee() * 2
|
||||||
|
main_amount_sat = lightning_amount_sat - prepay_amount_sat
|
||||||
lnaddr, invoice = self.lnworker.get_bolt11_invoice(
|
lnaddr, invoice = self.lnworker.get_bolt11_invoice(
|
||||||
payment_hash=payment_hash,
|
payment_hash=payment_hash,
|
||||||
amount_msat=lightning_amount_sat * 1000,
|
amount_msat=main_amount_sat * 1000,
|
||||||
message='Submarine swap',
|
message='Submarine swap',
|
||||||
expiry=3600 * 24,
|
expiry=3600 * 24,
|
||||||
fallback_address=None,
|
fallback_address=None,
|
||||||
channels=None,
|
channels=None,
|
||||||
)
|
)
|
||||||
# add payment info to lnworker
|
# add payment info to lnworker
|
||||||
self.lnworker.add_payment_info_for_hold_invoice(payment_hash, lightning_amount_sat)
|
self.lnworker.add_payment_info_for_hold_invoice(payment_hash, main_amount_sat)
|
||||||
self.lnworker.register_callback_for_hold_invoice(payment_hash, self.hold_invoice_callback, 60*60*24)
|
self.lnworker.register_callback_for_hold_invoice(payment_hash, self.hold_invoice_callback, 60*60*24)
|
||||||
|
prepay_hash = self.lnworker.create_payment_info(amount_msat=prepay_amount_sat*1000)
|
||||||
|
_, prepay_invoice = self.lnworker.get_bolt11_invoice(
|
||||||
|
payment_hash=prepay_hash,
|
||||||
|
amount_msat=prepay_amount_sat * 1000,
|
||||||
|
message='prepay',
|
||||||
|
expiry=3600 * 24,
|
||||||
|
fallback_address=None,
|
||||||
|
channels=None,
|
||||||
|
)
|
||||||
|
self.lnworker.bundle_payments([payment_hash, prepay_hash])
|
||||||
redeem_script = construct_script(
|
redeem_script = construct_script(
|
||||||
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}
|
||||||
@@ -354,7 +367,7 @@ class SwapManager(Logger):
|
|||||||
swap._payment_hash = payment_hash
|
swap._payment_hash = payment_hash
|
||||||
self._add_or_reindex_swap(swap)
|
self._add_or_reindex_swap(swap)
|
||||||
self.add_lnwatcher_callback(swap)
|
self.add_lnwatcher_callback(swap)
|
||||||
return swap, payment_hash, invoice
|
return swap, payment_hash, invoice, prepay_invoice
|
||||||
|
|
||||||
async def normal_swap(
|
async def normal_swap(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Reference in New Issue
Block a user