Пример #1
0
        public Transaction(NBitcoin.Block parent, NBitcoin.Transaction tx)
        {
            this.Id      = Transaction.ToId(tx.GetHash());
            this.BlockId = Block.ToId(parent.GetHash());
            this.Size    = tx.GetVirtualSize();
            this.Version = tx.Version;

            this.LockType = tx.GetLockType();
            this.LockTime = tx.LockTime.Value;

            this.IsCoinBase = tx.IsCoinBase;
            this.HasWitness = tx.HasWitness;

            this.InputCount        = tx.Inputs.Count;
            this.InputTransactions = new InTransaction[InputCount];

            this.OutputCount        = tx.Outputs.Count;
            this.OutputTransactions = new OutTransaction[OutputCount];
            this.OutputBtc          = tx.TotalOut.ToDecimal(MoneyUnit.BTC);

            for (int i = 0; i < InputCount; i++)
            {
                var inTx = tx.Inputs[i];

                // Is this a Coinbase?
                string payer = null;
                if (inTx.PrevOut.IsNull)
                {
                    // Yes.
                    payer = inTx.PrevOut.Hash.ToString();
                }

                InputTransactions[i] = new InTransaction
                {
                    Index    = (uint)i,
                    Outpoint = new Outpoint {
                        Index = inTx.PrevOut.N, TxId = Transaction.ToId(inTx.PrevOut.Hash.ToString())
                    },
                    Payer     = payer,
                    PayerType = AddressType.None,
                    Value     = 0
                };
            }

            for (int i = 0; i < OutputCount; i++)
            {
                var outTx = tx.Outputs[i];

                AddressType type = outTx.GetAddressType();

                string payee;
                if (type == AddressType.PublicKey)
                {
                    payee = outTx.ScriptPubKey.GetDestinationPublicKeys().FirstOrDefault()?.ToString();
                }
                else if (type == AddressType.ScriptHash)
                {
                    payee = outTx.ScriptPubKey.GetScriptAddress(Network.Main).Hash.ToString();
                }
                else if (type == AddressType.MultiplePublicKeyHashes)
                {
                    payee = outTx.ScriptPubKey.GetDestinationPublicKeys()[0].GetAddress(Network.Main).ToString();
                }
                else
                {
                    payee = outTx.ScriptPubKey?.GetDestinationAddress(Network.Main)?.ToString();
                }

                OutputTransactions[i] = new OutTransaction
                {
                    Index     = (uint)i,
                    Payee     = payee,
                    PayeeType = type,
                    Value     = outTx.Value.ToDecimal(MoneyUnit.BTC)
                };
            }
        }