jmwallet.wallet.utxo_metadata
jmwallet.wallet.utxo_metadata
UTXO and address metadata persistence using BIP-329 wallet labels export format.
Stores UTXO-level metadata (frozen state, labels) and address-level metadata (addresses with on-chain history) in a single JSONL file. Each line is a BIP-329 record. This enables interoperability with external wallets like Sparrow for coin control and labeling.
BIP-329 format (JSON Lines)::
{"type": "output", "ref": "txid:vout", "spendable": false}
{"type": "output", "ref": "txid:vout", "label": "cold storage"}
{"type": "addr", "ref": "<address>", "label": "jm:used:deposit"}
The spendable field maps to frozen state:
- spendable: false -> UTXO is frozen
- spendable: true or absent -> UTXO is spendable (not frozen)
The addr records track which on-chain addresses the wallet has ever held
funds at (including spent-then-empty addresses). This is a privacy-critical
guarantee: once an address has been observed with any UTXO it must never be
reissued as a "next unused" deposit address. Light-client backends (Neutrino)
and Bitcoin Core's address-book-bound RPCs alone cannot give us that
guarantee across restarts; the persistent addr records do.
Label convention (informational, ignored by other BIP-329 consumers):
jm:used[:<origin>] where origin is one of deposit, change,
cj_out, cj_in, send (or a comma-separated combination). The
origin part is best-effort context; the mere presence of the record is
the privacy-relevant fact.
Reference: https://github.com/bitcoin/bips/blob/master/bip-0329.mediawiki
Attributes
DEFAULT_COINJOIN_LOCK_TTL = 600.0
module-attribute
USED_LABEL_PREFIX = 'jm:used'
module-attribute
Classes
AddressRecord
dataclass
A BIP-329 addr record marking an address with on-chain history.
The mere presence of a record means: this address has been observed
holding (or having held) funds and must never be reissued. The label
encodes optional origin context using the jm:used[:origin] convention.
Attributes:
ref: Bitcoin address.
label: jm:used or jm:used:<origin> (deposit, change,
cj_out, cj_in, send).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 | |
Attributes
label: str = USED_LABEL_PREFIX
class-attribute
instance-attribute
origins: set[str]
property
Decode the comma-separated origin set from the label, if any.
ref: str
instance-attribute
Functions
from_dict(d: dict[str, str | bool]) -> AddressRecord | None
classmethod
Deserialize from a BIP-329 JSON dict.
Returns None unless this is a type=addr record bearing our
jm:used label convention; addr records labeled by other tools
(Sparrow user labels etc.) are not treated as used-address markers
and are preserved verbatim by UTXOMetadataStore.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
to_dict() -> dict[str, str]
Serialize to a BIP-329 JSON dict.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
177 178 179 | |
with_added_origin(origin: str | None) -> AddressRecord
Return a copy of this record with origin merged into the label.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
166 167 168 169 170 171 172 173 174 175 | |
OutputRecord
dataclass
A BIP-329 output record for UTXO metadata.
Attributes:
ref: Outpoint string in txid:vout format.
spendable: Whether the UTXO is spendable. False means frozen.
None means no opinion (importing wallet should not alter state).
label: Optional human-readable label.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 | |
Attributes
has_metadata: bool
property
Whether this record carries any state worth persisting.
is_frozen: bool
property
Whether this UTXO is frozen (not spendable).
label: str | None = None
class-attribute
instance-attribute
lock_until: float | None = None
class-attribute
instance-attribute
ref: str
instance-attribute
spendable: bool | None = None
class-attribute
instance-attribute
Functions
from_dict(d: dict[str, str | bool | float]) -> OutputRecord | None
classmethod
Deserialize from a BIP-329 JSON dict.
Returns None if the record is not a valid output record.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
is_locked(now: float) -> bool
Whether this UTXO holds a non-expired temporary CoinJoin lock.
A lock is a time-limited reservation (distinct from a user freeze):
it is set while an input is committed to an in-flight CoinJoin so that
another concurrent round (in this or another process, maker or taker)
does not select the same UTXO and create a conflicting transaction. It
auto-expires after lock_until so a crashed/killed round never blocks
funds forever.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
81 82 83 84 85 86 87 88 89 90 91 | |
to_dict() -> dict[str, str | bool | float]
Serialize to a BIP-329 JSON dict.
jm_lock_until is a JoinMarket extension (other BIP-329 consumers
ignore unknown keys); it carries the temporary CoinJoin lock expiry.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
UTXOMetadataStore
dataclass
In-memory store for UTXO + address metadata backed by a BIP-329 JSONL file.
Thread-safety: This class is NOT thread-safe. If concurrent access is needed, external synchronization must be applied.
Attributes:
path: Path to the JSONL file on disk.
records: Mapping from outpoint (txid:vout) to OutputRecord.
address_records: Mapping from address to AddressRecord (only those
we own with our jm:used label convention).
foreign_addr_lines: Verbatim BIP-329 addr records written by
other tools (Sparrow user labels, etc.). Preserved on save so we
do not silently drop interoperable metadata we did not create.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
Attributes
address_records: dict[str, AddressRecord] = field(default_factory=dict)
class-attribute
instance-attribute
foreign_addr_lines: list[dict[str, str | bool]] = field(default_factory=list)
class-attribute
instance-attribute
path: Path
instance-attribute
records: dict[str, OutputRecord] = field(default_factory=dict)
class-attribute
instance-attribute
Functions
freeze(outpoint: str) -> None
Freeze a UTXO (set spendable to False) and persist.
Args:
outpoint: Outpoint string in txid:vout format.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
368 369 370 371 372 373 374 375 376 377 378 379 | |
get_frozen_outpoints() -> set[str]
Get all frozen outpoints.
Returns: Set of outpoint strings that are frozen.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
360 361 362 363 364 365 366 | |
get_label(outpoint: str) -> str | None
Get the label for an outpoint.
Args:
outpoint: Outpoint string in txid:vout format.
Returns: Label string, or None if no label set.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
551 552 553 554 555 556 557 558 559 560 561 | |
get_locked_outpoints(now: float | None = None) -> set[str]
Return outpoints currently holding a non-expired CoinJoin lock.
Note: reads in-memory state. Call :meth:load first to observe locks
written by other processes.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
468 469 470 471 472 473 474 475 476 | |
get_used_addresses() -> set[str]
Return the set of addresses with on-chain history.
This is the privacy-critical "do not reissue" set, surviving across process restarts and backend swaps.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
623 624 625 626 627 628 629 | |
is_address_used(address: str) -> bool
Return True if address has been recorded as having history.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
619 620 621 | |
is_frozen(outpoint: str) -> bool
Check if an outpoint is frozen.
Args:
outpoint: Outpoint string in txid:vout format.
Returns: True if the UTXO is frozen (spendable is False).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
348 349 350 351 352 353 354 355 356 357 358 | |
load() -> None
Load metadata from disk.
Gracefully handles missing files, empty files, and malformed lines. Lines that cannot be parsed are logged and skipped.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 | |
mark_address_used(address: str, origin: str | None = None) -> bool
Record an address as having on-chain history.
Idempotent. If the address is already recorded, only the origin label
is augmented (best-effort context); the file is rewritten only when
the record actually changes. Returns True if disk state changed.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
mark_addresses_used(addresses: Iterable[str], origin: str | None = None) -> int
Batched variant of :meth:mark_address_used.
Performs a single save() for many addresses; returns the count of
records that were created or had their origin extended.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
release_outpoints(outpoints: Iterable[str]) -> None
Clear CoinJoin locks on outpoints (no-op for unlocked ones).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
save() -> None
Persist all records to disk.
Writes the entire file atomically (write to temp, then rename) to
prevent corruption on crash. output records, our jm:used
addr records, and any foreign records loaded from disk are all
serialized in a deterministic order.
Raises: OSError: If the file cannot be written (e.g., read-only filesystem).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 | |
set_label(outpoint: str, label: str | None) -> None
Set or clear the label for a UTXO and persist.
Args:
outpoint: Outpoint string in txid:vout format.
label: Label string, or None to clear.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
toggle_freeze(outpoint: str) -> bool
Toggle the frozen state of a UTXO and persist.
Args:
outpoint: Outpoint string in txid:vout format.
Returns: True if the UTXO is now frozen, False if now unfrozen.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
try_lock_outpoints(outpoints: Iterable[str], ttl: float = DEFAULT_COINJOIN_LOCK_TTL) -> bool
Atomically lock outpoints for ttl seconds.
Reloads on-disk state under an exclusive file lock so concurrent processes cannot both acquire the same UTXO. Fails (returning False, locking nothing) if any requested outpoint is frozen or already locked by another in-flight round.
Returns: True if all outpoints were locked; False on conflict.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 509 | |
unfreeze(outpoint: str) -> None
Unfreeze a UTXO (set spendable to True) and persist.
If the record has no other metadata (no label), it is removed
entirely since spendable=True is the default.
Args:
outpoint: Outpoint string in txid:vout format.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
verify_writable() -> None
Verify that the metadata file's directory is writable.
Attempts to create and immediately remove a temporary file in the same directory as the metadata file. This catches read-only mounts and permission issues early, before a real save attempt.
Raises: OSError: If the directory is not writable.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
Functions
load_metadata_store(data_dir: Path, fingerprint: str | None = None, owned_addresses: Iterable[str] | None = None) -> UTXOMetadataStore
Create and load a UTXOMetadataStore from the wallet's metadata file.
Args:
data_dir: JoinMarket data directory (e.g., ~/.joinmarket-ng).
fingerprint: Optional 8-char hex wallet fingerprint. When provided,
the per-wallet path wallet_metadata_<fp>.jsonl is used and a
one-shot migration from the legacy shared
wallet_metadata.jsonl is attempted on first open.
owned_addresses: Optional iterable of addresses this wallet derives
inside its scan range. Used to filter addr records during
migration so we do not import another wallet's used-address set
from the shared file. When None no addr records are
imported (safer default than "all"; the wallet's own sync will
re-populate any genuinely-funded addresses).
Returns: Loaded UTXOMetadataStore instance.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | |