Skip to content

jmwallet.cli.sign_psbt

jmwallet.cli.sign_psbt

Offline PSBT inspection and signing command.

Attributes

MAX_PSBT_SIZE = 10 * 1024 * 1024 module-attribute

Classes

Functions:

sign_psbt(psbt_base64: Annotated[str | None, typer.Argument(help='Base64-encoded PSBT v0')] = None, input_file: Annotated[Path | None, typer.Option('--input', '-i', help='Read a binary or base64 PSBT from a file')] = None, output_file: Annotated[Path | None, typer.Option('--output', '-o', help='Write the signed base64 PSBT to a file')] = None, mnemonic_file: Annotated[Path | None, typer.Option('--mnemonic-file', '-f', envvar='MNEMONIC_FILE')] = None, prompt_bip39_passphrase: Annotated[bool, typer.Option('--prompt-bip39-passphrase', help='Prompt for BIP39 passphrase')] = False, network: Annotated[str | None, typer.Option('--network', '-n', help='Bitcoin network')] = None, scan_range: Annotated[int | None, typer.Option('--scan-range', min=0, max=1000000, help='Fallback addresses per branch to derive when PSBT key origins are absent')] = None, yes: Annotated[bool, typer.Option('--yes', '-y', help='Sign without the confirmation prompt')] = False, data_dir: Annotated[Path | None, typer.Option('--data-dir', envvar='JOINMARKET_DATA_DIR', help='Data directory (default: ~/.joinmarket-ng or $JOINMARKET_DATA_DIR)')] = None, config_file: Annotated[Path | None, typer.Option('--config-file', envvar='JOINMARKET_CONFIG_FILE', help='Config file path (default: <data-dir>/config.toml)')] = None, log_level: Annotated[str | None, typer.Option('--log-level', '-l', help='Log level')] = None) -> None

Inspect and partially sign wallet-owned native SegWit PSBT inputs offline.

Supports regular P2WPKH wallet inputs and canonical JoinMarket fidelity bond P2WSH inputs. Every input must include witness_utxo data so the complete fee can be reviewed. The command never connects to a backend or broadcasts.

Source code in jmwallet/src/jmwallet/cli/sign_psbt.py
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
@app.command("sign-psbt", no_args_is_help=True)
def sign_psbt(
    psbt_base64: Annotated[str | None, typer.Argument(help="Base64-encoded PSBT v0")] = None,
    input_file: Annotated[
        Path | None,
        typer.Option("--input", "-i", help="Read a binary or base64 PSBT from a file"),
    ] = None,
    output_file: Annotated[
        Path | None,
        typer.Option("--output", "-o", help="Write the signed base64 PSBT to a file"),
    ] = None,
    mnemonic_file: Annotated[
        Path | None, typer.Option("--mnemonic-file", "-f", envvar="MNEMONIC_FILE")
    ] = None,
    prompt_bip39_passphrase: Annotated[
        bool, typer.Option("--prompt-bip39-passphrase", help="Prompt for BIP39 passphrase")
    ] = False,
    network: Annotated[str | None, typer.Option("--network", "-n", help="Bitcoin network")] = None,
    scan_range: Annotated[
        int | None,
        typer.Option(
            "--scan-range",
            min=0,
            max=1_000_000,
            help="Fallback addresses per branch to derive when PSBT key origins are absent",
        ),
    ] = None,
    yes: Annotated[
        bool, typer.Option("--yes", "-y", help="Sign without the confirmation prompt")
    ] = False,
    data_dir: Annotated[
        Path | None,
        typer.Option(
            "--data-dir",
            envvar="JOINMARKET_DATA_DIR",
            help="Data directory (default: ~/.joinmarket-ng or $JOINMARKET_DATA_DIR)",
        ),
    ] = None,
    config_file: Annotated[
        Path | None,
        typer.Option(
            "--config-file",
            envvar="JOINMARKET_CONFIG_FILE",
            help="Config file path (default: <data-dir>/config.toml)",
        ),
    ] = None,
    log_level: Annotated[str | None, typer.Option("--log-level", "-l", help="Log level")] = None,
) -> None:
    """Inspect and partially sign wallet-owned native SegWit PSBT inputs offline.

    Supports regular P2WPKH wallet inputs and canonical JoinMarket fidelity bond
    P2WSH inputs. Every input must include witness_utxo data so the complete fee
    can be reviewed. The command never connects to a backend or broadcasts.
    """
    settings = setup_cli(log_level, data_dir=data_dir, config_file=config_file)
    try:
        raw_psbt = _load_psbt(psbt_base64, input_file)
        resolved = resolve_mnemonic(
            settings,
            mnemonic_file=mnemonic_file,
            prompt_bip39_passphrase=prompt_bip39_passphrase,
        )
        if resolved is None:
            raise ValueError("No mnemonic provided")

        resolved_network = NetworkType(network or settings.network_config.network.value).value
        resolved_scan_range = settings.wallet.scan_range if scan_range is None else scan_range
        wallet = WalletService(
            mnemonic=resolved.mnemonic,
            backend=OfflineBackend(),
            network=resolved_network,
            mixdepth_count=settings.wallet.mixdepth_count,
            scan_range=resolved_scan_range,
            passphrase=resolved.bip39_passphrase,
        )
        plan = wallet.prepare_psbt_signing(raw_psbt, resolved_scan_range)
        if plan.owned_count == 0:
            raise ValueError("The PSBT contains no inputs owned by this wallet")
        if plan.fee > 0:
            enforce_fee_rate_cap(
                plan.estimated_fee_rate,
                settings.wallet.max_fee_rate_sat_vb,
                source="PSBT estimated",
            )
        else:
            logger.warning("PSBT pays a zero fee and may not be relayable")

        _display_plan(plan, resolved_network)
        if plan.signable_count > 0 and not yes:
            if not typer.confirm("Sign the wallet-owned inputs shown above?", default=False):
                typer.echo("Signing cancelled")
                raise typer.Exit(1)

        result = wallet.sign_psbt(plan)
        _write_result(result.psbt, output_file)
        typer.echo(
            f"Signed {len(result.signed_indices)} input(s); "
            f"{len(result.already_signed_indices)} already had valid wallet signatures."
        )
    except (
        ExcessiveFeeRateError,
        FileNotFoundError,
        PSBTError,
        TransactionSigningError,
        ValueError,
    ) as exc:
        logger.error(str(exc))
        raise typer.Exit(1) from exc