Skip to content

Commit 76ad098

Browse files
committed
Merge pull request #27 from 'refactor/psbt-derive-sp'
2 parents 7fa6d16 + 8e43b8d commit 76ad098

8 files changed

Lines changed: 2070 additions & 152 deletions

File tree

silentpayments/src/send/bip32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ impl XprivSilentPaymentSender {
5757
let partial_secret =
5858
create_silentpayment_partial_secret(&lex_min.bytes()?, &spks_with_keys)?;
5959

60-
create_silentpayment_scriptpubkeys(partial_secret, outputs)
60+
Ok(create_silentpayment_scriptpubkeys(partial_secret, outputs))
6161
}
6262
}

silentpayments/src/send/bip352.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ impl SpSender {
5050
let partial_secret =
5151
create_silentpayment_partial_secret(&lex_min.bytes()?, &spks_with_keys)?;
5252

53-
create_silentpayment_scriptpubkeys(partial_secret, outputs)
53+
Ok(create_silentpayment_scriptpubkeys(partial_secret, outputs))
5454
}
5555
}

silentpayments/src/send/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ pub enum SpSendError {
1616
IndexError(bitcoin::blockdata::transaction::OutputsIndexError),
1717
/// PSBT missing silent payment placeholder script
1818
MissingPlaceholderScript,
19+
/// Error while requesting keys
20+
KeyError,
21+
/// There are not enough silent payment derivations for all targeted outputs
22+
MissingDerivations,
23+
/// There are not enough outputs for the silent payments derived
24+
MissingOutputs,
1925
}
2026

2127
impl From<crate::LexMinError> for SpSendError {
@@ -49,11 +55,14 @@ impl std::fmt::Display for SpSendError {
4955
Self::Secp256k1Error(e) => write!(f, "Silent payment sending error: {e}"),
5056
Self::IndexError(e) => write!(f, "From PSBT: {e}"),
5157
Self::NoOutpoints(e) => write!(f, "Silent payment sending error: {e}"),
58+
Self::KeyError => write!(f, "Silent payment sending error: error while requesting private keys from key provider"),
5259
Self::MissingInputsForSharedSecretDerivation => write!(f, "No available inputs for shared secret derivation"),
5360
Self::MissingWitness => write!(
5461
f,
5562
"From PSBT, missing witness to get public key to derive silent payment output"
5663
),
64+
Self::MissingDerivations => write!(f, "From PSBT, there are not enough silent payment derivations for all targeted outputs"),
65+
Self::MissingOutputs => write!(f, "From PSBT, there are not enough outputs for the silent payments derived"),
5766
Self::MissingPrevout => write!(f, "From PSBT, unable to extract prevout script pubkey"),
5867
Self::MissingPlaceholderScript => write!(f, "From PSBT, missing placeholder script pubkey for associated silent payment recipient."),
5968
}

silentpayments/src/send/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn create_silentpayment_partial_secret(
7575
pub fn create_silentpayment_scriptpubkeys(
7676
partial_secret: SecretKey,
7777
outputs: &[SilentPaymentCode],
78-
) -> Result<HashMap<SilentPaymentCode, Vec<XOnlyPublicKey>>, SpSendError> {
78+
) -> HashMap<SilentPaymentCode, Vec<XOnlyPublicKey>> {
7979
let secp = Secp256k1::new();
8080

8181
// Cache to avoid recomputing ecdh shared secret for each B_scan and track the k to get the
@@ -117,7 +117,7 @@ pub fn create_silentpayment_scriptpubkeys(
117117
}
118118
}
119119

120-
Ok(payments)
120+
payments
121121
}
122122

123123
#[cfg(test)]
@@ -308,8 +308,7 @@ mod tests {
308308
fn test_create_silentpayment_spk_base() {
309309
let (partial_secret, sp_codes) = setup_test_data();
310310

311-
let result =
312-
create_silentpayment_scriptpubkeys(partial_secret, &sp_codes).expect("should succeed");
311+
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
313312

314313
assert_eq!(result.len(), 3);
315314

@@ -324,8 +323,7 @@ mod tests {
324323
let (partial_secret, _) = setup_test_data();
325324
let empty_outputs: Vec<SilentPaymentCode> = vec![];
326325

327-
let result = create_silentpayment_scriptpubkeys(partial_secret, &empty_outputs)
328-
.expect("should succeed with empty outputs");
326+
let result = create_silentpayment_scriptpubkeys(partial_secret, &empty_outputs);
329327

330328
assert!(result.is_empty());
331329
}
@@ -336,8 +334,7 @@ mod tests {
336334

337335
assert_eq!(sp_codes[0].scan, sp_codes[2].scan);
338336

339-
let result =
340-
create_silentpayment_scriptpubkeys(partial_secret, &sp_codes).expect("should succeed");
337+
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
341338

342339
// Get the pubkeys for codes with the same scan key
343340
let pubkeys_1 = &result[&sp_codes[0]];
@@ -353,10 +350,8 @@ mod tests {
353350
let (partial_secret, sp_codes) = setup_test_data();
354351

355352
// Generate sp_codes twice with the same inputs
356-
let result_1 =
357-
create_silentpayment_scriptpubkeys(partial_secret, &sp_codes).expect("should succeed");
358-
let result_2 =
359-
create_silentpayment_scriptpubkeys(partial_secret, &sp_codes).expect("should succeed");
353+
let result_1 = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
354+
let result_2 = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
360355

361356
// Results should be identical
362357
assert_eq!(result_1.len(), result_2.len());
@@ -373,8 +368,7 @@ mod tests {
373368
// Add a duplicate of the first code
374369
sp_codes.push(sp_codes[0].clone());
375370

376-
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes)
377-
.expect("should succeed with duplicates");
371+
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
378372

379373
// Should still have only 3 unique entries
380374
assert_eq!(result.len(), 3);
@@ -403,8 +397,7 @@ mod tests {
403397
sp_codes.push(code);
404398
}
405399

406-
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes)
407-
.expect("should succeed with many sp_codes");
400+
let result = create_silentpayment_scriptpubkeys(partial_secret, &sp_codes);
408401

409402
// Should have generated the correct number of sp_codes
410403
assert_eq!(result.len(), 100);
@@ -421,10 +414,8 @@ mod tests {
421414
let partial_secret_2 =
422415
SecretKey::from_str(PARTIAL_SECRET_2).expect("creating from constant");
423416

424-
let result_1 = create_silentpayment_scriptpubkeys(partial_secret_1, &sp_codes)
425-
.expect("should succeed");
426-
let result_2 = create_silentpayment_scriptpubkeys(partial_secret_2, &sp_codes)
427-
.expect("should succeed");
417+
let result_1 = create_silentpayment_scriptpubkeys(partial_secret_1, &sp_codes);
418+
let result_2 = create_silentpayment_scriptpubkeys(partial_secret_2, &sp_codes);
428419

429420
// Results should be different with different partial secrets
430421
for sp_code in &sp_codes {

silentpayments/src/send/psbt.rs

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)