did-btcr2-js

ADR 148: DidDocument Adds No Member, so @context Is Required and verificationMethod and service Are Optional

Context

The specification requires @context and id, and the other members are optional

The specification (Data Structures, DID Document) says: “A DID document is a map data structure defined by the DID core v1.1 specification.” The section then gives two lists:

The section also gives the condition for an updatable document: “In order for this DID document to be updatable, controllers must include at least one verification method with a capability invocation verification relationship and at least one BTCR2 Beacon service.”

The specification (Data Structures, Genesis Document) says: “A Genesis Document is a DID document (data structure) with the identifier set to the placeholder value (did:btcr2:_).”

The specification (Update) says: “Any property in the DID document may be updated except the id. Doing so would invalidate the DID document.”

DID Core v1.1 also makes verificationMethod and service optional. If a document has one of these properties, the value must be a set.

The specification serializes the genesis document with JCS

Specification pull request 387 (“Replace the Genesis Document placeholder in the JCS text”) merged on 2026-10-09. It changes “If genesis_bytes is a SHA-256 Hash” of “Resolve”. ADR 144 quotes the old text. The new text says: “Serialize the Genesis Document, from sidecar.genesisDocument or from CAS, with JCS, and replace every occurrence of did:btcr2:_ in the result with did. Parse the result as JSON to form current_document. The resulting DID Document (data structure) MUST be conformant to DID Core v1.1.”

Resolver.external serializes the genesis document with JSON.stringify, replaces each placeholder with replaceAll, and parses the result.

The constructor wrote three defaults

Before this change, the DidDocument constructor wrote a default for three absent members. It wrote [] for verificationMethod and service, and the two btcr2 contexts for @context. The validation ran after the defaults. This gave three problems:

  1. A genesis document without @context resolved. The resolved document had a member that the genesis document did not have. Thus the hash of the resolved document was not the hash of current_document. The specification requires @context, so the genesis document does not give a conformant DID document.
  2. For a DID document, the constructor checked verificationMethod and service before it wrote the defaults. An absent member failed with Invalid verificationMethod: undefined or Invalid service: undefined. Thus the resolver refused a genesis document without service or without verificationMethod. For a genesis document, the constructor did not run these checks, and it wrote []. Thus GenesisDocument.fromJSON and Identifier.validate accepted the genesis document that the resolver refused. They also changed a null member to [].
  3. Updater.construct and the resolver check a patched document with DidDocument.isValid, which writes no default. Thus both refused an update that removed /service. The specification lets an update change this property.

The genesis checks had a controller rule

validateGenesis required the controller did:btcr2:_ for each verification method. The specification has no such rule. The rule had no ADR. Only the method that signs an update must have the DID as its controller (ADR 147).

The resolver does not run the genesis checks. Thus Identifier.validate and the api createExternalFromDocument refused a genesis document that the resolver accepted. An example is a document with an authentication key of an organization.

validateGenesis also did not read @context, and it did not check the shape of each method and service. A method or a service without type passed. A service whose id was not a string or an array gave a plain TypeError. A service whose id was an array that contains the placeholder passed.

Examples

