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

specify protocol version in getSupportedProfiles #295

Merged
merged 8 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 1 addition & 11 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MediaType, ProtocolVersion } from '../constants';

Check warning on line 1 in src/iden3comm/handlers/auth.ts

View workflow job for this annotation

GitHub Actions / build

'ProtocolVersion' is defined but never used
import { IProofService } from '../../proof/proof-service';
import { PROTOCOL_MESSAGE_TYPE } from '../constants';

Expand Down Expand Up @@ -492,17 +492,7 @@
if (profile?.length) {
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
const supportedMediaTypes: MediaType[] = [];
for (const acceptProfile of profile) {
// 1. check protocol version
const { protocolVersion, env } = parseAcceptProfile(acceptProfile);
const responseTypeVersion = Number(responseType.split('/').at(-2));
if (
protocolVersion !== ProtocolVersion.V1 ||
(protocolVersion === ProtocolVersion.V1 &&
(responseTypeVersion < 1 || responseTypeVersion >= 2))
) {
continue;
}
// 2. check packer support
const { env } = parseAcceptProfile(acceptProfile);
if (this._packerMgr.isProfileSupported(env, acceptProfile)) {
supportedMediaTypes.push(env);
}
Expand Down
21 changes: 18 additions & 3 deletions src/iden3comm/packers/jws.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { BasicMessage, IPacker, JWSPackerParams } from '../types';
import { AcceptJwsAlgorithms, MediaType, SUPPORTED_PUBLIC_KEY_TYPES } from '../constants';
import {
AcceptJwsAlgorithms,
MediaType,
ProtocolVersion,
SUPPORTED_PUBLIC_KEY_TYPES
} from '../constants';
import { extractPublicKeyBytes, resolveVerificationMethods } from '../utils/did';
import { keyPath, KMS } from '../../kms/';

Expand Down Expand Up @@ -105,12 +110,18 @@ export class JWSPacker implements IPacker {

/** {@inheritDoc IPacker.getSupportedProfiles} */
getSupportedProfiles(): string[] {
return [`env=${this.mediaType()}&alg=${this.getSupportedAlgorithms().join(',')}`];
return this.getSupportedProtocolVersions().map(
(v) => `${v};env=${this.mediaType()};alg=${this.getSupportedAlgorithms().join(',')}`
);
}

/** {@inheritDoc IPacker.isProfileSupported} */
isProfileSupported(profile: string) {
const { env, circuits, alg } = parseAcceptProfile(profile);
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);

if (!this.getSupportedProtocolVersions().includes(protocolVersion)) {
return false;
}
if (env !== this.mediaType()) {
return false;
}
Expand All @@ -129,6 +140,10 @@ export class JWSPacker implements IPacker {
return [AcceptJwsAlgorithms.ES256K, AcceptJwsAlgorithms.ES256KR];
}

private getSupportedProtocolVersions(): ProtocolVersion[] {
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
return [ProtocolVersion.V1];
}

private async resolveDidDoc(from: string) {
let didDocument: DIDDocument;
try {
Expand Down
14 changes: 11 additions & 3 deletions src/iden3comm/packers/plain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BasicMessage, IPacker } from '../types';
import { MediaType } from '../constants';
import { MediaType, ProtocolVersion } from '../constants';
import { byteDecoder, byteEncoder } from '../../utils';
import { parseAcceptProfile } from '../utils';

Expand Down Expand Up @@ -57,12 +57,16 @@ export class PlainPacker implements IPacker {

/** {@inheritDoc IPacker.getSupportedProfiles} */
getSupportedProfiles(): string[] {
return [`env=${this.mediaType()}`];
return this.getSupportedProtocolVersions().map((v) => `${v};env=${this.mediaType()}`);
}

/** {@inheritDoc IPacker.isProfileSupported} */
isProfileSupported(profile: string) {
const { env, circuits, alg } = parseAcceptProfile(profile);
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);

if (!this.getSupportedProtocolVersions().includes(protocolVersion)) {
return false;
}
if (env !== this.mediaType()) {
return false;
}
Expand All @@ -77,4 +81,8 @@ export class PlainPacker implements IPacker {

return true;
}

private getSupportedProtocolVersions(): ProtocolVersion[] {
return [ProtocolVersion.V1];
}
}
24 changes: 17 additions & 7 deletions src/iden3comm/packers/zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ErrStateVerificationFailed,
ErrUnknownCircuitID
} from '../errors';
import { AcceptAuthCircuits, AcceptJwzAlgorithms, MediaType } from '../constants';
import { AcceptAuthCircuits, AcceptJwzAlgorithms, MediaType, ProtocolVersion } from '../constants';
import { byteDecoder, byteEncoder } from '../../utils';
import { DEFAULT_AUTH_VERIFY_DELAY } from '../constants';
import { parseAcceptProfile } from '../utils';
Expand Down Expand Up @@ -178,16 +178,22 @@ export class ZKPPacker implements IPacker {

/** {@inheritDoc IPacker.getSupportedProfiles} */
getSupportedProfiles(): string[] {
return [
`env=${this.mediaType()}&alg=${this.getSupportedAlgorithms().join(
','
)}&circuitIds=${this.getSupportedCircuitIds().join(',')}`
];
return this.getSupportedProtocolVersions().map(
(v) =>
`${v};env=${this.mediaType()};alg=${this.getSupportedAlgorithms().join(
','
)};circuitIds=${this.getSupportedCircuitIds().join(',')}`
);
}

/** {@inheritDoc IPacker.isProfileSupported} */
isProfileSupported(profile: string) {
const { env, circuits, alg } = parseAcceptProfile(profile);
const { protocolVersion, env, circuits, alg } = parseAcceptProfile(profile);

if (!this.getSupportedProtocolVersions().includes(protocolVersion)) {
return false;
}

if (env !== this.mediaType()) {
return false;
}
Expand All @@ -209,6 +215,10 @@ export class ZKPPacker implements IPacker {
private getSupportedCircuitIds(): AcceptAuthCircuits[] {
return [AcceptAuthCircuits.AuthV2];
}

private getSupportedProtocolVersions(): ProtocolVersion[] {
return [ProtocolVersion.V1];
}
}

const verifySender = async (token: Token, msg: BasicMessage): Promise<void> => {
Expand Down
26 changes: 15 additions & 11 deletions tests/handlers/discover-protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('discovery-protocol', () => {
expect(disclosures[0][DiscoverFeatureQueryType.FeatureType]).to.be.eq(
DiscoveryProtocolFeatureType.Accept
);
expect(disclosures[0].id).to.be.eq('env=application/iden3comm-plain-json');
expect(disclosures[0].id).to.be.eq('iden3comm/v1;env=application/iden3comm-plain-json');
});

it('jws and plain message accept disclosures', async () => {
Expand All @@ -72,8 +72,10 @@ describe('discovery-protocol', () => {
DiscoveryProtocolFeatureType.Accept
);
const disclosureIds = disclosures.map((d) => d.id);
expect(disclosureIds).to.include('env=application/iden3comm-plain-json');
expect(disclosureIds).to.include('env=application/iden3comm-signed-json&alg=ES256K,ES256K-R');
expect(disclosureIds).to.include('iden3comm/v1;env=application/iden3comm-plain-json');
expect(disclosureIds).to.include(
'iden3comm/v1;env=application/iden3comm-signed-json;alg=ES256K,ES256K-R'
);
});

it('zkp and plain message accept disclosures', async () => {
Expand All @@ -95,9 +97,9 @@ describe('discovery-protocol', () => {
DiscoveryProtocolFeatureType.Accept
);
const disclosureIds = disclosures.map((d) => d.id);
expect(disclosureIds).to.include('env=application/iden3comm-plain-json');
expect(disclosureIds).to.include('iden3comm/v1;env=application/iden3comm-plain-json');
expect(disclosureIds).to.include(
'env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2'
'iden3comm/v1;env=application/iden3-zkp-json;alg=groth16;circuitIds=authV2'
);
});

Expand All @@ -123,11 +125,13 @@ describe('discovery-protocol', () => {
DiscoveryProtocolFeatureType.Accept
);
const disclosureIds = disclosures.map((d) => d.id);
expect(disclosureIds).to.include('env=application/iden3comm-plain-json');
expect(disclosureIds).to.include('iden3comm/v1;env=application/iden3comm-plain-json');
expect(disclosureIds).to.include(
'iden3comm/v1;env=application/iden3-zkp-json;alg=groth16;circuitIds=authV2'
);
expect(disclosureIds).to.include(
'env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2'
'iden3comm/v1;env=application/iden3comm-signed-json;alg=ES256K,ES256K-R'
);
expect(disclosureIds).to.include('env=application/iden3comm-signed-json&alg=ES256K,ES256K-R');
});

it('zkp, jws and plain message accept disclosures with exact match', async () => {
Expand All @@ -140,7 +144,7 @@ describe('discovery-protocol', () => {
const acceptQueryMessageWithMatch = createDiscoveryFeatureQueryMessage([
{
[DiscoverFeatureQueryType.FeatureType]: DiscoveryProtocolFeatureType.Accept,
match: 'env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2'
match: 'iden3comm/v1;env=application/iden3-zkp-json;alg=groth16;circuitIds=authV2'
}
]);

Expand All @@ -153,7 +157,7 @@ describe('discovery-protocol', () => {
DiscoveryProtocolFeatureType.Accept
);
expect(disclosures[0].id).to.include(
'env=application/iden3-zkp-json&alg=groth16&circuitIds=authV2'
'iden3comm/v1;env=application/iden3-zkp-json;alg=groth16;circuitIds=authV2'
);
});

Expand Down Expand Up @@ -306,7 +310,7 @@ describe('discovery-protocol', () => {
DiscoveryProtocolFeatureType.Protocol
);
const disclosureIds = disclosures.map((d) => d.id);
expect(disclosureIds).to.include('env=application/iden3comm-plain-json');
expect(disclosureIds).to.include('iden3comm/v1;env=application/iden3comm-plain-json');
expect(disclosureIds).to.include(PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE);
expect(disclosureIds).to.include(PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_REQUEST_MESSAGE_TYPE);
});
Expand Down
6 changes: 6 additions & 0 deletions tests/iden3comm/jws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DIDResolutionResult } from 'did-resolver';
import { ES256KSigner } from 'did-jwt';
import { DID, getChainId, Id } from '@iden3/js-iden3-core';
import { Hex } from '@iden3/js-crypto';
import { MediaType } from '../../src/iden3comm/constants';

const didExample = {
'@context': [
Expand Down Expand Up @@ -217,4 +218,9 @@ describe('jws packer tests', () => {
const data = await packer.unpack(tokenBytes);
expect(data).to.not.be.undefined;
});

it('test getSupportedProfiles', async () => {
const [accept] = await packer.getSupportedProfiles();
expect(accept).to.be.eq(`iden3comm/v1;env=${MediaType.SignedMessage};alg=ES256K,ES256K-R`);
});
});
12 changes: 12 additions & 0 deletions tests/iden3comm/packageManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DataPrepareHandlerFunc,
PackageManager,
PlainPacker,
VerificationHandlerFunc,
ZKPPacker
} from '../../src/iden3comm/index';
Expand Down Expand Up @@ -70,6 +71,17 @@ describe('tests packageManager with ZKP Packer', () => {
expect(senderDID.string()).to.deep.equal(unpackedMessage.from);
expect(byteDecoder.decode(msgBytes)).to.deep.equal(JSON.stringify(unpackedMessage));
});

it('test getSupportedProfiles', async () => {
const pm = new PackageManager();
pm.registerPackers([new ZKPPacker(new Map(), new Map()), new PlainPacker()]);
const supportedProfiles = await pm.getSupportedProfiles();
expect(supportedProfiles.length).to.be.eq(2);
expect(supportedProfiles).to.include(
`iden3comm/v1;env=${MediaType.ZKPMessage};alg=groth16;circuitIds=authV2`
);
expect(supportedProfiles).to.include(`iden3comm/v1;env=${MediaType.PlainMessage}`);
});
});

const createFetchCredentialMessage = (typ: MediaType, from: DID, to: DID) => {
Expand Down
10 changes: 9 additions & 1 deletion tests/iden3comm/zkp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DID } from '@iden3/js-iden3-core';
import { Token, ProvingMethodAlg } from '@iden3/js-jwz';
import { ZKPPackerParams } from '../../src/iden3comm/types';
import { AuthV2PubSignals } from '../../src/circuits';
import { PROTOCOL_MESSAGE_TYPE } from '../../src/iden3comm/constants';
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../../src/iden3comm/constants';
import { byteDecoder, byteEncoder } from '../../src';
import { expect } from 'chai';
import { initZKPPacker } from './mock/proving';
Expand Down Expand Up @@ -41,4 +41,12 @@ describe('zkp packer tests', () => {

expect(PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE).to.deep.equal(iden3msg.type);
});

it('test getSupportedProfiles', async () => {
const p = await initZKPPacker();
const [accept] = await p.getSupportedProfiles();
Kolezhniuk marked this conversation as resolved.
Show resolved Hide resolved
expect(accept).to.be.eq(
`iden3comm/v1;env=${MediaType.ZKPMessage};alg=groth16;circuitIds=authV2`
);
});
});
Loading