did-btcr2-js

@did-btcr2/integrity

TypeScript implementation of the Data Integrity BIP340 Cryptosuite v0.1 specification.

Part of the did-btcr2-js monorepo.

Summary

This package produces and verifies method-agnostic Data Integrity proofs; its primary consumer is did:btcr2, whose updates it authenticates. A secured document is any JSON object plus a proof block carrying a BIP-340 Schnorr signature over the JCS-canonicalized payload.

The cryptosuite is what links the abstract Signer interface from @did-btcr2/keys to the on-disk shape of a signed DID update.

Install

npm install @did-btcr2/integrity

Or with pnpm:

pnpm add @did-btcr2/integrity

The package ships both ESM (dist/esm/) and CJS (dist/cjs/) via conditional exports, so it works with import and require out of the box.

Key Exports

Concern Entry point
Build / verify proofs BIP340Cryptosuite, Cryptosuite, CryptosuiteVerificationResult
Proof primitive BIP340DataIntegrityProof, DataIntegrityProof, DataIntegrityProofObject, VerificationResult
Document payload types UnsecuredDocument, SecuredDocument
Verification method wrapper SchnorrMultikey, Multikey, MultikeyObject
Construction helpers FromSecretKey, FromPublicKey
Proof options DataIntegrityProofOptions

Quick Start

Sign an update

import { SchnorrMultikey } from '@did-btcr2/integrity';
import { Secp256k1SecretKey } from '@did-btcr2/keys';

const controller   = 'did:btcr2:k1q5p...';
const id           = '#initialKey';
const secretKeyBytes = new Secp256k1SecretKey(rawSecretBytes).bytes; // 32-byte Uint8Array
const multikey     = SchnorrMultikey.fromSecretKey(id, controller, secretKeyBytes);

// UnsecuredDocument: any JSON object to add a proof to (for example, an unsigned did:btcr2 update).
const unsigned = {
  '@context' : ['https://w3id.org/json-ld-patch/v1', 'https://w3id.org/zcap/v1', 'https://w3id.org/security/data-integrity/v2', 'https://btcr2.dev/context/v1'],
  // ... the other members of the document
};
const config = {
  type               : 'DataIntegrityProof' as const,
  cryptosuite        : 'bip340-jcs-2025',
  proofPurpose       : 'capabilityInvocation',
  verificationMethod : `${controller}${id}`,
};
const proof = multikey.toCryptosuite().createProof(unsigned, config);
// proof.cryptosuite === 'bip340-jcs-2025'
// proof['@context'] is a copy of unsigned['@context']. createProof does not change unsigned or config.

createProof copies the document @context into the proof. Options with an @context for a document with no @context give a CryptosuiteError of type PROOF_GENERATION_ERROR, because Verify Proof can never verify such a proof.

Verify a signed update

import { SchnorrMultikey } from '@did-btcr2/integrity';

// Reconstruct the multikey from the DID verification method.
// verificationMethod is a DidVerificationMethod with id, controller, publicKeyMultibase.
const multikey = SchnorrMultikey.fromVerificationMethod(verificationMethod);
const result   = multikey.toCryptosuite().verifyProof(signedUpdate);

if (!result.verified) {
  throw new Error('DI proof failed');
}
// result.verifiedDocument is the update without its proof.

verifyProof throws a CryptosuiteError if the verification cannot complete, for example for a proof member that breaks a rule. See Verification results and refusals.

Sign via an external Signer (HSM, KMS, hardware wallet)

import { SchnorrMultikey } from '@did-btcr2/integrity';

// kmsSigner implements Signer: { publicKey: Uint8Array; sign(data, scheme): SignatureBytes }
const controller = 'did:btcr2:k1q5p...';
const id         = '#initialKey';
const multikey   = SchnorrMultikey.fromSigner(id, controller, kmsSigner);

// unsigned is the document of "Sign an update", with its @context.
const config = {
  type               : 'DataIntegrityProof' as const,
  cryptosuite        : 'bip340-jcs-2025',
  proofPurpose       : 'capabilityInvocation',
  verificationMethod : `${controller}${id}`,
};
const proof = multikey.toCryptosuite().createProof(unsigned, config);

fromSigner seeds the multikey’s public key from kmsSigner.publicKey and delegates all signing to the signer, so no secret bytes ever enter the JS process. A public-key mismatch at construction time throws immediately.

Verification results and refusals

