did-btcr2-js

ADR 161: A Beacon Endpoint Is a BIP321 URI: the Scheme Matches in Any Case, the Address Must Decode, and the Read and Write Paths Use One Parse

Context

The read path and the write path read an endpoint in two ways

BeaconUtils.parseBitcoinAddress needed the prefix bitcoin: in lower case. It returned the text before the first ? and did not check that text. BeaconUtils.isBeaconService refused an endpoint without the lower-case prefix. The resolver, the signal discovery, and getBeacons of the api use these two functions.

The write path did not use them. The Fund need of the Updater and the beacon transaction removed the first bitcoin: from the endpoint and kept the rest. Thus one endpoint gave two addresses:

serviceEndpoint of a SingletonBeacon Before: resolve Before: api update with this beaconId After
bitcoin:<addr> signals at <addr> UTXOs of <addr> no change
BITCOIN:<addr> not a beacon: no signals, no error UTXOs of BITCOIN:<addr>, then “unfunded” <addr> on both paths
bitcoin:<addr>?label=beacon signals at <addr> plain Error: Invalid address: contains illegal characters <addr> on both paths
bitcoin:<ADDR> (a bech32 address in upper case) no signals, no error SIGNER_KEY_MISMATCH <addr> in lower case on both paths
bitcoin:<addr>?req-pop=x signals at <addr> plain Error, as above no Beacon Address
bitcoin:?lightning=lnbc1 plain Error: Invalid address: contains illegal characters plain Error no Beacon Address
bitcoin:abc, a bad checksum, a bech32 address in mixed case a request for the text, which Esplora refuses with HTTP 400 not tested no Beacon Address

A beacon with no Beacon Address has no signals, and the resolution continues. The write path refuses its beaconId (decision 5).

A service that is not a beacon passed the factory

DidBtcr2.update found the service of beaconId by its id only. Thus a beaconId of a LinkedDomains service passed. With a bitcoin: endpoint at a funded address and publishToCas: 'always', the api published the signed update to the CAS. Then BeaconFactory.establish threw a MethodError of type INVALID_BEACON_ERROR. A CAS publication is permanent (ADR 150).

The specification reads a Beacon Address from each beacon

“Find Beacon Signals” says: “Scan the service entries in current_document ([DID Document (data structure)]) and identify [BTCR2 Beacons][BTCR2 Beacon] by matching service type to [Beacons Table 1: Beacon Types]. Parse each beacon serviceEndpoint as a [Beacon Address].” A signal of a beacon must meet this condition: “The transaction spends from the [Beacon Address].”

Beacons says: “Resolution of a DID MUST process signals from all [BTCR2 Beacons][BTCR2 Beacon] identified in the [Current DID Document]”. The Beacon Address is “The Bitcoin address of a [BTCR2 Beacon].” The DID Document section says: “A resolver parses only one URL in a DID document. That URL is the serviceEndpoint of a [BTCR2 Beacon] service, and the resolver parses it as a [Beacon Address].” A footnote of the section says: “A [Beacon Address] is a Bitcoin address and has no host.”

The specification cites BIP321 only in “Establish current_document”, for the template of a k DID: “Bitcoin addresses MUST use the Bitcoin URI Scheme”. The section “Announcing to a Singleton Beacon” of Update says: “The [Beacon Signal] is signed by the private key that controls the [Beacon Address] and broadcast to the Bitcoin network.” The specification names no error for an endpoint that gives no Beacon Address.

BIP321 and BIP173

BIP321 defines the URI as bitcoinurn = "bitcoin:" [ bitcoinaddress ] [ "?" bitcoinparams ]. It also says:

BIP173 says: “Encoders MUST always output an all lowercase Bech32 string.” It also says: “Decoders MUST NOT accept strings where some characters are uppercase and some are lowercase (such strings are referred to as mixed case strings).” BIP350 sets one checksum format for each witness version: bech32 for version 0, and bech32m for version 1 to 16.

ADR 086 accepted a case residual

