Пример #1
0
        public IRpcMethodResult GetBlock(string blockHash, int format = 0)
        {
            try
            {
                var blockComponent = new BlockComponent();

                var block = blockComponent.GetBlockMsgByHash(blockHash);

                if (block != null)
                {
                    if (format == 0)
                    {
                        var bytes  = block.Serialize();
                        var result = Base16.Encode(bytes);
                        return(Ok(result));
                    }
                    else
                    {
                        return(Ok(block));
                    }
                }
                else
                {
                    return(Ok());
                }
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Пример #2
0
        public IRpcMethodResult GetTxOut(string txid, int vount, bool unconfirmed = false)
        {
            try
            {
                GetTxOutOM result = null;

                var            txComponent    = new TransactionComponent();
                var            blockComponent = new BlockComponent();
                string         blockHash      = null;
                TransactionMsg tx             = txComponent.GetTransactionMsgFromDB(txid, out blockHash);

                if (tx == null && unconfirmed)
                {
                    tx = txComponent.GetTransactionMsgFromPool(txid);
                }


                if (tx != null && tx.OutputCount > vount)
                {
                    var  output        = tx.Outputs[vount];
                    long confirmations = 0;

                    if (!string.IsNullOrWhiteSpace(blockHash))
                    {
                        var block = blockComponent.GetBlockMsgByHash(blockHash);

                        if (block != null)
                        {
                            var latestHeight = blockComponent.GetLatestHeight();

                            if (latestHeight > block.Header.Height)
                            {
                                confirmations = latestHeight - block.Header.Height;
                            }
                        }
                    }

                    result               = new GetTxOutOM();
                    result.bestblock     = blockHash;
                    result.confirmations = confirmations;
                    result.value         = output.Amount;
                    result.scriptPubKey  = output.LockScript;
                    result.version       = tx.Version;
                    result.coinbase      = (tx.InputCount == 0 && tx.Inputs[0].OutputTransactionHash == Base16.Encode(HashHelper.EmptyHash()));
                }

                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Пример #3
0
        public IRpcMethodResult ListTransactions(string account, int count, int skip = 0, bool includeWatchOnly = true)
        {
            try
            {
                var txComponent                 = new TransactionComponent();
                var accountComponent            = new AccountComponent();
                var addressBookComponent        = new AddressBookComponent();
                var utxoComponent               = new UtxoComponent();
                var blockComponent              = new BlockComponent();
                var transactionCommentComponent = new TransactionCommentComponent();

                List <PaymentOM> result = new List <PaymentOM>();
                var accounts            = accountComponent.GetAllAccounts();
                var paymentAccountIds   = accounts.Where(a => !string.IsNullOrWhiteSpace(a.PrivateKey)).Select(a => a.Id).ToList();
                var allAccountIds       = accounts.Select(a => a.Id).ToList();
                var addressBook         = addressBookComponent.GetWholeAddressBook();
                var latestHeight        = blockComponent.GetLatestHeight();
                var data = txComponent.SearchTransactionEntities(account, count, skip, includeWatchOnly);

                foreach (var tx in data)
                {
                    long totalInput           = 0;
                    long selfTotalOutput      = 0;
                    long otherUserTotalOutput = 0;
                    bool coibase = false;

                    if (tx.Inputs.Count == 1 && tx.Outputs.Count == 1 && tx.Inputs[0].OutputTransactionHash == Base16.Encode(HashHelper.EmptyHash()))
                    {
                        coibase = true;
                    }

                    if (!coibase)
                    {
                        foreach (var input in tx.Inputs)
                        {
                            var oldOutput = txComponent.GetOutputEntiyByIndexAndTxHash(input.OutputTransactionHash, input.OutputIndex);

                            if (oldOutput != null && paymentAccountIds.Contains(oldOutput.ReceiverId))
                            {
                                totalInput += input.Amount;
                            }
                            else
                            {
                                totalInput = 0;
                                break;
                            }
                        }
                    }

                    foreach (var output in tx.Outputs)
                    {
                        if (allAccountIds.Contains(output.ReceiverId))
                        {
                            selfTotalOutput += output.Amount;
                        }
                        else
                        {
                            otherUserTotalOutput += output.Amount;
                        }
                    }

                    BlockMsg block = null;

                    if (tx.BlockHash != null)
                    {
                        block = blockComponent.GetBlockMsgByHash(tx.BlockHash);
                    }

                    if (coibase)
                    {
                        var payment = new PaymentOM();
                        payment.address     = tx.Outputs[0].ReceiverId;
                        payment.account     = accounts.Where(a => a.Id == payment.address).Select(a => a.Tag).FirstOrDefault();
                        payment.category    = "generate";
                        payment.totalInput  = totalInput;
                        payment.totalOutput = selfTotalOutput;
                        payment.amount      = selfTotalOutput;
                        payment.fee         = 0;
                        payment.txId        = tx.Hash;
                        payment.vout        = 0;
                        payment.time        = tx.Timestamp;
                        payment.size        = tx.Size;

                        var txComment = transactionCommentComponent.GetByTransactionHashAndIndex(tx.Hash, 0);
                        if (txComment != null)
                        {
                            payment.comment = txComment.Comment;
                        }

                        if (block != null)
                        {
                            payment.blockHash     = tx.BlockHash;
                            payment.blockIndex    = 0;// block.Transactions.FindIndex(t=>t.Hash == tx.Hash);
                            payment.blockTime     = block.Header.Timestamp;
                            payment.confirmations = latestHeight - block.Header.Height + 1;
                        }
                        else
                        {
                            payment.confirmations = 0;
                        }

                        result.Add(payment);
                    }
                    else if (totalInput > 0 && otherUserTotalOutput == 0)
                    {
                        var payment = new PaymentOM();
                        payment.address     = null;
                        payment.account     = null;
                        payment.category    = "self";
                        payment.totalInput  = totalInput;
                        payment.totalOutput = tx.Outputs[0].Amount;// selfTotalOutput;
                        payment.fee         = totalInput - selfTotalOutput;
                        payment.amount      = payment.fee;
                        payment.txId        = tx.Hash;
                        payment.vout        = 0;
                        payment.time        = tx.Timestamp;
                        payment.size        = tx.Size;

                        var txComments = transactionCommentComponent.GetByTransactionHash(tx.Hash);
                        if (txComments.Count > 0)
                        {
                            payment.comment = "";
                            foreach (var item in txComments)
                            {
                                if (!string.IsNullOrWhiteSpace(item.Comment))
                                {
                                    payment.comment += item.Comment + ";";
                                }
                            }
                        }

                        if (block != null)
                        {
                            payment.blockHash     = tx.BlockHash;
                            payment.blockIndex    = block.Transactions.FindIndex(t => t.Hash == tx.Hash);
                            payment.blockTime     = block.Header.Timestamp;
                            payment.confirmations = latestHeight - block.Header.Height + 1;
                        }
                        else
                        {
                            payment.confirmations = 0;
                        }

                        result.Add(payment);
                    }
                    else if (totalInput > 0)
                    {
                        for (int i = 0; i < tx.Outputs.Count; i++)
                        {
                            if (!allAccountIds.Contains(tx.Outputs[i].ReceiverId))
                            {
                                var payment = new PaymentOM();
                                payment.address     = tx.Outputs[i].ReceiverId;
                                payment.account     = addressBook.Where(a => a.Address == payment.address && !string.IsNullOrWhiteSpace(a.Tag)).Select(a => a.Tag).FirstOrDefault();
                                payment.category    = "send";
                                payment.totalInput  = totalInput;
                                payment.totalOutput = tx.Outputs[i].Amount;
                                payment.fee         = totalInput - (selfTotalOutput + otherUserTotalOutput);
                                payment.amount      = (i == 0 ? tx.Outputs[i].Amount + payment.fee : tx.Outputs[i].Amount);
                                payment.txId        = tx.Hash;
                                payment.vout        = i;
                                payment.time        = tx.Timestamp;
                                payment.size        = tx.Size;

                                var txComment = transactionCommentComponent.GetByTransactionHashAndIndex(tx.Hash, i);
                                if (txComment != null)
                                {
                                    payment.comment = txComment.Comment;
                                }

                                if (block != null)
                                {
                                    payment.blockHash     = tx.BlockHash;
                                    payment.blockIndex    = block.Transactions.FindIndex(t => t.Hash == tx.Hash);
                                    payment.blockTime     = block.Header.Timestamp;
                                    payment.confirmations = latestHeight - block.Header.Height + 1;
                                }
                                else
                                {
                                    payment.confirmations = 0;
                                }

                                result.Add(payment);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < tx.Outputs.Count; i++)
                        {
                            if (allAccountIds.Contains(tx.Outputs[i].ReceiverId))
                            {
                                var payment = new PaymentOM();
                                payment.address     = tx.Outputs[i].ReceiverId;
                                payment.account     = accounts.Where(a => a.Id == payment.address).Select(a => a.Tag).FirstOrDefault();;
                                payment.category    = "receive";
                                payment.totalInput  = totalInput;
                                payment.totalOutput = tx.Outputs[i].Amount;
                                payment.fee         = totalInput - (selfTotalOutput + otherUserTotalOutput);
                                payment.amount      = tx.Outputs[i].Amount;
                                payment.txId        = tx.Hash;
                                payment.vout        = i;
                                payment.time        = tx.Timestamp;
                                payment.size        = tx.Size;

                                var txComment = transactionCommentComponent.GetByTransactionHashAndIndex(tx.Hash, i);
                                if (txComment != null)
                                {
                                    payment.comment = txComment.Comment;
                                }

                                if (block != null)
                                {
                                    payment.blockHash     = tx.BlockHash;
                                    payment.blockIndex    = block.Transactions.FindIndex(t => t.Hash == tx.Hash);
                                    payment.blockTime     = block.Header.Timestamp;
                                    payment.confirmations = latestHeight - block.Header.Height + 1;
                                }
                                else
                                {
                                    payment.confirmations = 0;
                                }

                                result.Add(payment);
                            }
                        }
                    }
                }

                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
Пример #4
0
        public IRpcMethodResult ListUnspent(int minConfirmations, int maxConfirmations = 9999999, string[] addresses = null)
        {
            try
            {
                var result               = new List <ListUnspentOM>();
                var txComponent          = new TransactionComponent();
                var blockComponent       = new BlockComponent();
                var outputComponent      = new UtxoComponent();
                var accountComponent     = new AccountComponent();
                var addressBookComponent = new AddressBookComponent();

                var accounts     = accountComponent.GetAllAccounts();
                var transactions = txComponent.GetTransactionEntitiesContainUnspentUTXO();
                var latestHeight = blockComponent.GetLatestHeight();

                foreach (var tx in transactions)
                {
                    var blockMsg = blockComponent.GetBlockMsgByHash(tx.BlockHash);

                    if (blockMsg != null)
                    {
                        var confirmations = latestHeight - blockMsg.Header.Height;

                        if (confirmations >= minConfirmations && confirmations <= maxConfirmations)
                        {
                            var outputs = txComponent.GetOutputEntitesByTxHash(tx.Hash);

                            foreach (var output in outputs)
                            {
                                var pubKeyHash = Script.GetPublicKeyHashFromLockScript(output.LockScript);
                                var address    = AccountIdHelper.CreateAccountAddressByPublicKeyHash(Base16.Decode(pubKeyHash));

                                var account = accounts.Where(a => a.Id == address).FirstOrDefault();
                                if (account != null)
                                {
                                    if (addresses == null || addresses.Contains(address))
                                    {
                                        result.Add(new ListUnspentOM
                                        {
                                            txid          = output.TransactionHash,
                                            vout          = output.Index,
                                            address       = address,
                                            account       = addressBookComponent.GetTagByAddress(address),
                                            scriptPubKey  = output.LockScript,
                                            redeemScript  = null,
                                            amount        = output.Amount,
                                            confirmations = confirmations,
                                            spendable     = string.IsNullOrWhiteSpace(account.PrivateKey),
                                            solvable      = false
                                        });
                                    }
                                }
                            }
                        }
                    }
                }

                return(Ok(result));
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }