예제 #1
0
        public static void Initialize()
        {
            var notify = new NotifyComponent();
            BlockchainComponent blockChainComponent = new BlockchainComponent();
            AccountComponent    accountComponent    = new AccountComponent();
            UtxoComponent       utxoComponent       = new UtxoComponent();

            //从配置文件中读取
            ConfigurationTool tool   = new ConfigurationTool();
            NodeConfig        config = tool.GetAppSettings <NodeConfig>("NodeConfig");

            notify.SetCallbackApp(config.WalletNotify);

            if (GlobalActions.TransactionNotifyAction == null)
            {
                GlobalActions.TransactionNotifyAction = NewTransactionNotify;
            }

            blockChainComponent.Initialize();
            var accounts = accountComponent.GetAllAccounts();

            if (accounts.Count == 0)
            {
                var account = accountComponent.GenerateNewAccount();
                accountComponent.SetDefaultAccount(account.Id);
            }

            BlockchainJob.Current = new BlockchainJob();
            utxoComponent.Initialize();
        }
예제 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <returns></returns>
 public IRpcMethodResult ExportAddresses()
 {
     try
     {
         AccountComponent component = new AccountComponent();
         List <Account>   accounts  = component.GetAllAccounts();
         var list = accounts.Select(q => new { Id = q.Id, PublicKey = q.PublicKey });
         return(Ok(Newtonsoft.Json.JsonConvert.SerializeObject(list)));
     }
     catch (CommonException ce)
     {
         return(Error(ce.ErrorCode, ce.Message, ce));
     }
     catch (Exception ex)
     {
         return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
     }
 }
예제 #3
0
        public static void Initialize()
        {
            BlockchainComponent blockChainComponent = new BlockchainComponent();
            AccountComponent    accountComponent    = new AccountComponent();
            UtxoComponent       utxoComponent       = new UtxoComponent();

            blockChainComponent.Initialize();
            var accounts = accountComponent.GetAllAccounts();

            if (accounts.Count == 0)
            {
                var account = accountComponent.GenerateNewAccount();
                accountComponent.SetDefaultAccount(account.Id);
            }

            BlockchainJob.Current = new BlockchainJob();
            utxoComponent.Initialize();
        }
예제 #4
0
        public IRpcMethodResult GetPageAccountCategory(int category, int pageSize = 0, int pageCount = int.MaxValue, bool isSimple = true)
        {
            try
            {
                var            accountCompent = new AccountComponent();
                List <Account> result         = accountCompent.GetAllAccounts();;
                switch (category)
                {
                case 1:
                    result = result.Where(x => x.IsDefault).ToList();
                    break;

                case 2:
                    result = result.Where(x => !x.IsDefault).ToList();
                    break;

                case 3:
                    result = result.Where(x => string.IsNullOrEmpty(x.PrivateKey)).ToList();
                    break;

                default:
                    break;
                }

                var accounts = result.OrderBy(x => x.Id).Skip(pageSize * pageCount).Take(pageCount);

                if (isSimple)
                {
                    return(Ok(new { Count = result.Count.ToString(), Accounts = accounts.Select(x => new { x.Id, x.Tag }) }));
                }
                else
                {
                    return(Ok(new { Count = result.Count.ToString(), Accounts = accounts }));
                }
            }
            catch (CommonException ce)
            {
                return(Error(ce.ErrorCode, ce.Message, ce));
            }
            catch (Exception ex)
            {
                return(Error(ErrorCode.UNKNOWN_ERROR, ex.Message, ex));
            }
        }
예제 #5
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));
            }
        }
예제 #6
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));
            }
        }