mirror of
https://github.com/matrix-construct/construct
synced 2025-02-18 17:50:16 +01:00
ircd:Ⓜ️ Add event::hash(json::object) w/ branch from event.source.
This commit is contained in:
parent
a36f375162
commit
bb68dfc1e7
2 changed files with 46 additions and 22 deletions
|
@ -105,6 +105,7 @@ struct ircd::m::event
|
|||
static ed25519::sig sign(json::iov &event, const json::iov &content);
|
||||
static json::object signatures(const mutable_buffer &, json::iov &event, const json::iov &content);
|
||||
static sha256::buf hash(json::iov &event, const string_view &content);
|
||||
static sha256::buf hash(const json::object &);
|
||||
static json::object hashes(const mutable_buffer &, json::iov &event, const string_view &content);
|
||||
|
||||
json::object source; // Contextual availability only.
|
||||
|
|
|
@ -1405,6 +1405,41 @@ ircd::m::make_hashes(const mutable_buffer &out,
|
|||
return json::stringify(mutable_buffer{out}, hashes);
|
||||
}
|
||||
|
||||
ircd::sha256::buf
|
||||
ircd::m::event::hash(const json::object &event)
|
||||
try
|
||||
{
|
||||
static const size_t iov_max{json::iov::max_size};
|
||||
thread_local std::array<json::object::member, iov_max> member;
|
||||
|
||||
size_t i(0);
|
||||
for(const auto &m : event)
|
||||
{
|
||||
if(m.first == "signatures" ||
|
||||
m.first == "unsigned" ||
|
||||
m.first == "hashes")
|
||||
continue;
|
||||
|
||||
member.at(i++) = m;
|
||||
}
|
||||
|
||||
thread_local char buf[event::MAX_SIZE];
|
||||
const string_view reimage
|
||||
{
|
||||
json::stringify(buf, member.data(), member.data() + i)
|
||||
};
|
||||
|
||||
return sha256{reimage};
|
||||
}
|
||||
catch(const std::out_of_range &e)
|
||||
{
|
||||
throw m::BAD_JSON
|
||||
{
|
||||
"Object has more than %zu member properties.",
|
||||
json::iov::max_size
|
||||
};
|
||||
}
|
||||
|
||||
ircd::sha256::buf
|
||||
ircd::m::event::hash(json::iov &event,
|
||||
const string_view &content)
|
||||
|
@ -1420,32 +1455,20 @@ ircd::m::event::hash(json::iov &event,
|
|||
ircd::sha256::buf
|
||||
ircd::m::hash(const event &event)
|
||||
{
|
||||
if(event.source)
|
||||
return event::hash(event.source);
|
||||
|
||||
m::event event_{event};
|
||||
json::get<"signatures"_>(event_) = {};
|
||||
json::get<"hashes"_>(event_) = {};
|
||||
|
||||
thread_local char buf[event::MAX_SIZE];
|
||||
string_view preimage;
|
||||
|
||||
//TODO: tuple::keys::selection
|
||||
if(defined(json::get<"signatures"_>(event)) ||
|
||||
defined(json::get<"hashes"_>(event)))
|
||||
const string_view preimage
|
||||
{
|
||||
m::event event_{event};
|
||||
json::get<"signatures"_>(event_) = {};
|
||||
json::get<"hashes"_>(event_) = {};
|
||||
preimage =
|
||||
{
|
||||
stringify(buf, event_)
|
||||
};
|
||||
}
|
||||
else preimage =
|
||||
{
|
||||
stringify(buf, event)
|
||||
stringify(buf, event_)
|
||||
};
|
||||
|
||||
const sha256::buf hash
|
||||
{
|
||||
sha256{preimage}
|
||||
};
|
||||
|
||||
return hash;
|
||||
return sha256{preimage};
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
Loading…
Add table
Reference in a new issue