Validates the provided app data, returning an [`OrderAppData`] if valid. The validation entails verifying that the app data is well formed and its size. In the case the app data contains both a hash and the data, the data will be compared against the hash. The app data the override will be ignored unless the provided app data is a hash.
(
&self,
app_data: &OrderCreationAppData,
full_app_data_override: &Option<String>,
)
| 834 | /// * The app data the override will be ignored unless the provided app data |
| 835 | /// is a hash. |
| 836 | fn validate_app_data( |
| 837 | &self, |
| 838 | app_data: &OrderCreationAppData, |
| 839 | full_app_data_override: &Option<String>, |
| 840 | ) -> Result<OrderAppData, AppDataValidationError> { |
| 841 | let validate = |app_data: &str| -> Result<_, AppDataValidationError> { |
| 842 | let app_data = self |
| 843 | .app_data_validator |
| 844 | .validate(app_data.as_bytes()) |
| 845 | .map_err(AppDataValidationError::Invalid)?; |
| 846 | Ok(app_data) |
| 847 | }; |
| 848 | |
| 849 | let app_data = match app_data { |
| 850 | OrderCreationAppData::Both { full, expected } => { |
| 851 | let validated = validate(full)?; |
| 852 | if validated.hash != *expected { |
| 853 | return Err(AppDataValidationError::Mismatch { |
| 854 | provided: *expected, |
| 855 | actual: validated.hash, |
| 856 | }); |
| 857 | } |
| 858 | validated |
| 859 | } |
| 860 | OrderCreationAppData::Hash { hash } => { |
| 861 | // Eventually we're not going to accept orders that set only a |
| 862 | // hash and where we can't find full app data elsewhere. |
| 863 | let validated = if let Some(full) = full_app_data_override { |
| 864 | validate(full)? |
| 865 | } else { |
| 866 | return Err(AppDataValidationError::Invalid(anyhow!( |
| 867 | "Unknown pre-image for app data hash {:?}", |
| 868 | hash, |
| 869 | ))); |
| 870 | }; |
| 871 | |
| 872 | // Keep the validated document, since the order creation |
| 873 | // simulator re-parses this document to rebuild |
| 874 | // the pre/post hooks, so dropping it |
| 875 | // makes the simulation skip the hooks (e.g. a permit approval) |
| 876 | // and revert spuriously. |
| 877 | ValidatedAppData { |
| 878 | hash: *hash, |
| 879 | document: validated.document, |
| 880 | protocol: validated.protocol, |
| 881 | } |
| 882 | } |
| 883 | OrderCreationAppData::Full { full } => validate(full)?, |
| 884 | }; |
| 885 | |
| 886 | let interactions = self.custom_interactions(&app_data.protocol.hooks); |
| 887 | |
| 888 | Ok(OrderAppData { |
| 889 | inner: app_data, |
| 890 | interactions, |
| 891 | }) |
| 892 | } |
| 893 |