Skip to content

Commit

Permalink
extend BasicMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Apr 3, 2024
1 parent 917fc7a commit 072a79a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
43 changes: 25 additions & 18 deletions src/iden3comm/handlers/credential-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ export interface ICredentialProposalHandler {
}

/** @beta ProposalRequestHandlerOptions represents proposal-request handler options */
export type ProposalRequestHandlerOptions = {
export type ProposalRequestHandlerOptions = object;

/** @beta PackerParams represents params for packer */
export type PackerParams = {
mediaType: MediaType;
packerOptions?: JWSPackerParams;
};
Expand All @@ -137,6 +140,13 @@ export type ProposalHandlerOptions = {
proposalRequest?: ProposalRequestMessage;
};

/** @beta CredentialProposalHandlerParams represents credential proposal handler params */
export type CredentialProposalHandlerParams = {
agentUrl: string;
proposalResolverFn: (context: string, type: string) => Promise<Proposal>;
packerParams: PackerParams;
};

/**
*
* Allows to process ProposalRequest protocol message
Expand All @@ -149,15 +159,14 @@ export class CredentialProposalHandler implements ICredentialProposalHandler {
* @beta Creates an instance of CredentialProposalHandler.
* @param {IPackageManager} _packerMgr - package manager to unpack message envelope
* @param {IIdentityWallet} _identityWallet - identity wallet
* @param {(context: string, type: string) => Promise<Proposal>} _proposalResolverFn - resolves Proposal by context and type
* @param {CredentialProposalHandlerParams} _params - credential proposal handler params
*
*/

constructor(
private readonly _packerMgr: IPackageManager,
private readonly _identityWallet: IIdentityWallet,
private readonly _agentUrl: string,
private readonly _proposalResolverFn: (context: string, type: string) => Promise<Proposal>
private readonly _params: CredentialProposalHandlerParams
) {}

/**
Expand All @@ -177,15 +186,13 @@ export class CredentialProposalHandler implements ICredentialProposalHandler {
*/
async handleProposalRequest(
request: Uint8Array,
//eslint-disable-next-line @typescript-eslint/no-unused-vars
opts?: ProposalRequestHandlerOptions
): Promise<Uint8Array> {
if (!opts) {
opts = {
mediaType: MediaType.PlainMessage
};
}

if (opts.mediaType === MediaType.SignedMessage && !opts.packerOptions) {
if (
this._params.packerParams.mediaType === MediaType.SignedMessage &&
!this._params.packerParams.packerOptions
) {
throw new Error(`jws packer options are required for ${MediaType.SignedMessage}`);
}

Expand Down Expand Up @@ -228,11 +235,11 @@ export class CredentialProposalHandler implements ICredentialProposalHandler {
if (!credOfferMessage) {
credOfferMessage = {
id: guid,
typ: opts.mediaType,
typ: this._params.packerParams.mediaType,
type: PROTOCOL_MESSAGE_TYPE.CREDENTIAL_OFFER_MESSAGE_TYPE,
thid: proposalRequest.thid ?? guid,
body: {
url: this._agentUrl,
url: this._params.agentUrl,
credentials: []
},
from: proposalRequest.to,
Expand All @@ -250,15 +257,15 @@ export class CredentialProposalHandler implements ICredentialProposalHandler {
}

// credential not found in the wallet, prepare proposal protocol message
const proposal = await this._proposalResolverFn(cred.context, cred.type);
const proposal = await this._params.proposalResolverFn(cred.context, cred.type);
if (!proposal) {
throw new Error(`can't resolve Proposal for type: ${cred.type}, context: ${cred.context}`);
}
if (!proposalMessage) {
const guid = uuid.v4();
proposalMessage = {
id: guid,
typ: opts.mediaType,
typ: this._params.packerParams.mediaType,
type: PROTOCOL_MESSAGE_TYPE.PROPOSAL_MESSAGE_TYPE,
thid: proposalRequest.thid ?? guid,
body: {
Expand All @@ -275,13 +282,13 @@ export class CredentialProposalHandler implements ICredentialProposalHandler {
const response = byteEncoder.encode(JSON.stringify(credOfferMessage ?? proposalMessage));

const packerOpts =
opts.mediaType === MediaType.SignedMessage
? opts.packerOptions
this._params.packerParams.mediaType === MediaType.SignedMessage
? this._params.packerParams.packerOptions
: {
provingMethodAlg: proving.provingMethodGroth16AuthV2Instance.methodAlg
};

return this._packerMgr.pack(opts.mediaType, response, {
return this._packerMgr.pack(this._params.packerParams.mediaType, response, {
senderDID,
...packerOpts
});
Expand Down
19 changes: 3 additions & 16 deletions src/iden3comm/types/protocol/proposal-request.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { JSONObject, ProtocolMessage } from '../';
import { MediaType } from '../../constants';
import { BasicMessage, JSONObject } from '../';

/** @beta ProposalRequestMessage is struct the represents proposal-request message */
export type ProposalRequestMessage = {
id: string;
typ?: MediaType;
type: ProtocolMessage;
thid?: string;
export type ProposalRequestMessage = BasicMessage & {
body?: ProposalRequestMessageBody;
from?: string;
to?: string;
};

/** @beta ProposalRequestMessageBody is struct the represents body for proposal-request */
Expand All @@ -20,14 +13,8 @@ export type ProposalRequestMessageBody = {
};

/** @beta ProposalMessage is struct the represents proposal message */
export type ProposalMessage = {
id: string;
typ?: MediaType;
type: ProtocolMessage;
thid?: string;
export type ProposalMessage = BasicMessage & {
body?: ProposalMessageBody;
from?: string;
to?: string;
};

/** @beta ProposalMessageBody is struct the represents body for proposal message */
Expand Down
13 changes: 7 additions & 6 deletions tests/handlers/credential-proposal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {

import { expect } from 'chai';
import path from 'path';
import { PROTOCOL_MESSAGE_TYPE } from '../../src/iden3comm/constants';
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../../src/iden3comm/constants';
import { DID } from '@iden3/js-iden3-core';

describe('proposal-request handler', () => {
Expand Down Expand Up @@ -87,12 +87,13 @@ describe('proposal-request handler', () => {
proofService.generateAuthV2Inputs.bind(proofService),
proofService.verifyState.bind(proofService)
);
proposalRequestHandler = new CredentialProposalHandler(
packageMgr,
idWallet,
proposalRequestHandler = new CredentialProposalHandler(packageMgr, idWallet, {
agentUrl,
proposalResolverFn
);
proposalResolverFn,
packerParams: {
mediaType: MediaType.PlainMessage
}
});

const userIdentity = await createIdentity(idWallet, {
seed: SEED_USER
Expand Down

0 comments on commit 072a79a

Please sign in to comment.