Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Commit

Permalink
Added profile resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarcia360 committed Dec 28, 2019
1 parent 5e4a9e8 commit 5502397
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 81 deletions.
43 changes: 13 additions & 30 deletions src/commands/account/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import chalk from 'chalk';
import * as Table from 'cli-table3';
import {HorizontalTable} from 'cli-table3';
import {Command, command, metadata, option, Options} from 'clime';
import {Account, BlockHttp, NetworkType, Password, SimpleWallet} from 'nem2-sdk';
import {Account, BlockHttp, Password, SimpleWallet} from 'nem2-sdk';
import * as readlineSync from 'readline-sync';
import {OptionsResolver} from '../../options-resolver';
import {NetworkTypeResolver} from '../../resolvers/networkType.resolver';
import {PasswordResolver} from '../../resolvers/password.resolver';
import {ProfileNameResolver} from '../../resolvers/profile.resolver';
import {URLResolver} from '../../resolvers/url.resolver';
import {ProfileRepository} from '../../respository/profile.repository';
import {ProfileService} from '../../service/profile.service';
import {NetworkValidator} from '../../validators/network.validator';
import {PasswordValidator} from '../../validators/password.validator';

export class CommandOptions extends Options {
Expand All @@ -48,22 +50,16 @@ export class CommandOptions extends Options {

@option({
flag: 'n',
description: 'Network Type (MAIN_NET, TEST_NET, MIJIN, MIJIN_TEST).',
validator: new NetworkValidator(),
description: 'Network Type. (0: MAIN_NET, 1: TEST_NET, 2: MIJIN, 3: MIJIN_TEST)',
})
network: string;
network: number;

@option({
flag: 'p',
description: '(Optional) Profile password',
validator: new PasswordValidator(),
})
password: string;

getNetwork(network: any): NetworkType {
new NetworkValidator().validate(network);
return parseInt(NetworkType[network], 10);
}
}

export class AccountCredentialsTable {
Expand Down Expand Up @@ -110,31 +106,18 @@ export default class extends Command {

@metadata
execute(options: CommandOptions) {
const networkType = options.getNetwork(OptionsResolver(options,
'network',
() => undefined,
'Enter a network type: '));

const profile = options.profile || readlineSync.question('Insert the profile name: ');
profile.trim();

const password = options.password || readlineSync.question('Enter your wallet password: ');
new PasswordValidator().validate(password);
const passwordObject = new Password(password);
const simpleWallet = SimpleWallet.create(profile, passwordObject, networkType);

let text = new AccountCredentialsTable(simpleWallet.open(passwordObject), passwordObject).toString();
const networkType = new NetworkTypeResolver().resolve(options);
const profile = new ProfileNameResolver().resolve(options);
const password = new PasswordResolver().resolve(options);
const simpleWallet = SimpleWallet.create(profile, password, networkType);
let text = new AccountCredentialsTable(simpleWallet.open(password), password).toString();

if (!options.save && readlineSync.keyInYN('Do you want to save the account?')) {
options.save = true;
}

if (options.save) {
const url = OptionsResolver(options,
'url',
() => undefined,
'Enter the NEM2 node URL. (Example: http://localhost:3000): ').trim();

const url = new URLResolver().resolve(options);
const blockHttp = new BlockHttp(url);

blockHttp.getBlockByHeight('1')
Expand Down
58 changes: 16 additions & 42 deletions src/commands/profile/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/
import chalk from 'chalk';
import {Command, command, metadata, option, Options} from 'clime';
import {BlockHttp, NetworkType, Password, SimpleWallet} from 'nem2-sdk';
import {BlockHttp, SimpleWallet} from 'nem2-sdk';
import * as readlineSync from 'readline-sync';
import {OptionsResolver} from '../../options-resolver';
import {NetworkTypeResolver} from '../../resolvers/networkType.resolver';
import {PasswordResolver} from '../../resolvers/password.resolver';
import {PrivateKeyResolver} from '../../resolvers/privateKey.resolver';
import {ProfileNameResolver} from '../../resolvers/profile.resolver';
import {URLResolver} from '../../resolvers/url.resolver';
import {ProfileRepository} from '../../respository/profile.repository';
import {ProfileService} from '../../service/profile.service';
import {NetworkValidator} from '../../validators/network.validator';
import {PasswordValidator} from '../../validators/password.validator';
import {PrivateKeyValidator} from '../../validators/privateKey.validator';

Expand All @@ -36,10 +39,9 @@ export class CommandOptions extends Options {

@option({
flag: 'n',
description: 'Network Type. Example: MAIN_NET, TEST_NET, MIJIN, MIJIN_TEST.',
validator: new NetworkValidator(),
description: 'Network Type. (0: MAIN_NET, 1: TEST_NET, 2: MIJIN, 3: MIJIN_TEST)',
})
network: string;
network: number;

@option({
flag: 'u',
Expand All @@ -58,11 +60,6 @@ export class CommandOptions extends Options {
validator: new PasswordValidator(),
})
password: string;

getNetwork(network: any): NetworkType {
new NetworkValidator().validate(network);
return parseInt(NetworkType[network], 10);
}
}

@command({
Expand All @@ -80,43 +77,20 @@ export default class extends Command {

@metadata
execute(options: CommandOptions) {
const networkType = options.getNetwork(OptionsResolver(options,
'network',
() => undefined,
'Enter network type (MIJIN_TEST, MIJIN, MAIN_NET, TEST_NET): '));

const url = OptionsResolver(options,
'url',
() => undefined,
'Enter NEM 2 Node URL. (Example: http://localhost:3000): ');

let profileName: string;
if (options.profile) {
profileName = options.profile;
} else {
profileName = readlineSync.question('Insert profile name: ');
}
profileName.trim();

const password = OptionsResolver(options,
'password',
() => undefined,
'Enter your wallet password: ');

new PasswordValidator().validate(password);
const passwordObject = new Password(password);
const networkType = new NetworkTypeResolver().resolve(options);
const url = new URLResolver().resolve(options);
const profileName = new ProfileNameResolver().resolve(options);
const password = new PasswordResolver().resolve(options);
const privateKey = new PrivateKeyResolver().resolve(options);
const blockHttp = new BlockHttp(url);

const simpleWallet: SimpleWallet = SimpleWallet.createFromPrivateKey(
profileName,
passwordObject,
OptionsResolver(options,
'privateKey',
() => undefined,
'Enter your private key: '),
password,
privateKey,
networkType,
);

const blockHttp = new BlockHttp(url);
blockHttp.getBlockByHeight('1')
.subscribe((block) => {
if (block.networkType !== networkType) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/transaction/accountaddressrestriction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CommandOptions extends AnnounceTransactionsOptions {
description: 'Restriction flags.' +
'(0: AllowOutgoingAddress, 1: AllowIncomingAddress, 2: BlockOutgoingAddress, 3: BlockIncomingAddress)',
})
flags: string;
flags: number;

@option({
flag: 'a',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/transaction/accountmosaicrestriction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class CommandOptions extends AnnounceTransactionsOptions {
description: 'Restriction flags.' +
'(0: AllowMosaic, 1: BlockMosaic)',
})
flags: string;
flags: number;

@option({
flag: 'a',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/transaction/accountoperationrestriction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CommandOptions extends AnnounceTransactionsOptions {
description: 'Restriction flag. (0: AllowOutgoingTransactionType, 1: BlockOutgoingTransactionType)',
validator: new AccountRestrictionTypeValidator(),
})
flags: string;
flags: number;

@option({
flag: 'a',
Expand Down
9 changes: 4 additions & 5 deletions src/model/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {ISimpleWalletDTO} from 'nem2-sdk/dist/src/infrastructure/wallet/simpleWa
import * as readlineSync from 'readline-sync';
import {ProfileOptions} from '../profile.command';
import {PasswordValidator} from '../validators/password.validator';
import {PasswordResolver} from '../resolvers/password.resolver';

/**
* Profile data transfer object.
Expand Down Expand Up @@ -128,12 +129,10 @@ export class Profile {
* @returns {Account}
*/
decrypt(options: ProfileOptions): Account {
const password = options.password || readlineSync.question('Enter your wallet password: ');
new PasswordValidator().validate(password);
const passwordObject = new Password(password);
if (!this.isPasswordValid(passwordObject)) {
const password = new PasswordResolver().resolve(options);
if (!this.isPasswordValid(password)) {
throw new ExpectedError('The password provided does not match your account password');
}
return this.simpleWallet.open(passwordObject);
return this.simpleWallet.open(password);
}
}
9 changes: 9 additions & 0 deletions src/options-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ export const OptionsChoiceResolver = (options: any,
const readline = readlineDependency || readlineSync;
return options[key] !== undefined ? options[key] : (readline.keyInSelect(choices, promptText));
};

export const OptionsPasswordResolver = (options: any,
key: string,
secondSource: () => string | undefined,
promptText: string,
readlineDependency?: any) => {
const readline = readlineDependency || readlineSync;
return options[key] !== undefined ? options[key] : (secondSource() || readline.questionNewPassword(promptText));
};
31 changes: 31 additions & 0 deletions src/resolvers/networkType.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {NetworkType} from 'nem2-sdk';
import {Profile} from '../model/profile';
import {OptionsChoiceResolver} from '../options-resolver';
import {ProfileOptions} from '../profile.command';
import {NetworkValidator} from '../validators/network.validator';
import {Resolver} from './resolver';

/**
* Restriction account address flags resolver
*/
export class NetworkTypeResolver implements Resolver {

/**
* Resolves a network type provided by the user.
* @param {ProfileOptions} options - Command options.
* @param {Profile} secondSource - Secondary data source.
* @param {string} altText - Alternative text.
* @returns {number}
*/
resolve(options: ProfileOptions, secondSource?: Profile, altText?: string): any {
const choices = ['MAIN_NET', 'TEST_NET', 'MIJIN', 'MIJIN_TEST'];
const index = +OptionsChoiceResolver(options,
'network',
altText ? altText : 'Select the network type: ',
choices,
);
const networkFriendlyName = choices[index] as any;
new NetworkValidator().validate(networkFriendlyName);
return NetworkType[networkFriendlyName];
}
}
28 changes: 28 additions & 0 deletions src/resolvers/password.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {Password} from 'nem2-sdk';
import {Profile} from '../model/profile';
import {OptionsPasswordResolver} from '../options-resolver';
import {ProfileOptions} from '../profile.command';
import {PasswordValidator} from '../validators/password.validator';
import {Resolver} from './resolver';

/**
* Password resolver
*/
export class PasswordResolver implements Resolver {

/**
* Resolves a password provided by the user.
* @param {ProfileOptions} options - Command options.
* @param {Profile} secondSource - Secondary data source.
* @param {string} altText - Alternative text.
* @returns {Password}
*/
resolve(options: ProfileOptions, secondSource?: Profile, altText?: string): any {
const resolution = OptionsPasswordResolver(options,
'password',
() => undefined,
'Enter your wallet password: ');
new PasswordValidator().validate(resolution);
return new Password(resolution);
}
}
29 changes: 29 additions & 0 deletions src/resolvers/privateKey.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Password} from 'nem2-sdk';
import {Profile} from '../model/profile';
import {OptionsPasswordResolver} from '../options-resolver';
import {ProfileOptions} from '../profile.command';
import {PasswordValidator} from '../validators/password.validator';
import {Resolver} from './resolver';
import {PrivateKeyValidator} from '../validators/privateKey.validator';

/**
* Private key resolver
*/
export class PrivateKeyResolver implements Resolver {

/**
* Resolves a private key provided by the user.
* @param {ProfileOptions} options - Command options.
* @param {Profile} secondSource - Secondary data source.
* @param {string} altText - Alternative text.
* @returns {Password}
*/
resolve(options: ProfileOptions, secondSource?: Profile, altText?: string): any {
const resolution = OptionsPasswordResolver(options,
'privateKey',
() => undefined,
'Enter your private key: ').trim();
new PrivateKeyValidator().validate(resolution);
return resolution;
}
}
27 changes: 27 additions & 0 deletions src/resolvers/profile.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Password} from 'nem2-sdk';
import {Profile} from '../model/profile';
import {OptionsPasswordResolver, OptionsResolver} from '../options-resolver';
import {ProfileOptions} from '../profile.command';
import {PasswordValidator} from '../validators/password.validator';
import {Resolver} from './resolver';

/**
* Profile name resolver
*/
export class ProfileNameResolver implements Resolver {

/**
* Resolves a profile name provided by the user.
* @param {ProfileOptions} options - Command options.
* @param {Profile} secondSource - Secondary data source.
* @param {string} altText - Alternative text.
* @returns {string}
*/
resolve(options: ProfileOptions, secondSource?: Profile, altText?: string): any {
const resolution = OptionsResolver(options,
'profile',
() => undefined,
'Enter profile name: ').trim();
return resolution;
}
}
2 changes: 1 addition & 1 deletion src/validators/privateKey.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PrivateKeyValidator implements Validator<string> {
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: string, context: ValidationContext): void {
validate(value: string, context?: ValidationContext): void {
if (value.length !== 64 || !/^[0-9a-fA-F]+$/.test(value)) {
throw new ExpectedError('private key should be a 64 characters hexadecimal string');
}
Expand Down

0 comments on commit 5502397

Please sign in to comment.