Example #1
0
 public static void EncodeMessage(BinaryWriter writer, Message message)
 {
     writer.WriteUInt32(message.Magic);
     writer.WriteFixedString(12, message.Command);
     writer.WriteUInt32(message.PayloadSize);
     writer.WriteUInt32(message.PayloadChecksum);
     writer.WriteBytes(message.PayloadSize.ToIntChecked(), message.Payload.ToArray());
 }
Example #2
0
 public static void EncodeNetworkAddress(BinaryWriter writer, NetworkAddress networkAddress)
 {
     writer.WriteUInt64(networkAddress.Services);
     writer.WriteBytes(16, networkAddress.IPv6Address.ToArray());
     writer.WriteUInt16BE(networkAddress.Port);
 }
Example #3
0
        public static void EncodeTotalWork(BinaryWriter writer, BigInteger totalWork)
        {
            if (totalWork < 0)
                throw new ArgumentOutOfRangeException();

            var totalWorkBytesLittleEndian = totalWork.ToByteArray();
            if (totalWorkBytesLittleEndian.Length > 64)
                throw new ArgumentOutOfRangeException();

            var totalWorkBytesLittleEndian64 = new byte[64];
            Buffer.BlockCopy(totalWorkBytesLittleEndian, 0, totalWorkBytesLittleEndian64, 0, totalWorkBytesLittleEndian.Length);

            var totalWorkBytesBigEndian = totalWorkBytesLittleEndian64.Reverse().ToArray();

            writer.WriteBytes(totalWorkBytesBigEndian);
            Debug.Assert(new BigInteger(totalWorkBytesLittleEndian64) == totalWork);
        }
Example #4
0
        private int PrepareSaveData(byte[] destBuf)
        {
            using (var stream = new MemoryStream(destBuf))
            {
                using (var bw = new BinaryWriter(stream))
                {
                    bw.BaseStream.Seek(4, SeekOrigin.Begin);
                    bw.WriteUInt32(SaveFileRevision);
                    bw.WriteUInt32((uint) SystemVars.Instance.GameVersion.Version.Minor);

                    bw.WriteUInt16(_skySound.SaveSounds[0]);
                    bw.WriteUInt16(_skySound.SaveSounds[1]);

                    bw.WriteUInt32(_skyMusic.CurrentMusic);
                    bw.WriteUInt32(_savedCharSet);
                    bw.WriteUInt32(_savedMouse);
                    bw.WriteUInt32(SystemVars.Instance.CurrentPalette);
                    for (var cnt = 0; cnt < Logic.NumSkyScriptVars; cnt++)
                        bw.WriteUInt32(_skyLogic.ScriptVariables[cnt]);
                    var loadedFilesList = _skyDisk.LoadedFilesList;

                    for (var cnt = 0; cnt < 60; cnt++)
                        bw.WriteUInt32(loadedFilesList[cnt]);

                    for (var cnt = 0; cnt < _skyCompact.SaveIds.Length; cnt++)
                    {
                        var rawCpt = _skyCompact.FetchCptRaw(_skyCompact.SaveIds[cnt]);
                        bw.WriteBytes(rawCpt, rawCpt.Length);
                    }

                    var length = bw.BaseStream.Position;
                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    bw.WriteUInt32((uint) length);
                    return (int) length;
                }
            }
        }
Example #5
0
 public static void EncodeBlockTxNode(BinaryWriter writer, BlockTxNode blockTx)
 {
     writer.WriteInt32(blockTx.Index);
     writer.WriteInt32(blockTx.Depth);
     writer.WriteUInt256(blockTx.Hash);
     writer.WriteBool(blockTx.Pruned);
     if (!blockTx.Pruned)
         writer.WriteBytes(blockTx.TxBytes.ToArray());
 }
Example #6
0
 public static void EncodeBlock(BinaryWriter writer, Block block)
 {
     EncodeBlockHeader(writer, block.Header);
     writer.WriteList(block.BlockTxes, tx => writer.WriteBytes(tx.TxBytes.ToArray()));
 }
Example #7
0
        public byte[] TxSignature(byte[] scriptPubKey, Transaction tx, int inputIndex, byte hashType)
        {
            ///TODO
            Debug.Assert(inputIndex < tx.Inputs.Length);

            // Blank out other inputs' signatures
            var empty = ImmutableArray.Create<byte>();
            var newInputs = ImmutableArray.CreateBuilder<TxInput>(tx.Inputs.Length);
            for (var i = 0; i < tx.Inputs.Length; i++)
            {
                var oldInput = tx.Inputs[i];
                var newInput = oldInput.With(scriptSignature: i == inputIndex ? scriptPubKey.ToImmutableArray() : empty);
                newInputs.Add(newInput);
            }

            //// Blank out some of the outputs
            //if ((hashType & 0x1F) == (int)ScriptHashType.SIGHASH_NONE)
            //{
            //    //TODO
            //    Debug.Assert(false);

            //    // Wildcard payee

            //    // Let the others update at will
            //}
            //else if ((hashType & 0x1F) == (int)ScriptHashType.SIGHASH_SINGLE)
            //{
            //    //TODO
            //    Debug.Assert(false);

            //    // Only lock-in the txout payee at same index as txin

            //    // Let the others update at will
            //}

            //// Blank out other inputs completely, not recommended for open transactions
            //if ((hashType & 0x80) == (int)ScriptHashType.SIGHASH_ANYONECANPAY)
            //{
            //    //TODO
            //    Debug.Assert(false);
            //}

            // create simplified transaction
            var newTx = tx.With(Inputs: newInputs.ToImmutable());

            // return wire-encoded simplified transaction with the 4-byte hashType tacked onto the end
            using (var stream = new MemoryStream())
            using (var writer = new BinaryWriter(stream))
            {
                writer.WriteBytes(DataEncoder.EncodeTransaction(newTx));
                writer.WriteUInt32(hashType);

                return stream.ToArray();
            }
        }
Example #8
0
 public static void EncodeNetworkAddress(Stream stream, NetworkAddress networkAddress)
 {
     using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true))
     {
         writer.Write8Bytes(networkAddress.Services);
         writer.WriteBytes(16, networkAddress.IPv6Address.ToArray());
         writer.Write2BytesBE(networkAddress.Port);
     }
 }
Example #9
0
 public static void EncodeMessage(Stream stream, Message message)
 {
     using (var writer = new BinaryWriter(stream, Encoding.ASCII, leaveOpen: true))
     {
         writer.Write4Bytes(message.Magic);
         writer.WriteFixedString(12, message.Command);
         writer.Write4Bytes(message.PayloadSize);
         writer.Write4Bytes(message.PayloadChecksum);
         writer.WriteBytes(message.PayloadSize.ToIntChecked(), message.Payload.ToArray());
     }
 }