Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online services implementation #1170

Merged
merged 8 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions codegen/src/main/java/org/web3j/codegen/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public static void exitSuccess(String message) {
System.out.println(message);
System.exit(0);
}

public static void exitSuccess() {
System.exit(0);
}
}
66 changes: 66 additions & 0 deletions core/src/main/java/org/web3j/account/LocalWeb3jAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.account;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import org.web3j.protocol.Network;
import org.web3j.protocol.http.HttpService;

public class LocalWeb3jAccount {

private static final Path EPIRUS_CONFIG_PATh =
Paths.get(System.getProperty("user.home"), ".epirus", ".config");

private static String NODE_RPC_ENDPOINT = "https://%s-eth.epirus.io/%s";

public static HttpService getOnlineServicesHttpService(final Network network) throws Exception {
if (configExists()) {

final ObjectNode node = readConfigAsJson();
if (loginTokenExists(node)) {
return createHttpServiceWithToken(network);
}
}
throw new IllegalStateException(
"Config file does not exist or could not be read. In order to use Web3j without a specified endpoint, you must use the CLI and log in to Web3j Cloud");
}

public static boolean configExists() {
return EPIRUS_CONFIG_PATh.toFile().exists();
}

public static ObjectNode readConfigAsJson() throws IOException {
String configContents = new String(Files.readAllBytes(EPIRUS_CONFIG_PATh));
return new ObjectMapper().readValue(configContents, ObjectNode.class);
}

public static boolean loginTokenExists(ObjectNode node) {
return node.has("loginToken");
}

public static HttpService createHttpServiceWithToken(Network network) throws IOException {
String httpEndpoint =
String.format(
NODE_RPC_ENDPOINT,
network.getNetworkName(),
readConfigAsJson().get("loginToken").asText());
return new HttpService(httpEndpoint);
}
}
31 changes: 31 additions & 0 deletions core/src/main/java/org/web3j/protocol/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.protocol;

public enum Network {
MAINNET("mainnet"),
ROPSTEN("ropsten"),
KOVAN("kovan"),
GORLI("gorli"),
RINKEBY("rinkeby");

public String getNetworkName() {
return network;
}

String network;

Network(final String network) {
this.network = network;
}
}
11 changes: 10 additions & 1 deletion core/src/main/java/org/web3j/protocol/Web3j.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Web3 Labs Ltd.
* Copyright 2019 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
Expand All @@ -14,6 +14,7 @@

import java.util.concurrent.ScheduledExecutorService;

import org.web3j.account.LocalWeb3jAccount;
import org.web3j.protocol.core.Batcher;
import org.web3j.protocol.core.Ethereum;
import org.web3j.protocol.core.JsonRpc2_0Web3j;
Expand All @@ -22,6 +23,14 @@
/** JSON-RPC Request object building factory. */
public interface Web3j extends Ethereum, Web3jRx, Batcher {

static Web3j build() throws Exception {
return new JsonRpc2_0Web3j(LocalWeb3jAccount.getOnlineServicesHttpService(Network.MAINNET));
}

static Web3j build(Network network) throws Exception {
return new JsonRpc2_0Web3j(LocalWeb3jAccount.getOnlineServicesHttpService(network));
}

/**
* Construct a new Web3j instance.
*
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.web3j
version=4.6.0-SNAPSHOT
version=4.6.0-SNAPSHOT
34 changes: 34 additions & 0 deletions integration-tests/src/test/java/org/web3j/protocol/CloudIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.protocol;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.web3j.protocol.core.DefaultBlockParameter;

public class CloudIT {

@Test
@Disabled(
"this will not work unless the machine test setup includes the CLI and has a logged-in user")
public void testWeb3jCloudIsFunctional() throws Exception {
Web3j web3j = Web3j.build();
String netVersion =
web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf("latest"), false)
.send()
.getBlock()
.getHash();
System.out.println(netVersion);
}
}