BIP: 322 Layer: Applications Title: Generic Signed Message Format Author: Karl-Johan Alm <[email protected]> Comments-Summary: No comments yet. Comments-URI: https://github.com/bitcoin/bips/wiki/Comments:BIP-0322 Status: Draft Type: Standards Track Created: 2018-09-10 License: CC0-1.0
A standard for interoperable generic signed messages based on the Bitcoin Script format.
The current message signing standard only works for P2PKH (1...) addresses. By extending it to use a Bitcoin Script based approach, it could be made more generic without causing a too big burden on implementers, who most likely have access to Bitcoin Script interpreters already.
A new structure SignatureProof
is added, which is a simple serializable scriptSig & witness container.
Two actions "Sign" and "Verify" are defined along with one purpose, "SignMessage", with the ability to expand in the future to add a potential "ProveFunds" purpose.
Type | Length | Name | Comment |
---|---|---|---|
Uint32 | 4 | flags | standard flags (1-to-1 with standard flags in Bitcoin Core) |
Uint8 | 1 | entries | number of proof entries[1] |
The above is followed by [entries] number of signature entries:
Type | Length | Name | Comment |
---|---|---|---|
VarInt | 1-8 | scriptsiglen | Number of bytes in scriptSig data |
Uint8* | [scriptsiglen] | scriptsig | ScriptSig data |
VarInt | 1-8 | witlen | Number of entries in witness stack |
Uint8[]* | [witlen] | wit | Witness stack, as [witlen] uint8* vectors, each one prepended with a varint of its size |
In some cases, the scriptsig or wit may be empty. If both are empty, the proof is incomplete.
A verification call will return a result code according to the table below.
Code | Description |
---|---|
INCOMPLETE | One or several of the given challenges had an empty proof. The prover may need some other entity to complete the proof. |
INCONCLUSIVE | One or several of the given proofs used unknown opcodes or the scriptPubKey had an unknown witness version, perhaps due to the verifying node being outdated. |
VALID | All proofs were deemed valid. |
INVALID | One or more of the given proofs were invalid |
ERROR | An error was encountered |
If the challenge consists of a single address and the address is in the P2PK(H) (legacy) format, sign using the legacy format (further information below). Otherwise continue as stated below.
Let there be an empty set inputs
which is populated and tested at each call to one of the actions below.
The "SignMessage" purpose generates a sighash based on a scriptPubKey and a message. It emits a VALID verification result code unless otherwise stated.
- Return INVALID if scriptPubKey already exists in
inputs
set, otherwise insert it[2] - Define the message pre-image as the sequence "Bitcoin Message:" concatenated with the message, encoded in UTF-8 using Normalization Form Compatibility Decomposition (NFKD)
- Let sighash = sha256(sha256(scriptPubKey || pre-image))
The "Sign" action takes as input a purpose. It returns a signature or fails.
- Obtain the sighash and scriptPubKey from the purpose; FAIL if not VALID
- Derive the private key privkey for the scriptPubKey; FAIL if not VALID
- Generate and return a signature sig with privkey=privkey, sighash=sighash
The "Verify" action takes as input a standard flags value, a script sig, an optional witness, and a purpose. It emits one of INCONCLUSIVE, VALID, INVALID, or ERROR.
- Obtain the sighash and scriptPubKey from the purpose; pass on result code if not VALID
- If one or more of the standard flags are unknown, return INCONCLUSIVE
- Verify Script with flags=standard flags, scriptSig=script sig, scriptPubKey=scriptPubKey, witness=witness, and sighash=sighash
- Return VALID if verify succeeds, otherwise return INVALID
When more than one proof is created or verified, repeat the operation for each proof, retaining the inputs set. As noted, if the same input appears more than once, the operation must fail accordingly.
Note that the order of the entries in the proof must match the order of the entries given by the verifier.
- If any of the proofs are empty during a verification process, skip the verification and set the INCOMPLETE flag
- If a verification call returns ERROR or INVALID, return ERROR or INVALID immediately, ignoring as yet unverified entries
- After all verifications complete,
- return INCONCLUSIVE if any verification call returned INCONCLUSIVE
- return INCOMPLETE if the INCOMPLETE flag is set
- return VALID
The legacy format is restricted to the legacy P2PK(H) address format, and restricted to one single challenge (address).
Any other input (e.g. multiple addresses, or non-P2PK(H) address format(s)) must be signed using the new format described above.
Given the P2PK(H) address a
and the message m
:
- let
p
be the pubkey-hash contained ina
- let
x
be the private key associated withp
- let
digest
beSHA56d("Bitcoin Signed Message:\n"||m)
- create a compact signature
sig
(aka "recoverable ECDSA signature") usingx
ondigest
sig
, serialized using the base64 encoding.
Given the P2PK(H) address a
, the message m
, and the compact signature sig
:
- let
p
be the pubkey-hash contained ina
- let
digest
beSHA56d("Bitcoin Signed Message:\n"||m)
- attempt pubkey recovery for
digest
using the signaturesig
and store the resulting pubkey intoQ
- fail verification if pubkey recovery above fails
- let
q
be the pubkey-hash ofQ
- if
p == q
, the proof is valid, otherwise it is invalid
This specification is backwards compatible with the legacy signmessage/verifymessage specification through the special case as described above.
- ^ Why support multiple proofs? It is non-trivial to check a large number of individual proofs for duplicates. Software could be written to do so, but it seems more efficient to build this check into the specification itself.
- ^ Why track duplicates? Because a 3-entry proof is not proving 3 entries unless they are all distinct
- Pull request to Bitcoin Core: bitcoin/bitcoin#16440
Thanks to David Harding, Jim Posen, Kalle Rosenbaum, Pieter Wuille, and many others for their feedback on the specification.
- Original mailing list thread: https://lists.linuxfoundation.org/pipermail/bitcoin-dev/2018-March/015818.html
This document is licensed under the Creative Commons CC0 1.0 Universal license.