Shared types, errors, JCS canonicalization, hashing, and JSON Patch for the did-btcr2-js packages.
Part of the did-btcr2-js monorepo.
This package is the foundational layer that every other @did-btcr2/* package depends on. It has no workspace dependencies of its own. Its responsibilities:
canonicalize sorts the members of each object, also of an object with a data member named toJSON. It refuses a value with no JCS form with a CanonicalizationError. The types are NON_FINITE_NUMBER (NaN, Infinity, or -Infinity), LONE_SURROGATE (a string or a member name), and NOT_JSON_DATA (a BigInt or a cycle). data.path gives the location of the value (ADR 154).JSONPatch.apply applies a JSON Patch (RFC 6902) with the JSON Pointer rules of RFC 6901, and PatchOperation is the type of an operation. The DID update paths use it. Each member name is data, also constructor and __proto__ (ADR 155).KeyBytes, HashBytes, SignatureBytes, etc.), DID-related enums (IdentifierTypes, IdentifierHrp, BitcoinNetworkNames), and the JSONObject family used by canonical signing. ProblemDetails (RFC 9457) is the type of an error in a proof verification result.DidMethodError base with one subclass per failure domain (MethodError, IdentifierError, UpdateError, ResolveError, KeyManagerError, KeyPairError, MultikeyError, CryptosuiteError, etc.) Every typed error carries a type string tag and a structured data payload.DateUtils.isValidXsdDateTime and DateUtils.isValidXsdDateTimeStamp check a value with the dateTime rules of XML Schema 1.1. DateUtils.xsdDateTimeToEpochMs and DateUtils.compareXsdDateTime accept each year, also a year above 9999 (ADR 158).npm install @did-btcr2/std
Or with pnpm:
pnpm add @did-btcr2/std
Ships both ESM and CommonJS. Requires Node >= 22.
| Concern | Entry point |
|---|---|
| Canonicalize an object | canonicalize(obj) |
| Hash canonical bytes | hash(string), canonicalHashBytes(obj) |
| Hash and encode in one call | canonicalHash(obj, { encoding }) |
| Encode/decode hash bytes | encode(bytes, encoding), decode(string, encoding) |
| Apply a JSON Patch | JSONPatch.apply, PatchOperation |
| Base error class | DidMethodError |
| Subclassed errors | MethodError, IdentifierError, UpdateError, ResolveError, KeyPairError, KeyManagerError, MultikeyError, … |
| Byte type aliases | Bytes, KeyBytes, HashBytes, SignatureBytes, DocumentBytes |
| DID enums | IdentifierTypes, IdentifierHrp, BitcoinNetworkNames |
| Cryptosuite names | CryptosuiteName ('bip340-jcs-2025' or 'bip340-rdfc-2025') |
import { canonicalHash, JSONPatch, MethodError } from '@did-btcr2/std';
// Hash a DID document canonically (JCS + SHA-256, base64urlnopad).
const docHash = canonicalHash({ id: 'did:btcr2:k1q5p...', verificationMethod: [] });
// Apply a JSON Patch to a document. The result is a new document; the source document
// and the operations do not change. The apply uses the rules of RFC 6902 and RFC 6901:
// the first operation that fails, a failed test included, fails the whole patch. For
// example, an array index with a leading zero, a remove of a location that does not
// exist, and a move into a child of its own location fail (ADR 155).
const patched = JSONPatch.apply(
{ id: 'did:btcr2:k1q5p...', service: [] },
[{ op: 'add', path: '/service/-', value: { id: '#dwn' } }],
);
// Throw a structured error with type tag + data payload.
throw new MethodError('beacon address mismatch', 'BEACON_VALIDATION', {
address : 'tb1q...',
kind : 'P2WPKH',
});
@noble/hashes and @scure/base. @scure/bip32 is an optional peer dependency for HD-key consumers.base64urlnopad is the default for hash encoding; protocol fields that carry raw bytes are hex-encoded, so downstream callers normalize to a common encoding before comparing.# From packages/std/
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)
canonicalize, canonicalHash, JSONPatch, and the error classes in errors.ts.