예제 #1
0
        public IActionResult GetUtxoSet(int atBlockHeight)
        {
            try
            {
                ReconstructedCoinviewContext coinView = this.utxoIndexer.GetCoinviewAtHeight(atBlockHeight);

                var outputs = new List <UtxoModel>();

                foreach (OutPoint outPoint in coinView.UnspentOutputs)
                {
                    TxOut txOut = coinView.Transactions[outPoint.Hash].Outputs[outPoint.N];
                    var   utxo  = new UtxoModel()
                    {
                        TxId = outPoint.Hash, Index = outPoint.N, ScriptPubKey = txOut.ScriptPubKey, Value = txOut.Value
                    };

                    outputs.Add(utxo);
                }

                return(this.Json(outputs));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }
예제 #2
0
        public IActionResult GetUtxoSetForAddress(string address)
        {
            // Get coinview at current height (SLOW)
            var coinView = this.utxoIndexer.GetCoinviewAtHeight(this.chainIndexer.Height);

            var utxos = new ConcurrentBag <UtxoModel>();

            // Get utxos for this address
            try
            {
                // Fine to do this in parallel because we don't care about the order
                Parallel.ForEach(coinView.UnspentOutputs, (utxo) =>
                {
                    var tx = coinView.Transactions[utxo.Hash];

                    // Get the actual output
                    var output = tx.Outputs[utxo.N];

                    var destinationAddress = this.scriptAddressReader.GetAddressFromScriptPubKey(this.network, output.ScriptPubKey);

                    if (destinationAddress != address)
                    {
                        return;
                    }

                    var utxoModel = new UtxoModel()
                    {
                        TxId         = utxo.Hash,
                        Index        = utxo.N,
                        ScriptPubKey = output.ScriptPubKey,
                        Value        = output.Value
                    };

                    utxos.Add(utxoModel);
                });

                var balance = new Money(utxos.Sum(u => u.Value)).ToUnit(MoneyUnit.BTC);

                return(this.Json(new
                {
                    balance,
                    utxos
                }));
            }
            catch (Exception e)
            {
                this.logger.LogError("Exception occurred: {0}", e.ToString());
                return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString()));
            }
        }