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

Suppoort attachments in the protocol #299

Open
wants to merge 9 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@0xpolygonid/js-sdk",
"version": "1.27.0",
"version": "1.28.0",
"description": "SDK to work with Polygon ID",
"main": "dist/node/cjs/index.js",
"module": "dist/node/esm/index.js",
Expand Down
4 changes: 3 additions & 1 deletion src/iden3comm/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export const PROTOCOL_MESSAGE_TYPE = Object.freeze({
`${DIDCOMM_PROTOCOL}discover-features/2.0/queries` as const,
// DiscoveryProtocolDiscloseMessageType is type for didcomm discovery protocol disclose
DISCOVERY_PROTOCOL_DISCLOSE_MESSAGE_TYPE:
`${DIDCOMM_PROTOCOL}discover-features/2.0/disclose` as const
`${DIDCOMM_PROTOCOL}discover-features/2.0/disclose` as const,
TRANSPARENT_PAYMENT_INSTRUCTION_MESSAGE_TYPE:
`${IDEN3_PROTOCOL}credentials/0.1/transparent-payment-instruction` as const
vmidyllic marked this conversation as resolved.
Show resolved Hide resolved
});

/**
Expand Down
7 changes: 5 additions & 2 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
IPackageManager,
JWSPackerParams,
ZeroKnowledgeProofRequest,
JSONObject
JSONObject,
Attachment
} from '../types';
import { DID, getUnixTimestamp } from '@iden3/js-iden3-core';
import { proving } from '@iden3/js-jwz';
Expand All @@ -35,6 +36,7 @@ export type AuthorizationRequestCreateOptions = {
accept?: string[];
scope?: ZeroKnowledgeProofRequest[];
expires_time?: Date;
attachments?: Attachment[];
};

/**
Expand Down Expand Up @@ -84,7 +86,8 @@ export function createAuthorizationRequestWithMessage(
scope: opts?.scope ?? []
},
created_time: getUnixTimestamp(new Date()),
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined,
attachments: opts?.attachments
};
return request;
}
Expand Down
41 changes: 35 additions & 6 deletions src/iden3comm/handlers/credential-proposal.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { PROTOCOL_MESSAGE_TYPE, MediaType } from '../constants';
import {
Attachment,
BasicMessage,
CredentialOffer,
CredentialsOfferMessage,
DIDDocument,
IPackageManager,
PackerParams
PackerParams,
TransparentPaymentData,
TransparentPaymentInstructionMessage
} from '../types';

import { DID, getUnixTimestamp } from '@iden3/js-iden3-core';
import * as uuid from 'uuid';
import { proving } from '@iden3/js-jwz';
import {
Proposal,
ProposalRequestCredential,
ProposalRequestMessage,
ProposalMessage
ProposalMessage,
ProposalRequestCredential
} from '../types/protocol/proposal-request';
import { IIdentityWallet } from '../../identity';
import { byteEncoder } from '../../utils';
Expand All @@ -26,12 +29,14 @@ import {
IProtocolMessageHandler
} from './message-handler';
import { verifyExpiresTime } from './common';
import { getProtocolMessageTypeByGoalCode } from '../types/protocol/common';

/** @beta ProposalRequestCreationOptions represents proposal-request creation options */
export type ProposalRequestCreationOptions = {
credentials: ProposalRequestCredential[];
did_doc?: DIDDocument;
expires_time?: Date;
attachments?: Attachment[];
};

/** @beta ProposalCreationOptions represents proposal creation options */
Expand Down Expand Up @@ -62,7 +67,8 @@ export function createProposalRequest(
type: PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE,
body: opts,
created_time: getUnixTimestamp(new Date()),
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined,
attachments: opts.attachments
};
return request;
}
Expand Down Expand Up @@ -153,7 +159,11 @@ export type ProposalHandlerOptions = BasicHandlerOptions & {
/** @beta CredentialProposalHandlerParams represents credential proposal handler params */
export type CredentialProposalHandlerParams = {
agentUrl: string;
proposalResolverFn: (context: string, type: string) => Promise<Proposal>;
proposalResolverFn: (
context: string,
type: string,
opts?: { paymentInfo?: TransparentPaymentData }
) => Promise<Proposal>;
packerParams: PackerParams;
};

Expand Down Expand Up @@ -231,6 +241,20 @@ export class CredentialProposalHandler
let credOfferMessage: CredentialsOfferMessage | undefined = undefined;
let proposalMessage: ProposalMessage | undefined = undefined;

const paymentInstructionsMessages: TransparentPaymentInstructionMessage[] = (
proposalRequest.attachments ?? []
)
.flatMap((a) => a.data.json as TransparentPaymentInstructionMessage)
.filter(
(m) =>
m &&
m.body?.goal_code &&
getProtocolMessageTypeByGoalCode(m.body.goal_code) ===
PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE &&
m.to === proposalRequest.to && // issuer
(!m.body.paymentReference || m.body.paymentReference === proposalRequest.from) // user
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove user

);

for (let i = 0; i < proposalRequest.body.credentials.length; i++) {
const cred = proposalRequest.body.credentials[i];

Expand Down Expand Up @@ -280,8 +304,13 @@ export class CredentialProposalHandler
continue;
}

const paymentInfo = paymentInstructionsMessages.find((m) =>
m.body.credentials.find((c) => c.type === cred.type && c.context === cred.context)
);
// credential not found in the wallet, prepare proposal protocol message
const proposal = await this._params.proposalResolverFn(cred.context, cred.type);
const proposal = await this._params.proposalResolverFn(cred.context, cred.type, {
paymentInfo: paymentInfo?.body?.paymentData
Copy link
Collaborator

Choose a reason for hiding this comment

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

paymentMessage.body.payments[]

});
if (!proposal) {
throw new Error(`can't resolve Proposal for type: ${cred.type}, context: ${cred.context}`);
}
Expand Down
4 changes: 3 additions & 1 deletion src/iden3comm/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export * from './protocol/proposal-request';
export * from './protocol/payment';
export * from './protocol/accept-profile';
export * from './protocol/discovery-protocol';

export * from './protocol/attachment';
export * from './protocol/transparent-payment';
export * from './protocol/common';
export * from './packer';
export * from './models';
export * from './packageManager';
8 changes: 7 additions & 1 deletion src/iden3comm/types/packer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CircuitId } from '../../circuits';
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../constants';
import { DIDDocument, VerificationMethod } from 'did-resolver';
import { StateVerificationOpts } from './models';
import { Attachment } from './protocol/attachment';

