-
Notifications
You must be signed in to change notification settings - Fork 39
Suppoort attachments in the protocol #299
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
base: main
Are you sure you want to change the base?
Changes from all commits
c95c0b8
51b1d99
325567c
765da64
d54ee3c
facc33b
c64d7bc
f4c864f
a95be40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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'; | ||
|
@@ -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 */ | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
}; | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]; | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`); | ||
} | ||
|
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; | ||
}; |
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}`); | ||
} | ||
}; |
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; | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.