From 119ad862add6e75a60f753df54649b706c13f65e Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Fri, 1 Nov 2024 15:14:30 +0200 Subject: [PATCH] resolve comments --- src/iden3comm/handlers/payment.ts | 46 +++++++++++-------------- src/iden3comm/types/protocol/payment.ts | 20 ++++++++++- tests/handlers/payment.test.ts | 5 +-- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/iden3comm/handlers/payment.ts b/src/iden3comm/handlers/payment.ts index 85a3383e..203db4cf 100644 --- a/src/iden3comm/handlers/payment.ts +++ b/src/iden3comm/handlers/payment.ts @@ -18,7 +18,9 @@ import { MultiChainPaymentConfig, PaymentMessage, PaymentRequestInfo, - PaymentRequestMessage + PaymentRequestMessage, + PaymentRequestTypeUnion, + PaymentTypeUnion } from '../types/protocol/payment'; import { PaymentFeatures, @@ -82,7 +84,7 @@ export type PaymentRailsChainInfo = { amount: bigint; currency: SupportedCurrencies | string; chainId: string; - expirationDate?: Date; + expirationDate?: string; features?: PaymentFeatures[]; type: | PaymentRequestDataType.Iden3PaymentRailsRequestV1 @@ -100,7 +102,7 @@ export type PaymentRailsChainInfo = { export function createPayment( sender: DID, receiver: DID, - payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[] + payments: PaymentTypeUnion[] ): PaymentMessage { const uuidv4 = uuid.v4(); const request: PaymentMessage = { @@ -174,9 +176,7 @@ export interface IPaymentHandler { /** @beta PaymentRequestMessageHandlerOptions represents payment-request handler options */ export type PaymentRequestMessageHandlerOptions = { - paymentHandler: ( - data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1 - ) => Promise; + paymentHandler: (data: PaymentRequestTypeUnion) => Promise; /* selected payment nonce (for Iden3PaymentRequestCryptoV1 type it should be equal to Payment id field) */ @@ -187,10 +187,7 @@ export type PaymentRequestMessageHandlerOptions = { /** @beta PaymentHandlerOptions represents payment handler options */ export type PaymentHandlerOptions = { paymentRequest: PaymentRequestMessage; - paymentValidationHandler: ( - txId: string, - data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1 - ) => Promise; + paymentValidationHandler: (txId: string, data: PaymentRequestTypeUnion) => Promise; }; /** @beta PaymentHandlerParams represents payment handler params */ @@ -277,18 +274,16 @@ export class PaymentHandler const senderDID = DID.parse(paymentRequest.to); const receiverDID = DID.parse(paymentRequest.from); - const payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[] = []; + const payments: PaymentTypeUnion[] = []; for (let i = 0; i < paymentRequest.body.payments.length; i++) { - const paymentReq = paymentRequest.body.payments[i]; - const dataArray = Array.isArray(paymentReq.data) ? paymentReq.data : [paymentReq.data]; - const selectedPayment = - dataArray.length === 1 - ? dataArray[0] - : dataArray.find((p) => { - return p.type === PaymentRequestDataType.Iden3PaymentRequestCryptoV1 - ? p.id === ctx.nonce - : p.nonce === ctx.nonce; - }); + const { data } = paymentRequest.body.payments[i]; + const selectedPayment = Array.isArray(data) + ? data.find((p) => { + return p.type === PaymentRequestDataType.Iden3PaymentRequestCryptoV1 + ? p.id === ctx.nonce + : p.nonce === ctx.nonce; + }) + : data; if (!selectedPayment) { throw new Error(`failed request. no payment in request for nonce ${ctx.nonce}`); @@ -441,9 +436,8 @@ export class PaymentHandler if (type === PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 && !tokenAddress) { throw new Error(`failed request. no token address for currency ${currency}`); } - const expiration = expirationDate - ? expirationDate.getTime() - : new Date(new Date().setHours(new Date().getHours() + 1)).getTime(); + const expiration = + expirationDate ?? new Date(new Date().setHours(new Date().getHours() + 1)).toISOString(); const typeUrl = `https://schema.iden3.io/core/json/${type}.json`; const typesFetchResult = await fetch(typeUrl); const types = await typesFetchResult.json(); @@ -499,7 +493,7 @@ export class PaymentHandler recipient, amount: amount.toString(), currency, - expirationDate: new Date(expiration).toISOString(), + expirationDate: expiration, nonce: nonce.toString(), metadata: '0x', proof @@ -515,7 +509,7 @@ export class PaymentHandler recipient, amount: amount.toString(), currency, - expirationDate: new Date(expiration).toISOString(), + expirationDate: expiration, nonce: nonce.toString(), metadata: '0x', proof diff --git a/src/iden3comm/types/protocol/payment.ts b/src/iden3comm/types/protocol/payment.ts index 47e59186..accc7d44 100644 --- a/src/iden3comm/types/protocol/payment.ts +++ b/src/iden3comm/types/protocol/payment.ts @@ -91,7 +91,7 @@ export type PaymentMessage = BasicMessage & { /** @beta PaymentMessageBody is struct the represents body for payment message */ export type PaymentMessageBody = { - payments: (Iden3PaymentCryptoV1 | Iden3PaymentRailsV1 | Iden3PaymentRailsERC20V1)[]; + payments: PaymentTypeUnion[]; }; /** @beta Iden3PaymentCryptoV1 is struct the represents payment info for payment */ @@ -137,3 +137,21 @@ export type MultiChainPaymentConfig = { address: string; }[]; }; + +/** + * @beta + * PaymentRequestTypeUnion is a type of supported payment request types + */ +export type PaymentRequestTypeUnion = + | Iden3PaymentRequestCryptoV1 + | Iden3PaymentRailsRequestV1 + | Iden3PaymentRailsERC20RequestV1; + +/** + * @beta + * PaymentTypeUnion is a type of supported payment types + */ +export type PaymentTypeUnion = + | Iden3PaymentCryptoV1 + | Iden3PaymentRailsV1 + | Iden3PaymentRailsERC20V1; diff --git a/tests/handlers/payment.test.ts b/tests/handlers/payment.test.ts index 12256b32..2b7eccd1 100644 --- a/tests/handlers/payment.test.ts +++ b/tests/handlers/payment.test.ts @@ -47,7 +47,8 @@ import { Iden3PaymentRailsERC20RequestV1, Iden3PaymentRailsRequestV1, Iden3PaymentRequestCryptoV1, - PaymentRequestInfo + PaymentRequestInfo, + PaymentRequestTypeUnion } from '../../src/iden3comm/types/protocol/payment'; import { Contract, ethers, JsonRpcProvider } from 'ethers'; import fetchMock from '@gr2m/fetch-mock'; @@ -380,7 +381,7 @@ describe('payment-request handler', () => { const paymentValidationIntegrationHandlerFunc = async ( txId: string, - data: Iden3PaymentRequestCryptoV1 | Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1 + data: PaymentRequestTypeUnion ): Promise => { const rpcProvider = new JsonRpcProvider(RPC_URL); const tx = await rpcProvider.getTransaction(txId);