Skip to content

Commit

Permalink
add wallet verification + tests + documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Zimmermann committed Jan 14, 2017
1 parent 67b4a00 commit 7d5856a
Show file tree
Hide file tree
Showing 7 changed files with 552 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# exclude maven stuff
target/
dependency-reduced-pom.xml
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,54 @@ After cloning this repo build the command line tool using Maven.
mvn clean package
```

The result of the Maven build is an executable JAR file.
Use the following command to run the tool after building it.
The result of the Maven build is an executable JAR file.

### Creating a Paper Wallet

Use the following command to create a paper wallet.

```
java -jar target/epwg-0.2.0-SNAPSHOT.jar -d C:\Users\mzi\AppData\Local\Temp -p 'test pass phrase'
```

This will lead to some information on the console

```
creating wallet ...
wallet file successfully created
wallet pass phrase: 'test pass phrase'
wallet file location: C:\Users\mzi\AppData\Local\Temp\UTC--2017-01-14T11-34-23.830000000Z--b86bab51c139f9662ccea6547a5e34e13d144bb0.json
writing additional output files ...
html wallet: C:\Users\mzi\AppData\Local\Temp\UTC--2017-01-14T11-34-23.830000000Z--b86bab51c139f9662ccea6547a5e34e13d144bb0.html
address qr code: C:\Users\mzi\AppData\Local\Temp\UTC--2017-01-14T11-34-23.830000000Z--b86bab51c139f9662ccea6547a5e34e13d144bb0.png
```

Three file are created by the tool as indicated in the output above
* The actual wallet file (UTC--2017-01-14T11-34-23.83... .json)
* The HTML file for printing (UTC--2017-01-14T11-34-23.83... .html)
* The image file with the QR code for the paper wallet address (UTC--2017-01-14T11-34-23.83... .png)

### Verifying a (Paper) Wallet

The tool also allows to verify a provided wallet file against a provided pass phrase.

```
java -jar target/epwg-0.2.0-SNAPSHOT.jar -p 'test pass phrase' -v C:\Users\mzi\AppData\Local\Temp\UTC--2017-01-14T11-34-23.830000000Z--b86bab51c139f9662ccea6547a5e34e13d144bb0.json
```
java -jar target/epwg-0.1.0-SNAPSHOT.jar

This will lead to some information on the console

```
veriying wallet file ...
wallet file successfully verified
wallet file: C:\Users\mzi\AppData\Local\Temp\UTC--2017-01-14T11-34-23.830000000Z--b86bab51c139f9662ccea6547a5e34e13d144bb0.json
pass phrase: test pass phrase
```

## Dependencies

The project is maintained with the Eclipse IDE using Java 8. Building the project is done with Maven.
For Ethereum the [web3j](https://web3j.github.io/web3j/) library is used, and [ZXing](https://github.com/zxing/zxing) to create QR codes.
For Ethereum the [web3j](https://web3j.github.io/web3j/) library is used,
to create QR codes the [ZXing](https://github.com/zxing/zxing) library and
for command line parsing the [JCommander](https://github.com/cbeust/jcommander) library.

7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.matthiaszimmermann.ethereum</groupId>
<artifactId>epwg</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Paper Wallet Generator</name>
Expand All @@ -27,6 +27,11 @@
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.48</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
133 changes: 126 additions & 7 deletions src/main/java/org/matthiaszimmermann/ethereum/pwg/Application.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,135 @@
package org.matthiaszimmermann.ethereum.pwg;

import java.io.File;
import java.util.Scanner;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

public class Application {

public static void main(String[] args) throws Exception {
public static final String COMMAND_NAME = "java -jar epgw.jar";
public static final String SWITCH_DIRECTORY = "-d";
public static final String SWITCH_PASS_PHRASE = "-p";
public static final String SWITCH_VERIFY = "-v";

public static final String CREATE_OK = "WALLET CREATION OK";
public static final String CRATE_ERROR = "WALLET CREATION ERROR";

public static final String VERIFY_OK = "WALLET VERIFICATION OK";
public static final String VERIFY_ERROR = "WALLET VERIFICATION ERROR";

public static final String EXT_HTML = "html";
public static final String EXT_PNG = "png";

@Parameter(names = {SWITCH_DIRECTORY, "--target-directory"}, description = "target directory for wallet file etc.")
private String targetDirectory = PaperWallet.getPathToFileDefault();

@Parameter(names = {SWITCH_PASS_PHRASE, "--pass-phrase"}, description = "pass phrase for the wallet file")
private String passPhrase;

@Parameter(names = {SWITCH_VERIFY, "--verify-wallet-file"}, description = "verify the specified wallet file")
private String walletFile = null;

@Parameter(names = {"-s", "--silent"}, description = "silent mode, suppress command line output")
private boolean silent = false;

@Parameter(names = {"-h", "--help"}, help = true)
private boolean help;

public static void main(String[] args) throws Exception {
Application app = new Application();
app.run(args);
}

public String run(String [] args) {
parseCommandLine(args);

if(walletFile != null) {
return verifyWalletFile();
}
else {
return createWalletFile();
}
}

public String verifyWalletFile() {
if(passPhrase == null) {
Scanner scanner = new Scanner(System.in);

// prompt for the user's name
System.out.print("wallet pass phrase: ");

// get their input as a String
passPhrase = scanner.next();
scanner.close();
}

log("veriying wallet file ...");
String statusMessage = PaperWallet.checkWalletFileStatus(new File(walletFile), passPhrase);

if(statusMessage.startsWith(PaperWallet.WALLET_OK)) {
log("wallet file successfully verified");
log("wallet file: " + walletFile);
log("pass phrase: " + passPhrase);

return VERIFY_OK;
}
else {
log("verification failed: " + statusMessage);
log("wallet file: " + walletFile);
log("pass phrase: " + passPhrase);

return String.format("%s %s", VERIFY_ERROR, statusMessage);
}
}

public String createWalletFile() {
PaperWallet pw = null;

// TODO add optional parameter (pass phrase)
// TODO add optional parameter (target directory)
// TODO test + write more tests
// TODO add more docu
log("creating wallet ...");

PaperWallet pw = new PaperWallet(null, null);
try {
pw = new PaperWallet(passPhrase, targetDirectory);
}
catch(Exception e) {
return String.format("%s %s", CRATE_ERROR, e.getLocalizedMessage());
}

log("wallet file successfully created");
log(String.format("wallet pass phrase: '%s'", pw.getPassPhrase()));
log(String.format("wallet file location: %s", pw.getFile().getAbsolutePath()));

String html = WalletPageUtility.createHtml(pw);
FileUtility.saveToFile(html, "testli.html");
byte [] qrCode = QrCodeUtility.contentToPngBytes(pw.getAddress(), 256);

String path = pw.getPathToFile();
String baseName = pw.getBaseName();
String htmlFile = String.format("%s%s%s.%s", path, File.separator, baseName, EXT_HTML);
String pngFile = String.format("%s%s%s.%s", path, File.separator, baseName, EXT_PNG);

log("writing additional output files ...");
FileUtility.saveToFile(html, htmlFile);
FileUtility.saveToFile(qrCode, pngFile);
log(String.format("html wallet: %s", htmlFile));
log(String.format("address qr code: %s", pngFile));

return String.format("%s %s", CREATE_OK, pw.getFile().getAbsolutePath());
}

private void parseCommandLine(String [] args) {
JCommander cmd = new JCommander(this, args);
cmd.setProgramName(COMMAND_NAME);

if(help) {
cmd.usage();
System.exit(0);
}
}

private void log(String message) {
if(!silent) {
System.out.println(message);
}
}
}
Loading

0 comments on commit 7d5856a

Please sign in to comment.