Skip to content

Commit b7eb85e

Browse files
remove currency from multichain payment, refactor config (#293)
* remove currency from multichain payment, refactor config
1 parent dad41d7 commit b7eb85e

File tree

6 files changed

+114
-165
lines changed

6 files changed

+114
-165
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@0xpolygonid/js-sdk",
3-
"version": "1.24.1",
3+
"version": "1.25.0",
44
"description": "SDK to work with Polygon ID",
55
"main": "dist/node/cjs/index.js",
66
"module": "dist/node/esm/index.js",

src/iden3comm/handlers/payment.ts

+34-63
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ import {
3030
PaymentFeatures,
3131
PaymentRequestDataType,
3232
PaymentType,
33-
SupportedCurrencies,
3433
SupportedPaymentProofType
3534
} from '../../verifiable';
36-
import { Signer, ethers, parseEther, parseUnits } from 'ethers';
35+
import { Signer, ethers } from 'ethers';
3736
import { Resolvable } from 'did-resolver';
3837
import { verifyExpiresTime } from './common';
3938

@@ -85,23 +84,19 @@ export async function verifyEIP712TypedData(
8584
data: Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1,
8685
resolver: Resolvable
8786
): Promise<string> {
88-
const convertedAmount = await convertPaymentAmount(
89-
data.amount,
90-
data.currency as SupportedCurrencies
91-
);
9287
const paymentData =
9388
data.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
9489
? {
9590
recipient: data.recipient,
96-
amount: convertedAmount,
91+
amount: data.amount,
9792
expirationDate: getUnixTimestamp(new Date(data.expirationDate)),
9893
nonce: data.nonce,
9994
metadata: '0x'
10095
}
10196
: {
10297
tokenAddress: data.tokenAddress,
10398
recipient: data.recipient,
104-
amount: convertedAmount,
99+
amount: data.amount,
105100
expirationDate: getUnixTimestamp(new Date(data.expirationDate)),
106101
nonce: data.nonce,
107102
metadata: '0x'
@@ -134,34 +129,6 @@ export async function verifyEIP712TypedData(
134129
throw new Error(`failed request. no matching verificationMethod`);
135130
}
136131

137-
export async function convertPaymentAmount(
138-
amount: string,
139-
currency: SupportedCurrencies
140-
): Promise<bigint> {
141-
let convertedAmount = 0n;
142-
switch (currency) {
143-
case SupportedCurrencies.ETH:
144-
case SupportedCurrencies.POL:
145-
case SupportedCurrencies.MATIC:
146-
convertedAmount = parseEther(amount.toString());
147-
break;
148-
case SupportedCurrencies.ETH_WEI:
149-
convertedAmount = parseUnits(amount.toString(), 'wei');
150-
break;
151-
case SupportedCurrencies.ETH_GWEI:
152-
convertedAmount = parseUnits(amount.toString(), 'gwei');
153-
break;
154-
case SupportedCurrencies.USDC:
155-
case SupportedCurrencies.USDT: {
156-
convertedAmount = parseUnits(amount.toString(), 6);
157-
break;
158-
}
159-
default:
160-
throw new Error(`failed request. unsupported currency ${currency}`);
161-
}
162-
return convertedAmount;
163-
}
164-
165132
/**
166133
* @beta
167134
* PaymentRailsInfo represents payment info for payment rails
@@ -172,23 +139,19 @@ export type PaymentRailsInfo = {
172139
context: string;
173140
}[];
174141
description?: string;
175-
chains: PaymentRailsChainInfo[];
142+
options: PaymentRailsOptionInfo[];
176143
};
177144

178145
/**
179146
* @beta
180-
* PaymentRailsChainInfo represents chain info for payment rails
147+
* PaymentRailsOptionInfo represents option info for payment rails
181148
*/
182-
export type PaymentRailsChainInfo = {
149+
export type PaymentRailsOptionInfo = {
150+
optionId: string;
151+
chainId: string;
183152
nonce: bigint;
184153
amount: string;
185-
currency: SupportedCurrencies | string;
186-
chainId: string;
187154
expirationDate?: Date;
188-
features?: PaymentFeatures[];
189-
type:
190-
| PaymentRequestDataType.Iden3PaymentRailsRequestV1
191-
| PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1;
192155
};
193156

194157
/**
@@ -530,43 +493,47 @@ export class PaymentHandler
530493
for (let i = 0; i < payments.length; i++) {
531494
const { credentials, description } = payments[i];
532495
const dataArr: (Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1)[] = [];
533-
for (let j = 0; j < payments[i].chains.length; j++) {
534-
const { features, nonce, amount, currency, chainId, expirationDate, type } =
535-
payments[i].chains[j];
496+
for (let j = 0; j < payments[i].options.length; j++) {
497+
const { nonce, amount, chainId, optionId, expirationDate } = payments[i].options[j];
536498

537499
const multiChainConfig = this._params.multiChainPaymentConfig?.find(
538500
(c) => c.chainId === chainId
539501
);
540502
if (!multiChainConfig) {
541503
throw new Error(`failed request. no config for chain ${chainId}`);
542504
}
543-
const { recipient, paymentContract, erc20TokenAddressArr } = multiChainConfig;
505+
const { recipient, paymentRails, options } = multiChainConfig;
544506

545-
const tokenAddress = erc20TokenAddressArr.find((t) => t.symbol === currency)?.address;
546-
if (type === PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 && !tokenAddress) {
547-
throw new Error(`failed request. no token address for currency ${currency}`);
507+
const option = options.find((t) => t.id === optionId);
508+
if (!option) {
509+
throw new Error(`failed request. no option for id ${optionId}`);
510+
}
511+
if (
512+
option.type === PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1 &&
513+
!option.contractAddress
514+
) {
515+
throw new Error(`failed request. no token address for option id ${optionId}`);
548516
}
549517
const expirationDateRequired =
550518
expirationDate ?? new Date(new Date().setHours(new Date().getHours() + 1));
551-
const typeUrl = `https://schema.iden3.io/core/json/${type}.json`;
519+
const typeUrl = `https://schema.iden3.io/core/json/${option.type}.json`;
552520
const typesFetchResult = await fetch(typeUrl);
553521
const types = await typesFetchResult.json();
554522
delete types.EIP712Domain;
555523

556-
const convertedAmount = await convertPaymentAmount(amount, currency as SupportedCurrencies);
557524
const paymentData =
558-
type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
525+
option.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
559526
? {
560527
recipient,
561-
amount: convertedAmount,
528+
amount: amount,
562529
expirationDate: getUnixTimestamp(expirationDateRequired),
563530
nonce,
564531
metadata: '0x'
565532
}
566533
: {
567-
tokenAddress,
534+
tokenAddress: option.contractAddress,
568535
recipient,
569-
amount: convertedAmount,
536+
amount: amount,
570537
expirationDate: getUnixTimestamp(expirationDateRequired),
571538
nonce,
572539
metadata: '0x'
@@ -576,7 +543,7 @@ export class PaymentHandler
576543
name: 'MCPayment',
577544
version: '1.0.0',
578545
chainId,
579-
verifyingContract: paymentContract
546+
verifyingContract: paymentRails
580547
};
581548
const signature = await signer.signTypedData(domain, types, paymentData);
582549
const proof: EthereumEip712Signature2021[] = [
@@ -597,21 +564,25 @@ export class PaymentHandler
597564
const d: Iden3PaymentRailsRequestV1 = {
598565
type: PaymentRequestDataType.Iden3PaymentRailsRequestV1,
599566
'@context': [
600-
`https://schema.iden3.io/core/jsonld/payment.jsonld#${type}`,
567+
`https://schema.iden3.io/core/jsonld/payment.jsonld#${option.type}`,
601568
'https://w3id.org/security/suites/eip712sig-2021/v1'
602569
],
603570
recipient,
604571
amount: amount.toString(),
605-
currency,
606572
expirationDate: expirationDateRequired.toISOString(),
607573
nonce: nonce.toString(),
608574
metadata: '0x',
609575
proof
610576
};
611577
dataArr.push(
612-
type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
578+
option.type === PaymentRequestDataType.Iden3PaymentRailsRequestV1
613579
? d
614-
: { ...d, type, tokenAddress: tokenAddress || '', features: features || [] }
580+
: {
581+
...d,
582+
type: option.type,
583+
tokenAddress: option.contractAddress || '',
584+
features: option.features || []
585+
}
615586
);
616587
}
617588
paymentRequestInfo.push({

src/iden3comm/types/protocol/payment.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export type Iden3PaymentRailsRequestV1 = {
5252
'@context': string | (string | object)[];
5353
recipient: string;
5454
amount: string;
55-
currency: SupportedCurrencies | string;
5655
expirationDate: string;
5756
nonce: string;
5857
metadata: string;
@@ -130,11 +129,15 @@ export type Iden3PaymentRailsERC20V1 = {
130129
/** @beta MultiChainPaymentConfig is struct that represents payments contracts information for different chains */
131130
export type MultiChainPaymentConfig = {
132131
chainId: string;
133-
paymentContract: string;
132+
paymentRails: string;
134133
recipient: string;
135-
erc20TokenAddressArr: {
136-
symbol: string;
137-
address: string;
134+
options: {
135+
id: string;
136+
type:
137+
| PaymentRequestDataType.Iden3PaymentRailsRequestV1
138+
| PaymentRequestDataType.Iden3PaymentRailsERC20RequestV1;
139+
contractAddress?: string;
140+
features?: PaymentFeatures[];
138141
}[];
139142
};
140143

src/verifiable/constants.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,15 @@ export enum SupportedPaymentProofType {
147147
/**
148148
* Media types for Payment supported currencies
149149
* @beta
150+
* @deprecated
150151
* @enum {string}
151152
*/
152153
export enum SupportedCurrencies {
153154
ETH = 'ETH',
154155
ETH_WEI = 'ETHWEI',
155156
ETH_GWEI = 'ETHGWEI',
156157
MATIC = 'MATIC',
157-
POL = 'POL',
158-
USDT = 'USDT',
159-
USDC = 'USDC'
158+
POL = 'POL'
160159
}
161160

162161
/**

0 commit comments

Comments
 (0)