Skip to content

Commit

Permalink
Merge pull request #22 from Concordium/crypto-transactions
Browse files Browse the repository at this point in the history
Align conversion with other FFI types by using serde
  • Loading branch information
soerenbf authored Aug 6, 2024
2 parents 4b62e13 + 15199d3 commit 16df5e5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Aligned fields in `SecToPubTransferData` with serde version of the underlying struct from concordium-base, which is similar to other FFI types in the crate.

## [3.1.0] - 2024-08-06

### Added
Expand Down
9 changes: 5 additions & 4 deletions src/lib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,15 @@ dictionary InputEncryptedAmount {
dictionary SecToPubTransferData {
/// The serialized remaining amount after deducting the amount to transfer
/// Serialized according to the [`Serial`] implementation of [`concordium_base::encrypted_transfers::types::EncryptedAmount`]
Bytes serialized_remaining_amount;
/// The amount to transfer
u64 transfer_amount;
Bytes remaining_amount;
/// The amount to transfer in microCCD.
/// For historic reasons, amounts are serialized as strings.
string transfer_amount;
/// The transfer index of the transfer
u64 index;
/// The serialized proof that the transfer is correct.
/// Serialized according to the [`Serial`] implementation of [`concordium_base::encrypted_transfers::types::SecToPubAmountTransferProof`]
Bytes serialized_proof;
Bytes proof;
};

dictionary SecToPubTransferDataDeserializeResult {
Expand Down
44 changes: 23 additions & 21 deletions src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,31 @@ impl TryFrom<InputEncryptedAmount> for AggregatedDecryptedAmount<ArCurve> {

/// UniFFI compatible bridge from [`SecToPubAmountTransferData<ArCurve>`],
/// providing the implementation of the UDL declaration of the same name.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SecToPubTransferData {
/// Serialized according to the [`Serial`] implementation of [`concordium_base::encrypted_transfers::types::EncryptedAmount`]
pub serialized_remaining_amount: Bytes,
pub transfer_amount: u64,
pub remaining_amount: Bytes,
/// In microCCD. For historic resons, amounts are serialized as strings
pub transfer_amount: String,
pub index: u64,
/// Serialized according to the [`Serial`] implementation of [`concordium_base::encrypted_transfers::types::SecToPubAmountTransferProof`]
pub serialized_proof: Bytes,
pub proof: Bytes,
}

impl TryFrom<SecToPubTransferData> for SecToPubAmountTransferData<ArCurve> {
type Error = serde_json::Error;

fn try_from(value: SecToPubTransferData) -> Result<Self, Self::Error> {
serde_json::to_string(&value).and_then(|json| serde_json::from_str::<Self>(&json))
}
}

impl From<SecToPubAmountTransferData<ArCurve>> for SecToPubTransferData {
fn from(value: SecToPubAmountTransferData<ArCurve>) -> Self {
let mut serialized_remaining_amount = vec![];
value
.remaining_amount
.serial(&mut serialized_remaining_amount);
let mut serialized_proof = vec![];
value.proof.serial(&mut serialized_proof);

SecToPubTransferData {
serialized_remaining_amount: Bytes::from(serialized_remaining_amount),
transfer_amount: value.transfer_amount.micro_ccd,
index: value.index.index,
serialized_proof: Bytes::from(serialized_proof),
}
impl TryFrom<SecToPubAmountTransferData<ArCurve>> for SecToPubTransferData {
type Error = serde_json::Error;

fn try_from(value: SecToPubAmountTransferData<ArCurve>) -> Result<Self, Self::Error> {
serde_json::to_string(&value).and_then(|json| serde_json::from_str::<Self>(&json))
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl SecToPubTransferData {
)
.context("Failed to create transfer data")?;

Ok(transfer_data.into())
Ok(transfer_data.try_into()?)
}
}

Expand Down Expand Up @@ -143,9 +143,11 @@ pub fn deserialize_sec_to_pub_transfer_data(
let mut bytes = std::io::Cursor::new(bytes);
let transfer_data = SecToPubAmountTransferData::deserial(&mut bytes)
.map_err(|e| e.to_call_failed(fn_name.to_string()))?;
let transfer_data = SecToPubTransferData::try_from(transfer_data)
.map_err(|e| e.to_call_failed(fn_name.to_string()))?;

let result = DeserializeResult {
value: transfer_data.into(),
value: transfer_data,
bytes_read: bytes.position(),
};
Ok(result)
Expand Down

0 comments on commit 16df5e5

Please sign in to comment.