示例#1
0
 internal HttpKeyRing(HttpKeyRingMonitor httpKeyRingMonitor)
     : base(httpKeyRingMonitor.BaseKeyRing, httpKeyRingMonitor.AddressCount)
 {
     HttpKeyRingMonitor = httpKeyRingMonitor;
 }
示例#2
0
        public BitcoinService(string baseAddress, ILoggerFactory loggerFactory)
        {
            _logger      = loggerFactory?.CreateLogger <BitcoinService>();
            _baseAddress = baseAddress;

            const string  walletFilePath = @"Wallets/hidden.dat";
            const string  password       = "******";
            const Network network        = Network.TestNet;

            KeyRing keyRing;
            string  mnemonic;

            if (!System.IO.File.Exists(walletFilePath))
            {
                keyRing = KeyRing.Create(out mnemonic, password, walletFilePath, network);
            }
            else
            {
                keyRing = KeyRing.Load(password, walletFilePath);
                if (keyRing.Network != network)
                {
                    throw new Exception("Wrong network");
                }
            }

            _httpKeyRingMonitor = new HttpKeyRingMonitor(_baseAddress, keyRing, addressCount: 100);

            // Report initialization progress
            _httpKeyRingMonitor.InitializationStateChanged += delegate(object sender, EventArgs args)
            {
                var monitor = (HttpKeyRingMonitor)sender;

                _logger.LogDebug($"state changed: {monitor.InitializationState}");

                InitializationState = monitor.InitializationState;
                InitializationStateChanged?.Invoke(null,
                                                   new BitcoinServiceArgs {
                    State = monitor.InitializationState
                });
            };

            _httpKeyRingMonitor.InitializationProgressPercentChanged += delegate(object sender, EventArgs args)
            {
                var monitor = (HttpKeyRingMonitor)sender;

                _logger.LogDebug($"progress changed: {monitor.InitializationProgressPercent}");

                InitializationProgress = monitor.InitializationProgressPercent;
                InitializationProgressChanged?.Invoke(null,
                                                      new BitcoinServiceArgs {
                    InitializationProgress = monitor.InitializationProgressPercent
                });
            };

            _httpKeyRingMonitor.BalanceChanged += delegate(object sender, EventArgs args)
            {
                var monitor   = (HttpKeyRingMonitor)sender;
                var arguments = (BalanceUpdateEventArgs)args;

                _logger.LogDebug($"Change happened\r\n" +
                                 $"Balance of safe: {monitor.KeyRingBalanceInfo.Balance}\r\n" +
                                 $"Confirmed balance of safe: {monitor.KeyRingBalanceInfo.Confirmed}\r\n" +
                                 $"Unconfirmed balance of safe: {monitor.KeyRingBalanceInfo.Unconfirmed}");

                BalanceChanged?.Invoke(null,
                                       new BitcoinServiceArgs {
                    BalanceInfo = monitor.KeyRingBalanceInfo, Transactions = arguments.HistoryRecords
                });
            };
        }