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
AUTO_FREEZE_REUSE_LABEL = 'jm:autofrozen:reuse'
module-attribute
DEFAULT_COINJOIN_LOCK_TTL = 600.0
module-attribute
FUNDED_LABEL_PREFIX = 'jm:funded'
module-attribute
RESERVED_LABEL_PREFIX = 'jm:reserved'
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
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 232 233 234 235 236 237 238 239 240 | |
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
Methods:
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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
to_dict() -> dict[str, str]
Serialize to a BIP-329 JSON dict.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
217 218 219 | |
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
206 207 208 209 210 211 212 213 214 215 | |
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
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 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 | |
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
seen: bool = False
class-attribute
instance-attribute
spendable: bool | None = None
class-attribute
instance-attribute
Methods:
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
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 | |
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
112 113 114 115 116 117 118 119 120 121 122 | |
to_dict() -> dict[str, str | bool | float]
Serialize to a BIP-329 JSON dict.
jm_lock_until and jm_seen are JoinMarket extensions (other
BIP-329 consumers ignore unknown keys); they carry the temporary
CoinJoin lock expiry and the observed-outpoint marker respectively.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
ReservedAddressRecord
dataclass
A BIP-329 addr record marking an address the user has set aside.
The presence of a record means: this deposit address was handed out or
manually reserved and must not be reissued as the next unused address.
It carries an optional free-form user_label (e.g. "Alice") for
display. Unlike :class:AddressRecord (jm:used) it does not imply the
address has on-chain history, so the wallet still shows it distinctly
("reserved") rather than as "used-empty".
Attributes: ref: Bitcoin address. user_label: Optional human-readable label; empty string if none.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
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 | |
Attributes
label: str
property
The BIP-329 label string (jm:reserved or jm:reserved:<label>).
ref: str
instance-attribute
user_label: str = ''
class-attribute
instance-attribute
Methods:
from_dict(d: dict[str, str | bool]) -> ReservedAddressRecord | None
classmethod
Deserialize from a BIP-329 addr record bearing jm:reserved.
Returns None for records that are not ours. Everything after the
jm:reserved: prefix is treated as the raw user label (so labels
may contain colons and commas).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
to_dict() -> dict[str, str]
Serialize to a BIP-329 JSON dict.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
269 270 271 | |
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
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 653 654 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 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
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
funded_addresses: set[str] = field(default_factory=set)
class-attribute
instance-attribute
path: Path
instance-attribute
records: dict[str, OutputRecord] = field(default_factory=dict)
class-attribute
instance-attribute
reserved_records: dict[str, ReservedAddressRecord] = field(default_factory=dict)
class-attribute
instance-attribute
Methods:
freeze(outpoint: str, label: str | None = None) -> None
Freeze a UTXO (set spendable to False) and persist.
Args:
outpoint: Outpoint string in txid:vout format.
label: Optional label to attach (only set when the record has no
label yet), e.g. to mark an automatic forced-reuse freeze.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | |
get_address_origins(address: str) -> set[str]
Return the origin tags recorded for address (empty if none).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
799 800 801 802 | |
get_coinjoin_address_types() -> dict[str, str]
Map addresses to a CoinJoin display type from persisted origins.
Import-time label reconstruction (see
WalletService.reconstruct_imported_labels) tags addresses with
cj_out / cj_change origins derived from on-chain analysis of
their creating transaction. This returns those addresses using the
vocabulary the wallet display expects (cj_out and change,
matching get_address_history_types), so imported wallets surface
cj-out / cj-change instead of falling back to deposit /
non-cj-change.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 | |
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
481 482 483 484 485 486 487 | |
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
715 716 717 718 719 720 721 722 723 724 725 | |
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
630 631 632 633 634 635 636 637 638 | |
get_observed_funded_addresses() -> set[str]
Return addresses this wallet has observed funded (across restarts).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
908 909 910 | |
get_reserved_addresses() -> set[str]
Return the set of reserved addresses.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
861 862 863 | |
get_reserved_labels() -> dict[str, str]
Return a mapping of reserved address -> user label (may be empty).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
865 866 867 | |
get_seen_outpoints() -> set[str]
Return outpoints this wallet has observed unspent (across restarts).
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
912 913 914 | |
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
791 792 793 794 795 796 797 | |
has_record(outpoint: str) -> bool
Whether any metadata record exists for outpoint.
Used by the forced-address-reuse auto-freeze to skip UTXOs the wallet already tracks (frozen, labeled, locked, or previously auto-evaluated), so a user's explicit unfreeze of a reuse UTXO is never overridden.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
489 490 491 492 493 494 495 496 | |
is_address_reserved(address: str) -> bool
Return True if address is currently reserved.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
857 858 859 | |
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
787 788 789 | |
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
469 470 471 472 473 474 475 476 477 478 479 | |
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
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 | |
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
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 | |
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
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
record_reuse_observations(funded_addresses: Iterable[str], seen_outpoints: Iterable[str]) -> bool
Persist observed funded addresses and seen outpoints in one write.
Funded addresses become jm:funded addr records; seen outpoints
set a jm_seen flag on the outpoint's output record (without
affecting freeze/label state). Both markers survive restarts so the
forced-address-reuse defense can distinguish a coin that predates a
restart from a genuinely new arrival. Idempotent; returns True if disk
state changed.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 | |
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
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
reserve_address(address: str, label: str = '') -> bool
Mark address as reserved (set aside) with an optional label.
Idempotent: re-reserving with the same label is a no-op. Changing the
label updates the record. Returns True if disk state changed.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 | |
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
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 | |
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
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | |
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
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 | |
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
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
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
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 | |
unreserve_address(address: str) -> bool
Remove any reservation for address. Returns True if changed.
Source code in jmwallet/src/jmwallet/wallet/utxo_metadata.py
847 848 849 850 851 852 853 854 855 | |
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
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
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
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 | |