Skip to content

Commit

Permalink
Allow @context array values in multikeys.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Mar 18, 2024
1 parent 11bd557 commit 001849e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}".`);
}
}
17 changes: 17 additions & 0 deletions test/Ed25519Multikey.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 001849e

Please sign in to comment.