Module: PWN::Blockchain::BTC

Defined in:
lib/pwn/blockchain/btc.rb

Overview

This plugin interacts with BitCoin’s Blockchain API.

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



256
257
258
259
260
# File 'lib/pwn/blockchain/btc.rb', line 256

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.get_block_details(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Blockchain::BTC.get_block_details(

height: 'required - block height as an integer (0 for genesis block / Defaults to latest block)'

)



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/pwn/blockchain/btc.rb', line 203

public_class_method def self.get_block_details(opts = {})
  latest_block = get_latest_block[:result][:blocks]
  height = opts[:height] ||= latest_block

  raise "ERROR: height must be >= 0 && <= #{latest_block}" if height.negative? || height > latest_block

  hash_res = btc_rpc_call(method: 'getblockhash', params: [height])
  block_hash = hash_res[:result]

  # Verbosity 1: block details with tx IDs
  block_res = btc_rpc_call(method: 'getblock', params: [block_hash, 1])

  block_res[:result]
rescue StandardError => e
  raise e
end

.get_latest_blockObject

Supported Method Parameters

latest_block = PWN::Blockchain::BTC.get_latest_block



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/pwn/blockchain/btc.rb', line 183

public_class_method def self.get_latest_block
  latest_block = btc_rpc_call(method: 'getblockchaininfo', params: [])
  system_role_content = 'Provide a useful summary of this latest bitcoin block returned from a bitcoin node via getblockchaininfo.'
  ai_analysis = PWN::AI::Introspection.reflect_on(
    request: latest_block.to_s,
    system_role_content: system_role_content,
    suppress_pii_warning: true
  )
  puts ai_analysis

  latest_block
rescue StandardError => e
  raise e
end

.get_transactions(opts = {}) ⇒ Object

Supported Method Parameters

transactions = PWN::Blockchain::BTC.get_transactions(

from: 'required - start date in YYYY-MM-DD format',
to: 'required - end date in YYYY-MM-DD format'

)



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/pwn/blockchain/btc.rb', line 226

public_class_method def self.get_transactions(opts = {})
  from_date = opts[:from]
  to_date = opts[:to]

  raise ArgumentError, 'from and to dates are required' if from_date.nil? || to_date.nil?

  start_ts = Date.parse(from_date).to_time.to_i
  end_ts = Date.parse(to_date).to_time.to_i + 86_399 # Include the entire end day

  start_height = find_first_block_ge_timestamp(start_ts)
  end_height = find_last_block_le_timestamp(end_ts)

  txs = []
  if start_height && end_height && start_height <= end_height
    (start_height..end_height).each do |height|
      block_hash_res = btc_rpc_call(method: 'getblockhash', params: [height])
      block_hash = block_hash_res[:result]

      block_res = btc_rpc_call(method: 'getblock', params: [block_hash, 1])
      txs.concat(block_res[:result][:tx])
    end
  end

  txs
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/pwn/blockchain/btc.rb', line 264

public_class_method def self.help
  puts "USAGE:
    latest_block = #{self}.get_latest_block

    block_details = #{self}.get_block_details(
      height: 'required - block height as an integer (0 for genesis block / Defaults to latest block)'
    )

    transactions = #{self}.get_transactions(
      from: 'required - start date in YYYY-MM-DD format',
      to: 'required - end date in YYYY-MM-DD format'
    )

    #{self}.authors
  "
end