use bc_components::{PrivateKeyBase, PublicKeyBase, ARID};
use anyhow::Result;
use crate::{known_values, Envelope, Function};
impl Envelope {
pub fn into_transaction_request_with_metadata(
self,
id: impl AsRef<ARID>,
sender: impl AsRef<PublicKeyBase>,
note: impl Into<String>,
date: Option<dcbor::Date>,
) -> Self {
self.into_request_with_metadata(id, note, date)
.add_assertion(known_values::SENDER_PUBLIC_KEY, sender.as_ref().clone())
}
pub fn into_signed_transaction_request_with_metadata(
self,
id: impl AsRef<ARID>,
sender: &PrivateKeyBase,
note: impl Into<String>,
date: Option<dcbor::Date>,
) -> Self {
self.into_transaction_request_with_metadata(id, sender.public_key(), note, date)
.sign(sender)
}
pub fn into_sealed_transaction_request_with_metadata(
self,
id: impl AsRef<ARID>,
sender: &PrivateKeyBase,
recipient: &PublicKeyBase,
note: impl Into<String>,
date: Option<dcbor::Date>,
) -> Result<Envelope> {
self.into_transaction_request_with_metadata(id, sender.public_key(), note, date)
.seal(sender, recipient)
}
pub fn into_transaction_request(
self,
id: impl AsRef<ARID>,
sender: impl AsRef<PublicKeyBase>,
) -> Self {
self.into_transaction_request_with_metadata(id, sender, "", None)
}
pub fn into_signed_transaction_request(
self,
id: impl AsRef<ARID>,
sender: &PrivateKeyBase,
) -> Self {
self.into_signed_transaction_request_with_metadata(id, sender, "", None)
}
pub fn into_sealed_transaction_request(
self,
id: impl AsRef<ARID>,
sender: &PrivateKeyBase,
recipient: &PublicKeyBase,
) -> Result<Envelope> {
self.into_sealed_transaction_request_with_metadata(id, sender, recipient, "", None)
}
}
impl Envelope {
pub fn parse_transaction_request_with_metadata(&self, expected_function: Option<&Function>) -> Result<(ARID, PublicKeyBase, Envelope, Function, String, Option<dcbor::Date>)> {
let (id, body, function, note, date) = self.parse_request_with_metadata(expected_function)?;
let sender: PublicKeyBase = self.extract_object_for_predicate(known_values::SENDER_PUBLIC_KEY)?;
Ok((id, sender, body, function, note, date))
}
pub fn parse_signed_transaction_request_with_metadata(&self, expected_function: Option<&Function>) -> Result<(ARID, PublicKeyBase, Envelope, Function, String, Option<dcbor::Date>)> {
let inner = self.unwrap_envelope()?;
let (id, sender, body, function, note, date) = inner.parse_transaction_request_with_metadata(expected_function)?;
self.verify_signature_from(&sender)?;
Ok((id, sender, body, function, note, date))
}
pub fn parse_sealed_transaction_request_with_metadata(&self, expected_function: Option<&Function>, recipient: &PrivateKeyBase) -> Result<(ARID, PublicKeyBase, Envelope, Function, String, Option<dcbor::Date>)> {
let signed = self.decrypt(recipient)?;
let (id, sender, body, function, note, date) = signed.parse_transaction_request_with_metadata(expected_function)?;
Ok((id, sender, body, function, note, date))
}
pub fn parse_transaction_request(&self, expected_function: Option<&Function>) -> Result<(ARID, PublicKeyBase, Envelope, Function)> {
let (id, body, function) = self.from_request(expected_function)?;
let sender: PublicKeyBase = self.extract_object_for_predicate(known_values::SENDER_PUBLIC_KEY)?;
Ok((id, sender, body, function))
}
pub fn parse_signed_transaction_request(&self, expected_function: Option<&Function>) -> Result<(ARID, PublicKeyBase, Envelope, Function)> {
let inner = self.unwrap_envelope()?;
let (id, sender, body, function) = inner.parse_transaction_request(expected_function)?;
self.verify_signature_from(&sender)?;
Ok((id, sender, body, function))
}
pub fn parse_sealed_transaction_request(&self, expected_function: Option<&Function>, recipient: &PrivateKeyBase) -> Result<(ARID, PublicKeyBase, Envelope, Function)> {
let signed = self.decrypt(recipient)?;
let (id, sender, body, function) = signed.parse_signed_transaction_request(expected_function)?;
Ok((id, sender, body, function))
}
}