Skip to content

Commit 3daeb11

Browse files
author
nostronaut
committed
MMR tag: add merkle mountain range tag
1 parent 2c98c20 commit 3daeb11

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

crates/nostr/src/event/builder.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::fmt;
77
#[cfg(not(target_arch = "wasm32"))]
88
use std::time::Instant;
99

10+
use bitcoin_hashes::sha256::Hash;
1011
#[cfg(target_arch = "wasm32")]
1112
use instant::Instant;
1213
use secp256k1::XOnlyPublicKey;
@@ -169,6 +170,47 @@ impl EventBuilder {
169170
tags.pop();
170171
}
171172
}
173+
174+
/// Build MMR [`Event`]
175+
pub fn to_mmr_event(
176+
self,
177+
keys: &Keys,
178+
prev_event_hash: Hash,
179+
prev_mmr_root: Hash,
180+
prev_event_pos: i64,
181+
) -> Result<Event, Error> {
182+
let pubkey: XOnlyPublicKey = keys.public_key();
183+
Ok(self
184+
.to_unsigned_mmr_event(pubkey, prev_event_hash, prev_mmr_root, prev_event_pos)
185+
.sign(keys)?)
186+
}
187+
188+
/// Build unsigned MMR [`Event`]
189+
pub fn to_unsigned_mmr_event(
190+
self,
191+
pubkey: XOnlyPublicKey,
192+
prev_event_id: Hash,
193+
prev_mmr_root: Hash,
194+
prev_event_pos: i64,
195+
) -> UnsignedEvent {
196+
let mut tags: Vec<Tag> = self.tags;
197+
tags.push(Tag::Mmr {
198+
prev_event_id,
199+
prev_mmr_root,
200+
prev_event_pos,
201+
});
202+
let created_at: Timestamp = Timestamp::now();
203+
let id = EventId::new(&pubkey, created_at, &self.kind, &tags, &self.content);
204+
// TODO verify if valid MMR append operation for vector commitment
205+
UnsignedEvent {
206+
id,
207+
pubkey,
208+
created_at,
209+
kind: self.kind,
210+
tags,
211+
content: self.content,
212+
}
213+
}
172214
}
173215

174216
impl EventBuilder {

crates/nostr/src/event/tag.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::fmt;
77
use core::num::ParseIntError;
88
use core::str::FromStr;
99

10+
use bitcoin_hashes::sha256::Hash;
1011
use secp256k1::schnorr::Signature;
1112
use secp256k1::XOnlyPublicKey;
1213
use serde::de::Error as DeserializerError;
@@ -242,6 +243,8 @@ pub enum TagKind {
242243
Lnurl,
243244
/// Name tag
244245
Name,
246+
/// Merkle mountain range
247+
Mmr,
245248
/// Custom tag kind
246249
Custom(String),
247250
}
@@ -275,6 +278,7 @@ impl fmt::Display for TagKind {
275278
Self::Amount => write!(f, "amount"),
276279
Self::Lnurl => write!(f, "lnurl"),
277280
Self::Name => write!(f, "name"),
281+
Self::Mmr => write!(f, "mmr"),
278282
Self::Custom(tag) => write!(f, "{tag}"),
279283
}
280284
}
@@ -313,6 +317,7 @@ where
313317
"amount" => Self::Amount,
314318
"lnurl" => Self::Lnurl,
315319
"name" => Self::Name,
320+
"mmr" => Self::Mmr,
316321
tag => Self::Custom(tag.to_string()),
317322
}
318323
}
@@ -370,6 +375,11 @@ pub enum Tag {
370375
Lnurl(String),
371376
Name(String),
372377
PublishedAt(Timestamp),
378+
Mmr {
379+
prev_event_id: Hash,
380+
prev_mmr_root: Hash,
381+
prev_event_pos: i64,
382+
},
373383
}
374384

375385
impl Tag {
@@ -420,6 +430,7 @@ impl Tag {
420430
Tag::Amount(..) => TagKind::Amount,
421431
Tag::Name(..) => TagKind::Name,
422432
Tag::Lnurl(..) => TagKind::Lnurl,
433+
Tag::Mmr { .. } => TagKind::Mmr,
423434
}
424435
}
425436
}
@@ -581,6 +592,11 @@ where
581592
conditions: Conditions::from_str(&tag[2])?,
582593
sig: Signature::from_str(&tag[3])?,
583594
}),
595+
TagKind::Mmr => Ok(Self::Mmr {
596+
prev_event_id: Hash::from_str(tag[1].as_str())?,
597+
prev_mmr_root: Hash::from_str(tag[2].as_str())?,
598+
prev_event_pos: i64::from_str(tag[3].as_str())?,
599+
}),
584600
_ => Ok(Self::Generic(tag_kind, tag[1..].to_vec())),
585601
}
586602
} else {
@@ -726,6 +742,18 @@ impl From<Tag> for Vec<String> {
726742
Tag::Lnurl(lnurl) => {
727743
vec![TagKind::Lnurl.to_string(), lnurl]
728744
}
745+
Tag::Mmr {
746+
prev_event_id,
747+
prev_mmr_root,
748+
prev_event_pos,
749+
} => {
750+
vec![
751+
TagKind::Mmr.to_string(),
752+
prev_event_id.to_string(),
753+
prev_mmr_root.to_string(),
754+
prev_event_pos.to_string(),
755+
]
756+
}
729757
}
730758
}
731759
}

0 commit comments

Comments
 (0)