public TumblerClient CreateTumblerClient(int cycleId, TumblerProtocolType tumblerProtocol, Identity?identity = null, TimeSpan?connectTimeout = null)
 {
     if (identity == null)
     {
         identity = RandomUtils.GetUInt32() % 2 == 0 ? Identity.Alice : Identity.Bob;
     }
     return(CreateTumblerClient(cycleId, identity == Identity.Alice ? AliceSettings : BobSettings, tumblerProtocol, connectTimeout));
 }
예제 #2
0
        public virtual HttpMessageHandler CreateHttpHandler(TumblerProtocolType tumblerProtocol, TimeSpan?connectTimeout)
        {
            if (tumblerProtocol == TumblerProtocolType.Http)
            {
                return(new HttpClientHandler());
            }
            else if (tumblerProtocol == TumblerProtocolType.Tcp)
            {
                var handler = new TCPHttpMessageHandler(new ClientOptions()
                {
                    IncludeHeaders = false
                });

                if (connectTimeout != null)
                {
                    handler.Options.ConnectTimeout = connectTimeout.Value;
                }

                return(handler);
            }

            throw new NotImplementedException($"Tumbler Protocol type {tumblerProtocol} is not implemented.");
        }
예제 #3
0
 public override HttpMessageHandler CreateHttpHandler(TumblerProtocolType tumblerProtocol, TimeSpan?connectTimeout = null)
 {
     throw new NotSupportedException();
 }
