[−][src]Function ssb_publish::publish
pub fn publish<T, P>(
content: Content<T>,
previous_msg_value_bytes: Option<P>,
public_key: &PublicKey,
secret_key: &SecretKey,
timestamp: f64
) -> Result<Vec<u8>, Error> where
T: Serialize,
P: AsRef<[u8]>,
Publish a new message.
- Bring your own ed25519 keys (as bytes)
- You choose whether or not to publish actual timestamps or just random numbers.
Returns a tuple of:
- the new message as a Vec of bytes. This is the message value with keys
previous
,sequence
,content
etc. - the [Multihash] (ssb message key) of the new message
You may use this to publish public or private messages.
If you want to publish private messages, you'll have to encrypt them first and wrap them in
the Content::Encrypted
enum variant.
Example
use ssb_publish::{publish, Content}; use ssb_multiformats::multikey::Multikey; use ssb_validate::validate_message_hash_chain; use ssb_verify_signatures::verify_message; use serde::{Deserialize, Serialize}; use ssb_crypto::{generate_longterm_keypair}; let (pk, sk) = generate_longterm_keypair(); #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "type")] #[serde(rename = "contact")] struct Contact { contact: Multikey, following: bool, blocking: bool, } // Create a new `Contact` message. let contact = Contact { contact: Multikey::from_legacy( b"@9Zf0se86PotjNqaOt9ue8BNBLkGVLQcLNDw/pRQHY3U=.ed25519", ) .unwrap() .0, following: true, blocking: false, }; let content = Content::Plain(contact); let msg = publish::<_, &[u8]>( content, None, &pk, &sk, 0.0, ) .unwrap(); let is_valid = validate_message_hash_chain::<_, &[u8]>(&msg, None).is_ok(); let is_verified = verify_message(&msg).is_ok(); assert!(is_valid); assert!(is_verified);