ADR 086 has the section “Accepted residual: beacon addresses are compared as case-sensitive strings”. The resolver compared the beacon address with the lower-case address of the spent output, so a bech32 address in upper case gave no signals. The section accepted the defect on this premise: “every in-repo endpoint generator emits the lowercase form”. The genesis builder of ADR 108 made the premise false: buildGenesisDocument wrote a supplied address as given, also in upper case.

The section gives a fix direction: lower-case a bech32 or bech32m address in parseBitcoinAddress, and keep the case of a base58 address. Its rejected alternative, a comparison of the script of the spent output, is “Strictly stronger”. ADR 086 says: “It remains the preferred long-term shape.”

Decision

1. parseBitcoinAddress reads a BIP321 URI

BeaconUtils.parseBitcoinAddress(uri) does these steps:

  1. The scheme bitcoin: matches in any case.
  2. The address part is the text between the scheme and the first ?.
  3. The address part must be one of the forms of BIP321, with the structure of BIP173 and BIP350 (decision 2).
  4. A query key that starts with req-, in any case, makes the URI invalid. The parse ignores each other query key.

The parse refuses an empty address part, also if the query has a payment instruction. A beacon needs a Bitcoin address. A refusal is a MethodError of type BEACON_SERVICE_ERROR with the data { uri }. The messages are:

2. The address part must decode as an address of BIP321

The parse accepts these forms, and no other text:

The parse does not check the network. A checksum alone is not enough: a WIF secret key and an extended public key also have a valid base58check checksum.

3. A bech32 address becomes lower case

The parse returns a bech32 or bech32m address in lower case. A base58 address keeps its case: base58check is case-sensitive. The decoder refuses a bech32 text in mixed case, so only a text in upper case changes. This is the fix direction of ADR 086.

The decode rule makes the address comparison equal to a script comparison. Esplora and Bitcoin Core give the address of a spent output in one form. A bech32 address is in lower case, and a base58 address is the base58check text of the payload. The parse gives the same form for each address that decodes. Thus an address comparison and a script comparison give the same result for each address of the network of the DID. The signal discovery needs no network argument, and the fullnode map keeps its keys.

4. isBeaconService needs a Beacon Address

BeaconUtils.isBeaconService returns true only if the type is in Beacons Table 1 and parseBitcoinAddress reads the serviceEndpoint. The function does not use this, because getBeaconServices passes it unbound to filter. Thus each caller of getBeaconServices can parse each result with no error.

A beacon whose endpoint gives no Beacon Address is still a beacon of the document, but it has no signals. No transaction spends from a text that is not a Bitcoin address, so the resolver loses no signal. The resolution continues.

The removed-address test of ADR 114 compares parsed addresses. Thus an update that changes only the case of the scheme or of a bech32 address does not remove the beacon address.

5. The write path uses the parse, and the factory refuses a service with no Beacon Address

The Fund need (NeedFunding.beaconAddress) and SinglePartyBeacon.buildSignAndBroadcast get the address from parseBitcoinAddress. The Singleton, CAS, and SMT beacons all use buildSignAndBroadcast.

DidBtcr2.update keeps the message No beacon service found for provided beaconId for an id that names no service. After the id match, it refuses a service that isBeaconService refuses. The refusal is an UpdateError of type INVALID_DID_UPDATE, with this message:

The service <beaconId> is not a beacon service: its type is not in Beacons Table 1, or its serviceEndpoint gives no Bitcoin address.

The check runs before Construct. Thus the api refuses such a beaconId before any I/O, and before any CAS publication. A beaconId that the api derives is always a beacon with a Beacon Address, because the api selects it from getBeacons, which uses getBeaconServices.

6. The genesis builder takes a BIP321 URI

buildGenesisDocument takes a beacon address as a Bitcoin address or as a BIP321 URI. If the input has no scheme, the builder adds bitcoin:. Then it uses parseBitcoinAddress. If the parse refuses the input, the builder refuses it with a DidDocumentError of type INVALID_DID_DOCUMENT. The message is <label>.address "<input>" is not a Bitcoin address or a BIP321 URI of one: <parse message>.

