assets.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, contentId, doTypeId, size)
  33. {
  34. contentId = parseContentId(contentId)
  35. const tx = this.base.api.tx.dataDirectory.addContent(contentId, doTypeId, size);
  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 await 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(accountId, 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. const encoded = encodeAddress(obj.raw.liaison);
  66. if (encoded != accountId) {
  67. throw new Error(`This storage node is not liaison for the content ID: ${contentId}`);
  68. }
  69. if (obj.raw.liaison_judgement.type != 'Pending') {
  70. throw new Error(`Expected Pending judgement, but found: ${obj.raw.liaison_judgement.type}`);
  71. }
  72. return obj.unwrap();
  73. }
  74. /*
  75. * Changes a data object liaison judgement.
  76. */
  77. async acceptContent(accountId, contentId)
  78. {
  79. contentId = parseContentId(contentId)
  80. const tx = this.base.api.tx.dataDirectory.acceptContent(contentId);
  81. return await this.base.signAndSend(accountId, tx);
  82. }
  83. /*
  84. * Changes a data object liaison judgement.
  85. */
  86. async rejectContent(accountId, contentId)
  87. {
  88. contentId = parseContentId(contentId)
  89. const tx = this.base.api.tx.dataDirectory.rejectContent(contentId);
  90. return await this.base.signAndSend(accountId, tx);
  91. }
  92. /*
  93. * Create storage relationship
  94. */
  95. async createStorageRelationship(accountId, contentId, callback)
  96. {
  97. contentId = parseContentId(contentId)
  98. const tx = this.base.api.tx.dataObjectStorageRegistry.addRelationship(contentId);
  99. const subscribed = [['dataObjectStorageRegistry', 'DataObjectStorageRelationshipAdded']];
  100. return await this.base.signAndSend(accountId, tx, 3, subscribed, callback);
  101. }
  102. /*
  103. * Get storage relationship for contentId
  104. */
  105. async getStorageRelationshipAndId(accountId, contentId) {
  106. contentId = parseContentId(contentId)
  107. let rids = await this.base.api.query.dataObjectStorageRegistry.relationshipsByContentId(contentId);
  108. while(rids.length) {
  109. const relationshipId = rids.shift();
  110. let relationship = await this.base.api.query.dataObjectStorageRegistry.relationships(relationshipId);
  111. relationship = relationship.unwrap();
  112. if (relationship.storage_provider.eq(decodeAddress(accountId))) {
  113. return ({ relationship, relationshipId });
  114. }
  115. }
  116. return {};
  117. }
  118. async createAndReturnStorageRelationship(accountId, contentId)
  119. {
  120. contentId = parseContentId(contentId)
  121. return new Promise(async (resolve, reject) => {
  122. try {
  123. await this.createStorageRelationship(accountId, contentId, (events) => {
  124. events.forEach((event) => {
  125. resolve(event[1].DataObjectStorageRelationshipId);
  126. });
  127. });
  128. } catch (err) {
  129. reject(err);
  130. }
  131. });
  132. }
  133. /*
  134. * Toggle ready state for DOSR.
  135. */
  136. async toggleStorageRelationshipReady(accountId, dosrId, ready)
  137. {
  138. var tx = ready
  139. ? this.base.api.tx.dataObjectStorageRegistry.setRelationshipReady(dosrId)
  140. : this.base.api.tx.dataObjectStorageRegistry.unsetRelationshipReady(dosrId);
  141. return await this.base.signAndSend(accountId, tx);
  142. }
  143. async getKnownContentIds() {
  144. return this.base.api.query.dataDirectory.knownContentIds();
  145. }
  146. }
  147. module.exports = {
  148. AssetsApi: AssetsApi,
  149. }