From 001849efcb48043cb92a4e4351612dcf106b95a6 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Sun, 17 Mar 2024 22:20:54 -0400 Subject: [PATCH] Allow `@context` array values in multikeys. --- CHANGELOG.md | 3 +++ lib/index.js | 7 +++++-- test/Ed25519Multikey.spec.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bed15c..4b98981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - Enable loading keys from `publicKeyJwk` via `from()` and converting to/from JWK. +### Fixed +- Allow `@context` array values in multikeys. + ## 1.0.2 - 2024-01-25 ### Fixed diff --git a/lib/index.js b/lib/index.js index fd29356..847718a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -136,8 +136,11 @@ function _assertMultikey(key) { if(key.type !== 'Multikey') { throw new Error('"key" must be a Multikey with type "Multikey".'); } - if(key['@context'] !== MULTIKEY_CONTEXT_V1_URL) { - throw new Error('"key" must be a Multikey with context ' + + if(!(key['@context'] === MULTIKEY_CONTEXT_V1_URL || + (Array.isArray(key['@context']) && + key['@context'].includes(MULTIKEY_CONTEXT_V1_URL)))) { + throw new TypeError( + '"key" must be a Multikey with context ' + `"${MULTIKEY_CONTEXT_V1_URL}".`); } } diff --git a/test/Ed25519Multikey.spec.js b/test/Ed25519Multikey.spec.js index f619ca9..a902328 100644 --- a/test/Ed25519Multikey.spec.js +++ b/test/Ed25519Multikey.spec.js @@ -151,6 +151,23 @@ describe('Ed25519Multikey', () => { expect(await imported.export({publicKey: true, secretKey: true})) .to.eql(exported); }); + it('should import with `@context` array', async () => { + // Encoding returns a 64 byte uint8array, seed needs to be 32 bytes + const seedBytes = (new TextEncoder()).encode(seed).slice(0, 32); + const keyPair = await Ed25519Multikey.generate({ + seed: seedBytes, controller: 'did:example:1234' + }); + const exported = await keyPair.export({ + publicKey: true, secretKey: true + }); + const imported = await Ed25519Multikey.from({ + ...exported, + '@context': [{}, exported['@context']] + }); + + expect(await imported.export({publicKey: true, secretKey: true})) + .to.eql(exported); + }); it('should load `publicKeyJwk`', async () => { const jwk = { crv: 'Ed25519',