Skip to content

Commit

Permalink
Add seed recovery to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
vsima committed Mar 2, 2023
1 parent eba4971 commit 6addd4a
Show file tree
Hide file tree
Showing 6 changed files with 542 additions and 580 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.breadwallet.presenter.activities.util.BRActivity;
import com.breadwallet.presenter.entities.BRSettingsItem;
import com.breadwallet.presenter.interfaces.BRAuthCompletion;
import com.breadwallet.tools.animation.BRAnimator;
import com.breadwallet.tools.manager.BRSharedPrefs;
import com.breadwallet.tools.security.AuthManager;
import com.platform.APIClient;
Expand Down Expand Up @@ -126,6 +127,11 @@ private void populateItems() {

}, false));

// recover seed phrase
items.add(new BRSettingsItem(getString(R.string.Settings_recover_seed), "", v -> {
BRAnimator.showBalanceSeedFragment(this);
}, false));

/*Wipe Start_Recover Wallet*/
items.add(new BRSettingsItem(getString(R.string.Settings_wipe), "", v -> {
Intent intent = new Intent(SettingsActivity.this, WipeActivity.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.breadwallet.presenter.fragments

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -10,39 +12,28 @@ import android.widget.*
import androidx.fragment.app.Fragment
import com.breadwallet.R
import com.breadwallet.tools.animation.BRAnimator
import com.breadwallet.tools.security.BRKeyStore
import com.breadwallet.tools.util.BRConstants
import com.breadwallet.tools.util.Bip39Reader
import com.breadwallet.wallet.BRWalletManager
import java.util.*
import kotlin.math.log


class FragmentBalanceSeedReminder : Fragment() {
class FragmentRecoverSeedPhrase : Fragment() {
private lateinit var backgroundLayout: ScrollView
private lateinit var signalLayout: LinearLayout
private lateinit var showSeedButton: Button
private lateinit var currentBalanceTextView: TextView
private lateinit var seedPhraseTextView: TextView
private lateinit var closeButton: View


override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val rootView = inflater.inflate(R.layout.fragment_balance_seed_reminder, container, false)
val rootView = inflater.inflate(R.layout.fragment_recover_seed_phrase, container, false)
backgroundLayout = rootView.findViewById(R.id.background_layout)
signalLayout = rootView.findViewById(R.id.signal_layout)
signalLayout = rootView.findViewById<View>(R.id.signal_layout) as LinearLayout
currentBalanceTextView = rootView.findViewById(R.id.current_balance)
seedPhraseTextView = rootView.findViewById(R.id.seed_phrase)
closeButton = rootView.findViewById(R.id.close_button)
showSeedButton = rootView.findViewById(R.id.show_seed_button)
// signalLayout.layoutTransition = BRAnimator.getDefaultTransition()

return rootView
}

Expand All @@ -66,28 +57,32 @@ class FragmentBalanceSeedReminder : Fragment() {
observer.removeOnGlobalLayoutListener(this)
}
BRAnimator.animateBackgroundDim(backgroundLayout, false)
BRAnimator.animateSignalSlide(signalLayout, false) {
// val bundle = arguments
// if (bundle?.getString("url") != null) setUrl(bundle.getString("url"))
}
BRAnimator.animateSignalSlide(signalLayout, false) {}
}
})
setListeners()
fetchWalletInfo()
}

fun fetchWalletInfo() {
val walletManager = BRWalletManager.getInstance()
val balance = walletManager.getBalance(requireContext())
currentBalanceTextView.text = "$balance"
seedPhraseTextView.text = walletManager.getSeedPhrase(requireContext())
seedPhraseTextView.setOnClickListener {
if (it.visibility != View.VISIBLE) return@setOnClickListener
val clipboardManager =
requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText("seed phrase", seedPhraseTextView.text)
clipboardManager.setPrimaryClip(clipData)
}
}

private fun animateClose() {
BRAnimator.animateBackgroundDim(backgroundLayout, true)
BRAnimator.animateSignalSlide(signalLayout, true) { close() }
}

private fun close() {
if(activity != null && activity?.isFinishing != true) {
if (activity != null && activity?.isFinishing != true) {
activity?.onBackPressed()
}
}
Expand Down
34 changes: 10 additions & 24 deletions app/src/main/java/com/breadwallet/tools/animation/BRAnimator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -29,12 +28,11 @@
import com.breadwallet.R;
import com.breadwallet.presenter.activities.BreadActivity;
import com.breadwallet.presenter.activities.LoginActivity;
import com.breadwallet.presenter.activities.camera.CameraActivity;
import com.breadwallet.presenter.activities.camera.ScanQRActivity;
import com.breadwallet.presenter.customviews.BRDialogView;
import com.breadwallet.presenter.entities.TxItem;
import com.breadwallet.presenter.fragments.DynamicDonationFragment;
import com.breadwallet.presenter.fragments.FragmentBalanceSeedReminder;
import com.breadwallet.presenter.fragments.FragmentRecoverSeedPhrase;
import com.breadwallet.presenter.fragments.FragmentBuy;
import com.breadwallet.presenter.fragments.FragmentGreetings;
import com.breadwallet.presenter.fragments.FragmentMenu;
Expand Down Expand Up @@ -79,33 +77,21 @@ public static void showBalanceSeedFragment(@NonNull FragmentActivity app) {
Timber.d("timber: fetched info");

androidx.fragment.app.FragmentManager fragmentManager = app.getSupportFragmentManager();
FragmentBalanceSeedReminder fragmentBalanceSeedReminder = (FragmentBalanceSeedReminder) fragmentManager.findFragmentByTag(FragmentBalanceSeedReminder.class.getName());
if (fragmentBalanceSeedReminder != null) {
fragmentBalanceSeedReminder.fetchWalletInfo();
FragmentRecoverSeedPhrase fragmentRecoverSeedPhrase = (FragmentRecoverSeedPhrase) fragmentManager.findFragmentByTag(FragmentRecoverSeedPhrase.class.getName());
if (fragmentRecoverSeedPhrase != null) {
fragmentRecoverSeedPhrase.fetchWalletInfo();
Timber.d("timber: fetched info");

return;
}

try {
fragmentBalanceSeedReminder = new FragmentBalanceSeedReminder();
fragmentManager.beginTransaction()
.setCustomAnimations(0, 0, 0, R.animator.plain_300)
.add(android.R.id.content, fragmentBalanceSeedReminder, FragmentBalanceSeedReminder.class.getName())
.addToBackStack(FragmentBalanceSeedReminder.class.getName()).commit();
} finally {
}
fragmentRecoverSeedPhrase = new FragmentRecoverSeedPhrase();
fragmentManager.beginTransaction()
.setCustomAnimations(0, 0, 0, R.animator.plain_300)
.add(android.R.id.content, fragmentRecoverSeedPhrase, FragmentRecoverSeedPhrase.class.getName())
.addToBackStack(FragmentRecoverSeedPhrase.class.getName()).commit();
}

// app.getSupportFragmentManager().beginTransaction()
// .setCustomAnimations(0, 0, 0, R.animator.plain_300)
// .add(android.R.id.content, new FragmentBalanceSeedReminder(), FragmentBalanceSeedReminder.class.getName())
// .addToBackStack(FragmentBalanceSeedReminder.class.getName())
// .commit();




}
public static void showSendFragment(FragmentActivity app, final String bitcoinUrl) {
if (app == null) {
Timber.i("timber: showSendFragment: app is null");
Expand Down
118 changes: 53 additions & 65 deletions app/src/main/java/com/breadwallet/wallet/BRWalletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.widget.Toast;

import androidx.annotation.WorkerThread;
import androidx.fragment.app.FragmentActivity;

import com.breadwallet.BreadApp;
import com.breadwallet.R;
Expand Down Expand Up @@ -55,8 +54,6 @@
import com.breadwallet.tools.util.Utils;
import com.platform.entities.WalletInfo;

import org.jetbrains.annotations.Nullable;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
Expand Down Expand Up @@ -494,77 +491,68 @@ public void initWallet(final Context ctx) {
BRWalletManager m = BRWalletManager.getInstance();
final BRPeerManager pm = BRPeerManager.getInstance();

// int currentBalance = (int) m.getBalance();
// Timber.i("timber: wallet manager balance %d", currentBalance);

BRAnimator.showBalanceSeedFragment((FragmentActivity) ctx);
Timber.d("timber: Showing seed fragment");
// if (m.isCreated() && pm.isCreated()) {
// BRAnimator.showBalanceSeedFragment((FragmentActivity) ctx);
// Timber.i("timber: Showing seed fragment");
// }
if (!m.isCreated()) {
List<BRTransactionEntity> transactions = TransactionDataSource.getInstance(ctx).getAllTransactions();
int transactionsCount = transactions.size();
if (transactionsCount > 0) {
m.createTxArrayWithCount(transactionsCount);
for (BRTransactionEntity entity : transactions) {
m.putTransaction(entity.getBuff(), entity.getBlockheight(), entity.getTimestamp());
}
if (!m.isCreated()) {
List<BRTransactionEntity> transactions = TransactionDataSource.getInstance(ctx).getAllTransactions();
int transactionsCount = transactions.size();
if (transactionsCount > 0) {
m.createTxArrayWithCount(transactionsCount);
for (BRTransactionEntity entity : transactions) {
m.putTransaction(entity.getBuff(), entity.getBlockheight(), entity.getTimestamp());
}
}

byte[] pubkeyEncoded = BRKeyStore.getMasterPublicKey(ctx);
if (Utils.isNullOrEmpty(pubkeyEncoded)) {
Timber.i("timber: initWallet: pubkey is missing");
return;
}
//Save the first address for future check
m.createWallet(transactionsCount, pubkeyEncoded);
String firstAddress = BRWalletManager.getFirstAddress(pubkeyEncoded);
BRSharedPrefs.putFirstAddress(ctx, firstAddress);
FeeManager feeManager = FeeManager.getInstance();
if (feeManager.isRegularFee()) {
Fee fees = feeManager.getFees();
BRWalletManager.getInstance().setFeePerKb(fees.regular);
}
byte[] pubkeyEncoded = BRKeyStore.getMasterPublicKey(ctx);
if (Utils.isNullOrEmpty(pubkeyEncoded)) {
Timber.i("timber: initWallet: pubkey is missing");
return;
}
//Save the first address for future check
m.createWallet(transactionsCount, pubkeyEncoded);
String firstAddress = BRWalletManager.getFirstAddress(pubkeyEncoded);
BRSharedPrefs.putFirstAddress(ctx, firstAddress);
FeeManager feeManager = FeeManager.getInstance();
if (feeManager.isRegularFee()) {
Fee fees = feeManager.getFees();
BRWalletManager.getInstance().setFeePerKb(fees.regular);
}
}

if (!pm.isCreated()) {
List<BRMerkleBlockEntity> blocks = MerkleBlockDataSource.getInstance(ctx).getAllMerkleBlocks();
List<BRPeerEntity> peers = PeerDataSource.getInstance(ctx).getAllPeers();
final int blocksCount = blocks.size();
final int peersCount = peers.size();
if (blocksCount > 0) {
pm.createBlockArrayWithCount(blocksCount);
for (BRMerkleBlockEntity entity : blocks) {
pm.putBlock(entity.getBuff(), entity.getBlockHeight());
}
if (!pm.isCreated()) {
List<BRMerkleBlockEntity> blocks = MerkleBlockDataSource.getInstance(ctx).getAllMerkleBlocks();
List<BRPeerEntity> peers = PeerDataSource.getInstance(ctx).getAllPeers();
final int blocksCount = blocks.size();
final int peersCount = peers.size();
if (blocksCount > 0) {
pm.createBlockArrayWithCount(blocksCount);
for (BRMerkleBlockEntity entity : blocks) {
pm.putBlock(entity.getBuff(), entity.getBlockHeight());
}
if (peersCount > 0) {
pm.createPeerArrayWithCount(peersCount);
for (BRPeerEntity entity : peers) {
pm.putPeer(entity.getAddress(), entity.getPort(), entity.getTimeStamp());
}
}
if (peersCount > 0) {
pm.createPeerArrayWithCount(peersCount);
for (BRPeerEntity entity : peers) {
pm.putPeer(entity.getAddress(), entity.getPort(), entity.getTimeStamp());
}
Timber.d("timber: blocksCount before connecting: %s", blocksCount);
Timber.d("timber: peersCount before connecting: %s", peersCount);
}
Timber.d("timber: blocksCount before connecting: %s", blocksCount);
Timber.d("timber: peersCount before connecting: %s", peersCount);

int walletTime = BRKeyStore.getWalletCreationTime(ctx);
int walletTime = BRKeyStore.getWalletCreationTime(ctx);

Timber.d("timber: initWallet: walletTime: %s", walletTime);
pm.create(walletTime, blocksCount, peersCount);
BRPeerManager.getInstance().updateFixedPeer(ctx);
}
Timber.d("timber: initWallet: walletTime: %s", walletTime);
pm.create(walletTime, blocksCount, peersCount);
BRPeerManager.getInstance().updateFixedPeer(ctx);
}

pm.connect();
if (BRSharedPrefs.getStartHeight(ctx) == 0) {
BRExecutor.getInstance().forLightWeightBackgroundTasks().execute(new Runnable() {
@Override
public void run() {
BRSharedPrefs.putStartHeight(ctx, BRPeerManager.getCurrentBlockHeight());
}
});
}
pm.connect();
if (BRSharedPrefs.getStartHeight(ctx) == 0) {
BRExecutor.getInstance().forLightWeightBackgroundTasks().execute(new Runnable() {
@Override
public void run() {
BRSharedPrefs.putStartHeight(ctx, BRPeerManager.getCurrentBlockHeight());
}
});
}


} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,17 @@
android:layout_width="match_parent"
android:layout_height="240dp"
android:orientation="vertical">

<TextView
android:id="@+id/current_balance"
style="@style/BRBoldTextStyle"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginStart="@dimen/bread_margin"
android:layout_marginEnd="@dimen/bread_margin"
android:layout_marginTop="10dp"
android:fontFamily="@font/barlowsemicondensed_light"
android:text="@string/current_balance.label"
android:textColor="@color/almost_black"
android:textSize="@dimen/header"
/>
<TextView
android:id="@+id/seed_phrase"
style="@style/BRBoldTextStyle"
style="@style/BRTextStyle"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/bread_margin"
android:layout_marginEnd="@dimen/bread_margin"
android:layout_marginBottom="@dimen/bread_margin"
android:layout_weight="1"
android:visibility="invisible"
android:text="@string/seed_phrase.label"
android:textIsSelectable="true"/>

</LinearLayout>
Expand Down
Loading

0 comments on commit 6addd4a

Please sign in to comment.