From 76bc5fead04f9029b8bed8dce3f9dba851f92af5 Mon Sep 17 00:00:00 2001 From: Rabbit Date: Tue, 12 Mar 2024 09:43:10 +0800 Subject: [PATCH] chore: fix suggest query param validation (#1671) --- .../api/v2/ckb_transactions_controller.rb | 12 ++++++------ app/controllers/validations/suggest_query.rb | 10 ++++++++-- app/workers/bitcoin_transaction_detect_worker.rb | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v2/ckb_transactions_controller.rb b/app/controllers/api/v2/ckb_transactions_controller.rb index 781bd3e18..f50cb55d2 100644 --- a/app/controllers/api/v2/ckb_transactions_controller.rb +++ b/app/controllers/api/v2/ckb_transactions_controller.rb @@ -54,19 +54,19 @@ def display_outputs def rgb_digest expires_in 10.seconds, public: true, must_revalidate: true - transfers = combine_transfers(@transaction).map do |address_id, transfers| - vout = BitcoinVout.include(:bitcoin_address).find_by(address_id:) + transfers = combine_transfers(@ckb_transaction).map do |address_id, transfers| + vout = BitcoinVout.includes(:bitcoin_address).find_by(address_id:) next unless vout { address: vout.bitcoin_address.address_hash, transfers: } end - vout = @transaction.bitcoin_vouts.find_by(op_return: true) + vout = @ckb_transaction.bitcoin_vouts.find_by(op_return: true) render json: { data: { - txid: vout.bitcoin_transaction.txid, - confirmations: vout.bitcoin_transaction.confirmations, - commitment: vout.bitcoin_transaction.commitment, + txid: vout&.bitcoin_transaction&.txid, + confirmations: vout&.bitcoin_transaction&.confirmations, + commitment: vout&.commitment, transfers:, }, } diff --git a/app/controllers/validations/suggest_query.rb b/app/controllers/validations/suggest_query.rb index 1af31ac3a..e2353e85e 100644 --- a/app/controllers/validations/suggest_query.rb +++ b/app/controllers/validations/suggest_query.rb @@ -15,7 +15,7 @@ def error_object api_errors << Api::V1::Exceptions::SuggestQueryKeyInvalidError.new if :query_key.in?(errors.attribute_names) { status: api_errors.first.status, - errors: RequestErrorSerializer.new(api_errors, message: api_errors.first.title) + errors: RequestErrorSerializer.new(api_errors, message: api_errors.first.title), } end end @@ -25,7 +25,13 @@ def error_object attr_accessor :query_key def query_key_format_must_be_correct - if query_key.blank? || (!QueryKeyUtils.integer_string?(query_key) && !QueryKeyUtils.valid_hex?(query_key) && !QueryKeyUtils.valid_address?(query_key)) + query_key_invalid = + !QueryKeyUtils.integer_string?(query_key) && + !QueryKeyUtils.valid_hex?(query_key) && + !QueryKeyUtils.valid_address?(query_key) && + !QueryKeyUtils.valid_bitcoin_txid?(query_key) + + if query_key.blank? || query_key_invalid errors.add(:query_key, "query key is invalid") end end diff --git a/app/workers/bitcoin_transaction_detect_worker.rb b/app/workers/bitcoin_transaction_detect_worker.rb index 573ce8ed7..337e64210 100644 --- a/app/workers/bitcoin_transaction_detect_worker.rb +++ b/app/workers/bitcoin_transaction_detect_worker.rb @@ -76,7 +76,7 @@ def build_vouts! return if vout_attributes.blank? - `BitcoinVout`.upsert_all(vout_attributes, unique_by: %i[bitcoin_transaction_id index]) + BitcoinVout.upsert_all(vout_attributes, unique_by: %i[bitcoin_transaction_id index]) end def build_tx!(raw_tx)