Class: Clickwrap::Integrity::Attestor

Inherits:
Object
  • Object
show all
Defined in:
lib/clickwrap/integrity/attestor.rb

Overview

Invokes optional external adapters only after the required event commit. An outage can never roll back the protected action; every ordinary result is persisted, and an exception is reported through the host's existing after-commit failure hook.

Defined Under Namespace

Classes: ChainSnapshot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Attestor

Returns a new instance of Attestor.



16
17
18
# File 'lib/clickwrap/integrity/attestor.rb', line 16

def initialize(event)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



20
21
22
# File 'lib/clickwrap/integrity/attestor.rb', line 20

def event
  @event
end

Class Method Details

.attest_after_commit(event) ⇒ Object



14
# File 'lib/clickwrap/integrity/attestor.rb', line 14

def self.attest_after_commit(event) = new(event).attest_after_commit

Instance Method Details

#anchor_eventObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/clickwrap/integrity/attestor.rb', line 65

def anchor_event
  adapter = Clickwrap.config.anchor_event_history_with
  attempted_at = Clickwrap.now
  snapshot = ChainSnapshot.new(
    chain_scope: event.chain_scope,
    sequence: event.chain_sequence,
    last_event_id: event.id,
    last_event_digest: event.event_digest
  )
  publication = normalized_result(adapter.anchor(snapshot))
  verification = if publication["anchored"] == true
                   # Verification receives the exact publication result
                   # as well as the expected chain head. An adapter that
                   # merely re-reads "some current head" cannot prove the
                   # reference stored beside this attestation is the one
                   # it actually checked.
                   normalized_result(adapter.verify(publication, snapshot))
                 else
                   { "checked" => false, "verified" => false,
                     "detail" => publication["detail"] }
                 end

  record!(
    kind: "event_anchor",
    state: attestation_state(publication["anchored"], verification),
    adapter: adapter,
    attempted_at: attempted_at,
    provider_result: publication,
    verification: verification,
    provider_reference: publication["reference"],
    provider_reported_at: parse_time(publication["published_at"]),
    chain_scope: event.chain_scope,
    chain_sequence: event.chain_sequence
  )
rescue StandardError => error
  record_exception(kind: "event_anchor", adapter: adapter,
                   attempted_at: attempted_at, error: error,
                   chain_scope: event.chain_scope, chain_sequence: event.chain_sequence)
end

#attest_after_commitObject



22
23
24
25
# File 'lib/clickwrap/integrity/attestor.rb', line 22

def attest_after_commit
  timestamp_event if Clickwrap.config.timestamp_receipts_with
  anchor_event if Clickwrap.config.anchor_event_history_with && event.chain_scope.present?
end

#timestamp_eventObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/clickwrap/integrity/attestor.rb', line 27

def timestamp_event
  adapter = Clickwrap.config.timestamp_receipts_with
  attempted_at = Clickwrap.now
  token = adapter.timestamp(event.event_digest)
  token_body = normalized_result(token)
  token_digest = token_body["digest"]

  unless token_digest.to_s == event.event_digest.to_s
    return record!(
      kind: "third_party_timestamp", state: "failed", adapter: adapter,
      attempted_at: attempted_at, provider_result: token_body,
      verification: { "checked" => false, "verified" => false,
                      "detail" => "The provider result named a different digest." }
    )
  end

  verification = if token_body["issued"] == true
                   normalized_result(adapter.verify(token_body["token"], event.event_digest))
                 else
                   { "checked" => false, "verified" => false,
                     "detail" => token_body["detail"] }
                 end

  record!(
    kind: "third_party_timestamp",
    state: attestation_state(token_body["issued"], verification),
    adapter: adapter,
    attempted_at: attempted_at,
    provider_result: token_body,
    verification: verification,
    provider_reference: token_body["token_reference"],
    provider_reported_at: parse_time(token_body["provider_reported_time"])
  )
rescue StandardError => error
  record_exception(kind: "third_party_timestamp", adapter: adapter,
                   attempted_at: attempted_at, error: error)
end