Search in sources :

Example 6 with StorableWallet

use of com.lingtuan.firefly.wallet.vo.StorableWallet in project SmartMesh_Android by SmartMeshFoundation.

the class WalletStorage method reLoad.

/**
 * Read in json
 * @param context  context
 */
@SuppressWarnings("unchecked")
public synchronized void reLoad(Context context) throws IOException, ClassNotFoundException {
    String walletList = MySharedPrefs.readWalletList(context);
    String walletModeList = MySharedPrefs.readWalletModeList(context);
    if (TextUtils.isEmpty(walletList) && TextUtils.isEmpty(walletModeList)) {
        return;
    }
    if (mapdb != null) {
        mapdb.clear();
    } else {
        mapdb = new ArrayList<>();
    }
    if (!TextUtils.isEmpty(walletList) && !TextUtils.isEmpty(walletModeList)) {
        try {
            JSONObject object = new JSONObject(walletList);
            JSONArray walletArray = object.optJSONArray("data");
            addWalletArray(context, walletArray, false);
            JSONObject objectMode = new JSONObject(walletModeList);
            JSONArray walletModeArray = objectMode.optJSONArray("data");
            boolean hasExists = false;
            for (int i = 0; i < walletModeArray.length(); i++) {
                JSONObject walletObj = walletModeArray.optJSONObject(i);
                for (int j = 0; j < mapdb.size(); j++) {
                    hasExists = false;
                    if (mapdb.get(j).getPublicKey().equals(walletObj.optString(Constants.WALLET_ADDRESS))) {
                        hasExists = true;
                        break;
                    }
                }
                if (hasExists) {
                    continue;
                }
                StorableWallet storableWallet = new StorableWallet();
                storableWallet.setPublicKey(walletObj.optString(Constants.WALLET_ADDRESS));
                storableWallet.setWalletName(walletObj.optString(Constants.WALLET_NAME));
                storableWallet.setCanExportPrivateKey(walletObj.optInt(Constants.WALLET_EXTRA));
                storableWallet.setBackup(walletObj.optBoolean(Constants.WALLET_BACKUP));
                File destination = new File(new File(context.getFilesDir(), SDCardCtrl.WALLERPATH), storableWallet.getPublicKey());
                if (!destination.exists()) {
                    storableWallet.setWalletType(1);
                }
                addWalletToList(NextApplication.mContext, storableWallet);
                mapdb.add(storableWallet);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else if (TextUtils.isEmpty(walletList) && !TextUtils.isEmpty(walletModeList)) {
        try {
            JSONObject object = new JSONObject(walletModeList);
            JSONArray walletArray = object.optJSONArray("data");
            addWalletArray(context, walletArray, true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else if (!TextUtils.isEmpty(walletList) && TextUtils.isEmpty(walletModeList)) {
        try {
            JSONObject object = new JSONObject(walletList);
            JSONArray walletArray = object.optJSONArray("data");
            addWalletArray(context, walletArray, false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    addAddressToServer(mapdb);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) StorableWallet(com.lingtuan.firefly.wallet.vo.StorableWallet) JSONException(org.json.JSONException) File(java.io.File)

Example 7 with StorableWallet

use of com.lingtuan.firefly.wallet.vo.StorableWallet in project SmartMesh_Android by SmartMeshFoundation.

the class GestureLoginActivity method initWalletInfo.

/**
 * Load or refresh the wallet information
 */
private void initWalletInfo() {
    StorableWallet storableWallet;
    ArrayList<StorableWallet> storableWallets = WalletStorage.getInstance(NextApplication.mContext).get();
    if (storableWallets.size() <= 0) {
        walletBody.setVisibility(View.GONE);
        gestureView.setVisibility(View.VISIBLE);
    } else {
        gestureView.setVisibility(View.GONE);
        walletBody.setVisibility(View.VISIBLE);
    }
    for (int i = 0; i < storableWallets.size(); i++) {
        if (storableWallets.get(i).isSelect()) {
            index = i;
            int imgId = Utils.getWalletImg(GestureLoginActivity.this, i);
            walletImg.setImageResource(imgId);
            storableWallet = storableWallets.get(i);
            storableWallet.setImgId(imgId);
            walletName.setText(storableWallet.getWalletName());
            String address = storableWallet.getPublicKey();
            if (!address.startsWith("0x")) {
                address = "0x" + address;
            }
            walletAddress.setText(address);
            break;
        }
    }
    if (index == -1 && storableWallets.size() > 0) {
        int imgId = Utils.getWalletImg(GestureLoginActivity.this, 0);
        walletImg.setImageResource(imgId);
        storableWallet = storableWallets.get(0);
        storableWallet.setImgId(imgId);
        walletName.setText(storableWallet.getWalletName());
        String address = storableWallet.getPublicKey();
        if (!address.startsWith("0x")) {
            address = "0x" + address;
        }
        walletAddress.setText(address);
    }
}
Also used : StorableWallet(com.lingtuan.firefly.wallet.vo.StorableWallet)

Example 8 with StorableWallet

use of com.lingtuan.firefly.wallet.vo.StorableWallet in project SmartMesh_Android by SmartMeshFoundation.

the class WalletThread method run.

@Override
public void run() {
    try {
        String walletAddress;
        if (type == 0) {
            // Generate a new address 0 x...
            WalletFile walletFile = OwnWalletUtils.generateNewWalletFile(password, false);
            walletAddress = OwnWalletUtils.getWalletFileName(walletFile);
            File destination = new File(new File(context.getFilesDir(), SDCardCtrl.WALLERPATH), walletAddress);
            ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
            objectMapper.writeValue(destination, walletFile);
        } else if (type == 1) {
            // By private key into the new address
            ECKeyPair keys = ECKeyPair.create(new BigInteger(source));
            WalletFile walletFile = OwnWalletUtils.generateWalletFile(password, keys, false);
            walletAddress = OwnWalletUtils.getWalletFileName(walletFile);
            boolean exists = WalletStorage.getInstance(context).checkExists(walletAddress);
            if (exists) {
                Message message = Message.obtain();
                message.what = WalletHandler.WALLET_REPEAT_ERROR;
                mHandler.sendMessage(message);
                return;
            }
            File destination = new File(new File(context.getFilesDir(), SDCardCtrl.WALLERPATH), walletAddress);
            ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
            objectMapper.writeValue(destination, walletFile);
        } else {
            // Through the keyStore import new address
            ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
            WalletFile walletFile = objectMapper.readValue(source, WalletFile.class);
            Credentials credentials = Credentials.create(Wallet.decrypt(password, walletFile));
            credentials.getEcKeyPair().getPublicKey();
            walletFile = OwnWalletUtils.importNewWalletFile(password, credentials.getEcKeyPair(), false);
            walletAddress = OwnWalletUtils.getWalletFileName(walletFile);
            boolean exists = WalletStorage.getInstance(context).checkExists(walletAddress);
            if (exists) {
                Message message = Message.obtain();
                message.what = WalletHandler.WALLET_REPEAT_ERROR;
                mHandler.sendMessage(message);
                return;
            }
            File destination = new File(new File(context.getFilesDir(), SDCardCtrl.WALLERPATH), walletAddress);
            objectMapper = ObjectMapperFactory.getObjectMapper();
            objectMapper.writeValue(destination, walletFile);
        }
        if (TextUtils.isEmpty(walletAddress)) {
            mHandler.sendEmptyMessage(WalletHandler.WALLET_ERROR);
        } else {
            if (TextUtils.isEmpty(walletName)) {
                // When import operation
                walletName = Utils.getWalletName(context);
            }
            StorableWallet storableWallet = new StorableWallet();
            storableWallet.setPublicKey(walletAddress);
            storableWallet.setWalletName(walletName);
            storableWallet.setPwdInfo(pwdInfo);
            if (type == 0) {
                storableWallet.setCanExportPrivateKey(1);
            }
            if (WalletStorage.getInstance(context).get().size() <= 0) {
                storableWallet.setSelect(true);
            }
            int walletMode = MySharedPrefs.readInt(NextApplication.mContext, MySharedPrefs.FILE_USER, MySharedPrefs.KEY_IS_WALLET_PATTERN);
            if (walletMode == 1) {
                MySharedPrefs.writeInt(NextApplication.mContext, MySharedPrefs.FILE_USER, MySharedPrefs.KEY_IS_WALLET_PATTERN, 2);
                WalletStorage.getInstance(context).addWalletList(storableWallet, context);
            } else {
                WalletStorage.getInstance(context).add(storableWallet, context);
            }
            if (WalletStorage.getInstance(context).get().size() > 0) {
                WalletStorage.getInstance(NextApplication.mContext).updateMapDb(storableWallet.getPublicKey());
                WalletStorage.getInstance(NextApplication.mContext).updateWalletToList(NextApplication.mContext, storableWallet.getPublicKey(), false);
            }
            mHandler.sendEmptyMessage(WalletHandler.WALLET_SUCCESS);
            addAddressMethod(walletAddress);
        }
        return;
    } catch (CipherException e) {
        e.printStackTrace();
        mHandler.sendEmptyMessage(WalletHandler.WALLET_PWD_ERROR);
        return;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
        mHandler.sendEmptyMessage(WalletHandler.NO_MEMORY);
        return;
    } catch (Exception e) {
        e.printStackTrace();
    }
    mHandler.sendEmptyMessage(WalletHandler.WALLET_ERROR);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Message(android.os.Message) ECKeyPair(org.web3j.crypto.ECKeyPair) StorableWallet(com.lingtuan.firefly.wallet.vo.StorableWallet) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CipherException(org.web3j.crypto.CipherException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) WalletFile(org.web3j.crypto.WalletFile) CipherException(org.web3j.crypto.CipherException) BigInteger(java.math.BigInteger) NoSuchProviderException(java.security.NoSuchProviderException) File(java.io.File) WalletFile(org.web3j.crypto.WalletFile) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Credentials(org.web3j.crypto.Credentials)

Example 9 with StorableWallet

use of com.lingtuan.firefly.wallet.vo.StorableWallet in project SmartMesh_Android by SmartMeshFoundation.

the class AccountFragment method initWalletInfo.

/**
 * Load or refresh the wallet information
 */
private void initWalletInfo() {
    ArrayList<StorableWallet> storableWallets = WalletStorage.getInstance(getActivity().getApplicationContext()).get();
    for (int i = 0; i < storableWallets.size(); i++) {
        if (storableWallets.get(i).isSelect()) {
            WalletStorage.getInstance(NextApplication.mContext).updateWalletToList(NextApplication.mContext, storableWallets.get(i).getPublicKey(), false);
            index = i;
            int imgId = Utils.getWalletImg(getActivity(), i);
            walletImg.setImageResource(imgId);
            storableWallet = storableWallets.get(i);
            storableWallet.setImgId(imgId);
            break;
        }
    }
    if (index == -1 && storableWallets.size() > 0) {
        int imgId = Utils.getWalletImg(getActivity(), 0);
        walletImg.setImageResource(imgId);
        storableWallet = storableWallets.get(0);
        storableWallet.setImgId(imgId);
    }
    if (storableWallet != null) {
        walletName.setText(storableWallet.getWalletName());
        if (storableWallet.isBackup()) {
            walletBackup.setVisibility(View.GONE);
        } else {
            walletBackup.setVisibility(View.VISIBLE);
        }
        String address = storableWallet.getPublicKey();
        if (!address.startsWith("0x")) {
            address = "0x" + address;
        }
        walletAddress.setText(address);
    }
    mAdapter.resetSource(WalletStorage.getInstance(NextApplication.mContext).get());
    new Handler().postDelayed(new Runnable() {

        public void run() {
            swipe_refresh.setRefreshing(true);
            loadData(true);
        }
    }, 500);
}
Also used : StorableWallet(com.lingtuan.firefly.wallet.vo.StorableWallet) Handler(android.os.Handler) SuppressLint(android.annotation.SuppressLint)

Example 10 with StorableWallet

use of com.lingtuan.firefly.wallet.vo.StorableWallet in project SmartMesh_Android by SmartMeshFoundation.

the class GestureLoginActivity method onClick.

@Override
public void onClick(View v) {
    super.onClick(v);
    switch(v.getId()) {
        case R.id.forgetGestureBtn:
            ArrayList<StorableWallet> storableWallets = WalletStorage.getInstance(NextApplication.mContext).get();
            if (storableWallets.size() <= 0) {
                showToast(getString(R.string.gesture_no_wallet));
                return;
            }
            Intent intent = new Intent(GestureLoginActivity.this, GesturePasswordLoginActivity.class);
            intent.putExtra("type", type);
            startActivityForResult(intent, 100);
            break;
    }
}
Also used : StorableWallet(com.lingtuan.firefly.wallet.vo.StorableWallet) Intent(android.content.Intent)

Aggregations

StorableWallet (com.lingtuan.firefly.wallet.vo.StorableWallet)15 Intent (android.content.Intent)5 File (java.io.File)5 JSONObject (org.json.JSONObject)4 SuppressLint (android.annotation.SuppressLint)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 MainFragmentUI (com.lingtuan.firefly.ui.MainFragmentUI)2 Handler (android.os.Handler)1 Message (android.os.Message)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 OptionPicker (com.lingtuan.firefly.custom.picker.OptionPicker)1 WheelView (com.lingtuan.firefly.custom.picker.WheelView)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 ArrayList (java.util.ArrayList)1 CipherException (org.web3j.crypto.CipherException)1