Search in sources :

Example 1 with WalletFile

use of org.web3j.crypto.WalletFile in project nutzboot by nutzam.

the class Web3jStarter method loadCredentials.

@IocBean(name = "web3jCredentials")
public Map<String, Web3jAccount> loadCredentials() throws IOException, CipherException {
    Map<String, Web3jAccount> accounts = new HashMap<>();
    for (String key : conf.keys()) {
        // web3j.accounts.wendal.address=xxxxxxxxxx
        if (key.startsWith(PRE + "accounts.") && key.endsWith(".password")) {
            Web3jAccount account = new Web3jAccount();
            account.setName(key.substring((PRE + "accounts.").length(), key.length() - ".password".length()));
            log.debug("loading account name=" + account.getName());
            account.setPassword(conf.get(PRE + "accounts." + account.getName() + ".password"));
            if (conf.has(PRE + "accounts." + account.getName() + ".keystore.path")) {
                String path = conf.get(PRE + "accounts." + account.getName() + ".keystore.path");
                byte[] source = null;
                try {
                    InputStream ins = getClass().getClassLoader().getResourceAsStream(path);
                    if (ins != null) {
                        source = Streams.readBytesAndClose(ins);
                    }
                } catch (Throwable e) {
                }
                if (source == null) {
                    source = Files.readBytes(path);
                }
                if (source == null) {
                    throw new FileNotFoundException("not such keystore path=" + path);
                }
                WalletFile wf = objectMapper.readValue(source, WalletFile.class);
                account.setCredentials(Credentials.create(Wallet.decrypt(account.getPassword(), wf)));
                account.setAddress(account.getCredentials().getAddress());
            }
            accounts.put(account.getName(), account);
        }
    }
    return accounts;
}
Also used : HashMap(java.util.HashMap) WalletFile(org.web3j.crypto.WalletFile) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IocBean(org.nutz.ioc.loader.annotation.IocBean)

Example 2 with WalletFile

use of org.web3j.crypto.WalletFile 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)

Aggregations

WalletFile (org.web3j.crypto.WalletFile)2 Message (android.os.Message)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StorableWallet (com.lingtuan.firefly.wallet.vo.StorableWallet)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 HashMap (java.util.HashMap)1 IocBean (org.nutz.ioc.loader.annotation.IocBean)1 CipherException (org.web3j.crypto.CipherException)1 Credentials (org.web3j.crypto.Credentials)1 ECKeyPair (org.web3j.crypto.ECKeyPair)1