123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 'use strict';
- const debug = require('debug')('joystream:runtime:assets');
- const { Null } = require('@polkadot/types/primitive');
- const { _ } = require('lodash');
- const { decodeAddress, encodeAddress } = require('@polkadot/keyring');
- function parseContentId(contentId) {
- try {
- return decodeAddress(contentId)
- } catch (err) {
- return contentId
- }
- }
- /*
- * Add asset related functionality to the substrate API.
- */
- class AssetsApi
- {
- static async create(base)
- {
- const ret = new AssetsApi();
- ret.base = base;
- await ret.init();
- return ret;
- }
- async init(account_file)
- {
- debug('Init');
- }
- /*
- * Create a data object.
- */
- async createDataObject(accountId, contentId, doTypeId, size)
- {
- contentId = parseContentId(contentId)
- const tx = this.base.api.tx.dataDirectory.addContent(contentId, doTypeId, size);
- await this.base.signAndSend(accountId, tx);
- // If the data object constructed properly, we should now be able to return
- // the data object from the state.
- return await this.getDataObject(contentId);
- }
- /*
- * Return the Data Object for a CID
- */
- async getDataObject(contentId)
- {
- contentId = parseContentId(contentId)
- const obj = await this.base.api.query.dataDirectory.dataObjectByContentId(contentId);
- return obj;
- }
- /*
- * Verify the liaison state for a DO:
- * - Check the content ID has a DO
- * - Check the account is the liaison
- * - Check the liaison state is pending
- *
- * Each failure errors out, success returns the data object.
- */
- async checkLiaisonForDataObject(accountId, contentId)
- {
- contentId = parseContentId(contentId)
- let obj = await this.getDataObject(contentId);
- if (obj.isNone) {
- throw new Error(`No DataObject created for content ID: ${contentId}`);
- }
- const encoded = encodeAddress(obj.raw.liaison);
- if (encoded != accountId) {
- throw new Error(`This storage node is not liaison for the content ID: ${contentId}`);
- }
- if (obj.raw.liaison_judgement.type != 'Pending') {
- throw new Error(`Expected Pending judgement, but found: ${obj.raw.liaison_judgement.type}`);
- }
- return obj.unwrap();
- }
- /*
- * Changes a data object liaison judgement.
- */
- async acceptContent(accountId, contentId)
- {
- contentId = parseContentId(contentId)
- const tx = this.base.api.tx.dataDirectory.acceptContent(contentId);
- return await this.base.signAndSend(accountId, tx);
- }
- /*
- * Changes a data object liaison judgement.
- */
- async rejectContent(accountId, contentId)
- {
- contentId = parseContentId(contentId)
- const tx = this.base.api.tx.dataDirectory.rejectContent(contentId);
- return await this.base.signAndSend(accountId, tx);
- }
- /*
- * Create storage relationship
- */
- async createStorageRelationship(accountId, contentId, callback)
- {
- contentId = parseContentId(contentId)
- const tx = this.base.api.tx.dataObjectStorageRegistry.addRelationship(contentId);
- const subscribed = [['dataObjectStorageRegistry', 'DataObjectStorageRelationshipAdded']];
- return await this.base.signAndSend(accountId, tx, 3, subscribed, callback);
- }
- /*
- * Get storage relationship for contentId
- */
- async getStorageRelationshipAndId(accountId, contentId) {
- contentId = parseContentId(contentId)
- let rids = await this.base.api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId);
- while(rids.length) {
- const relationshipId = rids.shift();
- let relationship = await this.base.api.query.dataObjectStorageRegistry.relationships(relationshipId);
- relationship = relationship.unwrap();
- if (relationship.storage_provider.eq(decodeAddress(accountId))) {
- return ({ relationship, relationshipId });
- }
- }
- return {};
- }
- async createAndReturnStorageRelationship(accountId, contentId)
- {
- contentId = parseContentId(contentId)
- return new Promise(async (resolve, reject) => {
- try {
- await this.createStorageRelationship(accountId, contentId, (events) => {
- events.forEach((event) => {
- resolve(event[1].DataObjectStorageRelationshipId);
- });
- });
- } catch (err) {
- reject(err);
- }
- });
- }
- /*
- * Toggle ready state for DOSR.
- */
- async toggleStorageRelationshipReady(accountId, dosrId, ready)
- {
- var tx = ready
- ? this.base.api.tx.dataObjectStorageRegistry.setRelationshipReady(dosrId)
- : this.base.api.tx.dataObjectStorageRegistry.unsetRelationshipReady(dosrId);
- return await this.base.signAndSend(accountId, tx);
- }
- async getKnownContentIds() {
- return this.base.api.query.dataDirectory.knownContentIds();
- }
- }
- module.exports = {
- AssetsApi: AssetsApi,
- }
|