protected override void OnReceived(byte[] buffer, long offset, long size) { byte[] remaining = new byte[size]; Array.Copy(buffer, remaining, size); Log.Debug("[{SessionId}] RECV: {Bytes}", Id, remaining.GetPrintableBytes()); // Legacy packet if (remaining[0] == 0xfe) { Disconnect(); return; } while (true) { int packetLength = VarInt.Decode(remaining, out remaining); byte[] packet = new byte[packetLength]; Array.Copy(remaining, packet, packetLength); PacketBuffer.Enqueue(packet); remaining = remaining.Skip(packetLength).ToArray(); if (remaining.Length == 0) { break; } } }
public void ReadStream(BinaryReader br) { magicId = br.ReadUInt32(); size = br.ReadUInt32(); version = br.ReadUInt32(); byte[] hexPrevBlockHash = br.ReadBytes(32); prevBlockHash = new Hash(hexPrevBlockHash); byte[] hexMerkleRootHash = br.ReadBytes(32); merkleRootHash = new Hash(hexMerkleRootHash); timestamp = br.ReadUInt32(); creationTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc). AddSeconds(timestamp).ToLocalTime(); targetDifficulty = br.ReadUInt32(); nonce = br.ReadUInt32(); blockHash = GetBlockHash(); VarInt.Decode(br, out firstVarIntByteNTxns, out nTxns); txns = Util.InstantiateArrayOf <Transaction>(nTxns); for (UInt64 i = 0; i < nTxns; ++i) { txns[i].ReadStream(br); } }
public void ReadStream(BinaryReader br) { VarInt.Decode(br, out firstVarIntByteNItems, out nStackItems); stackItems = Util.InstantiateArrayOf<StackItem>(nStackItems); for (int i = 0; i < stackItems.Length; ++i) { stackItems[i].ReadStream(br); } }
public void ReadStream(BinaryReader br) { VarInt.Decode(br, out firstVarIntByteLength, out itemLength); item = Util.InstantiateArrayOf <byte>(itemLength); for (int i = 0; i < item.Length; ++i) { item[i] = br.ReadByte(); } }
private void NetworkLoop() { _server.Start(); while (!TridentMc.Instance.State.IsShuttingDown) { Thread.Sleep(1); foreach (var session in Sessions) { while (session.PacketBuffer.Count > 0) { var packet = session.PacketBuffer.Dequeue(); if (session.UsesCompression) { throw new NotYetImplemented("Compression isn't implemented yet"); } else { var packetId = VarInt.Decode(packet, out packet); Log.Debug("[{SessionId}] Packet {PacketId} with state being {State}", session.Id, packetId, session.ConnectionState); var serverPacket = _packetManager.CreateServerPacket(session.ConnectionState, packetId, packet); if (serverPacket == null) { Log.Debug("[{SessionId}] -> Unknown packet", session.Id); session.Disconnect(); break; } Type packetReceivedType = typeof(PacketReceived <>).MakeGenericType(serverPacket.GetType()); object[] args = { serverPacket, session }; TridentMc.Instance.EventManager.FireEvent((IEvent)Activator.CreateInstance(packetReceivedType, args)); if (serverPacket is IChangesState stateChangePacket) { Log.Debug("[{SessionId}] Changing state to {NewState}", session.Id, stateChangePacket.NextState); session.ConnectionState = stateChangePacket.NextState; } serverPacket.Handle(session); } } } } }
private static void Main(string[] args) { //Key Generation const string privKeyHex = "E9873D79C6D87DC0FB6A5778633389F4453213303DA61F20BD67FC233AA33262"; BigInteger privateKey = Hex.HexToBigInteger(privKeyHex); ECPoint publicKey = Secp256k1.G.Multiply(privateKey); string bitcoinAddressUncompressed = publicKey.GetBitcoinAddress(false); string bitcoinAddressCompressed = publicKey.GetBitcoinAddress(compressed: true); Console.WriteLine("PrivateKey (Hex): {0}", privateKey.ToHex()); Console.WriteLine("Address (Uncompressed): {0}", bitcoinAddressUncompressed); Console.WriteLine("Address (Compressed): {0}", bitcoinAddressCompressed); uint value = 268435263; var varInt = new VarInt(value); //create VarInt from Integer Console.WriteLine("VarInt Internal Value: {0}", varInt.Value); Console.WriteLine("VarInt (hex): 0x{0:X}", varInt.Value); value = value ^ 2; byte[] varIntBytes = VarInt.Encode(value); Console.WriteLine("VarInt Static Encode & Decode test for value: {0}", value); Console.WriteLine("VarInt Encoded (hex): {0}", varIntBytes.ToHex()); Console.WriteLine("Value Decoded: {0}", VarInt.Decode(varIntBytes)); // encryption ECEncryption encryption = new ECEncryption(); const string message = "This is my encrypted message"; byte[] encrypted = encryption.Encrypt(publicKey, message); byte[] decrypted = encryption.Decrypt(privateKey, encrypted); string decryptedMessage = Encoding.UTF8.GetString(decrypted); // signing MessageSignerVerifier messageSigner = new MessageSignerVerifier(); SignedMessage signedMessage = messageSigner.Sign(privateKey, "Test Message to sign, you can verify this on http://brainwallet.org/#verify"); bool verified = messageSigner.Verify(signedMessage); Console.WriteLine("Press Any Key ..."); Console.ReadKey(); }
public void ReadStream(BinaryReader br) { versionNum = br.ReadUInt32(); isSegwit = CheckForSegwitFlag(br); VarInt.Decode(br, out firstVarIntByteNInputs, out nInputs); inputs = Util.InstantiateArrayOf <InputTxn>(nInputs); for (int i = 0; i < inputs.Length; ++i) { inputs[i].ReadStream(br); } VarInt.Decode(br, out firstVarIntByteNOutputs, out nOutputs); outputs = Util.InstantiateArrayOf <OutputTxn>(nOutputs); for (int i = 0; i < outputs.Length; ++i) { outputs[i].ReadStream(br); } if (isSegwit) { witnesses = Util.InstantiateArrayOf <Witness>(nInputs); for (int i = 0; i < witnesses.Length; ++i) { witnesses[i].ReadStream(br); } } lockTime = br.ReadUInt32(); foreach (var output in outputs) { output.destinationAddress = Script.ExtractDestination(output); } txId = GetTxId(); }
public void ReadStream(BinaryReader br) { byte[] hexTxnHash = new byte[32]; hexTxnHash = br.ReadBytes(32); txnHash = new Hash(hexTxnHash); vout = br.ReadUInt32(); if (vout == 0xFFFFFFFF) { vout = 0x11111111; } VarInt.Decode(br, out firstVarIntByteScriptLength, out inputScriptLength); inputScript = Util.InstantiateArrayOf <byte>(inputScriptLength); for (int i = 0; i < inputScript.Length; ++i) { inputScript[i] = br.ReadByte(); } sequenceNum = br.ReadUInt32(); }
public int ReadVarint() { return(VarInt.Decode(_buffer, out _buffer)); }
public void ReadStream(BinaryReader br) { value = br.ReadUInt64(); VarInt.Decode(br, out firstVarIntByteScriptLength, out outputScriptLength); scriptPubKey = new ScriptPubKey(br, outputScriptLength); }