class Tierion::HashApi::Receipt

Public Instance Methods

confirmations() click to toggle source
# File lib/tierion/hash_api/receipt.rb, line 12
def confirmations
  get_confirmations
  @confs
end
to_pretty_json() click to toggle source
# File lib/tierion/hash_api/receipt.rb, line 8
def to_pretty_json
  puts JSON.pretty_generate(self)
end
valid?() click to toggle source

Checks the validity of the Merkle tree proof and return true or false

# File lib/tierion/hash_api/receipt.rb, line 19
def valid?
  return false if targetHash.blank? || merkleRoot.blank?

  # No siblings, single item tree, so the hash
  # should also be the root
  return targetHash == merkleRoot if proof.empty?

  # The target hash (the hash the user submitted)
  # is always hashed in the first cycle through the
  # proofs. After that, the proof_hash value will
  # contain intermediate hashes.
  proof_hash = targetHash

  proof.each do |p|
    h = Digest::SHA256.new
    if p.key?('left')
      h.update hex2bin(p['left'])
      h.update hex2bin(proof_hash)
    elsif p.key?('right')
      h.update hex2bin(proof_hash)
      h.update hex2bin(p['right'])
    else
      return false
    end
    proof_hash = h.hexdigest
  end

  proof_hash == merkleRoot
end

Private Instance Methods

btc_op_return_confirmed?(source_id) click to toggle source

Confirm Bitcoin OP_RETURN anchor

# File lib/tierion/hash_api/receipt.rb, line 77
def btc_op_return_confirmed?(source_id)
  url = "https://blockchain.info/tx-index/#{source_id}?format=json"

  # op_return values begin with 0x6a (op_return code) &
  # 0x20 (length in hex : 32 bytes)
  op_return = ['6a20', merkleRoot].join('')

  response = HTTParty.get(url)

  if response.success? && response['out'].present?
    has_op_return = response['out'].any? do |o|
      o['script'].present? && o['script'] == op_return
    end

    return has_op_return
  else
    false
  end
end
get_confirmations() click to toggle source
# File lib/tierion/hash_api/receipt.rb, line 55
def get_confirmations
  @confs = {} if @confs.blank?

  return {} if anchors.blank?

  anchors.each do |a|
    # allready confirmed this anchor
    next if @confs[a['type']].is_a?(TrueClass)

    case a['type']
    when 'BTCOpReturn'
      # txn_id
      if a['sourceId'].present?
        @confs[a['type']] = btc_op_return_confirmed?(a['sourceId'])
      end
    end
  end

  @confs
end
hex2bin(hex) click to toggle source
# File lib/tierion/hash_api/receipt.rb, line 51
def hex2bin(hex)
  [hex.to_s].pack('H*')
end