From 24e36ec9edbb676d3a84b3ab64158120db03627d Mon Sep 17 00:00:00 2001 From: alexander Date: Tue, 3 Dec 2019 13:22:53 +0000 Subject: [PATCH] Draft commit for wallet Creation. --- build.gradle | 4 ++- .../org/web3j/console/project/Project.java | 27 +++++++++++++++++-- .../web3j/console/project/ProjectCreator.java | 13 +++++---- .../console/project/ProjectStructure.java | 21 ++++++++++++++- .../console/project/TemplateProvider.java | 17 +++++++++--- .../console/project/utills/ProjectUtils.java | 22 +++++++++++++++ src/main/resources/Template.java | 6 ++++- .../org/web3j/console/KeyImporterTest.java | 4 +-- .../org/web3j/console/WalletCreatorTest.java | 8 +++--- .../org/web3j/console/WalletUpdaterTest.java | 8 +++--- .../console/project/ProjectStructureTest.java | 6 ++++- 11 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 src/main/java/org/web3j/console/project/utills/ProjectUtils.java diff --git a/build.gradle b/build.gradle index d3fcc76..099e9f6 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/src/main/java/org/web3j/console/project/Project.java b/src/main/java/org/web3j/console/project/Project.java index 7779659..79c6582 100644 --- a/src/main/java/org/web3j/console/project/Project.java +++ b/src/main/java/org/web3j/console/project/Project.java @@ -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 { @@ -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; @@ -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()) { @@ -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(), diff --git a/src/main/java/org/web3j/console/project/ProjectCreator.java b/src/main/java/org/web3j/console/project/ProjectCreator.java index 4dd40bf..c055d28 100644 --- a/src/main/java/org/web3j/console/project/ProjectCreator.java +++ b/src/main/java/org/web3j/console/project/ProjectCreator.java @@ -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; @@ -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; @@ -55,6 +55,8 @@ public class ProjectCreator { s.replace( "", InputVerifier.capitalizeFirstLetter(projectName))) + .withPrivateKeyReplacement( + s -> s.replace("", walletPassword)) .build(); } @@ -86,6 +88,7 @@ void generate() { generate(true, Optional.empty()); } + void generate(boolean withTests, Optional solidityFile) { try { Project.Builder builder = @@ -128,4 +131,4 @@ private void onSuccess() { + " has been created in" + projectStructure.getProjectRoot()); } -} +} \ No newline at end of file diff --git a/src/main/java/org/web3j/console/project/ProjectStructure.java b/src/main/java/org/web3j/console/project/ProjectStructure.java index cbddad8..6f9c1f7 100644 --- a/src/main/java/org/web3j/console/project/ProjectStructure.java +++ b/src/main/java/org/web3j/console/project/ProjectStructure.java @@ -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); @@ -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"); } @@ -61,6 +63,10 @@ final String getSolidityPath() { return solidityPath; } + final String getWalletPath() { + return walletPath; + } + final String getMainPath() { return mainPath; } @@ -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); + } } diff --git a/src/main/java/org/web3j/console/project/TemplateProvider.java b/src/main/java/org/web3j/console/project/TemplateProvider.java index 3aab16d..c7402d0 100644 --- a/src/main/java/org/web3j/console/project/TemplateProvider.java +++ b/src/main/java/org/web3j/console/project/TemplateProvider.java @@ -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 packageNameReplacement = s -> s; private Function projectNameReplacement = s -> s; + private Function privateKeyReplacement = s -> s; + public Builder loadMainJavaClass(final String name) throws IOException { this.mainJavaClass = readFile(name); @@ -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; } @@ -149,16 +151,23 @@ public Builder withProjectNameReplacement( return this; } + public Builder withPrivateKeyReplacement( + final Function 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 { diff --git a/src/main/java/org/web3j/console/project/utills/ProjectUtils.java b/src/main/java/org/web3j/console/project/utills/ProjectUtils.java new file mode 100644 index 0000000..c95c63b --- /dev/null +++ b/src/main/java/org/web3j/console/project/utills/ProjectUtils.java @@ -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); + } +} diff --git a/src/main/resources/Template.java b/src/main/resources/Template.java index df8182a..ad6a38d 100644 --- a/src/main/resources/Template.java +++ b/src/main/resources/Template.java @@ -1,7 +1,11 @@ package ; + + public class { - public static void main(String[] args) { + private static String walletPassword = ""; + + public static void main(String[]args){ } } \ No newline at end of file diff --git a/src/test/java/org/web3j/console/KeyImporterTest.java b/src/test/java/org/web3j/console/KeyImporterTest.java index 93f62c3..7b75b2d 100644 --- a/src/test/java/org/web3j/console/KeyImporterTest.java +++ b/src/test/java/org/web3j/console/KeyImporterTest.java @@ -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; diff --git a/src/test/java/org/web3j/console/WalletCreatorTest.java b/src/test/java/org/web3j/console/WalletCreatorTest.java index 0f2c824..d860e4e 100644 --- a/src/test/java/org/web3j/console/WalletCreatorTest.java +++ b/src/test/java/org/web3j/console/WalletCreatorTest.java @@ -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 { diff --git a/src/test/java/org/web3j/console/WalletUpdaterTest.java b/src/test/java/org/web3j/console/WalletUpdaterTest.java index 807734d..5744b01 100644 --- a/src/test/java/org/web3j/console/WalletUpdaterTest.java +++ b/src/test/java/org/web3j/console/WalletUpdaterTest.java @@ -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 { diff --git a/src/test/java/org/web3j/console/project/ProjectStructureTest.java b/src/test/java/org/web3j/console/project/ProjectStructureTest.java index d31785c..69ee0b6 100644 --- a/src/test/java/org/web3j/console/project/ProjectStructureTest.java +++ b/src/test/java/org/web3j/console/project/ProjectStructureTest.java @@ -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