Beispiel #1
0
        public (PrivateKey PrivateKey, Result Result) GenerateKey(SecureString password)
        {
            var privateKey = new PrivateKeyProvider(_cryptoRandom).PrivateKey;
            var result     = StoreKey(privateKey, password);

            return(result.ResultType == ResultType.Success ? (privateKey, result) : (null, result));
        }
Beispiel #2
0
        public void Initialize()
        {
            _logManager            = new OneLoggerLogManager(new SimpleConsoleLogger());
            _configurationProvider = new JsonConfigProvider();
            ((NetworkConfig)_configurationProvider.GetConfig <NetworkConfig>()).DbBasePath = Path.Combine(Path.GetTempPath(), "PeerManagerTests");
            if (!Directory.Exists(_configurationProvider.GetConfig <NetworkConfig>().DbBasePath))
            {
                Directory.CreateDirectory(_configurationProvider.GetConfig <NetworkConfig>().DbBasePath);
            }
            _nodeFactory = new NodeFactory();
            _localPeer   = new TestRlpxPeer();
            var keyProvider = new PrivateKeyProvider(new CryptoRandom());
            var key         = keyProvider.PrivateKey.PublicKey;

            _synchronizationManager = Substitute.For <ISynchronizationManager>();

            var nodeTable = new NodeTable(_nodeFactory, Substitute.For <IKeyStore>(), new NodeDistanceCalculator(_configurationProvider), _configurationProvider, _logManager);

            nodeTable.Initialize(new NodeId(key));

            _discoveryManager = new DiscoveryManager(new NodeLifecycleManagerFactory(_nodeFactory, nodeTable, new DiscoveryMessageFactory(_configurationProvider), Substitute.For <IEvictionManager>(), new NodeStatsProvider(_configurationProvider), _configurationProvider, _logManager), _nodeFactory, nodeTable, new DiscoveryStorage(_configurationProvider, _nodeFactory, _logManager, new PerfService(_logManager)), _configurationProvider, _logManager);
            _discoveryManager.MessageSender = Substitute.For <IMessageSender>();

            _peerManager = new PeerManager(_localPeer, _discoveryManager, _synchronizationManager, new NodeStatsProvider(_configurationProvider), new PeerStorage(_configurationProvider, _nodeFactory, _logManager, new PerfService(_logManager)), _nodeFactory, _configurationProvider, new PerfService(_logManager), _logManager);
        }
Beispiel #3
0
        public byte[] Encrypt(PublicKey recipientPublicKey, byte[] plaintext, byte[] macData)
        {
            byte[]     iv = _cryptoRandom.GenerateRandomBytes(KeySize / 8);
            PrivateKey ephemeralPrivateKey = new PrivateKeyProvider(_cryptoRandom).PrivateKey;

            ECPublicKeyParameters  publicKeyParameters           = BouncyCrypto.WrapPublicKey(recipientPublicKey);
            ECPrivateKeyParameters ephemeralPrivateKeyParameters = BouncyCrypto.WrapPrivateKey(ephemeralPrivateKey);
            EthereumIesEngine      iesEngine = MakeIesEngine(true, publicKeyParameters, ephemeralPrivateKeyParameters, iv);

            try
            {
                byte[]       cipher       = iesEngine.ProcessBlock(plaintext, 0, plaintext.Length, macData);
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(ephemeralPrivateKey.PublicKey.PrefixedBytes, 0, ephemeralPrivateKey.PublicKey.PrefixedBytes.Length);
                memoryStream.Write(iv, 0, iv.Length);
                memoryStream.Write(cipher, 0, cipher.Length);
                return(memoryStream.ToArray());
            }
            catch (InvalidCipherTextException)
            {
                throw;
            }
            catch (IOException)
            {
                throw;
            }
        }
