This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
channel-creation.py
executable file
·508 lines (392 loc) · 16.2 KB
/
channel-creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python3
# TODO: consistent error handling
import json
import math
import random
from dataclasses import dataclass
from enum import Enum
from hashlib import sha256
from os import path, urandom
from typing import Mapping
import requests
from bitcoin import SelectParams
from bitcoin.core import CTransaction, CMutableTransaction, CMutableTxIn, CMutableTxOut, COutPoint, CTxWitness, \
CTxInWitness, CScriptWitness
from bitcoin.core import x, b2x
from bitcoin.core.script import OP_0, OP_HASH160, OP_EQUAL, CScript, Hash160, SignatureHash, SIGHASH_ALL, \
SIGVERSION_WITNESS_V0
from bitcoin.wallet import CBitcoinAddress, CBitcoinAddressError, CBitcoinSecret
from pyln.client import Plugin, Millisatoshi
class Status(Enum):
Created = "created"
ChannelAccepted = "channel_accepted"
InvoicePaid = "invoice_paid"
Refunded = "refunded"
@dataclass
class ChannelCreation:
status: Status
id: str
private_key: str
redeem_script: str
private: bool
invoice_amount: int
inbound_percentage: int
invoice_label: str
preimage_hash: str
address: str
expected_amount: int
bip21: str
PLUGIN = Plugin()
class BoltzApi:
def __init__(self, api: str):
self.api = api
def get_nodes(self):
request = requests.get("{}/getnodes".format(self.api))
return request.json()
def get_swap_transaction(self, swap_id: str):
request = requests.post(
"{}/getswaptransaction".format(self.api),
json={
"id": swap_id,
},
)
return request.json()
def create_swap(self, invoice: str, refund_key: str, private: bool, inbound: int):
request = requests.post(
"{}/createswap".format(self.api),
json={
"type": "submarine",
"pairId": "BTC/BTC",
"orderSide": "buy",
"invoice": invoice,
"refundPublicKey": refund_key,
"channel": {
"auto": False,
"private": private,
"inboundLiquidity": inbound,
},
},
)
return request.json()
def print_error(message: str):
print(message)
return {
"error": message[0].lower() + message[1:],
}
def get_keys():
entropy = urandom(32)
private_key = CBitcoinSecret.from_secret_bytes(entropy)
return entropy.hex(), private_key.pub.hex()
def format_channel_creation(channel_creation: ChannelCreation):
return {
"status": channel_creation.status.name,
"id": channel_creation.id,
"private_key": channel_creation.private_key,
"redeem_script": channel_creation.redeem_script,
"private": channel_creation.private,
"invoice_amount": channel_creation.invoice_amount,
"inbound_percentage": channel_creation.inbound_percentage,
"invoice_label": channel_creation.invoice_label,
"preimage_hash": channel_creation.preimage_hash,
"address": channel_creation.address,
"expected_amount": channel_creation.expected_amount,
"bip21": channel_creation.bip21,
}
def write_channel_creation(plugin: Plugin, channel_creation: ChannelCreation):
with open(plugin.data_location, 'w') as file:
json.dump(format_channel_creation(channel_creation), file)
def update_channel_creation_status(plugin: Plugin, channel_creation: ChannelCreation, new_state: Status):
channel_creation.status = new_state
write_channel_creation(plugin, channel_creation)
def read_channel_creation(plugin: Plugin):
try:
with open(plugin.data_location, 'r') as file:
raw_data = json.load(file)
plugin.channel_creation = ChannelCreation(
status=Status[raw_data["status"]],
id=raw_data["id"],
private_key=raw_data["private_key"],
redeem_script=raw_data["redeem_script"],
private=raw_data["private"],
invoice_amount=raw_data["invoice_amount"],
inbound_percentage=raw_data["inbound_percentage"],
invoice_label=raw_data["invoice_label"],
preimage_hash=raw_data["preimage_hash"],
address=raw_data["address"],
expected_amount=raw_data["expected_amount"],
bip21=raw_data["bip21"],
)
print("Read exiting channel creation state: {}".format(format_channel_creation(plugin.channel_creation)))
except FileNotFoundError:
print("Did not find existing channel creation state")
def check_channel_open(openchannel, plugin: Plugin) -> str:
push_amount = Millisatoshi(openchannel["push_msat"]).millisatoshis
if push_amount != 0:
return "push amount not 0"
channel_creation = plugin.channel_creation
capacity = Millisatoshi(openchannel["funding_satoshis"]).millisatoshis
expected_capacity = math.floor(channel_creation.invoice_amount / (1 - (channel_creation.inbound_percentage / 100)))
if expected_capacity > capacity:
return "minimum capacity requirement of {}msat not met".format(expected_capacity)
channel_flags = openchannel["channel_flags"]
# A private channel has "channel_flags" 0
if channel_creation.private and channel_flags == 1:
return "channel is not private"
elif not channel_creation.private and channel_flags == 0:
return "channel is not public"
return ""
def find_swap_output(lockup_transaction: CTransaction, redeem_script: str) -> int:
redeem_hash = sha256(x(redeem_script)).digest()
pw2sh_script = CScript([OP_0, redeem_hash])
p2sh_script = CScript([OP_HASH160, Hash160(pw2sh_script), OP_EQUAL]).hex()
for i in range(len(lockup_transaction.vout)):
if lockup_transaction.vout[i].scriptPubKey.hex() == p2sh_script:
return i
raise ValueError("could not find swap output")
def construct_refund_transaction(
channel_creation: ChannelCreation,
lockup_transaction: CTransaction,
address: str,
timeout_block_height: int,
) -> CMutableTransaction:
redeem_script = CScript.fromhex(channel_creation.redeem_script)
lockup_vout = find_swap_output(lockup_transaction, channel_creation.redeem_script)
nested_script = b'\x22\x00\x20' + sha256(redeem_script).digest()
inputs = [CMutableTxIn(COutPoint(lockup_transaction.GetTxid(), lockup_vout), nested_script, 0xfffffffd)]
try:
output_script = CBitcoinAddress(address).to_scriptPubKey()
except CBitcoinAddressError:
raise ValueError("Could not parse Bitcoin address: {}".format(address))
input_amount = lockup_transaction.vout[lockup_vout].nValue
# TODO: configurable transaction fee + reasonable default
output_amount = input_amount - 1000
outputs = [CMutableTxOut(output_amount, output_script)]
refund_transaction = CMutableTransaction(inputs, outputs, timeout_block_height)
sighash = SignatureHash(
inIdx=0,
amount=input_amount,
hashtype=SIGHASH_ALL,
script=redeem_script,
txTo=refund_transaction,
sigversion=SIGVERSION_WITNESS_V0,
)
key = CBitcoinSecret.from_secret_bytes(bytes.fromhex(channel_creation.private_key))
sig = key.sign(sighash) + bytes([SIGHASH_ALL])
refund_transaction.wit = CTxWitness([CTxInWitness(CScriptWitness([sig, bytes(), redeem_script]))])
return refund_transaction
@PLUGIN.init()
def init(plugin: Plugin, options: Mapping[str, str], **_kwargs):
plugin.data_location = path.join(plugin.rpc.listconfigs()["lightning-dir"], "channel-creation.json")
print("Channel Creation data location: {}".format(plugin.data_location))
read_channel_creation(plugin)
info = plugin.rpc.getinfo()
network = info["network"]
SelectParams(network)
if options["boltz-api"] == "":
if network == "mainnet":
api_url = "https://boltz.exchange/api"
elif network == "testnet":
api_url = "https://testnet.boltz.exchange/api"
elif network == "regtest":
api_url = "http://127.0.0.1:9001"
else:
raise ValueError("No default API for network {} available".format(network))
plugin.boltz_api = BoltzApi(api_url)
print("Using default Boltz API for network {}: {}".format(network, api_url))
else:
plugin.boltz_api = BoltzApi(options["boltz-api"])
plugin.boltz_node = options["boltz-node"]
if plugin.boltz_node == "":
nodes = plugin.boltz_api.get_nodes()
plugin.boltz_node = nodes["nodes"]["BTC"]["nodeKey"]
print("Fetched Boltz lightning node public key: {}".format(plugin.boltz_node))
print("Started channel-creation plugin: {}".format({
"boltz_api": plugin.boltz_api.api,
"boltz_node": plugin.boltz_node,
}))
#
# RPC methods
#
# TODO: create valid refund.json
# TODO: sanity check if there is a channel already
@PLUGIN.method("addchannelcreation")
def add_channel_creation(plugin: Plugin, invoice_amount: int, inbound_percentage: int, private=False):
"""Adds a new Boltz Channel Creation Swap"""
if hasattr(plugin, "channel_creation") \
and plugin.channel_creation is not None \
and (plugin.channel_creation.status is not Status.InvoicePaid and
plugin.channel_creation.status is not Status.Refunded):
return {
"error": "there is a pending channel creation already",
}
peers = plugin.rpc.listpeers()["peers"]
for peer in peers:
if peer["id"] == plugin.boltz_node and len(peer["channels"]) != 0:
channels = peer["channels"]
for channel in channels:
if channel["state"] != "ONCHAIN":
return {
"error": "there is a channel with the Boltz node already"
}
try:
invoice_label = "boltz-channel-{}".format(random.randint(0, 100000))
invoice_response = plugin.rpc.invoice(
invoice_amount,
invoice_label,
"Boltz Channel Creation Swap",
)
private_key, public_key = get_keys()
swap = plugin.boltz_api.create_swap(
invoice_response["bolt11"],
public_key,
private,
inbound_percentage,
)
if "error" in swap:
return print_error("Could not setup channel creation: {}".format(str(swap["error"])))
print("Created swap: {}".format(swap))
plugin.channel_creation = ChannelCreation(
id=swap["id"],
private=private,
bip21=swap["bip21"],
status=Status.Created,
address=swap["address"],
private_key=private_key,
invoice_label=invoice_label,
invoice_amount=invoice_amount,
redeem_script=swap["redeemScript"],
inbound_percentage=inbound_percentage,
expected_amount=swap["expectedAmount"],
preimage_hash=invoice_response["payment_hash"],
)
write_channel_creation(plugin, plugin.channel_creation)
print("Added channel creation: {}".format(format_channel_creation(plugin.channel_creation)))
return {
"address": plugin.channel_creation.address,
"expectedAmount": plugin.channel_creation.expected_amount,
"bip21": swap["bip21"],
}
except requests.ConnectionError or requests.HTTPError as error:
return print_error("Could not add channel creation: {}".format(str(error)))
@PLUGIN.method("getchannelcreation")
def get_channel_creation(plugin: Plugin):
"""Gets all available information about the added Boltz Channel Creation Swap"""
if not hasattr(plugin, "channel_creation"):
return {
"error": "no channel creation was added"
}
return format_channel_creation(plugin.channel_creation)
@PLUGIN.method("refundchannelcreation")
def refund_channel_creation(plugin: Plugin, address=""):
"""Refunds the lockup transaction of a Channel Creation Swap"""
if not hasattr(plugin, "channel_creation") or \
plugin.channel_creation is None or \
(plugin.channel_creation.status is Status.InvoicePaid or plugin.channel_creation.status is Status.Refunded):
return {
"error": "no refundable channel creation found"
}
print("Refunding channel creation {}".format(plugin.channel_creation.id))
info = plugin.rpc.getchaininfo()
swap_transaction = plugin.boltz_api.get_swap_transaction(plugin.channel_creation.id)
if hasattr(swap_transaction, "error"):
return {
"error": "could not find lockup transaction: {}".format(swap_transaction["error"]),
}
if info["blockcount"] < swap_transaction["timeoutBlockHeight"]:
return {
"error": "channel creation cannot be refunded before block height {}"
.format(swap_transaction["timeoutBlockHeight"])
}
if address == "":
new_addr = plugin.rpc.newaddr()["address"]
address = new_addr
print("Got address to refund to: {}".format(address))
lock_transaction = CTransaction.deserialize(x(swap_transaction["transactionHex"]))
refund_transaction = construct_refund_transaction(
plugin.channel_creation,
lock_transaction,
address,
swap_transaction["timeoutBlockHeight"],
)
refund_transaction_hex = b2x(refund_transaction.serialize())
refund_transaction_id = b2x(refund_transaction.GetTxid()[::-1])
print("Constructed refund transaction {}: {}".format(refund_transaction_id, refund_transaction_hex))
broadcast_response = plugin.rpc.sendrawtransaction(refund_transaction_hex, False)
if broadcast_response["success"]:
print("Broadcast refund transaction: {}".format(refund_transaction_id))
update_channel_creation_status(plugin, plugin.channel_creation, Status.Refunded)
else:
return {
"error": "could not broadcast refund transaction {}"
.format(broadcast_response["errmsg"])
}
return {
"txid": refund_transaction_id,
"txhex": refund_transaction_hex,
}
#
# Hooks
#
@PLUGIN.hook("openchannel")
def on_openchannel(openchannel, plugin: Plugin, **_kwargs):
if openchannel["id"] != plugin.boltz_node or \
not hasattr(plugin, "channel_creation") or \
plugin.channel_creation.status != Status.Created:
return {
"result": "continue",
}
error = check_channel_open(openchannel, plugin)
if error != "":
print("Rejected channel creation: {}".format(error))
return {
"result": "reject",
"error_message": error,
}
update_channel_creation_status(plugin, plugin.channel_creation, Status.ChannelAccepted)
print("Accepted channel creation")
return {
"result": "continue",
}
@PLUGIN.async_hook("invoice_payment")
def on_invoice_payment(payment, plugin: Plugin, request, **_kwargs):
if not hasattr(plugin, "channel_creation") or plugin.channel_creation.invoice_label != payment["label"]:
request.set_result({
"result": "continue"
})
return
# No channel has been opened yet
if plugin.channel_creation.status != Status.ChannelAccepted:
print("Rejected invoice payment: no channel was opened yet")
request.set_result({
"result": "reject",
})
return
sent_over_right_channel = False
channel = plugin.rpc.listpeers(plugin.boltz_node)["peers"][0]["channels"][-1]
for htlc in channel["htlcs"]:
if htlc["payment_hash"] == plugin.channel_creation.preimage_hash:
sent_over_right_channel = True
break
if not sent_over_right_channel:
print("Rejected invoice payment: it was not paid trough the right channel")
request.set_result({
"result": "reject",
})
return
update_channel_creation_status(plugin, plugin.channel_creation, Status.InvoicePaid)
print("Accepted invoice payment")
request.set_result({
"result": "continue"
})
# TODO: automatically connect to node
PLUGIN.add_option(
"boltz-api",
"",
"Boltz API endpoint"
)
PLUGIN.add_option(
"boltz-node",
"",
"Public key of the Boltz Lightning node"
)
PLUGIN.run()