Search in sources :

Example 1 with Credentials

use of org.web3j.crypto.Credentials in project SmartMesh_Android by SmartMeshFoundation.

the class GesturePasswordLoginActivity method checkPwd.

/**
 * access to the private key
 * Password @ param walletPwd purse
 */
private void checkPwd() {
    LoadingDialog.show(GesturePasswordLoginActivity.this, "");
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Credentials keys = WalletStorage.getInstance(getApplicationContext()).getFullWallet(GesturePasswordLoginActivity.this, keyStorePwd.getText().toString().trim(), storableWallet.getPublicKey());
                BigInteger privateKey = keys.getEcKeyPair().getPrivateKey();
                Message message = Message.obtain();
                message.obj = privateKey;
                message.what = 1;
                mHandler.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
                mHandler.sendEmptyMessage(4);
            } catch (JSONException e) {
                e.printStackTrace();
                mHandler.sendEmptyMessage(4);
            } catch (CipherException e) {
                e.printStackTrace();
                mHandler.sendEmptyMessage(3);
            } catch (RuntimeException e) {
                e.printStackTrace();
                mHandler.sendEmptyMessage(5);
            }
        }
    }).start();
}
Also used : Message(android.os.Message) CipherException(org.web3j.crypto.CipherException) BigInteger(java.math.BigInteger) JSONException(org.json.JSONException) IOException(java.io.IOException) Credentials(org.web3j.crypto.Credentials)

Example 2 with Credentials

use of org.web3j.crypto.Credentials 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 3 with Credentials

use of org.web3j.crypto.Credentials in project jbpm-work-items by kiegroup.

the class GetBalanceWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        if (StringUtils.isNotEmpty(serviceURL)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            results.put(RESULTS_VALUE, EthereumUtils.getBalanceInEther(credentials, web3j));
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url.");
            throw new IllegalArgumentException("Missing service url.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) HttpService(org.web3j.protocol.http.HttpService) Credentials(org.web3j.crypto.Credentials)

Example 4 with Credentials

use of org.web3j.crypto.Credentials in project jbpm-work-items by kiegroup.

the class SendEtherWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        String amount = (String) workItem.getParameter("Amount");
        String toAddress = (String) workItem.getParameter("ToAddress");
        if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(toAddress) && StringUtils.isNotEmpty(amount) && NumberUtils.isDigits(amount)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            TransactionManager transactionManager = new RawTransactionManager(web3j, credentials);
            if (transfer == null) {
                transfer = new Transfer(web3j, transactionManager);
            }
            int amountToSend = 0;
            if (amount != null) {
                amountToSend = Integer.parseInt(amount);
            }
            TransactionReceipt transactionReceipt = EthereumUtils.sendFundsToContract(credentials, web3j, amountToSend, toAddress, transfer);
            results.put(RESULTS, transactionReceipt);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url or valid ether amount or toAddress.");
            throw new IllegalArgumentException("Missing service url or valid ether amount or toAddress.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) RawTransactionManager(org.web3j.tx.RawTransactionManager) HttpService(org.web3j.protocol.http.HttpService) RawTransactionManager(org.web3j.tx.RawTransactionManager) TransactionManager(org.web3j.tx.TransactionManager) Transfer(org.web3j.tx.Transfer) Credentials(org.web3j.crypto.Credentials)

Example 5 with Credentials

use of org.web3j.crypto.Credentials in project jbpm-work-items by kiegroup.

the class TransactExistingContractWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    try {
        String serviceURL = (String) workItem.getParameter("ServiceURL");
        String contractAddress = (String) workItem.getParameter("ContractAddress");
        String waitForReceiptStr = (String) workItem.getParameter("WaitForReceipt");
        String methodName = (String) workItem.getParameter("MethodName");
        Type methodInputType = (Type) workItem.getParameter("MethodInputType");
        String depositAmount = (String) workItem.getParameter("DepositAmount");
        if (StringUtils.isNotEmpty(serviceURL) && StringUtils.isNotEmpty(contractAddress) && StringUtils.isNotEmpty(methodName)) {
            Map<String, Object> results = new HashMap<String, Object>();
            if (web3j == null) {
                web3j = Web3j.build(new HttpService(serviceURL));
            }
            auth = new EthereumAuth(walletPassword, walletPath, classLoader);
            Credentials credentials = auth.getCredentials();
            int depositEtherAmountToSend = 0;
            if (depositAmount != null) {
                depositEtherAmountToSend = Integer.parseInt(depositAmount);
            }
            boolean waitForReceipt = false;
            if (waitForReceiptStr != null) {
                waitForReceipt = Boolean.parseBoolean(waitForReceiptStr);
            }
            List<Type> methodInputTypeList = new ArrayList<>();
            if (methodInputType != null) {
                methodInputTypeList = Collections.singletonList(methodInputType);
            }
            TransactionReceipt transactionReceipt = EthereumUtils.transactExistingContract(credentials, web3j, depositEtherAmountToSend, EthereumUtils.DEFAULT_GAS_PRICE, EthereumUtils.DEFAULT_GAS_LIMIT, contractAddress, methodName, methodInputTypeList, null, waitForReceipt, EthereumUtils.DEFAULT_SLEEP_DURATION, EthereumUtils.DEFAULT_ATTEMPTS);
            results.put(RESULTS, transactionReceipt);
            workItemManager.completeWorkItem(workItem.getId(), results);
        } else {
            logger.error("Missing service url, valid toAddress or method name to execute.");
            throw new IllegalArgumentException("Missing service url, valid toAddress or method name to execute.");
        }
    } catch (Exception e) {
        logger.error("Error executing workitem: " + e.getMessage());
        handleException(e);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransactionReceipt(org.web3j.protocol.core.methods.response.TransactionReceipt) Type(org.web3j.abi.datatypes.Type) HttpService(org.web3j.protocol.http.HttpService) Credentials(org.web3j.crypto.Credentials)

Aggregations

Credentials (org.web3j.crypto.Credentials)8 HashMap (java.util.HashMap)5 HttpService (org.web3j.protocol.http.HttpService)5 Message (android.os.Message)3 IOException (java.io.IOException)3 BigInteger (java.math.BigInteger)3 CipherException (org.web3j.crypto.CipherException)3 JSONException (org.json.JSONException)2 TransactionReceipt (org.web3j.protocol.core.methods.response.TransactionReceipt)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StorableWallet (com.lingtuan.firefly.wallet.vo.StorableWallet)1 File (java.io.File)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 ArrayList (java.util.ArrayList)1 TypeReference (org.web3j.abi.TypeReference)1 Type (org.web3j.abi.datatypes.Type)1 ECKeyPair (org.web3j.crypto.ECKeyPair)1 WalletFile (org.web3j.crypto.WalletFile)1