示例#1
0
        public async Task <IEnumerable <BitcoinTransaction> > GetTransactionsAfterTxAsync(string address, string afterHash)
        {
            var heightTo = 0;

            if (!string.IsNullOrEmpty(afterHash))
            {
                var tx = await _ninjaClient.GetTransaction(uint256.Parse(afterHash));

                heightTo = tx?.Block?.Height ?? 0;
            }

            var operations = await _ninjaClient.GetBalanceBetween(new BalanceSelector(address),
                                                                  BlockFeature.Parse("tip"), new BlockFeature(heightTo));

            return((await operations.Operations.SelectAsync(async op =>
            {
                var tx = await _ninjaClient.GetTransaction(op.TransactionId);
                return new BitcoinTransaction
                {
                    Hash = op.TransactionId.ToString(),
                    Timestamp = tx.FirstSeen.DateTime,
                    Inputs = tx.SpentCoins.Select(i => new BitcoinInput
                    {
                        Address = i.TxOut.ScriptPubKey.GetDestinationAddress(_network)?.ToString(),
                        Value = i.TxOut.Value
                    }).ToList(),
                    Outputs = tx.ReceivedCoins.Select(output => new BitcoinOutput
                    {
                        Address = output.TxOut.ScriptPubKey.GetDestinationAddress(_network)?.ToString(),
                        Value = output.TxOut.Value
                    }).ToList()
                };
            })).OrderBy(o => o.Timestamp).ToList());
        }
示例#2
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (!typeof(BlockFeature).IsAssignableFrom(bindingContext.ModelType))
            {
                return(null);
            }

            ValueProviderResult val = bindingContext.ValueProvider.GetValue(
                bindingContext.ModelName);

            if (val == null)
            {
                return(null);
            }

            string key = val.RawValue as string;

            if (key == null)
            {
                return(null);
            }

            BlockFeature feature = BlockFeature.Parse(key);

            return(feature);
        }
        public bool BindModel(System.Web.Http.Controllers.HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            if (!typeof(BlockFeature).IsAssignableFrom(bindingContext.ModelType))
            {
                return(false);
            }

            ValueProviderResult val = bindingContext.ValueProvider.GetValue(
                bindingContext.ModelName);

            if (val == null)
            {
                return(true);
            }

            string key = val.RawValue as string;

            if (key == null)
            {
                bindingContext.Model = null;
                return(true);
            }

            BlockFeature feature = BlockFeature.Parse(key);

            bindingContext.Model = feature;
            return(true);
        }
        private async Task <(string block, bool exists)> IsBlock(string blockFeature)
        {
            try
            {
                var block = await _bitNinjaClient.GetClient().GetBlock(BlockFeature.Parse(blockFeature), headerOnly: true);

                return(blockFeature, block != null);
            }
            catch (Exception)
            {
                return(blockFeature, false);
            }
        }
示例#5
0
        public async Task <IEnumerable <(string txHash, IEnumerable <string> destinationAddresses)> > GetTxOutputAddresses(int blockHeight)
        {
            var blockResponse = await _ninjaClient.GetBlock(BlockFeature.Parse(blockHeight.ToString()));

            if (blockResponse == null)
            {
                throw new ArgumentException("Block not found", nameof(blockHeight));
            }

            var result = new List <(string txHash, IEnumerable <string> destinationAddresses)>();

            foreach (var tx in blockResponse.Block.Transactions)
            {
                var destinationAddresses = tx.Outputs.AsIndexedOutputs().Select(p => p.TxOut.ScriptPubKey
                                                                                .GetDestinationAddress(_network)
                                                                                ?.ToString())
                                           .Where(p => p != null);

                result.Add((txHash: tx.GetHash().ToString(), destinationAddresses: destinationAddresses));
            }

            return(result);
        }
        public async Task <Stream> GetTransactionsReport(string blockId)
        {
            var getBlock = _blockService.GetBlock(BlockFeature.Parse(blockId));
            var getAssetDefDictionary = _assetDefinitionService.GetAssetDefinitionsAsync();

            await Task.WhenAll(getBlock, getAssetDefDictionary);

            if (getBlock.Result == null)
            {
                throw new Exception($"Block {blockId} not found");
            }

            var transactionIds = getBlock.Result.Block.Transactions.Select(p => p.GetHash());

            var txResps = await _transactionService.GetTransactions(transactionIds);

            var xlsxData = XlsxTransactionsReportData.Create(
                txResps,
                getAssetDefDictionary.Result,
                _network);

            return(await _transactionXlsxRenderer.RenderTransactionReport(xlsxData));
        }
