11import { ZeroAddress } from 'ethers' ;
2- import { NULL_ADDRESS } from 'iexec/utils' ;
3- import { createBulkOrder } from '../../utils/createBulkOrder.js' ;
2+ import { DATASET_INFINITE_VOLUME , NULL_ADDRESS } from 'iexec/utils' ;
43import {
54 ValidationError ,
65 WorkflowError ,
@@ -49,7 +48,7 @@ export const grantAccess = async ({
4948 protectedData,
5049 authorizedApp,
5150 authorizedUser,
52- pricePerAccess,
51+ pricePerAccess = 0 ,
5352 numberOfAccess,
5453 allowBulk = false ,
5554 onStatusUpdate = ( ) => { } ,
@@ -65,10 +64,10 @@ export const grantAccess = async ({
6564 const vAuthorizedUser = addressOrEnsSchema ( )
6665 . label ( 'authorizedUser' )
6766 . validateSync ( authorizedUser ) ;
68- const vPricePerAccess = positiveIntegerStringSchema ( )
67+ let vPricePerAccess = positiveIntegerStringSchema ( )
6968 . label ( 'pricePerAccess' )
7069 . validateSync ( pricePerAccess ) ;
71- const vNumberOfAccess = positiveStrictIntegerStringSchema ( )
70+ let vNumberOfAccess = positiveStrictIntegerStringSchema ( )
7271 . label ( 'numberOfAccess' )
7372 . validateSync ( numberOfAccess ) ;
7473 const vAllowBulk = booleanSchema ( ) . label ( 'allowBulk' ) . validateSync ( allowBulk ) ;
@@ -77,6 +76,25 @@ export const grantAccess = async ({
7776 onStatusUpdate
7877 ) ;
7978
79+ // Validate consistency between allowBulk, pricePerAccess and numberOfAccess
80+ if ( vAllowBulk ) {
81+ if ( vPricePerAccess && vPricePerAccess !== '0' ) {
82+ throw new ValidationError (
83+ 'allowBulk requires pricePerAccess to be 0 or undefined'
84+ ) ;
85+ }
86+ vPricePerAccess = '0' ;
87+ if (
88+ vNumberOfAccess &&
89+ vNumberOfAccess !== DATASET_INFINITE_VOLUME . toString ( )
90+ ) {
91+ throw new ValidationError (
92+ `allowBulk requires numberOfAccess to be ${ DATASET_INFINITE_VOLUME . toString ( ) } or undefined`
93+ ) ;
94+ }
95+ vNumberOfAccess = DATASET_INFINITE_VOLUME . toString ( ) ;
96+ }
97+
8098 if ( vAuthorizedApp && isEnsTest ( vAuthorizedApp ) ) {
8199 const resolved = await iexec . ens . resolveName ( vAuthorizedApp ) ;
82100 if ( ! resolved ) {
@@ -127,95 +145,52 @@ export const grantAccess = async ({
127145 } ) ;
128146 }
129147
130- let datasetorder ;
131-
132- if ( vAllowBulk ) {
133- vOnStatusUpdate ( {
134- title : 'CREATE_BULK_ORDER' ,
135- isDone : false ,
136- } ) ;
148+ vOnStatusUpdate ( {
149+ title : 'CREATE_DATASET_ORDER' ,
150+ isDone : false ,
151+ } ) ;
137152
138- datasetorder = await createBulkOrder ( iexec , {
153+ const datasetorder = await iexec . order
154+ . createDatasetorder ( {
139155 dataset : vProtectedData ,
140- app : vAuthorizedApp ,
141- requester : vAuthorizedUser ,
156+ apprestrict : vAuthorizedApp ,
157+ requesterrestrict : vAuthorizedUser ,
158+ datasetprice : vPricePerAccess ,
159+ volume : vNumberOfAccess ,
142160 tag,
143- } ) . catch ( ( e ) => {
144- throw new WorkflowError ( {
145- message : 'Failed to create bulk order' ,
146- errorCause : e ,
147- } ) ;
148- } ) ;
149-
150- vOnStatusUpdate ( {
151- title : 'CREATE_BULK_ORDER' ,
152- isDone : true ,
153- } ) ;
154-
155- vOnStatusUpdate ( {
156- title : 'PUBLISH_BULK_ORDER' ,
157- isDone : false ,
158- } ) ;
159-
160- await iexec . order . publishDatasetorder ( datasetorder ) . catch ( ( e ) => {
161- handleIfProtocolError ( e ) ;
161+ } )
162+ . then ( ( datasetorderTemplate ) =>
163+ iexec . order . signDatasetorder ( datasetorderTemplate )
164+ )
165+ . catch ( ( e ) => {
162166 throw new WorkflowError ( {
163- message : 'Failed to publish bulk order ' ,
167+ message : 'Failed to sign data access ' ,
164168 errorCause : e ,
165169 } ) ;
166170 } ) ;
167171
168- vOnStatusUpdate ( {
169- title : 'PUBLISH_BULK_ORDER' ,
170- isDone : true ,
171- } ) ;
172- } else {
173- vOnStatusUpdate ( {
174- title : 'CREATE_DATASET_ORDER' ,
175- isDone : false ,
176- } ) ;
177-
178- datasetorder = await iexec . order
179- . createDatasetorder ( {
180- dataset : vProtectedData ,
181- apprestrict : vAuthorizedApp ,
182- requesterrestrict : vAuthorizedUser ,
183- datasetprice : vPricePerAccess ,
184- volume : vNumberOfAccess ,
185- tag,
186- } )
187- . then ( ( datasetorderTemplate ) =>
188- iexec . order . signDatasetorder ( datasetorderTemplate )
189- )
190- . catch ( ( e ) => {
191- throw new WorkflowError ( {
192- message : 'Failed to sign data access' ,
193- errorCause : e ,
194- } ) ;
195- } ) ;
172+ vOnStatusUpdate ( {
173+ title : 'CREATE_DATASET_ORDER' ,
174+ isDone : true ,
175+ } ) ;
196176
197- vOnStatusUpdate ( {
198- title : 'CREATE_DATASET_ORDER ' ,
199- isDone : true ,
200- } ) ;
177+ vOnStatusUpdate ( {
178+ title : 'PUBLISH_DATASET_ORDER ' ,
179+ isDone : false ,
180+ } ) ;
201181
202- vOnStatusUpdate ( {
203- title : 'PUBLISH_DATASET_ORDER' ,
204- isDone : false ,
182+ await iexec . order . publishDatasetorder ( datasetorder ) . catch ( ( e ) => {
183+ handleIfProtocolError ( e ) ;
184+ throw new WorkflowError ( {
185+ message : 'Failed to publish data access' ,
186+ errorCause : e ,
205187 } ) ;
188+ } ) ;
206189
207- await iexec . order . publishDatasetorder ( datasetorder ) . catch ( ( e ) => {
208- handleIfProtocolError ( e ) ;
209- throw new WorkflowError ( {
210- message : 'Failed to publish data access' ,
211- errorCause : e ,
212- } ) ;
213- } ) ;
190+ vOnStatusUpdate ( {
191+ title : 'PUBLISH_DATASET_ORDER' ,
192+ isDone : true ,
193+ } ) ;
214194
215- vOnStatusUpdate ( {
216- title : 'PUBLISH_DATASET_ORDER' ,
217- isDone : true ,
218- } ) ;
219- }
220195 return formatGrantedAccess ( datasetorder , parseInt ( datasetorder . volume ) ) ;
221196} ;
0 commit comments