Generar clave privada y dirección usando web3j

¿Cómo puedo generar una clave privada y una dirección usando web3jen lugar de crear un archivo json de almacén de claves?

Es una buena pregunta

Respuestas (2)

A continuación se muestra mi enfoque, verificado al importar la clave privada del resultado en MetaMask y obtener la misma dirección que se esperaba.

private static JSONObject process(String seed){

         JSONObject processJson = new JSONObject();

         try {
            ECKeyPair ecKeyPair = Keys.createEcKeyPair();
            BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();

            String sPrivatekeyInHex = privateKeyInDec.toString(16);

            WalletFile aWallet = Wallet.createLight(seed, ecKeyPair);
            String sAddress = aWallet.getAddress();


            processJson.put("address", "0x" + sAddress);
            processJson.put("privatekey", sPrivatekeyInHex);


        } catch (CipherException e) {
            //
        } catch (InvalidAlgorithmParameterException e) {
            //
        } catch (NoSuchAlgorithmException e) {
            //
        } catch (NoSuchProviderException e) {
            //
        } 

        return processJson;
}


main(){  // unit test 
    String seed = UUID.randomUUID().toString();
    JSONObject result = process(seed); // get a json containing private key and address
}
String sPrivatekeyInHex = privateKeyInDec.toString(16);¿Qué hace esta línea? ¿Podrías explicar?
@Ajit Soman getPrivateKey() está devolviendo decimal, por lo general, necesitamos usar una clave privada de cadena hexadecimal.
He publicado una pregunta. ¿Podría proporcionarme información sobre esto: ethereum.stackexchange.com/questions/41194/…

Esto es lo que hice:

public class MainActivity extends AppCompatActivity {
    private WalletFile wallet;
    private String password = "PASSWORD";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupBouncyCastle();


        try {
            wallet = createWallet();
        } catch (Exception e) {
            System.out.println("BIG RIP");
        }
    }


    public WalletFile createWallet() throws Exception {
        ECKeyPair keyPair = Keys.createEcKeyPair();
        return Wallet.createLight(password, keyPair);

    }


    private void setupBouncyCastle() {
        final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
        if (provider == null) {
            // Web3j will set up the provider lazily when it's first used.
            return;
        }
        if (provider.getClass().equals(BouncyCastleProvider.class)) {
            // BC with same package name, shouldn't happen in real life.
            return;
        }
        // Android registers its own BC provider. As it might be outdated and might not include
        // all needed ciphers, we substitute it with a known BC bundled in the app.
        // Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
        // of that it's possible to have another BC implementation loaded in VM.
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
}

Por alguna razón, en las versiones más nuevas de web3j, Keys.createEcKeyPair() arroja un error, así que copié un código de aquí: https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java /org/solovyev/android/web3a/App.java#L47 para configurar el castillo hinchable antes de ejecutar.