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 to as string[] #303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import {
IProtocolMessageHandler
} from './message-handler';
import { parseAcceptProfile } from '../utils';
import {
getIden3CommSingleRecipient,
Iden3DIDcommCompatibilityOptions
} from '../types/protocol/common';

/**
* Options to pass to createAuthorizationRequest function
Expand Down Expand Up @@ -99,7 +103,7 @@ export type AuthResponseHandlerOptions = StateVerificationOpts &
BasicHandlerOptions & {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks loke we can extend BasicHandlerOptions with Iden3DIDcommCompatibilityOptions. Than it's automatically adds Iden3DIDcommCompatibilityOptions where we had BasicHandlerOptions.

// acceptedProofGenerationDelay is the period of time in milliseconds that a generated proof remains valid.
acceptedProofGenerationDelay?: number;
};
} & Iden3DIDcommCompatibilityOptions;

/**
* Interface that allows the processing of the authorization request in the raw format for given identifier
Expand Down Expand Up @@ -161,13 +165,13 @@ export interface IAuthHandler {
type AuthReqOptions = {
senderDid: DID;
mediaType?: MediaType;
};
} & Iden3DIDcommCompatibilityOptions;

type AuthRespOptions = {
request: AuthorizationRequestMessage;
acceptedStateTransitionDelay?: number;
acceptedProofGenerationDelay?: number;
};
} & Iden3DIDcommCompatibilityOptions;

export type AuthMessageHandlerOptions = AuthReqOptions | AuthRespOptions;
/**
Expand All @@ -180,7 +184,7 @@ export type AuthMessageHandlerOptions = AuthReqOptions | AuthRespOptions;
export type AuthHandlerOptions = BasicHandlerOptions & {
mediaType: MediaType;
packerOptions?: JWSPackerParams;
};
} & Iden3DIDcommCompatibilityOptions;

/**
*
Expand Down Expand Up @@ -252,7 +256,7 @@ export class AuthHandler
throw new Error('Invalid message type for authorization request');
}
// override sender did if it's explicitly specified in the auth request
const to = authRequest.to ? DID.parse(authRequest.to) : ctx.senderDid;
const to = getIden3CommSingleRecipient(authRequest) ?? ctx.senderDid;
const guid = uuid.v4();

if (!authRequest.from) {
Expand Down Expand Up @@ -285,7 +289,7 @@ export class AuthHandler
scope: responseScope
},
from: to.string(),
to: authRequest.from
to: ctx.multipleRecipientsFormat ? [authRequest.from] : authRequest.from
};
}

Expand Down Expand Up @@ -317,7 +321,8 @@ export class AuthHandler

const authResponse = await this.handleAuthRequest(authRequest, {
senderDid: did,
mediaType: opts.mediaType
mediaType: opts.mediaType,
multipleRecipientsFormat: opts.multipleRecipientsFormat
});

const msgBytes = byteEncoder.encode(JSON.stringify(authResponse));
Expand Down Expand Up @@ -351,7 +356,12 @@ export class AuthHandler
throw new Error('message for signing from request is not presented in response');
}

if (request.from !== response.to) {
const to = getIden3CommSingleRecipient(response);
if (!to) {
throw new Error('auth response must have valid recipient');
}

if (request.from !== to.string()) {
throw new Error(
`sender of the request is not a target of response - expected ${request.from}, given ${response.to}`
);
Expand Down Expand Up @@ -443,7 +453,8 @@ export class AuthHandler
const authResp = (await this.handleAuthResponse(response, {
request,
acceptedStateTransitionDelay: opts?.acceptedStateTransitionDelay,
acceptedProofGenerationDelay: opts?.acceptedProofGenerationDelay
acceptedProofGenerationDelay: opts?.acceptedProofGenerationDelay,
multipleRecipientsFormat: opts?.multipleRecipientsFormat
})) as AuthorizationResponseMessage;

return { request, response: authResp };
Expand Down
21 changes: 15 additions & 6 deletions src/iden3comm/handlers/contract-request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { CircuitId } from '../../circuits/models';
import { IProofService } from '../../proof/proof-service';
import { PROTOCOL_MESSAGE_TYPE } from '../constants';
import { BasicMessage, IPackageManager, ZeroKnowledgeProofResponse } from '../types';
import {
BasicMessage,
getIden3CommSingleRecipient,
Iden3DIDcommCompatibilityOptions,
IPackageManager,
ZeroKnowledgeProofResponse
} from '../types';
import { ContractInvokeRequest, ContractInvokeResponse } from '../types/protocol/contract-request';
import { DID, ChainIds, getUnixTimestamp } from '@iden3/js-iden3-core';
import { FunctionSignatures, IOnChainZKPVerifier } from '../../storage';
Expand Down Expand Up @@ -52,7 +58,7 @@ export type ContractMessageHandlerOptions = {
senderDid: DID;
ethSigner: Signer;
challenge?: bigint;
};
} & Iden3DIDcommCompatibilityOptions;

/**
*
Expand Down Expand Up @@ -98,7 +104,7 @@ export class ContractRequestHandler
case PROTOCOL_MESSAGE_TYPE.CONTRACT_INVOKE_REQUEST_MESSAGE_TYPE: {
const ciMessage = message as ContractInvokeRequest;
const txHashResponsesMap = await this.handleContractInvoke(ciMessage, ctx);
return this.createContractInvokeResponse(ciMessage, txHashResponsesMap);
return this.createContractInvokeResponse(ciMessage, txHashResponsesMap, ctx);
}
default:
return super.handle(message, ctx);
Expand Down Expand Up @@ -193,14 +199,17 @@ export class ContractRequestHandler
*/
private async createContractInvokeResponse(
request: ContractInvokeRequest,
txHashToZkpResponseMap: Map<string, ZeroKnowledgeProofResponse[]>
txHashToZkpResponseMap: Map<string, ZeroKnowledgeProofResponse[]>,
ctx: ContractMessageHandlerOptions
): Promise<ContractInvokeResponse> {
const recipient = getIden3CommSingleRecipient(request);
const target = request.from && ctx.multipleRecipientsFormat ? [request.from] : request.from;
const contractInvokeResponse: ContractInvokeResponse = {
id: request.id,
thid: request.thid,
type: PROTOCOL_MESSAGE_TYPE.CONTRACT_INVOKE_RESPONSE_MESSAGE_TYPE,
from: request.to,
to: request.from,
from: recipient ? recipient.string() : undefined,
to: target,
Comment on lines 208 to +212
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe it's make sense to create helper function createBasicMsg(..params)
that hides logic of multiple to passing thid, mediaTyp etc.
And when you creates concrete message you could do something like that

    ...createBasicMsg(thid, to, isMultipleRecipients..., typ),
body: {...}
type: ide3MsgType
}

body: {
transaction_data: request.body.transaction_data,
scope: []
Expand Down
19 changes: 11 additions & 8 deletions src/iden3comm/handlers/credential-proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
CredentialOffer,
CredentialsOfferMessage,
DIDDocument,
getIden3CommSingleRecipient,
Iden3DIDcommCompatibilityOptions,
IPackageManager,
PackerParams
} from '../types';
Expand Down Expand Up @@ -143,7 +145,7 @@ export interface ICredentialProposalHandler {
}

/** @beta ProposalRequestHandlerOptions represents proposal-request handler options */
export type ProposalRequestHandlerOptions = BasicHandlerOptions;
export type ProposalRequestHandlerOptions = BasicHandlerOptions & Iden3DIDcommCompatibilityOptions;

/** @beta ProposalHandlerOptions represents proposal handler options */
export type ProposalHandlerOptions = BasicHandlerOptions & {
Expand Down Expand Up @@ -216,7 +218,8 @@ export class CredentialProposalHandler
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ctx?: ProposalRequestHandlerOptions
): Promise<ProposalMessage | CredentialsOfferMessage | undefined> {
if (!proposalRequest.to) {
const to = getIden3CommSingleRecipient(proposalRequest);
if (!to) {
throw new Error(`failed request. empty 'to' field`);
}

Expand Down Expand Up @@ -246,7 +249,7 @@ export class CredentialProposalHandler
},
type: cred.type,
context: cred.context,
allowedIssuers: [proposalRequest.to]
allowedIssuers: [to?.string()]
Copy link
Collaborator

Choose a reason for hiding this comment

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

allowedIssuers: [to.string()]

});
} catch (e) {
if ((e as Error).message !== 'no credential satisfied query') {
Expand All @@ -266,8 +269,8 @@ export class CredentialProposalHandler
url: this._params.agentUrl,
credentials: []
},
from: proposalRequest.to,
to: proposalRequest.from
from: to.string(),
to: ctx?.multipleRecipientsFormat ? [proposalRequest.from] : proposalRequest.from
};
}

