Commit d9cf0208 authored by dev0tion's avatar dev0tion

Update wallet creation to include Bitcoin and Stratis

parent 6418f25b
import { Component, Injectable } from '@angular/core'; import { Component, Injectable, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms'; import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
...@@ -16,7 +16,7 @@ import { Mnemonic } from '../../shared/classes/mnemonic'; ...@@ -16,7 +16,7 @@ import { Mnemonic } from '../../shared/classes/mnemonic';
styleUrls: ['./create.component.css'], styleUrls: ['./create.component.css'],
}) })
export class CreateComponent { export class CreateComponent implements OnInit {
constructor(private globalService: GlobalService, private apiService: ApiService, private router: Router, private fb: FormBuilder) { constructor(private globalService: GlobalService, private apiService: ApiService, private router: Router, private fb: FormBuilder) {
this.buildCreateForm(); this.buildCreateForm();
} }
...@@ -25,6 +25,10 @@ export class CreateComponent { ...@@ -25,6 +25,10 @@ export class CreateComponent {
private newWallet: WalletCreation; private newWallet: WalletCreation;
private mnemonic: string; private mnemonic: string;
ngOnInit() {
this.getNewMnemonic();
}
private buildCreateForm(): void { private buildCreateForm(): void {
this.createWalletForm = this.fb.group({ this.createWalletForm = this.fb.group({
"walletName": ["", "walletName": ["",
...@@ -95,22 +99,76 @@ export class CreateComponent { ...@@ -95,22 +99,76 @@ export class CreateComponent {
} }
private onCreateClicked() { private onCreateClicked() {
this.newWallet = new WalletCreation( if (this.mnemonic) {
this.createWalletForm.get("walletPassword").value, this.newWallet = new WalletCreation(
this.createWalletForm.get("selectNetwork").value, this.createWalletForm.get("walletName").value,
this.globalService.getWalletPath(), this.mnemonic,
this.createWalletForm.get("walletName").value this.createWalletForm.get("walletPassword").value,
this.createWalletForm.get("selectNetwork").value
); );
this.createWallet(this.newWallet); this.createWallets(this.newWallet);
}
} }
private createWallet(wallet: WalletCreation) { private getNewMnemonic() {
this.apiService this.apiService
.createWallet(wallet) .getNewMnemonic()
.subscribe( .subscribe(
response => { response => {
if (response.status >= 200 && response.status < 400){ if (response.status >= 200 && response.status < 400){
this.mnemonic = response.json(); this.mnemonic = response.json();
}
},
error => {
console.log(error);
if (error.status === 0) {
alert("Something went wrong while connecting to the API. Please restart the application.");
} else if (error.status >= 400) {
if (!error.json().errors[0]) {
console.log(error);
}
else {
alert(error.json().errors[0].message);
}
}
}
)
;
}
private createWallets(wallet: WalletCreation) {
this.apiService
.createBitcoinWallet(wallet)
.subscribe(
response => {
if (response.status >= 200 && response.status < 400){
// Bitcoin wallet created
}
},
error => {
console.log(error);
if (error.status === 0) {
alert("Something went wrong while connecting to the API. Please restart the application.");
} else if (error.status >= 400) {
if (!error.json().errors[0]) {
console.log(error);
}
else {
alert(error.json().errors[0].message);
}
}
},
() => this.createStratisWallet(wallet)
)
;
}
private createStratisWallet(wallet: WalletCreation) {
this.apiService
.createStratisWallet(wallet)
.subscribe(
response => {
if (response.status >= 200 && response.status < 400){
alert("Your wallet has been created.\n\nPlease write down your 12 word passphrase: \n" + this.mnemonic + "\n\nYou can recover your wallet on any computer with:\n- your passphrase AND\n- your password AND\n- the wallet creation time\n\nUnlike most other wallets if an attacker acquires your passphrase, it will not be able to hack your wallet without knowing your password. On the contrary, unlike other wallets, you will not be able to recover your wallet only with your passphrase if you lose your password."); alert("Your wallet has been created.\n\nPlease write down your 12 word passphrase: \n" + this.mnemonic + "\n\nYou can recover your wallet on any computer with:\n- your passphrase AND\n- your password AND\n- the wallet creation time\n\nUnlike most other wallets if an attacker acquires your passphrase, it will not be able to hack your wallet without knowing your password. On the contrary, unlike other wallets, you will not be able to recover your wallet only with your passphrase if you lose your password.");
this.router.navigate(['']); this.router.navigate(['']);
} }
......
export class WalletCreation { export class WalletCreation {
constructor(password: string, network:string, folderPath: string, name: string) { constructor(name: string, mnemonic: string, password: string, network:string, folderPath: string = null ) {
this.name = name;
this.mnemonic = mnemonic;
this.password = password; this.password = password;
this.network = network; this.network = network;
this.folderPath = folderPath; this.folderPath = folderPath;
this.name = name;
} }
name: string;
mnemonic: string;
password: string; password: string;
network: string; network: string;
folderPath: string; folderPath?: string;
name: string;
} }
...@@ -43,19 +43,39 @@ export class ApiService { ...@@ -43,19 +43,39 @@ export class ApiService {
* Gets available wallets at the default path * Gets available wallets at the default path
*/ */
getWalletFiles(): Observable<any> { getWalletFiles(): Observable<any> {
return this.http return this.http
.get(this.bitcoinApiUrl + '/wallet/files') .get(this.bitcoinApiUrl + '/wallet/files')
.map((response: Response) => response); .map((response: Response) => response);
} }
/**
* Get a new mnemonic
*/
getNewMnemonic(): Observable<any> {
let params: URLSearchParams = new URLSearchParams();
params.set('language', 'English');
params.set('wordCount', '12');
return this.http
.get(this.bitcoinApiUrl + '/wallet/mnemonic', new RequestOptions({headers: this.headers, search: params}))
.map((response: Response) => response);
}
/** /**
* Create a new wallet. * Create a new Bitcoin wallet.
*/ */
createWallet(data: WalletCreation): Observable<any> { createBitcoinWallet(data: WalletCreation): Observable<any> {
this.getCurrentCoin(); return this.http
.post(this.bitcoinApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers})
.map((response: Response) => response);
}
/**
* Create a new Stratis wallet.
*/
createStratisWallet(data: WalletCreation): Observable<any> {
return this.http return this.http
.post(this.currentApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers}) .post(this.stratisApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers})
.map((response: Response) => response); .map((response: Response) => response);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment