submarine swap server plugin:
- hold invoices - uses the same web API as the Boltz backend
This commit is contained in:
6
electrum/plugins/swapserver/__init__.py
Normal file
6
electrum/plugins/swapserver/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from electrum.i18n import _
|
||||
|
||||
fullname = _('SwapServer')
|
||||
description = ''
|
||||
|
||||
available_for = ['qt', 'cmdline']
|
||||
31
electrum/plugins/swapserver/cmdline.py
Normal file
31
electrum/plugins/swapserver/cmdline.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Electrum - Lightweight Bitcoin Client
|
||||
# Copyright (C) 2023 The Electrum Developers
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
|
||||
from .swapserver import SwapServerPlugin
|
||||
|
||||
class Plugin(SwapServerPlugin):
|
||||
pass
|
||||
|
||||
31
electrum/plugins/swapserver/qt.py
Normal file
31
electrum/plugins/swapserver/qt.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Electrum - Lightweight Bitcoin Client
|
||||
# Copyright (C) 2023 The Electrum Developers
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
|
||||
from .swapserver import SwapServerPlugin
|
||||
|
||||
class Plugin(SwapServerPlugin):
|
||||
pass
|
||||
|
||||
138
electrum/plugins/swapserver/server.py
Normal file
138
electrum/plugins/swapserver/server.py
Normal file
@@ -0,0 +1,138 @@
|
||||
import os
|
||||
import asyncio
|
||||
import attr
|
||||
import random
|
||||
from collections import defaultdict
|
||||
|
||||
from aiohttp import ClientResponse
|
||||
from aiohttp import web, client_exceptions
|
||||
from aiorpcx import timeout_after, TaskTimeout, ignore_after
|
||||
from aiorpcx import NetAddress
|
||||
|
||||
|
||||
from electrum.util import log_exceptions, ignore_exceptions
|
||||
from electrum.logging import Logger
|
||||
from electrum.util import EventListener, event_listener
|
||||
from electrum.invoices import PR_PAID, PR_EXPIRED
|
||||
|
||||
|
||||
class SwapServer(Logger, EventListener):
|
||||
"""
|
||||
public API:
|
||||
- getpairs
|
||||
- createswap
|
||||
"""
|
||||
|
||||
WWW_DIR = os.path.join(os.path.dirname(__file__), 'www')
|
||||
|
||||
def __init__(self, config, wallet):
|
||||
Logger.__init__(self)
|
||||
self.config = config
|
||||
self.wallet = wallet
|
||||
self.addr = NetAddress.from_string(self.config.SWAPSERVER_ADDRESS)
|
||||
self.register_callbacks() # eventlistener
|
||||
|
||||
self.pending = defaultdict(asyncio.Event)
|
||||
self.pending_msg = {}
|
||||
|
||||
@ignore_exceptions
|
||||
@log_exceptions
|
||||
async def run(self):
|
||||
self.root = '/root'
|
||||
app = web.Application()
|
||||
app.add_routes([web.get('/api/getpairs', self.get_pairs)])
|
||||
app.add_routes([web.post('/api/createswap', self.create_swap)])
|
||||
|
||||
runner = web.AppRunner(app)
|
||||
await runner.setup()
|
||||
site = web.TCPSite(runner, host=str(self.addr.host), port=self.addr.port, ssl_context=self.config.get_ssl_context())
|
||||
await site.start()
|
||||
self.logger.info(f"now running and listening. addr={self.addr}")
|
||||
|
||||
async def get_pairs(self, r):
|
||||
sm = self.wallet.lnworker.swap_manager
|
||||
sm.init_pairs()
|
||||
pairs = {
|
||||
"info": [],
|
||||
"warnings": [],
|
||||
"pairs": {
|
||||
"BTC/BTC": {
|
||||
"hash": "dfe692a026d6964601bfd79703611af333d1d5aa49ef5fedd288f5a620fced60",
|
||||
"rate": 1,
|
||||
"limits": {
|
||||
"maximal": sm._max_amount,
|
||||
"minimal": sm._min_amount,
|
||||
"maximalZeroConf": {
|
||||
"baseAsset": 0,
|
||||
"quoteAsset": 0
|
||||
}
|
||||
},
|
||||
"fees": {
|
||||
"percentage": 0.5,
|
||||
"minerFees": {
|
||||
"baseAsset": {
|
||||
"normal": sm.normal_fee,
|
||||
"reverse": {
|
||||
"claim": sm.claim_fee,
|
||||
"lockup": sm.lockup_fee
|
||||
}
|
||||
},
|
||||
"quoteAsset": {
|
||||
"normal": sm.normal_fee,
|
||||
"reverse": {
|
||||
"claim": sm.claim_fee,
|
||||
"lockup": sm.lockup_fee
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return web.json_response(pairs)
|
||||
|
||||
async def create_swap(self, r):
|
||||
sm = self.wallet.lnworker.swap_manager
|
||||
sm.init_pairs()
|
||||
request = await r.json()
|
||||
req_type = request['type']
|
||||
assert request['pairId'] == 'BTC/BTC'
|
||||
if req_type == 'reversesubmarine':
|
||||
lightning_amount_sat=request['invoiceAmount']
|
||||
payment_hash=bytes.fromhex(request['preimageHash'])
|
||||
their_pubkey=bytes.fromhex(request['claimPublicKey'])
|
||||
assert len(payment_hash) == 32
|
||||
assert len(their_pubkey) == 33
|
||||
swap, payment_hash, invoice = sm.add_server_swap(
|
||||
lightning_amount_sat=lightning_amount_sat,
|
||||
payment_hash=payment_hash,
|
||||
their_pubkey=their_pubkey
|
||||
)
|
||||
response = {
|
||||
'id': payment_hash.hex(),
|
||||
'invoice': invoice,
|
||||
'minerFeeInvoice': None,
|
||||
'lockupAddress': swap.lockup_address,
|
||||
'redeemScript': swap.redeem_script.hex(),
|
||||
'timeoutBlockHeight': swap.locktime,
|
||||
"onchainAmount": swap.onchain_amount,
|
||||
}
|
||||
elif req_type == 'submarine':
|
||||
their_invoice=request['invoice']
|
||||
their_pubkey=bytes.fromhex(request['refundPublicKey'])
|
||||
assert len(their_pubkey) == 33
|
||||
swap, payment_hash, invoice = sm.add_server_swap(
|
||||
invoice=their_invoice,
|
||||
their_pubkey=their_pubkey
|
||||
)
|
||||
response = {
|
||||
"id": payment_hash.hex(),
|
||||
"acceptZeroConf": False,
|
||||
"expectedAmount": swap.onchain_amount,
|
||||
"timeoutBlockHeight": swap.locktime,
|
||||
"address": swap.lockup_address,
|
||||
"redeemScript": swap.redeem_script.hex()
|
||||
}
|
||||
else:
|
||||
raise Exception('unsupported request type:' + req_type)
|
||||
return web.json_response(response)
|
||||
58
electrum/plugins/swapserver/swapserver.py
Normal file
58
electrum/plugins/swapserver/swapserver.py
Normal file
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Electrum - Lightweight Bitcoin Client
|
||||
# Copyright (C) 2023 The Electrum Developers
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person
|
||||
# obtaining a copy of this software and associated documentation files
|
||||
# (the "Software"), to deal in the Software without restriction,
|
||||
# including without limitation the rights to use, copy, modify, merge,
|
||||
# publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
# and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be
|
||||
# included in all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import random
|
||||
from electrum.plugin import BasePlugin, hook
|
||||
from electrum.util import log_exceptions, ignore_exceptions
|
||||
from electrum import ecc
|
||||
|
||||
from .server import SwapServer
|
||||
|
||||
|
||||
class SwapServerPlugin(BasePlugin):
|
||||
|
||||
def __init__(self, parent, config, name):
|
||||
BasePlugin.__init__(self, parent, config, name)
|
||||
self.config = config
|
||||
self.server = None
|
||||
|
||||
@hook
|
||||
def daemon_wallet_loaded(self, daemon, wallet):
|
||||
# we use the first wallet loaded
|
||||
if self.server is not None:
|
||||
return
|
||||
if self.config.get('offline'):
|
||||
return
|
||||
|
||||
self.server = SwapServer(self.config, wallet)
|
||||
sm = wallet.lnworker.swap_manager
|
||||
jobs = [
|
||||
sm.pay_pending_invoices(),
|
||||
self.server.run(),
|
||||
]
|
||||
asyncio.run_coroutine_threadsafe(daemon._run(jobs=jobs), daemon.asyncio_loop)
|
||||
Reference in New Issue
Block a user