Skip to content

feat: impl Soft Binding assertion definition #1147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/src/assertions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ mod uuid_assertion;
pub(crate) use uuid_assertion::Uuid;

pub mod region_of_interest;

mod soft_binding;
pub use soft_binding::{
SoftBinding, SoftBindingBlockMap, SoftBindingScopeMap, SoftBindingTimespanMap,
};
5 changes: 3 additions & 2 deletions sdk/src/assertions/region_of_interest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ pub enum Role {

/// A region of interest within an asset describing the change.
///
/// This struct can be used from [`Action::changes`][crate::assertions::Action::changes] or
/// [`Metadata::region_of_interest`][crate::assertions::Metadata::region_of_interest].
/// This struct can be used from [`Action::changes`][crate::assertions::Action::changes],
/// [`Metadata::region_of_interest`][crate::assertions::Metadata::region_of_interest], or
/// [`SoftBindingScopeMap::region`][crate::assertions::soft_binding::SoftBindingScopeMap::region].
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "json_schema", derive(JsonSchema))]
Expand Down
109 changes: 109 additions & 0 deletions sdk/src/assertions/soft_binding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use serde::{Deserialize, Serialize};

use super::labels;
use crate::{
assertion::{Assertion, AssertionBase, AssertionCbor, AssertionJson},
assertions::region_of_interest::RegionOfInterest,
cbor_types::UriT,
Result,
};

const ASSERTION_CREATION_VERSION: usize = 1;

/// The data structure used to store one or more soft bindings across some or all of the asset's content.
///
/// https://c2pa.org/specifications/specifications/2.2/specs/C2PA_Specification.html#soft_binding_assertion
#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct SoftBinding {
/// A string identifying the soft binding algorithm and version of that algorithm used to compute the value,
/// taken from the [C2PA soft binding algorithm list](https://github.com/c2pa-org/softbinding-algorithm-list).
///
/// If this field is absent, the algorithm is taken from the `alg_soft` value of the enclosing structure.
/// If both are present, the field in this structure is used. If no value is present in any of these places,
/// this structure is invalid; there is no default.
#[serde(skip_serializing_if = "Option::is_none")]
pub alg: Option<String>,

/// A list of details about the soft binding.
pub blocks: Vec<SoftBindingBlockMap>,

/// Zero-filled bytes used for filling up space.
#[serde(with = "serde_bytes")]
pub pad: Vec<u8>,

/// Zero-filled bytes used for filling up space.
#[serde(skip_serializing_if = "Option::is_none")]
pub pad2: Option<serde_bytes::ByteBuf>,

/// A human-readable description of what this hash covers.
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,

/// CBOR byte string describing parameters of the soft binding algorithm.
#[serde(skip_serializing_if = "Option::is_none")]
pub alg_params: Option<String>,

/// A file or http(s) URL to where the bytes that are being hashed lived.
///
/// This is useful for cases where the data lives in a different file chunk or side-car
/// than the claim.
#[deprecated]
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<UriT>,
}

/// Details about the soft binding, including the referenced value and scope.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SoftBindingBlockMap {
/// The scope of the soft binding where it is applicable.
pub scope: SoftBindingScopeMap,

/// In algorithm specific format, the value of the soft binding computed over this block of digital content.
pub value: String,
}

/// Soft binding scope, specifying specifically where in an asset the soft binding is applicable.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SoftBindingScopeMap {
/// In algorithm specific format, the part of the digital content over which the soft binding value has been computed.
#[deprecated]
#[serde(skip_serializing_if = "Option::is_none")]
pub extent: Option<String>,

/// For temporal assets, the timespan in which the soft binding is applicable.
#[serde(skip_serializing_if = "Option::is_none")]
pub timespan: Option<SoftBindingTimespanMap>,

/// Region of interest in regard to the soft binding.
pub region: RegionOfInterest,
}

/// Soft binding timespan for temporal assets.
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct SoftBindingTimespanMap {
/// Start of the time range (as milliseconds from media start) over which the soft binding value has been computed.
pub start: u32,

/// End of the time range (as milliseconds from media start) over which the soft binding value has been computed.
pub end: u32,
}

impl SoftBinding {
pub const LABEL: &'static str = labels::SOFT_BINDING;
}

impl AssertionBase for SoftBinding {
const LABEL: &'static str = Self::LABEL;
const VERSION: Option<usize> = Some(ASSERTION_CREATION_VERSION);

fn to_assertion(&self) -> Result<Assertion> {
Self::to_cbor_assertion(self)
}

fn from_assertion(assertion: &Assertion) -> Result<Self> {
Self::from_cbor_assertion(assertion)
}
}

impl AssertionJson for SoftBinding {}
impl AssertionCbor for SoftBinding {}