I need to connect to private ethereum node with using ethereumj without ethereum.conf file. I need configure connect from code using Spring Framework.
2 Answers
It's working for me. May be it will help for some people.
public class EthereumAppConfig implements BeanPostProcessor {
private static final List<String> ipList = new ArrayList<>();
private static ConfigValue value(Object value) {
return ConfigValueFactory.fromAnyRef(value);
}
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
if (o instanceof SystemProperties)
((SystemProperties) o).overrideParams(getConfig());
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
return o;
}
private static Config getConfig() {
ipList.add("0.0.0.0:00000");
List<Map<String, String>> peerActives = new ArrayList<>();
Map<String, String> urls = new HashMap<>();
urls.put("url", "enode://0");
urls.put("url", "enode://1");
urls.put("url", "enode://2");
urls.put("url", "enode://4");
peerActives.add(
urls
);
return ConfigFactory.empty()
.withValue("peer.discovery.enabled", value(true))
.withValue("peer.discovery.external.ip", value("0.0.0.0"))
.withValue("peer.discovery.bind.ip", value("0.0.0.0"))
.withValue("peer.discovery.persist", value("false"))
.withValue("peer.listen.port", value(00000))
.withValue("peer.privateKey", value(Hex.toHexString(ECKey.fromPrivate(("0").getBytes()).getPrivKeyBytes())))
.withValue("peer.networkId", value(76543))
.withValue("sync.enabled", value(true))
.withValue("genesis", value("genesis.json"))
.withValue("database.dir", value("database"))
.withValue("peer.discovery.ip.list", value(ipList))
.withValue("peer.active", value(peerActives))
.withValue("mine.start", value(true));
}
}
Please see the code that I've uploaded to Github which does exactly this :) It contains all the relevant config in the .conf
file.