示例#7
0
        public async Task <object> Find(string data)
        {
            data = data.Trim();
            var b58 = NoException(() => WhatIsBase58.GetFromBitcoinString(data));

            if (b58 != null)
            {
                if (b58 is WhatIsAddress)
                {
                    var address = (WhatIsAddress)b58;
                    TryFetchRedeemOrPubKey(address);
                }
                return(b58);
            }

            if (data.Length == 0x40)
            {
                try
                {
                    return(await Controller.JsonTransaction(uint256.Parse(data), false));
                }
                catch
                {
                }
            }
            var b = NoException(() => Controller.JsonBlock(BlockFeature.Parse(data), true, false));

            if (b != null)
            {
                return(b);
            }

            if (data.Length == 0x28) //Hash of pubkey or script
            {
                TxDestination dest    = new KeyId(data);
                var           address = new WhatIsAddress(dest.GetAddress(Network));
                if (TryFetchRedeemOrPubKey(address))
                {
                    return(address);
                }

                dest    = new ScriptId(data);
                address = new WhatIsAddress(dest.GetAddress(Network));
                if (TryFetchRedeemOrPubKey(address))
                {
                    return(address);
                }
            }


            var script = NoException(() => GetScriptFromBytes(data));

            if (script != null)
            {
                return(new WhatIsScript(script, Network));
            }
            script = NoException(() => GetScriptFromText(data));
            if (script != null)
            {
                return(new WhatIsScript(script, Network));
            }

            var sig = NoException(() => new TransactionSignature(Encoders.Hex.DecodeData(data)));

            if (sig != null)
            {
                return(new WhatIsTransactionSignature(sig));
            }

            var pubkeyBytes = NoException(() => Encoders.Hex.DecodeData(data));

            if (pubkeyBytes != null && PubKey.Check(pubkeyBytes, true))
            {
                var pubKey = NoException(() => new PubKey(data));
                if (pubKey != null)
                {
                    return(new WhatIsPublicKey(pubKey, Network));
                }
            }

            if (data.Length == 80 * 2)
            {
                var blockHeader = NoException(() =>
                {
                    var h = new BlockHeader();
                    h.ReadWrite(Encoders.Hex.DecodeData(data));
                    return(h);
                });
                if (blockHeader != null)
                {
                    return(new WhatIsBlockHeader(blockHeader));
                }
            }
            return(null);
        }
示例#8
0
        public async Task <int> GetLastBlockHeightAsync()
        {
            var block = await _ninjaClient.GetBlock(BlockFeature.Parse("tip"), true);

            return(block.AdditionalInformation.Height);
        }
示例#9
0
        public async Task <BalanceModel> GetAddressBalance(string walletAddress)
        {
            var client = _clientFactory();

            client.Colored = true;
            if (_settings.Bcc.UseBccNinja)
            {
                return(await client.GetBalance(BitcoinAddress.Create(walletAddress, _connectionParams.Network), true));
            }
            return(await client.GetBalanceBetween(new BalanceSelector(BitcoinAddress.Create(walletAddress, _connectionParams.Network)), from : BlockFeature.Parse(Constants.BccBlock.ToString()), unspentOnly : true));
        }
