Function ssb_validate::message::validate_message_hash_chain [−][src]
pub fn validate_message_hash_chain<T: AsRef<[u8]>, U: AsRef<[u8]>>(
message_bytes: T,
previous_msg_bytes: Option<U>
) -> Result<()>
Check that a message is a valid message relative to the previous message.
It expects the messages to be the JSON encoded message of shape: {key: "", value: {...}}
This checks that:
- the sequence starts at one if it’s the first message
- the previous is correctly set to null if it’s the first message
- the sequence increments correctly
- the author has not changed
- the feed is not forked
- the actual hash matches the hash claimed in
key
This does not check:
- the signature. See ssb-verify-signatures which lets you to batch verification of signatures.
previous_msg_bytes
will be None
only when message_bytes
is the first message by that author.
Example
use ssb_validate::message::validate_message_hash_chain; let valid_message_1 = r##"{ "key": "%/v5mCnV/kmnVtnF3zXtD4tbzoEQo4kRq/0d/bgxP1WI=.sha256", "value": { "previous": null, "author": "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519", "sequence": 1, "timestamp": 1470186877575, "hash": "sha256", "content": { "type": "about", "about": "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519", "name": "Piet" }, "signature": "QJKWui3oyK6r5dH13xHkEVFhfMZDTXfK2tW21nyfheFClSf69yYK77Itj1BGcOimZ16pj9u3tMArLUCGSscqCQ==.sig.ed25519" }, "timestamp": 1571140551481 }"##; let valid_message_2 = r##"{ "key": "%kLWDux4wCG+OdQWAHnpBGzGlCehqMLfgLbzlKCvgesU=.sha256", "value": { "previous": "%/v5mCnV/kmnVtnF3zXtD4tbzoEQo4kRq/0d/bgxP1WI=.sha256", "author": "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519", "sequence": 2, "timestamp": 1470187292812, "hash": "sha256", "content": { "type": "about", "about": "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519", "image": { "link": "&MxwsfZoq7X6oqnEX/TWIlAqd6S+jsUA6T1hqZYdl7RM=.sha256", "size": 642763, "type": "image/png", "width": 512, "height": 512 } }, "signature": "j3C7Us3JDnSUseF4ycRB0dTMs0xC6NAriAFtJWvx2uyz0K4zSj6XL8YA4BVqv+AHgo08+HxXGrpJlZ3ADwNnDw==.sig.ed25519" }, "timestamp": 1571140551485 }"##; let result = validate_message_hash_chain(valid_message_2.as_bytes(), Some(valid_message_1)); assert!(result.is_ok());