Skip to content

Commit

Permalink
Simplify generating recipient pubkeys
Browse files Browse the repository at this point in the history
Remove generate_recipient_pubkey and rename
generate_multiple_recipient_pubkeys to generate_recipient_pubkey.
  • Loading branch information
cygnet3 committed Mar 13, 2024
1 parent 9a247a8 commit cf5e629
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 44 deletions.
46 changes: 5 additions & 41 deletions src/sending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ impl From<SilentPaymentAddress> for String {
}
}

/// Create multiple outputs for a given set of silent payment recipients and their corresponding shared secrets.
/// Create outputs for a given set of silent payment recipients and their corresponding shared secrets.
/// When creating the outputs for a transaction, this function should be used to generate the output keys.
/// This function should only be used once per transaction! If used multiple times, address reuse may occur.
///
/// # Arguments
///
/// * `recipients` - A `Vec` of silent payment addresses to be paid.
/// * `partial_secret` - A `SecretKey` that represents the sum of the private keys of eligible inputs of the transaction multiplied by the hash of its outpoints.
/// * `partial_secret` - A `SecretKey` that represents the sum of the private keys of eligible inputs of the transaction multiplied by the input hash.
///
/// # Returns
///
Expand All @@ -131,9 +133,8 @@ impl From<SilentPaymentAddress> for String {
/// This function will return an error if:
///
/// * The recipients Vec contains a silent payment address with an incorrect format.
/// * The ecdh_shared_secrets does not contain a secret for every B_scan that is being paid to.
/// * Edge cases are hit during elliptic curve computation (extremely unlikely).
pub fn generate_multiple_recipient_pubkeys(
pub fn generate_recipient_pubkeys(
recipients: Vec<String>,
partial_secret: SecretKey,
) -> Result<HashMap<String, Vec<XOnlyPublicKey>>> {
Expand Down Expand Up @@ -174,40 +175,3 @@ pub fn generate_multiple_recipient_pubkeys(
}
Ok(result)
}

/// Create output for a single silent payment recipient using a shared secret.
/// This function is intended for the common use-case of paying a single silent payment address once.
/// For sending to multiple recipients, or sending to the same recipient multiple times,
/// use `create_multiple_recipient_pubkeys` instead.
///
/// # Arguments
///
/// * `recipient` - A `String` of the bech32m-encoded silent payment address to be paid.
/// * `partial_secret` - A `SecretKey` representing the private keys of the outputs to spend multiplied by the hash of its outpoints.
///
/// # Returns
///
/// If successful, the function returns a `Result` wrapping the taproot output as an `XOnlyPublicKey`.
///
/// # Errors
///
/// This function will return an error if:
///
/// * The recipient silent payment address has an incorrect format.
/// * Edge cases are hit during elliptic curve computation (extremely unlikely).
pub fn generate_recipient_pubkey(
recipient: String,
partial_secret: SecretKey,
) -> Result<XOnlyPublicKey> {
let res = generate_multiple_recipient_pubkeys(vec![recipient], partial_secret)?;

let output = res
.into_values()
.next()
.expect("Map should always have 1 value");

Ok(output
.into_iter()
.next()
.expect("Vec should always be of length 1"))
}
5 changes: 2 additions & 3 deletions tests/vector_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod tests {
use silentpayments::receiving::Receiver;

#[cfg(feature = "sending")]
use silentpayments::sending::generate_multiple_recipient_pubkeys;
use silentpayments::sending::generate_recipient_pubkeys;
use silentpayments::{
utils::hash_outpoints,
utils::receiving::{recipient_calculate_shared_secret, recipient_calculate_tweak_data},
Expand Down Expand Up @@ -84,8 +84,7 @@ mod tests {

let partial_secret = sender_calculate_partial_secret(a_sum, input_hash).unwrap();

let outputs =
generate_multiple_recipient_pubkeys(silent_addresses, partial_secret).unwrap();
let outputs = generate_recipient_pubkeys(silent_addresses, partial_secret).unwrap();

for output_pubkeys in &outputs {
for pubkey in output_pubkeys.1 {
Expand Down

0 comments on commit cf5e629

Please sign in to comment.