2019-02-21 18:14:07 +01:00
|
|
|
import save = require("save-file")
|
2019-02-21 17:58:54 +01:00
|
|
|
import { File } from "../ddo/MetaData"
|
|
|
|
import Account from "../ocean/Account"
|
2018-11-19 12:16:11 +01:00
|
|
|
import WebServiceConnectorProvider from "../utils/WebServiceConnectorProvider"
|
2019-03-21 02:56:58 +01:00
|
|
|
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
|
2018-11-09 10:43:29 +01:00
|
|
|
|
2018-11-26 15:24:59 +01:00
|
|
|
const apiPath = "/api/v1/brizo/services"
|
|
|
|
|
2019-01-09 16:15:32 +01:00
|
|
|
/**
|
|
|
|
* Provides a interface with Brizo.
|
|
|
|
* Brizo is the technical component executed by the Publishers allowing to them to provide extended data services.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export class Brizo extends Instantiable {
|
|
|
|
private get url() {
|
|
|
|
return this.config.brizoUri
|
|
|
|
}
|
2018-11-09 10:43:29 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
constructor(config: InstantiableConfig) {
|
|
|
|
super()
|
|
|
|
this.setInstanceConfig(config)
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPurchaseEndpoint() {
|
2018-12-12 20:52:11 +01:00
|
|
|
return `${this.url}${apiPath}/access/initialize`
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
|
|
|
|
2018-12-08 00:46:18 +01:00
|
|
|
public getConsumeEndpoint() {
|
|
|
|
return `${this.url}${apiPath}/consume`
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|
2018-11-09 11:16:22 +01:00
|
|
|
|
2019-04-01 12:40:45 +02:00
|
|
|
public getEncryptEndpoint() {
|
|
|
|
return `${this.url}${apiPath}/publish`
|
|
|
|
}
|
|
|
|
|
2018-11-09 11:16:22 +01:00
|
|
|
public getComputeEndpoint(pubKey: string, serviceId: string, algo: string, container: string) {
|
|
|
|
// tslint:disable-next-line
|
2018-12-12 20:52:11 +01:00
|
|
|
return `${this.url}${apiPath}/compute`
|
2018-11-09 11:16:22 +01:00
|
|
|
}
|
2018-11-19 12:16:11 +01:00
|
|
|
|
2018-11-21 14:59:22 +01:00
|
|
|
public async initializeServiceAgreement(
|
|
|
|
did: string,
|
|
|
|
serviceAgreementId: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
signature: string,
|
2019-02-21 17:58:54 +01:00
|
|
|
consumerAddress: string,
|
|
|
|
): Promise<any> {
|
2018-11-26 15:24:59 +01:00
|
|
|
|
2018-11-29 08:45:10 +01:00
|
|
|
const args = {
|
|
|
|
did,
|
|
|
|
serviceAgreementId,
|
|
|
|
serviceDefinitionId,
|
|
|
|
signature,
|
|
|
|
consumerAddress,
|
|
|
|
}
|
|
|
|
|
2019-01-23 12:29:13 +01:00
|
|
|
try {
|
|
|
|
return await WebServiceConnectorProvider
|
|
|
|
.getConnector()
|
|
|
|
.post(
|
|
|
|
this.getPurchaseEndpoint(),
|
|
|
|
decodeURI(JSON.stringify(args)),
|
|
|
|
)
|
2019-01-24 10:58:05 +01:00
|
|
|
} catch (e) {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.logger.error(e)
|
2019-01-23 12:29:13 +01:00
|
|
|
throw new Error("HTTP request failed")
|
|
|
|
}
|
2018-11-19 12:16:11 +01:00
|
|
|
}
|
2019-02-21 17:58:54 +01:00
|
|
|
|
|
|
|
public async consumeService(
|
|
|
|
agreementId: string,
|
|
|
|
serviceEndpoint: string,
|
|
|
|
account: Account,
|
|
|
|
files: File[],
|
|
|
|
destination: string,
|
2019-04-03 17:57:47 +02:00
|
|
|
index: number = -1,
|
2019-02-21 17:58:54 +01:00
|
|
|
): Promise<string> {
|
2019-04-01 12:40:45 +02:00
|
|
|
const agreementIdSignature = await this.ocean.utils.signature.signText(agreementId, account.getId())
|
2019-02-21 17:58:54 +01:00
|
|
|
const filesPromises = files
|
2019-04-03 17:57:47 +02:00
|
|
|
.filter(({}, i) => index === -1 || i === index)
|
|
|
|
.map(async ({index}) => {
|
2019-02-21 17:58:54 +01:00
|
|
|
let consumeUrl = serviceEndpoint
|
2019-04-03 17:57:47 +02:00
|
|
|
consumeUrl += `?index=${index}`
|
2019-02-21 17:58:54 +01:00
|
|
|
consumeUrl += `&serviceAgreementId=${agreementId}`
|
|
|
|
consumeUrl += `&consumerAddress=${account.getId()}`
|
2019-04-01 12:40:45 +02:00
|
|
|
consumeUrl += `&signature=${agreementIdSignature}`
|
2019-02-21 17:58:54 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.downloadFile(
|
|
|
|
consumeUrl,
|
2019-04-03 17:57:47 +02:00
|
|
|
`file-${index}`,
|
2019-02-21 17:58:54 +01:00
|
|
|
destination,
|
|
|
|
)
|
|
|
|
} catch (e) {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.logger.error("Error consuming assets")
|
|
|
|
this.logger.error(e)
|
2019-02-21 17:58:54 +01:00
|
|
|
throw new Error("Error consuming assets")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
await Promise.all(filesPromises)
|
|
|
|
return destination
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:40:45 +02:00
|
|
|
public async encrypt(
|
|
|
|
did: string,
|
|
|
|
signedDid: string,
|
|
|
|
document: any,
|
|
|
|
publisher: string,
|
|
|
|
): Promise<string> {
|
|
|
|
|
|
|
|
const args = {
|
|
|
|
documentId: did,
|
|
|
|
signedDocumentId: signedDid,
|
|
|
|
document: JSON.stringify(document),
|
|
|
|
publisherAddress: publisher,
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await WebServiceConnectorProvider
|
|
|
|
.getConnector()
|
|
|
|
.post(
|
|
|
|
this.getEncryptEndpoint(),
|
|
|
|
decodeURI(JSON.stringify(args)),
|
|
|
|
)
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error("HTTP request failed")
|
|
|
|
}
|
|
|
|
return await response.text()
|
|
|
|
} catch (e) {
|
|
|
|
this.logger.error(e)
|
|
|
|
throw new Error("HTTP request failed")
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 17:26:42 +02:00
|
|
|
|
|
|
|
private async downloadFile(url: string, filename: string, destination?: string): Promise<string> {
|
|
|
|
const path = `${destination}${filename}`
|
|
|
|
const response = await WebServiceConnectorProvider
|
|
|
|
.getConnector()
|
|
|
|
.get(url)
|
|
|
|
await save(await response.arrayBuffer(), path)
|
|
|
|
return path
|
|
|
|
}
|
2018-11-09 10:43:29 +01:00
|
|
|
}
|