diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java b/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java index 30d293ab..a91b454a 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/Ethereum.java @@ -120,9 +120,9 @@ public class Ethereum implements org.ethereum.facade.Ethereum { public byte[] createRandomAccount() { - byte[] randomAddress = HashUtil.sha3(HashUtil.randomPeerId()); - wallet.importKey(randomAddress); - return randomAddress; + byte[] randomPrivateKey = HashUtil.sha3(HashUtil.randomPeerId()); + wallet.importKey(randomPrivateKey); + return randomPrivateKey; } public void loadBlockchain() { diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java index 4e49b3f4..8866b782 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumConnector.java @@ -11,7 +11,9 @@ import org.ethereum.net.peerdiscovery.PeerInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.EnumSet; +import java.util.List; public class EthereumConnector extends ServiceConnector { @@ -23,6 +25,22 @@ public class EthereumConnector extends ServiceConnector { super(context, serviceClass); } + public void init(List privateKeys) { + + if (!isBound) + return; + + Message msg = Message.obtain(null, EthereumServiceMessage.MSG_INIT, 0, 0); + Bundle data = new Bundle(); + data.putStringArrayList("privateKeys", (ArrayList)privateKeys); + msg.setData(data); + try { + serviceMessenger.send(msg); + } catch (RemoteException e) { + logger.error("Exception sending message(init) to service: " + e.getMessage()); + } + } + /** * Connect ethereum to peer * @param ip String Peer ip address diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java index 384144c3..5b2acae7 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumRemoteService.java @@ -14,6 +14,7 @@ import org.ethereum.android.manager.BlockLoader; import org.ethereum.android.service.events.EventData; import org.ethereum.android.service.events.EventFlag; import org.ethereum.core.Transaction; +import org.ethereum.crypto.HashUtil; import org.ethereum.facade.Ethereum; import org.ethereum.manager.AdminInfo; import org.ethereum.net.peerdiscovery.PeerInfo; @@ -40,6 +41,7 @@ public class EthereumRemoteService extends EthereumService { static HashMap clientListeners = new HashMap<>(); static EnumMap> listenersByType = new EnumMap>(EventFlag.class); + public boolean isEthereumStarted = false; public EthereumRemoteService() { @@ -136,6 +138,10 @@ public class EthereumRemoteService extends EthereumService { switch (message.what) { + case EthereumServiceMessage.MSG_INIT: + init(message); + break; + case EthereumServiceMessage.MSG_CONNECT: connect(message); break; @@ -203,6 +209,21 @@ public class EthereumRemoteService extends EthereumService { return true; } + protected void init(Message message) { + + if (isEthereumStarted) { + closeEthereum(null); + ethereum = null; + component = null; + isInitialized = false; + initializeEthereum(); + } + Bundle data = message.getData(); + List privateKeys = data.getStringArrayList("privateKeys"); + ethereum.init(privateKeys); + isEthereumStarted = true; + } + /** * Connect to peer * @@ -499,6 +520,7 @@ public class EthereumRemoteService extends EthereumService { protected void closeEthereum(Message message) { ethereum.close(); + isEthereumStarted = false; logger.info("Closed ethereum."); } diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumService.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumService.java index 265b84bb..f7e3b763 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumService.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumService.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.os.IBinder; import org.ethereum.android.di.components.DaggerEthereumComponent; +import org.ethereum.android.di.components.EthereumComponent; import org.ethereum.android.di.modules.EthereumModule; import org.ethereum.android.jsonrpc.JsonRpcServer; import org.ethereum.android.service.events.BlockEventData; @@ -21,7 +22,6 @@ import org.ethereum.core.TransactionReceipt; import org.ethereum.crypto.HashUtil; import org.ethereum.android.Ethereum; import org.ethereum.net.p2p.HelloMessage; -import org.spongycastle.util.encoders.Hex; import java.util.ArrayList; import java.util.List; @@ -36,6 +36,7 @@ public class EthereumService extends Service { static boolean isInitialized = false; static Ethereum ethereum = null; + static EthereumComponent component = null; static JsonRpcServer jsonRpcServer; static Thread jsonRpcServerThread; @@ -61,6 +62,7 @@ public class EthereumService extends Service { jsonRpcServerThread.interrupt(); jsonRpcServerThread = null; } + ethereum.close(); } protected void initializeEthereum() { @@ -73,20 +75,12 @@ public class EthereumService extends Service { System.out.println("Database folder: " + databaseFolder); SystemProperties.CONFIG.setDataBaseDir(databaseFolder); - ethereum = (Ethereum)DaggerEthereumComponent.builder() + component = DaggerEthereumComponent.builder() .ethereumModule(new EthereumModule(this)) - .build().ethereum(); + .build(); + ethereum = (Ethereum)component.ethereum(); ethereum.addListener(new EthereumListener()); - // TODO: Add init and add/remove address service messages - List addresses = new ArrayList(); - byte[] cowAddr = HashUtil.sha3("cow".getBytes()); - addresses.add(Hex.toHexString(cowAddr)); - String secret = CONFIG.coinbaseSecret(); - byte[] cbAddr = HashUtil.sha3(secret.getBytes()); - addresses.add(Hex.toHexString(cbAddr)); - ethereum.init(addresses); - isInitialized = true; } else { System.out.println(" Already initialized"); diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java index 48e70f27..157a940b 100644 --- a/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java +++ b/ethereumj-core-android/src/main/java/org/ethereum/android/service/EthereumServiceMessage.java @@ -77,4 +77,9 @@ public class EthereumServiceMessage { * Command to the service to get the pernding transactions */ public static final int MSG_GET_PENDING_TRANSACTIONS = 15; + + /** + * Command to the service to initialize with specific addresses. If already initialized, restart the service + */ + public static final int MSG_INIT = 16; }