示例#10
0
        public async Task <ActionResult> Index(string search = null, int count = 5)
        {
            count = Math.Max(0, count);
            count = Math.Min(50, count);
            if (!string.IsNullOrWhiteSpace(search))
            {
                search = search.Trim();
                if (search.StartsWith("0x") || search.Contains("OP_"))
                {
                    return(RedirectToAction("Address", new
                    {
                        address = search
                    }));
                }
                try
                {
                    BitcoinAddress.Create(search, QBit.Network);
                    return(RedirectToAction("Address", new
                    {
                        address = search
                    }));
                }
                catch { }

                if (search.Length == 32 * 2)
                {
                    if (search.StartsWith("0000000000"))
                    {
                        return(RedirectToAction("Block", new
                        {
                            blockFeature = search
                        }));
                    }
                    else
                    {
                        return(RedirectToAction("Transaction", new
                        {
                            txId = search
                        }));
                    }
                }

                try
                {
                    BlockFeature.Parse(search);
                    return(RedirectToAction("Block", new
                    {
                        blockFeature = search
                    }));
                }
                catch { }
                return(View());
            }

            var responses =
                await Task.WhenAll(Enumerable
                                   .Range(0, count)
                                   .Select(i => QBit.GetBlock(new BlockFeature(SpecialFeature.Last)
            {
                Offset = -i
            }, true, true))
                                   .ToArray());

            var model = new MainModel();

            model.NextCount = count + 5;
            foreach (var response in responses.Where(r => r.ExtendedInformation != null && r.AdditionalInformation != null))
            {
                var blockModel = new MainBlockModel();
                blockModel.Hash              = response.AdditionalInformation.BlockId;
                blockModel.Height            = response.AdditionalInformation.Height;
                blockModel.Size              = ToKB(response.ExtendedInformation.Size);
                blockModel.Time              = ToRelative(response.AdditionalInformation.BlockTime);
                blockModel.TransactionsCount = response.ExtendedInformation.TransactionCount;
                blockModel.Fees              = ToString(response.ExtendedInformation.BlockReward - response.ExtendedInformation.BlockSubsidy);
                model.Blocks.Add(blockModel);
            }
            return(View(model));
        }
示例#11
0
        /// <summary>
        /// Try to interpret the given string in a few ways in order to detect what object it's supposed to represent.
        /// </summary>
        /// <returns>The object represented by the input string. This may be a Bitcoin address, a script, a signature, a public key, etc.</returns>
        public async Task <object> Find(string data)
        {
            data = data.Trim();


            // Is it a Bitcoin address?
            var b58 = NoException(() => WhatIsBase58.GetFromBitcoinString(data));

            if (b58 != null)
            {
                if (b58 is WhatIsAddress address)
                {
                    await TryFetchRedeemOrPubKey(address);  // Shouldn't the return value here be checked?
                }

                return(b58);
            }


            // Is it a transaction ID?
            if (data.Length == 0x40)
            {
                try
                {
                    return(await Controller.JsonTransaction(uint256.Parse(data), false));
                }
                catch
                {
                    // Well, apparently it's not a transaction ID.
                }
            }


            // Is it a block feature?
            var b = NoException(() => Controller.JsonBlock(BlockFeature.Parse(data), true, false));

            if (b != null)
            {
                return(b);
            }


            // Is it the hash of a public key (modeled as KeyId in NBitcoin), or is it the hash of a script ID?
            if (data.Length == 0x28) // Hash of pubkey or script
            {
                TxDestination dest = new KeyId(data);

                var address = new WhatIsAddress(dest.GetAddress(Network));

                if (await TryFetchRedeemOrPubKey(address))
                {
                    return(address);
                }

                dest    = new ScriptId(data);
                address = new WhatIsAddress(dest.GetAddress(Network));

                if (await TryFetchRedeemOrPubKey(address))
                {
                    return(address);
                }
            }


            // Is it a script?
            var script = NoException(() => GetScriptFromBytes(data));

            if (script != null)
            {
                return(new WhatIsScript(script, Network));
            }

            script = NoException(() => GetScriptFromText(data));

            if (script != null)
            {
                return(new WhatIsScript(script, Network));
            }


            // Is it a transaction signature?
            var sig = NoException(() => new TransactionSignature(Encoders.Hex.DecodeData(data)));

            if (sig != null)
            {
                return(new WhatIsTransactionSignature(sig));
            }


            // Is it a hexstring representing the bytes of a public key?
            var pubkeyBytes = NoException(() => Encoders.Hex.DecodeData(data));

            if (pubkeyBytes != null && PubKey.Check(pubkeyBytes, true))
            {
                var pubKey = NoException(() => new PubKey(data));

                if (pubKey != null)
                {
                    return(new WhatIsPublicKey(pubKey, Network));
                }
            }


            // Is it a blockheader?
            if (data.Length == 80 * 2)
            {
                var blockHeader = NoException(() =>
                {
                    var h = ConsensusFactory.CreateBlockHeader();
                    h.ReadWrite(Encoders.Hex.DecodeData(data), ConsensusFactory);
                    return(h);
                });

                if (blockHeader != null)
                {
                    return(new WhatIsBlockHeader(blockHeader));
                }
            }


            // No idea what this is.
            return(null);
        }