did-btcr2-js

ADR 144: DidDocument Keeps Every Property, and Only the KEY Template Sets the #initialKey Relationships

Context

The resolver dropped genesis properties

The specification (Resolve, “If genesis_bytes is a SHA-256 Hash”) says: “Process the Genesis Document provided in sidecar.genesisDocument by replacing the identifier placeholder ("did:btcr2:_") with the did. A simple string replacement is sufficient. Parse the result as JSON to form current_document.”

Resolver.external did the replacement and the parse. Then it returned new DidDocument(currentDocument). The constructor set only nine properties: id, @context, verificationMethod, service, deactivated, and the four verification relationships. toJSON returned only these nine properties. Thus the resolver dropped each other property of the genesis document, for example alsoKnownAs, controller, keyAgreement, or an extension property.

The specification (Data Structures, DID Document) says: “A DID document is a map data structure defined by the DID core v1.1 specification.” Its list of optional properties (“It can optionally include one or more of the following properties”) is not a closed list.

The hash of the resolved document was not the hash of the specification current_document. Example: the genesis document has "alsoKnownAs": ["https://example.com/alice"].

  Resolved document Document hash
Specification has alsoKnownAs hash A
This library no alsoKnownAs hash B

Each update states the hash of the document that it changes (sourceHash). Thus:

  1. A first update from a conformant implementation has sourceHash A. The resolver of this library refused it with INVALID_DID_UPDATE.
  2. A first update from this library has sourceHash B. A conformant resolver refuses it.

The DID itself did not change: the genesis bytes are the hash of the raw genesis document. After an update, the document is a JSON copy (JSONPatch.apply clones it), so a property that an update adds stays. Only the properties of the genesis document were lost.

The constructor added #initialKey relationships

The constructor tested id.includes('k1'). For a k1 id, it set each absent relationship to [<id>#initialKey]. The specification has these four relationships in one place only: the template of “If genesis_bytes is a secp256k1 Public Key”. The constructor also added them to a k1 document after an update removed a relationship, for example through DidDocumentBuilder.build. The hash of that document then changed. The resolver does not construct a DidDocument after a patch, so resolution results were correct.

Decision

1. The constructor keeps every property

The constructor copies each own property of the input to the instance. Then it sets the defaults of verificationMethod, service, and @context as before. toJSON returns every own property of the instance ({ ...this }).

The class fields set the order of the nine named properties. Thus the JSON of a document with no other properties does not change. The other properties come after them.

2. The copy uses Object.defineProperty

JSON.parse makes a "__proto__" key an own property. An assignment this[key] = value with this key replaces the prototype of the instance. Object.defineProperty keeps the property as data, and the instance stays a DidDocument.

3. Only the KEY template sets the #initialKey relationships

The constructor adds no relationship. DidDocument.fromKeyIdentifier sets authentication, assertionMethod, capabilityInvocation, and capabilityDelegation to [<did>#initialKey]. Resolver.deterministic calls fromKeyIdentifier. Thus the code has one KEY template, and the equality of ADR 122 (fromKeyIdentifier equals Resolver.deterministic) holds by construction.

4. deactivated does not change

The copy keeps deactivated as the input holds it. The separate assignment of ADR 142 goes, and its rule stays.

Alternatives

Consequences

Implementation

References