1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/brizo/Brizo.ts

225 lines
6.7 KiB
TypeScript
Raw Normal View History

2020-01-09 12:20:19 +01:00
import { File, MetaData } from '../ddo/MetaData'
2019-06-20 00:20:09 +02:00
import Account from '../ocean/Account'
import { noZeroX } from '../utils'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
2020-01-28 12:20:35 +01:00
import { DDO } from '../ddo/DDO'
import { ServiceType } from '../ddo/Service'
2020-02-01 15:11:06 +01:00
import { ComputeJob, Output } from '../ocean/OceanCompute'
2018-11-09 10:43:29 +01:00
2019-06-20 00:20:09 +02:00
const apiPath = '/api/v1/brizo/services'
2018-11-26 15:24:59 +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-06-20 00:20:09 +02:00
export class Brizo extends Instantiable {
private get url() {
return this.config.brizoUri
}
2018-11-09 10:43:29 +01:00
constructor(config: InstantiableConfig) {
super()
this.setInstanceConfig(config)
2018-11-09 10:43:29 +01:00
}
2019-06-14 12:07:35 +02:00
public async getVersionInfo() {
2019-06-24 13:06:38 +02:00
return (await this.ocean.utils.fetch.get(this.url)).json()
2019-06-14 00:34:53 +02:00
}
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
}
public getConsumeEndpoint() {
return `${this.url}${apiPath}/consume`
2018-11-09 10:43:29 +01:00
}
public getEncryptEndpoint() {
return `${this.url}${apiPath}/publish`
}
2020-01-09 12:20:19 +01:00
public getComputeEndpoint() {
2020-01-21 12:45:50 +01:00
return `${this.url}${apiPath}/compute`
}
2018-11-19 12:16:11 +01:00
2020-01-28 12:20:35 +01:00
public async getEndpointFromAgreement(
type: ServiceType,
agreementId: string
): Promise<string> {
const { assets, keeper } = this.ocean
const { did } = await keeper.agreementStoreManager.getAgreement(agreementId)
const ddo: DDO = await assets.resolve(did)
const { serviceEndpoint } = ddo.findServiceByType(type)
return serviceEndpoint
}
2018-11-21 14:59:22 +01:00
public async initializeServiceAgreement(
did: string,
serviceAgreementId: string,
2019-08-16 16:12:42 +02:00
serviceIndex: number,
2018-11-21 14:59:22 +01:00
signature: string,
2019-06-20 00:20:09 +02:00
consumerAddress: string
2019-02-21 17:58:54 +01:00
): Promise<any> {
2018-11-29 08:45:10 +01:00
const args = {
did,
serviceAgreementId,
2019-08-16 16:12:42 +02:00
serviceIndex,
2018-11-29 08:45:10 +01:00
signature,
2019-06-20 00:20:09 +02:00
consumerAddress
2018-11-29 08:45:10 +01:00
}
try {
2019-11-15 00:00:10 +01:00
return await this.ocean.utils.fetch.post(
this.getPurchaseEndpoint(),
decodeURI(JSON.stringify(args))
)
2019-01-24 10:58:05 +01:00
} catch (e) {
this.logger.error(e)
2019-06-20 00:20:09 +02: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-06-20 00:20:09 +02:00
index: number = -1
2019-02-21 17:58:54 +01:00
): Promise<string> {
2020-01-09 12:20:19 +01:00
const signature = await this.createSignature(account, agreementId)
2019-02-21 17:58:54 +01:00
const filesPromises = files
2019-06-24 13:06:38 +02:00
.filter((_, i) => index === -1 || i === index)
2019-06-20 00:20:09 +02:00
.map(async ({ index: i }) => {
2019-02-21 17:58:54 +01:00
let consumeUrl = serviceEndpoint
2019-04-05 12:20:42 +02:00
consumeUrl += `?index=${i}`
2019-04-15 17:45:06 +02:00
consumeUrl += `&serviceAgreementId=${noZeroX(agreementId)}`
2019-02-21 17:58:54 +01:00
consumeUrl += `&consumerAddress=${account.getId()}`
consumeUrl += `&signature=${signature}`
2019-02-21 17:58:54 +01:00
try {
2019-09-09 12:18:54 +02:00
await this.ocean.utils.fetch.downloadFile(consumeUrl, destination, i)
2019-02-21 17:58:54 +01:00
} catch (e) {
2019-06-20 00:20:09 +02:00
this.logger.error('Error consuming assets')
this.logger.error(e)
throw e
2019-02-21 17:58:54 +01:00
}
})
await Promise.all(filesPromises)
return destination
}
2020-01-22 10:01:04 +01:00
public async compute(
2020-01-21 12:45:50 +01:00
method: string,
serviceAgreementId: string,
consumerAccount: Account,
algorithmDid?: string,
algorithmMeta?: MetaData,
2020-02-01 14:25:24 +01:00
jobId?: string,
2020-02-01 15:11:06 +01:00
output?: Output
2020-01-28 18:00:06 +01:00
): Promise<ComputeJob | ComputeJob[]> {
2020-01-21 12:45:50 +01:00
const signature = await this.createSignature(consumerAccount, serviceAgreementId)
const address = consumerAccount.getId()
2020-01-28 12:20:35 +01:00
const serviceEndpoint = await this.getEndpointFromAgreement(
'compute',
serviceAgreementId
)
if (!serviceEndpoint) {
throw new Error(
'Computing on asset failed, service definition is missing the `serviceEndpoint`.'
)
}
2020-01-09 12:20:19 +01:00
2020-01-21 12:45:50 +01:00
// construct Brizo URL
2020-01-28 12:20:35 +01:00
let url = serviceEndpoint
url += `?signature=${signature}`
2020-01-21 12:45:50 +01:00
url += `&consumerAddress=${address}`
url += `&serviceAgreementId=${noZeroX(serviceAgreementId)}`
url += (algorithmDid && `&algorithmDid=${algorithmDid}`) || ''
url += (algorithmMeta && `&algorithmMeta=${algorithmMeta}`) || ''
url += (output && `&output=${JSON.stringify(output)}`) || ''
url += (jobId && `&jobId=${jobId}`) || ''
2020-01-21 12:45:50 +01:00
// switch fetch method
let fetch
switch (method) {
case 'post':
fetch = this.ocean.utils.fetch.post(url, '')
break
case 'put':
fetch = this.ocean.utils.fetch.put(url, '')
break
case 'delete':
fetch = this.ocean.utils.fetch.delete(url)
break
default:
fetch = this.ocean.utils.fetch.get(url)
break
}
const result = await fetch
2020-01-09 12:20:19 +01:00
.then((response: any) => {
if (response.ok) {
return response.json()
}
this.logger.error(
2020-01-21 12:45:50 +01:00
'Compute job failed:',
2020-01-09 12:20:19 +01:00
response.status,
response.statusText
)
return null
})
2020-01-21 12:45:50 +01:00
.catch((error: Error) => {
this.logger.error('Error with compute job')
this.logger.error(error.message)
2020-01-09 12:20:19 +01:00
throw error
})
2020-01-21 12:45:50 +01:00
return result
2020-01-09 12:20:19 +01:00
}
public async createSignature(account: Account, agreementId: string): Promise<string> {
const signature =
(await account.getToken()) ||
(await this.ocean.utils.signature.signText(
noZeroX(agreementId),
account.getId()
))
return signature
}
2019-11-15 00:00:10 +01:00
public async encrypt(
did: string,
signature: string,
document: any,
publisher: string
): Promise<string> {
const args = {
documentId: did,
signature,
document: JSON.stringify(document),
2019-06-20 00:20:09 +02:00
publisherAddress: publisher
}
try {
2019-06-20 00:20:09 +02:00
const response = await this.ocean.utils.fetch.post(
this.getEncryptEndpoint(),
decodeURI(JSON.stringify(args))
)
if (!response.ok) {
2019-06-20 00:20:09 +02:00
throw new Error('HTTP request failed')
}
return await response.text()
} catch (e) {
this.logger.error(e)
2019-06-20 00:20:09 +02:00
throw new Error('HTTP request failed')
}
}
2018-11-09 10:43:29 +01:00
}