public byte[] ToArray() { //ew var tis = TxIn.Sum(a => a.Size); var tos = TxOut.Sum(a => a.Size); var ret = new byte[8 + tis + tos + TxInCount.Size + TxOutCount.Size]; var v = BitConverter.GetBytes(Version); Buffer.BlockCopy(v, 0, ret, 0, v.Length); var ti = TxInCount.ToArray(); Buffer.BlockCopy(ti, 0, ret, 4, ti.Length); var txInOffset = 4 + ti.Length; for (var x = 0; x < TxInCount; x++) { var tx = TxIn[x].ToArray(); Buffer.BlockCopy(tx, 0, ret, txInOffset, tx.Length); txInOffset += tx.Length; } var to = TxOutCount.ToArray(); Buffer.BlockCopy(to, 0, ret, txInOffset, to.Length); var txOutOffset = txInOffset + to.Length; for (var x = 0; x < TxOutCount; x++) { var tx = TxOut[x].ToArray(); Buffer.BlockCopy(tx, 0, ret, txOutOffset, tx.Length); txOutOffset += tx.Length; } var lt = BitConverter.GetBytes(LockTime); Buffer.BlockCopy(lt, 0, ret, txOutOffset, lt.Length); return(ret); }
public byte[] ToArray() { Flags |= AllowWitness && HasWitness() ? (byte)1 : (byte)0; var woffset = 0; var ret = new byte[Size]; ret.CopyAndIncr(BitConverter.GetBytes(Version), ref woffset); //use extended block format if (Flags != 0) { ret.CopyAndIncr(new byte[] { 0x00, Flags }, ref woffset); } ret.CopyAndIncr(TxInCount.ToArray(), ref woffset); foreach (var tin in TxIn) { ret.CopyAndIncr(tin.ToArray(), ref woffset); } ret.CopyAndIncr(TxOutCount.ToArray(), ref woffset); foreach (var tout in TxOut) { ret.CopyAndIncr(tout.ToArray(), ref woffset); } //serialize witness scripts if ((Flags & 1) == 1 && AllowWitness) { foreach (var tin in TxIn) { var s = tin.WitnessScripts ?? new WitnessScript(); ret.CopyAndIncr(s.ToArray(), ref woffset); } } ret.CopyAndIncr(BitConverter.GetBytes(LockTime), ref woffset); return(ret); }