Skip to content

feat: add did:key support#103

Merged
RishabhS7 merged 2 commits into
mainfrom
feat/did-key-support
Jun 2, 2026
Merged

feat: add did:key support#103
RishabhS7 merged 2 commits into
mainfrom
feat/did-key-support

Conversation

@rongquan1
Copy link
Copy Markdown
Contributor

@rongquan1 rongquan1 commented May 28, 2026

Summary by CodeRabbit

  • New Features

    • Added support for self‑certifying did:key as an alternative DID method; issuance, signing and verification work with both did:web and did:key
    • Added generation of did:key key pairs for ECDSA-SD-2023 and BBS-2023; end-to-end issuance and verification flows updated
  • Documentation

    • Expanded docs and Getting Started to cover did:key setup, usage examples, and differences from did:web
    • Updated About to note W3C Verifiable Credentials Data Model v2.0 and added Status List 2021 and Bitstring status support

Review Change Stack

Introduces a self-certifying did:key method alongside the existing did:web
flow, with key pair generation, parsing, varint encoding, and round-trip
tests. Threads did:key through w3c-issuer exports and w3c-vc sign/verify
fixtures, and updates package READMEs to document the new method.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3a060e79-bb76-41ff-88d9-46ed0c10422c

📥 Commits

Reviewing files that changed from the base of the PR and between 51b41b0 and 6269ed2.

📒 Files selected for processing (8)
  • README.md
  • packages/w3c-issuer/README.md
  • packages/w3c-issuer/src/did-key/README.md
  • packages/w3c-issuer/src/did-key/parse.test.ts
  • packages/w3c-issuer/src/did-key/parse.ts
  • packages/w3c-issuer/src/did-key/varint.ts
  • packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts
  • packages/w3c-vc/src/lib/w3c-vc.test.ts
✅ Files skipped from review due to trivial changes (3)
  • packages/w3c-issuer/src/did-key/README.md
  • README.md
  • packages/w3c-issuer/README.md
🚧 Files skipped from review as they are similar to previous changes (5)
  • packages/w3c-vc/src/lib/fixtures/key-pairs.ts
  • packages/w3c-vc/src/lib/w3c-vc.test.ts
  • packages/w3c-issuer/src/did-key/parse.test.ts
  • packages/w3c-issuer/src/did-key/varint.ts
  • packages/w3c-issuer/src/did-key/parse.ts

📝 Walkthrough

Walkthrough

Implements did:key support across packages: adds types and varint codec, parsing/conversion utilities, did:key key generation, in-memory well-known DID document synthesis, tests/fixtures (unit and end-to-end), package exports, and documentation updates.

Changes

did:key Implementation and Integration

Layer / File(s) Summary
Type contracts and varint codec support
packages/w3c-issuer/src/did-key/types.ts, packages/w3c-issuer/src/did-key/varint.ts
Defines DidKeyType union and DidKeyInfo/GeneratedDidKey interfaces; implements encodeVarint and decodeVarint functions for multicodec prefix serialization.
did:key parsing, detection, and conversion
packages/w3c-issuer/src/did-key/parse.ts, packages/w3c-issuer/src/did-key/parse.test.ts
Implements isDidKey, parseDidKey, publicKeyToDidKey, multibaseToDidKey, and buildDidKeyDocument; tests cover P-256 and BLS12-381 G2, fragment handling, error cases, and document synthesis.
did:key key pair generation
packages/w3c-issuer/src/did-key/keyPair.ts, packages/w3c-issuer/src/did-key/keyPair.test.ts
Implements generateDidKeyPair that wraps key generation and produces did:key identifiers + multikey key material; tests verify DID generation and parse round-trips for ECDSA-SD-2023 and BBS-2023.
did:key module documentation and exports
packages/w3c-issuer/src/did-key/README.md, packages/w3c-issuer/src/did-key/index.ts
Adds comprehensive did:key documentation and re-exports types, parse, and keyPair from the did-key module entrypoint.
did-web resolver integration with did:key
packages/w3c-issuer/src/did-web/wellKnown/query.ts, packages/w3c-issuer/src/did-web/wellKnown/query.test.ts
Adds early did:key branch to queryDidDocument to synthesize well-known DID documents in-memory; tests verify bare did:key and fragment-qualified DID URLs.
w3c-issuer package-level exports and documentation
packages/w3c-issuer/src/index.ts, packages/w3c-issuer/README.md, packages/w3c-issuer/package.json
Re-exports did-key from package index; documents did:key issuance and usage; fixes package.json trailing content.
Test fixtures and scenarios for did:key
packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts, packages/w3c-vc/src/lib/__fixtures__/test-scenarios.ts
Adds ecdsa2023DidKeyPair, bbs2023DidKeyPair, ECDSA_DID_KEY_ISSUER, BBS_DID_KEY_ISSUER; extends modernCryptosuiteTestScenarios with did:key variants.
End-to-end did:key integration tests
packages/w3c-vc/src/lib/w3c-vc.test.ts
Adds a parameterized end-to-end test that generates did:key pairs, issues credentials, derives selective-disclosure credentials, verifies them, and asserts tamper detection.
Project-level documentation updates
README.md, packages/w3c-vc/README.md
Updates root and package READMEs to include did:key as a DID method option and clarify DID-method-agnostic signing/verification with in-memory did:key resolution.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • TrustVC/w3c#65: Wires queryDidDocument and document-loader integration used by in-memory did:key resolution.
  • TrustVC/w3c#67: Adds multikey/BBS-2023 and ECDSA-SD-2023 key generation support leveraged by did:key generation.

