did-btcr2-js

ADR 149: The DID Document Checks Apply DID Core v1.1, and the Write and Create Paths Also Apply the Rules of a Producer

Context

The specification requires DID Core v1.1 on each path

The specification (Resolve) gives the same requirement for the initial DID document of each identifier type: “The resulting DID Document (data structure) MUST be conformant to DID Core v1.1.” For each update, it says: “Verify that current_document conforms to DID Core v1.1 and that current_document.id equals did. Otherwise raise INVALID_DID_UPDATE.”

The specification (Update) says: “targetDidDocument MUST be conformant to DID Core v1.1.” The Deactivate operation “is the Update operation with a predetermined patch”.

The specification (Data Structures, DID Document) gives one exception for the resolver: “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 resolver MAY treat all other URLs in the document as opaque strings.” Footnote 2 adds: “This rule applies to the resolver, and does not change the requirements of DID Core v1.1 or of a JSON-LD processor.” The same section also says: “Implementations MUST resolve a relative DID URL against the document id before they compare that URL with another reference.”

DID Core v1.1 takes the member rules from Controlled Identifiers v1.0

DID Core v1.1 (section 5) defines the members of a DID document through Controlled Identifiers (CID) v1.0, section 2. These rules apply:

Member Rule
controller (CID 2.1.2) “If present, its value MUST be a string or a set of strings, each of which conforms to the rules in the URL Standard.” DID Core 5.1.2 extends the rule “to include DIDs”.
alsoKnownAs (CID 2.1.3) “If present, its value MUST be a set where each item in the set is a URI conforming to [RFC3986].”
service (CID 2.1.4) “Each service map MUST contain id, type, and serviceEndpoint properties.” The id: “its value MUST be a URL conforming to URL Standard. A conforming document MUST NOT include multiple service entries with the same id.” The type: “Its value MUST be a string or a set of strings.” The serviceEndpoint: “a single string, a single map, or a set composed of one or more strings and/or maps. Each string value MUST be a valid URL conforming to URL Standard.”
verificationMethod (DID Core 5.2) “(a) the id value MUST conform to Section 3.2 DID URL Syntax or Section 3.2.1 Relative DID URLs and (b) the controller value MUST conform to Section 3.1 DID Syntax.”
expires, revoked (CID 2.2) “it MUST be an [XMLSCHEMA11-2] dateTimeStamp string”.
Key material (CID 2.2.1) “A verification method MUST NOT contain multiple verification material properties for the same material. For example, expressing key material in a verification method using both publicKeyJwk and publicKeyMultibase at the same time is prohibited.”
publicKeyMultibase, secretKeyMultibase (CID 2.2.2) “If present, its value MUST be a Multibase encoded value as described in Section 2.4 Multibase.” Section 2.4 gives the alphabets of the headers u and z, and says: “Other Multibase encoding values MAY be used, but interoperability is not guaranteed between implementations using such values.”
publicKeyJwk, secretKeyJwk (CID 2.2.3) “The publicKeyJwk property MUST NOT contain any property marked as “Private” or “Secret” in any registry contained in the JOSE Registries [JOSE-REGISTRIES], including “d”.” For secretKeyJwk: “It MUST NOT be used if the data structure containing it is public or may be revealed to parties other than the legitimate holders of the secret key.”
The verification relationships (CID 2.3) “If present, its value MUST be a set of one or more verification methods. Each verification method MAY be embedded or referenced.”

DID Core v1.1 (section 4) defines a set: “A finite ordered sequence of items that does not contain the same item twice as specified in the Infra Standard.”

DID Core v1.1 separates a producer and a consumer

DID Core v1.1 (section 1.4) defines two roles. “A conforming producer is any algorithm realized as software and/or hardware that generates conforming DIDs or conforming DID Documents”. “A conforming consumer is any algorithm realized as software and/or hardware that consumes conforming DIDs or conforming DID documents”.

Section 6.2 gives each role its rules. For production, a set is “A JSON Array, where each element of the set is added, in order, as a value of the array”. For consumption, a JSON Array of a set gives “A set, where each value of the JSON Array is added to the set in order”. The Infra Standard says: “To append to an ordered set: if the set contains the given item, then do nothing; otherwise, perform the normal list append operation.”

Thus a producer writes each item of a set one time. A consumer that reads the same item two times gets a set with that item one time.

The checks before this change