Beispiel #4
0
        private static void Start()
        {
            var privateKeyProvider = new PrivateKeyProvider(PrivateKey);
            var config             = new DiscoveryConfigurationProvider(new NetworkHelper(Logger));
            var signer             = new Signer();
            var cryptoRandom       = new CryptoRandom();
            var configProvider     = new ConfigurationProvider();

            var nodeFactory = new NodeFactory();
            var calculator  = new NodeDistanceCalculator(config);

            var nodeTable = new NodeTable(config, nodeFactory, new FileKeyStore(configProvider, new JsonSerializer(Logger), new AesEncrypter(configProvider, Logger), cryptoRandom, Logger), Logger, calculator);

            var evictionManager  = new EvictionManager(nodeTable, Logger);
            var lifecycleFactory = new NodeLifecycleManagerFactory(nodeFactory, nodeTable, Logger, config, new DiscoveryMessageFactory(config), evictionManager);

            var discoveryManager = new DiscoveryManager(Logger, config, lifecycleFactory, nodeFactory, nodeTable);

            var nodesLocator           = new NodesLocator(nodeTable, discoveryManager, config, Logger);
            var discoveryMesageFactory = new DiscoveryMessageFactory(config);
            var nodeIdResolver         = new NodeIdResolver(signer);

            var pingSerializer      = new PingMessageSerializer(signer, privateKeyProvider, discoveryMesageFactory, nodeIdResolver, nodeFactory);
            var pongSerializer      = new PongMessageSerializer(signer, privateKeyProvider, discoveryMesageFactory, nodeIdResolver, nodeFactory);
            var findNodeSerializer  = new FindNodeMessageSerializer(signer, privateKeyProvider, discoveryMesageFactory, nodeIdResolver, nodeFactory);
            var neighborsSerializer = new NeighborsMessageSerializer(signer, privateKeyProvider, discoveryMesageFactory, nodeIdResolver, nodeFactory);

            var messageSerializationService = new MessageSerializationService();

            messageSerializationService.Register(pingSerializer);
            messageSerializationService.Register(pongSerializer);
            messageSerializationService.Register(findNodeSerializer);
            messageSerializationService.Register(neighborsSerializer);


            //P2P initialization
            IMessagePad eip8Pad = new Eip8MessagePad(cryptoRandom);

            messageSerializationService.Register(new AuthEip8MessageSerializer(eip8Pad));
            messageSerializationService.Register(new AckEip8MessageSerializer(eip8Pad));
            messageSerializationService.Register(new HelloMessageSerializer());
            messageSerializationService.Register(new DisconnectMessageSerializer());
            messageSerializationService.Register(new Nethermind.Network.P2P.PingMessageSerializer());
            messageSerializationService.Register(new Nethermind.Network.P2P.PongMessageSerializer());
            messageSerializationService.Register(new StatusMessageSerializer());
            IEciesCipher eciesCipher = new EciesCipher(cryptoRandom);
            IEncryptionHandshakeService encryptionHandshakeService = new EncryptionHandshakeService(messageSerializationService, eciesCipher, cryptoRandom, signer, PrivateKey, Logger);
            var p2pManager = new P2PManager(encryptionHandshakeService, Logger, messageSerializationService, Substitute.For <ISynchronizationManager>());

            //Connect discovery with P2P
            discoveryManager.RegisterDiscoveryListener(p2pManager);

            _discoveryApp = new DiscoveryApp(config, nodesLocator, Logger, discoveryManager, nodeFactory, nodeTable, messageSerializationService, cryptoRandom);
            _discoveryApp.Start(PrivateKey.PublicKey);
        }
