Skip to content

Commit d2dff4b

Browse files
authored
Merge pull request #37 from AlexandrouR/online-services-integration
Online services integration
2 parents b454c6d + b86a987 commit d2dff4b

File tree

11 files changed

+286
-23
lines changed

11 files changed

+286
-23
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ apply {
4343
}
4444

4545
repositories {
46+
4647
maven { url "https://dl.bintray.com/ethereum/maven/" }
4748
}
4849

@@ -83,4 +84,5 @@ dependencies {
8384
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion",
8485
"org.mockito:mockito-core:$mockitoVersion",
8586
"com.github.tomakehurst:wiremock-jre8:$wireMockVersion"
87+
testImplementation 'org.mockito:mockito-inline:3.3.0'
8688
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=4.6.0-SNAPSHOT
1+
version=4.6.0-SNAPSHOT

src/main/java/org/web3j/console/Runner.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.web3j.codegen.Console;
1616
import org.web3j.codegen.SolidityFunctionWrapperGenerator;
1717
import org.web3j.codegen.TruffleJsonFunctionWrapperGenerator;
18+
import org.web3j.console.account.AccountManager;
1819
import org.web3j.console.config.CliConfig;
1920
import org.web3j.console.project.ProjectCreator;
2021
import org.web3j.console.project.ProjectImporter;
@@ -32,7 +33,7 @@
3233
public class Runner {
3334

3435
private static final String USAGE =
35-
"Usage: web3j version|wallet|solidity|new|import|generate-tests|audit ...";
36+
"Usage: web3j version|wallet|solidity|new|import|generate-tests|audit|account ...";
3637

3738
private static final String LOGO =
3839
"\n" // generated at http://patorjk.com/software/taag
@@ -47,7 +48,6 @@ public class Runner {
4748

4849
public static void main(String[] args) throws Exception {
4950
System.out.println(LOGO);
50-
5151
CliConfig config = CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile());
5252
Updater updater = new Updater(config);
5353
updater.promptIfUpdateAvailable();
@@ -85,14 +85,17 @@ public static void main(String[] args) throws Exception {
8585
case "audit":
8686
ContractAuditor.main(tail(args));
8787
break;
88+
case "account":
89+
AccountManager.main(config, tail(args));
90+
break;
8891
case COMMAND_GENERATE_TESTS:
8992
UnitTestCreator.main(tail(args));
9093
break;
9194
default:
9295
Console.exitError(USAGE);
9396
}
9497
}
95-
9698
config.save();
99+
Console.exitSuccess();
97100
}
98101
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2020 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.console.account;
14+
15+
import java.io.Closeable;
16+
import java.io.IOException;
17+
import java.util.Scanner;
18+
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonParser;
21+
import okhttp3.FormBody;
22+
import okhttp3.OkHttpClient;
23+
import okhttp3.Request;
24+
import okhttp3.RequestBody;
25+
import okhttp3.Response;
26+
import okhttp3.ResponseBody;
27+
28+
import org.web3j.console.config.CliConfig;
29+
30+
import static org.web3j.codegen.Console.exitError;
31+
32+
public class AccountManager implements Closeable {
33+
private static final String USAGE = "account login|logout|create";
34+
private static final String CLOUD_URL = "https://auth.epirus.io";
35+
private OkHttpClient client;
36+
CliConfig config;
37+
38+
public AccountManager(final CliConfig cliConfig, OkHttpClient client) {
39+
this.client = client;
40+
this.config = cliConfig;
41+
}
42+
43+
public static void main(final CliConfig config, final String[] args) throws IOException {
44+
45+
Scanner console = new Scanner(System.in);
46+
if ("create".equals(args[0])) {
47+
System.out.println("Please enter your email address: ");
48+
String email = console.nextLine().trim();
49+
AccountManager accountManager = new AccountManager(config, new OkHttpClient());
50+
accountManager.createAccount(email);
51+
accountManager.close();
52+
} else {
53+
exitError(USAGE);
54+
}
55+
}
56+
57+
public void createAccount(String email) throws IOException {
58+
RequestBody requestBody = createRequestBody(email);
59+
Request newAccountRequest = createRequest(requestBody);
60+
61+
try {
62+
Response sendRawResponse = executeClientCall(newAccountRequest);
63+
ResponseBody body;
64+
if (sendRawResponse.code() == 200 && (body = sendRawResponse.body()) != null) {
65+
String rawResponse = body.string();
66+
JsonObject responseJsonObj = JsonParser.parseString(rawResponse).getAsJsonObject();
67+
68+
if (responseJsonObj.get("token") == null) {
69+
70+
String tokenError = responseJsonObj.get("tokenError").getAsString();
71+
if (tokenError == null || tokenError.isEmpty()) {
72+
System.out.println("Could not retrieve token. Try again later.");
73+
} else {
74+
System.out.println(tokenError);
75+
}
76+
return;
77+
}
78+
String token = responseJsonObj.get("token").getAsString();
79+
config.setLoginToken(token);
80+
System.out.println(
81+
"Account created successfully. You can now use Web3j Cloud. Please confirm your e-mail within 24 hours to continue using all features without interruption.");
82+
} else {
83+
System.out.println("Account creation failed. Please try again later.");
84+
}
85+
86+
} catch (IOException e) {
87+
System.out.println("Could not connect to the server.\nReason:" + e.getMessage());
88+
}
89+
}
90+
91+
protected final Response executeClientCall(Request newAccountRequest) throws IOException {
92+
return client.newCall(newAccountRequest).execute();
93+
}
94+
95+
protected final RequestBody createRequestBody(String email) {
96+
97+
return new FormBody.Builder().add("email", email).build();
98+
}
99+
100+
protected final Request createRequest(RequestBody accountBody) {
101+
102+
return new Request.Builder()
103+
.url(String.format("%s/auth/realms/EpirusPortal/web3j-token/create", CLOUD_URL))
104+
.post(accountBody)
105+
.build();
106+
}
107+
108+
@Override
109+
public void close() throws IOException {
110+
this.client.dispatcher().executorService().shutdown();
111+
this.client.connectionPool().evictAll();
112+
}
113+
}

src/main/java/org/web3j/console/config/CliConfig.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class CliConfig {
2727
private static final Path web3jConfigPath =
2828
Paths.get(System.getProperty("user.home"), ".web3j", ".config");
29-
private static final String defaultServicesUrl = "https://internal.services.web3labs.com";
29+
private static final String defaultServicesUrl = "https://auth.epirus.io";
3030

3131
public static Path getWeb3jConfigPath() {
3232
return web3jConfigPath;
@@ -42,6 +42,7 @@ private static CliConfig initializeDefaultConfig(File configFile) throws IOExcep
4242
defaultServicesUrl,
4343
UUID.randomUUID().toString(),
4444
Version.getVersion(),
45+
null,
4546
null);
4647
}
4748

@@ -60,18 +61,6 @@ public static CliConfig getConfig(File configFile) throws IOException {
6061
}
6162
}
6263

