//Deserialize TxIn public static TxIn Deserialize(byte[] serializedTxIn) { int length = serializedTxIn.Length; int current = 0; byte[] bytes; //Check if the system uses little endian; //Reverse the array of bytes if it's little endian; bool reverse = BitConverter.IsLittleEndian; if (length < 4) { return(null); } bytes = Tx.SubArray(serializedTxIn, 0, 4); if (reverse) { Array.Reverse(bytes); } int totalLen = BitConverter.ToInt32(bytes, 0); current += 4; if (length != totalLen + 4) { return(null); } bytes = Tx.SubArray(serializedTxIn, current, 4); if (reverse) { Array.Reverse(bytes); } int hashLen = BitConverter.ToInt32(bytes, 0); current += 4; byte[] hash = Tx.SubArray(serializedTxIn, current, hashLen); current += hashLen; bytes = Tx.SubArray(serializedTxIn, current, 8); if (reverse) { Array.Reverse(bytes); } int index = (int)BitConverter.ToInt64(bytes, 0); current += 8; byte[] script = Tx.SubArray(serializedTxIn, current, length - current); return(new TxIn(hash, index, script)); }
//Deserialize the byte array of a list of TxIns; public static List <TxIn> DeserializeTxIns(byte[] serializedTxIns) { List <TxIn> TxIns = new List <TxIn>(); int length = 0; int inLen = 0; byte[] bytes; bool reverse = BitConverter.IsLittleEndian; while ((length = serializedTxIns.Length) > 0) { if (length < 4) { return(null); } bytes = Tx.SubArray(serializedTxIns, 0, 4); if (reverse) { Array.Reverse(bytes); } inLen = BitConverter.ToInt32(bytes, 0); if (length < 4 + inLen) { return(null); } TxIn txIn = Deserialize(Tx.SubArray(serializedTxIns, 0, 4 + inLen)); if (txIn == null) { return(null); } serializedTxIns = Tx.SubArray(serializedTxIns, 4 + inLen, length - 4 - inLen); TxIns.Add(txIn); } return(TxIns); }
//Deserialize the byte array of a list of TxOuts; public static List <TxOut> DeserializeTxOuts(byte[] serializedTxOuts) { List <TxOut> txOuts = new List <TxOut>(); int length; int outLen; byte[] bytes; bool reverse = BitConverter.IsLittleEndian; while ((length = serializedTxOuts.Length) > 0) { if (length < 4) { return(null); } bytes = Tx.SubArray(serializedTxOuts, 0, 4); if (reverse) { Array.Reverse(bytes); } outLen = BitConverter.ToInt32(bytes, 0); if (length < outLen + 4) { return(null); } TxOut txOut = Deserialize(Tx.SubArray(serializedTxOuts, 0, 4 + outLen)); if (txOut == null) { return(null); } serializedTxOuts = Tx.SubArray(serializedTxOuts, 4 + outLen, length - outLen - 4); txOuts.Add(txOut); } return(txOuts); }
//Deserialzie serialized Tx public static Tx Deserialize(byte[] serializedTx) { int current = 0; int length = serializedTx.Length; byte[] bytes; bool reverse = BitConverter.IsLittleEndian; if (length < 4) { return(null); } bytes = Tx.SubArray(serializedTx, 0, 4); if (reverse) { Array.Reverse(bytes); } int hashLen = BitConverter.ToInt32(bytes, 0); current += 4; if (length < current + hashLen) { return(null); } byte[] hash = SubArray(serializedTx, current, hashLen); current += hashLen; if (length < current + 4) { return(null); } bytes = Tx.SubArray(serializedTx, current, 4); if (reverse) { Array.Reverse(bytes); } int insLen = BitConverter.ToInt32(bytes, 0); current += 4; if (length < current + insLen) { return(null); } byte[] insBytes = SubArray(serializedTx, current, insLen); current += insLen; List <TxIn> txIns = TxIn.DeserializeTxIns(insBytes); if (txIns == null) { return(null); } if (length < current + 4) { return(null); } bytes = Tx.SubArray(serializedTx, current, 4); if (reverse) { Array.Reverse(bytes); } int outsLen = BitConverter.ToInt32(bytes, 0); current += 4; if (length < current + outsLen) { return(null); } byte[] outsBytes = SubArray(serializedTx, current, outsLen); List <TxOut> txOuts = TxOut.DeserializeTxOuts(outsBytes); if (txOuts == null) { return(null); } return(new Tx(txIns, txOuts)); }
//Deserialize SignedTX public static Tx DeserializeSignedTx(byte[] signedTx) { int length = signedTx.Length; int currentIndex = 0; byte[] bytes; bool reverse = BitConverter.IsLittleEndian; if (length < 4) { return(null); } bytes = Tx.SubArray(signedTx, 0, 4); if (reverse) { Array.Reverse(bytes); } int sigLen = BitConverter.ToInt32(bytes, 0); currentIndex += 4; if (length < currentIndex + sigLen) { return(null); } byte[] sigser = SubArray(signedTx, currentIndex, sigLen); currentIndex += sigLen; if (length < currentIndex + 4) { return(null); } bytes = Tx.SubArray(signedTx, currentIndex, 4); if (reverse) { Array.Reverse(bytes); } int pubLen = BitConverter.ToInt32(bytes, 0); currentIndex += 4; if (length < currentIndex + pubLen) { return(null); } byte[] pubKey = SubArray(signedTx, currentIndex, pubLen); currentIndex += pubLen; byte[] addr = Account.ToAddr(pubKey); byte[] txBytes = SubArray(signedTx, currentIndex, length - currentIndex); Tx tx = Deserialize(txBytes); tx.FromAddress = addr; tx.Hash = pubKey; return(tx); }
//Deserialize TxOut; public static TxOut Deserialize(byte[] serializedTxOut) { int length = serializedTxOut.Length; int current = 0; byte[] bytes; //Check if the system uses little endian; //Reverse the array of bytes if it's little endian; bool reverse = BitConverter.IsLittleEndian; if (length < 4) { return(null); } bytes = Tx.SubArray(serializedTxOut, 0, 4); if (reverse) { Array.Reverse(bytes); } int totalLen = BitConverter.ToInt32(bytes, 0); current += 4; if (length != totalLen + 4) { return(null); } bytes = Tx.SubArray(serializedTxOut, current, 8); if (reverse) { Array.Reverse(bytes); } int value = (int)BitConverter.ToInt64(bytes, 0); current += 8; if (value < 0) { return(null); } bytes = Tx.SubArray(serializedTxOut, current, 4); if (reverse) { Array.Reverse(bytes); } int scriptLen = BitConverter.ToInt32(bytes, 0); current += 4; byte[] script = Tx.SubArray(serializedTxOut, current, scriptLen); current += scriptLen; byte[] addr = Tx.SubArray(serializedTxOut, current, length - current); return(new TxOut(value, script, Convert.ToBase64String(addr))); }