DidDocument.isValid checked @context, the id, and the shape of verificationMethod, service, and four verification relationships. A method needed a string id, type, and controller (ADR 147). No check read the syntax of a URL, the key material, the dates, the items of a set, keyAgreement, or two services with one id. A relationship with no item passed.

For a DID as the id, the constructor ran three checks before the copy, so the first error depended on the path (ADR 148, decision 8).

The genesis checks required the placeholder did:btcr2:_ in each method id and each service id (ADR 108). Identifier.validate ran these checks, and the resolver did not. Thus Identifier.validate refused a genesis document that the resolver accepted.

Updater.construct read targetDocument.id before it checked that the target is a JSON object. A patch that replaces the whole document with null gave a plain TypeError.

Examples

Input Before After
A method whose controller is did:example:a#key-1, on each path Valid Invalid "verificationMethod": verificationMethod[1].controller must be a DID
A method whose publicKeyJwk has the member d, on each path Valid Invalid "verificationMethod": verificationMethod[1].publicKeyJwk.d must not be present: a public key has no private member
authentication: [], on each path Valid Invalid "authentication": authentication must be an array of one or more items
Two services with the ids #links and <did>#links, on each path Valid Invalid "service": service[1].id is the id of service[0]: each service needs a different id
authentication holds one reference two times Valid on each path The resolver accepts the document. Updater.construct: Error validating targetDocument: Invalid "authentication": authentication[1] repeats authentication[0]: a set contains each item one time
A method with a valid secretKeyMultibase Valid on each path The resolver accepts the document. The write path and the create path refuse it: verificationMethod[0].secretKeyMultibase must not be present: a DID document is public
alsoKnownAs: ["a.example"] Valid on each path The resolver accepts the document. The write path and the create path refuse it: alsoKnownAs[0] must be a URI
A genesis document with a service whose id is https://example.com/svc Identifier.validate: Invalid GenesisDocument service. The resolver accepts the document. Identifier.validate: valid. createExternalFromDocument: Invalid GenesisDocument service: service[1].id must contain "did:btcr2:_"
A DID document without @context and with service: null, in new DidDocument Invalid service: null Invalid "@context", on each path
A patch that replaces the whole document with null, in Updater.construct A plain TypeError Error validating targetDocument: Invalid DID document: the document must be a JSON object

Decision

1. One internal module holds the member rules

The internal module src/utils/did-core-conformance.ts holds the function didCoreProblem(document, role). The package does not export the module (ADR 132). The function returns the first rule that a member breaks, as { member, message }, or undefined. The checks run in the order of DID Core section 5: controller, alsoKnownAs, verificationMethod, the five verification relationships, and service. A producer then checks the ids that two items share. Each message starts with the path of the value, for example service[1].id.

DidDocument.isValid(document, role) runs these checks in this order. The first check that fails throws a DidDocumentError of type INVALID_DID_DOCUMENT:

  1. The document is a JSON object. The message is Invalid DID document: the document must be a JSON object.
  2. @context contains the two btcr2 contexts (ADR 057, decision 1). The message is Invalid "@context".
  3. The id is a did:btcr2 identifier. The message is Invalid "id".
  4. didCoreProblem. The message is Invalid "<member>": <rule>.

validateGenesis(role) runs these checks in this order:

  1. The id is the placeholder. The message is Invalid GenesisDocument ID.
  2. @context contains the two btcr2 contexts. The message is Invalid GenesisDocument "@context".
  3. didCoreProblem. The message is Invalid GenesisDocument <member>: <rule>.
  4. A producer only: each method id and each service id contains the placeholder (ADR 108). The message is Invalid GenesisDocument <member>: <member>[<index>].id must contain "did:btcr2:_".

This change removes the private checks isValidVerificationMethods and isValidServices, and the public static isValidVerificationRelationships. The shape check of a service no longer uses isDidService of @web5/dids.

Reason: each path uses one rule set, and the role selects the extra rules. A message with the path of the value shows the caller which item to correct.

2. The read path is a consumer, and the write path and the create path are a producer

The type ConformanceRole is 'consumer' | 'producer'. The terms are the terms of DID Core section 6. The default role is consumer.

These call sites use the role producer:

The other call sites use the role consumer: the resolver (the initial DID document and each update), the DidDocument constructor, GenesisDocument.fromJSON, DidDocument.fromExternalIdentifier, and Identifier.validate.

Reason: the write path and the create path generate the DID document, so they are producers. The resolver reads the DID document, so it is a consumer. DID Core section 6 gives each role its own rules for a set. The specification lets a resolver treat a URL as an opaque string.

3. The rules on each path

Each path applies these rules. An absent member passes. A present member must have the value of its rule, so null fails.

  1. controller is a string or an array of strings.
  2. alsoKnownAs is an array of strings.
  3. verificationMethod is an array of maps. Each map has a string id, type, and controller.
  4. The id of a method follows the reference rule of decision 5. The controller of a method is a DID (DID Core 5.2(b)).
  5. The key material and the dates follow decision 6.
  6. Each of the five verification relationships, keyAgreement included, is an array of one or more items. An item is a string that follows the reference rule, or an embedded map with the rules of items 3 to 5.
  7. service is an array of maps. Each map has a string id and a type that is a string or an array of strings. Its serviceEndpoint is a string, a map, or an array of one or more strings and maps.
  8. No two services have the same id after a relative id resolves against the document id (CID 2.1.4).

An empty array is valid for controller, alsoKnownAs, verificationMethod, service, and the type of a service. CID defines these values as “a set” with no minimum size. Only the verification relationships and an array serviceEndpoint need “one or more” items.

4. The rules of a producer

The write path and the create path also apply these rules:

  1. A set does not contain the same item two times. Two maps are the same item if their JCS text is the same. Two references are the same item if they give the same absolute DID URL. The rule applies to controller, alsoKnownAs, verificationMethod, the verification relationships, the type of a service, and an array serviceEndpoint.
  2. A top-level controller item is a DID or a URL. An item with the did scheme must be a DID, so it cannot have a path, a query, or a fragment (DID Core 5.1.3).
  3. An alsoKnownAs item is an RFC 3986 URI.
  4. A service id follows the reference rule of decision 5.
  5. A serviceEndpoint string is a URL.
  6. A method has no secretKeyMultibase.
  7. No two items of verificationMethod, the verification relationships, and service have the same id after a relative id resolves against the document id. An embedded method counts.

The message of rule 1 is <path>[<index>] repeats <path>[<first>]: a set contains each item one time. The message of rule 7 is <path>.id is the id of <first path>: each verification method and each service needs a different id.

Reason: a producer generates the DID document, so it must write a conformant document. The specification lets only the resolver treat a URL as opaque.

5. The syntax checks use the grammar that each rule names

The internal module src/utils/url-syntax.ts holds the syntax checks:

A method id and a relationship reference follow this reference rule:

  1. A value with the did scheme must be a DID URL. The message is <path> must be a DID URL: the value has the "did" scheme.
  2. A value with no scheme is a relative DID URL. It must be an RFC 3986 relative reference. The message is <path> must be a relative DID URL: an RFC 3986 relative reference. A consumer does not check a relative reference that starts with //.
  3. A producer checks that a value with another scheme is a URL. The message is <path> must be a URL. A consumer treats such a value as opaque.

