did-btcr2-js

@did-btcr2/method

TypeScript reference implementation of the did:btcr2 DID Method: a censorship-resistant Decentralized Identifier method using Bitcoin as a verifiable data registry.

This package is the core of the did-btcr2-js monorepo. It implements DID create/resolve/update operations and the three beacon types (Singleton, CAS, SMT). Multi-party aggregation over MuSig2 and the pluggable peer-to-peer transport layer live in the companion @did-btcr2/aggregation package, which builds on this one.

Installation

pnpm add @did-btcr2/method

What’s in the Box

Feature Entry point
Create a DID (offline, deterministic or external) DidBtcr2.create()
Resolve a DID (sans-I/O state machine) DidBtcr2.resolve()
Update a DID (sans-I/O state machine) DidBtcr2.update() returns an Updater
Deactivate a DID (the update with the fixed patch) DidBtcr2.deactivate() returns an Updater; DEACTIVATION_PATCH
Update utilities (construct, signing method, sign, announce) Updater.construct(), Updater.signingMethod(), Updater.sign(), Updater.announce()
Beacon types (Singleton, CAS, SMT) SingletonBeacon, CASBeacon, SMTBeacon
Fee estimation (pluggable) FeeEstimator, StaticFeeEstimator
Signer abstraction (local key, KMS, custom) Signer / LocalSigner from @did-btcr2/keys; KeyManagerSigner from @did-btcr2/key-manager
DID document types and builders Btcr2DidDocument, DidDocumentBuilder
Multi-party aggregation (MuSig2) and transport @did-btcr2/aggregation (separate package)

Quick Start

Create a DID

import { DidBtcr2 } from '@did-btcr2/method';
import { SchnorrKeyPair } from '@did-btcr2/keys';

// Deterministic (k-type): the identifier IS the public key
const keys = SchnorrKeyPair.generate();
const did = DidBtcr2.create(keys.publicKey.compressed, {
  idType  : 'KEY',
  network : 'mutinynet',
});
// did:btcr2:k1q5p...

DidBtcr2.create applies only the encoding algorithm of the specification. Identifier.encode checks each input and sets the defaults of version and network. Each input fault gives an IdentifierError of type INVALID_DID (ADR 163):

Resolve a DID

DidBtcr2.resolve() returns a sans-I/O state machine. The caller drives resolution by fulfilling typed data needs (beacon signals, CAS announcements, signed updates).

import { DidBtcr2 } from '@did-btcr2/method';

const resolver = DidBtcr2.resolve(did, { sidecar });
let state = resolver.resolve();

while (state.status === 'action-required') {
  for (const need of state.needs) {
    const data = await fetchData(need);  // your I/O goes here
    resolver.provide(need, data);
  }
  state = resolver.resolve();
}

const { didDocument, metadata } = state.result;

See src/core/resolver.ts for the full DataNeed union and phase transitions.

Resolution processes a beacon signal only if its transaction has at least minConf confirmations. The default is 6 (DEFAULT_MIN_CONF), the value the specification mandates. A signal below the threshold is excluded as if it did not exist yet; the other signals are processed. Indexer signal discovery skips a mempool transaction before the threshold applies.

Pass { minConf: 1 } to see a fresh update after one block, at a higher exposure to a block reorganization. metadata.confirmations reports the depth of the signal of the resolved version. A minConf that is not a positive integer fails with a ResolveError of type INVALID_OPTIONS.

The metadata of the result is the DID document metadata of the specification. versionId, confirmations, and deactivated are always present. confirmations is 0 and updated is absent if the resolved version is 1. Otherwise, confirmations and updated report the block of the update that yields the resolved version. updated is the mediantime of that block. A duplicate re-announcement does not change them.

An identifier that does not conform to the DID syntax fails with an IdentifierError of type INVALID_DID. A valid DID of another method, for example did:example:a:b, fails with the type METHOD_NOT_SUPPORTED (ADR 157). A did:btcr2 identifier that does not decode fails with INVALID_DID, also if the Bech32m decoder rejects the body. A sidecar genesis document whose hash is not the genesis bytes of the identifier fails with a ResolveError of type INVALID_DID.

Identifier.decode returns network as a name of BitcoinNetworkNames, so BitcoinNetworkNames[network] gives the network value of the identifier (ADR 163).

