added prepare

This commit is contained in:
Paul 2022-09-04 13:44:12 +03:00
parent aca6c71fb3
commit d3ad5e14b8
3 changed files with 68 additions and 9 deletions

View File

@ -2,7 +2,7 @@
use memuse::DynamicUsage;
use nonempty::NonEmpty;
use rand::RngCore;
use rand::{CryptoRng, RngCore};
use std::fmt;
use crate::keys::IssuerValidatingKey;
@ -28,6 +28,17 @@ impl IssueAction<Unauthorized> {
authorization: Unauthorized,
}
}
/// inject the `sighash` for signature into the bundle.
pub fn prepare(self, sighash: [u8; 32]) -> IssueAction<Prepared> {
return IssueAction {
ik: self.ik,
asset_desc: self.asset_desc,
notes: self.notes,
finalize: self.finalize,
authorization: Prepared { sighash },
};
}
}
impl<T: IssueAuth> IssueAction<T> {
@ -234,7 +245,6 @@ impl Default for IssueBundle<Unauthorized> {
}
impl<T: IssueAuth> IssueBundle<T> {
/// Return the actions for a given `IssueBundle`.
pub fn actions(&self) -> &Vec<IssueAction<T>> {
&self.actions
@ -342,6 +352,22 @@ impl IssueBundle<Unauthorized> {
Ok(())
}
/// Loads the sighash into this bundle, preparing it for signing.
///
/// This API ensures that all signatures are created over the same sighash.
/// pub fn prepare<R: RngCore + CryptoRng>(
// self,
// mut rng: R,
pub fn prepare(self, sighash: [u8; 32]) -> IssueBundle<Prepared> {
IssueBundle {
actions: self
.actions
.into_iter()
.map(|a| a.prepare(sighash))
.collect(),
}
}
}
/// Errors produced during the issuance process
@ -505,6 +531,39 @@ mod tests {
IssueActionAlreadyFinalized
);
}
#[test]
fn issue_bundle_prepare() {
let mut rng = OsRng;
let sk = SpendingKey::random(&mut rng);
let isk: IssuerAuthorizingKey = (&sk).into();
let ik: IssuerValidatingKey = (&isk).into();
let fvk = FullViewingKey::from(&sk);
let recipient = fvk.address_at(0u32, Scope::External);
let mut bundle = IssueBundle::new();
bundle
.add_recipient(
ik.clone(),
String::from("Frost"),
recipient,
NoteValue::from_raw(5),
false,
rng,
)
.unwrap();
let fake_sighash = [1; 32];
let prepared = bundle.prepare(fake_sighash);
let action = prepared
.get_action(ik.clone(), String::from("Frost"))
.unwrap();
let auth = action.authorization();
assert_eq!(auth.sighash, fake_sighash);
}
}
// mod tests {

View File

@ -212,7 +212,11 @@ impl IssuerAuthorizingKey {
/// RXXXX
///
/// XXXXX
pub fn sign(&self, rng: &mut (impl RngCore + CryptoRng), msg: &[u8]) -> redpallas::Signature<SpendAuth> {
pub fn sign(
&self,
rng: &mut (impl RngCore + CryptoRng),
msg: &[u8],
) -> redpallas::Signature<SpendAuth> {
self.0.sign(rng, msg)
}
}

View File

@ -92,17 +92,13 @@ pub mod testing {
pub fn arb_note_type()(
is_native in prop::bool::ANY,
sk in arb_spending_key(),
// bytes32a in prop::array::uniform32(prop::num::u8::ANY),
// bytes32b in prop::array::uniform32(prop::num::u8::ANY),
vec in prop::collection::vec(any::<u8>(), 0..=255),
str in "[A-Za-z]{255}",
) -> NoteType {
if is_native {
NoteType::native()
} else {
//let bytes64 = [bytes32a, bytes32b].concat();
let asset_desc = String::from_utf8(vec).unwrap();
let isk = IssuerAuthorizingKey::from(&sk);
NoteType::derive(&IssuerValidatingKey::from(&isk), asset_desc.as_str())
NoteType::derive(&IssuerValidatingKey::from(&isk), &str)
}
}
}