diff --git a/include/ircd/m/event/event.h b/include/ircd/m/event/event.h index 5c7605d62..4e0c07f79 100644 --- a/include/ircd/m/event/event.h +++ b/include/ircd/m/event/event.h @@ -40,11 +40,6 @@ namespace ircd::m size_t degree(const event &); bool before(const event &a, const event &b); - bool check_id(const event &) noexcept; - bool check_id(const event &, const string_view &room_version) noexcept; - id::event make_id(const event &, const string_view &version, id::event::buf &buf, const const_buffer &hash); - id::event make_id(const event &, const string_view &version, id::event::buf &buf); - json::object hashes(const mutable_buffer &, const event &); event signatures(const mutable_buffer &, const m::event &, const string_view &origin); event signatures(const mutable_buffer &, const m::event &); @@ -64,6 +59,11 @@ namespace ircd::m ed25519::sig sign(const event &, const ed25519::sk &); ed25519::sig sign(const event &, const string_view &origin); ed25519::sig sign(const event &); + + id::event make_id(const event &, const string_view &version, id::event::buf &buf, const const_buffer &hash); + id::event make_id(const event &, const string_view &version, id::event::buf &buf); + bool check_id(const event &, const string_view &room_version) noexcept; + bool check_id(const event &) noexcept; } /// diff --git a/matrix/event.cc b/matrix/event.cc index 816e85f4d..27dd70de8 100644 --- a/matrix/event.cc +++ b/matrix/event.cc @@ -23,6 +23,122 @@ ircd::m::event::max_size { "default", 65507L }, }; +bool +ircd::m::check_id(const event &event) +noexcept +{ + if(!event.event_id) + return false; + + const string_view &version + { + event.event_id.version() + }; + + return check_id(event, version); +} + +bool +ircd::m::check_id(const event &event, + const string_view &room_version) +noexcept try +{ + assert(event.event_id); + const auto &version + { + room_version?: event.event_id.version() + }; + + char buf[64]; + const event::id &check_id + { + version == "1" || version == "2"? + event::id{json::get<"event_id"_>(event)}: + + version == "3"? + event::id{event::id::v3{buf, event}}: + + event::id{event::id::v4{buf, event}} + }; + + return event.event_id == check_id; +} +catch(const std::exception &e) +{ + log::error + { + "m::check_id() :%s", e.what() + }; + + return false; +} +catch(...) +{ + assert(0); + return false; +} + +ircd::m::id::event +ircd::m::make_id(const event &event, + const string_view &version, + id::event::buf &buf) +{ + if(version == "1" || version == "2") + { + const crh::sha256::buf hash{event}; + return make_id(event, version, buf, hash); + } + + if(version == "3") + return event::id::v3 + { + buf, event + }; + + return event::id::v4 + { + buf, event + }; +} + +ircd::m::id::event +ircd::m::make_id(const event &event, + const string_view &version, + id::event::buf &buf, + const const_buffer &hash) +{ + char readable[b64encode_size(sha256::digest_size)]; + + if(version == "1" || version == "2") + { + const id::event ret + { + buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), at<"origin"_>(event) + }; + + buf.assigned(ret); + return ret; + } + else if(version == "3") + { + const id::event ret + { + buf, b64encode_unpadded(readable, hash), string_view{} + }; + + buf.assigned(ret); + return ret; + } + + const id::event ret + { + buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), string_view{} + }; + + buf.assigned(ret); + return ret; +} + ircd::json::object ircd::m::hashes(const mutable_buffer &out, const event &event) @@ -821,122 +937,6 @@ catch(const json::not_found &e) throw; } -ircd::m::id::event -ircd::m::make_id(const event &event, - const string_view &version, - id::event::buf &buf) -{ - if(version == "1" || version == "2") - { - const crh::sha256::buf hash{event}; - return make_id(event, version, buf, hash); - } - - if(version == "3") - return event::id::v3 - { - buf, event - }; - - return event::id::v4 - { - buf, event - }; -} - -ircd::m::id::event -ircd::m::make_id(const event &event, - const string_view &version, - id::event::buf &buf, - const const_buffer &hash) -{ - char readable[b64encode_size(sha256::digest_size)]; - - if(version == "1" || version == "2") - { - const id::event ret - { - buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), at<"origin"_>(event) - }; - - buf.assigned(ret); - return ret; - } - else if(version == "3") - { - const id::event ret - { - buf, b64encode_unpadded(readable, hash), string_view{} - }; - - buf.assigned(ret); - return ret; - } - - const id::event ret - { - buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), string_view{} - }; - - buf.assigned(ret); - return ret; -} - -bool -ircd::m::check_id(const event &event) -noexcept -{ - if(!event.event_id) - return false; - - const string_view &version - { - event.event_id.version() - }; - - return check_id(event, version); -} - -bool -ircd::m::check_id(const event &event, - const string_view &room_version) -noexcept try -{ - assert(event.event_id); - const auto &version - { - room_version?: event.event_id.version() - }; - - char buf[64]; - const event::id &check_id - { - version == "1" || version == "2"? - event::id{json::get<"event_id"_>(event)}: - - version == "3"? - event::id{event::id::v3{buf, event}}: - - event::id{event::id::v4{buf, event}} - }; - - return event.event_id == check_id; -} -catch(const std::exception &e) -{ - log::error - { - "m::check_id() :%s", e.what() - }; - - return false; -} -catch(...) -{ - assert(0); - return false; -} - bool ircd::m::before(const event &a, const event &b)