Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for publicKeyMultibase. #94

Merged
merged 1 commit into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ' +
Copy link
Contributor

Choose a reason for hiding this comment

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

related to w3c/did#731

'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);
};