예제 #4
0
        public TumbleBitManager(
            ILoggerFactory loggerFactory,
            NodeSettings nodeSettings,
            IWalletManager walletManager,
            IWatchOnlyWalletManager watchOnlyWalletManager,
            ConcurrentChain chain,
            Network network,
            Signals signals,
            IWalletTransactionHandler walletTransactionHandler,
            IWalletSyncManager walletSyncManager,
            IWalletFeePolicy walletFeePolicy,
            IBroadcasterManager broadcasterManager,
            FullNode fullNode,
            ConfigurationOptionWrapper <object>[] configurationOptions)
        {
            this.walletManager            = walletManager as WalletManager;
            this.watchOnlyWalletManager   = watchOnlyWalletManager;
            this.walletSyncManager        = walletSyncManager as WalletSyncManager;
            this.walletTransactionHandler = walletTransactionHandler as WalletTransactionHandler;
            this.chain              = chain;
            this.signals            = signals;
            this.network            = network;
            this.nodeSettings       = nodeSettings;
            this.loggerFactory      = loggerFactory;
            this.logger             = loggerFactory.CreateLogger(this.GetType().FullName);
            this.walletFeePolicy    = walletFeePolicy;
            this.broadcasterManager = broadcasterManager;
            this.connectionManager  = fullNode.ConnectionManager as ConnectionManager;
            this.fullNode           = fullNode;

            foreach (var option in configurationOptions)
            {
                if (option.Name.Equals("RegistrationStoreDirectory"))
                {
                    if (option.Value != null)
                    {
                        this.RegistrationStore = new RegistrationStore((string)option.Value);
                    }
                    else
                    {
                        this.RegistrationStore = new RegistrationStore(this.nodeSettings.DataDir);
                    }
                }
                else if (option.Name.Equals("MasterNodeUri"))
                {
                    if (option.Value != null)
                    {
                        this.TumblerAddress = (string)option.Value;
                    }
                }
                else if (option.Name.Equals("Tor"))
                {
                    UseTor = (bool)option.Value;
                }
                else if (option.Name.Equals("TumblerProtocol"))
                {
                    TumblerProtocol = (TumblerProtocolType)option.Value;
                }
                else if (option.Name.Equals("UseDummyAddress"))
                {
                    UseDummyAddress = (bool)option.Value;
                }
            }

            this.TumblingState = new TumblingState(
                this.loggerFactory,
                this.chain,
                this.walletManager,
                this.watchOnlyWalletManager,
                this.network,
                this.walletTransactionHandler,
                this.walletSyncManager,
                this.walletFeePolicy,
                this.nodeSettings,
                this.broadcasterManager,
                this.connectionManager);

            // Load saved state e.g. previously selected server
            if (File.Exists(this.TumblingState.GetStateFilePath()))
            {
                try
                {
                    this.TumblingState.LoadStateFromMemory();
                }
                catch (NullReferenceException)
                {
                    // The file appears to get corrupted sometimes, not clear why
                    // May be if the node is not shut down correctly
                }
            }

            this.TumblingState.Save();

            // If there was a server address saved, that means we were previously
            // connected to it, and should try to reconnect to it by default when
            // the connect method is invoked by the UI
            if ((this.TumblerAddress == null) && (this.TumblingState.TumblerUri != null))
            {
                this.TumblerAddress = this.TumblingState.TumblerUri.ToString();
            }

            RemoveProgress();
        }
        public async Task ConfigureAsync(TumblerClientConfigurationBase configuration, TumblerProtocolType tumblerProtocol, ClientInteraction interaction = null, bool connectionTest = false)
        {
            interaction = interaction ?? new AcceptAllClientInteraction();
            Network     = configuration.Network;

            // if connectiontest then just test the connection, don't care about anything else
            // todo: refactor it in NTumbleBit for proper connectionTest, it's hacking
            if (connectionTest)
            {
                TumblerServer = configuration.TumblerServer;
                BobSettings   = configuration.BobConnectionSettings;
                AliceSettings = configuration.AliceConnectionSettings;
                AllowInsecure = configuration.AllowInsecure;
                if (this.TumblerServer.IsOnion)
                {
                    await SetupTorAsync(interaction, configuration.TorPath).ConfigureAwait(false);
                }
                else if (configuration.TorMandatory)
                {
                    throw new ConfigException("The tumbler server should use TOR");
                }
                var client = CreateTumblerClient(0, tumblerProtocol, connectTimeout: TimeSpan.FromSeconds(15));
                TumblerParameters = Retry(1, () => client.GetTumblerParameters());
                if (TumblerParameters == null)
                {
                    throw new ConfigException("Unable to download tumbler's parameters");
                }
                return;
            }

            Repository = configuration.DBreezeRepository;
            _Disposables.Add(Repository);
            Tracker  = configuration.Tracker;
            Services = configuration.Services;

            if (!configuration.OnlyMonitor)
            {
                DataDir       = configuration.DataDir;
                TumblerServer = configuration.TumblerServer;
                BobSettings   = configuration.BobConnectionSettings;
                AliceSettings = configuration.AliceConnectionSettings;
                AllowInsecure = configuration.AllowInsecure;

                if (this.TumblerServer.IsOnion)
                {
                    await SetupTorAsync(interaction, configuration.TorPath).ConfigureAwait(false);
                }
                else if (configuration.TorMandatory)
                {
                    throw new ConfigException("The tumbler server should use TOR");
                }

                Cooperative = configuration.Cooperative;

                DestinationWallet = configuration.DestinationWallet;

                try
                {
                    TumblerParameters = Repository.Get <ClassicTumblerParameters>("Configuration", configuration.TumblerServer.ToString());
                }
                catch
                {
                    TumblerParameters = null;
                }
                if (TumblerParameters != null && TumblerParameters.GetHash() != configuration.TumblerServer.ConfigurationHash)
                {
                    TumblerParameters = null;
                }

                var client = CreateTumblerClient(0, tumblerProtocol);

                Logs.Configuration.LogInformation("Downloading tumbler information of " + configuration.TumblerServer.ToString());
                var parameters = Retry(3, () => client.GetTumblerParameters());
                if (parameters == null)
                {
                    throw new ConfigException("Unable to download tumbler's parameters");
                }

                if (parameters.GetHash() != configuration.TumblerServer.ConfigurationHash)
                {
                    throw new ConfigException("The tumbler returned an invalid configuration");
                }

                var standardCycles = new StandardCycles(configuration.Network);
                var standardCycle  = standardCycles.GetStandardCycle(parameters);
                if (parameters.ExpectedAddress != TumblerServer.GetRoutableUri(false).AbsoluteUri)
                {
                    throw new ConfigException("This tumbler has parameters used for an unexpected uri");
                }
                Logs.Configuration.LogInformation("Checking RSA key proof and standardness of the settings...");
                try
                {
                    if (standardCycle == null || !parameters.IsStandard())
                    {
                        Logs.Configuration.LogWarning("This tumbler has non standard parameters");
                        if (!AllowInsecure)
                        {
                            throw new ConfigException("This tumbler has non standard parameters");
                        }
                        standardCycle = null;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception checking tumbler parameters: " + e);
                }

                await interaction.ConfirmParametersAsync(parameters, standardCycle).ConfigureAwait(false);

                if (TumblerParameters == null)
                {
                    Repository.UpdateOrInsert("Configuration", TumblerServer.ToString(), parameters, (o, n) => n);
                    TumblerParameters = parameters;
                    Logs.Configuration.LogInformation("Tumbler parameters saved");
                }

                Logs.Configuration.LogInformation($"Using tumbler {TumblerServer.ToString()}");
            }
        }
        public static async Task <TumblerClientRuntime> FromConfigurationAsync(TumblerClientConfigurationBase configuration, TumblerProtocolType tumblerProtocol, ClientInteraction interaction = null, bool connectionTest = false)
        {
            TumblerClientRuntime runtime = new TumblerClientRuntime();

            runtime.TumblerProtocol = tumblerProtocol;
            try
            {
                await runtime.ConfigureAsync(configuration, tumblerProtocol, interaction, connectionTest).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception during runtime configuration: " + e);
                runtime?.Dispose();
                throw;
            }
            return(runtime);
        }
 public static TumblerClientRuntime FromConfiguration(TumblerClientConfigurationBase configuration, TumblerProtocolType tumblerProtocol, ClientInteraction interaction = null, bool connectionTest = false)
 {
     return(FromConfigurationAsync(configuration, tumblerProtocol, interaction, connectionTest).GetAwaiter().GetResult());
 }
        private TumblerClient CreateTumblerClient(int cycleId, ConnectionSettingsBase settings, TumblerProtocolType tumblerProtocol, TimeSpan?connectTimeout = null)
        {
            if (!AllowInsecure && DateTimeOffset.UtcNow - previousHandlerCreationDate < CircuitRenewInterval)
            {
                throw new PrematureRequestException();
            }
            previousHandlerCreationDate = DateTime.UtcNow;
            var client  = new TumblerClient(Network, TumblerServer, cycleId);
            var handler = settings.CreateHttpHandler(tumblerProtocol, connectTimeout);

            if (handler != null)
            {
                client.SetHttpHandler(handler);
            }
            return(client);
        }