jmcore.crypto
jmcore.crypto
Cryptographic primitives for JoinMarket.
Attributes
NICK_HASH_LENGTH = 10
module-attribute
NICK_MAX_ENCODED = 14
module-attribute
Classes
CryptoError
Bases: Exception
Source code in jmcore/src/jmcore/crypto.py
23 24 | |
KeyPair
Source code in jmcore/src/jmcore/crypto.py
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 | |
Attributes
private_key: PrivateKey
property
public_key: PublicKey
property
Methods:
__init__(private_key: PrivateKey | None = None)
Source code in jmcore/src/jmcore/crypto.py
296 297 298 299 300 | |
public_key_bytes() -> bytes
Source code in jmcore/src/jmcore/crypto.py
320 321 | |
public_key_hex() -> str
Source code in jmcore/src/jmcore/crypto.py
323 324 | |
sign(message: bytes) -> bytes
Sign a message with SHA256 hashing.
Source code in jmcore/src/jmcore/crypto.py
310 311 312 | |
verify(message: bytes, signature: bytes) -> bool
Source code in jmcore/src/jmcore/crypto.py
314 315 316 317 318 | |
NickIdentity
Encapsulates a JoinMarket nick identity with signing capabilities.
Each participant has a nick identity consisting of: - A private key for signing messages - A public key derived from the private key - A nick derived from hash(hex(pubkey))
All private messages must be signed with this key for nick authentication.
Source code in jmcore/src/jmcore/crypto.py
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 | |
Attributes
nick: str
property
The JoinMarket nick (e.g., J5xxx...).
public_key_hex: str
property
Public key as hex string (compressed, 33 bytes).
Methods:
__init__(version: int = 5, private_key_bytes: bytes | None = None)
Create a new nick identity.
Args: version: JoinMarket protocol version (default 5) private_key_bytes: Optional 32-byte private key (random if not provided)
Source code in jmcore/src/jmcore/crypto.py
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 | |
sign_bytes(message: bytes) -> str
Sign raw bytes using the Bitcoin Signed Message algorithm.
Source code in jmcore/src/jmcore/crypto.py
288 289 290 291 292 | |
sign_message(message: str, hostid: str = '') -> str
Sign a message for transmission using Bitcoin message signing format.
Args: message: The message content (without pubkey/sig) hostid: Directory server hostid (appended to message before signing)
Returns:
Signed message string: "
Source code in jmcore/src/jmcore/crypto.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
Functions:
base58_encode(data: bytes) -> str
Encode bytes as Base58 (no checksum). Thin wrapper over the
audited base58 library; returns "" for empty input to match
the historical JoinMarket behavior.
Source code in jmcore/src/jmcore/crypto.py
27 28 29 30 31 32 33 | |
base58check_encode(payload: bytes) -> str
Encode bytes with Base58Check (with double-SHA256 checksum).
Delegates to the audited base58 library.
Source code in jmcore/src/jmcore/crypto.py
36 37 38 39 | |
bitcoin_message_hash(message: str) -> bytes
Hash a message using Bitcoin's message signing format.
Format: SHA256(SHA256("\x18Bitcoin Signed Message:\n" + varint(len) + message))
Source code in jmcore/src/jmcore/crypto.py
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 | |
bitcoin_message_hash_bytes(message: bytes) -> bytes
Hash a raw bytes message using Bitcoin's message signing format.
Format: SHA256(SHA256("\x18Bitcoin Signed Message:\n" + varint(len) + message))
This is the same as bitcoin_message_hash but takes raw bytes instead of a string. Used for certificate messages that contain binary data (pubkeys).
Args: message: Raw message bytes (NOT pre-hashed)
Returns: 32-byte message hash
Source code in jmcore/src/jmcore/crypto.py
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 | |
ecdsa_sign(message: str, private_key_bytes: bytes) -> str
Sign a message with ECDSA using Bitcoin message format.
Args: message: The message to sign (as string) private_key_bytes: 32-byte private key
Returns: Base64-encoded signature
Source code in jmcore/src/jmcore/crypto.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
ecdsa_verify(message: str, signature_b64: str, pubkey_bytes: bytes) -> bool
Verify an ECDSA signature using Bitcoin message format.
Args: message: The signed message (as string) signature_b64: Base64-encoded signature pubkey_bytes: Compressed public key (33 bytes)
Returns: True if signature is valid
Source code in jmcore/src/jmcore/crypto.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
generate_jm_nick(version: int = JM_VERSION) -> str
Source code in jmcore/src/jmcore/crypto.py
93 94 95 96 97 98 99 100 101 102 103 104 105 | |
get_ascii_cert_msg(cert_pub: bytes, cert_expiry: int) -> bytes
Get the ASCII certificate message for signing/verification.
This is an alternative format where the pubkey is hex-encoded: b'fidelity-bond-cert|' + hexlify(cert_pub) + b'|' + str(cert_expiry).encode('ascii')
Args: cert_pub: Certificate public key (33 bytes) cert_expiry: Certificate expiry (encoded as cert_expiry_encoded, NOT multiplied by 2016)
Returns: Message bytes for signing
Source code in jmcore/src/jmcore/crypto.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | |
get_cert_msg(cert_pub: bytes, cert_expiry: int) -> bytes
Get the binary certificate message for signing/verification.
This is the format used by the reference implementation: b'fidelity-bond-cert|' + cert_pub + b'|' + str(cert_expiry).encode('ascii')
Args: cert_pub: Certificate public key (33 bytes) cert_expiry: Certificate expiry (encoded as cert_expiry_encoded, NOT multiplied by 2016)
Returns: Message bytes for signing
Source code in jmcore/src/jmcore/crypto.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
mnemonic_to_seed(mnemonic: str, passphrase: str = '') -> bytes
Convert BIP39 mnemonic to 64-byte seed using PBKDF2-HMAC-SHA512.
Delegates to the audited mnemonic library, which applies the
NFKD Unicode normalization to both the mnemonic and passphrase
before PBKDF2 as required by BIP39. The previous hand-rolled
implementation skipped normalization, so non-ASCII passphrases
derived seeds that did not match the BIP39 spec (and did not match
the legacy joinmarket-clientserver, which also uses this library).
For ASCII passphrases (the overwhelmingly common case) the output
is bit-identical to the previous implementation.
Args: mnemonic: Space-separated mnemonic words passphrase: Optional BIP39 passphrase (default empty)
Returns: 64-byte seed
Source code in jmcore/src/jmcore/crypto.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
nick_from_pubkey_hex(pubkey_hex: str, version: int = JM_VERSION) -> str
Derive the JoinMarket nick a compressed pubkey maps to (J{version}{hash}).
Source code in jmcore/src/jmcore/crypto.py
335 336 337 338 339 340 341 | |
strip_signature_padding(signature: bytes) -> bytes
Strip padding bytes from a DER signature.
The reference JoinMarket implementation pads signatures to 72 bytes using rjust(72, b'\xff'). This function finds the DER header (0x30) and strips any leading padding bytes.
Args: signature: Possibly padded DER signature
Returns: DER signature with padding removed
Source code in jmcore/src/jmcore/crypto.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
validate_bip39_checksum(mnemonic: str) -> bool
Check whether a BIP39 mnemonic has a valid checksum.
The phrase is tested against every language wordlist supported by the
mnemonic library, so valid non-English seed phrases are accepted.
Args: mnemonic: Space-separated mnemonic words
Returns: True if the checksum is valid in any supported language
Source code in jmcore/src/jmcore/crypto.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
verify_bitcoin_message_signature(message: bytes, signature: bytes, pubkey_bytes: bytes) -> bool
Verify an ECDSA signature using Bitcoin message signing format.
The message is hashed using Bitcoin's message signing format: SHA256(SHA256("\x18Bitcoin Signed Message:\n" + varint(len) + message))
Args: message: Raw message bytes (NOT pre-hashed) signature: DER-encoded signature (may have padding) pubkey_bytes: Compressed public key (33 bytes)
Returns: True if signature is valid
Source code in jmcore/src/jmcore/crypto.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
verify_fidelity_bond_proof(proof_base64: str, maker_nick: str, taker_nick: str) -> tuple[bool, dict[str, str | int | bytes] | None, str]
Verify a fidelity bond proof by checking both signatures.
The proof structure (252 bytes total): - 72 bytes: Nick signature (signs "taker_nick|maker_nick" with Bitcoin message format) - 72 bytes: Certificate signature (signs cert message with Bitcoin message format) - 33 bytes: Certificate public key - 2 bytes: Certificate expiry (blocks / 2016) - 33 bytes: UTXO public key - 32 bytes: TXID (display order) - 4 bytes: Vout (little-endian) - 4 bytes: Locktime (little-endian)
The nick signature message format is: (taker_nick + '|' + maker_nick).encode('ascii')
The certificate signature message has two valid formats: 1. Binary: b'fidelity-bond-cert|' + cert_pub + b'|' + str(cert_expiry).encode('ascii') 2. ASCII: b'fidelity-bond-cert|' + hexlify(cert_pub) + b'|' + str(cert_expiry).encode('ascii')
Both signatures use Bitcoin message signing format (double SHA256 with prefix).
Args: proof_base64: Base64-encoded bond proof maker_nick: Maker's JoinMarket nick taker_nick: Taker's nick (the verifier)
Returns: Tuple of (is_valid, bond_data, error_message) bond_data contains parsed fields if successful
Source code in jmcore/src/jmcore/crypto.py
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 | |
verify_raw_ecdsa(message_hash: bytes, signature_der: bytes, pubkey_bytes: bytes) -> bool
Verify an ECDSA signature on a pre-hashed message.
Args: message_hash: The message hash (already SHA256'd) signature_der: DER-encoded signature (may have leading 0xff padding or trailing zeros) pubkey_bytes: Compressed public key (33 bytes)
Returns: True if signature is valid
Source code in jmcore/src/jmcore/crypto.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
verify_signature(public_key_hex: str, message: bytes, signature: bytes) -> bool
Source code in jmcore/src/jmcore/crypto.py
327 328 329 330 331 332 | |
verify_signed_privmsg(from_nick: str, rest: str, hostid: str = 'onion-network') -> tuple[bool, str, str]
Authenticate a relayed private message.
rest is "<command> <data...> <pubkey_hex> <sig_b64>". Returns
(ok, command, data); ok is True only when the appended pubkey hashes
to from_nick and the signature over data + hostid verifies. This binds
the message to its claimed sender so callers must never attribute by substring.
Source code in jmcore/src/jmcore/crypto.py
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 | |