Suggested labels

released

Suggested reviewers

  • RishabhS7
  • Moiz47
  • kmk142789

"I hopped through bytes and multibase streams,
I stitched a did:key from bright small beams.
P-256 bells and BLS berries too,
I signed your VC and watched it prove true.
A tiny rabbit, with crypto for you."

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is entirely missing. The template requires Summary, Changes, Issues, and Releases sections, but none were provided by the author. Add a complete pull request description following the template: include background/summary, itemized list of changes, related issue references, and release information with target channel and ETA.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main feature addition: support for did:key DIDs. This directly matches the substantial additions across multiple files including new did-key module, documentation, and test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/did-key-support

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🧹 Nitpick comments (2)
packages/w3c-issuer/src/did-key/parse.test.ts (1)

28-69: ⚡ Quick win

Add negative tests for invalid key byte lengths.

Please add cases where codec is valid but public key length is wrong (e.g., P-256 with 32 bytes, BLS12-381 G2 with 95 bytes) to lock parser validation behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/w3c-issuer/src/did-key/parse.test.ts` around lines 28 - 69, Add two
negative unit tests in parse.test.ts that call parseDidKey with otherwise-valid
multibase multicodecs but tampered decoded key lengths: one for P-256 using the
P-256 multibase prefix but with 32 bytes (expected compressed is 33) and one for
Bls12381G2 using its multibase prefix but with 95 bytes (expected 96); assert
parseDidKey throws an error that indicates an invalid/incorrect key length. Use
the same pattern as existing tests (wrap parseDidKey call in expect(() =>
...).toThrow(...)) and reference parseDidKey, P256_PUBLIC_KEY_MULTIBASE and
BLS_PUBLIC_KEY_MULTIBASE to locate where to add these cases. Ensure the
thrown-regex checks match the parser's error message for invalid key length.
packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts (1)

38-43: ⚡ Quick win

Derive did:key fixtures from existing key-pair fixtures to avoid drift.

The new did:key fixtures duplicate key material already defined above; this can diverge over time and also creates extra secret-scanner hits on Line 51 and Line 60.

♻️ Proposed refactor
-const ECDSA_PK_MULTIBASE = 'zDnaemDNwi4G5eTzGfRooFFu5Kns3be6yfyVNtiaMhWkZbwtc';
+const ECDSA_PK_MULTIBASE = ecdsa2023KeyPair.publicKeyMultibase;
 const ECDSA_DID_KEY = `did:key:${ECDSA_PK_MULTIBASE}`;

-const BBS_PK_MULTIBASE =
-  'zUC7HnpncVAkTjtL6B8prX6bQM2WA5sJ7rXFeCqyrvPnrzoFBjYsVUTNwzhhPUazja73tWwPeEBWCUgq5qBSrtrXiYhVvBCgZPTCiWANj7TSiZJ6SnyC3pkt94GiuChhAvmRRbt';
+const BBS_PK_MULTIBASE = bbs2023KeyPair.publicKeyMultibase;
 const BBS_DID_KEY = `did:key:${BBS_PK_MULTIBASE}`;
@@
   type: VerificationType.Multikey,
   controller: ECDSA_DID_KEY,
   publicKeyMultibase: ECDSA_PK_MULTIBASE,
-  secretKeyMultibase: 'z42tmUXTVn3n9BihE6NhdMpvVBTnFTgmb6fw18o5Ud6puhRW',
+  secretKeyMultibase: ecdsa2023KeyPair.secretKeyMultibase,
 };
@@
   type: VerificationType.Multikey,
   controller: BBS_DID_KEY,
   publicKeyMultibase: BBS_PK_MULTIBASE,
-  secretKeyMultibase: 'z488ur1KSFDd3Y1L6pXcPrZRjE18PNBhgzwJvMeoSxKPNysj',
+  secretKeyMultibase: bbs2023KeyPair.secretKeyMultibase,
 };

Also applies to: 50-51, 59-60

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts` around lines 38 - 43, The
did:key fixtures are hardcoded duplicates of existing key-pair material
(ECDSA_PK_MULTIBASE, BBS_PK_MULTIBASE); replace the duplicated literals by
deriving the DID strings from the existing key-pair variables (e.g., build
ECDSA_DID_KEY as `did:key:${ECDSA_PK_MULTIBASE}` using the exported/public key
multibase value from the ECDSA key-pair fixture, and similarly build BBS_DID_KEY
from the BBS key-pair fixture's publicKeyMultibase), remove the duplicate
literal definitions, and update any other occurrences (the other duplicated
spots mentioned) to reference these derived constants so the fixtures cannot
drift and secret-scanner hits are eliminated.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/w3c-issuer/README.md`:
- Line 152: The README link to "./src/did-key/README.md" is broken for npm
consumers because only "dist" is published; update the link to point to the
published artifact path (e.g., "./dist/src/did-key/README.md" or the equivalent
location under dist) so the documentation resolves in the installed package and
verify the link text remains the same in packages/w3c-issuer/README.md.

In `@packages/w3c-issuer/src/did-key/parse.ts`:
- Around line 37-55: In parseDidKey (parse.ts) after decoding the varint
(variables codec, bytesRead) and slicing publicKey, validate the length for
supported codecs: if codec === MULTICODEC_P256_PUB ensure publicKey.length ===
33, if codec === MULTICODEC_BLS12381_G2_PUB ensure publicKey.length === 96; on
mismatch throw an Error mentioning the expected and actual lengths and include
the multicodec value (use codec or toString(16)) before returning the
did/verificationMethodId/publicKey/keyType result so malformed payloads are
rejected early.

In `@packages/w3c-issuer/src/did-key/README.md`:
- Around line 31-34: Several fenced code blocks in README.md (the ones
containing the examples starting with "did:web:trustvc.github.io:did:1", the
"did:key: + z + base58btc( <multicodec varint> || <public key bytes> )" snippet,
and the diagram beginning with
"did:key:zDnaemDNwi4G5eTzGfRooFFu5Kns3be6yfyVNtiaMhWkZbwtc") are missing
language identifiers and trigger MD040; fix by adding a language tag such as
text to each opening ``` fence (e.g. change ``` to ```text) so the three blocks
render and lint-clean.

In `@packages/w3c-vc/src/lib/w3c-vc.test.ts`:
- Line 159: The signCredential call on the test line is misformatted for
Prettier; reformat the invocation of signCredential(credential, didKeyPairs,
cryptosuite as CryptoSuiteName) to match the repo's Prettier style (e.g., put
each argument on its own line or ensure proper spacing after commas and around
the type assertion), or extract the cast into a separate variable (const suite =
cryptosuite as CryptoSuiteName) and call signCredential(credential, didKeyPairs,
suite) so ESLint/Prettier no longer flags the line.

