/// <summary> /// Encrypt a packet according to wallet phase. /// </summary> /// <param name="packet"></param> /// <returns></returns> private string EncryptPacketWallet(string packet) { switch (WalletPhase) { case "": return(packet); case ClassWalletPhase.Restore: case ClassWalletPhase.Create: return(packet); case ClassWalletPhase.Login: return(packet); default: if (AesIvCertificate == null) { using (PasswordDeriveBytes password = new PasswordDeriveBytes(WalletAddress + WalletPassword + WalletKey + ClassConnectorSetting.NETWORK_GENESIS_KEY, ClassUtils.GetByteArrayFromString(ClassUtils.FromHex((WalletAddress + WalletPassword + WalletKey + ClassConnectorSetting.NETWORK_GENESIS_KEY).Substring(0, 8))))) { AesIvCertificate = password.GetBytes(ClassConnectorSetting.MAJOR_UPDATE_1_SECURITY_CERTIFICATE_SIZE / 8); AesSaltCertificate = password.GetBytes(16); } } return(ClassAlgo.GetEncryptedResult(ClassAlgoEnumeration.Rijndael, packet + "|" + ClassUtils.DateUnixTimeNowSecond(), ClassWalletNetworkSetting.KeySize, AesIvCertificate, AesSaltCertificate)); // AES } }
/// <summary> /// Send packet to seed node. /// </summary> /// <param name="packet"></param> /// <param name="certificate"></param> /// <param name="isSeedNode"></param> /// <param name="isEncrypted"></param> /// <returns></returns> public async Task <bool> SendPacketToSeedNodeAsync(string packet, string certificate, bool isSeedNode = false, bool isEncrypted = false) { if (!ReturnStatus()) { return(false); } try { using (var _connectorStream = new NetworkStream(_connector.Client)) { using (var bufferedNetworkStream = new BufferedStream(_connectorStream, ClassConnectorSetting.MaxNetworkPacketSize)) { // 10/08/2018 - MAJOR_UPDATE_1_SECURITY if (ClassConnectorSetting.MAJOR_UPDATE_1_SECURITY) // SSL Layer for Send packet. { if (isEncrypted) { if (AesIvCertificate == null) { using (PasswordDeriveBytes password = new PasswordDeriveBytes(certificate, ClassUtils.GetByteArrayFromString(ClassUtils.FromHex(certificate.Substring(0, 8))))) { AesIvCertificate = password.GetBytes(ClassConnectorSetting.MAJOR_UPDATE_1_SECURITY_CERTIFICATE_SIZE / 8); AesSaltCertificate = password.GetBytes(16); } } using (ClassSeedNodeConnectorObjectSendPacket packetObject = new ClassSeedNodeConnectorObjectSendPacket(ClassAlgo.GetEncryptedResult(ClassAlgoEnumeration.Rijndael, packet, ClassConnectorSetting.MAJOR_UPDATE_1_SECURITY_CERTIFICATE_SIZE, AesIvCertificate, AesSaltCertificate) + "*")) { await bufferedNetworkStream.WriteAsync(packetObject.packetByte, 0, packetObject.packetByte.Length); await bufferedNetworkStream.FlushAsync(); } } else { if (isSeedNode) { using (ClassSeedNodeConnectorObjectSendPacket packetObject = new ClassSeedNodeConnectorObjectSendPacket(packet + "*")) { await bufferedNetworkStream.WriteAsync(packetObject.packetByte, 0, packetObject.packetByte.Length); await bufferedNetworkStream.FlushAsync(); } } else { using (ClassSeedNodeConnectorObjectSendPacket packetObject = new ClassSeedNodeConnectorObjectSendPacket(packet)) { await bufferedNetworkStream.WriteAsync(packetObject.packetByte, 0, packetObject.packetByte.Length); await bufferedNetworkStream.FlushAsync(); } } } } } } } catch (Exception error) { #if DEBUG Console.WriteLine("Error to send packet on seed node: " + error.Message); #endif _isConnected = false; return(false); } return(true); }