0

I'm new to blockchain development. Currently, I'm learning the Ethereum platform and it sounds a very good environment to start with. I tested the web3j library on my Android application and it works fine. I used the following code to connect to my testrpc node:

Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545"));
 BigInteger gasPrice = BigInteger.valueOf(20000000000L);
    BigInteger gasLimit = BigInteger.valueOf(500000L);
    BigInteger nonce = null;


    String contractAddress="0x0dd3b0efbce5c4eba2dc9b8500ecafb0b1cec28f";

    String from = "0x2d6fcee3c3435ebda9184bdddf8481a87b7d1948";
    List<Type> inputParameters = new ArrayList<>();
   String hash ="232131231232141231231231231232123123123";
     byte[] b =Arrays.copyOfRange(new BigInteger(hash, 16).toByteArray(), 1, 33);

    Type _telNumber = new Bytes32(b);
    Type _publicKey = new Bytes32(b);
    inputParameters.add(_telNumber);
    inputParameters.add(_publicKey);
    Function function = new Function("addPerson",
            inputParameters,
            Collections.<TypeReference<?>>emptyList());
    String functionEncoder = FunctionEncoder.encode(function);



    Transaction transaction = Transaction.createFunctionCallTransaction(from, nonce,gasPrice,gasLimit,contractAddress,new BigInteger("0"), functionEncoder);

    try {
        EthSendTransaction transactionResponse = web3.ethSendTransaction(transaction).sendAsync().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

The above code worked and I was able to interact with a smart contract (call a function).

The main problem is that the from parameter is hardcoded (I got it from the testrpc list of accounts).

What I want to achieve: I want to create an application in which the users can create new wallets (accounts), and use them to transact with the network. I created the wallets succesfully using the following code:

 String filePath = Environment.getExternalStorageDirectory().toString() + "/Pictures";


    Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545"));
    Web3ClientVersion web3ClientVersion = null;

    try {
        String fileName = WalletUtils.generateNewWalletFile("your password",new File(filePath),false);
        Log.d("FILENAME",fileName);

        Credentials credentials = WalletUtils.loadCredentials(
                "your password",
                filePath+"/"+fileName);

        myAddress = credentials.getAddress();
        Log.d("My address",credentials.getAddress());

    } catch (CipherException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }

What's the next step? Should I broadcast my address to the network?

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
Brad Boil
  • 99
  • 1
  • 8

1 Answers1

0

The main problem is that the from parameter is hardcoded (I got it from the testrpc list of accounts).

You can call your file and decrypt it to get your address, every time you create a new account you generate a file with your keys.

val credentialsOne = WalletUtils.loadCredentials("your password", "your path")

When you connect with your node, it will automatically detect the address

Leonardo Alves Machado
  • 2,747
  • 10
  • 38
  • 53
cubo1123
  • 151
  • 1
  • 4