123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- 'use strict'
- const path = require('path')
- const fs = require('fs')
- const debug = require('debug')('joystream:runtime:identities')
- const { Keyring } = require('@polkadot/keyring')
- const utilCrypto = require('@polkadot/util-crypto')
- class IdentitiesApi {
- static async create(base, { accountFile, passphrase, canPromptForPassphrase }) {
- const ret = new IdentitiesApi()
- ret.base = base
- await ret.init(accountFile, passphrase, canPromptForPassphrase)
- return ret
- }
- async init(accountFile, passphrase, canPromptForPassphrase) {
- debug('Init')
-
- this.keyring = new Keyring()
- this.canPromptForPassphrase = canPromptForPassphrase || false
-
- try {
- this.key = await this.loadUnlock(accountFile, passphrase)
- } catch (err) {
- debug('Error loading account file:', err.message)
- }
- }
-
- async loadUnlock(accountFile, passphrase) {
- const fullname = path.resolve(accountFile)
- debug('Initializing key from', fullname)
- const key = this.keyring.addFromJson(require(fullname))
- await this.tryUnlock(key, passphrase)
- debug('Successfully initialized with address', key.address)
- return key
- }
-
- async tryUnlock(key, passphrase) {
- if (!key.isLocked) {
- debug('Key is not locked, not attempting to unlock')
- return
- }
-
- try {
- key.decodePkcs8('')
- if (passphrase) {
- debug('Key was not encrypted, supplied passphrase was ignored')
- }
- return
- } catch (err) {
-
- }
-
- try {
- debug('Decrypting with supplied passphrase')
- key.decodePkcs8(passphrase)
- return
- } catch (err) {
-
- }
-
- if (this.canPromptForPassphrase) {
- passphrase = await this.askForPassphrase(key.address)
- key.decodePkcs8(passphrase)
- return
- }
- throw new Error('invalid passphrase supplied')
- }
-
-
-
- askForPassphrase(address) {
-
- const prompt = require('password-prompt')
- return prompt(`Enter passphrase for ${address}: `, { required: false })
- }
-
- async isMember(accountId) {
- const memberIds = await this.memberIdsOf(accountId)
- return memberIds.length > 0
- }
-
- async memberIdsOf(accountId) {
- const decoded = this.keyring.decodeAddress(accountId)
- return this.base.api.query.members.memberIdsByRootAccountId(decoded)
- }
-
- async firstMemberIdOf(accountId) {
- const decoded = this.keyring.decodeAddress(accountId)
- const ids = await this.base.api.query.members.memberIdsByRootAccountId(decoded)
- return ids[0]
- }
-
- async exportKeyPair(accountId) {
- const passphrase = await this.askForPassphrase(accountId)
-
- return this.keyring.toJson(accountId, passphrase)
- }
-
- async writeKeyPairExport(accountId, prefix) {
-
- const data = await this.exportKeyPair(accountId)
-
- let filename = `${data.address}.json`
- if (prefix) {
- const path = require('path')
- filename = path.resolve(prefix, filename)
- }
- fs.writeFileSync(filename, JSON.stringify(data), {
- encoding: 'utf8',
- mode: 0o600,
- })
- return filename
- }
-
- async registerMember(accountId, userInfo) {
- const tx = this.base.api.tx.members.buyMembership(0, userInfo)
- return this.base.signAndSendThenGetEventResult(accountId, tx, {
- eventModule: 'members',
- eventName: 'MemberRegistered',
- eventProperty: 'MemberId',
- })
- }
-
- useKeyPair(keyPair) {
- this.key = this.keyring.addPair(keyPair)
- }
-
- async createNewRoleKey(name) {
- name = name || 'storage-provider'
-
- const keyPair = utilCrypto.naclKeypairFromRandom()
-
- const addr = this.keyring.encodeAddress(keyPair.publicKey)
- debug('Generated new key pair with address', addr)
-
-
- const meta = {
- name: `${name} role account`,
- }
- const createPair = require('@polkadot/keyring/pair').default
- const pair = createPair('ed25519', keyPair, meta)
- this.keyring.addPair(pair)
- return pair
- }
- getSudoAccount() {
- return this.base.api.query.sudo.key()
- }
- }
- module.exports = {
- IdentitiesApi,
- }
|