public TumblerClientConfiguration LoadArgs(String[] args)
        {
            ConfigurationFile = args.Where(a => a.StartsWith("-conf=", StringComparison.Ordinal)).Select(a => a.Substring("-conf=".Length).Replace("\"", "")).FirstOrDefault();
            DataDir           = args.Where(a => a.StartsWith("-datadir=", StringComparison.Ordinal)).Select(a => a.Substring("-datadir=".Length).Replace("\"", "")).FirstOrDefault();
            if (DataDir != null && ConfigurationFile != null)
            {
                var isRelativePath = Path.GetFullPath(ConfigurationFile).Length > ConfigurationFile.Length;
                if (isRelativePath)
                {
                    ConfigurationFile = Path.Combine(DataDir, ConfigurationFile);
                }
            }

            Network = args.Contains("-testnet", StringComparer.OrdinalIgnoreCase) ? Network.TestNet :
                      args.Contains("-regtest", StringComparer.OrdinalIgnoreCase) ? Network.RegTest :
                      Network.Main;

            if (ConfigurationFile != null)
            {
                AssetConfigFileExists();
                var configTemp = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));
                Network = configTemp.GetOrDefault <bool>("testnet", false) ? Network.TestNet :
                          configTemp.GetOrDefault <bool>("regtest", false) ? Network.RegTest :
                          Network.Main;
            }

            if (DataDir == null)
            {
                DataDir = DefaultDataDirectory.GetDefaultDirectory("NTumbleBit", Network);
            }

            if (ConfigurationFile == null)
            {
                ConfigurationFile = GetDefaultConfigurationFile(DataDir, Network);
            }
            Logs.Configuration.LogInformation("Network: " + Network);

            Logs.Configuration.LogInformation("Data directory set to " + DataDir);
            Logs.Configuration.LogInformation("Configuration file set to " + ConfigurationFile);

            if (!Directory.Exists(DataDir))
            {
                throw new ConfigurationException("Data directory does not exists");
            }

            var consoleConfig = new TextFileConfiguration(args);
            var config        = TextFileConfiguration.Parse(File.ReadAllText(ConfigurationFile));

            consoleConfig.MergeInto(config, true);
            config.AddAlias("server", "tumbler.server");

            OnlyMonitor   = config.GetOrDefault <bool>("onlymonitor", false);
            Cooperative   = config.GetOrDefault <bool>("cooperative", true);
            TumblerServer = config.GetOrDefault("tumbler.server", null as Uri);
            TorPath       = config.GetOrDefault <string>("torpath", "tor");

            RPCArgs = RPCArgs.Parse(config, Network);

            if (!OnlyMonitor && TumblerServer == null)
            {
                throw new ConfigException("tumbler.server not configured");
            }

            try
            {
                ClassicTumblerParameters.ExtractHashFromUrl(TumblerServer);
            }
            catch (FormatException)
            {
                throw new ConfigException("tumbler.server does not contains the parameter hash");
            }

            try
            {
                var key = config.GetOrDefault("outputwallet.extpubkey", null as string);
                if (key != null)
                {
                    OutputWallet.RootKey = new BitcoinExtPubKey(key, Network);
                }
            }
            catch
            {
                throw new ConfigException("outputwallet.extpubkey is not configured correctly");
            }

            OutputWallet.KeyPath = new KeyPath("0");
            string keyPathString = config.GetOrDefault("outputwallet.keypath", null as string);

            if (keyPathString != null)
            {
                try
                {
                    OutputWallet.KeyPath = new KeyPath(keyPathString);
                }
                catch
                {
                    throw new ConfigException("outputwallet.keypath is not configured correctly");
                }
            }

            if (OutputWallet.KeyPath.ToString().Contains("'"))
            {
                throw new ConfigException("outputwallet.keypath should not contain any hardened derivation");
            }

            if (OutputWallet.RootKey != null && OutputWallet.RootKey.Network != Network)
            {
                throw new ConfigException("outputwallet.extpubkey is pointing an incorrect network");
            }

            OutputWallet.RPCArgs = RPCArgs.Parse(config, Network, "outputwallet");

            AliceConnectionSettings = ConnectionSettingsBase.ParseConnectionSettings("alice", config);
            BobConnectionSettings   = ConnectionSettingsBase.ParseConnectionSettings("bob", config);

            AllowInsecure = config.GetOrDefault <bool>("allowinsecure", IsTest(Network));
            return(this);
        }
예제 #2
0
 public TumblerConfiguration()
 {
     ClassicTumblerParameters = new ClassicTumblerParameters();
 }
예제 #3
0
 public Task ConfirmParametersAsync(ClassicTumblerParameters parameters, StandardCycle standardCyle)
 {
     return(Task.CompletedTask);
 }
