did-btcr2-js

ADR 171: The DID Document Context Has the DID v1.1 Context as Its First Item on Each Path

Context

The specification

DID Core v1.1, section 6.2.3 “JSON-LD Processors”, says: “In addition to using the JSON representation production rules, production MUST include the @context entry. The serialized value of @context MUST be the JSON String https://www.w3.org/ns/did/v1.1, or a JSON Array where the first item is the JSON String https://www.w3.org/ns/did/v1.1 and the subsequent items are serialized according to the JSON production rules.”

The same section defines the value of @context: “The JSON-LD Context is either a string or a list containing any combination of strings and/or ordered maps.”

DID Core v1.1 also says:

The did:btcr2 specification says:

The check accepted the two required contexts at any position

DidDocument.isValid and GenesisDocument.validateGenesis called the private check isValidContext. ADR 057, decision 1, set its rule: the value is a non-empty array, and it contains each of the two required contexts. ADR 057 rejected the alternative “Also require the DID Core context first.” The reason was that the rule “could reject documents accepted today”.

So the check accepted the two contexts in any order. It also accepted an item of any kind, for example null or 5. The check runs on these paths:

The api had its own copy of the check

assertGenesisDocument in packages/api/src/genesis.ts checks the genesis document of createExternalFromDocument. It had its own @context check, with the message The genesis document "@context" must include "https://www.w3.org/ns/did/v1.1" and "https://btcr2.dev/context/v1". and the data { context }. Then it called GenesisDocument.fromJSON(document).validateGenesis('producer'), and that call ran the method check again.

Examples

Case Before After
["https://www.w3.org/ns/did/v1.1", "https://btcr2.dev/context/v1"] Valid Valid
["https://btcr2.dev/context/v1", "https://www.w3.org/ns/did/v1.1"] Valid Invalid "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1"
["https://www.w3.org/ns/did/v1.1", "https://x.example/v1", "https://btcr2.dev/context/v1"] Valid Valid
["https://www.w3.org/ns/did/v1.1", "https://btcr2.dev/context/v1", null] Valid Invalid "@context": @context[2] must be a string or a JSON object
["https://www.w3.org/ns/did/v1.1", "https://btcr2.dev/context/v1", { "@vocab": "https://x.example/#" }] Valid Valid
["https://www.w3.org/ns/did/v1.1"] Invalid "@context" Invalid "@context": @context must contain "https://btcr2.dev/context/v1"
"https://www.w3.org/ns/did/v1.1" (a string), or no @context Invalid "@context" Invalid "@context": @context must be an array
[] Invalid "@context" Invalid "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1"
A genesis document with the btcr2 context first, in the api createExternalFromDocument Valid Invalid GenesisDocument "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1"
An update with the patch [{ "op": "move", "from": "/@context/0", "path": "/@context/-" }] The resolver applies the update. Updater.construct accepts the patch. The resolver refuses the update with INVALID_DID_UPDATE. The message is Invalid update: the patched document does not conform to DID Core: Invalid "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1". Updater.construct refuses the patch with INVALID_DID_UPDATE. The message is Error validating targetDocument: Invalid "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1".

The first eight rows show the result of DidDocument.isValid. validateGenesis gives the same rule in the message Invalid GenesisDocument "@context": <rule>. In each of these rows, the result is the same for a producer and for a consumer.

Decision

1. The first item of @context is the DID v1.1 context

The private check contextProblem(context) replaces isValidContext. It returns the first rule that the @context value breaks, or undefined. The checks run in this order:

  1. The value is an array. The rule is @context must be an array. An absent @context breaks this rule.
  2. The first item is https://www.w3.org/ns/did/v1.1. The rule is @context[0] must be "https://www.w3.org/ns/did/v1.1". An empty array breaks this rule. This ADR calls this rule the order rule.
  3. Each item is a string or a JSON object (decision 2). The rule is @context[<i>] must be a string or a JSON object, with the index of the first item of a wrong kind.
  4. An item is https://btcr2.dev/context/v1 (decision 3). The rule is @context must contain "https://btcr2.dev/context/v1".

The check accepts only an array. DID Core v1.1 also allows the string https://www.w3.org/ns/did/v1.1. A did:btcr2 document must also contain the btcr2 context, so the string form cannot be a valid did:btcr2 @context.

2. Each item is a string or a JSON object

The DID Core v1.1 definition of @context allows “any combination of strings and/or ordered maps”. This ADR calls this rule the kinds rule. The check uses isJsonMap of src/utils/did-core-conformance.ts for an ordered map: a value of type object that is not null and not an array. So the check refuses null, a number, a boolean, and an array as an item.

The check does not parse the URL of a string item (see the residuals).

3. The btcr2 context can be at any position

The DID Document (data structure) section says “containing” and gives no order. So the check requires only that one item is https://btcr2.dev/context/v1. The array can hold other contexts before or after it, for example a proof suite context (ADR 057, decision 1).

4. The rules apply to both roles

DidDocument.isValid(document, role) and validateGenesis(role) apply the same @context rules for each role.

5. The message names the rule

