Skip to main content

sign_event

Function sign_event 

Source
pub fn sign_event<K>(
    entity_id: &str,
    key_pair: &K,
    object: &mut BTreeMap<String, CanonicalJsonValue>,
    redaction_rules: &RedactionRules,
) -> Result<(), JsonError>
where K: KeyPair,
Available on crate feature signatures only.
Expand description

Compute and add the signature of the given event.

This adds or overwrites the signature for the given entity and key in the signatures object of the event.

When creating a new event, add_content_hash_to_event() should be called first.

§Parameters

  • entity_id: The identifier of the entity creating the signature. Generally this means a homeserver, e.g. “example.com”.
  • key_pair: The cryptographic key pair used to sign the event.
  • object: The event to be signed according to the Matrix specification.
  • redaction_rules: The redaction rules for the version of the event’s room.

§Errors

Returns an error if:

  • object contains a field called content that is not a JSON object.
  • object contains a field called signatures that is not a JSON object.
  • object is missing the type field or the field is not a JSON string.

§Examples

use ruma_common::CanonicalJsonObject;
use ruma_signatures::sign_event;

// Deserialize an event from JSON.
let mut event = serde_json::from_str(
    r#"{
        "room_id": "!x:domain",
        "sender": "@a:domain",
        "origin": "domain",
        "origin_server_ts": 1000000,
        "type": "X",
        "content": {},
        "prev_events": [],
        "auth_events": [],
        "depth": 3,
        "hashes": {
            "sha256": "5jM4wQpv6lnBo7CLIghJuHdW+s2CMBJPUOGOC89ncos"
        }
    }"#,
)?;

// Get the rules for the version of the current room.
let rules = room_version_rules();

// Sign the event with the key pair.
sign_event("domain", &key_pair, &mut event, &rules.redaction)?;

// The signature was added.
assert_eq!(
    event,
    serde_json::from_str::<CanonicalJsonObject>(
        r#"{
            "room_id": "!x:domain",
            "sender": "@a:domain",
            "origin": "domain",
            "origin_server_ts": 1000000,
            "type": "X",
            "content": {},
            "prev_events": [],
            "auth_events": [],
            "depth": 3,
            "hashes": {
                "sha256": "5jM4wQpv6lnBo7CLIghJuHdW+s2CMBJPUOGOC89ncos"
            },
            "signatures": {
                "domain": {
                    "ed25519:1": "PxOFMn6ORll8PFSQp0IRF6037MEZt3Mfzu/ROiT/gb/ccs1G+f6Ddoswez4KntLPBI3GKCGIkhctiK37JOy2Aw"
                }
            }
        }"#,
    )?
);