Each hash step of the resolver uses JCS (RFC 8785). A value with no JCS form, for example a value that holds NaN, Infinity, or a lone surrogate, has no hash (ADR 154). The resolver handles such a value as follows:

The data.cause of each of these errors gives the type and the message of the CanonicalizationError. On the write path, Updater.construct and Updater.sign refuse a source document or a patch with no JCS form with an UpdateError of type INVALID_DID_UPDATE.

The resolver checks retrieved data in the order of the specification (ADR 160). provide checks the hash first, with the codes above. Then it checks that the value is a JSON object. A signed update or a CAS Announcement with the right hash that is not a JSON object is not available: MISSING_UPDATE_DATA. The sidecar data and provide give one result for one input:

A genesis document from the sidecar or from the CAS gives the initial DID document. The resolver replaces each did:btcr2:_ in the genesis document with the DID, and it adds no member (ADR 148). Then @context must be an array, and its first item must be https://www.w3.org/ns/did/v1.1. Each item must be a string or a JSON object, and one item must be https://btcr2.dev/context/v1 (ADR 171). If a genesis document has no @context, the message is Invalid "@context": @context must be an array. If the btcr2 context is the first item, the message is Invalid "@context": @context[0] must be "https://www.w3.org/ns/did/v1.1".

The members of the initial DID document must conform to DID Core v1.1 with the rules of a consumer (ADR 149). For example, each verification method needs a string id and type and a DID as its controller. The key material of each method must have a valid form. On the read path, the check does not read the syntax of a top-level controller item, an alsoKnownAs item, a service id, or a serviceEndpoint. A failed check of @context or of a member gives a DidDocumentError of type INVALID_DID_DOCUMENT. A genesis document without service gives a DID document without service, and the resolver asks for no beacon signal.

Two resolution options select a version. The two options are mutually exclusive. A request with both, or with a value that does not parse, fails with INVALID_OPTIONS before any data need. The resolver processes the updates in version order. Each option selects the version as follows:

After the resolver keeps the state, it processes the rest of the history. Then it returns the kept state (ADR 145). Thus an error in a later update fails a historical request, as it fails a request for the current version. Examples are LATE_PUBLISHING for a second, different update with the same version, and INVALID_DID_UPDATE for an invalid later update. The resolver also emits the data needs of each later update, for example NeedBeaconSignals for a beacon that a later update adds. An opt-in maxDiscoveryRounds counts the discovery rounds of the full history.

DidBtcr2.resolve checks the resolution options after the DID and before it returns the Resolver, in the order of DID Resolution v1, section 4.4 (ADR 157). An option with the value undefined is absent.

The resolver processes one update per pass, as the specification describes. After each applied update it scans the beacon addresses that the document now carries and that it did not scan yet, then it continues with the next update. The resolver ignores a signal at a beacon address that an applied update removed from the document: the signal stamps no metadata and changes no version. A BeaconSignal carries the block height, the header time, the mediantime, and the confirmations of its block. Both discovery paths fill mediantime: the indexer path reads the Esplora block record once per distinct block, the fullnode path reads the RPC block.

The resolver identifies a beacon by the type of its service: SingletonBeacon, CASBeacon, or SMTBeacon. The beacon address is the address part of the BIP321 bitcoin: URI in serviceEndpoint (ADR 161):

A beacon whose serviceEndpoint gives no Bitcoin address has no signals, and the resolution continues. An update that changes only the case of the scheme or of a bech32 address does not remove the beacon address.

time and mediantime must be integers from 0 to 2^32-1, the range of the 32-bit time field of a block header. If a signal with minConf or more confirmations has another value, the resolution fails with a ResolveError of type INVALID_DID_UPDATE (ADR 158).

Update a DID

DidBtcr2.update() returns a sans-I/O state machine: the counterpart to the Resolver. The caller drives the update by fulfilling typed data needs (signing key, funding confirmation, broadcast).

import { DidBtcr2, Updater } from '@did-btcr2/method';
import { LocalSigner } from '@did-btcr2/keys';
import { BitcoinConnection } from '@did-btcr2/bitcoin';

// Build a Signer from a raw secret key. For KMS-backed keys use KeyManagerSigner
// from '@did-btcr2/key-manager' instead.
const signer = new LocalSigner(secretKeyBytes);
const bitcoin = new BitcoinConnection({ network: 'mutinynet', rest: { host: 'https://mutinynet.com/api' } });

