1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-01 15:55:34 +01:00
docs/developers/using-ocean-libraries/update-metadata.md

103 lines
3.1 KiB
Markdown
Raw Normal View History

# Update Metadata
2022-07-25 12:17:51 +02:00
This tutorial will guide you to update an existing asset published on-chain using Ocean libraries. The tutorial assumes that you already have the `did` of the asset which needs to be updated. In this tutorial, we will update the name, description, tags of the data NFT. Please refer [the page on DDO](../../core-concepts/did-ddo.md) to know more about additional the fields which can be updated.
#### Prerequisites
2022-07-25 12:17:51 +02:00
- [Obtain an API key](configuration.md#obtaining-api-key-for-ethereum-node-provider)
- [Set up the .env file](configuration.md#create-a-.env-file)
- [Install the dependencies](configuration.md#setup-dependencies)
- [Create a configuration file](configuration.md#create-a-configuration-file)
2022-07-25 12:17:51 +02:00
{% hint style="info" %}
The variable **AQUARIUS\_URL** and **PROVIDER\_URL** should be set correctly in `.env` file
{% endhint %}
2022-07-25 12:17:51 +02:00
#### Create a script to update the metadata
Create a new file in the same working directory where configuration file (`config.py`/`config.js`) and `.env` files are present, and copy the code as listed below.  
2022-07-25 12:17:51 +02:00
{% tabs %}
{% tab title="ocean.js" %}
2022-07-26 11:59:58 +02:00
{% code title="updateMetadata.js" %}
```javascript
// Import dependencies
2022-07-26 11:59:58 +02:00
const {
Nft,
ProviderInstance,
getHash,
Aquarius
} = require('@oceanprotocol/lib');
const { SHA256 } = require('crypto-js');
const Web3 = require('web3');
const { web3Provider, oceanConfig } = require('./config');
2022-07-25 12:17:51 +02:00
// Create a web3 instance
2022-07-26 11:59:58 +02:00
const web3 = new Web3(web3Provider);
// Create Aquarius instance
2022-07-26 11:59:58 +02:00
const aquarius = new Aquarius(oceanConfig.metadataCacheUri);
const nft = new Nft(web3);
const providerUrl = oceanConfig.providerUri;
// replace the did here
const did = "did:op:a419f07306d71f3357f8df74807d5d12bddd6bcd738eb0b461470c64859d6f0f";
// This function takes did as a parameter and updates the data NFT information
2022-07-26 11:59:58 +02:00
const setMetadata = async (did) => {
const accounts = await web3.eth.getAccounts();
const publisherAccount = accounts[0];
// Fetch ddo from Aquarius
2022-07-26 11:59:58 +02:00
const ddo = await aquarius.resolve(did);
// update the ddo here
ddo.metadata.name = "Sample dataset v2";
ddo.metadata.description = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam";
ddo.metadata.tags = ["new tag1", "new tag2"];
providerResponse = await ProviderInstance.encrypt(ddo, providerUrl);
const encryptedResponse = await providerResponse;
const metadataHash = getHash(JSON.stringify(ddo));
// Update the data NFT metadata
2022-07-26 11:59:58 +02:00
await nft.setMetadata(
ddo.nftAddress,
publisherAccount,
0,
providerUrl,
'',
'0x2',
encryptedResponse,
`0x${metadataHash}`
);
// Check if ddo is correctly udpated in Aquarius
2022-07-26 11:59:58 +02:00
await aquarius.waitForAqua(ddo.id);
console.log(`Resolved asset did [${ddo.id}]from aquarius.`);
console.log(`Updated name: [${ddo.metadata.name}].`);
console.log(`Updated description: [${ddo.metadata.description}].`);
console.log(`Updated tags: [${ddo.metadata.tags}].`);
};
// Call setMetadata(...) function defined above
2022-07-26 11:59:58 +02:00
setMetadata(did).then(() => {
process.exit();
}).catch((err) => {
console.error(err);
process.exit(1);
});
```
{% endcode %}
Execute the script
```bash
node updateMetadata.js
```
2022-07-25 12:17:51 +02:00
{% endtab %}
{% endtabs %}