From ed5af1fdbdd7aca5f9e9379a87d2fb125fcd46ea Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 26 Jun 2019 04:15:56 -0700 Subject: [PATCH] ircd::m::id: Add class representations for event::v3 and event::v4. --- include/ircd/m/id.h | 21 +++++++++++++ ircd/m_id.cc | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/include/ircd/m/id.h b/include/ircd/m/id.h index cf2ab6793..c29efe317 100644 --- a/include/ircd/m/id.h +++ b/include/ircd/m/id.h @@ -157,6 +157,9 @@ struct ircd::m::id::user struct ircd::m::id::event :ircd::m::id { + struct v3; + struct v4; + using buf = m::id::buf; using closure = std::function; using closure_bool = std::function; @@ -171,6 +174,24 @@ struct ircd::m::id::event event() = default; }; +/// Version 3 event_id +struct ircd::m::id::event::v3 +:ircd::m::id::event +{ + v3(const mutable_buffer &out, const json::object &); + v3(const string_view &id); + v3() = default; +}; + +/// Version 4 event_id +struct ircd::m::id::event::v4 +:ircd::m::id::event +{ + v4(const mutable_buffer &out, const json::object &); + v4(const string_view &id); + v4() = default; +}; + /// (Appendix 4.2.2) Room IDs and Event IDs /// /// A room has exactly one room ID. A room ID has the format: diff --git a/ircd/m_id.cc b/ircd/m_id.cc index 8f238cd49..56d156a5d 100644 --- a/ircd/m_id.cc +++ b/ircd/m_id.cc @@ -675,6 +675,79 @@ const "1"; } +// +// id::event::v3 +// + +ircd::m::id::event::v3::v3(const string_view &id) +:id::event{id} +{ + if(unlikely(version() != "3")) + throw m::INVALID_MXID + { + "Not a version 3 event mxid" + }; +} + +ircd::m::id::event::v3::v3(const mutable_buffer &out, + const json::object &event) +:v3{[&out, event] +{ + if(unlikely(buffer::size(out) < 44)) + throw std::out_of_range + { + "Output buffer insufficient for v3 event_id" + }; + + const sha256::buf hash + { + sha256{event} + }; + + out[0] = '$'; + return b64encode_unpadded(out + 1, hash); +}()} +{ +} + +// +// id::event::v4 +// + +ircd::m::id::event::v4::v4(const string_view &id) +:id::event{id} +{ + if(unlikely(version() != "4")) + throw m::INVALID_MXID + { + "Not a version 4 event mxid" + }; +} + +ircd::m::id::event::v4::v4(const mutable_buffer &out, + const json::object &event) +:v4{[&out, event] +{ + if(unlikely(buffer::size(out) < 44)) + throw std::out_of_range + { + "Output buffer insufficient for v4 event_id" + }; + + const sha256::buf hash + { + sha256{event} + }; + + out[0] = '$'; + string_view ret; + ret = b64encode_unpadded(out + 1, hash); + ret = b64tob64url(out + 1, ret); + return ret; +}()} +{ +} + // // util //