Input Before After
An x1 genesis document without @context, in the resolver or in the api btcr2.getInitialDocument Resolves. The resolved document has the two btcr2 contexts, so its hash is not the hash of current_document. DidDocumentError INVALID_DID_DOCUMENT: Invalid "@context"
The same genesis document in GenesisDocument.fromJSON or Identifier.validate Valid Invalid GenesisDocument "@context". The detail of the genesisDocument check is Invalid genesis document: Invalid GenesisDocument "@context".
The same genesis document in the api createExternalFromDocument The genesis document "@context" must include "https://www.w3.org/ns/did/v1.1" and "https://btcr2.dev/context/v1". No change
An x1 genesis document without service The resolver and the api createExternalFromDocument: Invalid service: undefined. GenesisDocument.fromJSON and Identifier.validate: valid, and the document gets service: []. Valid on each path. The resolver asks for no beacon signals, and the resolved document has no service.
An x1 genesis document without verificationMethod, whose relationships embed the method The resolver and the api createExternalFromDocument: Invalid verificationMethod: undefined Valid on each path. The resolved document has no verificationMethod.
An update with the patch [{ "op": "remove", "path": "/service" }] Updater.construct: Error validating targetDocument: Invalid "service". The resolver: INVALID_DID_UPDATE, Invalid update: the patched document does not conform to DID Core: Invalid "service" Updater.construct accepts the patch. The resolver applies the update, and the document has no service.
A genesis document with a second method, for authentication only, whose controller is did:example:org Identifier.validate and the api createExternalFromDocument: Invalid GenesisDocument verificationMethod. The resolver accepts the document. Valid on each path
A genesis service whose id is 42, in GenesisDocument.fromJSON or the api createExternalFromDocument A plain TypeError DidDocumentError INVALID_DID_DOCUMENT: Invalid GenesisDocument service. The resolver still accepts the document (decision 5).
A genesis document with service: null GenesisDocument.fromJSON and Identifier.validate: valid, and the member becomes [] Invalid GenesisDocument service
resolveBtcr2SenderPk for an x1 sender whose genesis document has no @context The key of capabilityInvocation[0] undefined
resolveBtcr2SenderPk for an x1 sender whose genesis document has no service undefined The key of capabilityInvocation[0]
resolveBtcr2SenderPk for an x1 sender whose genesis document has no verificationMethod, and capabilityInvocation embeds the method undefined The key of capabilityInvocation[0]

Decision

1. The constructor sets no default member

The constructor copies each own property of the input (ADR 144) and writes no other member. An absent @context, verificationMethod, or service stays absent. The validation then checks the members of the input. DidDocument.fromExternalIdentifier and Resolver.external add no member.

isValidVerificationMethods and isValidServices accept an absent member (undefined). They refuse null and each other value that is not an array of valid items.

Reason: the specification requires @context and makes verificationMethod and service optional. A default changes the document, so the hash of the resolved document was not the hash of current_document. The default also hid an absent @context from the @context check of ADR 057. A null member is not an absent member: DID Core v1.1 requires a set if the property is present.

2. The types follow the specification

In Btcr2DidDocument, ExternalData, and the DidDocument class, @context is required, and verificationMethod and service are optional. The class field @context has no initial value. The class fields still set the key order of ADR 144.

Reason: the types then agree with the checks at run time. For fromExternalIdentifier, the compiler refuses data without @context, and the constructor refuses it at run time. A TypeScript caller must check verificationMethod and service before use, for example with document.service ?? [].

3. Each helper that makes a document writes the two contexts

DidDocument.fromKeyIdentifier and GenesisDocument.create write the two btcr2 contexts. GenesisDocument.fromPublicKey calls GenesisDocument.create. DidDocumentBuilder writes the two contexts only if the initial document has no @context, and it no longer writes verificationMethod: []. Each helper writes a copy of BTCR2_DID_DOCUMENT_CONTEXT.

Reason: these helpers make a new document from parts, so they must write the required member. The copy keeps the exported constant safe. If a caller adds a context to the document, the constant does not change. The generated documents and their hashes do not change.

4. The genesis checks apply the rules of a DID document

validateGenesis runs these checks in this order. The first check that fails throws a DidDocumentError of type INVALID_DID_DOCUMENT:

  1. The id is did:btcr2:_. The message is Invalid GenesisDocument ID.
  2. @context is an array that contains the two btcr2 contexts. The message is Invalid GenesisDocument "@context".
  3. verificationMethod is absent, or an array of methods with a string id, type, and controller. Each method id contains the placeholder. The message is Invalid GenesisDocument verificationMethod.
  4. service is absent, or an array of valid services. Each service id is a string that contains the placeholder. The message is Invalid GenesisDocument service.
  5. The verification relationships have a valid shape.

Steps 3 and 4 use the shape checks of DidDocument.isValid. validateGenesis no longer compares the controller with did:btcr2:_. A method needs a string controller, and the value can be another DID.

