Class: BSV::Wallet::Postgres::ProofStore

Inherits:
Object
  • Object
show all
Includes:
Interface::ProofStore
Defined in:
lib/bsv/wallet/postgres/proof_store.rb

Overview

Merkle proof manager backed by tx_proofs table.

Instance Method Summary collapse

Constructor Details

#initialize(db: nil) ⇒ ProofStore

Returns a new instance of ProofStore.



10
11
12
# File 'lib/bsv/wallet/postgres/proof_store.rb', line 10

def initialize(db: nil)
  @db = db || BSV::Wallet::Postgres.db
end

Instance Method Details

#find_proof(wtxid:) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/bsv/wallet/postgres/proof_store.rb', line 30

def find_proof(wtxid:)
  BSV::Primitives::Hex.validate_wtxid!(wtxid, name: 'find_proof wtxid')
  record = TxProof.first(wtxid: Sequel.blob(wtxid))
  return unless record

  proof_to_hash(record)
end

#proof_exists?(wtxid:) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/bsv/wallet/postgres/proof_store.rb', line 38

def proof_exists?(wtxid:)
  BSV::Primitives::Hex.validate_wtxid!(wtxid, name: 'proof_exists? wtxid')
  TxProof.where(wtxid: Sequel.blob(wtxid)).any?
end

#save_proof(wtxid:, proof:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bsv/wallet/postgres/proof_store.rb', line 14

def save_proof(wtxid:, proof:)
  BSV::Primitives::Hex.validate_wtxid!(wtxid, name: 'save_proof wtxid')
  BSV.logger&.debug { "[ProofStore] save_proof: dtxid=#{wtxid.reverse.unpack1('H*')} height=#{proof[:height]}" }

  block_id = find_or_create_block(proof) if proof[:height]

  existing = TxProof.first(wtxid: Sequel.blob(wtxid))
  cols = proof_columns(proof).merge(block_id ? { block_id: block_id } : {})
  if existing
    existing.update(cols)
    existing.id
  else
    TxProof.create({ wtxid: wtxid }.merge(cols)).id
  end
end