In `@README.md`:
- Around line 25-26: The README uses non-descriptive link labels `[link](...)`;
update the two markdown bullets to replace the generic `[link]` texts with
meaningful link text such as "did:web setup guide" for the did:web bullet and
"did:key key pair guide" for the did:key bullet, preserving the existing URLs
and markdown link syntax so the links remain unchanged but are now accessible
and descriptive.
- Line 5: Update the opening sentence in README.md to fix wording: change "A
wrapper lib build for TrustVC to work with..." to use "library" and the correct
past participle "built" — e.g., "A wrapper library built for TrustVC to work
with W3C Verifiable Credentials..." — so replace "lib" with "library" and
"build" with "built" in that sentence.

---

Nitpick comments:
In `@packages/w3c-issuer/src/did-key/parse.test.ts`:
- Around line 28-69: Add two negative unit tests in parse.test.ts that call
parseDidKey with otherwise-valid multibase multicodecs but tampered decoded key
lengths: one for P-256 using the P-256 multibase prefix but with 32 bytes
(expected compressed is 33) and one for Bls12381G2 using its multibase prefix
but with 95 bytes (expected 96); assert parseDidKey throws an error that
indicates an invalid/incorrect key length. Use the same pattern as existing
tests (wrap parseDidKey call in expect(() => ...).toThrow(...)) and reference
parseDidKey, P256_PUBLIC_KEY_MULTIBASE and BLS_PUBLIC_KEY_MULTIBASE to locate
where to add these cases. Ensure the thrown-regex checks match the parser's
error message for invalid key length.

