1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

add edit helpers

This commit is contained in:
alexcos20 2020-09-16 04:57:39 -07:00
parent 2994cb5b8a
commit 323e57e6f4
5 changed files with 102 additions and 63 deletions

View File

@ -1,4 +1,4 @@
export interface ServicePrices { export interface ServicePrices {
serviceIndex: number serviceIndex: number
price: string cost: string
} }

View File

@ -99,7 +99,6 @@ export class OnChainMetadataStore {
return null return null
} }
try { try {
// const data = this.web3.utils.bytesToHex(ddo)
const estGas = await this.DDOContract.methods const estGas = await this.DDOContract.methods
.create(did, flags, data) .create(did, flags, data)
.estimateGas(function (err, estGas) { .estimateGas(function (err, estGas) {
@ -127,7 +126,7 @@ export class OnChainMetadataStore {
public async updateRaw( public async updateRaw(
did: string, did: string,
flags: any, flags: any,
ddo: any, data: any,
consumerAccount: string consumerAccount: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
if (!this.DDOContract) { if (!this.DDOContract) {
@ -135,16 +134,9 @@ export class OnChainMetadataStore {
return null return null
} }
try { try {
const data = this.web3.utils.bytesToHex(ddo)
const estGas = await this.DDOContract.methods
.update(did, flags, data)
.estimateGas(function (err, estGas) {
if (err) console.log('OnChainMetadataStore: ' + err)
return estGas
})
const trxReceipt = await this.DDOContract.methods const trxReceipt = await this.DDOContract.methods
.update(did, flags, data) .update(did, flags, data)
.send({ from: consumerAccount, gas: estGas + 1 }) .send({ from: consumerAccount })
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
console.error(e) console.error(e)

View File

@ -223,23 +223,39 @@ export class Assets extends Instantiable {
did: string, did: string,
newMetadata: EditableMetadata, newMetadata: EditableMetadata,
account: Account account: Account
): Promise<string> { ): Promise<DDO> {
const oldDdo = await this.ocean.metadatastore.retrieveDDO(did) const oldDdo = await this.ocean.metadatastore.retrieveDDO(did)
// get a signature let i
const signature = await this.ocean.utils.signature.signForAquarius( for (i = 0; i < oldDdo.service.length; i++) {
oldDdo.updated, if (oldDdo.service[i].type === 'metadata') {
account if (newMetadata.title) oldDdo.service[i].attributes.main.name = newMetadata.title
if (!oldDdo.service[i].attributes.additionalInformation)
oldDdo.service[i].attributes.additionalInformation = Object()
if (newMetadata.description)
oldDdo.service[i].attributes.additionalInformation.description =
newMetadata.description
if (newMetadata.links)
oldDdo.service[i].attributes.additionalInformation.links = newMetadata.links
}
}
if (newMetadata.servicePrices) {
for (i = 0; i < newMetadata.servicePrices.length; i++) {
if (
newMetadata.servicePrices[i].cost &&
newMetadata.servicePrices[i].serviceIndex
) {
oldDdo.service[newMetadata.servicePrices[i].serviceIndex].attributes.main.cost =
newMetadata.servicePrices[i].cost
}
}
}
const storeTx = await this.ocean.OnChainMetadataStore.update(
oldDdo.id,
oldDdo,
account.getId()
) )
let result = null if (storeTx) return oldDdo
if (signature != null) else return null
result = await this.ocean.metadatastore.editMetadata(
did,
newMetadata,
oldDdo.updated,
signature
)
return result
} }
/** /**
@ -255,45 +271,22 @@ export class Assets extends Instantiable {
serviceIndex: number, serviceIndex: number,
computePrivacy: ServiceComputePrivacy, computePrivacy: ServiceComputePrivacy,
account: Account account: Account
): Promise<string> { ): Promise<DDO> {
const oldDdo = await this.ocean.metadatastore.retrieveDDO(did) const oldDdo = await this.ocean.metadatastore.retrieveDDO(did)
// get a signature if (oldDdo.service[serviceIndex].type !== 'compute') return null
const signature = await this.ocean.utils.signature.signForAquarius( oldDdo.service[serviceIndex].attributes.main.privacy.allowRawAlgorithm =
oldDdo.updated, computePrivacy.allowRawAlgorithm
account oldDdo.service[serviceIndex].attributes.main.privacy.allowNetworkAccess =
computePrivacy.allowNetworkAccess
oldDdo.service[serviceIndex].attributes.main.privacy.trustedAlgorithms =
computePrivacy.trustedAlgorithms
const storeTx = await this.ocean.OnChainMetadataStore.update(
oldDdo.id,
oldDdo,
account.getId()
) )
let result = null if (storeTx) return oldDdo
if (signature != null) else return null
result = await this.ocean.metadatastore.updateComputePrivacy(
did,
serviceIndex,
computePrivacy.allowRawAlgorithm,
computePrivacy.allowNetworkAccess,
computePrivacy.trustedAlgorithms,
oldDdo.updated,
signature
)
return result
}
/**
* Retire a DDO (Delete)
* @param {did} string DID.
* @param {Account} account Ethereum account of owner to sign and prove the ownership.
* @return {Promise<string>}
*/
public async retire(did: string, account: Account): Promise<string> {
const oldDdo = await this.ocean.metadatastore.retrieveDDO(did)
// get a signature
const signature = await this.ocean.utils.signature.signForAquarius(
oldDdo.updated,
account
)
let result = null
if (signature != null)
result = await this.ocean.metadatastore.retire(did, oldDdo.updated, signature)
return result
} }
/** /**

View File

@ -388,6 +388,42 @@ describe('Compute flow', () => {
jobId = response.jobId jobId = response.jobId
assert(response.status >= 10) assert(response.status >= 10)
}) })
it('Alice updates Compute Privacy', async () => {
const newComputePrivacy = {
allowRawAlgorithm: false,
allowNetworkAccess: true,
trustedAlgorithms: ['did:op:1234', 'did:op:1235']
}
let computeIndex = 0
for (let i = 0; i < ddo.service.length; i++) {
if (ddo.service[i].type === 'compute') {
computeIndex = i
break
}
}
assert(computeIndex > 0)
const newDdo = await ocean.assets.updateComputePrivacy(
ddo.id,
computeIndex,
newComputePrivacy,
alice
)
assert(newDdo !== null)
await sleep(6000)
const metaData = await ocean.assets.getServiceByType(ddo.id, 'compute')
assert(
metaData.attributes.main.privacy.allowRawAlgorithm ===
newComputePrivacy.allowRawAlgorithm
)
assert(
metaData.attributes.main.privacy.allowNetworkAccess ===
newComputePrivacy.allowNetworkAccess
)
assert(
metaData.attributes.main.privacy.trustedAlgorithms ===
newComputePrivacy.trustedAlgorithms
)
})
// it('Bob restarts compute job', async () => {}) // it('Bob restarts compute job', async () => {})
// it('Bob gets outputs', async () => {}) // it('Bob gets outputs', async () => {})

View File

@ -3,12 +3,14 @@ import { TestContractHandler } from '../TestContractHandler'
import { DataTokens } from '../../src/datatokens/Datatokens' import { DataTokens } from '../../src/datatokens/Datatokens'
import { Ocean } from '../../src/ocean/Ocean' import { Ocean } from '../../src/ocean/Ocean'
import { ConfigHelper } from '../../src/utils/ConfigHelper' import { ConfigHelper } from '../../src/utils/ConfigHelper'
// import config from './config' // import config from './config'
import { assert } from 'console' import { assert } from 'console'
import Web3 from 'web3' import Web3 from 'web3'
import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json'
import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
import { EditableMetadata } from '../../src/lib'
const web3 = new Web3('http://127.0.0.1:8545') const web3 = new Web3('http://127.0.0.1:8545')
function sleep(ms) { function sleep(ms) {
@ -193,4 +195,20 @@ describe('Marketplace flow', () => {
const assets = await ocean.assets.ownerAssets(alice.getId()) const assets = await ocean.assets.ownerAssets(alice.getId())
assert(assets.length > 0) assert(assets.length > 0)
}) })
it('Alice updates metadata', async () => {
const newMetaData: EditableMetadata = {
description: 'new description',
title: 'new title',
links: [{ name: 'link1', type: 'sample', url: 'http://example.net' }]
}
const newDdo = await ocean.assets.editMetadata(ddo.id, newMetaData, alice)
assert(newDdo !== null)
await sleep(6000)
const metaData = await ocean.assets.getServiceByType(ddo.id, 'metadata')
assert(metaData.attributes.main.name === newMetaData.title)
assert(
metaData.attributes.additionalInformation.description === newMetaData.description
)
assert(metaData.attributes.additionalInformation.links === newMetaData.links)
})
}) })