Beispiel #5
0
 public ProtocolListener(
     PrivateKeyProvider keyProvider,
     AgentSettings settings,
     ILifetimeScope scope,
     ILogger log)
 {
     _keyProvider = keyProvider;
     _settings    = settings;
     _scope       = scope;
     _log         = log;
 }
        public SerializationBuilder WithDiscovery(PrivateKey privateKey)
        {
            var    config             = new JsonConfigProvider();
            Signer signer             = new Signer();
            var    privateKeyProvider = new PrivateKeyProvider(privateKey);

            PingMessageSerializer      pingSerializer      = new PingMessageSerializer(signer, privateKeyProvider, new DiscoveryMessageFactory(config), new NodeIdResolver(signer), new NodeFactory());
            PongMessageSerializer      pongSerializer      = new PongMessageSerializer(signer, privateKeyProvider, new DiscoveryMessageFactory(config), new NodeIdResolver(signer), new NodeFactory());
            FindNodeMessageSerializer  findNodeSerializer  = new FindNodeMessageSerializer(signer, privateKeyProvider, new DiscoveryMessageFactory(config), new NodeIdResolver(signer), new NodeFactory());
            NeighborsMessageSerializer neighborsSerializer = new NeighborsMessageSerializer(signer, privateKeyProvider, new DiscoveryMessageFactory(config), new NodeIdResolver(signer), new NodeFactory());

            return(With(pingSerializer)
                   .With(pongSerializer)
                   .With(findNodeSerializer)
                   .With(neighborsSerializer));
        }
Beispiel #7
0
        public async Task Initialize()
        {
            _logManager            = new OneLoggerLogManager(new SimpleConsoleLogger());
            _configurationProvider = new JsonConfigProvider();
            ((NetworkConfig)_configurationProvider.GetConfig <INetworkConfig>()).DbBasePath = Path.Combine(Path.GetTempPath(), "PeerManagerTests");
            if (!Directory.Exists(_configurationProvider.GetConfig <INetworkConfig>().DbBasePath))
            {
                Directory.CreateDirectory(_configurationProvider.GetConfig <INetworkConfig>().DbBasePath);
            }

            var serializationService = Build.A.SerializationService().WithEncryptionHandshake().WithP2P().WithEth().TestObject;

            var   syncManager  = Substitute.For <ISynchronizationManager>();
            Block genesisBlock = Build.A.Block.Genesis.TestObject;

            syncManager.Head.Returns(genesisBlock.Header);
            syncManager.Genesis.Returns(genesisBlock.Header);

            _nodeFactory = new NodeFactory();
            _localPeer   = new TestRlpxPeer();
            var keyProvider = new PrivateKeyProvider(new CryptoRandom());
            var key         = keyProvider.PrivateKey.PublicKey;

            _synchronizationManager = Substitute.For <ISynchronizationManager>();

            var nodeTable = new NodeTable(_nodeFactory, Substitute.For <IKeyStore>(), new NodeDistanceCalculator(_configurationProvider), _configurationProvider, _logManager);

            nodeTable.Initialize(new NodeId(key));

            INetworkConfig networkConfig = _configurationProvider.GetConfig <INetworkConfig>();
            IStatsConfig   statsConfig   = _configurationProvider.GetConfig <IStatsConfig>();

            _discoveryManager = new DiscoveryManager(new NodeLifecycleManagerFactory(_nodeFactory, nodeTable, new DiscoveryMessageFactory(_configurationProvider), Substitute.For <IEvictionManager>(), new NodeStatsProvider(_configurationProvider.GetConfig <IStatsConfig>(), _nodeFactory, _logManager), _configurationProvider, _logManager), _nodeFactory, nodeTable, new NetworkStorage("test", networkConfig, _logManager, new PerfService(_logManager)), _configurationProvider, _logManager);
            _discoveryManager.MessageSender = Substitute.For <IMessageSender>();

            var networkStorage = new NetworkStorage("test", networkConfig, _logManager, new PerfService(_logManager));

            _peerManager = new PeerManager(_localPeer, _discoveryManager, _synchronizationManager, new NodeStatsProvider(statsConfig, _nodeFactory, _logManager), networkStorage, _nodeFactory, _configurationProvider, new PerfService(_logManager), _logManager);
            _peerManager.Init(true);
        }
