public static KeyValuePair <BaseTransaction, int> Deserialize(IEnumerable <byte> payload)
        {
            int currentStartIndex    = 0;
            var version              = BitConverter.ToUInt32(payload.Take(4).ToArray(), 0);
            var category             = (TransactionCategories)payload.ElementAt(4);
            var transactionType      = (TransactionTypes)payload.ElementAt(5);
            BcBaseTransaction result = null;

            switch (transactionType)
            {
            case TransactionTypes.Coinbase:
                result = new CoinbaseTransaction();
                break;

            case TransactionTypes.NoneCoinbase:
                result = new NoneCoinbaseTransaction();
                break;
            }

            result.Version    = version;
            result.Category   = category;
            currentStartIndex = 6;
            var transactionInCompactSize = CompactSize.Deserialize(payload.Skip(currentStartIndex).ToArray());

            currentStartIndex += transactionInCompactSize.Value;
            if (transactionInCompactSize.Key.Size > 0)
            {
                var kvp = result.DeserializeInputs(payload.Skip(currentStartIndex), (int)transactionInCompactSize.Key.Size);
                result.TransactionIn = kvp.Key;
                currentStartIndex   += kvp.Value;
            }

            var transactionOutputCompactSize = CompactSize.Deserialize(payload.Skip(currentStartIndex).ToArray());

            currentStartIndex += transactionOutputCompactSize.Value;
            if (transactionOutputCompactSize.Key.Size > 0)
            {
                for (var i = 0; i < (int)transactionOutputCompactSize.Key.Size; i++)
                {
                    var kvp = result.DeserializeOutput(payload.Skip(currentStartIndex).ToArray());
                    result.TransactionOut.Add(kvp.Key);
                    currentStartIndex += kvp.Value;
                }
            }

            result.LockTime    = BitConverter.ToUInt32(payload.Skip(currentStartIndex).Take(4).ToArray(), 0);
            currentStartIndex += 4;
            return(new KeyValuePair <BaseTransaction, int>(result, currentStartIndex));
        }
Exemplo n.º 2
0
        public static KeyValuePair <BaseTransaction, int> Deserialize(IEnumerable <byte> payload)
        {
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            var type = (TransactionCategories)payload.ElementAt(4);

            switch (type)
            {
            case TransactionCategories.Monetary:
                return(BcBaseTransaction.Deserialize(payload));

            case TransactionCategories.SmartContract:
                return(SmartContractTransaction.Deserialize(payload));
            }

            return(default(KeyValuePair <BaseTransaction, int>));
        }