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

Commit

Permalink
Merge branch 'master' into add-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarcia360 committed Dec 28, 2019
2 parents cbc973c + e8271f3 commit 2d8749f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 13 deletions.
14 changes: 1 addition & 13 deletions src/commands/profile/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ 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';

export class CommandOptions extends Options {
@option({
flag: 'p',
description: 'Account private key.',
validator: new PrivateKeyValidator(),
})
privateKey: string;

@option({
flag: 'n',
description: 'Network Type. Example: MAIN_NET, TEST_NET, MIJIN, MIJIN_TEST.',
Expand Down Expand Up @@ -106,13 +98,9 @@ export default class extends Command {
new PasswordValidator().validate(password);
const passwordObject = new Password(password);

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

Expand Down
139 changes: 139 additions & 0 deletions src/commands/profile/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
*
* Copyright 2018-present NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import chalk from 'chalk';
import {Command, command, metadata, option, Options} from 'clime';
import {BlockHttp, NetworkType, Password, SimpleWallet} from 'nem2-sdk';
import * as readlineSync from 'readline-sync';
import {OptionsResolver} from '../../options-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';

export class CommandOptions extends Options {
@option({
flag: 'P',
description: 'Account private key.',
validator: new PrivateKeyValidator(),
})
privateKey: string;

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

@option({
flag: 'u',
description: 'NEM2 Node URL. Example: http://localhost:3000',
})
url: string;

@option({
description: 'Profile name.',
})
profile: string;

@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);
}
}

@command({
description: 'Create a new profile with existing private key',
})
export default class extends Command {
private readonly profileService: ProfileService;

constructor() {
super();
const profileRepository = new ProfileRepository('.nem2rc.json');
this.profileService = new ProfileService(profileRepository);
}

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

const url = OptionsResolver(options,
'url',
() => undefined,
'Introduce 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 simpleWallet: SimpleWallet = SimpleWallet.createFromPrivateKey(
profileName,
passwordObject,
OptionsResolver(options,
'privateKey',
() => undefined,
'Introduce your private key'),
networkType,
);

const blockHttp = new BlockHttp(url);
blockHttp.getBlockByHeight('1')
.subscribe((block) => {
if (block.networkType !== networkType) {
console.log('The network provided and the node network don\'t match.');
} else {
const profile = this.profileService.createNewProfile(simpleWallet,
url as string,
block.generationHash);
if (readlineSync.keyInYN('Do you want to set the account as the default profile?')) {
this.profileService.setDefaultProfile(profileName);
}
console.log(chalk.green('\nProfile stored correctly\n') + profile.toString() + '\n');
}
}, (ignored) => {
let error = '';
error += chalk.red('Error');
error += ' Check if you can reach the NEM2 url provided: ' + url + '/block/1';
console.log(error);
});
}
}

0 comments on commit 2d8749f

Please sign in to comment.