In `@packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts`:
- Around line 38-43: The did:key fixtures are hardcoded duplicates of existing
key-pair material (ECDSA_PK_MULTIBASE, BBS_PK_MULTIBASE); replace the duplicated
literals by deriving the DID strings from the existing key-pair variables (e.g.,
build ECDSA_DID_KEY as `did:key:${ECDSA_PK_MULTIBASE}` using the exported/public
key multibase value from the ECDSA key-pair fixture, and similarly build
BBS_DID_KEY from the BBS key-pair fixture's publicKeyMultibase), remove the
duplicate literal definitions, and update any other occurrences (the other
duplicated spots mentioned) to reference these derived constants so the fixtures
cannot drift and secret-scanner hits are eliminated.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 07e9fbcb-2b5a-4a7a-b352-8a45599c6d06

📥 Commits

Reviewing files that changed from the base of the PR and between 1ce290a and 51b41b0.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (18)
  • README.md
  • packages/w3c-issuer/README.md
  • packages/w3c-issuer/package.json
  • packages/w3c-issuer/src/did-key/README.md
  • packages/w3c-issuer/src/did-key/index.ts
  • packages/w3c-issuer/src/did-key/keyPair.test.ts
  • packages/w3c-issuer/src/did-key/keyPair.ts
  • packages/w3c-issuer/src/did-key/parse.test.ts
  • packages/w3c-issuer/src/did-key/parse.ts
  • packages/w3c-issuer/src/did-key/types.ts
  • packages/w3c-issuer/src/did-key/varint.ts
  • packages/w3c-issuer/src/did-web/wellKnown/query.test.ts
  • packages/w3c-issuer/src/did-web/wellKnown/query.ts
  • packages/w3c-issuer/src/index.ts
  • packages/w3c-vc/README.md
  • packages/w3c-vc/src/lib/__fixtures__/key-pairs.ts
  • packages/w3c-vc/src/lib/__fixtures__/test-scenarios.ts
  • packages/w3c-vc/src/lib/w3c-vc.test.ts

Comment thread packages/w3c-issuer/README.md Outdated
Comment thread packages/w3c-issuer/src/did-key/parse.ts
Comment thread packages/w3c-issuer/src/did-key/README.md Outdated
Comment thread packages/w3c-issuer/src/did-key/varint.ts
Comment thread packages/w3c-vc/src/lib/w3c-vc.test.ts Outdated
Comment thread README.md Outdated
Comment thread README.md Outdated
- varint: replace 32-bit bitwise accumulator with arithmetic accumulation
  and cap prefix length, so non-canonical 5-byte varints can no longer
  truncate to a supported codec.
- parse: validate decoded public key byte length (P-256 33, Bls12381G2 96)
  and add negative tests for tampered lengths and over-long varints.
- key-pairs fixtures: derive did:key material from existing key pairs
  instead of duplicating multibase / secret key strings.
- docs: tag fenced code blocks, replace relative did:key README link with
  the GitHub URL so it resolves for npm consumers, fix root README grammar
  and link labels, and apply prettier to w3c-vc.test.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

@rongquan1 rongquan1 requested a review from RishabhS7 May 28, 2026 05:41
@RishabhS7 RishabhS7 merged commit 67dbb86 into main Jun 2, 2026
19 checks passed
@RishabhS7 RishabhS7 deleted the feat/did-key-support branch June 2, 2026 02:43
nghaninn pushed a commit that referenced this pull request Jun 2, 2026
@tradetrustimda
Copy link
Copy Markdown

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@tradetrustimda
Copy link
Copy Markdown

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@tradetrustimda
Copy link
Copy Markdown

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

nghaninn pushed a commit that referenced this pull request Jun 2, 2026
@tradetrustimda
Copy link
Copy Markdown

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

nghaninn pushed a commit that referenced this pull request Jun 2, 2026
@tradetrustimda
Copy link
Copy Markdown

🎉 This PR is included in version 2.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants