From cf143a4fef146ef3cd9486cd6ce1dfdfdc141935 Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Tue, 28 Jan 2020 08:35:46 +0000 Subject: [PATCH 1/8] Adds support for a default constructor to use online services --- .../org/web3j/account/LocalWeb3jAccount.java | 31 +++++++++++++++++++ .../main/java/org/web3j/protocol/Network.java | 19 ++++++++++++ .../main/java/org/web3j/protocol/Web3j.java | 12 ++++++- .../test/java/org/web3j/protocol/CloudIT.java | 23 ++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/org/web3j/account/LocalWeb3jAccount.java create mode 100644 core/src/main/java/org/web3j/protocol/Network.java create mode 100644 integration-tests/src/test/java/org/web3j/protocol/CloudIT.java diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java new file mode 100644 index 000000000..90847b616 --- /dev/null +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -0,0 +1,31 @@ +package org.web3j.account; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.web3j.protocol.Network; +import org.web3j.protocol.http.HttpService; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class LocalWeb3jAccount { + + private static final Path web3jConfigPath = + Paths.get(System.getProperty("user.home"), ".web3j", ".config"); + + static String SERVICES_ENDPOINT = "http://localhost/api/geth/%s/%s/"; + + public static HttpService getOnlineServicesHttpService(final Network network) throws Exception { + if (web3jConfigPath.toFile().exists()) { + String configContents = new String(Files.readAllBytes(web3jConfigPath)); + final ObjectNode node = new ObjectMapper().readValue(configContents, ObjectNode.class); + if (node.has("loginToken")) { + String httpEndpoint = String.format(SERVICES_ENDPOINT, network.getNetworkName(), node.get("loginToken").asText()); + return new HttpService(httpEndpoint); + } + } + 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"); + } + +} diff --git a/core/src/main/java/org/web3j/protocol/Network.java b/core/src/main/java/org/web3j/protocol/Network.java new file mode 100644 index 000000000..42284f00f --- /dev/null +++ b/core/src/main/java/org/web3j/protocol/Network.java @@ -0,0 +1,19 @@ +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; + } +} diff --git a/core/src/main/java/org/web3j/protocol/Web3j.java b/core/src/main/java/org/web3j/protocol/Web3j.java index 2f21f302d..eae11c1f7 100644 --- a/core/src/main/java/org/web3j/protocol/Web3j.java +++ b/core/src/main/java/org/web3j/protocol/Web3j.java @@ -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 @@ -12,8 +12,10 @@ */ package org.web3j.protocol; +import java.io.IOException; 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; @@ -22,6 +24,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. * diff --git a/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java b/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java new file mode 100644 index 000000000..ddee7d8dc --- /dev/null +++ b/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java @@ -0,0 +1,23 @@ +package org.web3j.protocol; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.web3j.protocol.admin.methods.response.PersonalListAccounts; +import org.web3j.protocol.core.DefaultBlockParameter; +import org.web3j.protocol.core.methods.response.NetVersion; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +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); + } + +} From 2998889fcedcd34ab6639258fdd715d9c88aa685 Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Thu, 30 Jan 2020 21:28:56 +0000 Subject: [PATCH 2/8] Update services URL --- core/src/main/java/org/web3j/account/LocalWeb3jAccount.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java index 90847b616..b7cc35c4d 100644 --- a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -14,7 +14,7 @@ public class LocalWeb3jAccount { private static final Path web3jConfigPath = Paths.get(System.getProperty("user.home"), ".web3j", ".config"); - static String SERVICES_ENDPOINT = "http://localhost/api/geth/%s/%s/"; + static String SERVICES_ENDPOINT = "http://localhost/api/rpc/%s/%s/"; public static HttpService getOnlineServicesHttpService(final Network network) throws Exception { if (web3jConfigPath.toFile().exists()) { From 3319db1483bc2490c359ef276af01e7f164e8540 Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Thu, 30 Jan 2020 23:58:01 +0000 Subject: [PATCH 3/8] Spotless --- .../org/web3j/account/LocalWeb3jAccount.java | 31 ++++++++++++++----- .../main/java/org/web3j/protocol/Network.java | 12 +++++++ .../main/java/org/web3j/protocol/Web3j.java | 1 - .../test/java/org/web3j/protocol/CloudIT.java | 27 +++++++++++----- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java index b7cc35c4d..73c1c4560 100644 --- a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -1,14 +1,27 @@ +/* + * 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.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; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - public class LocalWeb3jAccount { private static final Path web3jConfigPath = @@ -21,11 +34,15 @@ public static HttpService getOnlineServicesHttpService(final Network network) th String configContents = new String(Files.readAllBytes(web3jConfigPath)); final ObjectNode node = new ObjectMapper().readValue(configContents, ObjectNode.class); if (node.has("loginToken")) { - String httpEndpoint = String.format(SERVICES_ENDPOINT, network.getNetworkName(), node.get("loginToken").asText()); + String httpEndpoint = + String.format( + SERVICES_ENDPOINT, + network.getNetworkName(), + node.get("loginToken").asText()); return new HttpService(httpEndpoint); } } - 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"); + 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"); } - } diff --git a/core/src/main/java/org/web3j/protocol/Network.java b/core/src/main/java/org/web3j/protocol/Network.java index 42284f00f..f57d0da8e 100644 --- a/core/src/main/java/org/web3j/protocol/Network.java +++ b/core/src/main/java/org/web3j/protocol/Network.java @@ -1,3 +1,15 @@ +/* + * 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 { diff --git a/core/src/main/java/org/web3j/protocol/Web3j.java b/core/src/main/java/org/web3j/protocol/Web3j.java index eae11c1f7..433de9686 100644 --- a/core/src/main/java/org/web3j/protocol/Web3j.java +++ b/core/src/main/java/org/web3j/protocol/Web3j.java @@ -12,7 +12,6 @@ */ package org.web3j.protocol; -import java.io.IOException; import java.util.concurrent.ScheduledExecutorService; import org.web3j.account.LocalWeb3jAccount; diff --git a/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java b/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java index ddee7d8dc..49cf16202 100644 --- a/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java +++ b/integration-tests/src/test/java/org/web3j/protocol/CloudIT.java @@ -1,23 +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.admin.methods.response.PersonalListAccounts; -import org.web3j.protocol.core.DefaultBlockParameter; -import org.web3j.protocol.core.methods.response.NetVersion; -import static org.junit.jupiter.api.Assertions.assertNotNull; +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") + @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(); + web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf("latest"), false) + .send() + .getBlock() + .getHash(); System.out.println(netVersion); } - } From 35b26d0f2f9cecb24eb278ded5aa186ae8282759 Mon Sep 17 00:00:00 2001 From: Joshua Richardson Date: Fri, 31 Jan 2020 00:34:43 +0000 Subject: [PATCH 4/8] Bump snapshot version due to troubleshooting --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 2bc2ab902..4dc7a2595 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ group=org.web3j -version=4.6.0-SNAPSHOT +version=4.6.1-SNAPSHOT From 59c1b5174d3d24bf80e404482291dfecaa216d39 Mon Sep 17 00:00:00 2001 From: alexandrou Date: Wed, 4 Mar 2020 16:06:18 +0000 Subject: [PATCH 5/8] Refactored LocalWeb3jAccount Added Console exitSuccess method with no message. --- .../main/java/org/web3j/codegen/Console.java | 10 ++++- .../org/web3j/account/LocalWeb3jAccount.java | 43 +++++++++++++------ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/codegen/src/main/java/org/web3j/codegen/Console.java b/codegen/src/main/java/org/web3j/codegen/Console.java index 87f1ff302..8061bd85a 100644 --- a/codegen/src/main/java/org/web3j/codegen/Console.java +++ b/codegen/src/main/java/org/web3j/codegen/Console.java @@ -12,7 +12,9 @@ */ package org.web3j.codegen; -/** Command line utility classes. */ +/** + * Command line utility classes. + */ public class Console { public static void exitError(String message) { System.err.println(message); @@ -27,4 +29,8 @@ public static void exitSuccess(String message) { System.out.println(message); System.exit(0); } -} + + public static void exitSuccess() { + System.exit(0); + } +} \ No newline at end of file diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java index 73c1c4560..f3d10b45e 100644 --- a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -12,6 +12,7 @@ */ package org.web3j.account; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -24,25 +25,43 @@ public class LocalWeb3jAccount { - private static final Path web3jConfigPath = + private final static Path WEB3J_CONFIG_PATH = Paths.get(System.getProperty("user.home"), ".web3j", ".config"); - static String SERVICES_ENDPOINT = "http://localhost/api/rpc/%s/%s/"; + private static String SERVICES_ENDPOINT = "https://%s-eth.epirus.io/%s"; public static HttpService getOnlineServicesHttpService(final Network network) throws Exception { - if (web3jConfigPath.toFile().exists()) { - String configContents = new String(Files.readAllBytes(web3jConfigPath)); - final ObjectNode node = new ObjectMapper().readValue(configContents, ObjectNode.class); - if (node.has("loginToken")) { - String httpEndpoint = - String.format( - SERVICES_ENDPOINT, - network.getNetworkName(), - node.get("loginToken").asText()); - return new HttpService(httpEndpoint); + 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 WEB3J_CONFIG_PATH.toFile().exists(); + } + + public static ObjectNode readConfigAsJson() throws IOException { + String configContents = new String(Files.readAllBytes(WEB3J_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( + SERVICES_ENDPOINT, + network.getNetworkName(), + readConfigAsJson().get("loginToken").asText()); + return new HttpService(httpEndpoint); + } + } From 18051f49f854ed9aa42a8b5de348843ae4ecc5eb Mon Sep 17 00:00:00 2001 From: alexandrou Date: Wed, 4 Mar 2020 16:13:52 +0000 Subject: [PATCH 6/8] changed gradle properties version to match master branch. --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 4dc7a2595..dc9657607 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ group=org.web3j -version=4.6.1-SNAPSHOT +version=4.6.0-SNAPSHOT \ No newline at end of file From 35e9efe04bf4db70e480c1f96e83f1cfe3f81e1e Mon Sep 17 00:00:00 2001 From: alexandrou Date: Wed, 4 Mar 2020 16:14:30 +0000 Subject: [PATCH 7/8] Spotless. --- codegen/src/main/java/org/web3j/codegen/Console.java | 6 ++---- core/src/main/java/org/web3j/account/LocalWeb3jAccount.java | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/codegen/src/main/java/org/web3j/codegen/Console.java b/codegen/src/main/java/org/web3j/codegen/Console.java index 8061bd85a..7c11151e2 100644 --- a/codegen/src/main/java/org/web3j/codegen/Console.java +++ b/codegen/src/main/java/org/web3j/codegen/Console.java @@ -12,9 +12,7 @@ */ package org.web3j.codegen; -/** - * Command line utility classes. - */ +/** Command line utility classes. */ public class Console { public static void exitError(String message) { System.err.println(message); @@ -33,4 +31,4 @@ public static void exitSuccess(String message) { public static void exitSuccess() { System.exit(0); } -} \ No newline at end of file +} diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java index f3d10b45e..99e5943b2 100644 --- a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -25,7 +25,7 @@ public class LocalWeb3jAccount { - private final static Path WEB3J_CONFIG_PATH = + private static final Path WEB3J_CONFIG_PATH = Paths.get(System.getProperty("user.home"), ".web3j", ".config"); private static String SERVICES_ENDPOINT = "https://%s-eth.epirus.io/%s"; @@ -63,5 +63,4 @@ public static HttpService createHttpServiceWithToken(Network network) throws IOE readConfigAsJson().get("loginToken").asText()); return new HttpService(httpEndpoint); } - } From 88003f2f3af835e9b3fb513514cb464a19acbdff Mon Sep 17 00:00:00 2001 From: alexandrou Date: Fri, 6 Mar 2020 12:15:40 +0000 Subject: [PATCH 8/8] Changed web3j references to epirus. --- .../java/org/web3j/account/LocalWeb3jAccount.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java index 99e5943b2..ba4e095da 100644 --- a/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java +++ b/core/src/main/java/org/web3j/account/LocalWeb3jAccount.java @@ -25,10 +25,10 @@ public class LocalWeb3jAccount { - private static final Path WEB3J_CONFIG_PATH = - Paths.get(System.getProperty("user.home"), ".web3j", ".config"); + private static final Path EPIRUS_CONFIG_PATh = + Paths.get(System.getProperty("user.home"), ".epirus", ".config"); - private static String SERVICES_ENDPOINT = "https://%s-eth.epirus.io/%s"; + private static String NODE_RPC_ENDPOINT = "https://%s-eth.epirus.io/%s"; public static HttpService getOnlineServicesHttpService(final Network network) throws Exception { if (configExists()) { @@ -43,11 +43,11 @@ public static HttpService getOnlineServicesHttpService(final Network network) th } public static boolean configExists() { - return WEB3J_CONFIG_PATH.toFile().exists(); + return EPIRUS_CONFIG_PATh.toFile().exists(); } public static ObjectNode readConfigAsJson() throws IOException { - String configContents = new String(Files.readAllBytes(WEB3J_CONFIG_PATH)); + String configContents = new String(Files.readAllBytes(EPIRUS_CONFIG_PATh)); return new ObjectMapper().readValue(configContents, ObjectNode.class); } @@ -58,7 +58,7 @@ public static boolean loginTokenExists(ObjectNode node) { public static HttpService createHttpServiceWithToken(Network network) throws IOException { String httpEndpoint = String.format( - SERVICES_ENDPOINT, + NODE_RPC_ENDPOINT, network.getNetworkName(), readConfigAsJson().get("loginToken").asText()); return new HttpService(httpEndpoint);