Class: BSV::Transaction::ChainTrackers::WhatsOnChain

Inherits:
BSV::Transaction::ChainTracker show all
Defined in:
lib/bsv/transaction/chain_trackers/whats_on_chain.rb

Overview

Chain tracker that verifies merkle roots using the WhatsOnChain API.

Delegates all HTTP communication to Network::Protocols::WoCREST. The constructor signature and BSV::Transaction::ChainTracker contract are preserved.

Note: the WoC API key is sent as a raw Authorization header value (not Bearer-prefixed) to match the existing WoC API convention.

Examples:

tracker = BSV::Transaction::ChainTrackers::WhatsOnChain.new
tracker.valid_root_for_height?('abcd...', 800_000)

Defined Under Namespace

Classes: RawAuthClient

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network: :main, api_key: nil, http_client: nil, protocol: nil) ⇒ WhatsOnChain

Returns a new instance of WhatsOnChain.

Parameters:

  • network (Symbol) (defaults to: :main)

    :main, :mainnet, :test, :testnet (legacy compat)

  • api_key (String, nil) (defaults to: nil)

    optional WoC API key

  • http_client (#request, nil) (defaults to: nil)

    injectable HTTP client for testing

  • protocol (BSV::Network::Protocols::WoCREST, nil) (defaults to: nil)

    pre-configured protocol



34
35
36
37
38
39
40
41
42
43
# File 'lib/bsv/transaction/chain_trackers/whats_on_chain.rb', line 34

def initialize(network: :main, api_key: nil, http_client: nil, protocol: nil)
  super()
  if protocol
    @protocol = protocol
  else
    wrapped_client = api_key ? RawAuthClient.new(api_key, http_client) : http_client
    provider = BSV::Network::Providers::WhatsOnChain.default(network: network, http_client: wrapped_client)
    @protocol = provider.protocol_for(:valid_root)
  end
end

Class Method Details

.default(testnet: false, **opts) ⇒ WhatsOnChain

Returns a WhatsOnChain chain tracker using the provider default.

Parameters:

  • testnet (Boolean) (defaults to: false)

    when true, uses the testnet endpoint

  • opts (Hash)

    forwarded to the underlying protocol (e.g. api_key:, http_client:)

Returns:



25
26
27
28
# File 'lib/bsv/transaction/chain_trackers/whats_on_chain.rb', line 25

def self.default(testnet: false, **opts)
  provider = BSV::Network::Providers::WhatsOnChain.default(testnet: testnet, **opts)
  new(protocol: provider.protocol_for(:valid_root))
end

Instance Method Details

#current_heightInteger

Return the current blockchain height.

Returns:

  • (Integer)

Raises:



69
70
71
72
73
74
75
76
77
# File 'lib/bsv/transaction/chain_trackers/whats_on_chain.rb', line 69

def current_height
  result = @protocol.call(:current_height)
  return result.data if result.success?

  raise BSV::Network::ChainProviderError.new(
    result.message.to_s,
    status_code: result.[:status_code]
  )
end

#valid_root_for_height?(root, height) ⇒ Boolean

Verify that a merkle root is valid for the given block height.

Parameters:

  • root (String)

    merkle root as a hex string

  • height (Integer)

    block height

Returns:

  • (Boolean)

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bsv/transaction/chain_trackers/whats_on_chain.rb', line 51

def valid_root_for_height?(root, height)
  result = @protocol.call(:valid_root, root, height)
  return false if result.not_found?

  if result.error?
    raise BSV::Network::ChainProviderError.new(
      result.message.to_s,
      status_code: result.[:status_code]
    )
  end

  result.data == true
end