Beispiel #8
0
        private Task InitDiscovery(InitParams initParams)
        {
            _logger.Info("Initializing Discovery");

            if (initParams.DiscoveryPort.HasValue)
            {
                _configProvider.GetConfig <NetworkConfig>().MasterPort = initParams.DiscoveryPort.Value;
            }

            var privateKeyProvider      = new PrivateKeyProvider(_privateKey);
            var discoveryMessageFactory = new DiscoveryMessageFactory(_configProvider);
            var nodeIdResolver          = new NodeIdResolver(_signer);

            var msgSerializersProvider = new DiscoveryMsgSerializersProvider(_messageSerializationService, _signer, privateKeyProvider, discoveryMessageFactory, nodeIdResolver, _nodeFactory);

            msgSerializersProvider.RegisterDiscoverySerializers();

            var nodeDistanceCalculator = new NodeDistanceCalculator(_configProvider);
            var nodeTable = new NodeTable(_nodeFactory, _keyStore, nodeDistanceCalculator, _configProvider, _logManager);

            var evictionManager      = new EvictionManager(nodeTable, _logManager);
            var nodeLifeCycleFactory = new NodeLifecycleManagerFactory(_nodeFactory, nodeTable, discoveryMessageFactory, evictionManager, _nodeStatsProvider, _configProvider, _logManager);

            var discoveryStorage = new DiscoveryStorage(_configProvider, _nodeFactory, _logManager, _perfService);

            _discoveryManager = new DiscoveryManager(nodeLifeCycleFactory, _nodeFactory, nodeTable, discoveryStorage, _configProvider, _logManager);

            var nodesLocator = new NodesLocator(nodeTable, _discoveryManager, _configProvider, _logManager);

            _discoveryApp = new DiscoveryApp(nodesLocator, _discoveryManager, _nodeFactory, nodeTable, _messageSerializationService, _cryptoRandom, discoveryStorage, _configProvider, _logManager);
            _discoveryApp.Start(_privateKey.PublicKey);

            _logger.Info("Discovery initialization completed");

            return(Task.CompletedTask);
        }
Beispiel #9
0
        private void InitDiscovery()
        {
            _configProvider.GetConfig <INetworkConfig>().MasterPort = _initConfig.DiscoveryPort;

            var privateKeyProvider      = new PrivateKeyProvider(_privateKey);
            var discoveryMessageFactory = new DiscoveryMessageFactory(_configProvider);
            var nodeIdResolver          = new NodeIdResolver(_signer);

            IDiscoveryMsgSerializersProvider msgSerializersProvider = new DiscoveryMsgSerializersProvider(
                _messageSerializationService,
                _signer,
                privateKeyProvider,
                discoveryMessageFactory,
                nodeIdResolver,
                _nodeFactory);

            msgSerializersProvider.RegisterDiscoverySerializers();

            var nodeDistanceCalculator = new NodeDistanceCalculator(_configProvider);

            var nodeTable = new NodeTable(
                _nodeFactory,
                _keyStore,
                nodeDistanceCalculator,
                _configProvider,
                _logManager);

            var evictionManager = new EvictionManager(
                nodeTable,
                _logManager);

            var nodeLifeCycleFactory = new NodeLifecycleManagerFactory(
                _nodeFactory,
                nodeTable,
                discoveryMessageFactory,
                evictionManager,
                _nodeStatsProvider,
                _configProvider,
                _logManager);

            var discoveryStorage = new NetworkStorage(
                DiscoveryNodesDbPath,
                _configProvider.GetConfig <INetworkConfig>(),
                _logManager,
                _perfService);

            _discoveryManager = new DiscoveryManager(
                nodeLifeCycleFactory,
                _nodeFactory,
                nodeTable,
                discoveryStorage,
                _configProvider,
                _logManager);

            var nodesLocator = new NodesLocator(
                nodeTable,
                _discoveryManager,
                _configProvider,
                _logManager);

            _discoveryApp = new DiscoveryApp(
                nodesLocator,
                _discoveryManager,
                _nodeFactory,
                nodeTable,
                _messageSerializationService,
                _cryptoRandom,
                discoveryStorage,
                _configProvider,
                _logManager, _perfService);

            _discoveryApp.Initialize(_privateKey.PublicKey);
        }
 public PrivateKeyBuilder()
 {
     TestObject = new PrivateKeyProvider(CryptoRandom).PrivateKey;
 }