Class: SelfSDK::SignatureGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/signature_graph.rb

Instance Method Summary collapse

Constructor Details

#initialize(history) ⇒ SignatureGraph

Returns a new instance of SignatureGraph.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/signature_graph.rb', line 98

def initialize(history)
  @root = nil
  @keys = Hash.new
  @devices = Hash.new
  @signatures = Hash.new
  @operations = Array.new
  @recovery_key = nil

  history.each do |operation|
    execute(operation)
  end
end

Instance Method Details

#execute(operation) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/signature_graph.rb', line 123

def execute(operation)
  op = Operation.new(operation)

  raise "operation sequence is out of order" if op.sequence != @operations.length

  if op.sequence > 0
    if @signatures[op.previous] != op.sequence - 1
      raise "operation previous signature does not match"
    end

    if @operations[op.sequence - 1].timestamp >= op.timestamp
      raise "operation timestamp occurs before previous operation"
    end

    sk = @keys[op.signing_key]

    raise "operation specifies a signing key that does not exist" if sk.nil?

    if sk.revoked? && op.timestamp > sk.revoked
      raise "operation was signed by a key that was revoked at the time of signing"
    end

    if sk.type == KEY_TYPE_RECOVERY && op.revokes(op.signing_key) != true
      raise "account recovery operation does not revoke the current active recovery key"
    end
  end

  execute_actions(op)

  sk = @keys[op.signing_key]

  raise "operation specifies a signing key that does not exist" if sk.nil?

  if op.timestamp < sk.created || sk.revoked? && op.timestamp > sk.revoked
    raise "operation was signed with a key that was revoked"
  end

  sig = Base64.urlsafe_decode64(op.jws[:signature])

  sk.public_key.verify(sig, "#{op.jws[:protected]}.#{op.jws[:payload]}")

  has_valid_key = false

  @keys.each do |kid, k|
    has_valid_key = true unless k.revoked?
  end

  raise "signature graph does not contain any active or valid keys" unless has_valid_key
  raise "signature graph does not contain a valid recovery key" if @recovery_key.nil?
  raise "signature graph does not contain a valid recovery key" if @recovery_key.revoked?

  @operations.push(op)
  @signatures[op.jws[:signature]] = op.sequence
end

#key_by_device(did) ⇒ Object



117
118
119
120
121
# File 'lib/signature_graph.rb', line 117

def key_by_device(did)
  k = @devices[did]
  raise "key not found" if k.nil?
  k
end

#key_by_id(kid) ⇒ Object



111
112
113
114
115
# File 'lib/signature_graph.rb', line 111

def key_by_id(kid)
  k = @keys[kid]
  raise "key not found" if k.nil?
  k
end