Skip to content

Commit ec638f0

Browse files
authored
Suppoort attachments in the protocol (#299)
* Support attachments in the sdk
1 parent 1bd5bb7 commit ec638f0

12 files changed

Lines changed: 199 additions & 24 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.32.5",
3+
"version": "1.33.0",
44
"description": "SDK to work with Polygon ID",
55
"source": "./src/index.ts",
66
"exports": {

src/iden3comm/handlers/auth.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
IPackageManager,
1111
JWSPackerParams,
1212
ZeroKnowledgeProofRequest,
13-
JSONObject
13+
JSONObject,
14+
Attachment
1415
} from '../types';
1516
import { DID, getUnixTimestamp } from '@iden3/js-iden3-core';
1617
import { proving } from '@iden3/js-jwz';
@@ -35,6 +36,7 @@ export type AuthorizationRequestCreateOptions = {
3536
accept?: string[];
3637
scope?: ZeroKnowledgeProofRequest[];
3738
expires_time?: Date;
39+
attachments?: Attachment[];
3840
};
3941

4042
/**
@@ -84,7 +86,8 @@ export function createAuthorizationRequestWithMessage(
8486
scope: opts?.scope ?? []
8587
},
8688
created_time: getUnixTimestamp(new Date()),
87-
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined
89+
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined,
90+
attachments: opts?.attachments
8891
};
8992
return request;
9093
}

src/iden3comm/handlers/credential-proposal.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PROTOCOL_MESSAGE_TYPE, MediaType } from '../constants';
22
import {
3+
Attachment,
34
BasicMessage,
45
CredentialOffer,
56
CredentialsOfferMessage,
@@ -13,9 +14,9 @@ import * as uuid from 'uuid';
1314
import { proving } from '@iden3/js-jwz';
1415
import {
1516
Proposal,
16-
ProposalRequestCredential,
1717
ProposalRequestMessage,
18-
ProposalMessage
18+
ProposalMessage,
19+
ProposalRequestCredential
1920
} from '../types/protocol/proposal-request';
2021
import { IIdentityWallet } from '../../identity';
2122
import { byteEncoder } from '../../utils';
@@ -32,6 +33,7 @@ export type ProposalRequestCreationOptions = {
3233
credentials: ProposalRequestCredential[];
3334
did_doc?: DIDDocument;
3435
expires_time?: Date;
36+
attachments?: Attachment[];
3537
};
3638

3739
/** @beta ProposalCreationOptions represents proposal creation options */
@@ -62,7 +64,8 @@ export function createProposalRequest(
6264
type: PROTOCOL_MESSAGE_TYPE.PROPOSAL_REQUEST_MESSAGE_TYPE,
6365
body: opts,
6466
created_time: getUnixTimestamp(new Date()),
65-
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined
67+
expires_time: opts?.expires_time ? getUnixTimestamp(opts.expires_time) : undefined,
68+
attachments: opts.attachments
6669
};
6770
return request;
6871
}
@@ -153,7 +156,11 @@ export type ProposalHandlerOptions = BasicHandlerOptions & {
153156
/** @beta CredentialProposalHandlerParams represents credential proposal handler params */
154157
export type CredentialProposalHandlerParams = {
155158
agentUrl: string;
156-
proposalResolverFn: (context: string, type: string) => Promise<Proposal>;
159+
proposalResolverFn: (
160+
context: string,
161+
type: string,
162+
opts?: { msg?: BasicMessage }
163+
) => Promise<Proposal>;
157164
packerParams: PackerParams;
158165
};
159166

@@ -281,7 +288,9 @@ export class CredentialProposalHandler
281288
}
282289

283290
// credential not found in the wallet, prepare proposal protocol message
284-
const proposal = await this._params.proposalResolverFn(cred.context, cred.type);
291+
const proposal = await this._params.proposalResolverFn(cred.context, cred.type, {
292+
msg: proposalRequest
293+
});
285294
if (!proposal) {
286295
throw new Error(`can't resolve Proposal for type: ${cred.type}, context: ${cred.context}`);
287296
}