63-
public String getLatestVersion() {
64-
return latestVersion;
65-
}
66-
67-
public void setLatestVersion(String latestVersion) {
68-
this.latestVersion = latestVersion;
69-
}
70-
71-
public boolean isUpdateAvailable() {
72-
return !version.equals(latestVersion);
73-
}
74-
7564
public enum OS {
7665
DARWIN,
7766
FREEBSD,
@@ -109,6 +98,26 @@ public static OS determineOS() {
10998
}
11099
}
111100

101+
public String getLatestVersion() {
102+
return latestVersion;
103+
}
104+
105+
public void setLatestVersion(String latestVersion) {
106+
this.latestVersion = latestVersion;
107+
}
108+
109+
public boolean isUpdateAvailable() {
110+
return !version.equals(latestVersion);
111+
}
112+
113+
public String getLoginToken() {
114+
return loginToken;
115+
}
116+
117+
public void setLoginToken(final String loginToken) {
118+
this.loginToken = loginToken;
119+
}
120+
112121
public void setVersion(final String version) {
113122
this.version = version;
114123
}
@@ -118,18 +127,21 @@ public void setVersion(final String version) {
118127
private String clientId;
119128
private String latestVersion;
120129
private String updatePrompt;
130+
private String loginToken;
121131

122132
public CliConfig(
123133
String version,
124134
String servicesUrl,
125135
String clientId,
126136
String latestVersion,
127-
String updatePrompt) {
137+
String updatePrompt,
138+
String loginToken) {
128139
this.version = version;
129140
this.servicesUrl = servicesUrl;
130141
this.clientId = clientId;
131142
this.latestVersion = latestVersion;
132143
this.updatePrompt = updatePrompt;
144+
this.loginToken = loginToken;
133145
}
134146

135147
public String getVersion() {

src/main/java/org/web3j/console/project/InteractiveOptions.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
package org.web3j.console.project;
1414

1515
import java.io.File;
16+
import java.io.IOException;
1617
import java.nio.file.Path;
1718
import java.util.Optional;
1819
import java.util.Scanner;
1920

21+
import com.fasterxml.jackson.databind.node.ObjectNode;
22+
23+
import org.web3j.account.LocalWeb3jAccount;
2024
import org.web3j.console.project.utils.InputVerifier;
2125

2226
import static java.io.File.separator;
@@ -137,7 +141,6 @@ public static String getSolidityProjectPath() {
137141
}
138142

139143
static String getUserInput() {
140-
141144
return scanner.nextLine();
142145
}
143146

@@ -150,4 +153,24 @@ public static boolean overrideExistingProject() {
150153
String userAnswer = getUserInput();
151154
return userAnswer.toLowerCase().equals("y");
152155
}
156+
157+
public static boolean userHasWeb3jAccount() throws IOException {
158+
if (LocalWeb3jAccount.configExists()) {
159+
ObjectNode objectNode = LocalWeb3jAccount.readConfigAsJson();
160+
return LocalWeb3jAccount.loginTokenExists(objectNode);
161+
}
162+
return false;
163+
}
164+
165+
public static boolean configFileExists() {
166+
return LocalWeb3jAccount.configExists();
167+
}
168+
169+
public static boolean userWantsWeb3jAccount() throws IOException {
170+
171+
print("It looks like you don’t have a Web3j account, would you like to create one?");
172+
print("This will provide free access to the Ethereum network [Y/n]");
173+
String userAnswer = getUserInput();
174+
return userAnswer.toLowerCase().equals("y") || userAnswer.trim().equals("");
175+
}
153176
}

src/main/java/org/web3j/console/project/ProjectCreator.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.web3j.console.project;
1414

1515
import java.io.File;
16+
import java.io.IOException;
1617
import java.io.PrintWriter;
1718
import java.io.StringWriter;
1819
import java.util.ArrayList;
@@ -22,6 +23,8 @@
2223
import org.jetbrains.annotations.NotNull;
2324
import picocli.CommandLine;
2425

26+
import org.web3j.console.account.AccountManager;
27+
import org.web3j.console.config.CliConfig;
2528
import org.web3j.console.project.java.JavaBuilder;
2629
import org.web3j.console.project.java.JavaProjectCreatorCLIRunner;
2730
import org.web3j.console.project.kotlin.KotlinBuilder;
@@ -48,7 +51,7 @@ public ProjectCreator(final String root, final String packageName, final String
4851
this.root = root;
4952
}
5053

51-
public static void main(String[] args) {
54+
public static void main(String[] args) throws IOException {
5255
final List<String> stringOptions = new ArrayList<>();
5356
if (args.length > 0 && args[0].toLowerCase().equals(COMMAND_JAVA)) {
5457
args = tail(args);
@@ -61,7 +64,8 @@ public static void main(String[] args) {
6164
}
6265

6366
@NotNull
64-
private static String[] getValues(String[] args, List<String> stringOptions) {
67+
private static String[] getValues(String[] args, List<String> stringOptions)
68+
throws IOException {
6569
String projectName;
6670
if (args.length == 0) {
6771
stringOptions.add("-n");
@@ -75,6 +79,22 @@ private static String[] getValues(String[] args, List<String> stringOptions) {
7579
stringOptions.add("-o");
7680
stringOptions.add(projectDest);
7781
});
82+
if (InteractiveOptions.configFileExists()) {
83+
if (!InteractiveOptions.userHasWeb3jAccount()) {
84+
if (InteractiveOptions.userWantsWeb3jAccount()) {
85+
AccountManager.main(
86+
CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile()),
87+
new String[] {"create"});
88+
}
89+
}
90+
} else {
91+
if (InteractiveOptions.userWantsWeb3jAccount()) {
92+
AccountManager.main(
93+
CliConfig.getConfig(CliConfig.getWeb3jConfigPath().toFile()),
94+
new String[] {"create"});
95+
}
96+
}
97+
7898
args = stringOptions.toArray(new String[0]);
7999
}
80100
return args;

src/main/java/org/web3j/console/update/Updater.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public Updater(CliConfig config) {
3333
}
3434

3535
public void promptIfUpdateAvailable() {
36+
3637
if (config.isUpdateAvailable()) {
3738
System.out.println(
3839
String.format(

0 commit comments

Comments
 (0)