kaleido-cli 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ """Peer connection commands."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from typing import Annotated
7
+
8
+ import typer
9
+ from kaleido_sdk.rln import (
10
+ ConnectPeerRequest,
11
+ DisconnectPeerRequest,
12
+ ListPeersResponse,
13
+ )
14
+
15
+ from kaleido_cli.context import get_client
16
+ from kaleido_cli.output import (
17
+ is_json_mode,
18
+ output_collection,
19
+ print_json,
20
+ print_success,
21
+ )
22
+ from kaleido_cli.utils.errors import raise_cli_error
23
+ from kaleido_cli.utils.prompts import resolve_required_text
24
+
25
+ peer_app = typer.Typer(
26
+ no_args_is_help=True,
27
+ rich_markup_mode="rich",
28
+ help="Manage peer connections — list, connect, disconnect.",
29
+ )
30
+
31
+
32
+ @peer_app.command("list")
33
+ def peer_list() -> None:
34
+ """List connected peers."""
35
+ asyncio.run(_peer_list())
36
+
37
+
38
+ async def _peer_list() -> None:
39
+ try:
40
+ client = get_client(require_node=True)
41
+ resp: ListPeersResponse = await client.rln.list_peers()
42
+ if is_json_mode():
43
+ print_json(resp.model_dump())
44
+ return
45
+ output_collection(
46
+ "Peers", [p.model_dump() for p in (resp.peers or [])], item_title="Peer — {index}"
47
+ )
48
+ except Exception as e:
49
+ raise_cli_error(e)
50
+
51
+
52
+ @peer_app.command(
53
+ "connect",
54
+ epilog=(
55
+ "[bold]Examples[/bold]\n\n"
56
+ " Connect to a peer using pubkey@host:port format:\n"
57
+ " [cyan]kaleido peer connect 03abc...def@peer.kaleidoswap.com:9735[/cyan]\n\n"
58
+ "[dim]A peer connection is required before opening a channel.[/dim]"
59
+ ),
60
+ )
61
+ def peer_connect(
62
+ peer: Annotated[
63
+ str | None,
64
+ typer.Argument(help="Peer address in [green]pubkey@host:port[/green] format."),
65
+ ] = None,
66
+ ) -> None:
67
+ """Connect to a peer."""
68
+ resolved_peer = resolve_required_text(peer, "Peer (pubkey@host:port)", "PEER argument")
69
+
70
+ asyncio.run(_peer_connect(resolved_peer))
71
+
72
+
73
+ async def _peer_connect(peer: str) -> None:
74
+ try:
75
+ client = get_client(require_node=True)
76
+ await client.rln.connect_peer(ConnectPeerRequest(peer_pubkey_and_addr=peer))
77
+ print_success(f"Connected to {peer}")
78
+ except Exception as e:
79
+ raise_cli_error(e)
80
+
81
+
82
+ @peer_app.command(
83
+ "disconnect",
84
+ epilog=" [cyan]kaleido peer disconnect 03abc...def[/cyan] Use 'kaleido peer list' to find pubkeys.",
85
+ )
86
+ def peer_disconnect(
87
+ pubkey: Annotated[
88
+ str | None,
89
+ typer.Argument(help="Full pubkey of the peer to disconnect."),
90
+ ] = None,
91
+ ) -> None:
92
+ """Disconnect from a peer."""
93
+ resolved_pubkey = resolve_required_text(
94
+ pubkey,
95
+ "Peer pubkey (from 'kaleido peer list')",
96
+ "PUBKEY argument",
97
+ )
98
+
99
+ asyncio.run(_peer_disconnect(resolved_pubkey))
100
+
101
+
102
+ async def _peer_disconnect(pubkey: str) -> None:
103
+ try:
104
+ client = get_client(require_node=True)
105
+ await client.rln.disconnect_peer(DisconnectPeerRequest(peer_pubkey=pubkey))
106
+ print_success(f"Disconnected from {pubkey}")
107
+ except Exception as e:
108
+ raise_cli_error(e)
@@ -0,0 +1,460 @@
1
+ """Atomic swap commands."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from typing import Annotated
7
+
8
+ import typer
9
+ from kaleido_sdk import (
10
+ ConfirmSwapResponse,
11
+ PairQuoteResponse,
12
+ SwapResponse,
13
+ SwapStatusRequest,
14
+ SwapStatusResponse,
15
+ )
16
+ from kaleido_sdk.rln import (
17
+ TakerRequest,
18
+ )
19
+
20
+ from kaleido_cli.context import get_client
21
+ from kaleido_cli.output import (
22
+ is_interactive,
23
+ is_json_mode,
24
+ output_model,
25
+ print_error,
26
+ print_info,
27
+ print_json,
28
+ print_success,
29
+ )
30
+ from kaleido_cli.utils.errors import raise_cli_error
31
+ from kaleido_cli.utils.prompts import resolve_required_text
32
+ from kaleido_cli.utils.quotes import resolve_and_fetch_quote
33
+ from kaleido_cli.utils.swaps import (
34
+ confirm_swap_request,
35
+ decode_swapstring,
36
+ swap_request_from_quote,
37
+ validate_swapstring_against_quote,
38
+ validate_swapstring_against_swap,
39
+ )
40
+
41
+ swap_app = typer.Typer(
42
+ no_args_is_help=True,
43
+ rich_markup_mode="rich",
44
+ help=(
45
+ "Atomic swap flow against the Kaleidoswap maker server, using your local node as taker.\n\n"
46
+ "For low-level local node swaps, use [cyan]kaleido node swap ...[/cyan]."
47
+ ),
48
+ )
49
+ atomic_app = typer.Typer(
50
+ no_args_is_help=True,
51
+ rich_markup_mode="rich",
52
+ help="Atomic swap flow against the Kaleidoswap maker server, using your local node as taker.",
53
+ )
54
+
55
+ swap_app.add_typer(atomic_app, name="atomic")
56
+
57
+
58
+ def _confirm_quote_or_exit(quote: PairQuoteResponse, *, title: str, yes: bool) -> None:
59
+ """Show a quote and require explicit acceptance before continuing."""
60
+ if is_json_mode():
61
+ if not yes:
62
+ print_error("--yes is required in non-interactive mode to accept the quoted price.")
63
+ raise typer.Exit(1)
64
+ return
65
+
66
+ output_model(quote, title=title)
67
+
68
+ if is_interactive():
69
+ if yes:
70
+ return
71
+ if not typer.confirm("Proceed with this quote?", default=True):
72
+ print_error("Swap cancelled before initialization.")
73
+ raise typer.Exit(0)
74
+ return
75
+
76
+ if not yes:
77
+ print_error("--yes is required in non-interactive mode to accept the quoted price.")
78
+ raise typer.Exit(1)
79
+
80
+
81
+ @atomic_app.command(
82
+ "init",
83
+ epilog=(
84
+ "[bold]Examples[/bold]\n\n"
85
+ " Initialize an atomic swap from a live quote:\n"
86
+ " [cyan]kaleido swap atomic init BTC/USDT --to-amount 5[/cyan]\n\n"
87
+ "[dim]After init, you can whitelist explicitly, or let execute do it for you:[/dim]\n"
88
+ "[cyan]kaleido node swap whitelist --swapstring '<swapstring>'[/cyan]\n"
89
+ "[cyan]kaleido swap atomic execute --swapstring '<swapstring>' "
90
+ "--taker-pubkey <pubkey> --payment-hash <payment-hash>[/cyan]\n"
91
+ "[cyan]kaleido swap atomic execute --auto-whitelist --swapstring '<swapstring>' "
92
+ "--taker-pubkey <pubkey> --payment-hash <payment-hash>[/cyan]"
93
+ ),
94
+ )
95
+ def atomic_init(
96
+ pair: Annotated[
97
+ str | None, typer.Argument(help="Trading pair in BASE/QUOTE format, e.g. BTC/USDT.")
98
+ ] = None,
99
+ from_amount: Annotated[
100
+ str | None,
101
+ typer.Option(
102
+ "--from-amount",
103
+ help="Amount to send in display units. Provide this OR --to-amount.",
104
+ ),
105
+ ] = None,
106
+ to_amount: Annotated[
107
+ str | None,
108
+ typer.Option(
109
+ "--to-amount",
110
+ help="Amount to receive in display units. Provide this OR --from-amount.",
111
+ ),
112
+ ] = None,
113
+ from_layer: Annotated[
114
+ str | None,
115
+ typer.Option(
116
+ "--from-layer",
117
+ help="Source layer: BTC_L1, BTC_LN, RGB_L1, RGB_LN. Defaults from the requested pair direction.",
118
+ ),
119
+ ] = None,
120
+ to_layer: Annotated[
121
+ str | None,
122
+ typer.Option(
123
+ "--to-layer",
124
+ help="Destination layer: BTC_L1, BTC_LN, RGB_L1, RGB_LN. Defaults from the requested pair direction.",
125
+ ),
126
+ ] = None,
127
+ yes: Annotated[
128
+ bool,
129
+ typer.Option(
130
+ "--yes",
131
+ "-y",
132
+ help="Accept the displayed quote without prompting. Required in non-interactive mode.",
133
+ ),
134
+ ] = False,
135
+ ) -> None:
136
+ """Initialize an atomic swap against the maker server using a live quote."""
137
+ asyncio.run(
138
+ _atomic_init(
139
+ pair,
140
+ from_amount,
141
+ to_amount,
142
+ from_layer,
143
+ to_layer,
144
+ yes,
145
+ )
146
+ )
147
+
148
+
149
+ async def _atomic_init(
150
+ pair: str | None,
151
+ from_amount: str | None,
152
+ to_amount: str | None,
153
+ from_layer: str | None,
154
+ to_layer: str | None,
155
+ yes: bool,
156
+ ) -> None:
157
+ try:
158
+ client = get_client()
159
+ resolved_quote = await resolve_and_fetch_quote(
160
+ client,
161
+ pair=pair,
162
+ from_amount=from_amount,
163
+ to_amount=to_amount,
164
+ from_layer=from_layer,
165
+ to_layer=to_layer,
166
+ )
167
+ quote = resolved_quote.quote
168
+ _confirm_quote_or_exit(quote, title=f"Quote — {resolved_quote.inputs.pair}", yes=yes)
169
+ resp: SwapResponse = await client.maker.init_swap(swap_request_from_quote(quote))
170
+ if is_json_mode():
171
+ print_json(resp.model_dump())
172
+ else:
173
+ print_success(f"Atomic swap initialized: {resp.payment_hash}")
174
+ output_model(resp, title="Atomic Swap Init")
175
+ print_info("Next step: choose one of these two flows.")
176
+ print_info(
177
+ "Flow 1 (manual): whitelist first on your local taker node, then execute against the maker server."
178
+ )
179
+ print_info(f" kaleido node swap whitelist --swapstring '{resp.swapstring}'")
180
+ print_info(
181
+ f" kaleido swap atomic execute --swapstring '{resp.swapstring}' "
182
+ f"--taker-pubkey <pubkey> --payment-hash {resp.payment_hash}"
183
+ )
184
+ print_info(
185
+ "Flow 2 (automatic): let atomic execute whitelist on your local node first, then execute against the maker server."
186
+ )
187
+ print_info(
188
+ f" kaleido swap atomic execute --auto-whitelist --swapstring '{resp.swapstring}' "
189
+ f"--taker-pubkey <pubkey> --payment-hash {resp.payment_hash}"
190
+ )
191
+ except typer.Exit:
192
+ raise
193
+ except Exception as e:
194
+ raise_cli_error(e)
195
+
196
+
197
+ @atomic_app.command(
198
+ "execute",
199
+ epilog=(
200
+ "[bold]Examples[/bold]\n\n"
201
+ " Execute a previously initialized atomic swap:\n"
202
+ " [cyan]kaleido swap atomic execute --swapstring '<swapstring>' "
203
+ "--taker-pubkey 03ab... --payment-hash deadbeef...[/cyan]\n\n"
204
+ " Auto-whitelist before executing:\n"
205
+ " [cyan]kaleido swap atomic execute --auto-whitelist --swapstring '<swapstring>' "
206
+ "--taker-pubkey 03ab... --payment-hash deadbeef...[/cyan]\n\n"
207
+ "[dim]Use the taker node pubkey from 'kaleido node swap pubkey' or your node's pubkey.[/dim]"
208
+ ),
209
+ )
210
+ def atomic_execute(
211
+ swapstring: Annotated[
212
+ str | None, typer.Option("--swapstring", help="Swap string returned by atomic init.")
213
+ ] = None,
214
+ taker_pubkey: Annotated[
215
+ str | None, typer.Option("--taker-pubkey", help="Taker node pubkey.")
216
+ ] = None,
217
+ payment_hash: Annotated[
218
+ str | None, typer.Option("--payment-hash", help="Payment hash returned by atomic init.")
219
+ ] = None,
220
+ auto_whitelist: Annotated[
221
+ bool,
222
+ typer.Option(
223
+ "--auto-whitelist",
224
+ help="Whitelist the swap on the local taker node before executing it.",
225
+ ),
226
+ ] = False,
227
+ ) -> None:
228
+ """Execute an atomic swap against the maker server."""
229
+ resolved_swapstring = resolve_required_text(swapstring, "Swap string", "--swapstring")
230
+ resolved_taker_pubkey = resolve_required_text(taker_pubkey, "Taker pubkey", "--taker-pubkey")
231
+ resolved_payment_hash = resolve_required_text(payment_hash, "Payment hash", "--payment-hash")
232
+ if is_interactive() and not auto_whitelist:
233
+ auto_whitelist = typer.confirm(
234
+ "Auto-whitelist on the local taker node before executing?",
235
+ default=False,
236
+ )
237
+ asyncio.run(
238
+ _atomic_execute(
239
+ resolved_swapstring, resolved_taker_pubkey, resolved_payment_hash, auto_whitelist
240
+ )
241
+ )
242
+
243
+
244
+ async def _atomic_execute(
245
+ swapstring: str,
246
+ taker_pubkey: str,
247
+ payment_hash: str,
248
+ auto_whitelist: bool,
249
+ ) -> None:
250
+ try:
251
+ client = get_client(require_node=auto_whitelist)
252
+ if auto_whitelist:
253
+ decoded = decode_swapstring(swapstring)
254
+ status = await client.maker.get_atomic_swap_status(
255
+ SwapStatusRequest(payment_hash=payment_hash)
256
+ )
257
+ if status.swap is None:
258
+ print_error(
259
+ "Maker returned no swap payload for --payment-hash; refusing to auto-whitelist."
260
+ )
261
+ raise typer.Exit(1)
262
+ try:
263
+ validate_swapstring_against_swap(
264
+ decoded,
265
+ status.swap,
266
+ payment_hash=payment_hash,
267
+ )
268
+ except ValueError as exc:
269
+ print_error(f"Auto-whitelist validation failed: {exc}")
270
+ raise typer.Exit(1)
271
+ await client.rln.whitelist_swap(TakerRequest(swapstring=swapstring))
272
+ resp: ConfirmSwapResponse = await client.maker.execute_swap(
273
+ confirm_swap_request(
274
+ swapstring=swapstring,
275
+ taker_pubkey=taker_pubkey,
276
+ payment_hash=payment_hash,
277
+ )
278
+ )
279
+ if is_json_mode():
280
+ print_json(resp.model_dump())
281
+ else:
282
+ if auto_whitelist:
283
+ print_success("Atomic swap whitelisted on taker node")
284
+ print_success("Atomic swap execution submitted")
285
+ output_model(resp, title="Atomic Swap Execute")
286
+ except Exception as e:
287
+ raise_cli_error(e)
288
+
289
+
290
+ @atomic_app.command(
291
+ "status",
292
+ epilog=" [cyan]kaleido swap atomic status <payment-hash>[/cyan]",
293
+ )
294
+ def atomic_status(
295
+ payment_hash: Annotated[str, typer.Argument(help="Atomic swap payment hash.")],
296
+ ) -> None:
297
+ """Check the status of an atomic swap against the maker server."""
298
+ asyncio.run(_atomic_status(payment_hash))
299
+
300
+
301
+ async def _atomic_status(payment_hash: str) -> None:
302
+ try:
303
+ client = get_client()
304
+ resp: SwapStatusResponse = await client.maker.get_atomic_swap_status(
305
+ SwapStatusRequest(payment_hash=payment_hash)
306
+ )
307
+ if is_json_mode():
308
+ print_json(resp.model_dump())
309
+ else:
310
+ output_model(resp, title=f"Atomic Swap — {payment_hash[:16]}…")
311
+ except Exception as e:
312
+ raise_cli_error(e)
313
+
314
+
315
+ @atomic_app.command(
316
+ "run",
317
+ epilog=(
318
+ "[bold]Examples[/bold]\n\n"
319
+ " Run an atomic swap in one command using your local taker node:\n"
320
+ " [cyan]kaleido swap atomic run BTC/USDT --to-amount 5[/cyan]\n\n"
321
+ " Non-interactive flow with an explicit taker pubkey:\n"
322
+ " [cyan]kaleido swap atomic run BTC/USDT --from-amount 0.001 --from-layer BTC_LN "
323
+ "--to-layer RGB_LN --taker-pubkey 03ab... --yes[/cyan]\n\n"
324
+ "[dim]This wrapper automates atomic init, local taker whitelist, and atomic execute.[/dim]"
325
+ ),
326
+ )
327
+ def atomic_run(
328
+ pair: Annotated[
329
+ str | None, typer.Argument(help="Trading pair in BASE/QUOTE format, e.g. BTC/USDT.")
330
+ ] = None,
331
+ from_amount: Annotated[
332
+ str | None,
333
+ typer.Option(
334
+ "--from-amount",
335
+ help="Amount to send in display units. Provide this OR --to-amount.",
336
+ ),
337
+ ] = None,
338
+ to_amount: Annotated[
339
+ str | None,
340
+ typer.Option(
341
+ "--to-amount",
342
+ help="Amount to receive in display units. Provide this OR --from-amount.",
343
+ ),
344
+ ] = None,
345
+ from_layer: Annotated[
346
+ str | None,
347
+ typer.Option(
348
+ "--from-layer",
349
+ help="Source layer: BTC_L1, BTC_LN, RGB_L1, RGB_LN. Defaults from the requested pair direction.",
350
+ ),
351
+ ] = None,
352
+ to_layer: Annotated[
353
+ str | None,
354
+ typer.Option(
355
+ "--to-layer",
356
+ help="Destination layer: BTC_L1, BTC_LN, RGB_L1, RGB_LN. Defaults from the requested pair direction.",
357
+ ),
358
+ ] = None,
359
+ taker_pubkey: Annotated[
360
+ str | None,
361
+ typer.Option(
362
+ "--taker-pubkey", help="Taker node pubkey. Defaults to the local node taker pubkey."
363
+ ),
364
+ ] = None,
365
+ yes: Annotated[
366
+ bool,
367
+ typer.Option(
368
+ "--yes",
369
+ "-y",
370
+ help="Accept the displayed quote and skip later confirmations. Required in non-interactive mode.",
371
+ ),
372
+ ] = False,
373
+ ) -> None:
374
+ """Run an atomic swap end-to-end using the local node as taker."""
375
+ asyncio.run(
376
+ _atomic_run(
377
+ pair,
378
+ from_amount,
379
+ to_amount,
380
+ from_layer,
381
+ to_layer,
382
+ taker_pubkey,
383
+ yes,
384
+ )
385
+ )
386
+
387
+
388
+ async def _atomic_run(
389
+ pair: str | None,
390
+ from_amount: str | None,
391
+ to_amount: str | None,
392
+ from_layer: str | None,
393
+ to_layer: str | None,
394
+ taker_pubkey_override: str | None,
395
+ yes: bool,
396
+ ) -> None:
397
+ try:
398
+ client = get_client(require_node=True)
399
+ resolved_quote = await resolve_and_fetch_quote(
400
+ client,
401
+ pair=pair,
402
+ from_amount=from_amount,
403
+ to_amount=to_amount,
404
+ from_layer=from_layer,
405
+ to_layer=to_layer,
406
+ )
407
+ quote = resolved_quote.quote
408
+ _confirm_quote_or_exit(quote, title=f"Quote — {resolved_quote.inputs.pair}", yes=yes)
409
+ init_resp: SwapResponse = await client.maker.init_swap(swap_request_from_quote(quote))
410
+ decoded_swap = decode_swapstring(init_resp.swapstring)
411
+ validate_swapstring_against_quote(
412
+ decoded_swap,
413
+ quote,
414
+ payment_hash=init_resp.payment_hash,
415
+ )
416
+ resolved_taker_pubkey = taker_pubkey_override or await client.rln.get_taker_pubkey()
417
+
418
+ if not yes and is_interactive():
419
+ confirmed = typer.confirm(
420
+ "Whitelist this atomic swap on the local taker node and execute it now?",
421
+ default=True,
422
+ )
423
+ if not confirmed:
424
+ print_error("Swap cancelled after atomic init.")
425
+ raise typer.Exit(0)
426
+
427
+ await client.rln.whitelist_swap(TakerRequest(swapstring=init_resp.swapstring))
428
+ execute_resp: ConfirmSwapResponse = await client.maker.execute_swap(
429
+ confirm_swap_request(
430
+ swapstring=init_resp.swapstring,
431
+ taker_pubkey=resolved_taker_pubkey,
432
+ payment_hash=init_resp.payment_hash,
433
+ )
434
+ )
435
+
436
+ if is_json_mode():
437
+ print_json(
438
+ {
439
+ "init": init_resp.model_dump(),
440
+ "whitelisted": True,
441
+ "taker_pubkey": resolved_taker_pubkey,
442
+ "execute": execute_resp.model_dump(),
443
+ }
444
+ )
445
+ else:
446
+ print_success(f"Atomic swap initialized: {init_resp.payment_hash}")
447
+ output_model(init_resp, title="Atomic Swap Init")
448
+ print_success("Swap whitelisted on local taker node")
449
+ print_success("Atomic swap execution submitted")
450
+ output_model(
451
+ {
452
+ "taker_pubkey": resolved_taker_pubkey,
453
+ **execute_resp.model_dump(),
454
+ },
455
+ title="Atomic Swap Execute",
456
+ )
457
+ except typer.Exit:
458
+ raise
459
+ except Exception as e:
460
+ raise_cli_error(e)