-
Notifications
You must be signed in to change notification settings - Fork 61
/
Provider.ts
795 lines (758 loc) 路 24.7 KB
/
Provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
import Web3 from 'web3'
import fetch from 'cross-fetch'
import { LoggerInstance } from '../utils'
import {
Arweave,
FileInfo,
ComputeJob,
ComputeOutput,
ComputeAlgorithm,
ComputeAsset,
ComputeEnvironment,
ProviderInitialize,
ProviderComputeInitializeResults,
ServiceEndpoint,
UrlFile,
UserCustomParameters
} from '../@types'
export class Provider {
/**
* Returns the provider endpoints
* @return {Promise<ServiceEndpoint[]>}
*/
async getEndpoints(providerUri: string): Promise<any> {
try {
const endpoints = await this.getData(providerUri)
return await endpoints.json()
} catch (e) {
LoggerInstance.error('Finding the service endpoints failed:', e)
throw new Error('HTTP request failed calling Provider')
}
}
getEndpointURL(
servicesEndpoints: ServiceEndpoint[],
serviceName: string
): ServiceEndpoint {
if (!servicesEndpoints) return null
return servicesEndpoints.find((s) => s.serviceName === serviceName) as ServiceEndpoint
}
/**
* Returns the service endpoints that exist in provider.
* @param {any} endpoints
* @return {Promise<ServiceEndpoint[]>}
*/
public async getServiceEndpoints(providerEndpoint: string, endpoints: any) {
const serviceEndpoints: ServiceEndpoint[] = []
for (const i in endpoints.serviceEndpoints) {
const endpoint: ServiceEndpoint = {
serviceName: i,
method: endpoints.serviceEndpoints[i][0],
urlPath: providerEndpoint + endpoints.serviceEndpoints[i][1]
}
serviceEndpoints.push(endpoint)
}
return serviceEndpoints
}
/** Gets current nonce
* @param {string} providerUri provider uri address
* @param {string} consumerAddress Publisher address
* @param {AbortSignal} signal abort signal
* @param {string} providerEndpoints Identifier of the asset to be registered in ocean
* @param {string} serviceEndpoints document description object (DDO)=
* @return {Promise<string>} urlDetails
*/
public async getNonce(
providerUri: string,
consumerAddress: string,
signal?: AbortSignal,
providerEndpoints?: any,
serviceEndpoints?: ServiceEndpoint[]
): Promise<string> {
if (!providerEndpoints) {
providerEndpoints = await this.getEndpoints(providerUri)
}
if (!serviceEndpoints) {
serviceEndpoints = await this.getServiceEndpoints(providerUri, providerEndpoints)
}
const path = this.getEndpointURL(serviceEndpoints, 'nonce')
? this.getEndpointURL(serviceEndpoints, 'nonce').urlPath
: null
if (!path) return null
try {
const response = await fetch(path + `?userAddress=${consumerAddress}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
signal
})
return (await response.json()).nonce.toString()
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
public async signProviderRequest(
web3: Web3,
accountId: string,
message: string,
password?: string
): Promise<string> {
const consumerMessage = web3.utils.soliditySha3({
t: 'bytes',
v: web3.utils.utf8ToHex(message)
})
const isMetaMask =
web3 && web3.currentProvider && (web3.currentProvider as any).isMetaMask
if (isMetaMask)
return await web3.eth.personal.sign(consumerMessage, accountId, password)
else return await web3.eth.sign(consumerMessage, accountId)
}
/** Encrypt data using the Provider's own symmetric key
* @param {string} data data in json format that needs to be sent , it can either be a DDO or a File array
* @param {string} providerUri provider uri address
* @param {AbortSignal} signal abort signal
* @return {Promise<string>} urlDetails
*/
public async encrypt(
data: any,
providerUri: string,
signal?: AbortSignal
): Promise<string> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const path = this.getEndpointURL(serviceEndpoints, 'encrypt')
? this.getEndpointURL(serviceEndpoints, 'encrypt').urlPath
: null
if (!path) return null
try {
const response = await fetch(path, {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/octet-stream' },
signal
})
return await response.text()
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
/** Get DDO File details (if possible)
* @param {string} did did
* @param {number} serviceId the id of the service for which to check the files
* @param {string} providerUri uri of the provider that will be used to check the file
* @param {AbortSignal} signal abort signal
* @return {Promise<FileInfo[]>} urlDetails
*/
public async checkDidFiles(
did: string,
serviceId: string,
providerUri: string,
withChecksum: boolean = false,
signal?: AbortSignal
): Promise<FileInfo[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const args = { did, serviceId, checksum: withChecksum }
const files: FileInfo[] = []
const path = this.getEndpointURL(serviceEndpoints, 'fileinfo')
? this.getEndpointURL(serviceEndpoints, 'fileinfo').urlPath
: null
if (!path) return null
try {
const response = await fetch(path, {
method: 'POST',
body: JSON.stringify(args),
headers: { 'Content-Type': 'application/json' },
signal
})
const results: FileInfo[] = await response.json()
for (const result of results) {
files.push(result)
}
return files
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
/** Get URL details (if possible)
* @param {string} url or did
* @param {string} providerUri uri of the provider that will be used to check the file
* @param {AbortSignal} signal abort signal
* @return {Promise<FileInfo[]>} urlDetails
*/
public async getFileInfo(
file: UrlFile | Arweave,
providerUri: string,
signal?: AbortSignal
): Promise<FileInfo[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const args = file
const files: FileInfo[] = []
const path = this.getEndpointURL(serviceEndpoints, 'fileinfo')
? this.getEndpointURL(serviceEndpoints, 'fileinfo').urlPath
: null
if (!path) return null
try {
const response = await fetch(path, {
method: 'POST',
body: JSON.stringify(args),
headers: { 'Content-Type': 'application/json' },
signal
})
const results: FileInfo[] = await response.json()
for (const result of results) {
files.push(result)
}
return files
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
/** Get Compute Environments
* @return {Promise<ComputeEnvironment[]>} urlDetails
*/
public async getComputeEnvironments(
providerUri: string,
signal?: AbortSignal
): Promise<ComputeEnvironment[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const path = this.getEndpointURL(serviceEndpoints, 'computeEnvironments')?.urlPath
if (!path) return null
try {
const response = await fetch(path, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
signal
})
const envs: ComputeEnvironment[] = await response.json()
return envs
} catch (e) {
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
/** Initialize a service request.
* @param {DDO | string} asset
* @param {number} serviceIndex
* @param {string} serviceType
* @param {string} consumerAddress
* @param {UserCustomParameters} userCustomParameters
* @param {string} providerUri Identifier of the asset to be registered in ocean
* @param {AbortSignal} signal abort signal
* @return {Promise<ProviderInitialize>} ProviderInitialize data
*/
public async initialize(
did: string,
serviceId: string,
fileIndex: number,
consumerAddress: string,
providerUri: string,
signal?: AbortSignal,
userCustomParameters?: UserCustomParameters,
computeEnv?: string,
validUntil?: number
): Promise<ProviderInitialize> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
let initializeUrl = this.getEndpointURL(serviceEndpoints, 'initialize')
? this.getEndpointURL(serviceEndpoints, 'initialize').urlPath
: null
if (!initializeUrl) return null
initializeUrl += `?documentId=${did}`
initializeUrl += `&serviceId=${serviceId}`
initializeUrl += `&fileIndex=${fileIndex}`
initializeUrl += `&consumerAddress=${consumerAddress}`
if (userCustomParameters)
initializeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
if (computeEnv) initializeUrl += '&environment=' + encodeURI(computeEnv)
if (validUntil) initializeUrl += '&validUntil=' + validUntil
try {
const response = await fetch(initializeUrl, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
signal
})
const results: ProviderInitialize = await response.json()
return results
} catch (e) {
LoggerInstance.error(e)
throw new Error('Asset URL not found or not available.')
}
}
/** Initialize a compute request.
* @param {ComputeAsset} assets
* @param {ComputeAlgorithmber} algorithm
* @param {string} computeEnv
* @param {number} validUntil
* @param {string} providerUri Identifier of the asset to be registered in ocean
* @param {string} accountId
* @param {AbortSignal} signal abort signal
* @return {Promise<ProviderComputeInitialize>} ProviderComputeInitialize data
*/
public async initializeCompute(
assets: ComputeAsset[],
algorithm: ComputeAlgorithm,
computeEnv: string,
validUntil: number,
providerUri: string,
accountId: string,
signal?: AbortSignal
): Promise<ProviderComputeInitializeResults> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const providerData = {
datasets: assets,
algorithm,
compute: { env: computeEnv, validUntil },
consumerAddress: accountId
}
const initializeUrl = this.getEndpointURL(serviceEndpoints, 'initializeCompute')
? this.getEndpointURL(serviceEndpoints, 'initializeCompute').urlPath
: null
if (!initializeUrl) return null
try {
const response = await fetch(initializeUrl, {
method: 'POST',
body: JSON.stringify(providerData),
headers: { 'Content-Type': 'application/json' },
signal
})
const results = await response.json()
return results
} catch (e) {
LoggerInstance.error(e)
throw new Error('ComputeJob cannot be initialized')
}
}
/** Gets fully signed URL for download
* @param {string} did
* @param {string} accountId
* @param {string} serviceId
* @param {number} fileIndex
* @param {string} providerUri
* @param {Web3} web3
* @param {UserCustomParameters} userCustomParameters
* @return {Promise<string>}
*/
public async getDownloadUrl(
did: string,
accountId: string,
serviceId: string,
fileIndex: number,
transferTxId: string,
providerUri: string,
web3: Web3,
userCustomParameters?: UserCustomParameters
): Promise<any> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const downloadUrl = this.getEndpointURL(serviceEndpoints, 'download')
? this.getEndpointURL(serviceEndpoints, 'download').urlPath
: null
if (!downloadUrl) return null
const nonce = Date.now()
const signature = await this.signProviderRequest(web3, accountId, did + nonce)
let consumeUrl = downloadUrl
consumeUrl += `?fileIndex=${fileIndex}`
consumeUrl += `&documentId=${did}`
consumeUrl += `&transferTxId=${transferTxId}`
consumeUrl += `&serviceId=${serviceId}`
consumeUrl += `&consumerAddress=${accountId}`
consumeUrl += `&nonce=${nonce}`
consumeUrl += `&signature=${signature}`
if (userCustomParameters)
consumeUrl += '&userdata=' + encodeURI(JSON.stringify(userCustomParameters))
return consumeUrl
}
/** Instruct the provider to start a compute job
* @param {string} did
* @param {string} consumerAddress
* @param {string} computeEnv
* @param {ComputeAlgorithm} algorithm
* @param {string} providerUri
* @param {Web3} web3
* @param {AbortSignal} signal abort signal
* @param {ComputeOutput} output
* @return {Promise<ComputeJob | ComputeJob[]>}
*/
public async computeStart(
providerUri: string,
web3: Web3,
consumerAddress: string,
computeEnv: string,
dataset: ComputeAsset,
algorithm: ComputeAlgorithm,
signal?: AbortSignal,
additionalDatasets?: ComputeAsset[],
output?: ComputeOutput
): Promise<ComputeJob | ComputeJob[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const computeStartUrl = this.getEndpointURL(serviceEndpoints, 'computeStart')
? this.getEndpointURL(serviceEndpoints, 'computeStart').urlPath
: null
const nonce = Date.now()
let signatureMessage = consumerAddress
signatureMessage += dataset.documentId
signatureMessage += nonce
const signature = await this.signProviderRequest(
web3,
consumerAddress,
signatureMessage
)
const payload = Object()
payload.consumerAddress = consumerAddress
payload.signature = signature
payload.nonce = nonce
payload.environment = computeEnv
payload.dataset = dataset
payload.algorithm = algorithm
if (payload.additionalDatasets) payload.additionalDatasets = additionalDatasets
if (output) payload.output = output
if (!computeStartUrl) return null
try {
const response = await fetch(computeStartUrl, {
method: 'POST',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
signal
})
if (response?.ok) {
const params = await response.json()
return params
}
LoggerInstance.error(
'Compute start failed: ',
response.status,
response.statusText,
await response.json()
)
LoggerInstance.error('Payload was:', payload)
return null
} catch (e) {
LoggerInstance.error('Compute start failed:')
LoggerInstance.error(e)
LoggerInstance.error('Payload was:', payload)
throw new Error('HTTP request failed calling Provider')
}
}
/** Instruct the provider to Stop the execution of a to stop a compute job.
* @param {string} did
* @param {string} consumerAddress
* @param {string} jobId
* @param {string} providerUri
* @param {Web3} web3
* @param {AbortSignal} signal abort signal
* @return {Promise<ComputeJob | ComputeJob[]>}
*/
public async computeStop(
did: string,
consumerAddress: string,
jobId: string,
providerUri: string,
web3: Web3,
signal?: AbortSignal
): Promise<ComputeJob | ComputeJob[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const computeStopUrl = this.getEndpointURL(serviceEndpoints, 'computeStop')
? this.getEndpointURL(serviceEndpoints, 'computeStop').urlPath
: null
const nonce = await this.getNonce(
providerUri,
consumerAddress,
signal,
providerEndpoints,
serviceEndpoints
)
let signatureMessage = consumerAddress
signatureMessage += jobId || ''
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
signatureMessage += nonce
const signature = await this.signProviderRequest(
web3,
consumerAddress,
signatureMessage
)
const payload = Object()
payload.signature = signature
payload.documentId = this.noZeroX(did)
payload.consumerAddress = consumerAddress
if (jobId) payload.jobId = jobId
if (!computeStopUrl) return null
try {
const response = await fetch(computeStopUrl, {
method: 'PUT',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
signal
})
if (response?.ok) {
const params = await response.json()
return params
}
LoggerInstance.error('Compute stop failed:', response.status, response.statusText)
LoggerInstance.error('Payload was:', payload)
return null
} catch (e) {
LoggerInstance.error('Compute stop failed:')
LoggerInstance.error(e)
LoggerInstance.error('Payload was:', payload)
throw new Error('HTTP request failed calling Provider')
}
}
/** Get compute status for a specific jobId/documentId/owner.
* @param {string} providerUri The URI of the provider we want to query
* @param {string} consumerAddress The consumer ethereum address
* @param {string} jobId The ID of a compute job.
* @param {string} did The ID of the asset
* @param {AbortSignal} signal abort signal
* @return {Promise<ComputeJob | ComputeJob[]>}
*/
public async computeStatus(
providerUri: string,
consumerAddress: string,
jobId?: string,
did?: string,
signal?: AbortSignal
): Promise<ComputeJob | ComputeJob[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const computeStatusUrl = this.getEndpointURL(serviceEndpoints, 'computeStatus')
? this.getEndpointURL(serviceEndpoints, 'computeStatus').urlPath
: null
let url = `?consumerAddress=${consumerAddress}`
url += (did && `&documentId=${this.noZeroX(did)}`) || ''
url += (jobId && `&jobId=${jobId}`) || ''
if (!computeStatusUrl) return null
try {
const response = await fetch(computeStatusUrl + url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
signal
})
if (response?.ok) {
const params = await response.json()
return params
}
LoggerInstance.error(
'Get compute status failed:',
response.status,
response.statusText
)
return null
} catch (e) {
LoggerInstance.error('Get compute status failed')
LoggerInstance.error(e)
throw new Error('HTTP request failed calling Provider')
}
}
/** Get compute result url
* @param {string} providerUri The URI of the provider we want to query
* @param {Web3} web3 Web3 instance
* @param {string} consumerAddress The consumer ethereum address
* @param {string} jobId The ID of a compute job.
* @param {number} index Result index
* @return {Promise<string>}
*/
public async getComputeResultUrl(
providerUri: string,
web3: Web3,
consumerAddress: string,
jobId: string,
index: number
): Promise<string> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const computeResultUrl = this.getEndpointURL(serviceEndpoints, 'computeResult')
? this.getEndpointURL(serviceEndpoints, 'computeResult').urlPath
: null
const nonce = Date.now()
let signatureMessage = consumerAddress
signatureMessage += jobId
signatureMessage += index.toString()
signatureMessage += nonce
const signature = await this.signProviderRequest(
web3,
consumerAddress,
signatureMessage
)
if (!computeResultUrl) return null
let resultUrl = computeResultUrl
resultUrl += `?consumerAddress=${consumerAddress}`
resultUrl += `&jobId=${jobId}`
resultUrl += `&index=${index.toString()}`
resultUrl += `&nonce=${nonce}`
resultUrl += (signature && `&signature=${signature}`) || ''
return resultUrl
}
/** Deletes a compute job.
* @param {string} did
* @param {string} consumerAddress
* @param {string} jobId
* @param {string} providerUri
* @param {Web3} web3
* @param {AbortSignal} signal abort signal
* @return {Promise<ComputeJob | ComputeJob[]>}
*/
public async computeDelete(
did: string,
consumerAddress: string,
jobId: string,
providerUri: string,
web3: Web3,
signal?: AbortSignal
): Promise<ComputeJob | ComputeJob[]> {
const providerEndpoints = await this.getEndpoints(providerUri)
const serviceEndpoints = await this.getServiceEndpoints(
providerUri,
providerEndpoints
)
const computeDeleteUrl = this.getEndpointURL(serviceEndpoints, 'computeDelete')
? this.getEndpointURL(serviceEndpoints, 'computeDelete').urlPath
: null
const nonce = await this.getNonce(
providerUri,
consumerAddress,
signal,
providerEndpoints,
serviceEndpoints
)
let signatureMessage = consumerAddress
signatureMessage += jobId || ''
signatureMessage += (did && `${this.noZeroX(did)}`) || ''
signatureMessage += nonce
const signature = await this.signProviderRequest(
web3,
consumerAddress,
signatureMessage
)
const payload = Object()
payload.documentId = this.noZeroX(did)
payload.consumerAddress = consumerAddress
payload.jobId = jobId
if (signature) payload.signature = signature
if (!computeDeleteUrl) return null
try {
const response = await fetch(computeDeleteUrl, {
method: 'DELETE',
body: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' },
signal
})
if (response?.ok) {
const params = await response.json()
return params
}
LoggerInstance.error(
'Delete compute job failed:',
response.status,
response.statusText
)
LoggerInstance.error('Payload was:', payload)
return null
} catch (e) {
LoggerInstance.error('Delete compute job failed:')
LoggerInstance.error(e)
LoggerInstance.error('Payload was:', payload)
throw new Error('HTTP request failed calling Provider')
}
}
/** Check for a valid provider at URL
* @param {String} url provider uri address
* @param {AbortSignal} signal abort signal
* @return {Promise<boolean>} string
*/
public async isValidProvider(url: string, signal?: AbortSignal): Promise<boolean> {
try {
const response = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
signal
})
if (response?.ok) {
const params = await response.json()
if (params && params.providerAddress) return true
}
return false
} catch (error) {
LoggerInstance.error(`Error validating provider: ${error.message}`)
return false
}
}
private noZeroX(input: string): string {
return this.zeroXTransformer(input, false)
}
private zeroXTransformer(input = '', zeroOutput: boolean): string {
const { valid, output } = this.inputMatch(
input,
/^(?:0x)*([a-f0-9]+)$/i,
'zeroXTransformer'
)
return (zeroOutput && valid ? '0x' : '') + output
}
// Shared functions
private inputMatch(
input: string,
regexp: RegExp,
conversorName: string
): { valid: boolean; output: string } {
if (typeof input !== 'string') {
LoggerInstance.debug('Not input string:')
LoggerInstance.debug(input)
throw new Error(`[${conversorName}] Expected string, input type: ${typeof input}`)
}
const match = input.match(regexp)
if (!match) {
LoggerInstance.warn(`[${conversorName}] Input transformation failed.`)
return { valid: false, output: input }
}
return { valid: true, output: match[1] }
}
private async getData(url: string): Promise<Response> {
return fetch(url, {
method: 'GET',
headers: {
'Content-type': 'application/json'
}
})
}
}
export const ProviderInstance = new Provider()