Example #1
0
            private void SerializeTxn(BitcoinStream stream, bool witSupported)
            {
                byte flags   = 0;
                var  version = (witSupported && (this.Inputs.Count == 0 && this.Outputs.Count > 0)) ? this.Version | NoDummyInput : this.Version;

                stream.ReadWrite(ref version);

                // POS Timestamp
                var time = this.Time;

                stream.ReadWrite(ref time);

                if (witSupported)
                {
                    /* Check whether witnesses need to be serialized. */
                    if (HasWitness)
                    {
                        flags |= 1;
                    }
                }
                if (flags != 0)
                {
                    /* Use extended format in case witnesses are to be serialized. */
                    TxInList vinDummy = null;
                    stream.ReadWrite(ref vinDummy);
                    stream.ReadWrite(ref flags);
                }
                TxInList vin = this.Inputs;

                stream.ReadWrite(ref vin);
                vin.Transaction = this;
                TxOutList vout = this.Outputs;

                stream.ReadWrite(ref vout);
                vout.Transaction = this;
                if ((flags & 1) != 0)
                {
                    BitcoinplusWitness wit = new BitcoinplusWitness(this.Inputs);
                    wit.ReadWrite(stream);
                }
                LockTime lockTime = this.LockTime;

                stream.ReadWriteStruct(ref lockTime);
            }
Example #2
0
            private void DeserializeTxn(BitcoinStream stream, bool witSupported)
            {
                byte flags = 0;

                UInt32 nVersionTemp = 0;

                stream.ReadWrite(ref nVersionTemp);

                // POS time stamp
                uint nTimeTemp = 0;

                stream.ReadWrite(ref nTimeTemp);

                TxInList  vinTemp  = null;
                TxOutList voutTemp = null;

                /* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
                stream.ReadWrite(ref vinTemp);
                vinTemp.Transaction = this;

                var hasNoDummy = (nVersionTemp & NoDummyInput) != 0 && vinTemp.Count == 0;

                if (witSupported && hasNoDummy)
                {
                    nVersionTemp = nVersionTemp & ~NoDummyInput;
                }

                if (vinTemp.Count == 0 && witSupported && !hasNoDummy)
                {
                    /* We read a dummy or an empty vin. */
                    stream.ReadWrite(ref flags);
                    if (flags != 0)
                    {
                        /* Assume we read a dummy and a flag. */
                        stream.ReadWrite(ref vinTemp);
                        vinTemp.Transaction = this;
                        stream.ReadWrite(ref voutTemp);
                        voutTemp.Transaction = this;
                    }
                    else
                    {
                        /* Assume read a transaction without output. */
                        voutTemp             = new TxOutList();
                        voutTemp.Transaction = this;
                    }
                }
                else
                {
                    /* We read a non-empty vin. Assume a normal vout follows. */
                    stream.ReadWrite(ref voutTemp);
                    voutTemp.Transaction = this;
                }
                if (((flags & 1) != 0) && witSupported)
                {
                    /* The witness flag is present, and we support witnesses. */
                    flags ^= 1;
                    BitcoinplusWitness wit = new BitcoinplusWitness(vinTemp);
                    wit.ReadWrite(stream);
                }
                if (flags != 0)
                {
                    /* Unknown flag in the serialization */
                    throw new FormatException("Unknown transaction optional data");
                }
                LockTime lockTimeTemp = 0;

                stream.ReadWriteStruct(ref lockTimeTemp);

                this.Version = nVersionTemp;
                this.Time    = nTimeTemp; // POS Timestamp
                vinTemp.ForEach(i => this.Inputs.Add(i));
                voutTemp.ForEach(i => this.Outputs.Add(i));
                this.LockTime = lockTimeTemp;
            }