Exemplo n.º 1
0
 public void TestShorts()
 {
     var a = new VarInt(64000);
     Assert.AreEqual(3, a.SizeInBytes);
     Assert.AreEqual(3, a.Encode().Length);
     Assert.AreEqual(64000, new VarInt(a.Encode(), 0).Value);
 }
Exemplo n.º 2
0
 public void TestBytes()
 {
     var a = new VarInt(10);
     Assert.AreEqual(1, a.SizeInBytes);
     Assert.AreEqual(1, a.Encode().Length);
     Assert.AreEqual(10, new VarInt(a.Encode(), 0).Value);
 }
Exemplo n.º 3
0
 public void TestLong()
 {
     var a = new VarInt(0xCAFEBABEDEADBEEF);
     Assert.AreEqual(9, a.SizeInBytes);
     Assert.AreEqual(9, a.Encode().Length);
     var bytes = a.Encode();
     Assert.AreEqual(0xCAFEBABEDEADBEEF, new VarInt(bytes, 0).Value);
 }
Exemplo n.º 4
0
 public void TestInts()
 {
     var a = new VarInt(0xAABBCCDD);
     Assert.AreEqual(5, a.SizeInBytes);
     Assert.AreEqual(5, a.Encode().Length);
     var bytes = a.Encode();
     Assert.AreEqual(0xAABBCCDD, new VarInt(bytes, 0).Value);
 }
Exemplo n.º 5
0
        public void TestShorts()
        {
            var a = new VarInt(64000);

            Assert.AreEqual(3, a.SizeInBytes);
            Assert.AreEqual(3, a.Encode().Length);
            Assert.AreEqual(64000UL, new VarInt(a.Encode(), 0).Value);
        }
Exemplo n.º 6
0
        public void TestBytes()
        {
            var a = new VarInt(10);

            Assert.AreEqual(1, a.SizeInBytes);
            Assert.AreEqual(1, a.Encode().Length);
            Assert.AreEqual(10UL, new VarInt(a.Encode(), 0).Value);
        }
Exemplo n.º 7
0
        public void TestLong()
        {
            var a = new VarInt(0xCAFEBABEDEADBEEF);

            Assert.AreEqual(9, a.SizeInBytes);
            Assert.AreEqual(9, a.Encode().Length);
            var bytes = a.Encode();

            Assert.AreEqual(0xCAFEBABEDEADBEEF, new VarInt(bytes, 0).Value);
        }
Exemplo n.º 8
0
        public void TestInts()
        {
            var a = new VarInt(0xAABBCCDD);

            Assert.AreEqual(5, a.SizeInBytes);
            Assert.AreEqual(5, a.Encode().Length);
            var bytes = a.Encode();

            Assert.AreEqual(0xAABBCCDD, new VarInt(bytes, 0).Value);
        }
Exemplo n.º 9
0
        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();
        }
Exemplo n.º 10
0
        public void SendPacket(ClientPacket packet)
        {
            if (UsesCompression)
            {
                throw new NotYetImplemented("Compression isn't implemented yet");
            }
            else
            {
                var packetId = VarInt.Encode(packet.Id);
                var encoded  = packet.Encode().Bytes;
                var length   = VarInt.Encode(packetId.Length + encoded.Length);
                var data     = length.Concat(packetId).Concat(encoded).ToArray();
                Log.Debug("[{SessionId}] SEND: {Bytes}", Id, data.GetPrintableBytes());
                Send(data);
            }

            Type packetReceivedType = typeof(PacketSent <>).MakeGenericType(packet.GetType());

            object[] args = { packet, this };
            TridentMc.Instance.EventManager.FireEvent((IEvent)Activator.CreateInstance(packetReceivedType, args));
        }
