2019-01-21 17:48:40 +01:00
|
|
|
import AquariusProvider from "../aquarius/AquariusProvider"
|
2019-02-12 15:07:10 +01:00
|
|
|
import { SearchQuery } from "../aquarius/query/SearchQuery"
|
2019-01-21 17:48:40 +01:00
|
|
|
import BrizoProvider from "../brizo/BrizoProvider"
|
2019-02-21 17:58:54 +01:00
|
|
|
import ConfigProvider from "../ConfigProvider"
|
2019-02-04 11:46:24 +01:00
|
|
|
import { Condition } from "../ddo/Condition"
|
|
|
|
import { DDO } from "../ddo/DDO"
|
|
|
|
import { MetaData } from "../ddo/MetaData"
|
2019-02-21 17:58:54 +01:00
|
|
|
import { Service, ServiceAuthorization } from "../ddo/Service"
|
2019-01-21 17:48:40 +01:00
|
|
|
import Keeper from "../keeper/Keeper"
|
|
|
|
import SecretStoreProvider from "../secretstore/SecretStoreProvider"
|
|
|
|
import Account from "./Account"
|
|
|
|
import DID from "./DID"
|
2019-02-14 11:26:12 +01:00
|
|
|
import OceanAgreements from "./OceanAgreements"
|
2019-01-21 17:48:40 +01:00
|
|
|
import ServiceAgreementTemplate from "./ServiceAgreements/ServiceAgreementTemplate"
|
|
|
|
import Access from "./ServiceAgreements/Templates/Access"
|
2019-02-21 17:58:54 +01:00
|
|
|
import EventListener from "../keeper/EventListener"
|
|
|
|
import ServiceAgreement from "./ServiceAgreements/ServiceAgreement"
|
|
|
|
import Logger from '../utils/Logger'
|
2019-01-21 17:48:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assets submodule of Ocean Protocol.
|
|
|
|
*/
|
|
|
|
export default class OceanAssets {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance of OceanAssets.
|
|
|
|
* @return {Promise<OceanAssets>}
|
|
|
|
*/
|
|
|
|
public static async getInstance(): Promise<OceanAssets> {
|
|
|
|
if (!OceanAssets.instance) {
|
|
|
|
OceanAssets.instance = new OceanAssets()
|
|
|
|
}
|
|
|
|
|
|
|
|
return OceanAssets.instance
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OceanAssets instance.
|
|
|
|
* @type {OceanAssets}
|
|
|
|
*/
|
|
|
|
private static instance: OceanAssets = null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a DDO by DID.
|
|
|
|
* @param {string} did Decentralized ID.
|
|
|
|
* @return {Promise<DDO>}
|
|
|
|
*/
|
|
|
|
public async resolve(did: string): Promise<DDO> {
|
|
|
|
const d: DID = DID.parse(did)
|
|
|
|
return AquariusProvider.getAquarius().retrieveDDO(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new DDO.
|
|
|
|
* @param {MetaData} metadata DDO metadata.
|
2019-02-14 11:26:12 +01:00
|
|
|
* @param {Account} publisher Publisher account.
|
2019-01-21 17:48:40 +01:00
|
|
|
* @return {Promise<DDO>}
|
|
|
|
*/
|
2019-02-07 11:32:50 +01:00
|
|
|
public async create(metadata: MetaData, publisher: Account, services: Service[] = []): Promise<DDO> {
|
2019-02-15 23:40:55 +01:00
|
|
|
const {secretStoreUri} = ConfigProvider.getConfig()
|
2019-01-21 17:48:40 +01:00
|
|
|
const {didRegistry} = await Keeper.getInstance()
|
|
|
|
const aquarius = AquariusProvider.getAquarius()
|
|
|
|
const brizo = BrizoProvider.getBrizo()
|
|
|
|
|
|
|
|
const did: DID = DID.generate()
|
|
|
|
|
2019-02-07 14:21:16 +01:00
|
|
|
const authorizationService = (services.find(({type}) => type === "Authorization") || {}) as ServiceAuthorization
|
|
|
|
const secretStoreUrl = authorizationService.service === "SecretStore" && authorizationService.serviceEndpoint
|
2019-02-15 23:40:55 +01:00
|
|
|
const secretStoreConfig = {
|
|
|
|
secretStoreUri: secretStoreUrl,
|
|
|
|
}
|
2019-02-07 14:21:16 +01:00
|
|
|
|
2019-02-15 23:40:55 +01:00
|
|
|
const encryptedFiles = await SecretStoreProvider.getSecretStore(secretStoreConfig).encryptDocument(did.getId(), metadata.base.files)
|
2019-01-21 17:48:40 +01:00
|
|
|
|
|
|
|
const template = new Access()
|
|
|
|
const serviceAgreementTemplate = new ServiceAgreementTemplate(template)
|
|
|
|
|
|
|
|
const conditions: Condition[] = await serviceAgreementTemplate.getConditions(metadata, did.getId())
|
|
|
|
|
|
|
|
const serviceEndpoint = aquarius.getServiceEndpoint(did)
|
|
|
|
|
2019-02-07 11:29:55 +01:00
|
|
|
let serviceDefinitionIdCount = 0
|
2019-01-21 17:48:40 +01:00
|
|
|
// create ddo itself
|
|
|
|
const ddo: DDO = new DDO({
|
|
|
|
authentication: [{
|
|
|
|
type: "RsaSignatureAuthentication2018",
|
|
|
|
publicKey: did.getDid() + "#keys-1",
|
2019-02-04 11:46:24 +01:00
|
|
|
}],
|
2019-01-21 17:48:40 +01:00
|
|
|
id: did.getDid(),
|
|
|
|
publicKey: [
|
|
|
|
{
|
|
|
|
id: did.getDid() + "#keys-1",
|
|
|
|
type: "Ed25519VerificationKey2018",
|
|
|
|
owner: did.getDid(),
|
|
|
|
publicKeyBase58: await publisher.getPublicKey(),
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
2019-01-21 17:48:40 +01:00
|
|
|
],
|
2019-02-07 14:21:16 +01:00
|
|
|
service: [
|
2019-01-21 17:48:40 +01:00
|
|
|
{
|
|
|
|
type: template.templateName,
|
|
|
|
purchaseEndpoint: brizo.getPurchaseEndpoint(),
|
|
|
|
serviceEndpoint: brizo.getConsumeEndpoint(),
|
|
|
|
// the id of the service agreement?
|
2019-02-07 11:29:55 +01:00
|
|
|
serviceDefinitionId: String(serviceDefinitionIdCount++),
|
2019-01-21 17:48:40 +01:00
|
|
|
// the id of the service agreement template
|
|
|
|
templateId: serviceAgreementTemplate.getId(),
|
|
|
|
serviceAgreementContract: {
|
2019-01-30 15:35:31 +01:00
|
|
|
contractName: "ServiceExecutionAgreement",
|
2019-01-21 17:48:40 +01:00
|
|
|
fulfillmentOperator: template.fulfillmentOperator,
|
|
|
|
events: [
|
|
|
|
{
|
2019-01-30 15:35:31 +01:00
|
|
|
name: "AgreementInitialized",
|
2019-01-21 17:48:40 +01:00
|
|
|
actorType: "consumer",
|
|
|
|
handler: {
|
|
|
|
moduleName: "payment",
|
|
|
|
functionName: "lockPayment",
|
|
|
|
version: "0.1",
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
|
|
|
},
|
2019-01-21 17:48:40 +01:00
|
|
|
],
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
2019-01-21 17:48:40 +01:00
|
|
|
conditions,
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
2019-01-21 17:48:40 +01:00
|
|
|
{
|
|
|
|
type: "Compute",
|
2019-02-07 11:29:55 +01:00
|
|
|
serviceEndpoint: brizo.getComputeEndpoint(publisher.getId(), String(serviceDefinitionIdCount), "xxx", "xxx"),
|
|
|
|
serviceDefinitionId: String(serviceDefinitionIdCount++),
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
2019-02-15 23:40:55 +01:00
|
|
|
{
|
|
|
|
type: "Authorization",
|
|
|
|
services: 'SecretStore',
|
|
|
|
serviceEndpoint: secretStoreUri,
|
|
|
|
serviceDefinitionId: String(serviceDefinitionIdCount++),
|
|
|
|
},
|
2019-01-21 17:48:40 +01:00
|
|
|
{
|
|
|
|
type: "Metadata",
|
|
|
|
serviceEndpoint,
|
2019-02-07 11:29:55 +01:00
|
|
|
serviceDefinitionId: String(serviceDefinitionIdCount++),
|
2019-02-04 17:45:49 +01:00
|
|
|
metadata: {
|
|
|
|
// Default values
|
|
|
|
curation: {
|
|
|
|
rating: 0,
|
|
|
|
numVotes: 0,
|
|
|
|
},
|
|
|
|
additionalInformation: {
|
|
|
|
updateFrecuency: "yearly",
|
|
|
|
structuredMarkup: [],
|
|
|
|
},
|
|
|
|
// Overwrites defaults
|
|
|
|
...metadata,
|
2019-02-06 13:10:24 +01:00
|
|
|
// Cleaning not needed information
|
|
|
|
base: {
|
|
|
|
...metadata.base,
|
2019-02-07 14:21:16 +01:00
|
|
|
contentUrls: [],
|
|
|
|
encryptedFiles,
|
2019-02-06 13:10:24 +01:00
|
|
|
files: undefined,
|
2019-02-06 13:37:31 +01:00
|
|
|
} as any,
|
2019-02-04 17:45:49 +01:00
|
|
|
},
|
2019-02-04 11:46:24 +01:00
|
|
|
},
|
2019-02-07 14:21:16 +01:00
|
|
|
...services
|
|
|
|
.map((_) => ({..._, serviceDefinitionId: String(serviceDefinitionIdCount++)})),
|
2019-02-15 23:40:55 +01:00
|
|
|
]
|
|
|
|
// Remove duplications
|
|
|
|
.reverse()
|
|
|
|
.filter(({type}, i, list) => list.findIndex(({type: t}) => t === type) === i)
|
|
|
|
.reverse() as Service[],
|
2019-01-21 17:48:40 +01:00
|
|
|
})
|
|
|
|
|
2019-02-04 17:45:49 +01:00
|
|
|
ddo.addChecksum()
|
|
|
|
await ddo.addProof(publisher.getId(), publisher.getPassword())
|
|
|
|
|
2019-01-21 17:48:40 +01:00
|
|
|
const storedDdo = await aquarius.storeDDO(ddo)
|
|
|
|
|
|
|
|
await didRegistry.registerAttribute(
|
|
|
|
did.getId(),
|
2019-02-06 12:54:06 +01:00
|
|
|
ddo.getChecksum(),
|
2019-01-21 17:48:40 +01:00
|
|
|
serviceEndpoint,
|
2019-02-06 13:10:24 +01:00
|
|
|
publisher.getId(),
|
|
|
|
)
|
2019-01-21 17:48:40 +01:00
|
|
|
|
|
|
|
return storedDdo
|
|
|
|
}
|
|
|
|
|
2019-02-21 17:58:54 +01:00
|
|
|
public async consume (agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account, resultPath: string): Promise<string>
|
|
|
|
public async consume (agreementId: string, did: string, serviceDefinitionId: string, consumerAccount: Account): Promise<true>
|
|
|
|
public async consume (
|
|
|
|
agreementId: string,
|
|
|
|
did: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
consumerAccount: Account,
|
|
|
|
resultPath?: string,
|
|
|
|
): Promise<string | true> {
|
|
|
|
|
|
|
|
const brizo = BrizoProvider.getBrizo()
|
|
|
|
const ddo = await this.resolve(did)
|
|
|
|
const {metadata} = ddo.findServiceByType('Metadata')
|
|
|
|
|
|
|
|
const authorizationService = ddo.findServiceByType('Authorization')
|
|
|
|
const accessService = ddo.findServiceById(serviceDefinitionId)
|
|
|
|
|
|
|
|
const files = metadata.base.encryptedFiles
|
|
|
|
|
|
|
|
const {serviceEndpoint} = accessService
|
|
|
|
|
|
|
|
if (!serviceEndpoint) {
|
|
|
|
throw new Error('Consume asset failed, service definition is missing the `serviceEndpoint`.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const secretStoreUrl = authorizationService.service === "SecretStore" && authorizationService.serviceEndpoint
|
|
|
|
const secretStoreConfig = {
|
|
|
|
secretStoreUri: secretStoreUrl,
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.log("Decrypting files")
|
|
|
|
const decryptedFiles = await SecretStoreProvider.getSecretStore(secretStoreConfig).decryptDocument(DID.parse(did).getId(), files)
|
|
|
|
Logger.log("Files decrypted")
|
|
|
|
|
|
|
|
Logger.log("Consuming files")
|
|
|
|
|
|
|
|
resultPath = resultPath ? `${resultPath}/datafile.${ddo.shortId()}.${agreementId}/` : undefined
|
|
|
|
await brizo.consumeService(
|
|
|
|
agreementId,
|
|
|
|
serviceEndpoint,
|
|
|
|
consumerAccount,
|
|
|
|
decryptedFiles,
|
|
|
|
resultPath,
|
|
|
|
)
|
|
|
|
Logger.log("Files consumed")
|
|
|
|
|
|
|
|
if (resultPath) {
|
|
|
|
return resultPath
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:48:40 +01:00
|
|
|
/**
|
2019-02-13 21:28:42 +01:00
|
|
|
* Start the purchase/order of an asset's service. Starts by signing the service agreement
|
|
|
|
* then sends the request to the publisher via the service endpoint (Brizo http service).
|
2019-01-21 17:48:40 +01:00
|
|
|
* @param {string} did Decentralized ID.
|
|
|
|
* @param {string} serviceDefinitionId Service definition ID.
|
|
|
|
* @param {Account} consumer Consumer account.
|
2019-02-13 13:05:08 +01:00
|
|
|
* @return {Promise<string>} Returns Agreement ID
|
2019-01-21 17:48:40 +01:00
|
|
|
*/
|
|
|
|
public async order(
|
|
|
|
did: string,
|
|
|
|
serviceDefinitionId: string,
|
|
|
|
consumer: Account,
|
2019-02-13 13:05:08 +01:00
|
|
|
): Promise<string> {
|
2019-01-21 17:48:40 +01:00
|
|
|
|
2019-02-13 21:28:42 +01:00
|
|
|
const oceanAreements = await OceanAgreements.getInstance()
|
2019-01-21 17:48:40 +01:00
|
|
|
|
2019-02-21 17:58:54 +01:00
|
|
|
Logger.log("Asking for agreement signature")
|
2019-02-13 21:28:42 +01:00
|
|
|
const {agreementId, signature} = await oceanAreements.prepare(did, serviceDefinitionId, consumer)
|
2019-02-21 17:58:54 +01:00
|
|
|
Logger.log(`Agreement ${agreementId} signed`)
|
|
|
|
|
|
|
|
const ddo = await this.resolve(did)
|
|
|
|
|
|
|
|
const paymentFlow = new Promise((resolve, reject) => {
|
|
|
|
EventListener
|
|
|
|
.subscribe(
|
|
|
|
"ServiceExecutionAgreement",
|
|
|
|
"AgreementInitialized",
|
|
|
|
{agreementId: "0x" + agreementId},
|
|
|
|
)
|
|
|
|
.listenOnce(async (...args) => {
|
|
|
|
Logger.log("Agreement initialized")
|
|
|
|
const serviceAgreement = new ServiceAgreement("0x" + agreementId)
|
|
|
|
const {metadata} = ddo.findServiceByType("Metadata")
|
|
|
|
|
|
|
|
Logger.log("Locking payment")
|
|
|
|
const paid = await serviceAgreement.payAsset(ddo.shortId(), metadata.base.price, consumer)
|
|
|
|
|
|
|
|
if (paid) {
|
|
|
|
Logger.log("Payment was OK")
|
|
|
|
} else {
|
|
|
|
Logger.error("Payment was KO")
|
|
|
|
Logger.error("Agreement ID: ", agreementId)
|
|
|
|
Logger.error("DID: ", ddo.id)
|
|
|
|
reject("Error on payment")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
EventListener
|
|
|
|
.subscribe(
|
|
|
|
"AccessConditions",
|
|
|
|
"AccessGranted",
|
|
|
|
{agreementId: "0x" + agreementId},
|
|
|
|
)
|
|
|
|
.listenOnce(async (...args) => {
|
|
|
|
Logger.log("Access granted")
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Logger.log("Sending agreement request")
|
2019-02-13 21:28:42 +01:00
|
|
|
await oceanAreements.send(did, agreementId, serviceDefinitionId, signature, consumer)
|
2019-01-21 17:48:40 +01:00
|
|
|
|
2019-02-21 17:58:54 +01:00
|
|
|
await paymentFlow
|
|
|
|
|
2019-02-13 21:28:42 +01:00
|
|
|
return agreementId
|
2019-01-21 17:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search over the assets using a query.
|
|
|
|
* @param {SearchQuery} query Query to filter the assets.
|
|
|
|
* @return {Promise<DDO[]>}
|
|
|
|
*/
|
|
|
|
public async query(query: SearchQuery): Promise<DDO[]> {
|
2019-02-12 15:07:10 +01:00
|
|
|
return AquariusProvider.getAquarius().queryMetadataByText(query)
|
2019-01-21 17:48:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search over the assets using a keyword.
|
|
|
|
* @param {SearchQuery} text Text to filter the assets.
|
|
|
|
* @return {Promise<DDO[]>}
|
|
|
|
*/
|
|
|
|
public async search(text: string): Promise<DDO[]> {
|
|
|
|
return AquariusProvider.getAquarius().queryMetadataByText({
|
|
|
|
text,
|
|
|
|
page: 0,
|
|
|
|
offset: 100,
|
|
|
|
query: {
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
} as SearchQuery)
|
|
|
|
}
|
|
|
|
}
|