Skip to content

Commit

Permalink
Draft commit for wallet Creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrouR committed Dec 9, 2019
1 parent 6a35b28 commit 24e36ec
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 23 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ dependencies {
"com.sun.xml.bind:jaxb-impl:2.2.11",
"javax.activation:activation:1.1.1",
"org.web3j:hosted-providers:$web3jVersion",
"com.google.code.gson:gson:$gsonVersion"
"com.google.code.gson:gson:$gsonVersion",
"javax.activation:activation:1.1.1",
"org.apache.commons:commons-lang3:3.9"

runtime "org.slf4j:slf4j-nop:$slf4jVersion"

Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/web3j/console/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import org.web3j.console.project.utils.InputVerifier;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;

import org.web3j.console.project.utils.ProgressCounter;

public class Project {
Expand All @@ -31,6 +37,8 @@ public static class Builder {
private ProjectStructure projectStructure;
private TemplateProvider templateProvider;
private File solidityImportPath;
private boolean withWallet = false;
private String walletPassword;

public Builder withSolidityFile(final File solidityImportPath) {
this.solidityImportPath = solidityImportPath;
Expand All @@ -49,6 +57,12 @@ public Builder withTemplateProvider(final TemplateProvider templateProvider) {
return this;
}

public Builder withWallet(String walletPassword) {
this.walletPassword = walletPassword;
this.withWallet = true;
return this;
}

private void buildGradleProject(final String pathToDirectory)
throws IOException, InterruptedException {
if (!isWindows()) {
Expand Down Expand Up @@ -85,8 +99,17 @@ private void executeCommand(final File workingDir, final String[] command)
.waitFor();
}

public Project build() throws IOException, InterruptedException {
projectStructure.createDirectoryStructure();
public Project build() throws IOException, InterruptedException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException {
projectStructure.createMainDirectory();
projectStructure.createTestDirectory();
projectStructure.createSolidityDirectory();
projectStructure.createWrapperDirectory();
if (withWallet) {
projectStructure.createWalletDirectory();
WalletUtils.generateNewWalletFile(
walletPassword, new File(projectStructure.getWalletPath()));
}

final ProjectWriter projectWriter = new ProjectWriter();
projectWriter.writeResourceFile(
templateProvider.getMainJavaClass(),
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/web3j/console/project/ProjectCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Optional;

import org.web3j.console.project.utills.ProjectUtils;
import picocli.CommandLine;

import org.web3j.console.project.utils.InputVerifier;
Expand All @@ -30,11 +31,10 @@
public class ProjectCreator {

public static final String COMMAND_NEW = "new";

private final ProjectStructure projectStructure;
private final TemplateProvider templateProvider;
private final String walletPassword = ProjectUtils.generateWalletPassword();
final ProjectStructure projectStructure;
final TemplateProvider templateProvider;
private final String projectName;

ProjectCreator(final String root, final String packageName, final String projectName)
throws IOException {
this.projectName = projectName;
Expand All @@ -55,6 +55,8 @@ public class ProjectCreator {
s.replace(
"<project_name>",
InputVerifier.capitalizeFirstLetter(projectName)))
.withPrivateKeyReplacement(
s -> s.replace("<wallet_password_placeholder>", walletPassword))
.build();
}

Expand Down Expand Up @@ -86,6 +88,7 @@ void generate() {
generate(true, Optional.empty());
}


void generate(boolean withTests, Optional<File> solidityFile) {
try {
Project.Builder builder =
Expand Down Expand Up @@ -128,4 +131,4 @@ private void onSuccess() {
+ " has been created in"
+ projectStructure.getProjectRoot());
}
}
}
21 changes: 20 additions & 1 deletion src/main/java/org/web3j/console/project/ProjectStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ProjectStructure {
private final String wrapperPath;
private final String root;
private final String projectRoot;
private final String walletPath;

ProjectStructure(final String root, final String packageName, final String projectName) {
this.root = generateRoot(root);
Expand All @@ -34,6 +35,7 @@ public class ProjectStructure {
this.mainPath = generatePath(this.projectRoot, "src", "main", "java", formattedPackageName);
this.solidityPath = generatePath(this.projectRoot, "src", "main", "solidity");
this.testPath = generatePath(this.projectRoot, "src", "test", "java", formattedPackageName);
this.walletPath = generatePath(this.projectRoot, "src", "test", "resources", "wallet");
this.wrapperPath = generatePath(this.projectRoot, "gradle", "wrapper");
}

Expand Down Expand Up @@ -61,6 +63,10 @@ final String getSolidityPath() {
return solidityPath;
}

final String getWalletPath() {
return walletPath;
}

final String getMainPath() {
return mainPath;
}
Expand Down Expand Up @@ -102,10 +108,23 @@ private void createDirectory(final String path) {
directory.mkdirs();
}

void createDirectoryStructure() {
void createMainDirectory() {
createDirectory(mainPath);
}

void createTestDirectory() {
createDirectory(testPath);
}

void createSolidityDirectory() {
createDirectory(solidityPath);
}

void createWrapperDirectory() {
createDirectory(wrapperPath);
}

void createWalletDirectory() {
createDirectory(walletPath);
}
}
17 changes: 13 additions & 4 deletions src/main/java/org/web3j/console/project/TemplateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ public static class Builder {
private String gradlewBatScript;
private String gradlewScript;
private String solidityProject;
private String gradlewWraperJar;
private String gradlewWrapperJar;
private Function<String, String> packageNameReplacement = s -> s;
private Function<String, String> projectNameReplacement = s -> s;
private Function<String, String> privateKeyReplacement = s -> s;


public Builder loadMainJavaClass(final String name) throws IOException {
this.mainJavaClass = readFile(name);
Expand Down Expand Up @@ -133,7 +135,7 @@ public Builder loadGradlewScript(final String name) throws IOException {
}

public Builder loadGradleJar(final String name) {
this.gradlewWraperJar = name;
this.gradlewWrapperJar = name;
return this;
}

Expand All @@ -149,16 +151,23 @@ public Builder withProjectNameReplacement(
return this;
}

public Builder withPrivateKeyReplacement(
final Function<String, String> privateKeyReplacement) {
this.privateKeyReplacement = privateKeyReplacement;
return this;
}

TemplateProvider build() {
return new TemplateProvider(
projectNameReplacement.apply(packageNameReplacement.apply(mainJavaClass)),
projectNameReplacement.apply(packageNameReplacement.apply(privateKeyReplacement.apply(mainJavaClass))),
solidityProject,
packageNameReplacement.apply(gradleBuild),
projectNameReplacement.apply(gradleSettings),
gradlewWrapperSettings,
gradlewBatScript,
gradlewScript,
gradlewWraperJar);
gradlewWrapperJar
);
}

private String readFile(final String name) throws IOException {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/web3j/console/project/utills/ProjectUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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
*
* 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.console.project.utills;

import org.apache.commons.lang3.RandomStringUtils;

public class ProjectUtils {

public static String generateWalletPassword() {
return RandomStringUtils.random(10, true, true);
}
}
6 changes: 5 additions & 1 deletion src/main/resources/Template.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package <package_name>;


public class <project_name> {

public static void main(String[] args) {
private static String walletPassword = "<wallet_password_placeholder>";

public static void main(String[]args){

}
}
4 changes: 2 additions & 2 deletions src/test/java/org/web3j/console/KeyImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.mockito.Matchers.contains;
import static org.mockito.Matchers.startsWith;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/web3j/console/WalletCreatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.mockito.Matchers.contains;
import static org.mockito.Matchers.startsWith;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.web3j.console.KeyImporterTest.WALLET_PASSWORD;

public class WalletCreatorTest {
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/org/web3j/console/WalletUpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.mockito.Matchers.contains;
import static org.mockito.Matchers.startsWith;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.web3j.console.KeyImporterTest.WALLET_PASSWORD;

public class WalletUpdaterTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public class ProjectStructureTest {
public void init(@TempDir Path temp) {
tempDirPath = temp.toString();
projectStructure = new ProjectStructure(tempDirPath, "test.test", "Test");
projectStructure.createDirectoryStructure();
projectStructure.createMainDirectory();
projectStructure.createTestDirectory();
projectStructure.createSolidityDirectory();
projectStructure.createWrapperDirectory();
projectStructure.createTestDirectory();
}

@Test
Expand Down

0 comments on commit 24e36ec

Please sign in to comment.