Skip to content

Commit

Permalink
Add TLS server ports to MrPlow
Browse files Browse the repository at this point in the history
  • Loading branch information
fireduck64 committed Feb 28, 2022
1 parent a5f7349 commit e5b1548
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 41 deletions.
41 changes: 41 additions & 0 deletions client/src/WalletUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.protobuf.ByteString;
import duckutil.AtomicFileOutputStream;
import duckutil.Config;
import duckutil.ConfigMem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -772,4 +773,44 @@ public static boolean isXpub(String s)
return (s.startsWith("xpub"));
}


/**
* Loads or creates a single key wallet based on the config param name pointing
* to a path.
*/
public static WalletDatabase loadNodeWalletFromConfig(NetworkParams params, Config config, String param_name)
throws Exception
{
if (!config.isSet(param_name)) return null;

config.require(param_name);

TreeMap<String, String> wallet_config_map = new TreeMap<>();
wallet_config_map.put("wallet_path", config.get(param_name));
wallet_config_map.put("key_count", "1");
wallet_config_map.put("key_mode", WalletUtil.MODE_STANDARD);
ConfigMem config_wallet = new ConfigMem(wallet_config_map);
File wallet_path = new File(config_wallet.get("wallet_path"));

WalletDatabase wallet_db = WalletUtil.loadWallet(wallet_path, true, params);
if (wallet_db == null)
{
logger.log(Level.WARNING, String.format("Directory %s does not contain keys, creating new keys", wallet_path.getPath()));
wallet_db = WalletUtil.makeNewDatabase(config_wallet, params);
WalletUtil.saveWallet(wallet_db, wallet_path);

AddressSpecHash spec = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
String addr = AddressUtil.getAddressString("node", spec);

File dir = new File(config.get(param_name));
PrintStream out = new PrintStream(new FileOutputStream( new File(dir, "address.txt"), false));
out.println(addr);
out.close();

}

return wallet_db;

}

}
2 changes: 2 additions & 0 deletions miner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ java_library(
"//lib:atomicfiledb",
"//client",
"@io_grpc_grpc_java//context",
"@io_netty_netty_handler//:io_netty_netty_handler",
"@io_grpc_grpc_java//netty",
"@duckutil//:duckutil_jsonrpc_lib",
"@duckutil//:duckutil_lib",
"@maven//:com_thetransactioncompany_jsonrpc2_base",
Expand Down
28 changes: 28 additions & 0 deletions miner/src/plow/MrPlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import snowblossom.proto.UserServiceGrpc.UserServiceStub;
import com.google.common.collect.ImmutableList;
import java.util.TreeSet;
import snowblossom.client.WalletUtil;
import io.netty.handler.ssl.SslContext;
import snowblossom.lib.tls.CertGen;
import io.grpc.netty.NettyServerBuilder;

public class MrPlow
{
Expand Down Expand Up @@ -86,6 +90,7 @@ public static void main(String args[]) throws Exception
private final PlowLoop loop;
private List<NodeConnection> connections;
private final Server grpc_server;
private final Server grpc_server_tls;

public MrPlow(Config config) throws Exception
{
Expand Down Expand Up @@ -143,6 +148,29 @@ public MrPlow(Config config) throws Exception
.build();
grpc_server.start();

if (config.isSet("tls_mining_pool_port"))
{
int tls_port = config.getInt("tls_mining_pool_port");
config.require("tls_key_path");
WalletDatabase wallet_db = WalletUtil.loadNodeWalletFromConfig(params, config, "tls_key_path");

AddressSpecHash node_tls_address = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
logger.info("My TLS address: " + AddressUtil.getAddressString(Globals.NODE_ADDRESS_STRING, node_tls_address));

SslContext ssl_ctx = CertGen.getServerSSLContext(wallet_db);
grpc_server_tls = NettyServerBuilder
.forPort(tls_port)
.addService(agent)
.sslContext(ssl_ctx)
.build();
grpc_server_tls.start();

}
else
{
grpc_server_tls = null;
}

if (config.isSet("rpc_port"))
{
JsonRpcServer json_server = new JsonRpcServer(config, false);
Expand Down
43 changes: 2 additions & 41 deletions node/src/SnowBlossomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void setStatus(String status)
private void loadWidgets()
throws Exception
{
trustnet_wallet_db = loadWalletFromConfig("trustnet_key_path");
trustnet_wallet_db = WalletUtil.loadNodeWalletFromConfig(params, config, "trustnet_key_path");
if (trustnet_wallet_db != null)
{
AddressSpecHash trust_addr = getTrustnetAddress();
Expand Down Expand Up @@ -299,7 +299,7 @@ private void startServices()
if (config.isSet("tls_service_port"))
{
config.require("tls_key_path");
WalletDatabase wallet_db = loadWalletFromConfig("tls_key_path");
WalletDatabase wallet_db = WalletUtil.loadNodeWalletFromConfig(params, config, "tls_key_path");

node_tls_address = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
logger.info("My TLS address: " + AddressUtil.getAddressString(Globals.NODE_ADDRESS_STRING, node_tls_address));
Expand Down Expand Up @@ -335,45 +335,6 @@ private void startServices()
}


/**
* Loads or creates a single key wallet based on the config param name pointing
* to a path.
*/
private WalletDatabase loadWalletFromConfig(String param_name)
throws Exception
{
if (!config.isSet(param_name)) return null;

config.require(param_name);

TreeMap<String, String> wallet_config_map = new TreeMap<>();
wallet_config_map.put("wallet_path", config.get(param_name));
wallet_config_map.put("key_count", "1");
wallet_config_map.put("key_mode", WalletUtil.MODE_STANDARD);
ConfigMem config_wallet = new ConfigMem(wallet_config_map);
File wallet_path = new File(config_wallet.get("wallet_path"));

WalletDatabase wallet_db = WalletUtil.loadWallet(wallet_path, true, params);
if (wallet_db == null)
{
logger.log(Level.WARNING, String.format("Directory %s does not contain keys, creating new keys", wallet_path.getPath()));
wallet_db = WalletUtil.makeNewDatabase(config_wallet, params);
WalletUtil.saveWallet(wallet_db, wallet_path);

AddressSpecHash spec = AddressUtil.getHashForSpec(wallet_db.getAddresses(0));
String addr = AddressUtil.getAddressString("node", spec);

File dir = new File(config.get(param_name));
PrintStream out = new PrintStream(new FileOutputStream( new File(dir, "address.txt"), false));
out.println(addr);
out.close();

}

return wallet_db;

}


private void loadDB()
throws Exception
Expand Down

0 comments on commit e5b1548

Please sign in to comment.