Byteswap() public static method

public static Byteswap ( short value ) : short
value short
return short
        public void ComputeHash(bool useScrypt)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(mVersion);
            bw.Write(mPrevBlock);
            bw.Write(mMerkleRoot);
            bw.Write(mTimestamp);
            bw.Write(mDifficultyBits);
            bw.Write(mNOnce);
            byte[] data = ms.ToArray();
            bw.Close();

            if (useScrypt)
            {
                MinerLib_cs.Work work = new MinerLib_cs.Work();
                Buffer.BlockCopy(data, 0, work.data, 0, 80);
                for (int i = 0; i < 20; i++)
                {
                    work.data[i] = Utils.Byteswap(work.data[i]);
                }

                MinerLib_cs.Scrypt scrypt = new MinerLib_cs.Scrypt(work);
                mHash = scrypt.GetHash();
            }
            else
            {
                // SHA 256
                mHash = Utils.GenerateHash(data);
            }
        }
        public void NodeDiscovered(string node)
        {
            byte[] decoded = Utils.DecodeBase58StringChecked(node.Substring(1));
            if (decoded == null)
            {
                return;
            }

            MemoryStream ms = new MemoryStream(decoded);
            BinaryReader br = new BinaryReader(ms);

            byte[] addrBytes = br.ReadBytes(4);
            string address   = "";

            for (int i = 0; i < 4; i++)
            {
                //byte b = addrBytes[3 - i];
                byte b = addrBytes[i];
                address += b.ToString();
                if (i < 3)
                {
                    address += ".";
                }
            }
            ushort port = Utils.Byteswap(br.ReadUInt16());

            AddNode(address, port);
            br.Close();
        }
Beispiel #3
0
        void SendVersionPacket()
        {
            IPEndPoint remote = (IPEndPoint)mSocket.RemoteEndPoint;
            IPEndPoint local  = (IPEndPoint)mSocket.LocalEndPoint;

            // Send version packet
            MemoryStream stream = new MemoryStream();
            BinaryWriter w      = new BinaryWriter(stream);

            // -- PAYLOAD --
            // version
            w.Write(mProtocolVersion);

            // Services
            Int64 services = 0x0000000000000001;

            w.Write(services);

            // Timestamp
            UInt64 timestamp = Utils.UnixTime();

            w.Write(timestamp);

            // addr_recv
            w.Write(services);
            w.Write((UInt64)0);
            w.Write((ushort)0);
            w.Write((ushort)0xFFFF);
            byte[] remoteBytes = remote.Address.GetAddressBytes();
            w.Write(remoteBytes);
            w.Write(Utils.Byteswap(mPort));

            // addr_from
            w.Write(services);
            w.Write((UInt64)0);
            w.Write((ushort)0);
            w.Write((ushort)0xFFFF);
            byte[] localBytes = local.Address.GetAddressBytes();
            w.Write(localBytes);
            w.Write(Utils.Byteswap(mPort));

            // nonce
            w.Write((UInt64)0xC4ACFF3D04805523);

            // user_agent
            w.Write((byte)0xF);
            w.Write("/Satoshi:0.8.6/".ToArray());

            // start_height
            w.Write(mOwner.CurrentHeight);

            byte[] packetData = stream.ToArray();
            SendPacket("version", packetData);
            w.Close();
        }