1+ package com .omni .wallet .view .dialog ;
2+
3+ import android .content .Context ;
4+ import android .os .Handler ;
5+ import android .os .Looper ;
6+ import android .text .method .HideReturnsTransformationMethod ;
7+ import android .text .method .PasswordTransformationMethod ;
8+ import android .view .View ;
9+ import android .widget .EditText ;
10+ import android .widget .ImageView ;
11+ import android .widget .TextView ;
12+
13+ import com .google .protobuf .InvalidProtocolBufferException ;
14+ import com .omni .wallet .R ;
15+ import com .omni .wallet .baselibrary .dialog .AlertDialog ;
16+ import com .omni .wallet .baselibrary .utils .LogUtils ;
17+ import com .omni .wallet .baselibrary .utils .StringUtils ;
18+ import com .omni .wallet .baselibrary .utils .ToastUtils ;
19+ import com .omni .wallet .framelibrary .entity .User ;
20+ import com .omni .wallet .utils .CopyUtil ;
21+ import com .omni .wallet .utils .SecretAESOperator ;
22+
23+ import lnrpc .LightningOuterClass ;
24+ import obdmobile .Callback ;
25+ import obdmobile .Obdmobile ;
26+
27+ /**
28+ * 汉: 导出WIF的弹窗
29+ * En: ExportWifDialog
30+ * author: guoyalei
31+ * date: 2023/6/13
32+ */
33+ public class ExportWifDialog {
34+ private static final String TAG = ExportWifDialog .class .getSimpleName ();
35+ private Context mContext ;
36+ private AlertDialog mAlertDialog ;
37+ private boolean mCanClick = false ;
38+ String wifStr ;
39+
40+ public ExportWifDialog (Context context ) {
41+ this .mContext = context ;
42+ }
43+
44+ public void show () {
45+ if (mAlertDialog == null ) {
46+ mAlertDialog = new AlertDialog .Builder (mContext , R .style .dialog_translucent_theme )
47+ .setContentView (R .layout .layout_dialog_export_wif )
48+ .setAnimation (R .style .popup_anim_style )
49+ .fullWidth ()
50+ .fullHeight ()
51+ .create ();
52+ }
53+ EditText mPwdEdit = mAlertDialog .findViewById (R .id .password_input );
54+ mPwdEdit .setTransformationMethod (PasswordTransformationMethod .getInstance ());
55+ mAlertDialog .findViewById (R .id .btn_unlock ).setOnClickListener (v -> unlockWallet ());
56+ mAlertDialog .findViewById (R .id .pass_switch ).setOnClickListener (v -> {
57+ ImageView mPwdEyeIv = mAlertDialog .findViewById (R .id .pass_switch );
58+ if (mCanClick ) {
59+ mCanClick = false ;
60+ mPwdEyeIv .setImageResource (R .mipmap .icon_eye_open );
61+ //显示密码(show password)
62+ mPwdEdit .setTransformationMethod (HideReturnsTransformationMethod .getInstance ());
63+ } else {
64+ mCanClick = true ;
65+ mPwdEyeIv .setImageResource (R .mipmap .icon_eye_close );
66+ //隐藏密码(hide password)
67+ mPwdEdit .setTransformationMethod (PasswordTransformationMethod .getInstance ());
68+ }
69+ });
70+ /**
71+ * @描述: 点击 close
72+ * @desc: click close button
73+ */
74+ mAlertDialog .findViewById (R .id .layout_close ).setOnClickListener (new View .OnClickListener () {
75+ @ Override
76+ public void onClick (View v ) {
77+ mAlertDialog .dismiss ();
78+ }
79+ });
80+ if (mAlertDialog .isShowing ()) {
81+ mAlertDialog .dismiss ();
82+ }
83+ mAlertDialog .show ();
84+ }
85+
86+ private void unlockWallet () {
87+ EditText mPwdEdit = mAlertDialog .findViewById (R .id .password_input );
88+ String passwordString = mPwdEdit .getText ().toString ();
89+ String newSecretString = SecretAESOperator .getInstance ().encrypt (passwordString );
90+ boolean passIsMatched = checkedPassMatched (newSecretString );
91+ if (passIsMatched ) {
92+ showWif ();
93+ } else {
94+ String toastString = mContext .getResources ().getString (R .string .toast_unlock_error );
95+ ToastUtils .showToast (mContext , toastString );
96+ }
97+ }
98+
99+ private void showWif () {
100+ mAlertDialog .findViewById (R .id .layout_unlock ).setVisibility (View .GONE );
101+ mAlertDialog .findViewById (R .id .layout_wif ).setVisibility (View .VISIBLE );
102+ TextView wifTv = mAlertDialog .findViewById (R .id .tv_wif );
103+ LightningOuterClass .DumpPrivkeyRequest dumpPrivkeyRequest = LightningOuterClass .DumpPrivkeyRequest .newBuilder ()
104+ .setAddress (User .getInstance ().getWalletAddress (mContext ))
105+ .build ();
106+ Obdmobile .oB_DumpPrivkey (dumpPrivkeyRequest .toByteArray (), new Callback () {
107+ @ Override
108+ public void onError (Exception e ) {
109+ LogUtils .e (TAG , "------------------dumpPrivkeyOnError------------------" + e .getMessage ());
110+ }
111+
112+ @ Override
113+ public void onResponse (byte [] bytes ) {
114+ if (bytes == null ) {
115+ return ;
116+ }
117+ try {
118+ LightningOuterClass .DumpPrivkeyResponse resp = LightningOuterClass .DumpPrivkeyResponse .parseFrom (bytes );
119+ LogUtils .e (TAG , "------------------dumpPrivkeyOnResponse------------------" + resp .toString ());
120+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
121+ @ Override
122+ public void run () {
123+ wifStr = resp .getKeyWif ();
124+ wifTv .setText (wifStr );
125+ }
126+ });
127+ } catch (InvalidProtocolBufferException e ) {
128+ e .printStackTrace ();
129+ }
130+ }
131+ });
132+ mAlertDialog .findViewById (R .id .iv_copy_wif ).setOnClickListener (new View .OnClickListener () {
133+ @ Override
134+ public void onClick (View v ) {
135+ if (StringUtils .isEmpty (wifStr )) {
136+ ToastUtils .showToast (mContext , "WIF is empty" );
137+ return ;
138+ }
139+ //接收需要复制到粘贴板的地址
140+ //Get the address which will copy to clipboard
141+ String toCopyAddress = wifStr ;
142+ //接收需要复制成功的提示语
143+ //Get the notice when you copy success
144+ String toastString = "Already copy the WIF to clipboard!" ;
145+ CopyUtil .SelfCopy (mContext , toCopyAddress , toastString );
146+ }
147+ });
148+ }
149+
150+ public boolean checkedPassMatched (String inputPass ) {
151+ boolean isMatched ;
152+ String localPass = User .getInstance ().getPasswordMd5 (mContext );
153+ isMatched = inputPass .equals (localPass );
154+ return isMatched ;
155+ }
156+
157+ public void release () {
158+ if (mAlertDialog != null ) {
159+ mAlertDialog .dismiss ();
160+ mAlertDialog = null ;
161+ }
162+ }
163+ }
0 commit comments