Bitcoin Message Signing
Two specifications:
- BIP137 (Hourglass) — legacy. Works only for P2PKH (uncompressed
or compressed). Output: 65-byte recoverable ECDSA + Base64.
- BIP322 (modern) — works for any output type (P2PKH, P2WPKH,
P2WSH, P2TR, even script-only). Output: a virtual PSBT-like
signature.
BIP137 (legacy)
prefix = "Bitcoin Signed Message:\n"
preamble = varint(len(prefix)) || prefix || varint(len(msg)) || msg
hash = SHA256d(preamble)
signature = recoverable_ecdsa_sign(privkey, hash) # 65 bytes
encoded = base64(signature)
Verify: recover pubkey from signature, derive P2PKH address, compare.
Limitations:
- Only P2PKH. Cannot prove ownership of a SegWit / Taproot address.
- Wallets don't always agree on the prefix (some include trailing
newline, others don't).
- Recoverable ECDSA implies pubkey isn't directly transmitted.
BIP322 (modern)
Generalizes signing by constructing a virtual transaction:
to_spend = transaction with:
version=0, locktime=0,
vin = [{ prevout: 0xffffffff..., scriptSig: OP_0 PUSH msg_hash, sequence: 0 }],
vout = [{ value: 0, scriptPubKey: <message-target scriptPubKey> }]
to_sign = transaction with:
version=0, locktime=0,
vin = [{ prevout: txid_of_to_spend, vout: 0, sequence: 0 }],
vout = [{ value: 0, scriptPubKey: OP_RETURN }]
Then sign to_sign as a normal transaction spending to_spend's
output. The signature can be:
- A full BIP322 signature with witness data, OR
- A "simple" form where only the witness/scriptSig is encoded.
Address types supported
- P2PKH, P2SH-anything, P2WPKH, P2WSH, P2TR.
- Multi-key: produce multiple sigs across signers and combine into one
BIP322 witness.
- Even script-only addresses (no key) can produce a "proof of
knowledge" of preimage.
Encoded forms
- BIP322 simple — Base64 of a witness stack only. Suitable for
P2WPKH/P2WSH/P2TR.
- BIP322 full — Base64 of
to_sign PSBT-like blob. Required for
P2SH or anything with non-witness data.
Verification flow
def verify_bip322(address, message, signature_b64):
spk = address_to_scriptPubKey(address)
msg_hash = bip322_hash(message)
to_spend = build_to_spend(spk, msg_hash)
to_sign = build_to_sign(to_spend)
apply_signature(to_sign, signature_b64)
return script_executes_successfully(to_sign, vin[0])
Common use cases
- Exchange withdraw verification — "prove you own the destination
before we send".
- Federated/KYC-free login — sign a server-issued nonce with the
Bitcoin address you want to associate with the account.
- Off-chain attestation — sign a public commitment.
- DLC oracle attestation — adapter signatures + BIP322.
Library support
bitcoinjs-lib (since 6.x) — BIP322 sign/verify.
python-bitcoinlib — BIP137 native, BIP322 via plugin.
rust-bitcoin — BIP322 in signed-message crate.
bitcoinj — BIP137; BIP322 incomplete.
Common bugs
- Mixing BIP137 prefix conventions across wallets — verify with the
same library/wallet that signed where possible.
- Trying to BIP137-sign a SegWit address by deriving an arbitrary
P2PKH from the same key → semantically wrong; use BIP322 instead.
- For Taproot: the witness stack must use
SIGHASH_DEFAULT to be
byte-identical to a real Taproot spend; some libraries default to
SIGHASH_ALL (0x01) which encodes a different sighash.
See also
1---2name: bitcoin-message-signing3description: |4 Generic Bitcoin message signing: BIP137 (legacy P2PKH-only) and BIP3225 (modern, supports any output type). Used for proof-of-ownership,6 withdraw verification, federated authentication, exchange KYC-free7 attestation.8 USE WHEN: implementing "sign this message with your Bitcoin address",9 verifying ownership of an address, building auth via signing.10allowed-tools: Read, Grep, Glob11metadata:12 source: https://skillmd.com/skills/claude-dev-suite/bitcoin-message-signing13---1415# Bitcoin Message Signing1617Two specifications:1819- **BIP137** (Hourglass) — legacy. Works only for P2PKH (uncompressed20 or compressed). Output: 65-byte recoverable ECDSA + Base64.21- **BIP322** (modern) — works for any output type (P2PKH, P2WPKH,22 P2WSH, P2TR, even script-only). Output: a virtual PSBT-like23 signature.2425## BIP137 (legacy)2627```28prefix = "Bitcoin Signed Message:\n"29preamble = varint(len(prefix)) || prefix || varint(len(msg)) || msg30hash = SHA256d(preamble)31signature = recoverable_ecdsa_sign(privkey, hash) # 65 bytes32encoded = base64(signature)33```3435Verify: recover pubkey from signature, derive P2PKH address, compare.3637**Limitations**:38- Only P2PKH. Cannot prove ownership of a SegWit / Taproot address.39- Wallets don't always agree on the prefix (some include trailing40 newline, others don't).41- Recoverable ECDSA implies pubkey isn't directly transmitted.4243## BIP322 (modern)4445Generalizes signing by constructing a **virtual transaction**:4647```48to_spend = transaction with:49 version=0, locktime=0,50 vin = [{ prevout: 0xffffffff..., scriptSig: OP_0 PUSH msg_hash, sequence: 0 }],51 vout = [{ value: 0, scriptPubKey: <message-target scriptPubKey> }]5253to_sign = transaction with:54 version=0, locktime=0,55 vin = [{ prevout: txid_of_to_spend, vout: 0, sequence: 0 }],56 vout = [{ value: 0, scriptPubKey: OP_RETURN }]57```5859Then sign `to_sign` as a normal transaction spending `to_spend`'s60output. The signature can be:61- A full BIP322 signature with witness data, OR62- A "simple" form where only the witness/scriptSig is encoded.6364### Address types supported6566- P2PKH, P2SH-anything, P2WPKH, P2WSH, P2TR.67- Multi-key: produce multiple sigs across signers and combine into one68 BIP322 witness.69- Even script-only addresses (no key) can produce a "proof of70 knowledge" of preimage.7172## Encoded forms7374- **BIP322 simple** — Base64 of a witness stack only. Suitable for75 P2WPKH/P2WSH/P2TR.76- **BIP322 full** — Base64 of `to_sign` PSBT-like blob. Required for77 P2SH or anything with non-witness data.7879## Verification flow8081```python82def verify_bip322(address, message, signature_b64):83 spk = address_to_scriptPubKey(address)84 msg_hash = bip322_hash(message)85 to_spend = build_to_spend(spk, msg_hash)86 to_sign = build_to_sign(to_spend)87 apply_signature(to_sign, signature_b64)88 return script_executes_successfully(to_sign, vin[0])89```9091## Common use cases9293- **Exchange withdraw verification** — "prove you own the destination94 before we send".95- **Federated/KYC-free login** — sign a server-issued nonce with the96 Bitcoin address you want to associate with the account.97- **Off-chain attestation** — sign a public commitment.98- **DLC oracle attestation** — adapter signatures + BIP322.99100## Library support101102- `bitcoinjs-lib` (since 6.x) — BIP322 sign/verify.103- `python-bitcoinlib` — BIP137 native, BIP322 via plugin.104- `rust-bitcoin` — BIP322 in `signed-message` crate.105- `bitcoinj` — BIP137; BIP322 incomplete.106107## Common bugs108109- Mixing BIP137 prefix conventions across wallets — verify with the110 same library/wallet that signed where possible.111- Trying to BIP137-sign a SegWit address by deriving an arbitrary112 P2PKH from the same key → semantically wrong; use BIP322 instead.113- For Taproot: the witness stack must use `SIGHASH_DEFAULT` to be114 byte-identical to a real Taproot spend; some libraries default to115 `SIGHASH_ALL` (0x01) which encodes a different sighash.116117## See also118119- [psbt/SKILL.md](../psbt/SKILL.md)120- [../../wallets/hd/SKILL.md](../../wallets/hd/SKILL.md)121- [../../cryptography/schnorr/SKILL.md](../../cryptography/schnorr/SKILL.md)