assets.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. const debug = require('debug')('joystream:runtime:assets');
  3. const { Null } = require('@polkadot/types/primitive');
  4. const { _ } = require('lodash');
  5. const { decodeAddress, encodeAddress } = require('@polkadot/keyring');
  6. function parseContentId(contentId) {
  7. try {
  8. return decodeAddress(contentId)
  9. } catch (err) {
  10. return contentId
  11. }
  12. }
  13. /*
  14. * Add asset related functionality to the substrate API.
  15. */
  16. class AssetsApi
  17. {
  18. static async create(base)
  19. {
  20. const ret = new AssetsApi();
  21. ret.base = base;
  22. await ret.init();
  23. return ret;
  24. }
  25. async init(account_file)
  26. {
  27. debug('Init');
  28. }
  29. /*
  30. * Create a data object.
  31. */
  32. async createDataObject(accountId, memberId, contentId, doTypeId, size, ipfs_cid)
  33. {
  34. contentId = parseContentId(contentId)
  35. const tx = this.base.api.tx.dataDirectory.addContent(memberId, contentId, doTypeId, size, ipfs_cid);
  36. await this.base.signAndSend(accountId, tx);
  37. // If the data object constructed properly, we should now be able to return
  38. // the data object from the state.
  39. return this.getDataObject(contentId);
  40. }
  41. /*
  42. * Return the Data Object for a CID
  43. */
  44. async getDataObject(contentId)
  45. {
  46. contentId = parseContentId(contentId)
  47. const obj = await this.base.api.query.dataDirectory.dataObjectByContentId(contentId);
  48. return obj;
  49. }
  50. /*
  51. * Verify the liaison state for a DO:
  52. * - Check the content ID has a DO
  53. * - Check the account is the liaison
  54. * - Check the liaison state is pending
  55. *
  56. * Each failure errors out, success returns the data object.
  57. */
  58. async checkLiaisonForDataObject(storageProviderId, contentId)
  59. {
  60. contentId = parseContentId(contentId)
  61. let obj = await this.getDataObject(contentId);
  62. if (obj.isNone) {
  63. throw new Error(`No DataObject created for content ID: ${contentId}`);
  64. }
  65. if (obj.liaison.neq(storageProviderId)) {
  66. throw new Error(`This storage node is not liaison for the content ID: ${contentId}`);
  67. }
  68. if (obj.liaison_judgement.type != 'Pending') {
  69. throw new Error(`Expected Pending judgement, but found: ${obj.liaison_judgement.type}`);
  70. }
  71. return obj.unwrap();
  72. }
  73. /*
  74. * Changes a data object liaison judgement.
  75. */
  76. async acceptContent(providerAccoundId, storageProviderId, contentId)
  77. {
  78. contentId = parseContentId(contentId)
  79. const tx = this.base.api.tx.dataDirectory.acceptContent(storageProviderId, contentId);
  80. return await this.base.signAndSend(providerAccoundId, tx);
  81. }
  82. /*
  83. * Changes a data object liaison judgement.
  84. */
  85. async rejectContent(providerAccountId, storageProviderId, contentId)
  86. {
  87. contentId = parseContentId(contentId)
  88. const tx = this.base.api.tx.dataDirectory.rejectContent(storageProviderId, contentId);
  89. return await this.base.signAndSend(providerAccountId, tx);
  90. }
  91. /*
  92. * Create storage relationship
  93. */
  94. async createStorageRelationship(providerAccountId, storageProviderId, contentId, callback)
  95. {
  96. contentId = parseContentId(contentId)
  97. const tx = this.base.api.tx.dataObjectStorageRegistry.addRelationship(storageProviderId, contentId);
  98. const subscribed = [['dataObjectStorageRegistry', 'DataObjectStorageRelationshipAdded']];
  99. return await this.base.signAndSend(providerAccountId, tx, 3, subscribed, callback);
  100. }
  101. /*
  102. * Get storage relationship for contentId
  103. */
  104. async getStorageRelationshipAndId(storageProviderId, contentId) {
  105. contentId = parseContentId(contentId)
  106. let rids = await this.base.api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId);
  107. while(rids.length) {
  108. const relationshipId = rids.shift();
  109. let relationship = await this.base.api.query.dataObjectStorageRegistry.relationships(relationshipId);
  110. relationship = relationship.unwrap();
  111. if (relationship.storage_provider.eq(storageProviderId)) {
  112. return ({ relationship, relationshipId });
  113. }
  114. }
  115. return {};
  116. }
  117. async createAndReturnStorageRelationship(providerAccountId, storageProviderId, contentId)
  118. {
  119. contentId = parseContentId(contentId)
  120. return new Promise(async (resolve, reject) => {
  121. try {
  122. await this.createStorageRelationship(providerAccountId, storageProviderId, contentId, (events) => {
  123. events.forEach((event) => {
  124. resolve(event[1].DataObjectStorageRelationshipId);
  125. });
  126. });
  127. } catch (err) {
  128. reject(err);
  129. }
  130. });
  131. }
  132. /*
  133. * Toggle ready state for DOSR.
  134. */
  135. async toggleStorageRelationshipReady(providerAccountId, storageProviderId, dosrId, ready)
  136. {
  137. var tx = ready
  138. ? this.base.api.tx.dataObjectStorageRegistry.setRelationshipReady(storageProviderId, dosrId)
  139. : this.base.api.tx.dataObjectStorageRegistry.unsetRelationshipReady(storageProviderId, dosrId);
  140. return await this.base.signAndSend(providerAccountId, tx);
  141. }
  142. async getKnownContentIds() {
  143. return this.base.api.query.dataDirectory.knownContentIds();
  144. }
  145. }
  146. module.exports = {
  147. AssetsApi: AssetsApi,
  148. }