/**
* Protocol message type
Expand Down Expand Up @@ -51,14 +52,19 @@ export type BasicMessage = {
to?: string;
created_time?: number;
expires_time?: number;
attachments?: Attachment[];
};

/**
* Basic message with all possible fields required
*/
export type RequiredBasicMessage = Omit<Required<BasicMessage>, 'created_time' | 'expires_time'> & {
export type RequiredBasicMessage = Omit<
Required<BasicMessage>,
'created_time' | 'expires_time' | 'attachments'
> & {
created_time?: number;
expires_time?: number;
attachments?: Attachment[];
};

/**
Expand Down
13 changes: 13 additions & 0 deletions src/iden3comm/types/protocol/attachment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MediaType } from '../../constants';
import { JsonDocumentObject } from '../packer';

export type Attachment = {
id: string;
description?: string;
media_type: MediaType;
data: AttachData;
};

export type AttachData = {
json: JsonDocumentObject;
};
31 changes: 31 additions & 0 deletions src/iden3comm/types/protocol/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';

/**
* Represents information about a credential schema.
*
* @property {string} type - The type of the credential schema.
* @property {string} context - The context in which the credential schema is used.
*/
export type CredentialSchemaInfo = {
volodymyr-basiuk marked this conversation as resolved.
Show resolved Hide resolved
type: string;
context: string;
};

/**
* Enum representing the goal codes used in the iden3 communication protocol.
*
* @enum {string}
* @property {string} ProposalRequest - Represents a proposal request in the iden3 communication protocol.
*/
export enum GoalCode {
ProposalRequest = 'iden3comm.credentials.v1-1.proposal-request'
}

export const getProtocolMessageTypeByGoalCode = (goalCode: GoalCode) => {
switch (goalCode) {
case GoalCode.ProposalRequest:
return PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE;
default:
throw new Error(`Unknown goal code ${goalCode}`);
}
};
6 changes: 2 additions & 4 deletions src/iden3comm/types/protocol/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SupportedPaymentProofType
} from '../../../verifiable';
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
import { CredentialSchemaInfo } from './common';

/** @beta PaymentRequestMessage is struct the represents payment-request message */
export type PaymentRequestMessage = BasicMessage & {
Expand All @@ -21,10 +22,7 @@ export type PaymentRequestMessageBody = {

/** @beta PaymentRequestInfo is struct the represents payment info for payment-request */
export type PaymentRequestInfo = {
credentials: {
type: string;
context: string;
}[];
credentials: CredentialSchemaInfo[];
data:
| Iden3PaymentRequestCryptoV1
| (
Expand Down
13 changes: 5 additions & 8 deletions src/iden3comm/types/protocol/proposal-request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { BasicMessage, DIDDocument, JsonDocumentObject } from '../';
import { BasicMessage, DIDDocument } from '../';
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
import { CredentialSchemaInfo } from './common';

/** @beta ProposalRequestMessage is struct the represents proposal-request message */
export type ProposalRequestMessage = BasicMessage & {
body: ProposalRequestMessageBody;
type: typeof PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE;
};

/** @beta ProposalRequestCredential is struct the represents proposal request credential */
export type ProposalRequestCredential = CredentialSchemaInfo;

/** @beta ProposalRequestMessageBody is struct the represents body for proposal-request */
export type ProposalRequestMessageBody = {
credentials: ProposalRequestCredential[];
metadata?: { type: string; data?: JsonDocumentObject };
did_doc?: DIDDocument;
};

Expand All @@ -25,12 +28,6 @@ export type ProposalMessageBody = {
proposals: Proposal[];
};

/** @beta ProposalRequestCredential is struct the represents proposal request credential */
export type ProposalRequestCredential = {
type: string;
context: string;
};

/** @beta Proposal is struct the represents proposal inside proposal protocol message */
export type Proposal = {
credentials: ProposalRequestCredential[];
Expand Down
28 changes: 28 additions & 0 deletions src/iden3comm/types/protocol/transparent-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// package protocol

import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
import { RequiredBasicMessage } from '../packer';
import { CredentialSchemaInfo, GoalCode } from './common';

export type TransparentPaymentInstructionMessage = RequiredBasicMessage & {
body: TransparentPaymentInstructionMessageBody;
type: typeof PROTOCOL_MESSAGE_TYPE.TRANSPARENT_PAYMENT_INSTRUCTION_MESSAGE_TYPE;
};

export type TransparentPaymentInstructionMessageBody = {
goal_code: GoalCode;
paymentReference?: string;
credentials: CredentialSchemaInfo[];
paymentData: TransparentPaymentData;
};

export type TransparentPaymentData = {
type: string;
signature: string;
recipient: string;
amount: string;
token?: string;
expiration: number;
nonce: number;
metadata?: string;
};
Loading