Add serialization of finalize flag (#68)

Add a function `flags` to serialize the `finalize` flag of an IssueAction to a byte.
This function will be used by the client.
This commit is contained in:
Constance Beguier 2023-06-06 17:11:27 +02:00 committed by GitHub
parent 02fa582c80
commit 9965a6d06b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 0 deletions

View File

@ -61,6 +61,23 @@ impl IssueAction {
}
}
/// Constructs a new `IssueAction`.
pub fn new_with_finalize(asset_desc: String, note: &Note, flags: u8) -> Option<Self> {
let finalize = match flags {
0b0000_0000 => false,
0b0000_0001 => true,
_ => return None,
};
Some(IssueAction {
asset_desc,
notes: NonEmpty {
head: *note,
tail: vec![],
},
finalize,
})
}
/// Constructs an `IssueAction` from its constituent parts.
pub fn from_parts(asset_desc: String, notes: NonEmpty<Note>, finalize: bool) -> Self {
IssueAction {
@ -134,6 +151,11 @@ impl IssueAction {
.then(|| Ok((asset, AssetSupply::new(value_sum, self.is_finalized()))))
.ok_or(IssueBundleIkMismatchAssetBase)?
}
/// Serialize `finalize` flag to a byte
pub fn flags(&self) -> u8 {
self.finalize.then(|| 0b0000_0001).unwrap_or(0b0000_0000)
}
}
/// Defines the authorization type of an Issue bundle.
@ -1272,6 +1294,23 @@ mod tests {
WrongAssetDescSize
);
}
#[test]
fn test_finalize_flag_serialization() {
let mut rng = OsRng;
let (_, _, note) = Note::dummy(&mut rng, None, AssetBase::native());
let action =
IssueAction::new_with_finalize(String::from("Asset description"), &note, 0u8).unwrap();
assert_eq!(action.flags(), 0b0000_0000);
let action =
IssueAction::new_with_finalize(String::from("Asset description"), &note, 1u8).unwrap();
assert_eq!(action.flags(), 0b0000_0001);
let action = IssueAction::new_with_finalize(String::from("Asset description"), &note, 2u8);
assert!(action.is_none());
}
}
/// Generators for property testing.