DidDocument.isValid refuses a bad @context with a DidDocumentError of type INVALID_DID_DOCUMENT. The message is Invalid "@context": <rule>. validateGenesis refuses it with the same error type and the message Invalid GenesisDocument "@context": <rule>. The old message is the start of the new message. The data does not change: it is the document.

6. The api leaves the @context check to the method

assertGenesisDocument no longer has its own @context check. The method check in GenesisDocument.fromJSON and validateGenesis('producer') applies the rules. So createExternalFromDocument refuses a bad @context with the method message, for example Invalid GenesisDocument "@context": @context must be an array. The data is the document, not { context }.

The reason is that one rule must have one definition. Two copies of one rule can become different, and the api copy had no order rule. ADR 163, decision 3, applies the same pattern to the identifier input checks.

The api still checks first that the document is a JSON object with the id did:btcr2:_.

7. The update path needs no new code

In the “Apply update” step, the resolver checks the patched document with DidDocument.validate. It wraps a failure in a ResolveError of type INVALID_DID_UPDATE. Updater.construct checks the target document with DidDocument.isValid(targetDocument, 'producer'). It wraps a failure in an UpdateError of type INVALID_DID_UPDATE.

So the resolver and Updater.construct refuse a patch that moves the DID v1.1 context from the first position. They also refuse a patch that adds an item of a wrong kind. New tests show each of the two refusals for the resolver and for Updater.construct.

8. Each judgment call conforms to the specification texts

Judgment call Text Result
A consumer refuses a document whose first @context item is not the DID v1.1 context (decision 4). DID Core v1.1, section 6.1: “A conforming consumer MUST produce errors when consuming non-conforming DIDs or DID documents.” Resolve, “Apply update”: “Verify that current_document conforms to DID Core v1.1”. Conforms. The serialization breaks a MUST of section 6.2.3. Section 1.4 gives a narrower definition of a conforming DID document (see the alternatives).
A producer refuses the same document (decision 4). DID Core v1.1, section 6.2.3: “The serialized value of @context MUST be the JSON String https://www.w3.org/ns/did/v1.1, or a JSON Array where the first item is the JSON String https://www.w3.org/ns/did/v1.1”. Conformance: “Implementations MUST be conformant to all normative statements in Decentralized Identifiers v1.1”. Conforms.
Each item is a string or a JSON object (decision 2). DID Core v1.1, section 6.2.3: “The JSON-LD Context is either a string or a list containing any combination of strings and/or ordered maps.” Section 1.4: “Everything else in this specification is normative.” Conforms. The definition has no RFC 2119 keyword, but section 1.4 makes it normative.
The btcr2 context can be at any position (decision 3). DID Document (data structure): “@context: A context array containing the following context URLs:” Conforms. The text gives no order.
The check refuses the string form (decision 1). DID Document (data structure): “@context: A context array containing the following context URLs:” Conforms. A string cannot contain the btcr2 context.
The api leaves the check to the method (decision 6). The specification has no text about the location of a check. The specification is silent. The location is a library decision.
The resolver refuses a patch that moves the DID v1.1 context. The error type is INVALID_DID_UPDATE (decision 7). Resolve, “Apply update”: “Otherwise raise INVALID_DID_UPDATE.” Conforms.
Updater.construct refuses the same patch with INVALID_DID_UPDATE (decision 7). Update, “Construct BTCR2 Unsigned Update”: “targetDidDocument MUST be conformant to DID Core v1.1”. Conforms. The step names no error type for this MUST. The error type is a library decision.

Alternatives

Only a producer applies the order rule. Rejected. DID Core v1.1, section 1.4, defines a conforming DID document by sections 4 and 5, and the order rule is in section 6.2.3. This alternative lets a consumer accept another order. But section 6.1 tells a consumer to produce errors for a non-conforming document, and the resolver consumes the serialization. A resolver that accepts another order can also return a document that the media type check of section 6.3.1 (a non-normative section) does not recognize.

Check only the order, and not the kind of each item. Rejected. The DID Core definition of @context gives the kinds rule. The check reads only the kind of each item, so it needs no JSON-LD processor.

Also check the URL syntax of a string item. Rejected. That check needs an IRI parser (RFC 3987). The method has an RFC 3986 parser (src/utils/url-syntax.ts), but an IRI can hold non-ASCII characters that RFC 3986 does not allow. Only a JSON-LD processor fails on a string item that is not a URL.

Require the btcr2 context as the second item. Rejected. The DID Document (data structure) section gives no order. A producer can put another context, for example a proof suite context, before the btcr2 context.

Keep the api check with its own message. Rejected. The api check then needs the same four rules in a second place. The old api copy shows the risk: it had no order rule.

Keep the rule of ADR 057. Rejected. ADR 057 rejected the order rule because it “could reject documents accepted today”. The order rule is a MUST of DID Core v1.1, and the Conformance section of did:btcr2 requires each normative statement of DID Core v1.1. No test vector has a document with another order (see the consequences).

Consequences

These residuals stay:

Superseded.

Unchanged.

Implementation

References