public async Task <ActionResult> GetBlockByHash(string hash, int noTxList = 1)
        {
            if (!Validations.IsValidHash(hash))
            {
                return(BadRequest(hash + " is not a valid block hash"));
            }

            Utils.CheckIfChainIsFresh(chain_, config_.AcceptStaleRequests);

            string key = "block" + noTxList + hash;

            if (memoryCache_.TryGetValue(key, out JsonResult cachedBlockJson))
            {
                return(cachedBlockJson);
            }
            ;

            var binaryHash = Binary.HexStringToByteArray(hash);

            using (DisposableApiCallResult <GetBlockHeaderByHashTxSizeResult> getBlockResult = await chain_.FetchBlockHeaderByHashTxSizesAsync(binaryHash))
            {
                Utils.CheckBitprimApiErrorCode(getBlockResult.ErrorCode, "FetchBlockHeaderByHashTxSizesAsync(" + hash + ") failed, check error log");

                var getLastHeightResult = await chain_.FetchLastHeightAsync();

                Utils.CheckBitprimApiErrorCode(getLastHeightResult.ErrorCode, "FetchLastHeightAsync() failed, check error log");

                var blockHeight = getBlockResult.Result.Header.BlockHeight;

                ApiCallResult <GetBlockHashTimestampResult> getNextBlockResult = null;
                if (blockHeight != getLastHeightResult.Result)
                {
                    getNextBlockResult = await chain_.FetchBlockByHeightHashTimestampAsync(blockHeight + 1);

                    Utils.CheckBitprimApiErrorCode(getNextBlockResult.ErrorCode, "FetchBlockByHeightHashTimestampAsync(" + blockHeight + 1 + ") failed, check error log");
                }

                decimal  blockReward;
                PoolInfo poolInfo;
                using (DisposableApiCallResult <GetTxDataResult> coinbase = await chain_.FetchTransactionAsync(getBlockResult.Result.TransactionHashes[0], true))
                {
                    Utils.CheckBitprimApiErrorCode(coinbase.ErrorCode, "FetchTransactionAsync(" + getBlockResult.Result.TransactionHashes[0] + ") failed, check error log");
                    blockReward = Utils.SatoshisToCoinUnits(coinbase.Result.Tx.TotalOutputValue);
                    poolInfo    = poolsInfo_.GetPoolInfo(coinbase.Result.Tx);
                }

                JsonResult blockJson = Json(BlockToJSON
                                            (
                                                getBlockResult.Result.Header.BlockData, blockHeight, getBlockResult.Result.TransactionHashes,
                                                blockReward, getLastHeightResult.Result, getNextBlockResult?.Result.BlockHash,
                                                getBlockResult.Result.SerializedBlockSize, poolInfo, noTxList == 0)
                                            );

                memoryCache_.Set(key, blockJson, new MemoryCacheEntryOptions {
                    Size = Constants.Cache.BLOCK_CACHE_ENTRY_SIZE
                });
                return(blockJson);
            }
        }
Beispiel #2
0
        private static async Task <UInt64> SumAddressInputs(ITransaction tx, PaymentAddress address, IChain chain, bool useTestnetRules)
        {
            UInt64 inputSum = 0;

            foreach (Input input in tx.Inputs)
            {
                if (input.PreviousOutput == null)
                {
                    continue;
                }
                using (var getTxResult = await chain.FetchTransactionAsync(input.PreviousOutput.Hash, false))
                {
                    if (getTxResult.ErrorCode == ErrorCode.NotFound)
                    {
                        continue;
                    }
                    CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync(" + Binary.ByteArrayToHexString(input.PreviousOutput.Hash) + ") failed, check error log");
                    Output referencedOutput = getTxResult.Result.Tx.Outputs[input.PreviousOutput.Index];
                    if (referencedOutput.PaymentAddress(useTestnetRules).Encoded == address.Encoded)
                    {
                        inputSum += referencedOutput.Value;
                    }
                }
            }
            return(inputSum);
        }
Beispiel #3
0
        private async Task <List <Utxo> > GetUtxo(string paymentAddress, bool returnLegacyAddresses)
        {
            Utils.CheckIfChainIsFresh(chain_, config_.AcceptStaleRequests);

            using (var address = new PaymentAddress(paymentAddress))
                using (var getAddressHistoryResult = await chain_.FetchHistoryAsync(address, UInt64.MaxValue, 0))
                {
                    Utils.CheckBitprimApiErrorCode(getAddressHistoryResult.ErrorCode, "FetchHistoryAsync(" + paymentAddress + ") failed, check error log.");

                    var history = getAddressHistoryResult.Result;

                    var utxo = new List <Utxo>();

                    var getLastHeightResult = await chain_.FetchLastHeightAsync();

                    Utils.CheckBitprimApiErrorCode(getLastHeightResult.ErrorCode, "FetchLastHeightAsync failed, check error log");

                    var topHeight = getLastHeightResult.Result;

                    foreach (IHistoryCompact compact in history)
                    {
                        if (compact.PointKind == PointKind.Output)
                        {
                            using (var outPoint = new OutputPoint(compact.Point.Hash, compact.Point.Index))
                            {
                                var getSpendResult = await chain_.FetchSpendAsync(outPoint);

                                if (getSpendResult.ErrorCode == ErrorCode.NotFound) //Unspent = it's an utxo
                                {
                                    //Get the tx to get the script
                                    using (var getTxResult = await chain_.FetchTransactionAsync(compact.Point.Hash, true))
                                    {
                                        Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync (" + Binary.ByteArrayToHexString(outPoint.Hash) + ") failed, check error log");
                                        utxo.Add(new Utxo(address, compact.Point, getTxResult.ErrorCode, getTxResult.Result.Tx, compact, topHeight, returnLegacyAddresses));
                                    }
                                }
                            }
                        }
                    }
                    utxo.AddRange(GetUnconfirmedUtxo(address));
                    return(utxo);
                }
        }
        public async Task <ActionResult> GetRawTransactionByHash(string hash)
        {
            if (!Validations.IsValidHash(hash))
            {
                return(BadRequest(hash + " is not a valid transaction hash"));
            }
            Utils.CheckIfChainIsFresh(chain_, config_.AcceptStaleRequests);
            var binaryHash = Binary.HexStringToByteArray(hash);

            using (var getTxResult = await chain_.FetchTransactionAsync(binaryHash, false))
            {
                Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync(" + hash + ") failed, check error log");

                var tx = getTxResult.Result.Tx;
                return(Json(new GetRawTransactionResponse
                {
                    rawtx = Binary.ByteArrayToHexString(tx.ToData(false).Reverse().ToArray())
                }));
            }
        }
Beispiel #5
0
 public ITransaction GetTransactionByHash(string txHash)
 {
     return(chain_.FetchTransactionAsync(Binary.HexStringToByteArray(txHash), true).Result.Result.Tx);
 }