diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index 192fb8fe..93613ec0 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -1,7 +1,7 @@ import Account from '../ocean/Account' import { noZeroX } from '../utils' import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' -import { DDO } from '../ddo/DDO' +import { File } from '../ddo/interfaces/File' const apiPath = '/api/v1/services/' @@ -102,6 +102,43 @@ export class Provider extends Instantiable { } } + public async download( + did: string, + txId: string, + tokenAddress: string, + serviceType: string, + serviceIndex: string, + destination: string, + account: Account, + files: File[], + index: number = -1 + ): Promise { + const signature = await this.createSignature(account, did) + const filesPromises = files + .filter((_, i) => index === -1 || i === index) + .map(async ({ index: i }) => { + let consumeUrl = this.getDownloadEndpoint() + consumeUrl += `?index=${i}` + consumeUrl += `&documentId=${did}` + consumeUrl += `&serviceId=${serviceIndex}` + consumeUrl += `&serviceType=${serviceType}` + consumeUrl += `tokenAddress=${tokenAddress}` + consumeUrl += `&transferTxId=${txId}` + consumeUrl += `&consumerAddress=${account.getId()}` + consumeUrl += `&signature=${signature}` + + try { + await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i) + } catch (e) { + this.logger.error('Error consuming assets') + this.logger.error(e) + throw e + } + }) + await Promise.all(filesPromises) + return destination + } + public async getVersionInfo() { return (await this.ocean.utils.fetch.get(this.url)).json() }