@@ -30,10 +30,9 @@ import {
30
30
PaymentFeatures ,
31
31
PaymentRequestDataType ,
32
32
PaymentType ,
33
- SupportedCurrencies ,
34
33
SupportedPaymentProofType
35
34
} from '../../verifiable' ;
36
- import { Signer , ethers , parseEther , parseUnits } from 'ethers' ;
35
+ import { Signer , ethers } from 'ethers' ;
37
36
import { Resolvable } from 'did-resolver' ;
38
37
import { verifyExpiresTime } from './common' ;
39
38
@@ -85,23 +84,19 @@ export async function verifyEIP712TypedData(
85
84
data : Iden3PaymentRailsRequestV1 | Iden3PaymentRailsERC20RequestV1 ,
86
85
resolver : Resolvable
87
86
) : Promise < string > {
88
- const convertedAmount = await convertPaymentAmount (
89
- data . amount ,
90
- data . currency as SupportedCurrencies
91
- ) ;
92
87
const paymentData =
93
88
data . type === PaymentRequestDataType . Iden3PaymentRailsRequestV1
94
89
? {
95
90
recipient : data . recipient ,
96
- amount : convertedAmount ,
91
+ amount : data . amount ,
97
92
expirationDate : getUnixTimestamp ( new Date ( data . expirationDate ) ) ,
98
93
nonce : data . nonce ,
99
94
metadata : '0x'
100
95
}
101
96
: {
102
97
tokenAddress : data . tokenAddress ,
103
98
recipient : data . recipient ,
104
- amount : convertedAmount ,
99
+ amount : data . amount ,
105
100
expirationDate : getUnixTimestamp ( new Date ( data . expirationDate ) ) ,
106
101
nonce : data . nonce ,
107
102
metadata : '0x'
@@ -134,34 +129,6 @@ export async function verifyEIP712TypedData(
134
129
throw new Error ( `failed request. no matching verificationMethod` ) ;
135
130
}
136
131
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
-
165
132
/**
166
133
* @beta
167
134
* PaymentRailsInfo represents payment info for payment rails
@@ -172,23 +139,19 @@ export type PaymentRailsInfo = {
172
139
context : string ;
173
140
} [ ] ;
174
141
description ?: string ;
175
- chains : PaymentRailsChainInfo [ ] ;
142
+ options : PaymentRailsOptionInfo [ ] ;
176
143
} ;
177
144
178
145
/**
179
146
* @beta
180
- * PaymentRailsChainInfo represents chain info for payment rails
147
+ * PaymentRailsOptionInfo represents option info for payment rails
181
148
*/
182
- export type PaymentRailsChainInfo = {
149
+ export type PaymentRailsOptionInfo = {
150
+ optionId : string ;
151
+ chainId : string ;
183
152
nonce : bigint ;
184
153
amount : string ;
185
- currency : SupportedCurrencies | string ;
186
- chainId : string ;
187
154
expirationDate ?: Date ;
188
- features ?: PaymentFeatures [ ] ;
189
- type :
190
- | PaymentRequestDataType . Iden3PaymentRailsRequestV1
191
- | PaymentRequestDataType . Iden3PaymentRailsERC20RequestV1 ;
192
155
} ;
193
156
194
157
/**
@@ -530,43 +493,47 @@ export class PaymentHandler
530
493
for ( let i = 0 ; i < payments . length ; i ++ ) {
531
494
const { credentials, description } = payments [ i ] ;
532
495
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 ] ;
536
498
537
499
const multiChainConfig = this . _params . multiChainPaymentConfig ?. find (
538
500
( c ) => c . chainId === chainId
539
501
) ;
540
502
if ( ! multiChainConfig ) {
541
503
throw new Error ( `failed request. no config for chain ${ chainId } ` ) ;
542
504
}
543
- const { recipient, paymentContract , erc20TokenAddressArr } = multiChainConfig ;
505
+ const { recipient, paymentRails , options } = multiChainConfig ;
544
506
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 } ` ) ;
548
516
}
549
517
const expirationDateRequired =
550
518
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` ;
552
520
const typesFetchResult = await fetch ( typeUrl ) ;
553
521
const types = await typesFetchResult . json ( ) ;
554
522
delete types . EIP712Domain ;
555
523
556
- const convertedAmount = await convertPaymentAmount ( amount , currency as SupportedCurrencies ) ;
557
524
const paymentData =
558
- type === PaymentRequestDataType . Iden3PaymentRailsRequestV1
525
+ option . type === PaymentRequestDataType . Iden3PaymentRailsRequestV1
559
526
? {
560
527
recipient,
561
- amount : convertedAmount ,
528
+ amount : amount ,
562
529
expirationDate : getUnixTimestamp ( expirationDateRequired ) ,
563
530
nonce,
564
531
metadata : '0x'
565
532
}
566
533
: {
567
- tokenAddress,
534
+ tokenAddress : option . contractAddress ,
568
535
recipient,
569
- amount : convertedAmount ,
536
+ amount : amount ,
570
537
expirationDate : getUnixTimestamp ( expirationDateRequired ) ,
571
538
nonce,
572
539
metadata : '0x'
@@ -576,7 +543,7 @@ export class PaymentHandler
576
543
name : 'MCPayment' ,
577
544
version : '1.0.0' ,
578
545
chainId,
579
- verifyingContract : paymentContract
546
+ verifyingContract : paymentRails
580
547
} ;
581
548
const signature = await signer . signTypedData ( domain , types , paymentData ) ;
582
549
const proof : EthereumEip712Signature2021 [ ] = [
@@ -597,21 +564,25 @@ export class PaymentHandler
597
564
const d : Iden3PaymentRailsRequestV1 = {
598
565
type : PaymentRequestDataType . Iden3PaymentRailsRequestV1 ,
599
566
'@context' : [
600
- `https://schema.iden3.io/core/jsonld/payment.jsonld#${ type } ` ,
567
+ `https://schema.iden3.io/core/jsonld/payment.jsonld#${ option . type } ` ,
601
568
'https://w3id.org/security/suites/eip712sig-2021/v1'
602
569
] ,
603
570
recipient,
604
571
amount : amount . toString ( ) ,
605
- currency,
606
572
expirationDate : expirationDateRequired . toISOString ( ) ,
607
573
nonce : nonce . toString ( ) ,
608
574
metadata : '0x' ,
609
575
proof
610
576
} ;
611
577
dataArr . push (
612
- type === PaymentRequestDataType . Iden3PaymentRailsRequestV1
578
+ option . type === PaymentRequestDataType . Iden3PaymentRailsRequestV1
613
579
? d
614
- : { ...d , type, tokenAddress : tokenAddress || '' , features : features || [ ] }
580
+ : {
581
+ ...d ,
582
+ type : option . type ,
583
+ tokenAddress : option . contractAddress || '' ,
584
+ features : option . features || [ ]
585
+ }
615
586
) ;
616
587
}
617
588
paymentRequestInfo . push ( {
0 commit comments