The check of the network and the script kind stays. The document gets bitcoin:<address>, with a bech32 address in lower case.

7. The change removes BeaconUtils.parseBeaconServiceEndpoint

No code called it. It removed the first bitcoin: and kept the query, so it gave a third address for one endpoint.

8. Each judgment call conforms to the specification texts

Judgment call Text Result
The scheme matches in any case on both paths. “Find Beacon Signals”: “Parse each beacon serviceEndpoint as a [Beacon Address].” BIP321: “The scheme component (“bitcoin:”) is case-insensitive, and implementations must accept any combination of uppercase and lowercase letters.” Conforms.
The write path uses only the address part. Update: “The [Beacon Signal] is signed by the private key that controls the [Beacon Address] and broadcast to the Bitcoin network.” Conforms.
An address part that does not decode gives no Beacon Address, and the beacon has no signals. Terminology: “The Bitcoin address of a [BTCR2 Beacon].” “Find Beacon Signals”: “The transaction spends from the [Beacon Address].” BIP321: “The bitcoinaddress body MUST be either a base58 P2SH or P2PKH address, bech32 Segwit version 0 address, bech32m Segwit address, or empty.” The specification names no error. Conforms. The resolution processes zero signals of the beacon, so it meets Beacons: “Resolution of a DID MUST process signals from all [BTCR2 Beacons][BTCR2 Beacon] identified in the [Current DID Document]”.
The parse checks the structure of the address, not only the checksum. BIP321: “The bitcoinaddress body MUST be either a base58 P2SH or P2PKH address, bech32 Segwit version 0 address, bech32m Segwit address, or empty.” Conforms.
A req- query key makes the URI invalid. BIP321: “If a client does not implement handling a query parameter which has a key prefixed with req-, it MUST consider the entire URI invalid.” Conforms.
A bech32 address becomes lower case, and a mixed-case text is not an address. BIP173: “Encoders MUST always output an all lowercase Bech32 string.” and “Decoders MUST NOT accept strings where some characters are uppercase and some are lowercase (such strings are referred to as mixed case strings).” Conforms.
The factory refuses a beaconId of a service with no Beacon Address. Update: “announcing that update via one or more [BTCR2 Beacons][BTCR2 Beacon] listed in the DID document”. “Find Beacon Signals” identifies a beacon by its type and reads its Beacon Address. Conforms.
The genesis builder takes a BIP321 URI and writes bitcoin: with a lower-case bech32 address. “Establish current_document”: “Bitcoin addresses MUST use the Bitcoin URI Scheme”. BIP173: “Encoders MUST always output an all lowercase Bech32 string.” The specification does not define the input of the api. Conforms.

Alternatives

A character rule for the address part, for example /^[0-9A-Za-z]+$/. Rejected. It accepts abc, a bad checksum, a bech32 text in mixed case, and a base58 address in upper case. BIP321 and BIP173 refuse these texts. Such a text goes to the indexer, and Esplora refuses it with HTTP 400.

A checksum check only. Rejected. A WIF secret key and an extended public key pass a base58check checksum. BIP321 permits only P2PKH, P2SH, and segwit addresses.

A beacon with no Beacon Address fails the resolution with INVALID_DID_DOCUMENT. Rejected. One bad service then makes the DID unresolvable. The specification names no error for this case, and the beacon has no signal to lose.

Ignore each query key, also a req- key. Rejected. BIP321 says that the client “MUST consider the entire URI invalid”. The parse uses the scheme rule of BIP321, so it also uses the req- rule of BIP321.

Compare the script of the spent output, not its address (ADR 086). Rejected. The signal discovery then needs the network, and the fullnode map needs scripts as keys. With the decode rule, the address comparison gives the same result for each address of the network (decision 3).

Keep parseBeaconServiceEndpoint, and let it use the parse. Rejected. No code calls it, and one parse needs one entry point.

Keep the match of the factory by id only. Rejected. A service that is not a beacon then fails after a permanent CAS publication.

Consequences

Superseded.

Unchanged.

Implementation

References