Skip to content

Commit f6c38a8

Browse files
erskingardneryukibtc
authored andcommitted
mls: drop NostrMls::create_key_package method
- Drop event creation method to keep interface sync
1 parent c04276a commit f6c38a8

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

crates/nostr-mls/examples/mls_memory.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ async fn main() -> Result<()> {
3030

3131
// Create key package for Bob
3232
// This would be published to the Nostr network for other users to find
33-
let bob_key_package_event: Event = bob_nostr_mls
34-
.create_key_package(&bob_keys, [relay_url.clone()])
35-
.await?;
33+
let (bob_key_package_encoded, tags) =
34+
bob_nostr_mls.create_key_package_for_event(&bob_keys.public_key(), [relay_url.clone()])?;
3635

36+
let bob_key_package_event = EventBuilder::new(Kind::MlsKeyPackage, bob_key_package_encoded)
37+
.tags(tags)
38+
.build(bob_keys.public_key())
39+
.sign(&bob_keys)
40+
.await?;
3741
// ================================
3842
// We're now acting as Alice
3943
// ================================

crates/nostr-mls/examples/mls_sqlite.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ async fn main() -> Result<()> {
3636

3737
// Create key package for Bob
3838
// This would be published to the Nostr network for other users to find
39-
let bob_key_package_event: Event = bob_nostr_mls
40-
.create_key_package(&bob_keys, [relay_url.clone()])
39+
let (bob_key_package_encoded, tags) =
40+
bob_nostr_mls.create_key_package_for_event(&bob_keys.public_key(), [relay_url.clone()])?;
41+
42+
let bob_key_package_event = EventBuilder::new(Kind::MlsKeyPackage, bob_key_package_encoded)
43+
.tags(tags)
44+
.build(bob_keys.public_key())
45+
.sign(&bob_keys)
4146
.await?;
4247

4348
// ================================

crates/nostr-mls/src/key_packages.rs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Nostr MLS Key Packages
22
33
use nostr::util::hex;
4-
use nostr::{Event, EventBuilder, Kind, NostrSigner, PublicKey, RelayUrl, Tag, TagKind};
4+
use nostr::{Event, Kind, PublicKey, RelayUrl, Tag, TagKind};
55
use nostr_mls_storage::NostrMlsStorageProvider;
66
use openmls::key_packages::KeyPackage;
77
use openmls::prelude::*;
@@ -18,20 +18,29 @@ where
1818
{
1919
/// Creates a key package for a Nostr event.
2020
///
21-
/// This function generates a key package that can be used in a Nostr event to join an MLS group.
21+
/// This function generates a hex-encoded key package that is used as the content field of a kind:443 Nostr event.
2222
/// The key package contains the user's credential and capabilities required for MLS operations.
2323
///
2424
/// # Returns
2525
///
26-
/// A hex-encoded string containing the serialized key package on success, or a KeyPackageError on failure.
26+
/// A tuple containing:
27+
/// * A hex-encoded string containing the serialized key package
28+
/// * A tuple of tags for the Nostr event
2729
///
2830
/// # Errors
2931
///
3032
/// This function will return an error if:
3133
/// * It fails to generate the credential and signature keypair
3234
/// * It fails to build the key package
3335
/// * It fails to serialize the key package
34-
fn create_key_package_for_event(&self, public_key: &PublicKey) -> Result<String, Error> {
36+
pub fn create_key_package_for_event<I>(
37+
&self,
38+
public_key: &PublicKey,
39+
relays: I,
40+
) -> Result<(String, [Tag; 5]), Error>
41+
where
42+
I: IntoIterator<Item = RelayUrl>,
43+
{
3544
let (credential, signature_keypair) = self.generate_credential_with_key(public_key)?;
3645

3746
let capabilities: Capabilities = self.capabilities();
@@ -46,37 +55,20 @@ where
4655
credential,
4756
)?;
4857

49-
// serialize the key package, then encode it to hex and put it in the content field
5058
let key_package_serialized = key_package_bundle.key_package().tls_serialize_detached()?;
5159

52-
Ok(hex::encode(key_package_serialized))
53-
}
54-
55-
/// Create key package [`Event`]
56-
pub async fn create_key_package<T, I>(&self, signer: &T, relays: I) -> Result<Event, Error>
57-
where
58-
T: NostrSigner,
59-
I: IntoIterator<Item = RelayUrl>,
60-
{
61-
let public_key: PublicKey = signer.get_public_key().await?;
62-
63-
let serialized_key_package: String = self.create_key_package_for_event(&public_key)?;
64-
65-
let ciphersuite: String = self.ciphersuite_value().to_string();
66-
let extensions: String = self.extensions_value();
67-
6860
let tags = [
6961
Tag::custom(TagKind::MlsProtocolVersion, ["1.0"]),
70-
Tag::custom(TagKind::MlsCiphersuite, [ciphersuite]),
71-
Tag::custom(TagKind::MlsExtensions, [extensions]),
62+
Tag::custom(
63+
TagKind::MlsCiphersuite,
64+
[self.ciphersuite_value().to_string()],
65+
),
66+
Tag::custom(TagKind::MlsExtensions, [self.extensions_value()]),
7267
Tag::relays(relays),
7368
Tag::protected(),
7469
];
7570

76-
let builder: EventBuilder =
77-
EventBuilder::new(Kind::MlsKeyPackage, serialized_key_package).tags(tags);
78-
79-
Ok(builder.sign(signer).await?)
71+
Ok((hex::encode(key_package_serialized), tags))
8072
}
8173

8274
/// Parses and validates a hex-encoded key package.
@@ -196,10 +188,11 @@ mod tests {
196188
let test_pubkey =
197189
PublicKey::from_hex("884704bd421671e01c13f854d2ce23ce2a5bfe9562f4f297ad2bc921ba30c3a6")
198190
.unwrap();
191+
let relays = vec![RelayUrl::parse("wss://relay.example.com").unwrap()];
199192

200193
// Create key package
201-
let key_package_hex = nostr_mls
202-
.create_key_package_for_event(&test_pubkey)
194+
let (key_package_hex, tags) = nostr_mls
195+
.create_key_package_for_event(&test_pubkey, relays.clone())
203196
.expect("Failed to create key package");
204197

205198
// Create new instance for parsing
@@ -212,6 +205,22 @@ mod tests {
212205

213206
// Verify the key package has the expected properties
214207
assert_eq!(key_package.ciphersuite(), DEFAULT_CIPHERSUITE);
208+
209+
assert_eq!(tags.len(), 5);
210+
assert_eq!(tags[0].kind(), TagKind::MlsProtocolVersion);
211+
assert_eq!(tags[1].kind(), TagKind::MlsCiphersuite);
212+
assert_eq!(tags[2].kind(), TagKind::MlsExtensions);
213+
assert_eq!(tags[3].kind(), TagKind::Relays);
214+
assert_eq!(tags[4].kind(), TagKind::Protected);
215+
216+
assert_eq!(
217+
tags[3].content().unwrap(),
218+
relays
219+
.iter()
220+
.map(|r| r.to_string())
221+
.collect::<Vec<_>>()
222+
.join(",")
223+
);
215224
}
216225

217226
#[test]
@@ -221,9 +230,11 @@ mod tests {
221230
PublicKey::from_hex("884704bd421671e01c13f854d2ce23ce2a5bfe9562f4f297ad2bc921ba30c3a6")
222231
.unwrap();
223232

233+
let relays = vec![RelayUrl::parse("wss://relay.example.com").unwrap()];
234+
224235
// Create and parse key package
225-
let key_package_hex = nostr_mls
226-
.create_key_package_for_event(&test_pubkey)
236+
let (key_package_hex, _) = nostr_mls
237+
.create_key_package_for_event(&test_pubkey, relays.clone())
227238
.expect("Failed to create key package");
228239

229240
// Create new instance for parsing and deletion

0 commit comments

Comments
 (0)