Exemplo n.º 11
0
        public void EncodeTest()
        {
            var vi     = new VarInt(0);
            var result = vi.Encode().ToArray();

            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(0, result[0]);

            vi     = new VarInt(1);
            result = vi.Encode().ToArray();
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(1, result[0]);

            vi     = new VarInt(0x7f);
            result = vi.Encode().ToArray();
            Assert.AreEqual(1, result.Length);
            Assert.AreEqual(0x7f, result[0]);

            vi     = new VarInt(0x80);
            result = vi.Encode().ToArray();
            Assert.AreEqual(2, result.Length);
            Assert.AreEqual(0xC2, result[0]);
            Assert.AreEqual(0x80, result[1]);

            vi     = new VarInt(0x7ff);
            result = vi.Encode().ToArray();
            Assert.AreEqual(2, result.Length);
            Assert.AreEqual(0xDF, result[0]);
            Assert.AreEqual(0xBF, result[1]);

            vi     = new VarInt(0x800);
            result = vi.Encode().ToArray();
            Assert.AreEqual(3, result.Length);
            Assert.AreEqual(0xE0, result[0]);
            Assert.AreEqual(0xA0, result[1]);
            Assert.AreEqual(0x80, result[2]);

            vi     = new VarInt(0xFFFF);
            result = vi.Encode().ToArray();
            Assert.AreEqual(3, result.Length);
            Assert.AreEqual(result[0], 0xEF);
            Assert.AreEqual(result[1], 0xBF);
            Assert.AreEqual(result[2], 0xBF);

            vi     = new VarInt(0x10000);
            result = vi.Encode().ToArray();
            Assert.AreEqual(4, result.Length);
            Assert.AreEqual(0xF0, result[0]);
            Assert.AreEqual(0x90, result[1]);
            Assert.AreEqual(0x80, result[2]);
            Assert.AreEqual(0x80, result[3]);

            vi     = new VarInt(0x1FFFFF);
            result = vi.Encode().ToArray();
            Assert.AreEqual(4, result.Length, 4);
            Assert.AreEqual(0xF7, result[0]);
            Assert.AreEqual(0xBF, result[1]);
            Assert.AreEqual(0xBF, result[2]);
            Assert.AreEqual(0xBF, result[3]);

            vi     = new VarInt(0x200000);
            result = vi.Encode().ToArray();
            Assert.AreEqual(5, result.Length);
            Assert.AreEqual(0xF8, result[0]);
            Assert.AreEqual(0x88, result[1]);
            Assert.AreEqual(0x80, result[2]);
            Assert.AreEqual(0x80, result[3]);
            Assert.AreEqual(0x80, result[4]);

            vi     = new VarInt(0x3FFFFFF);
            result = vi.Encode().ToArray();
            Assert.AreEqual(5, result.Length);
            Assert.AreEqual(0xFB, result[0]);
            Assert.AreEqual(0xBF, result[1]);
            Assert.AreEqual(0xBF, result[2]);
            Assert.AreEqual(0xBF, result[3]);
            Assert.AreEqual(0xBF, result[4]);

            vi     = new VarInt(0x4000000);
            result = vi.Encode().ToArray();
            Assert.AreEqual(6, result.Length);
            Assert.AreEqual(0xFC, result[0]);
            Assert.AreEqual(0x84, result[1]);
            Assert.AreEqual(0x80, result[2]);
            Assert.AreEqual(0x80, result[3]);
            Assert.AreEqual(0x80, result[4]);
            Assert.AreEqual(0x80, result[5]);

            vi     = new VarInt(0xFFFFFFFF);
            result = vi.Encode().ToArray();
            Assert.AreEqual(6, result.Length);
            Assert.AreEqual(0xFF, result[0]);
            Assert.AreEqual(0xBF, result[1]);
            Assert.AreEqual(0xBF, result[2]);
            Assert.AreEqual(0xBF, result[3]);
            Assert.AreEqual(0xBF, result[4]);
            Assert.AreEqual(0xBF, result[5]);
        }
Exemplo n.º 12
0
 public void WriteVarInt(int value)
 {
     Write(VarInt.Encode(value));
 }