예제 #4
0
        async Task ConfigureAsyncCore(TumblerConfiguration conf, ClientInteraction interaction)
        {
            Cooperative = conf.Cooperative;
            ClassicTumblerParameters = conf.ClassicTumblerParameters.Clone();
            Network = conf.Network;
            RPCClient rpcClient = null;

            try
            {
                rpcClient = conf.RPC.ConfigureRPCClient(conf.Network);
            }
            catch
            {
                throw new ConfigException("Please, fix rpc settings in " + conf.ConfigurationFile);
            }

            bool torConfigured = false;

            if (conf.TorSettings != null)
            {
                Exception error = null;
                try
                {
                    _Resources.Add(await conf.TorSettings.SetupAsync(interaction, conf.TorPath).ConfigureAwait(false));
                    Logs.Configuration.LogInformation("Successfully authenticated to Tor");
                    var torRSA = Path.Combine(conf.DataDir, "Tor.rsa");


                    string privateKey = null;
                    if (File.Exists(torRSA))
                    {
                        privateKey = File.ReadAllText(torRSA, Encoding.UTF8);
                    }

                    IPEndPoint routable = GetLocalEndpoint(conf);
                    TorConnection = conf.TorSettings.CreateTorClient2();
                    _Resources.Add(TorConnection);

                    await TorConnection.ConnectAsync().ConfigureAwait(false);

                    await TorConnection.AuthenticateAsync().ConfigureAwait(false);

                    var result = await TorConnection.RegisterHiddenServiceAsync(routable, conf.TorSettings.VirtualPort, privateKey).ConfigureAwait(false);

                    if (privateKey == null)
                    {
                        File.WriteAllText(torRSA, result.PrivateKey, Encoding.UTF8);
                        Logs.Configuration.LogWarning($"Tor RSA private key generated to {torRSA}");
                    }

                    TorUri = result.HiddenServiceUri;
                    Logs.Configuration.LogInformation($"Tor configured on {TorUri.AbsoluteUri}");
                    torConfigured = true;
                }
                catch (ConfigException ex)
                {
                    error = ex;
                }
                catch (TorException ex)
                {
                    error = ex;
                }
                catch (ClientInteractionException)
                {
                }
                if (error != null)
                {
                    Logs.Configuration.LogWarning("Error while configuring Tor hidden service: " + error.Message);
                }
            }

            if (!torConfigured)
            {
                Logs.Configuration.LogWarning("The tumbler is not configured as a Tor Hidden service");
            }

            var rsaFile = Path.Combine(conf.DataDir, "Tumbler.pem");

            if (!File.Exists(rsaFile))
            {
                Logs.Configuration.LogWarning("RSA private key not found, please backup it. Creating...");
                TumblerKey = new RsaKey();
                File.WriteAllBytes(rsaFile, TumblerKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + rsaFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("RSA private key found (" + rsaFile + ")");
                TumblerKey = new RsaKey(File.ReadAllBytes(rsaFile));
            }

            var voucherFile = Path.Combine(conf.DataDir, "Voucher.pem");

            if (!File.Exists(voucherFile))
            {
                Logs.Configuration.LogWarning("Creation of Voucher Key");
                VoucherKey = new RsaKey();
                File.WriteAllBytes(voucherFile, VoucherKey.ToBytes());
                Logs.Configuration.LogInformation("RSA key saved (" + voucherFile + ")");
            }
            else
            {
                Logs.Configuration.LogInformation("Voucher key found (" + voucherFile + ")");
                VoucherKey = new RsaKey(File.ReadAllBytes(voucherFile));
            }

            ClassicTumblerParameters.ServerKey  = TumblerKey.PubKey;
            ClassicTumblerParameters.VoucherKey = VoucherKey.PubKey;
            ClassicTumblerParametersHash        = ClassicTumblerParameters.GetHash();

            if (TorUri != null)
            {
                TumblerUris.Add(CreateTumblerUri(TorUri));
            }

            foreach (var url in conf.GetUrls())
            {
                TumblerUris.Add(CreateTumblerUri(new Uri(url, UriKind.Absolute)));
            }


            Logs.Configuration.LogInformation("");
            Logs.Configuration.LogInformation($"--------------------------------");
            var uris = String.Join(Environment.NewLine, TumblerUris.ToArray().Select(u => u.AbsoluteUri).ToArray());

            Logs.Configuration.LogInformation($"Shareable URIs of the running tumbler are:");
            foreach (var uri in TumblerUris)
            {
                Logs.Configuration.LogInformation(uri.AbsoluteUri);
            }
            Logs.Configuration.LogInformation($"--------------------------------");
            Logs.Configuration.LogInformation("");

            var dbreeze = new DBreezeRepository(Path.Combine(conf.DataDir, "db2"));

            Repository = dbreeze;
            _Resources.Add(dbreeze);
            Tracker  = new Tracker(dbreeze, Network);
            Services = ExternalServices.CreateFromRPCClient(rpcClient, dbreeze, Tracker);
        }
예제 #5
0
 public MainController(TumblerConfiguration configuration, ClassicTumblerRepository repo, ClassicTumblerParameters parameters, ExternalServices services)
 {
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     if (parameters == null)
     {
         throw new ArgumentNullException(nameof(parameters));
     }
     if (services == null)
     {
         throw new ArgumentNullException(nameof(services));
     }
     if (repo == null)
     {
         throw new ArgumentNullException(nameof(repo));
     }
     _Tumbler    = configuration;
     _Repository = repo;
     _Parameters = parameters;
     _Services   = services;
 }