// Ids are resolved against the source document before matching, so either the
// absolute DID URL (`${did}#initialKey`) or the relative one (`#initialKey`)
// works, whichever the document itself uses. Documents this package generates
// write the absolute spelling.
const updater = DidBtcr2.update({
  sourceDocument,
  patches              : [{ op: 'add', path: '/service/-', value: newService }],
  sourceVersionId      : 1,
  verificationMethodId : `${sourceDocument.id}#initialKey`,
  // `service` is optional (ADR 148). Nobody can announce an update for a document without a beacon service.
  beaconId             : sourceDocument.service![0].id,
});

let state = updater.advance();
while (state.status === 'action-required') {
  for (const need of state.needs) {
    switch (need.kind) {
      case 'NeedSigningKey':
        // need.verificationMethodId is the absolute DID URL of the method.
        // If the caller gave the relative id '#initialKey', it is '<did>#initialKey'.
        updater.provide(need, signer);
        break;
      case 'NeedFunding':
        // Check UTXOs at need.beaconAddress, fund if needed
        updater.provide(need);
        break;
      case 'NeedBroadcast': {
        // Capture the BroadcastResult: broadcast.txid, plus broadcast.announcement
        // (CAS beacons) and broadcast.proof (SMT beacons; must be retained, the
        // proof's nonce exists nowhere else).
        const broadcast = await Updater.announce(
          need.beaconService, need.did, need.signedUpdate, signer, bitcoin
        );
        updater.provide(need);
        break;
      }
    }
  }
  state = updater.advance();
}

const { signedUpdate } = state.result;

See src/core/updater.ts for the full UpdaterDataNeed union and phase transitions.

beaconId must name a beacon service of sourceDocument. DidBtcr2.update() refuses an id that names no service, with No beacon service found for provided beaconId. It also refuses a service that is not a beacon, or a beacon whose serviceEndpoint gives no Bitcoin address (ADR 161). Each refusal is an UpdateError of type INVALID_DID_UPDATE. NeedFunding.beaconAddress and the beacon transaction use the address part of the BIP321 URI, not the full serviceEndpoint.

An update carries the @context array that the specification pins, in this order: https://w3id.org/json-ld-patch/v1, https://w3id.org/zcap/v1, https://w3id.org/security/data-integrity/v2, https://btcr2.dev/context/v1. Updater.construct() writes the array, and the proof of the signed update repeats it. The package exports the array as BTCR2_UPDATE_CONTEXT. The array is inside the hashed and signed bytes, so an update with a different array is a different update. The resolver rejects an update with a different array, or with a proof @context that differs from the update @context, with a ResolveError of type INVALID_DID_UPDATE.

Both update paths apply the checks of the specification. Each failure that the specification names is an UpdateError on the write path, or a ResolveError on the read path, of type INVALID_DID_UPDATE. The inner error of the cryptosuite, the multikey, or the patch rides along as data.cause.

Both paths apply the patch with the rules of RFC 6902 and RFC 6901 (ADR 155). The first operation that fails, a failed test included, fails the whole patch. These operations fail, for example:

Each member name is data, also constructor and __proto__.

The patched document must keep the DID as its id, and it must conform to DID Core. The write path applies the rules of a producer, and the read path applies the rules of a consumer (ADR 149). A producer also refuses an item that occurs two times in a set, two methods or services with one id, a secretKeyMultibase, and a URL with a wrong syntax.

Updater.signingMethod(sourceDocument, verificationMethodId) returns the verification method that signs an update. DidBtcr2.update() calls it before it returns the Updater, and Updater.sign() calls it again. An entry of capabilityInvocation must identify verificationMethodId, as a reference or as an embedded method object. Updater.signingMethod() resolves the id of each entry and verificationMethodId to absolute DID URLs before it compares them. A reference entry must name a member of verificationMethod. The resolver applies the same lookup and the same method conditions to update.proof (ADR 147).

The id of the method must be a DID URL under the DID: the DID, then a fragment, a query, or a path. Examples are <did>#key-1, <did>?key=1, and <did>/keys/1. The write path resolves a relative id against the DID by RFC 3986, section 5, in each form: #key-1, ?key=1, /keys/1, and keys/1 are valid (ADR 162). The write path refuses an id under another DID, a URI that is not a DID URL, and a value that starts with //. The proof and NeedSigningKey.verificationMethodId name the method by its absolute DID URL.

