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:
parent
2994cb5b8a
commit
323e57e6f4
@ -1,4 +1,4 @@
|
||||
export interface ServicePrices {
|
||||
serviceIndex: number
|
||||
price: string
|
||||
cost: string
|
||||
}
|
||||
|
@ -99,7 +99,6 @@ export class OnChainMetadataStore {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
// const data = this.web3.utils.bytesToHex(ddo)
|
||||
const estGas = await this.DDOContract.methods
|
||||
.create(did, flags, data)
|
||||
.estimateGas(function (err, estGas) {
|
||||
@ -127,7 +126,7 @@ export class OnChainMetadataStore {
|
||||
public async updateRaw(
|
||||
did: string,
|
||||
flags: any,
|
||||
ddo: any,
|
||||
data: any,
|
||||
consumerAccount: string
|
||||
): Promise<TransactionReceipt> {
|
||||
if (!this.DDOContract) {
|
||||
@ -135,16 +134,9 @@ export class OnChainMetadataStore {
|
||||
return null
|
||||
}
|
||||
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
|
||||
.update(did, flags, data)
|
||||
.send({ from: consumerAccount, gas: estGas + 1 })
|
||||
.send({ from: consumerAccount })
|
||||
return trxReceipt
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
|
@ -223,23 +223,39 @@ export class Assets extends Instantiable {
|
||||
did: string,
|
||||
newMetadata: EditableMetadata,
|
||||
account: Account
|
||||
): Promise<string> {
|
||||
): Promise<DDO> {
|
||||
const oldDdo = await this.ocean.metadatastore.retrieveDDO(did)
|
||||
// get a signature
|
||||
const signature = await this.ocean.utils.signature.signForAquarius(
|
||||
oldDdo.updated,
|
||||
account
|
||||
let i
|
||||
for (i = 0; i < oldDdo.service.length; i++) {
|
||||
if (oldDdo.service[i].type === 'metadata') {
|
||||
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 (signature != null)
|
||||
result = await this.ocean.metadatastore.editMetadata(
|
||||
did,
|
||||
newMetadata,
|
||||
oldDdo.updated,
|
||||
signature
|
||||
)
|
||||
|
||||
return result
|
||||
if (storeTx) return oldDdo
|
||||
else return null
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,45 +271,22 @@ export class Assets extends Instantiable {
|
||||
serviceIndex: number,
|
||||
computePrivacy: ServiceComputePrivacy,
|
||||
account: Account
|
||||
): Promise<string> {
|
||||
): Promise<DDO> {
|
||||
const oldDdo = await this.ocean.metadatastore.retrieveDDO(did)
|
||||
// get a signature
|
||||
const signature = await this.ocean.utils.signature.signForAquarius(
|
||||
oldDdo.updated,
|
||||
account
|
||||
if (oldDdo.service[serviceIndex].type !== 'compute') return null
|
||||
oldDdo.service[serviceIndex].attributes.main.privacy.allowRawAlgorithm =
|
||||
computePrivacy.allowRawAlgorithm
|
||||
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 (signature != 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
|
||||
if (storeTx) return oldDdo
|
||||
else return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -388,6 +388,42 @@ describe('Compute flow', () => {
|
||||
jobId = response.jobId
|
||||
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 gets outputs', async () => {})
|
||||
|
@ -3,12 +3,14 @@ import { TestContractHandler } from '../TestContractHandler'
|
||||
import { DataTokens } from '../../src/datatokens/Datatokens'
|
||||
import { Ocean } from '../../src/ocean/Ocean'
|
||||
import { ConfigHelper } from '../../src/utils/ConfigHelper'
|
||||
|
||||
// import config from './config'
|
||||
import { assert } from 'console'
|
||||
|
||||
import Web3 from 'web3'
|
||||
import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json'
|
||||
import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
|
||||
import { EditableMetadata } from '../../src/lib'
|
||||
const web3 = new Web3('http://127.0.0.1:8545')
|
||||
|
||||
function sleep(ms) {
|
||||
@ -193,4 +195,20 @@ describe('Marketplace flow', () => {
|
||||
const assets = await ocean.assets.ownerAssets(alice.getId())
|
||||
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)
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user