From 92d1a4c90aae8958aebb1d2351fb9a960f59fb7a Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Sun, 11 Oct 2026 23:47:51 +0300 Subject: [PATCH 1/6] feat(standards): Add ``Message`` notes --- standards/src/wallet/keys/mod.rs | 11 +++++++ standards/src/wallet/notes/content.rs | 44 +++++++++++++++++++++++++++ wallet/sdk/src/scanners/chain.rs | 1 + 3 files changed, 56 insertions(+) diff --git a/standards/src/wallet/keys/mod.rs b/standards/src/wallet/keys/mod.rs index 3744973..0c04fa3 100644 --- a/standards/src/wallet/keys/mod.rs +++ b/standards/src/wallet/keys/mod.rs @@ -41,6 +41,17 @@ pub(crate) fn deterministically_derive_seed_and_nonce( let e3: [u8; 8] = e3.into(); let seed: [u8; 32] = [e0, e1, e2, e3].concat().try_into().unwrap(); + (seed, e4) + } + NoteContent::Message(m) => { + let combined = Tip5::hash_pair(m.sender_randomness, Tip5::hash(&m.message)); + let [e0, e1, e2, e3, e4] = combined.values(); + let e0: [u8; 8] = e0.into(); + let e1: [u8; 8] = e1.into(); + let e2: [u8; 8] = e2.into(); + let e3: [u8; 8] = e3.into(); + let seed = [e0, e1, e2, e3].concat().try_into().unwrap(); + (seed, e4) } } diff --git a/standards/src/wallet/notes/content.rs b/standards/src/wallet/notes/content.rs index d9a79d5..9c64f4f 100644 --- a/standards/src/wallet/notes/content.rs +++ b/standards/src/wallet/notes/content.rs @@ -41,9 +41,28 @@ impl UtxoContent { } } +/// Text message payload. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, BFieldCodec)] +pub struct MessageContent { + pub message: Vec, + pub sender_randomness: Digest, +} + +impl MessageContent { + pub const DISCRIMINANT: u64 = 1; + + pub fn new(message: impl Into, sender_randomness: Digest) -> Self { + Self { + message: message.into().into_bytes(), + sender_randomness, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub enum NoteContent { Utxo(UtxoContent), + Message(MessageContent), } impl NoteContent { @@ -51,6 +70,7 @@ impl NoteContent { pub fn discriminant(&self) -> u64 { match self { Self::Utxo(_) => UtxoContent::DISCRIMINANT, + Self::Message(_) => MessageContent::DISCRIMINANT, } } } @@ -61,12 +81,19 @@ impl From for NoteContent { } } +impl From for NoteContent { + fn from(message: MessageContent) -> Self { + Self::Message(message) + } +} + impl BFieldCodec for NoteContent { type Error = NoteContentError; fn encode(&self) -> Vec { let (discriminant, mut payload) = match self { Self::Utxo(u) => (UtxoContent::DISCRIMINANT, u.encode()), + Self::Message(message) => (MessageContent::DISCRIMINANT, message.encode()), }; let mut out = Vec::with_capacity(1 + payload.len()); @@ -87,6 +114,11 @@ impl BFieldCodec for NoteContent { UtxoContent::decode(rest).map_err(|e| NoteContentError::Decode(e.into()))?; Ok(Box::new(Self::Utxo(*content))) } + d if d == MessageContent::DISCRIMINANT => { + let message = + MessageContent::decode(rest).map_err(|e| NoteContentError::Decode(e.into()))?; + Ok(Box::new(Self::Message(*message))) + } _ => Err(NoteContentError::UnknownDiscriminant(discriminant)), } } @@ -139,4 +171,16 @@ mod tests { NoteContent::decode(&[BFieldElement::new(UtxoContent::DISCRIMINANT)]).unwrap_err(); assert!(matches!(err, NoteContentError::Decode(_))); } + + #[test] + fn message_content_round_trips_through_note_content() { + let message = MessageContent::new("Hello, Nyks!", Digest::default()); + let content: NoteContent = message.clone().into(); + + assert_eq!(content.discriminant(), MessageContent::DISCRIMINANT); + assert_eq!(content, NoteContent::Message(message)); + let encoded = content.encode(); + assert_eq!(encoded[0], BFieldElement::new(MessageContent::DISCRIMINANT)); + assert_eq!(*NoteContent::decode(&encoded).unwrap(), content); + } } diff --git a/wallet/sdk/src/scanners/chain.rs b/wallet/sdk/src/scanners/chain.rs index 0d6174a..d9f1323 100644 --- a/wallet/sdk/src/scanners/chain.rs +++ b/wallet/sdk/src/scanners/chain.rs @@ -278,6 +278,7 @@ impl ChainScanner { utxo, sender_randomness, }) => Some((utxo, sender_randomness, receiver_preimage)), + NoteContent::Message(_) => None, }) // A third party can craft a note that decrypts under this key but // whose UTXO isn't actually spendable by us. From eb64ccdce9c00811d47c29869cc9c2cb5cf6b733 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Mon, 12 Oct 2026 18:19:18 +0300 Subject: [PATCH 2/6] feat(composer): On-chain graffiti by reserved first announcement --- composer/src/composer/prover/transaction.rs | 1 + composer/src/core/args.rs | 18 ++++++++++++++++++ wallet/core/src/transaction/builder/mod.rs | 15 ++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/composer/src/composer/prover/transaction.rs b/composer/src/composer/prover/transaction.rs index 3e19be3..351426f 100644 --- a/composer/src/composer/prover/transaction.rs +++ b/composer/src/composer/prover/transaction.rs @@ -130,6 +130,7 @@ impl TransactionProver { let transaction = TransactionBuilder::new() .inputs(TxInputList::empty()) .outputs(composer_outputs.clone()) + .custom_announcements(self.args.announcement().into_iter().collect()) .coinbase(coinbase_amount) .fee(guesser_fee) .timestamp(timestamp) diff --git a/composer/src/core/args.rs b/composer/src/core/args.rs index a2cc6a6..b98aff3 100644 --- a/composer/src/core/args.rs +++ b/composer/src/core/args.rs @@ -1,13 +1,18 @@ use clap::Parser; use num_traits::CheckedSub; use num_traits::Zero; +use nyks_consensus::BFieldElement; use nyks_consensus::block::MINING_REWARD_TIME_LOCK_PERIOD; use nyks_consensus::network::Network; use nyks_consensus::proof_abstractions::timestamp::Timestamp; use nyks_consensus::tasm_lib::prelude::Digest; +use nyks_consensus::transaction::announcement::Announcement; use nyks_consensus::type_scripts::native_currency_amount::NativeCurrencyAmount; use nyks_standards::wallet::keys::address::Address; use nyks_standards::wallet::keys::address::Recipient; +use nyks_standards::wallet::notes::content::MessageContent; +use nyks_standards::wallet::notes::note::Note; +use nyks_standards::wallet::notes::note::PublicNote; use nyks_wallet_core::transaction::builder::output::TxOutput; use nyks_wallet_core::transaction::builder::output::TxOutputList; use nyks_wallet_core::transaction::utxo::notifications::UtxoNotificationMedium; @@ -33,6 +38,10 @@ pub struct Args { /// How much reward we leave for guessers. #[arg(long, default_value_t = 0.5f64)] pub guesser_fee_fraction: f64, + + /// Graffiti message to publish in the block announcement. + #[arg(long)] + pub graffiti: Option, } impl Args { @@ -124,4 +133,13 @@ impl Args { ret.into() } + + pub(crate) fn announcement(&self) -> Option { + self.graffiti.as_ref().map(|message| { + let content = MessageContent::new(message.clone(), Digest::default()); + let public_note = PublicNote::new(BFieldElement::new(0), content.into()); + + Announcement::from(&Note::from(public_note)) + }) + } } diff --git a/wallet/core/src/transaction/builder/mod.rs b/wallet/core/src/transaction/builder/mod.rs index c1577d7..d150adc 100644 --- a/wallet/core/src/transaction/builder/mod.rs +++ b/wallet/core/src/transaction/builder/mod.rs @@ -6,6 +6,7 @@ use num_traits::Zero; use nyks_consensus::mutator_set::mutator_set_accumulator::MutatorSetAccumulator; use nyks_consensus::proof_abstractions::timestamp::Timestamp; +use nyks_consensus::transaction::announcement::Announcement; use nyks_consensus::transaction::transaction_kernel::TransactionKernelProxy; use nyks_consensus::type_scripts::native_currency_amount::NativeCurrencyAmount; use thiserror::Error; @@ -35,6 +36,7 @@ pub struct TransactionBuilder { coinbase: Option, timestamp: Option, mutator_set_accumulator: Option, + announcements: Vec, } // Builder interface @@ -47,6 +49,7 @@ impl TransactionBuilder { coinbase: None, timestamp: None, mutator_set_accumulator: None, + announcements: Vec::new(), } } @@ -79,6 +82,12 @@ impl TransactionBuilder { self.mutator_set_accumulator = Some(msa); self } + + /// Add public announcements before output notification announcements. + pub fn custom_announcements(mut self, announcements: Vec) -> Self { + self.announcements = announcements; + self + } } impl TransactionBuilder { @@ -94,7 +103,11 @@ impl TransactionBuilder { let kernel = { let input_records = inputs.removal_records(&msa); let output_records = outputs.addition_records(); - let announcements = outputs.announcements(); + let announcements = self + .announcements + .into_iter() + .chain(outputs.announcements()) + .collect(); TransactionKernelProxy { inputs: input_records, From 20b25a76ca6fad50c9eea0b17e333a446329c651 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Mon, 12 Oct 2026 18:23:01 +0300 Subject: [PATCH 3/6] feat(composer): Add node version and a default to graffitis --- composer/src/core/args.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer/src/core/args.rs b/composer/src/core/args.rs index b98aff3..1ce2625 100644 --- a/composer/src/core/args.rs +++ b/composer/src/core/args.rs @@ -40,7 +40,7 @@ pub struct Args { pub guesser_fee_fraction: f64, /// Graffiti message to publish in the block announcement. - #[arg(long)] + #[arg(long, default_value = "nyks-composer")] pub graffiti: Option, } @@ -136,7 +136,8 @@ impl Args { pub(crate) fn announcement(&self) -> Option { self.graffiti.as_ref().map(|message| { - let content = MessageContent::new(message.clone(), Digest::default()); + let message = format!("{message} {}", env!("CARGO_PKG_VERSION")); + let content = MessageContent::new(message, Digest::default()); let public_note = PublicNote::new(BFieldElement::new(0), content.into()); Announcement::from(&Note::from(public_note)) From 69ce279d641299f3342fb29f300c616ae6babe6f Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Mon, 12 Oct 2026 18:45:13 +0300 Subject: [PATCH 4/6] test(standards): Move Message note tests to where it belongs --- standards/src/wallet/notes/content.rs | 28 +++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/standards/src/wallet/notes/content.rs b/standards/src/wallet/notes/content.rs index 9c64f4f..1e99319 100644 --- a/standards/src/wallet/notes/content.rs +++ b/standards/src/wallet/notes/content.rs @@ -155,6 +155,22 @@ mod tests { assert_eq!(decoded, note_content); } + #[test] + fn message_content_round_trips_through_note_content() { + let message = MessageContent::new("Hello, Nyks!", Digest::default()); + let content: NoteContent = message.clone().into(); + + assert_eq!(content.discriminant(), MessageContent::DISCRIMINANT); + assert_eq!(content, NoteContent::Message(message)); + + // Encoding prefixes the message payload with the message discriminant. + let encoded = content.encode(); + assert_eq!(encoded[0], BFieldElement::new(MessageContent::DISCRIMINANT)); + + // Decoding the encoded content reconstructs the original value. + assert_eq!(*NoteContent::decode(&encoded).unwrap(), content); + } + #[test] fn decode_rejects_invalid_sequences() { // Empty sequence: no discriminant element at all. @@ -171,16 +187,4 @@ mod tests { NoteContent::decode(&[BFieldElement::new(UtxoContent::DISCRIMINANT)]).unwrap_err(); assert!(matches!(err, NoteContentError::Decode(_))); } - - #[test] - fn message_content_round_trips_through_note_content() { - let message = MessageContent::new("Hello, Nyks!", Digest::default()); - let content: NoteContent = message.clone().into(); - - assert_eq!(content.discriminant(), MessageContent::DISCRIMINANT); - assert_eq!(content, NoteContent::Message(message)); - let encoded = content.encode(); - assert_eq!(encoded[0], BFieldElement::new(MessageContent::DISCRIMINANT)); - assert_eq!(*NoteContent::decode(&encoded).unwrap(), content); - } } From dcbf8fd5e6fca6a4353afe36eb201a5bec0781e2 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Thu, 15 Oct 2026 22:04:56 +0300 Subject: [PATCH 5/6] feat(standards): Switch to random suffix instead of seperate randomness --- composer/src/core/args.rs | 2 +- standards/Cargo.toml | 1 + standards/src/wallet/keys/mod.rs | 11 +++++------ standards/src/wallet/notes/content.rs | 17 +++++++++++++---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/composer/src/core/args.rs b/composer/src/core/args.rs index 1ce2625..e78b5f2 100644 --- a/composer/src/core/args.rs +++ b/composer/src/core/args.rs @@ -137,7 +137,7 @@ impl Args { pub(crate) fn announcement(&self) -> Option { self.graffiti.as_ref().map(|message| { let message = format!("{message} {}", env!("CARGO_PKG_VERSION")); - let content = MessageContent::new(message, Digest::default()); + let content = MessageContent::without_random_suffix(message); let public_note = PublicNote::new(BFieldElement::new(0), content.into()); Announcement::from(&Note::from(public_note)) diff --git a/standards/Cargo.toml b/standards/Cargo.toml index efed333..c51a471 100644 --- a/standards/Cargo.toml +++ b/standards/Cargo.toml @@ -22,6 +22,7 @@ bincode = "1.3" thiserror = "1.0.65" num-traits = "0.2" tiny-bip39 = "1.0" +rand = "0.10" [dev-dependencies] diff --git a/standards/src/wallet/keys/mod.rs b/standards/src/wallet/keys/mod.rs index 0c04fa3..b93cc07 100644 --- a/standards/src/wallet/keys/mod.rs +++ b/standards/src/wallet/keys/mod.rs @@ -24,10 +24,9 @@ pub(crate) fn network_hrp_char(network: Network) -> char { } } -/// Derive a seed and a nonce deterministically, in order to produce -/// deterministic announcements, since these are needed to be able to -/// reuse proofs for tests. These values are used in the encryption -/// step. +/// Derive a seed and nonce deterministically from note content so encrypting +/// the same content again produces the same announcement. These values are +/// used in the encryption step and allow proofs to be reused in tests. pub(crate) fn deterministically_derive_seed_and_nonce( content: &NoteContent, ) -> ([u8; 32], BFieldElement) { @@ -44,8 +43,8 @@ pub(crate) fn deterministically_derive_seed_and_nonce( (seed, e4) } NoteContent::Message(m) => { - let combined = Tip5::hash_pair(m.sender_randomness, Tip5::hash(&m.message)); - let [e0, e1, e2, e3, e4] = combined.values(); + let hash = Tip5::hash(&m.message); + let [e0, e1, e2, e3, e4] = hash.values(); let e0: [u8; 8] = e0.into(); let e1: [u8; 8] = e1.into(); let e2: [u8; 8] = e2.into(); diff --git a/standards/src/wallet/notes/content.rs b/standards/src/wallet/notes/content.rs index 1e99319..64002f8 100644 --- a/standards/src/wallet/notes/content.rs +++ b/standards/src/wallet/notes/content.rs @@ -45,16 +45,25 @@ impl UtxoContent { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, BFieldCodec)] pub struct MessageContent { pub message: Vec, - pub sender_randomness: Digest, } impl MessageContent { pub const DISCRIMINANT: u64 = 1; - pub fn new(message: impl Into, sender_randomness: Digest) -> Self { + /// Add an 8-byte random suffix for seed and nonce derivation. + /// The suffix is part of the message. + pub fn new(message: impl Into) -> Self { + let mut message = message.into().into_bytes(); + message.extend_from_slice(&rand::random::().to_be_bytes()); + Self { message } + } + + /// Create message content without a random suffix. + /// Encrypting identical content under the same key reuses its nonce. Use only + /// for unencrypted content or when nonce uniqueness is handled elsewhere. + pub fn without_random_suffix(message: impl Into) -> Self { Self { message: message.into().into_bytes(), - sender_randomness, } } } @@ -157,7 +166,7 @@ mod tests { #[test] fn message_content_round_trips_through_note_content() { - let message = MessageContent::new("Hello, Nyks!", Digest::default()); + let message = MessageContent::new("Hello, Nyks!"); let content: NoteContent = message.clone().into(); assert_eq!(content.discriminant(), MessageContent::DISCRIMINANT); From d1b454a71be3904fcd82c1cf613ba2f52a756ea6 Mon Sep 17 00:00:00 2001 From: KaffinPX Date: Thu, 15 Oct 2026 23:56:46 +0300 Subject: [PATCH 6/6] feat(standards): Add a space between suffix and message for MessageNote --- standards/src/wallet/notes/content.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/standards/src/wallet/notes/content.rs b/standards/src/wallet/notes/content.rs index 64002f8..33f3ba0 100644 --- a/standards/src/wallet/notes/content.rs +++ b/standards/src/wallet/notes/content.rs @@ -50,11 +50,12 @@ pub struct MessageContent { impl MessageContent { pub const DISCRIMINANT: u64 = 1; - /// Add an 8-byte random suffix for seed and nonce derivation. - /// The suffix is part of the message. + /// Also adds an 8-byte random suffix, separated from the message by a space. pub fn new(message: impl Into) -> Self { let mut message = message.into().into_bytes(); + message.push(b' '); message.extend_from_slice(&rand::random::().to_be_bytes()); + Self { message } }