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

fix didVerify #34

Merged
merged 1 commit into from
Jan 18, 2023
Merged
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
30 changes: 18 additions & 12 deletions packages/verify/src/didVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
// SPDX-License-Identifier: Apache-2.0

import type { HexString } from '@zcloak/crypto/types';
import type { DidDocument, DidUrl } from '@zcloak/did-resolver/types';
import type { DidDocument, DidUrl, VerificationMethodType } from '@zcloak/did-resolver/types';

import { u8aToU8a } from '@polkadot/util';

import { decodeMultibase, ed25519Verify, secp256k1Verify } from '@zcloak/crypto';
import { ed25519Verify, secp256k1Verify } from '@zcloak/crypto';
import { helpers } from '@zcloak/did';
import { DidResolver } from '@zcloak/did-resolver';
import { defaultResolver } from '@zcloak/did-resolver/defaults';

const VERIFIERS: Record<
VerificationMethodType,
(message: Uint8Array, signature: HexString | Uint8Array, publicKey: Uint8Array) => boolean
> = {
EcdsaSecp256k1VerificationKey2019: secp256k1Verify,
Ed25519VerificationKey2020: ed25519Verify,
X25519KeyAgreementKey2019: () => false
};

/**
* @name didVerify
* @summary Verifies the signature on the supplied message.
Expand Down Expand Up @@ -58,18 +68,14 @@ export async function didVerify(
? await resolverOrDidDocument.resolve(didUrl)
: resolverOrDidDocument;

const finded = document.verificationMethod?.find((method) => {
return method.id;
});

if (!finded) return false;
const did = helpers.fromDidDocument(document);

const publicKey = decodeMultibase(finded.publicKeyMultibase);
for (const [, { publicKey, type }] of did.keyRelationship) {
const isTrue = VERIFIERS[type](messageU8a, signature, publicKey);

if (finded.type === 'EcdsaSecp256k1VerificationKey2019') {
return secp256k1Verify(messageU8a, signature, publicKey);
} else if (finded.type === 'Ed25519VerificationKey2020') {
return ed25519Verify(messageU8a, signature, publicKey);
if (isTrue) {
return isTrue;
}
}

return false;
Expand Down