123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- 'use strict';
- const path = require('path');
- const fs = require('fs');
- const debug = require('debug')('joystream:runtime:identities');
- const { Keyring } = require('@polkadot/keyring');
- const util_crypto = require('@polkadot/util-crypto');
- class IdentitiesApi
- {
- static async create(base, {account_file, passphrase, canPromptForPassphrase})
- {
- const ret = new IdentitiesApi();
- ret.base = base;
- await ret.init(account_file, passphrase, canPromptForPassphrase);
- return ret;
- }
- async init(account_file, passphrase, canPromptForPassphrase)
- {
- debug('Init');
-
- this.keyring = new Keyring();
- this.canPromptForPassphrase = canPromptForPassphrase || false;
-
- try {
- this.key = await this.loadUnlock(account_file, passphrase);
- } catch (err) {
- debug('Error loading account file:', err.message);
- }
- }
-
- async loadUnlock(account_file, passphrase)
- {
- const fullname = path.resolve(account_file);
- 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 await this.base.api.query.members.memberIdsByRootAccountId(decoded);
- }
-
- async firstMemberIdOf(accountId)
- {
- const decoded = this.keyring.decodeAddress(accountId);
- let 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);
-
- var 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 subscribed = [['members', 'MemberRegistered']]
- const tx = this.base.api.tx.members.buyMembership(0, userInfo)
- return new Promise(async (resolve, reject) => {
- try {
- await this.base.signAndSend(accountId, tx, 1, subscribed, (events) => {
- events.forEach((event) => {
- resolve(event[1].MemberId)
- })
- })
- } catch (err) {
- reject(err)
- }
- })
- }
- }
- module.exports = {
- IdentitiesApi: IdentitiesApi,
- }
|