Skip to content

Bitcoin

The escrow facade uses P2WSH sortedmulti with an optional CSV refund branch. The descriptor and policy helpers also support P2SH P2WSH wrapping for lower level integrations.

Supported networks

  • BitcoinNetwork.MAINNET
  • BitcoinNetwork.TESTNET
  • BitcoinNetwork.SIGNET
  • BitcoinNetwork.REGTEST

The network is part of the chain object and every payout and signer must match it.

Build signers

BtcSigner uses an extended public key, master fingerprint, origin path, derive path, and network.

from multisig_escrow import Role, Signer, SignerId
from multisig_escrow.bitcoin import BitcoinNetwork, BtcSigner

buyer_key = BtcSigner.from_xpub(
    xpub=buyer_xpub,
    fingerprint=buyer_fingerprint,
    origin_path="m/48'/0'/0'/2'",
    derive_path="0/0",
    network=BitcoinNetwork.MAINNET,
)

buyer = Signer(SignerId("buyer"), Role.BUYER, buyer_key)

The same pattern applies to the seller and arbiter. A signer key must be a public extended key. Private keys and private extended keys are rejected.

The signer_id field on BtcSigner is optional. The facade copies the SignerId from the enclosing Signer when it builds the Bitcoin policy. This matters for timelock refund signer matching.

Build a payout

from multisig_escrow.bitcoin import BitcoinNetwork, BtcPayout

payout = BtcPayout.from_address(
    "bc1qexampleaddress",
    BitcoinNetwork.MAINNET,
)

BtcPayout validates the address, network, and script pubkey. The same payout object can be used for the buyer and seller, but production terms should use distinct destinations.

Build terms

from datetime import UTC, datetime, timedelta

from multisig_escrow import (
    ConfirmationPolicy,
    EscrowId,
    EscrowTerms,
    Role,
    Signer,
    SignerId,
)
from multisig_escrow.bitcoin import (
    BitcoinChain,
    BitcoinFeePolicy,
    BitcoinNetwork,
    BtcAsset,
    BtcPayout,
)

now = datetime.now(UTC)

terms = EscrowTerms.create(
    escrow_id=EscrowId.generate(),
    chain=BitcoinChain.mainnet(),
    asset=BtcAsset(),
    amount=1_000_000,
    signers=(
        Signer(SignerId("buyer"), Role.BUYER, buyer_key),
        Signer(SignerId("seller"), Role.SELLER, seller_key),
        Signer(SignerId("arbiter"), Role.ARBITER, arbiter_key),
    ),
    threshold=2,
    buyer_payout=BtcPayout.from_address(buyer_address, BitcoinNetwork.MAINNET),
    seller_payout=BtcPayout.from_address(seller_address, BitcoinNetwork.MAINNET),
    funding_deadline=now + timedelta(days=1),
    settlement_deadline=now + timedelta(days=30),
    confirmation_policy=ConfirmationPolicy(3, 6),
    fee_policy=BitcoinFeePolicy(1000, 20_000),
    now=now,
)

Amounts are in satoshis. The threshold must be at least 2 and no greater than the number of signers. release_policy and refund_policy default to the same threshold across all signers. Supply explicit QuorumPolicy values to restrict who can release or refund.

BitcoinFeePolicy bounds the absolute fee and can also bound the fee rate. The settlement builder checks dust limits, fee bounds, and the change output.

Funding target

escrow = BitcoinEscrow.create(terms, store=store)
target = escrow.funding_target()

print(target.address)
print(target.script_pubkey.hex())
print(target.descriptor)
print(target.amount)
print(target.confirmations)

target.descriptor is a checksummed descriptor when the policy has no timelock. A timelock policy returns None because the descriptor is not a plain sortedmulti descriptor.

Funding evidence

from multisig_escrow.bitcoin import BitcoinFundingEvidence

evidence = BitcoinFundingEvidence(
    raw_tx=raw_transaction_bytes,
    txid=txid_bytes,
    output_index=0,
    amount=1_000_000,
    confirmations=3,
    block_height=850_000,
)

escrow.fund(evidence)