The cryptosuite follows the algorithms of VC Data Integrity 1.0 and of the bip340-jcs-2025 suite (ADR 158). No function of this section changes its input.

Verify with the cryptosuite

BIP340Cryptosuite.verifyProof(securedDocument) returns a CryptosuiteVerificationResult: { verified, verifiedDocument }. If verified is true, verifiedDocument is the document without its proof. Else verifiedDocument is null. If the document @context does not start with the proof @context, the result is verified: false.

verifyProof throws a CryptosuiteError if the verification cannot complete. The result is then an error, not verified: false.

Error type Cause
PARSING_ERROR The secured document or its proof is not a JSON object.
PROOF_VERIFICATION_ERROR A proof member breaks a rule of Proof members.
PROOF_VERIFICATION_ERROR The proofValue is not the base58btc Multibase text (prefix z) of 64 bytes.
PROOF_VERIFICATION_ERROR The type, the cryptosuite, or the verificationMethod is not the one of the cryptosuite instance.
PROOF_GENERATION_ERROR created is not an XML Schema dateTime, or the proof has no JCS form.
PROOF_TRANSFORMATION_ERROR The document has no JCS form.

The proofValue must be the canonical base58btc text of its 64 bytes. The base58btc decoder also reads some other text for the same bytes, for example text with a character above U+00FF. verifyProof refuses such text.

Verify with the Data Integrity layer

BIP340DataIntegrityProof.verifyProof(document, expectedPurpose?, mediaType?, expectedDomain?, expectedChallenge?) is the Verify Proof algorithm of VC Data Integrity 1.0. document is the JSON text of the secured document. Each expected value is optional, and the function compares only a value that you give. expectedDomain is one string or an array of strings.

The result is a VerificationResult: { verified, verifiedDocument, mediaType, warnings, errors }. A failed check of the table below gives verified: false and one ProblemDetails (RFC 9457) in errors. The type of the item is https://w3id.org/security#<TYPE>. <TYPE> is the error type of the failed check. If the signature does not verify, or if the document @context does not start with the proof @context, verified is false and errors is empty.

Check Error type if the check fails
The secured document and its proof are JSON objects. PARSING_ERROR
The proof has a type, a verificationMethod, and a proofPurpose. PROOF_VERIFICATION_ERROR
The proof has no previousProof. This suite does not verify a proof chain. PROOF_VERIFICATION_ERROR
proofPurpose is expectedPurpose. PROOF_VERIFICATION_ERROR
The proof domain and expectedDomain hold the same strings. A string is a set of one string. The order and a repeated string have no effect. INVALID_DOMAIN_ERROR
challenge is expectedChallenge. INVALID_CHALLENGE_ERROR

If document is not JSON text, verifyProof throws a DataIntegrityProofError of type PARSING_ERROR. The function does not catch an error of BIP340Cryptosuite.verifyProof.

Proof members

createProof and verifyProof check the members of a proof (VC Data Integrity 1.0, Proofs) before the algorithm. For a member that breaks a rule, createProof throws a CryptosuiteError of type PROOF_GENERATION_ERROR. verifyProof throws a CryptosuiteError of type PROOF_VERIFICATION_ERROR.

Member Rule
type, cryptosuite, verificationMethod, proofPurpose A string. Each of these members is necessary.
id A valid URL string of the URL Standard. Thus #key-0, proof-1, and urn:uuid:... are valid. not a url is not valid.
created On create: an XML Schema dateTimeStamp (a dateTime with a timezone). On verify, the Proof Configuration step needs an XML Schema dateTime, else it throws PROOF_GENERATION_ERROR.
expires On create: an XML Schema dateTimeStamp. On verify: an XML Schema dateTime.
domain A string or an array of strings. On create, the array must not have a repeated string.
challenge, nonce A string.
previousProof A string or an array of strings.

Refusals on create

BIP340Cryptosuite.createProof(document, options) returns a new proof: a copy of the options with the proofValue. If the document has an @context, the proof gets a copy of it in place of an @context of the options. createProof throws a CryptosuiteError of type PROOF_GENERATION_ERROR in these cases:

BIP340DataIntegrityProof.addProof(document, options) returns { ...document, proof }. Before it calls createProof, it refuses these inputs with a DataIntegrityProofError of type PROOF_GENERATION_ERROR:

This suite adds one proof. It does not make a proof set or a proof chain (VC Data Integrity 1.0, Add Proof Set/Chain).

Architecture Principles

Build & Test

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

Documentation

License

MPL-2.0