Skip to content

jmcore.confirmation

jmcore.confirmation

User confirmation prompts for fund-moving operations.

Functions:

confirm_transaction(operation: str, amount: int, destination: str | None = None, fee: int | None = None, mining_fee: int | None = None, additional_info: dict[str, Any] | None = None, skip_confirmation: bool = False, stage: str = '') -> bool

Prompt synchronously for confirmation of a fund-moving transaction.

Source code in jmcore/src/jmcore/confirmation.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def confirm_transaction(
    operation: str,
    amount: int,
    destination: str | None = None,
    fee: int | None = None,
    mining_fee: int | None = None,
    additional_info: dict[str, Any] | None = None,
    skip_confirmation: bool = False,
    stage: str = "",
) -> bool:
    """Prompt synchronously for confirmation of a fund-moving transaction."""
    should_prompt = _prepare_confirmation(
        operation=operation,
        amount=amount,
        destination=destination,
        fee=fee,
        mining_fee=mining_fee,
        additional_info=additional_info,
        skip_confirmation=skip_confirmation,
        stage=stage,
    )
    return _read_confirmation_response() if should_prompt else True

confirm_transaction_async(operation: str, amount: int, destination: str | None = None, fee: int | None = None, mining_fee: int | None = None, additional_info: dict[str, Any] | None = None, skip_confirmation: bool = False, stage: str = '') -> bool async

Prompt without blocking the event loop when stdin readers are supported.

Cancellation removes the registered reader, so a timed-out initial CoinJoin prompt cannot consume input intended for a later operation. Event loops that cannot watch stdin fall back to the synchronous prompt; callers must still enforce freshness after it returns.

Source code in jmcore/src/jmcore/confirmation.py
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
async def confirm_transaction_async(
    operation: str,
    amount: int,
    destination: str | None = None,
    fee: int | None = None,
    mining_fee: int | None = None,
    additional_info: dict[str, Any] | None = None,
    skip_confirmation: bool = False,
    stage: str = "",
) -> bool:
    """Prompt without blocking the event loop when stdin readers are supported.

    Cancellation removes the registered reader, so a timed-out initial CoinJoin
    prompt cannot consume input intended for a later operation. Event loops that
    cannot watch stdin fall back to the synchronous prompt; callers must still
    enforce freshness after it returns.
    """
    should_prompt = _prepare_confirmation(
        operation=operation,
        amount=amount,
        destination=destination,
        fee=fee,
        mining_fee=mining_fee,
        additional_info=additional_info,
        skip_confirmation=skip_confirmation,
        stage=stage,
    )
    if not should_prompt:
        return True

    loop = asyncio.get_running_loop()
    response_future: asyncio.Future[str] = loop.create_future()
    reader_registered = False

    def read_ready() -> None:
        if response_future.done():
            return
        try:
            response_future.set_result(sys.stdin.readline())
        except Exception as exc:
            response_future.set_exception(exc)

    try:
        try:
            loop.add_reader(sys.stdin.fileno(), read_ready)
            reader_registered = True
        except (AttributeError, NotImplementedError):
            return _read_confirmation_response()

        print("\nProceed with this transaction? [y/N]: ", end="", flush=True)
        try:
            response = await response_future
        except asyncio.CancelledError:
            print()
            raise
        if response == "":
            print("\n\nTransaction cancelled by user.")
            return False
        return response.strip().lower() in ("y", "yes")
    finally:
        if reader_registered:
            loop.remove_reader(sys.stdin.fileno())

format_maker_summary(makers: list[dict[str, Any]], fee_rate: float | None = None, amount: int | None = None, minimum_fee_rate: float | None = None) -> dict[str, Any]

Format maker information for confirmation display.

Args: makers: List of selected maker dicts with 'nick', 'fee', 'bond_value', 'location', etc. fee_rate: Fee rate in sat/vB (optional) amount: CoinJoin amount in satoshis, used to display fee percentages (optional) minimum_fee_rate: Required minimum fee rate in sat/vB (optional)

Returns: Dict with formatted maker info for confirmation display

Source code in jmcore/src/jmcore/confirmation.py
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
def format_maker_summary(
    makers: list[dict[str, Any]],
    fee_rate: float | None = None,
    amount: int | None = None,
    minimum_fee_rate: float | None = None,
) -> dict[str, Any]:
    """
    Format maker information for confirmation display.

    Args:
        makers: List of selected maker dicts with 'nick', 'fee', 'bond_value', 'location', etc.
        fee_rate: Fee rate in sat/vB (optional)
        amount: CoinJoin amount in satoshis, used to display fee percentages (optional)
        minimum_fee_rate: Required minimum fee rate in sat/vB (optional)

    Returns:
        Dict with formatted maker info for confirmation display
    """
    total_maker_fee = sum(m.get("fee", 0) for m in makers)

    # Find max widths for alignment
    max_fee_width = max((len(f"{m.get('fee', 0):,}") for m in makers), default=1)
    max_bond_width = max((len(f"{m.get('bond_value', 0):,}") for m in makers), default=1)

    maker_details = []
    for m in makers:
        nick = _sanitize_terminal_text(m.get("nick", "unknown"))
        fee = m.get("fee", 0)
        advertised_fee = m.get("advertised_fee", fee)
        bond_value = m.get("bond_value", 0)
        raw_location = m.get("location")
        location = _sanitize_terminal_text(raw_location) if raw_location else None

        # Right-align fee and bond values
        fee_str = f"{fee:>{max_fee_width},}"
        fee_percentage = _format_fee_percentage(fee, amount) if amount is not None else ""
        advertised_fee_str = (
            f" (advertised {advertised_fee:,} sats)" if advertised_fee != fee else ""
        )
        bond_str = f" [bond: {bond_value:>{max_bond_width},}]" if bond_value > 0 else " [no bond]"

        # Add location info if available
        if location and location != "NOT-SERVING-ONION":
            # Truncate onion address for readability (show first 16 chars)
            if ":" in location:
                onion, port = location.rsplit(":", 1)
                if onion.endswith(".onion") and len(onion) > 20:
                    location_str = f" @ {onion[:16]}...:{port}"
                else:
                    location_str = f" @ {location}"
            else:
                location_str = f" @ {location[:20]}..."
            maker_details.append(
                f"{nick}: {fee_str} sats{fee_percentage}{advertised_fee_str}{bond_str}{location_str}"
            )
        else:
            maker_details.append(
                f"{nick}: {fee_str} sats{fee_percentage}{advertised_fee_str}{bond_str}"
            )

    result: dict[str, Any] = {
        "Total Maker Fee": total_maker_fee,
        "Makers": maker_details,
    }

    if fee_rate is not None:
        result["Fee Rate"] = fee_rate

    if minimum_fee_rate is not None:
        result["Minimum Fee Rate"] = minimum_fee_rate

    return result

is_interactive_mode() -> bool

Check if we're running in interactive mode.

Returns False if NO_INTERACTIVE env var is set or if not attached to a TTY.

Source code in jmcore/src/jmcore/confirmation.py
15
16
17
18
19
20
21
22
23
def is_interactive_mode() -> bool:
    """
    Check if we're running in interactive mode.

    Returns False if NO_INTERACTIVE env var is set or if not attached to a TTY.
    """
    if os.environ.get("NO_INTERACTIVE"):
        return False
    return sys.stdin.isatty() and sys.stdout.isatty()