identities.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict'
  19. const path = require('path')
  20. const fs = require('fs')
  21. const debug = require('debug')('joystream:runtime:identities')
  22. const { Keyring } = require('@polkadot/keyring')
  23. const utilCrypto = require('@polkadot/util-crypto')
  24. /*
  25. * Add identity management to the substrate API.
  26. *
  27. * This loosely groups: accounts, key management, and membership.
  28. */
  29. class IdentitiesApi {
  30. static async create(base, { accountFile, passphrase, canPromptForPassphrase }) {
  31. const ret = new IdentitiesApi()
  32. ret.base = base
  33. await ret.init(accountFile, passphrase, canPromptForPassphrase)
  34. return ret
  35. }
  36. async init(accountFile, passphrase, canPromptForPassphrase) {
  37. debug('Init')
  38. // Creatre keyring
  39. this.keyring = new Keyring()
  40. this.canPromptForPassphrase = canPromptForPassphrase || false
  41. // Load account file, if possible.
  42. try {
  43. this.key = await this.loadUnlock(accountFile, passphrase)
  44. } catch (err) {
  45. debug('Error loading account file:', err.message)
  46. }
  47. }
  48. /*
  49. * Load a key file and unlock it if necessary.
  50. */
  51. async loadUnlock(accountFile, passphrase) {
  52. const fullname = path.resolve(accountFile)
  53. debug('Initializing key from', fullname)
  54. const key = this.keyring.addFromJson(require(fullname))
  55. await this.tryUnlock(key, passphrase)
  56. debug('Successfully initialized with address', key.address)
  57. return key
  58. }
  59. /*
  60. * Try to unlock a key if it isn't already unlocked.
  61. * passphrase should be supplied as argument.
  62. */
  63. async tryUnlock(key, passphrase) {
  64. if (!key.isLocked) {
  65. debug('Key is not locked, not attempting to unlock')
  66. return
  67. }
  68. // First try with an empty passphrase - for convenience
  69. try {
  70. key.decodePkcs8('')
  71. if (passphrase) {
  72. debug('Key was not encrypted, supplied passphrase was ignored')
  73. }
  74. return
  75. } catch (err) {
  76. // pass
  77. }
  78. // Then with supplied passphrase
  79. try {
  80. debug('Decrypting with supplied passphrase')
  81. key.decodePkcs8(passphrase)
  82. return
  83. } catch (err) {
  84. // pass
  85. }
  86. // If that didn't work, ask for a passphrase if appropriate
  87. if (this.canPromptForPassphrase) {
  88. passphrase = await this.askForPassphrase(key.address)
  89. key.decodePkcs8(passphrase)
  90. return
  91. }
  92. throw new Error('invalid passphrase supplied')
  93. }
  94. /*
  95. * Ask for a passphrase
  96. */
  97. /* eslint-disable class-methods-use-this */
  98. // Disable lint because the method used by a mocking library.
  99. askForPassphrase(address) {
  100. // Query for passphrase
  101. const prompt = require('password-prompt')
  102. return prompt(`Enter passphrase for ${address}: `, { required: false })
  103. }
  104. /*
  105. * Return true if the account is a root account of a member
  106. */
  107. async isMember(accountId) {
  108. const memberIds = await this.memberIdsOf(accountId) // return array of member ids
  109. return memberIds.length > 0 // true if at least one member id exists for the acccount
  110. }
  111. async getMembersEntries() {
  112. const memberEntries = await this.base.api.query.members.membershipById.entries()
  113. memberEntries.map(([storageKey, member]) => [storageKey.args[0], member])
  114. }
  115. /*
  116. * Return all the member IDs of an account by the root account id
  117. */
  118. async memberIdsOf(accountId) {
  119. const decoded = this.keyring.decodeAddress(accountId)
  120. // FIXME: Very slow, but probably the only way to retrieve this data now
  121. return this.getMembersEntries()
  122. .filter(([, member]) => member.root_account.eq(decoded))
  123. .map(([id]) => id)
  124. }
  125. /*
  126. * Return all the member IDs of an account by the controller account id
  127. */
  128. async memberIdsOfController(accountId) {
  129. const decoded = this.keyring.decodeAddress(accountId)
  130. // FIXME: Very slow, but probably the only way to retrieve this data now
  131. return this.getMembersEntries()
  132. .filter(([, member]) => member.controller_account.eq(decoded))
  133. .map(([id]) => id)
  134. }
  135. /*
  136. * Return the first member ID of an account, or undefined if not a member root account.
  137. */
  138. async firstMemberIdOf(accountId) {
  139. return this.memberIdsOf(accountId)[0]
  140. }
  141. /*
  142. * Export a key pair to JSON. Will ask for a passphrase.
  143. */
  144. async exportKeyPair(accountId) {
  145. const passphrase = await this.askForPassphrase(accountId)
  146. // Produce JSON output
  147. return this.keyring.toJson(accountId, passphrase)
  148. }
  149. /*
  150. * Export a key pair and write it to a JSON file with the account ID as the
  151. * name.
  152. */
  153. async writeKeyPairExport(accountId, prefix) {
  154. // Generate JSON
  155. const data = await this.exportKeyPair(accountId)
  156. // Write JSON
  157. let filename = `${data.address}.json`
  158. if (prefix) {
  159. const path = require('path')
  160. filename = path.resolve(prefix, filename)
  161. }
  162. fs.writeFileSync(filename, JSON.stringify(data), {
  163. encoding: 'utf8',
  164. mode: 0o600,
  165. })
  166. return filename
  167. }
  168. /*
  169. * Register account id with userInfo as a new member
  170. * returns new member id
  171. */
  172. async registerMember(accountId, userInfo) {
  173. const tx = this.base.api.tx.members.buyMembership({
  174. root_account: accountId,
  175. controller_account: accountId,
  176. handle: userInfo.handle,
  177. })
  178. return this.base.signAndSendThenGetEventResult(accountId, tx, {
  179. module: 'members',
  180. event: 'MembershipBought',
  181. type: 'MemberId',
  182. index: 0,
  183. })
  184. }
  185. /*
  186. * Injects a keypair and sets it as the default identity
  187. */
  188. useKeyPair(keyPair) {
  189. this.key = this.keyring.addPair(keyPair)
  190. }
  191. /*
  192. * Create a new role key. If no name is given,
  193. * default to 'storage'.
  194. */
  195. async createNewRoleKey(name) {
  196. name = name || 'storage-provider'
  197. // Generate new key pair
  198. const keyPair = utilCrypto.naclKeypairFromRandom()
  199. // Encode to an address.
  200. const addr = this.keyring.encodeAddress(keyPair.publicKey)
  201. debug('Generated new key pair with address', addr)
  202. // Add to key wring. We set the meta to identify the account as
  203. // a role key.
  204. const meta = {
  205. name: `${name} role account`,
  206. }
  207. const createPair = require('@polkadot/keyring/pair').default
  208. const pair = createPair('ed25519', keyPair, meta)
  209. this.keyring.addPair(pair)
  210. return pair
  211. }
  212. getSudoAccount() {
  213. return this.base.api.query.sudo.key()
  214. }
  215. }
  216. module.exports = {
  217. IdentitiesApi,
  218. }