Adds a JNI functionality to Create, Lock and Unlock a wallet
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.chromium.chrome.browser.crypto_wallet;
|
||||
|
||||
import org.chromium.base.annotations.CalledByNative;
|
||||
import org.chromium.base.annotations.JNINamespace;
|
||||
import org.chromium.base.annotations.NativeMethods;
|
||||
|
||||
@JNINamespace("chrome::android")
|
||||
public class BraveWalletNativeWorker {
|
||||
private long mNativeBraveWalletNativeWorker;
|
||||
private static final Object lock = new Object();
|
||||
private static BraveWalletNativeWorker instance;
|
||||
|
||||
public static BraveWalletNativeWorker getInstance() {
|
||||
synchronized (lock) {
|
||||
if (instance == null) {
|
||||
instance = new BraveWalletNativeWorker();
|
||||
instance.Init();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private BraveWalletNativeWorker() {
|
||||
}
|
||||
|
||||
private void Init() {
|
||||
if (mNativeBraveWalletNativeWorker == 0) {
|
||||
BraveWalletNativeWorkerJni.get().init(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() {
|
||||
Destroy();
|
||||
}
|
||||
|
||||
private void Destroy() {
|
||||
if (mNativeBraveWalletNativeWorker != 0) {
|
||||
BraveWalletNativeWorkerJni.get().destroy(
|
||||
mNativeBraveWalletNativeWorker, this);
|
||||
mNativeBraveWalletNativeWorker = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@CalledByNative
|
||||
private void setNativePtr(long nativePtr) {
|
||||
assert mNativeBraveWalletNativeWorker == 0;
|
||||
mNativeBraveWalletNativeWorker = nativePtr;
|
||||
}
|
||||
|
||||
public String CreateWallet(String password) {
|
||||
return BraveWalletNativeWorkerJni.get().createWallet(
|
||||
mNativeBraveWalletNativeWorker, password);
|
||||
}
|
||||
|
||||
public void LockWallet() {
|
||||
BraveWalletNativeWorkerJni.get().lockWallet(
|
||||
mNativeBraveWalletNativeWorker);
|
||||
}
|
||||
|
||||
public void UnlockWallet(String password) {
|
||||
BraveWalletNativeWorkerJni.get().unlockWallet(
|
||||
mNativeBraveWalletNativeWorker, password);
|
||||
}
|
||||
|
||||
@NativeMethods
|
||||
interface Natives {
|
||||
void init(BraveWalletNativeWorker caller);
|
||||
void destroy(long nativeBraveWalletNativeWorker,
|
||||
BraveWalletNativeWorker caller);
|
||||
String createWallet(long nativeBraveWalletNativeWorker, String password);
|
||||
void lockWallet(long nativeBraveWalletNativeWorker);
|
||||
boolean unlockWallet(long nativeBraveWalletNativeWorker, String password);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import androidx.viewpager.widget.ViewPager;
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.crypto_wallet.BraveWalletNativeWorker;
|
||||
import org.chromium.chrome.browser.crypto_wallet.adapters.WalletNavigationFragmentPageAdapter;
|
||||
import org.chromium.chrome.browser.crypto_wallet.fragments.CardsFragment;
|
||||
import org.chromium.chrome.browser.crypto_wallet.fragments.CryptoFragment;
|
||||
@@ -102,6 +103,7 @@ public class CryptoWalletActivity extends AppCompatActivity {
|
||||
} else if (itemId == R.id.navigation_lock) {
|
||||
viewPager.setCurrentItem(4);
|
||||
initTitle(4);
|
||||
BraveWalletNativeWorker.getInstance().LockWallet();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
+6
-2
@@ -34,6 +34,7 @@ import com.google.android.material.tabs.TabLayout;
|
||||
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.crypto_wallet.AddAccountOnboardingDialog;
|
||||
import org.chromium.chrome.browser.crypto_wallet.BraveWalletNativeWorker;
|
||||
import org.chromium.chrome.browser.crypto_wallet.CryptoWalletActivity;
|
||||
import org.chromium.chrome.browser.crypto_wallet.adapters.RecoveryPhraseAdapter;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.ItemOffsetDecoration;
|
||||
@@ -150,6 +151,9 @@ public class CryptoFragment extends Fragment {
|
||||
});
|
||||
|
||||
recoveryPhraseButton.setOnClickListener(v -> {
|
||||
// TODO(sergz): do something with a mnemonic string that CreateWallet returns
|
||||
BraveWalletNativeWorker.getInstance().CreateWallet(
|
||||
passwordEdittext.getText().toString());
|
||||
onFinishOnboarding.onFinish();
|
||||
showAddAccountDialog();
|
||||
});
|
||||
@@ -221,8 +225,8 @@ public class CryptoFragment extends Fragment {
|
||||
break;
|
||||
case BiometricPrompt.BIOMETRIC_ERROR_NO_BIOMETRICS:
|
||||
Toast.makeText(getActivity(), "User did not register any fingerprints.",
|
||||
Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
break;
|
||||
default:
|
||||
Toast.makeText(getActivity(), "unrecoverable error", Toast.LENGTH_SHORT).show();
|
||||
|
||||
Reference in New Issue
Block a user