/// <summary> /// Serialize the current instance as a byte[] that can be sent over the network. /// </summary> /// <param name="writer"></param> /// <param name="serializationType"> /// The serialization method used. /// This value will be encoded in the output of this method, /// so the SerializationType property of this instance may not match. /// </param> /// <returns></returns> /// <exception cref="InvalidDataException"></exception> private void Encode(BinaryWriter writer, TxSerializeType serializationType) { var serializedVersion = Version | ((uint)serializationType << 16); writer.Write(serializedVersion); switch (serializationType) { case TxSerializeType.NoWitness: EncodePrefix(writer); break; case TxSerializeType.OnlyWitness: EncodeWitness(writer); break; case TxSerializeType.WitnessSigning: EncodeWitnessSigning(writer); break; case TxSerializeType.WitnessValueSigning: EncodeWitnessValueSigning(writer); break; case TxSerializeType.Full: EncodePrefix(writer); EncodeWitness(writer); break; default: throw new InvalidEnumArgumentException($"unrecognized serialization type {serializationType}"); } }
/// <summary> /// Calculates the BLAKE256 hash of the current instance. Witness data is not serialized. /// </summary> /// <returns>The hash as a byte[] with length 32</returns> public byte[] GetHash(TxSerializeType serializationType = TxSerializeType.NoWitness) { byte[] bytes; using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { Encode(bw, serializationType); bw.Flush(); bytes = ms.ToArray(); } return(HashUtil.Blake256(bytes)); }
private TxIn ReadTxInPrefix(BinaryReader reader, TxSerializeType serializationType) { if (serializationType == TxSerializeType.OnlyWitness) { throw new WireException("tried to read a prefix input for a witness only tx"); } var prevOutPoint = ReadOutPoint(reader); var sequence = reader.ReadUInt32(); return(new TxIn { Sequence = sequence, PreviousOutPoint = prevOutPoint }); }