The library parses the raw transaction and checks:

  • The transaction ID matches the raw transaction
  • The output index exists
  • The output script matches the funding target script
  • The output amount matches the evidence amount
  • The output amount is within the target amount and tolerance
  • The confirmation count meets the target requirement

It cannot check that the transaction is in the best chain or that the confirmation count is true. Supply evidence from a trusted source.

Release proposal

from multisig_escrow import SignerId
from multisig_escrow.bitcoin import BtcReleaseParams

proposal = escrow.propose_release(
    SignerId("buyer"),
    BtcReleaseParams(utxos=(utxo,), fee=1500),
)

print(proposal.plan.psbt_base64)
print(proposal.summary())

The UTXO must match the funding record, use the escrow script, and meet the funding confirmation policy. The builder creates the payout output and a change output back to the escrow script when needed.

The proposal payload hash covers the unsigned transaction. Approvals are checked against that exact payload.

Approvals

A Bitcoin approval is a PSBT with partial signatures.

from multisig_escrow.bitcoin import PsbtApproval

signed = wallet.sign_psbt(proposal.plan.psbt_base64)
escrow.approve(PsbtApproval(signed))

Each call can include one or more allowed signatures. The library verifies the PSBT against the proposal and checks the signature policy. The escrow moves to release_ready when the policy is satisfied.

The library does not sign. Use a hardware wallet, air gapped signer, or wallet that can display and verify the PSBT before signing.

Finalize

final = escrow.finalize()
assert final.txid is not None
assert final.raw_tx is not None

finalize combines the approved PSBTs, removes signatures that are not backed by approval tokens, finalizes the script witnesses, and verifies the final transaction against the plan. It does not broadcast.

Broadcast and confirmation

from multisig_escrow import BroadcastEvidence

escrow.mark_broadcast(
    BroadcastEvidence(final.txid, final.payload_hash, final.raw_tx)
)
escrow.confirm(final.txid, confirmations=6, block_height=850_010)

mark_broadcast verifies the raw transaction against the proposal and checks the transaction ID. confirm records caller supplied confirmation evidence and requires the settlement confirmation policy before the escrow becomes released.

Timelock refund

A CSV refund branch lets a designated signer recover funds after a relative delay.

from multisig_escrow import RecoveryPolicy, SignerId
from multisig_escrow.bitcoin import CsvDelay, TimelockRefund

timelock = TimelockRefund(
    delay=CsvDelay.blocks(144),
    refund_signer_id=SignerId("buyer"),
    refund_policy=RecoveryPolicy(1, (SignerId("buyer"),)),
)

Pass timelock=timelock to EscrowTerms.create. The refund signer must exist in the signer set. A timelock refund uses BtcRefundParams with SettlementPath.TIMELOCK and a TimelockMaturity that meets the delay.

from multisig_escrow import SettlementPath
from multisig_escrow.bitcoin import BtcRefundParams, TimelockMaturity

proposal = escrow.propose_refund(
    SignerId("buyer"),
    BtcRefundParams(
        utxos=(utxo,),
        fee=1500,
        path=SettlementPath.TIMELOCK,
        maturity=TimelockMaturity(confirmations=144),
    ),
)

A timelock branch is a recovery path, not a substitute for the release policy. The refund transaction is only valid after the CSV delay.

Descriptor utilities

The Bitcoin module exposes descriptor and address helpers:

  • build_multisig_descriptor
  • descriptor_checksum
  • descriptor_address
  • descriptor_script_pubkey
  • parse_descriptor
  • address_to_script
  • script_to_address
  • verify_psbt
  • combine_psbts
  • finalize_psbt
  • verify_final_transaction

Use the facade for normal escrow flows. Use the helpers for integration and inspection.

Operational notes

  • Verify the payout address, amount, fee, network, and change on the signing device.
  • Check that the PSBT input is the expected funding UTXO.
  • Use a confirmation policy appropriate to the value at risk.
  • Do not reuse a UTXO across proposals.
  • Keep the trusted terms hash outside the store.
  • Use a durable store with revision checks.

Next