Reason: DID Core 5.2 requires a DID URL or a relative DID URL for a method id. A value with the did scheme has the syntax of the did URI scheme, so it is not a relative reference (RFC 3986, section 4.2). A relative reference with an authority (//) contains a host. The specification lets a resolver skip the parse of a host (Data Structures, footnote 1). The URL Standard parser accepts a non-ASCII host, and CID names the URL Standard for controller, a service id, and serviceEndpoint. CID names RFC 3986 for alsoKnownAs, so an alsoKnownAs item with a non-ASCII host fails.

6. The checks of the key material and the dates

These checks apply on each path to each method in verificationMethod and to each embedded method. They do not depend on the method type:

  1. A method does not have both publicKeyMultibase and publicKeyJwk (CID 2.2.1). A public key and a secret key are different material, so publicKeyMultibase with secretKeyMultibase passes this check.
  2. publicKeyMultibase and secretKeyMultibase start with a Multibase header. The headers are the 25 headers with an encoding in the multibase registry, at commit 72573756 of multibase.csv. After a u header or a z header, each character must be in the alphabet of that header. CID defines no alphabet for the other headers, so the check stops at the header.
  3. publicKeyJwk is a map with a string kty (RFC 7517, section 4.1). It has no member of the “Private” class of the IANA JSON Web Key Parameters registry: d, p, q, dp, dq, qi, oth, k, and priv (read on 2026-10-19).
  4. A method has no secretKeyJwk. A DID document is public, so CID 2.2.3 does not let it hold this property.
  5. expires and revoked are XML Schema 1.1 dateTimeStamp strings: a date and a time with a timezone. The new internal function isDateTimeStamp makes this check. The resolver uses it also for the created and expires values of a proof.

The checks do not compare a Multikey header with the method type, and they do not decode a key. CID 2.2.2 says: “Implementations that adhere to this specification will raise errors in the event of a Multikey header value that is not in the public key header table above”. This change does not apply that sentence. Only the method that signs an update must be a BIP340 Multikey, and the update paths decode its key (ADR 147).

7. The constructor and Updater.construct check the root value first

The DidDocument constructor checks only that its input is a JSON object. Then it copies the input (ADR 144) and runs DidDocument.isValid or validateGenesis with the role consumer. The three checks before the copy are removed. Thus one input gives the same first error on each path.

Updater.construct checks that the target document is a JSON object before it compares the id of the target with the id of the source.

The genesis checks now run in the order of decision 1. ADR 148 (decision 4) checked verificationMethod and its placeholders before service, and the verification relationships last. Now the checks follow DID Core section 5, and the placeholder checks run last. The specification gives no order for these checks.

Reason: ADR 148 (decision 8) kept the three checks until a change for DID Core conformance set one order. This is that change.

8. The check command applies the rules of the resolver

Identifier.validate with options.genesisDocument calls GenesisDocument.fromJSON, so it applies the rules of a consumer. The api did.validate and the cli identifier validate --genesis-document use it. The check no longer requires the placeholder in each method id and service id. The create path keeps the placeholder rule of ADR 108.

Reason: the check reports if a genesis document gives a DID document that resolves. The resolver does not apply the genesis checks of a producer. Before, the check refused a genesis document that the resolver accepted.

9. Each judgment call conforms to the specification texts

Judgment call Text Result
Only a producer refuses two items with the same id (methods, embedded methods, services). The service rule applies on each path. CID 3.4, note: “doing so is to be avoided and the behaviour is not defined.” CID 2.1.4: “A conforming document MUST NOT include multiple service entries with the same id.” Conforms. The note gives no requirement for a consumer. The service text is a MUST, so each path applies it.
Only a producer refuses secretKeyMultibase. CID 2.2.2: “The secretKeyMultibase property is OPTIONAL.” Also: “Developers are advised to not accidentally publish a representation of a secret key.” Conforms. The text has no MUST NOT for a consumer. A producer publishes the document, so it refuses the secret key.
A consumer accepts an item that occurs two times in a set. A producer refuses it. DID Core 6.2.2 and the Infra Standard (above). Conforms. The consumption rule adds each item to the set, and the set ignores an item that it contains.
An empty array is valid for controller, alsoKnownAs, verificationMethod, service, and a service type. CID 2.1.2, 2.1.3, and 2.1.4 say “a set”. Only the verification relationships and serviceEndpoint say “one or more”. Conforms.
The genesis checks follow DID Core section 5, and the placeholder checks run last. The specification gives no order. Conforms.
The read path checks the DID syntax of a method controller, and the DID URL syntax of a method id and a relationship reference. DID Core 5.2 (a) and (b) are MUST rules. Resolve requires DID Core v1.1. Data Structures requires a resolver to resolve a relative DID URL before a comparison, so the resolver reads these values. Conforms. The MAY of Data Structures does not cover a value that the resolver resolves and compares.
For a Multibase header other than u and z, the check stops at the header. CID 2.4: “Other Multibase encoding values MAY be used”. CID defines only the alphabets of u and z. Conforms.

The resolver does not treat a service whose type is an array that contains SingletonBeacon as a beacon. The specification (Beacons) says: “The type of a service defining a Singleton Beacon in a DID document is "SingletonBeacon".” Resolve matches the service type to Beacons Table 1. This change keeps that behavior.

10. The test vectors do not change

The 413 DID documents and the 241 genesis documents in packages/api/lib/data pass the rules of a consumer. Four genesis documents of one scenario (one on each network) have a relative method id, so they fail the placeholder rule of a producer. They failed GenesisDocument.fromJSON before this change too, and the resolver did not apply that rule then or now.

The offline vector check (lib/verify-scenarios.ts) gives the same failures as before this change. Each failure comes from the proof.invocationTarget check of ADR 147 or from an old error code in one vector. No failure comes from the DID Core checks.

The vector rule document-not-conformant matches the message by its prefix, Invalid update: the patched document does not conform. The new text after the prefix does not change the match. Its recipe sets the type of #initialKey to the number 42, and that document still breaks the rule. The message now ends with verificationMethod[0] must be a map with a string id, type, and controller.

Alternatives

Consequences

Superseded.

Unchanged.

Implementation

References