src/iden3comm/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export * from './protocol/accept-profile';
1010
export * from './protocol/discovery-protocol';
1111
export * from './protocol/problem-report';
1212

13+
export * from './protocol/attachment';
14+
export * from './protocol/common';
1315
export * from './packer';
1416
export * from './models';
1517
export * from './packageManager';

src/iden3comm/types/packer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CircuitId } from '../../circuits';
55
import { MediaType, PROTOCOL_MESSAGE_TYPE } from '../constants';
66
import { DIDDocument, VerificationMethod } from 'did-resolver';
77
import { StateVerificationOpts } from './models';
8+
import { Attachment } from './protocol/attachment';
89

910
/**
1011
* Protocol message type
@@ -51,14 +52,19 @@ export type BasicMessage = {
5152
to?: string;
5253
created_time?: number;
5354
expires_time?: number;
55+
attachments?: Attachment[];
5456
};
5557

5658
/**
5759
* Basic message with all possible fields required
5860
*/
59-
export type RequiredBasicMessage = Omit<Required<BasicMessage>, 'created_time' | 'expires_time'> & {
61+
export type RequiredBasicMessage = Omit<
62+
Required<BasicMessage>,
63+
'created_time' | 'expires_time' | 'attachments'
64+
> & {
6065
created_time?: number;
6166
expires_time?: number;
67+
attachments?: Attachment[];
6268
};
6369

6470
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { JsonDocumentObject } from '../packer';
2+
3+
export type Attachment = {
4+
id: string;
5+
description?: string;
6+
media_type?: string;
7+
data: AttachData;
8+
};
9+
10+
export type AttachData = {
11+
json: JsonDocumentObject;
12+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Represents information about a credential schema.
3+
*
4+
* @property {string} type - The type of the credential schema.
5+
* @property {string} context - The context in which the credential schema is used.
6+
*/
7+
export type CredentialSchemaInfo = {
8+
type: string;
9+
context: string;
10+
};

src/iden3comm/types/protocol/payment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
SupportedPaymentProofType
77
} from '../../../verifiable';
88
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
9+
import { CredentialSchemaInfo } from './common';
910

1011
/** @beta PaymentRequestMessage is struct the represents payment-request message */
1112
export type PaymentRequestMessage = BasicMessage & {
@@ -23,10 +24,7 @@ export type PaymentRequestMessageBody = {
2324

2425
/** @beta PaymentRequestInfo is struct the represents payment info for payment-request */
2526
export type PaymentRequestInfo = {
26-
credentials: {
27-
type: string;
28-
context: string;
29-
}[];
27+
credentials: CredentialSchemaInfo[];
3028
data:
3129
| Iden3PaymentRequestCryptoV1
3230
| (

src/iden3comm/types/protocol/proposal-request.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import { BasicMessage, DIDDocument, JsonDocumentObject } from '../';
1+
import { BasicMessage, DIDDocument } from '../';
22
import { PROTOCOL_MESSAGE_TYPE } from '../../constants';
3+
import { CredentialSchemaInfo } from './common';
34

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

11+
/** @beta ProposalRequestCredential is struct the represents proposal request credential */
12+
export type ProposalRequestCredential = CredentialSchemaInfo;
13+
1014
/** @beta ProposalRequestMessageBody is struct the represents body for proposal-request */
1115
export type ProposalRequestMessageBody = {
1216
credentials: ProposalRequestCredential[];
13-
metadata?: { type: string; data?: JsonDocumentObject };
1417
did_doc?: DIDDocument;
1518
};
1619

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

28-
/** @beta ProposalRequestCredential is struct the represents proposal request credential */
29-
export type ProposalRequestCredential = {
30-
type: string;
31-
context: string;
32-
};
33-
3431
/** @beta Proposal is struct the represents proposal inside proposal protocol message */
3532
export type Proposal = {
3633
credentials: ProposalRequestCredential[];

0 commit comments

Comments
 (0)