Expand Down Expand Up @@ -295,8 +298,8 @@ export class CredentialProposalHandler
body: {
proposals: []
},
from: proposalRequest.to,
to: proposalRequest.from
from: to.string(),
to: ctx?.multipleRecipientsFormat ? [proposalRequest.from] : proposalRequest.from
};
}
proposalMessage.body?.proposals.push(proposal);
Expand Down Expand Up @@ -329,7 +332,7 @@ export class CredentialProposalHandler
}

const senderDID = DID.parse(proposalRequest.from);
const message = await this.handleProposalRequestMessage(proposalRequest);
const message = await this.handleProposalRequestMessage(proposalRequest, opts);
const response = byteEncoder.encode(JSON.stringify(message));

const packerOpts =
Expand Down
20 changes: 14 additions & 6 deletions src/iden3comm/handlers/discovery-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../constants';

import { BasicMessage, IPackageManager, ProtocolMessage } from '../types';
import {
BasicMessage,
getIden3CommSingleRecipient,
Iden3DIDcommCompatibilityOptions,
IPackageManager,
ProtocolMessage
} from '../types';

import * as uuid from 'uuid';
import {
Expand Down Expand Up @@ -42,7 +48,7 @@ export interface DiscoveryProtocolOptions {
*/
export type DiscoveryProtocolHandlerOptions = BasicHandlerOptions & {
disclosureExpiresDate?: Date;
};
} & Iden3DIDcommCompatibilityOptions;

/**
* @beta
Expand All @@ -54,7 +60,7 @@ export function createDiscoveryFeatureQueryMessage(
queries: DiscoverFeatureQuery[],
opts?: {
from?: string;
to?: string;
to?: string | string[];
expires_time?: number;
}
): DiscoverFeatureQueriesMessage {
Expand Down Expand Up @@ -85,7 +91,7 @@ export function createDiscoveryFeatureDiscloseMessage(
disclosures: DiscoverFeatureDisclosure[],
opts?: {
from?: string;
to?: string;
to?: string | string[];
expires_time?: number;
}
): DiscoverFeatureDiscloseMessage {
Expand Down Expand Up @@ -192,10 +198,12 @@ export class DiscoveryProtocolHandler
disclosures.push(...this.handleQuery(query));
}

const to = getIden3CommSingleRecipient(message);
const from = opts?.multipleRecipientsFormat && message.from ? [message.from] : message.from;
return Promise.resolve(
createDiscoveryFeatureDiscloseMessage(disclosures, {
to: message.from,
from: message.to,
to: from,
from: to?.string(),
expires_time: opts?.disclosureExpiresDate
? getUnixTimestamp(opts.disclosureExpiresDate)
: undefined
Expand Down
Loading