feat: add did:key support#103
Conversation
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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (8)
✅ Files skipped from review due to trivial changes (3)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughImplements 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. Changesdid:key Implementation and Integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 7
🧹 Nitpick comments (2)
packages/w3c-issuer/src/did-key/parse.test.ts (1)
28-69: ⚡ Quick winAdd 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 winDerive 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
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (18)
README.mdpackages/w3c-issuer/README.mdpackages/w3c-issuer/package.jsonpackages/w3c-issuer/src/did-key/README.mdpackages/w3c-issuer/src/did-key/index.tspackages/w3c-issuer/src/did-key/keyPair.test.tspackages/w3c-issuer/src/did-key/keyPair.tspackages/w3c-issuer/src/did-key/parse.test.tspackages/w3c-issuer/src/did-key/parse.tspackages/w3c-issuer/src/did-key/types.tspackages/w3c-issuer/src/did-key/varint.tspackages/w3c-issuer/src/did-web/wellKnown/query.test.tspackages/w3c-issuer/src/did-web/wellKnown/query.tspackages/w3c-issuer/src/index.tspackages/w3c-vc/README.mdpackages/w3c-vc/src/lib/__fixtures__/key-pairs.tspackages/w3c-vc/src/lib/__fixtures__/test-scenarios.tspackages/w3c-vc/src/lib/w3c-vc.test.ts
- 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>
|
# [2.2.0](https://github.com/TrustVC/w3c/compare/@trustvc/w3c-issuer@2.1.0...@trustvc/w3c-issuer@2.2.0) (2026-06-02) ### Features * add did:key support ([#103](#103)) ([67dbb86](67dbb86))
|
🎉 This PR is included in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [2.2.0](https://github.com/TrustVC/w3c/compare/@trustvc/w3c-context@2.1.0...@trustvc/w3c-context@2.2.0) (2026-06-02) ### Features * add did:key support ([#103](#103)) ([67dbb86](67dbb86))
|
🎉 This PR is included in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [2.2.0](https://github.com/TrustVC/w3c/compare/@trustvc/w3c-credential-status@2.1.0...@trustvc/w3c-credential-status@2.2.0) (2026-06-02) ### Features * add did:key support ([#103](#103)) ([67dbb86](67dbb86))
|
🎉 This PR is included in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [2.2.0](https://github.com/TrustVC/w3c/compare/@trustvc/w3c-vc@2.1.0...@trustvc/w3c-vc@2.2.0) (2026-06-02) ### Features * add did:key support ([#103](#103)) ([67dbb86](67dbb86))
|
🎉 This PR is included in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
# [2.2.0](https://github.com/TrustVC/w3c/compare/@trustvc/w3c@2.1.0...@trustvc/w3c@2.2.0) (2026-06-02) ### Features * add did:key support ([#103](#103)) ([67dbb86](67dbb86))
|
🎉 This PR is included in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |



Summary by CodeRabbit
New Features
Documentation