The method must agree with three conditions:

  1. type is Multikey.
  2. controller is the DID, sourceDocument.id.
  3. publicKeyMultibase encodes a secp256k1 public key as a BIP340 Multikey, in the base58btc text of the key. A value that only starts with zQ3s does not agree with this condition.

A method that fails a condition gives an UpdateError of type INVALID_DID_UPDATE. An example message is Invalid update: verificationMethod.type must equal "Multikey". data.field names the field that fails.

If publicKeyMultibase is a string, a key failure also gives data.cause. It holds the type and the message of the error of SchnorrMultikey.fromVerificationMethod. For text that does not decode, the type is VERIFICATION_METHOD_ERROR and the message starts with Invalid publicKeyMultibase:. For text that is not the canonical encoding of the key, it holds the type VERIFICATION_METHOD_ERROR and the message Invalid publicKeyMultibase: not the base58btc encoding of the key. The conditions apply only to the method that signs. For each other verification method, DidDocument checks only that id, type, and controller are strings.

Updater.sign(sourceDocument, unsignedUpdate, verificationMethodId, signer) signs an update of sourceDocument. Make unsignedUpdate with Updater.construct() from the same sourceDocument. Before it signs, Updater.sign() checks the unsigned update in this order:

  1. unsignedUpdate is an object with no proof member.
  2. targetVersionId is an integer of 2 or more.
  3. @context is the array BTCR2_UPDATE_CONTEXT.
  4. sourceHash is the hash of sourceDocument.
  5. patch gives a valid DID document from sourceDocument.
  6. targetHash is the hash of that DID document.

The first check that fails gives an UpdateError of type INVALID_DID_UPDATE, with a message that starts with Invalid unsignedUpdate:. Each check after the object check names its field in data.field. For the patch check, data.cause holds the error of Updater.construct(). Updater.sign() does not get the version number of sourceDocument. The caller must make sure that targetVersionId is the version of sourceDocument plus 1. A resolver refuses another value.

The public key of the signer must be the key of the method. If the two keys differ, Updater.sign() gives an UpdateError with a message that starts with Signing key does not match verification method.

Updater.sign() wraps an error of the signer or of the cryptosuite as an UpdateError of type INVALID_DID_UPDATE, with the inner error in data.cause. The message of a wrapped error starts with Invalid signer: if the public key of the signer does not decode. If the public key decodes, the message starts with Invalid update: cannot make the proof with. For example, a key manager that holds only the public key gives the second message, with KEY_NOT_SIGNER in data.cause.type. A signer that returns a signature that does not have 64 bytes also gives the second message, with PROOF_GENERATION_ERROR in data.cause.type.

Updater.sign() verifies the proof with the public key of the verification method before the state machine asks for funding. A signer that returns a wrong signature spends no beacon UTXO. Updater.sign() returns a new object and adds no proof to its input, so the caller can sign the same unsigned update again.

Updater.construct() refuses a sourceVersionId that is not an integer of 1 or more, with Invalid sourceVersionId: expected an integer of 1 or more. DidBtcr2.update() also refuses such a sourceVersionId.

Updater.construct() also refuses a sourceDocument that is not an object, with Invalid sourceDocument: expected a DID document object. It refuses a sourceDocument.id that is not a string, with a message that starts with Invalid sourceDocument: expected a string id. Each refusal is an UpdateError of type INVALID_DID_UPDATE (ADR 156).

On the read path, the resolver checks each proof field by string equality before it verifies the signature. type is DataIntegrityProof, cryptosuite is bip340-jcs-2025, proofPurpose is capabilityInvocation, capabilityAction is Write, capability is urn:zcap:root:${encodeURIComponent(did)}, and invocationTarget is the DID. An update has one proof, so the resolver refuses a proof with a previousProof: Invalid update: proof.previousProof must not be present: an update has one proof.

A proof can carry created and expires, but the write path sets neither field. Each value must be an XML Datetime with a timezone. Each year is valid, also a year above 9999. created must not be after the header time of the block that contains the Beacon Signal. expires must not be before the mediantime of that block, and not before created. Each comparison is exact: each digit of a fraction of a second counts.

The resolver then verifies the proof with BIP340Cryptosuite.verifyProof. The cryptosuite also checks the other proof members of VC Data Integrity. For example, an id must be a valid URL string, and a nonce must be a string. The proofValue must be the canonical base58btc text of 64 bytes. A failed check gives Invalid update: proof verification failed: <message>. data.cause holds the type and the message of the CryptosuiteError (ADR 158). If the signature does not verify, the message is Invalid update: proof not verified.

DidBtcr2.deactivate() is DidBtcr2.update() with the patch [{ "op": "add", "path": "/deactivated", "value": true }], exported as DEACTIVATION_PATCH. Resolution stops at the deactivation for good. The factory does not refuse a source document that is deactivated already. The api does.

Update Aggregation (Multi-Party MuSig2)

Aggregation lets multiple DID controllers coordinate a single Bitcoin transaction that announces all of their updates at once, signed n-of-n with MuSig2. That subsystem now ships as its own package, @did-btcr2/aggregation, which builds on this one. Its high-level Runner API hides the message routing and decision plumbing:

import { AggregationServiceRunner } from '@did-btcr2/aggregation/service';
import { TransportFactory } from '@did-btcr2/aggregation';
import { getNetwork } from '@did-btcr2/bitcoin';
import { buildAggregationBeaconTx } from '@did-btcr2/method';

// Pick a transport. Nostr (relay-based) or HTTP/REST (operator-hosted).
const transport = TransportFactory.establish({
  type   : 'nostr',
  relays : ['wss://relay.damus.io'],
});
// Or for HTTP: { type: 'http', role: 'server' } on the operator side,
//              { type: 'http', role: 'client', baseUrl: '...' } on the participant side.

transport.registerActor(serviceDid, serviceKeys);
transport.start();

// onProvideTxData reads `runner`. The runner calls onProvideTxData only after
// the constructor returns.
const runner: AggregationServiceRunner = new AggregationServiceRunner({
  transport,
  did    : serviceDid,
  keys   : serviceKeys,
  config : {
    minParticipants : 2,
    network         : 'mutinynet',
    beaconType      : 'CASBeacon',
  },
  // The transaction must spend the beacon output. buildAggregationBeaconTx finds
  // a UTXO at beaconAddress and puts signalBytes in the last output (OP_RETURN).
  onProvideTxData : async ({ cohortId, beaconAddress, signalBytes, feeEstimator }) => {
    const cohort = runner.session.getCohort(cohortId)!;
    return await buildAggregationBeaconTx({
      beaconAddress,
      internalPubkey : cohort.internalKey,
      signalBytes,
      bitcoin,                   // a BitcoinConnection
      network        : getNetwork(cohort.network),
      feeEstimator,
    });
  },
});

runner.on('signing-complete', (result) => console.log('done'));
const result = await runner.run();

The full step-by-step protocol walkthrough (service flow, participant flow, decision callbacks, events, the low-level state machine API, and production deployment notes) is in docs/aggregation.md. The HTTP/REST transport has its own walkthrough in docs/http-transport.md. See the @did-btcr2/aggregation package for the runnable APIs those guides describe.

This package exports resolveBtcr2SenderPk, which gives the communication key of a sender DID to the aggregation transports. For an EXTERNAL (x1) sender, the key is the key of the capabilityInvocation[0] method in the initial DID document that its genesis document gives. That method must agree with the three conditions of the method that signs an update. If it does not, resolveBtcr2SenderPk returns undefined, and the transport drops the message.

If the resolver refuses the genesis document, resolveBtcr2SenderPk also returns undefined. For example, the resolver refuses a genesis document without @context (ADR 148). A genesis document without service gives the key, if the method agrees with the three conditions.

resolveBtcr2SenderPk reads no current DID document, so it gives the genesis key also after an update removes that key (ADR 153). After a leak of that key, the holder can act as the DID in a cohort and can make the DID invalid. The steps after a key rotation are in Sender authentication.

Architecture Principles

Build & Test

# From packages/method/
pnpm build              # Compile ESM + CJS + browser bundle
pnpm build:tests        # Compile tests to tests/compiled/
pnpm test               # Run the test suite with coverage
pnpm lint               # ESLint (zero warnings tolerated)

Tests run from compiled JS, so run pnpm build:tests before pnpm test after any test changes.

Documentation

License

MPL-2.0