WIP. TODO: RemoteIdentity#verify
This commit is contained in:
parent
2805bdf78c
commit
3226b1515c
|
@ -37,6 +37,7 @@ module Pigeon
|
|||
BLCK_NS = "blocked"
|
||||
MESG_NS = "messages"
|
||||
DEPTH_INDEX_NS = "messages.by_depth"
|
||||
COUNT_INDEX_NS = "messages.count"
|
||||
|
||||
# ^ Internal namespaces for PStore keys
|
||||
|
||||
|
|
|
@ -3,7 +3,8 @@ require "digest"
|
|||
module Pigeon
|
||||
class Message
|
||||
attr_reader :author, :kind, :body, :signature, :depth, :prev
|
||||
|
||||
class VerificationError < StandardError; end
|
||||
VERFIY_ERROR = "Expected field %s to equal %s. Got: %s"
|
||||
# Author a new message.
|
||||
def self.publish(draft, author: LocalIdentity.current)
|
||||
msg = self.new(author: LocalIdentity.current,
|
||||
|
@ -37,16 +38,36 @@ module Pigeon
|
|||
end
|
||||
|
||||
def verify!
|
||||
verify_depth
|
||||
verify_prev
|
||||
verify_depth_prev_and_depth
|
||||
verify_signature
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def verify_depth; raise "WIP"; end
|
||||
def verify_prev; raise "WIP"; end
|
||||
def verify_signature; raise "WIP"; end
|
||||
def assert(field, actual, expected)
|
||||
unless actual == expected
|
||||
message = VERFIY_ERROR % [field, actual, expected]
|
||||
raise VerificationError, message
|
||||
end
|
||||
end
|
||||
|
||||
def verify_depth_prev_and_depth
|
||||
store = Pigeon::Storage.current
|
||||
count = store.get_message_count_for(self.author)
|
||||
if count == 0
|
||||
assert("depth", self.depth, 0)
|
||||
assert("prev", self.prev, nil)
|
||||
else
|
||||
# Make sure the `depth` prop is equal to count + 1
|
||||
# Make sure the `prev` prop is equal to
|
||||
# message_by_depth(author, (depth - 1))
|
||||
binding.pry
|
||||
end
|
||||
end
|
||||
|
||||
def verify_signature
|
||||
author.verify(signature, template.render_without_signature)
|
||||
end
|
||||
|
||||
def initialize(author:, kind:, body:, signature: nil)
|
||||
raise MISSING_BODY if body.empty?
|
||||
|
|
|
@ -9,8 +9,12 @@ module Pigeon
|
|||
attr_reader :public_key
|
||||
def initialize(multihash)
|
||||
b64 = Base64.urlsafe_encode64(multihash)
|
||||
|
||||
@public_key = [HEADER, b64, FOOTER].join("")
|
||||
end
|
||||
|
||||
def verify(signature, string)
|
||||
binding.pry
|
||||
raise "TODO"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,6 +19,10 @@ module Pigeon
|
|||
end
|
||||
end
|
||||
|
||||
def get_message_count_for(author_multihash)
|
||||
store.transaction(true) { store[COUNT_INDEX_NS][author_multihash] || 0 }
|
||||
end
|
||||
|
||||
def message_count
|
||||
store.transaction do
|
||||
store[MESG_NS].count
|
||||
|
@ -130,6 +134,7 @@ module Pigeon
|
|||
store[BLOB_NS] ||= {}
|
||||
store[CONF_NS] ||= {}
|
||||
store[MESG_NS] ||= {}
|
||||
store[COUNT_INDEX_NS] ||= {}
|
||||
store[BLCK_NS] ||= Set.new
|
||||
store[PEER_NS] ||= Set.new
|
||||
end
|
||||
|
@ -158,6 +163,8 @@ module Pigeon
|
|||
# not lying about its depth?
|
||||
key = [message.author.public_key, message.depth]
|
||||
store[DEPTH_INDEX_NS][key] = message.multihash
|
||||
store[COUNT_INDEX_NS][message.author] ||= 0
|
||||
store[COUNT_INDEX_NS][message.author] += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue