Skip to content

Commit

Permalink
store wallet with password protection, basic miner class
Browse files Browse the repository at this point in the history
  • Loading branch information
saborsoft committed May 7, 2024
1 parent 3f2c8a4 commit f6d3a0e
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
logs/
keys/

target/
!.mvn/wrapper/maven-wrapper.jar
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package hu.saborsoft.blockchain.example.wallet;

import hu.saborsoft.blockchain.support.EncryptionAlgorithm;
import hu.saborsoft.blockchain.wallet.Wallet;

import java.util.Scanner;

public class WalletExample1 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("To create a wallet, please give your name");
String name = in.nextLine();
System.out.println("please create a password");
String password = in.nextLine();
in.close();
Wallet w = new Wallet(name, password, EncryptionAlgorithm.AES);
System.out.println("wallet created for " + w.getName());

// let's load this wallet
Wallet w2 = new Wallet(name, password, EncryptionAlgorithm.AES);
System.out.println("wallet loaded successfully, wallet name=" + w2.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package hu.saborsoft.blockchain.support;

public enum EncryptionAlgorithm {
XOR, AES
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ public static void displayTransaction(Transaction t, PrintStream out, int level)
displayTab(out, level, "}");
}

public static byte[] encrypt(byte[] key, String password, EncryptionAlgorithm algo) {
return switch (algo) {
case XOR -> encryptionByXOR(key, password);
case AES -> encryptionByAES(key, password);
};
}

public static byte[] decrypt(byte[] key, String password, EncryptionAlgorithm algo) {
return switch (algo) {
case XOR -> decryptionByXOR(key, password);
case AES -> decryptionByAES(key, password);
};
}

public static byte[] encryptionByXOR(byte[] key, String password) {
int more = 100;
byte[] p = messageDigestSHA256ToBytes(password);
Expand Down Expand Up @@ -204,7 +218,6 @@ public static byte[] decryptionByXOR(FileInputStream keyIn, String password) {
}
}


public static byte[] encryptionByAES(byte[] key, String password) {
try {
byte[] salt = new byte[8];
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/hu/saborsoft/blockchain/wallet/Miner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hu.saborsoft.blockchain.wallet;

import hu.saborsoft.blockchain.block.Block;

public class Miner extends Wallet {

public Miner(String minerName, String password) {
super(minerName, password);
}

public boolean mineBlock(Block block) {
return block.mineTheBlock();
}

}
58 changes: 56 additions & 2 deletions src/main/java/hu/saborsoft/blockchain/wallet/Wallet.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package hu.saborsoft.blockchain.wallet;

import hu.saborsoft.blockchain.support.EncryptionAlgorithm;
import hu.saborsoft.blockchain.support.UtilityMethods;

import java.io.*;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

public class Wallet {

private static final String KEY_LOCATION = "keys";

private KeyPair keyPair;

private String walletName;

public Wallet(String walletName) {
keyPair = UtilityMethods.generateKeyPair(2048);
public Wallet(String walletName, String password) {
this(walletName, password, EncryptionAlgorithm.AES);
}

public Wallet(String walletName, String password, EncryptionAlgorithm algo) {
keyPair = UtilityMethods.generateKeyPair();
this.walletName = walletName;
try {
populateExistingWallet(password, algo);
System.out.println("A wallet exists with the same name "
+ "and password. Loaded the existing wallet");
} catch (Exception ee) {
try {
prepareWallet(password, algo);
System.out.println("Created a new wallet based on "
+ "the name and password");
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}

public String getName() {
Expand All @@ -29,4 +50,37 @@ public PrivateKey getPrivateKey() {
return keyPair.getPrivate();
}

private void prepareWallet(String password, EncryptionAlgorithm algo) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bo);
out.writeObject(this.keyPair);
byte[] keyBytes = UtilityMethods.encrypt(bo.toByteArray(), password, algo);
File f = new File(KEY_LOCATION);
if (!f.exists()) {
f.mkdir();
}
FileOutputStream fout = new FileOutputStream(getFilePath());
fout.write(keyBytes);
fout.close();
bo.close();
}

private void populateExistingWallet(String password, EncryptionAlgorithm algo) throws IOException, ClassNotFoundException {
FileInputStream fin = new FileInputStream(getFilePath());
byte[] bb = new byte[4096];
int size = fin.read(bb);
fin.close();
byte[] data = new byte[size];
for (int i = 0; i < data.length; i++) {
data[i] = bb[i];
}
byte[] keyBytes = UtilityMethods.decrypt(data, password, algo);
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(keyBytes));
keyPair = (KeyPair) in.readObject();
}

private String getFilePath() {
return KEY_LOCATION + "/" + getName().replace(" ", "_") + "_keys";
}

}

0 comments on commit f6d3a0e

Please sign in to comment.