0

I am using 'org.ethereum:geth:1.6.5' dependency. I am trying to follow github code of https://github.com/ethmobile/ethdroid .I found it gives every time new private and address. I want to have same private key and address when user add same phrase or seed in any device.It should create same wallet based on same phrase (or passowrd).

I tried to create ECKey by adding same SecureRandom object and Provider object everytime.Yet it gives new private key. Is there any way to fullfil this requirements?

My code is:

           String TAG = "MainActivity >";
    SecureRandom secureRandom = new SecureRandom();
    TextView tv;
    Provider provider =  SpongyCastleProvider.getInstance();
    MainActivity activity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
 String stringToConvert = "strategy";
            byte[] theByteArray = stringToConvert.getBytes();

 secureRandom =  SecureRandom.getInstance("SHA1PRNG").setSeed(theByteArray);

  tv = (TextView)findViewById(R.id.tv);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getAddress();
                 //   Account();
                }
            });

}



    private void getAddress(){

        try {


            ECKey eck = new ECKey(provider , secureRandom);
            //new ECKey(Security.getProvider("EC"), new SecureRandom());
            Log.i(TAG , "Private key: " + Hex.toHexString(eck.getPrivKeyBytes()));
            Log.i(TAG , "Public key: " + Hex.toHexString(eck.getPubKey()));
            Log.i(TAG , "Address: " + ("0x" + Hex.toHexString(ECKey.computeAddress(eck.getPubKey()))));

            tv.setText(("0x" + Hex.toHexString(ECKey.computeAddress(eck.getPubKey()))));

        } catch (Exception e) {
            Log.e(TAG + "Exception = ", e.toString());
            e.printStackTrace();
        }

    }
  • Once you generated a key with new ECKey() you have to save it, otherwise you are going to generate new key every time. The behaviour you are describing is correct. Save the private key somewhere and then reuse it. – Nulik Jul 19 '18 at 15:31
  • 1
    I am going to compile keystore package fromgeth for Android in a few weeks more, for an ethereum wallet we are going to release for android, if you are interested in getting this code let me know. – Nulik Jul 19 '18 at 15:33
  • How are you seeding secureRandom? Are you using the same seed each time? Using the same SecureRandom object repeatedly will generate different random numbers each time, but you should be able to call .setSeed(...) with some fixed value to get the same random numbers each time. – user19510 Jul 19 '18 at 16:27
  • @smarx I converted seeds to bytearray than .setSeed(...) and used same object every click.Yet I found differen result everytime in an log. – Kush Jul 20 '18 at 7:54
  • @Nulik Sure.... – Kush Jul 20 '18 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.