Skip to content

Commit

Permalink
Add support for publicKeyMultibase.
Browse files Browse the repository at this point in the history
  • Loading branch information
msporny committed May 16, 2021
1 parent 5be2490 commit 9ddb948
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ const generateDidCorePropertiesTests = (
});
});

it('5.2 Verification Methods - The publicKeyBase58 property is ' +
it('5.2 Verification Methods - The publicKeyMultibase property is ' +
'OPTIONAL. This feature is non-normative. If present, the value MUST be a' +
'string representation of a [BASE58] encoded public key.', async () => {
'string representation of a [MULTIBASE] encoded public key.', async () => {
const verificationMethods = getAllVerificationMethods(didDocument);
verificationMethods.forEach(vm => {
const {publicKeyBase58} = vm;
if(publicKeyBase58) {
expect(publicKeyBase58).toBeBase58String();
const {publicKeyMultibase} = vm;
if(publicKeyMultibase) {
expect(publicKeyMultibase).toBeMultibaseString();
}
});
});
Expand Down Expand Up @@ -141,16 +141,25 @@ const generateDidCorePropertiesTests = (
it('5.2.1 Verification Material - A verification method MUST NOT ' +
'contain multiple verification material properties for the same ' +
'material. For example, expressing key material in a verification method ' +
'using both publicKeyJwk and publicKeyBase58 at the same time is ' +
'using both publicKeyJwk and publicKeyMultibase at the same time is ' +
'prohibited.',
async () => {
const verificationMethods = getAllVerificationMethods(didDocument);
verificationMethods.forEach(vm => {
const {publicKeyBase58} = vm;
const {publicKeyMultibase} = vm;
const {publicKeyJwk} = vm;
if(publicKeyBase58 !== undefined && publicKeyJwk !== undefined) {
throw new Error('Both publicKeyJwk and publicKeyBase58 are ' +
'defined at the same time.');
let count = 0;
count =
(publicKeyBase58 === undefined) ? count : count + 1;
count =
(publicKeyMultibase === undefined) ? count : count + 1;
count =
(publicKeyJwk === undefined) ? count : count + 1;

if(count > 1) {
throw new Error('Multiple key material values are ' +
'defined for the same verification method.');
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
import predicate from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeMultibaseString', 'received', '') +
'\n\n' +
'Expected value to not be a Multibase string received:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeMultibaseString', 'received', '') +
'\n\n' +
'Expected value to be a Multibase string:\n' +
` ${printExpected('a Multibase string')}` +
'Received:\n' +
` ${printReceived(received)}`;

export default {
toBeMultibaseString: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
},

isMultibaseString: obj => {
return predicate(obj);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import each from 'jest-each';

import matcher from './';

expect.extend(matcher);

describe('.toBeMultibaseString', () => {
each([
["z98RNr73hGzPakwdrkjdk7BSn5Xd3ZbaVgJX122hqZAaMv19p"],
]).test('passes when given the item is Multibase String', given => {
expect(given).toBeMultibaseString();
});
});

describe('.not.toBeMultibaseString', () => {
each([
[false],
[""],
["JKJKJK"],
["5!@)"],
[0],
[{}],
[[]],
[() => {}],
[null],
[undefined],
[NaN]
]).test('passes when the item is not a Multibase String: %s', given => {
expect(given).not.toBeMultibaseString();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default expected => {
const multibaseRegex = /^[079fFvVtTbBcChkKzZmMuUp].+$/;
if (! (typeof expected === 'string' || expected instanceof String) ) {
return false;
}
return multibaseRegex.test(expected);
};

0 comments on commit 9ddb948

Please sign in to comment.