+ self.check_payment_secret()?;
+
+ Ok(())
+ }
+
+ /// Checks that there is exactly one payment secret field
+ fn check_payment_secret(&self) -> Result<(), SemanticError> {
+ // "A writer MUST include exactly one `s` field."
+ let payment_secret_count = self.tagged_fields().filter(|&tf| match *tf {
+ TaggedField::PaymentSecret(_) => true,
+ _ => false,
+ }).count();
+ if payment_secret_count < 1 {
+ return Err(SemanticError::NoPaymentSecret);
+ } else if payment_secret_count > 1 {
+ return Err(SemanticError::MultiplePaymentSecrets);
+ }
+