separate wallet generation & confirmation

all methods of recovery & creation implemented
This commit is contained in:
m2049r
2017-08-16 21:28:00 +02:00
parent 952fb3a7f1
commit bd598deddd
12 changed files with 747 additions and 182 deletions
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2017 m2049r
*
* 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.
*/
package com.m2049r.xmrwallet;
import android.app.Fragment;
@@ -5,6 +21,7 @@ import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@@ -24,15 +41,17 @@ import java.io.File;
// TODO: somehow show which net we are generating for
public class GenerateFragment extends Fragment {
static final String TAG = "GenerateFragment";
EditText etWalletName;
EditText etWalletPassword;
EditText etWalletAddress;
EditText etWalletMnemonic;
LinearLayout llRestoreKeys;
EditText etWalletViewKey;
EditText etWalletSpendKey;
EditText etWalletRestoreHeight;
Button bGenerate;
LinearLayout llAccept;
TextView tvWalletAddress;
TextView tvWalletMnemonic;
Button bAccept;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -42,22 +61,27 @@ public class GenerateFragment extends Fragment {
etWalletName = (EditText) view.findViewById(R.id.etWalletName);
etWalletPassword = (EditText) view.findViewById(R.id.etWalletPassword);
etWalletMnemonic = (EditText) view.findViewById(R.id.etWalletMnemonic);
etWalletAddress = (EditText) view.findViewById(R.id.etWalletAddress);
llRestoreKeys = (LinearLayout) view.findViewById(R.id.llRestoreKeys);
etWalletViewKey = (EditText) view.findViewById(R.id.etWalletViewKey);
etWalletSpendKey = (EditText) view.findViewById(R.id.etWalletSpendKey);
etWalletRestoreHeight = (EditText) view.findViewById(R.id.etWalletRestoreHeight);
bGenerate = (Button) view.findViewById(R.id.bGenerate);
llAccept = (LinearLayout) view.findViewById(R.id.llAccept);
tvWalletAddress = (TextView) view.findViewById(R.id.tvWalletAddress);
tvWalletMnemonic = (TextView) view.findViewById(R.id.tvWalletMnemonic);
bAccept = (Button) view.findViewById(R.id.bAccept);
boolean testnet = WalletManager.getInstance().isTestNet();
tvWalletMnemonic.setTextIsSelectable(testnet);
etWalletMnemonic.setTextIsSelectable(testnet);
etWalletName.requestFocus();
Helper.showKeyboard(getActivity());
setGenerateEnabled();
etWalletName.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
showMnemonic("");
if (etWalletName.length() > 0) {
bGenerate.setEnabled(true);
} else {
bGenerate.setEnabled(false);
}
}
@Override
@@ -76,11 +100,11 @@ public class GenerateFragment extends Fragment {
});
etWalletName.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
if (etWalletName.length() > 0) {
etWalletPassword.requestFocus();
} // otherwise ignore
return false;
return true;
}
return false;
}
@@ -92,10 +116,52 @@ public class GenerateFragment extends Fragment {
Helper.showKeyboard(getActivity());
}
});
etWalletPassword.addTextChangedListener(new TextWatcher() {
etWalletPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
etWalletMnemonic.requestFocus();
return true;
}
return false;
}
});
etWalletMnemonic.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
if (etWalletMnemonic.length() == 0) {
etWalletAddress.requestFocus();
} else if (mnemonicOk()) {
etWalletRestoreHeight.requestFocus();
} else {
Toast.makeText(getActivity(), getString(R.string.generate_check_mnemonic), Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
});
etWalletMnemonic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.showKeyboard(getActivity());
}
});
etWalletMnemonic.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
showMnemonic("");
if (etWalletMnemonic.length() > 0) {
etWalletRestoreHeight.setVisibility(View.VISIBLE);
etWalletAddress.setVisibility(View.INVISIBLE);
} else {
etWalletAddress.setVisibility(View.VISIBLE);
if (etWalletAddress.length() == 0) {
etWalletRestoreHeight.setVisibility(View.INVISIBLE);
} else {
etWalletRestoreHeight.setVisibility(View.VISIBLE);
}
}
}
@Override
@@ -106,12 +172,103 @@ public class GenerateFragment extends Fragment {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
etWalletPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
etWalletAddress.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
if (etWalletAddress.length() == 0) {
Helper.hideKeyboard(getActivity());
generateWallet();
} else if (addressOk()) {
etWalletViewKey.requestFocus();
} else {
Toast.makeText(getActivity(), getString(R.string.generate_check_address), Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
});
etWalletAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.showKeyboard(getActivity());
}
});
etWalletAddress.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
if (etWalletAddress.length() > 0) {
llRestoreKeys.setVisibility(View.VISIBLE);
etWalletMnemonic.setVisibility(View.INVISIBLE);
etWalletRestoreHeight.setVisibility(View.VISIBLE);
} else {
llRestoreKeys.setVisibility(View.INVISIBLE);
etWalletMnemonic.setVisibility(View.VISIBLE);
if (etWalletMnemonic.length() == 0) {
etWalletRestoreHeight.setVisibility(View.INVISIBLE);
} else {
etWalletRestoreHeight.setVisibility(View.VISIBLE);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
etWalletViewKey.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
if (viewKeyOk()) {
etWalletSpendKey.requestFocus();
} else {
Toast.makeText(getActivity(), getString(R.string.generate_check_key), Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
});
etWalletViewKey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.showKeyboard(getActivity());
}
});
etWalletSpendKey.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
if (spendKeyOk()) {
Helper.hideKeyboard(getActivity());
generateWallet();
} else {
Toast.makeText(getActivity(), getString(R.string.generate_check_key), Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
});
etWalletSpendKey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.showKeyboard(getActivity());
}
});
etWalletRestoreHeight.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_NEXT)) {
Helper.hideKeyboard(getActivity());
generateWallet();
return false;
return true;
}
return false;
}
@@ -125,19 +282,29 @@ public class GenerateFragment extends Fragment {
}
});
bAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
acceptWallet();
}
});
bAccept.setEnabled(false);
llAccept.setVisibility(View.GONE);
return view;
}
private boolean mnemonicOk() {
String seed = etWalletMnemonic.getText().toString();
return (seed.split("\\s").length == 25); // 25 words
}
private boolean addressOk() {
String address = etWalletAddress.getText().toString();
return ((address.length() == 95) && ("49A".indexOf(address.charAt(0)) >= 0));
}
private boolean viewKeyOk() {
String viewKey = etWalletViewKey.getText().toString();
return (viewKey.length() == 64) && (viewKey.matches("^[0-9a-fA-F]+$"));
}
private boolean spendKeyOk() {
String spendKey = etWalletSpendKey.getText().toString();
return ((spendKey.length() == 0) || ((spendKey.length() == 64) && (spendKey.matches("^[0-9a-fA-F]+$"))));
}
private void generateWallet() {
String name = etWalletName.getText().toString();
if (name.length() == 0) return;
@@ -148,43 +315,56 @@ public class GenerateFragment extends Fragment {
return;
}
String password = etWalletPassword.getText().toString();
bGenerate.setEnabled(false);
activityCallback.onGenerate(name, password);
}
private void acceptWallet() {
String name = etWalletName.getText().toString();
String password = etWalletPassword.getText().toString();
bAccept.setEnabled(false);
activityCallback.onAccept(name, password);
}
String seed = etWalletMnemonic.getText().toString();
String address = etWalletAddress.getText().toString();
private void setGenerateEnabled() {
bGenerate.setEnabled(etWalletName.length() > 0);
}
public void showMnemonic(String mnemonic) {
setGenerateEnabled();
if (mnemonic.length() > 0) {
tvWalletMnemonic.setText(mnemonic);
bAccept.setEnabled(true);
llAccept.setVisibility(View.VISIBLE);
} else {
if (llAccept.getVisibility() != View.GONE) {
tvWalletMnemonic.setText(getActivity().getString(R.string.generate_seed));
bAccept.setEnabled(false);
llAccept.setVisibility(View.GONE);
}
long height;
try {
height = Long.parseLong(etWalletRestoreHeight.getText().toString());
} catch (NumberFormatException ex) {
Log.e(TAG, "Cannot parse " + etWalletRestoreHeight.getText().toString());
Log.e(TAG, ex.getLocalizedMessage());
height = 0; // Keep calm and carry on!
}
// figure out how we want to create this wallet
// A. from scratch
if ((seed.length() == 0) && (address.length() == 0)) {
bGenerate.setVisibility(View.INVISIBLE);
activityCallback.onGenerate(name, password);
} else
// B. from seed
if (mnemonicOk()) {
bGenerate.setVisibility(View.INVISIBLE);
activityCallback.onGenerate(name, password, seed, height);
} else
// C. from keys
if (addressOk() && viewKeyOk() && (spendKeyOk())) {
String viewKey = etWalletViewKey.getText().toString();
String spendKey = etWalletSpendKey.getText().toString();
bGenerate.setVisibility(View.INVISIBLE);
activityCallback.onGenerate(name, password, address, viewKey, spendKey, height);
} else
// D. none of the above :)
{
Toast.makeText(getActivity(), getString(R.string.generate_check_something), Toast.LENGTH_LONG).show();
}
}
public void walletGenerateError() {
bGenerate.setEnabled(etWalletName.length() > 0);
bGenerate.setVisibility(View.VISIBLE);
}
GenerateFragment.Listener activityCallback;
// Container Activity must implement this interface
public interface Listener {
void onGenerate(String name, String password);
void onAccept(String name, String password);
void onGenerate(String name, String password, String seed, long height);
void onGenerate(String name, String password, String address, String viewKey, String spendKey, long height);
File getStorageRoot();
}