Reason: the specification defines a genesis document only by its placeholder id, so a genesis document has the rules of a DID document. Only the method that signs an update must have the DID as its controller, and the update paths check that method (ADR 147). The lock-out guard of ADR 147 still refuses a genesis document if no capabilityInvocation method can sign. The resolver uses the same shape checks for these members. Only the genesis checks also require a string service id (decision 5) and the placeholder in each id (ADR 108).

5. A genesis service id must be a string

Step 4 checks the type of each service id before the placeholder check.

Reason: the service shape check (isDidService) checks only that an id exists. Before, an id that was not a string or an array gave a plain TypeError. An array id ["did:btcr2:_"] passed, because Array.prototype.includes found the placeholder. This change is minimal. The resolver still accepts a service whose id is not a string. A later change of the DID Core checks can apply the string rule on each path.

6. A document without service has no beacon

BeaconUtils.getBeaconServices returns [] if service is absent or is not an array. The resolver then asks for no beacon signals. For such a source document, DidBtcr2.update finds no beacon service. It raises an UpdateError of type INVALID_DID_UPDATE with the message No beacon service found for provided beaconId. Before, both functions threw a plain TypeError for a document without service.

Reason: the specification makes service optional. “Find Beacon Signals” scans the service entries of current_document, and a document without service has no entry to scan.

7. The resolver keeps JSON.stringify for the placeholder step

Resolver.external still serializes the genesis document with JSON.stringify, replaces each did:btcr2:_ with replaceAll, and parses the result. A comment names the step of the specification.

Reason: JCS writes each string and each number as JSON.stringify writes it. Only the member order is different. Outside a string, JSON text holds only structural characters, numbers, true, false, and null, and none of these can contain the placeholder. The placeholder contains no character that JSON escapes. Thus each occurrence is in one string, a member name or a value, and the two serializations give the same occurrences. The parsed value is the same, and so is its JCS hash.

Tests show this result for these inputs:

8. The constructor keeps its three shape checks before the validation

For an id that is not the placeholder, the constructor still checks id, verificationMethod, and service before it copies the input. DidDocument.validate then checks @context first. Thus the first error depends on the path. An example is a document without @context and with service: null:

Path Error
new DidDocument, with a DID as the id Invalid service: null
DidDocument.isValid, with a DID as the id Invalid "@context"
GenesisDocument.fromJSON, with the placeholder as the id Invalid GenesisDocument "@context"

Reason: a new order changes the error messages of the constructor. A later change for DID Core conformance replaces these checks, and that change sets one order.

9. The api does not refuse a document without a beacon service, and its JSDoc gives the condition

After this change, a patch can remove /service, and a genesis document can have no service. A document without a beacon service has no address for an announcement. Nobody can then update or deactivate the DID. This state was already possible before this change. Three examples are a patch that replaces /service with [], a patch that removes the last beacon service, and a genesis document with service: [].

The review of this change recommended a guard in the api. The proposed guard refuses a create or an update after which the document has no beacon service and is not deactivated. The reason was the api guard criterion of ADR 135: the removal of the last beacon is permanent, and the api holds the target document.

The maintainer chose documentation only. The maintainer gave this reason: “the spec does not disallow this. in fact, the spec allows one to update everything in the doc EXCEPT the id field”. The specification (Update) says: “Any property in the DID document may be updated except the id.”

The JSDoc of the api functions gives the condition:

The api README and the cli documentation of genesis, create, and update give the same information. The lock-out guard of ADR 147 does not change.

10. The test vectors do not change

packages/api/lib/data has 241 genesis documents. Each one has @context, verificationMethod, and service, and the controller of each method is did:btcr2:_. The documents of GenesisDocument.fromPublicKey, buildGenesisDocument, and Resolver.deterministic have the same hash as before. Thus each vector gives the same result, and this change regenerates no vector.

Alternatives

Consequences

Superseded.

Unchanged.

Implementation

References