/** * Novacoin classes library * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com) * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ using System; using System.Text; using System.Collections.Generic; namespace Novacoin { /// /// Transaction output. /// public class CTxOut { /// /// Input value. /// public long nValue = -1; /// /// Second half of script which contains spending instructions. /// public CScript scriptPubKey; /// /// Initialize new CTxOut instance as a copy of another instance. /// /// CTxOut instance. public CTxOut(CTxOut o) { nValue = o.nValue; scriptPubKey = o.scriptPubKey; } /// /// Initialize an empty instance of CTxOut class /// public CTxOut() { SetEmpty(); } /// /// Read vout list from byte sequence. /// /// Reference to byte sequence /// Outputs array public static CTxOut[] ReadTxOutList(ref ByteQueue wBytes) { int nOutputs = (int)wBytes.GetVarInt(); var vout =new CTxOut[nOutputs]; for (int nIndex = 0; nIndex < nOutputs; nIndex++) { // Fill outputs array vout[nIndex] = new CTxOut(); vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.Get(8), 0); int nScriptPKLen = (int)wBytes.GetVarInt(); vout[nIndex].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen)); } return vout; } /// /// Get raw bytes representation of our output. /// /// Byte sequence. public IList Bytes { get { var resultBytes = new List(); resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value var s = scriptPubKey.Bytes; resultBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptPubKey length resultBytes.AddRange(s); // scriptPubKey return resultBytes; } } /// /// Null prevouts have -1 value /// public void SetNull() { nValue = -1; scriptPubKey = new CScript(); } /// /// Empty outputs have zero value and empty scriptPubKey /// public void SetEmpty() { nValue = 0; scriptPubKey = new CScript(); } public bool IsNull { get { return (nValue == -1); } } public bool IsEmpty { get { return nValue == 0 && scriptPubKey.IsNull; } } public override string ToString () { var sb = new StringBuilder (); sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString()); return sb.ToString (); } } }