Skip to content

Commit

Permalink
Added wallet generation and code template in new project.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrouR committed Dec 9, 2019
1 parent 24e36ec commit 336b9b2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 26 deletions.
13 changes: 12 additions & 1 deletion src/main/java/org/web3j/console/project/ProjectCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.web3j.console.project.utills.ProjectUtils;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;
import picocli.CommandLine;

import org.web3j.console.project.utils.InputVerifier;
Expand All @@ -36,9 +41,14 @@ public class ProjectCreator {
final TemplateProvider templateProvider;
private final String projectName;
ProjectCreator(final String root, final String packageName, final String projectName)
throws IOException {
throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException {
this.projectName = projectName;
this.projectStructure = new ProjectStructure(root, packageName, projectName);
projectStructure.createWalletDirectory();
final String walletName =
WalletUtils.generateNewWalletFile(
walletPassword, new File(projectStructure.getWalletPath()));

this.templateProvider =
new TemplateProvider.Builder()
.loadGradlewBatScript("gradlew.bat.template")
Expand All @@ -57,6 +67,7 @@ public class ProjectCreator {
InputVerifier.capitalizeFirstLetter(projectName)))
.withPrivateKeyReplacement(
s -> s.replace("<wallet_password_placeholder>", walletPassword))
.withWalletNameReplacement(s -> s.replace("<wallet_name>", walletName))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import org.web3j.crypto.CipherException;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

Expand Down Expand Up @@ -68,7 +72,7 @@ public void run() {
private void createProject() {
try {
new ProjectCreator(outputDir, packageName, projectName).generate();
} catch (final IOException e) {
} catch (final IOException | NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException | CipherException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
exitError("Could not generate project reason:" + sw.toString());
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/web3j/console/project/ProjectImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
*/
package org.web3j.console.project;

import org.web3j.crypto.CipherException;
import picocli.CommandLine;

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import picocli.CommandLine;

import static org.web3j.console.project.InteractiveOptions.getPackageName;
import static org.web3j.console.project.InteractiveOptions.getProjectDestination;
import static org.web3j.console.project.InteractiveOptions.getProjectName;
import static org.web3j.console.project.InteractiveOptions.getSolidityProjectPath;
import static org.web3j.console.project.InteractiveOptions.userWantsTests;
import static org.web3j.console.project.InteractiveOptions.*;
import static org.web3j.utils.Collection.tail;

public class ProjectImporter extends ProjectCreator {
Expand All @@ -36,7 +36,8 @@ public ProjectImporter(
final String packageName,
final String projectName,
final String solidityImportPath)
throws IOException {
throws IOException, CipherException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException, NoSuchProviderException {
super(root, packageName, projectName);
this.solidityImportPath = solidityImportPath;
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/web3j/console/project/ProjectWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Objects;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;

class ProjectWriter {

final void writeResourceFile(
Expand Down Expand Up @@ -48,4 +54,10 @@ final void importSolidityProject(final File solidityImportPath, final String des
new ProjectVisitor(solidityImportPath.getAbsolutePath(), destination));
}
}

final String createWallet(String walletPassword, String walletPath)
throws NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException, CipherException, IOException {
return WalletUtils.generateNewWalletFile(walletPassword, new File(walletPath));
}
}
16 changes: 12 additions & 4 deletions src/main/java/org/web3j/console/project/TemplateProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static class Builder {
private Function<String, String> packageNameReplacement = s -> s;
private Function<String, String> projectNameReplacement = s -> s;
private Function<String, String> privateKeyReplacement = s -> s;

private Function<String, String> walletNameReplacement = s -> s;

public Builder loadMainJavaClass(final String name) throws IOException {
this.mainJavaClass = readFile(name);
Expand Down Expand Up @@ -157,17 +157,25 @@ public Builder withPrivateKeyReplacement(
return this;
}

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

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

private String readFile(final String name) throws IOException {
Expand Down
64 changes: 60 additions & 4 deletions src/main/resources/Template.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
package <package_name>;

import <package_name>.generated.contracts.HelloWorld;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.web3j.abi.datatypes.Address;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.evm.Configuration;
import org.web3j.evm.EmbeddedWeb3jService;
import org.web3j.evm.PassthroughTracer;
import org.web3j.protocol.Web3j;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class <project_name> {

private static String walletPassword = "<wallet_password_placeholder>";
private static final Logger log = LoggerFactory.getLogger(<project_name>.class);


public static void main(String[]args) throws Exception {
Credentials credentials=loadCredentials("<wallet_name>","<wallet_password_placeholder>");
Configuration configuration=fundAccount(credentials);
Web3j web3j=createWeb3jService(configuration);
HelloWorld helloWorld = deployHelloWorld(web3j,credentials,new DefaultGasProvider());
callGreetMethod(helloWorld);
}

private static Credentials loadCredentials(String walletName,String walletPassword)throws CipherException,IOException,URISyntaxException{
String root=System.getProperty("user.dir");
String pathToWallet=String.join(File.separator,root,"src","test","resources","wallet",walletName);
File file=new File(pathToWallet);

log.info("Loading wallet file: "+walletName+" from resources.");
log.info("Creating credentials from from wallet.");
return WalletUtils.loadCredentials(walletPassword,file);
}

private static Configuration fundAccount(Credentials credentials){
// Use the Web3j CLI fund command to obtain testnet Ether
// See <Link to docs here>
log.info("Funding address "+credentials.getAddress()+" with "+10+" ether.");
return new Configuration(new Address(credentials.getAddress()),10);

}

private static Web3j createWeb3jService(Configuration configuration){
// To run against a real Ethereum node use HttpService instead of EmbeddedWeb3jService and pass in the URL of your node.
log.info("Creating a web3j service locally with EmbeddedWeb3jService.");
return Web3j.build(new EmbeddedWeb3jService(configuration,new PassthroughTracer()));
}

public static void main(String[]args){
private static HelloWorld deployHelloWorld(Web3j web3j,Credentials credentials,ContractGasProvider contractGasProvider)throws Exception{
return HelloWorld.deploy(web3j,credentials,contractGasProvider,"Hello Blockchain World!").send();
}

}
}
private static void callGreetMethod(HelloWorld helloWorld)throws Exception{
log.info("Calling the greeting method of contract HelloWorld");
String response = helloWorld.greeting().send();
log.info("Contract returned: "+response);
}
}
21 changes: 13 additions & 8 deletions src/test/java/org/web3j/console/project/ProjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
*/
package org.web3j.console.project;

import java.io.File;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import org.web3j.console.project.utills.ProjectUtils;
import org.web3j.console.project.utils.InputVerifier;

import java.io.File;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ProjectTest {
Expand Down Expand Up @@ -50,6 +51,7 @@ public void setUpProject(@TempDir Path tempDirPath) throws Exception {
Project.builder()
.withTemplateProvider(templateProviderNew)
.withProjectStructure(projectStructure)
.withWallet(ProjectUtils.generateWalletPassword())
.build();
}

Expand Down Expand Up @@ -78,9 +80,9 @@ public void fileCreationTest() {
.exists();
final boolean gradleWrapperSettings =
new File(
projectStructure.getWrapperPath()
+ File.separator
+ "gradle-wrapper.properties")
projectStructure.getWrapperPath()
+ File.separator
+ "gradle-wrapper.properties")
.exists();
final boolean gradleWrapperJar =
new File(projectStructure.getWrapperPath() + File.separator + "gradle-wrapper.jar")
Expand All @@ -90,7 +92,10 @@ public void fileCreationTest() {
.exists();
final boolean gradlewScript =
new File(projectStructure.getProjectRoot() + File.separator + "gradlew").exists();

final File[] filesInWalletDirectory =
new File(projectStructure.getWalletPath()).listFiles();
assert filesInWalletDirectory != null;
assertEquals(2, filesInWalletDirectory.length);
assertTrue(
mainJavaClass
&& greeterContract
Expand Down

0 comments on commit 336b9b2

Please sign in to comment.