Skip to content

Commit

Permalink
UniFFI export JSON serde of P2PLinks (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored May 13, 2024
1 parent 047f580 commit 7783f79
Show file tree
Hide file tree
Showing 36 changed files with 111 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "0.7.14"
version = "0.7.15"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package com.radixdlt.sargon.extensions
import com.radixdlt.sargon.PublicKeyHash
import com.radixdlt.sargon.P2pLink
import com.radixdlt.sargon.annotation.KoverIgnore
import com.radixdlt.sargon.newP2PLinkFromJsonBytes
import com.radixdlt.sargon.newP2pLinksFromJsonBytes
import com.radixdlt.sargon.p2PLinkToJsonBytes
import com.radixdlt.sargon.p2pLinksToJsonBytes

class P2pLinks private constructor(
private val array: IdentifiedArray<PublicKeyHash, P2pLink>
Expand Down Expand Up @@ -36,6 +40,15 @@ class P2pLinks private constructor(
return "P2pLinks(array=$array)"
}

companion object

}

fun List<P2pLink>.asIdentifiable() = P2pLinks(p2pLinks = this)
fun List<P2pLink>.asIdentifiable() = P2pLinks(p2pLinks = this)

@Throws(SargonException::class)
fun P2pLinks.Companion.fromJson(json: String) =
newP2pLinksFromJsonBytes(jsonBytes = bagOfBytes(json))

fun P2pLinks.toJson(): String =
p2pLinksToJsonBytes(asList()).string
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.radixdlt.sargon

import com.radixdlt.sargon.extensions.P2pLinks
import com.radixdlt.sargon.extensions.asIdentifiable
import com.radixdlt.sargon.extensions.fromJson
import com.radixdlt.sargon.extensions.id
import com.radixdlt.sargon.extensions.toJson
import com.radixdlt.sargon.samples.sample
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
Expand Down Expand Up @@ -60,4 +62,14 @@ internal class P2PLinksTest : IdentifiedArrayTest<P2pLinks, PublicKeyHash, P2pLi
).size
)
}

@Test
fun testJsonRoundtrip() {
val sut = listOf(element(), elementWithDifferentId())

assertEquals(
sut,
P2pLinks.fromJson(json = sut.asIdentifiable().toJson())
)
}
}
2 changes: 2 additions & 0 deletions src/radix_connect/p2p_links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod link_connection_qr_data_uniffi_fn;
mod p2p_link;
mod p2p_link_uniffi_fn;
mod p2p_links;
mod p2p_links_uniffi_fn;
mod radix_connect_password;
mod radix_connect_purpose;

Expand All @@ -11,5 +12,6 @@ pub use link_connection_qr_data_uniffi_fn::*;
pub use p2p_link::*;
pub use p2p_link_uniffi_fn::*;
pub use p2p_links::*;
pub use p2p_links_uniffi_fn::*;
pub use radix_connect_password::*;
pub use radix_connect_purpose::*;
3 changes: 3 additions & 0 deletions src/radix_connect/p2p_links/p2p_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ decl_identified_vec_of!(
P2PLink
);

impl JsonDataDeserializing for P2PLinks {}
impl JsonDataSerializing for P2PLinks {}

impl HasSampleValues for P2PLinks {
/// A sample used to facilitate unit tests.
fn sample() -> Self {
Expand Down
29 changes: 29 additions & 0 deletions src/radix_connect/p2p_links/p2p_links_uniffi_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::prelude::*;

#[uniffi::export]
pub fn new_p2p_links_from_json_bytes(
json_bytes: &BagOfBytes,
) -> Result<P2PLinks> {
P2PLinks::new_from_json_bytes(json_bytes)
}

#[uniffi::export]
pub fn p2p_links_to_json_bytes(links: P2PLinks) -> BagOfBytes {
BagOfBytes::from(links.to_json_bytes())
}

#[cfg(test)]
mod tests {
use super::*;

#[allow(clippy::upper_case_acronyms)]
type SUT = P2PLinks;

#[test]
fn json_roundtrip() {
let sut = SUT::sample();
let json = p2p_links_to_json_bytes(sut.clone());
let deserialized = new_p2p_links_from_json_bytes(&json).unwrap();
assert_eq!(deserialized, sut)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteraction {
pub interaction_id: WalletInteractionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Enum)]
#[serde(tag = "discriminator")]
pub enum DappToWalletInteractionItems {
#[serde(rename = "unauthorizedRequest")]
UnauthorizedRequest(DappToWalletInteractionUnauthorizedRequestItems),

#[serde(rename = "authorizedRequest")]
AuthorizedRequest(DappToWalletInteractionAuthorizedRequestItems),

#[serde(rename = "transaction")]
Transaction(DappToWalletInteractionTransactionItems),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Enum)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Enum)]
#[serde(tag = "discriminator")]
pub enum DappToWalletInteractionAuthRequestItem {
#[serde(rename = "loginWithChallenge")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
pub struct DappToWalletInteractionAuthLoginWithChallengeRequestItem {
pub challenge: Exactly32Bytes,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionAuthUsePersonaRequestItem {
pub identity_address: IdentityAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionAuthorizedRequestItems {
pub auth: DappToWalletInteractionAuthRequestItem,
pub reset: Option<DappToWalletInteractionResetRequestItem>,

#[serde(skip_serializing_if = "Option::is_none")]
pub ongoing_accounts: Option<DappToWalletInteractionAccountsRequestItem>,

#[serde(skip_serializing_if = "Option::is_none")]
pub ongoing_persona_data:
Option<DappToWalletInteractionPersonaDataRequestItem>,

#[serde(skip_serializing_if = "Option::is_none")]
pub one_time_accounts: Option<DappToWalletInteractionAccountsRequestItem>,

#[serde(skip_serializing_if = "Option::is_none")]
pub one_time_persona_data:
Option<DappToWalletInteractionPersonaDataRequestItem>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionAccountsRequestItem {
pub number_of_accounts: RequestedQuantity,

#[serde(skip_serializing_if = "Option::is_none")]
pub challenge: Option<Exactly32Bytes>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionPersonaDataRequestItem {
#[serde(skip_serializing_if = "Option::is_none")]
pub is_requesting_name: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
pub number_of_requested_email_addresses: Option<RequestedQuantity>,

#[serde(skip_serializing_if = "Option::is_none")]
pub number_of_requested_phone_numbers: Option<RequestedQuantity>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionResetRequestItem {
pub accounts: bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionUnauthorizedRequestItems {
#[serde(skip_serializing_if = "Option::is_none")]
pub one_time_accounts: Option<DappToWalletInteractionAccountsRequestItem>,

#[serde(skip_serializing_if = "Option::is_none")]
pub one_time_persona_data:
Option<DappToWalletInteractionPersonaDataRequestItem>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
pub struct DappToWalletInteractionTransactionItems {
pub send: DappToWalletInteractionSendTransactionItem,
}
Expand All @@ -11,13 +11,16 @@ impl DappToWalletInteractionTransactionItems {
}
}

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionSendTransactionItem {
pub transaction_manifest: String,

pub version: TXVersion,

#[serde(skip_serializing_if = "Option::is_none")]
pub blobs: Option<Vec<String>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

uniffi::custom_newtype!(TXVersion, u64);
#[derive(Debug, Deserialize, Serialize, PartialEq)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct TXVersion(u64);

impl From<u64> for TXVersion {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct DappToWalletInteractionUnvalidated {
pub interaction_id: WalletInteractionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[serde(rename_all = "camelCase")]
pub enum DappWalletInteractionErrorType {
RejectedByUser,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct WalletToDappInteractionFailureResponse {
pub interaction_id: WalletInteractionId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[serde(tag = "discriminator")]
#[allow(clippy::large_enum_variant)]
pub enum WalletToDappInteractionResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct WalletToDappInteractionAccountProof {
pub account_address: AccountAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
pub struct WalletToDappInteractionAccountsRequestResponseItem {
pub accounts: Vec<WalletInteractionWalletAccount>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Enum)]
#[serde(tag = "discriminator")]
pub enum WalletToDappInteractionAuthRequestResponseItem {
#[serde(rename = "usePersona")]
UsePersona(WalletToDappInteractionAuthUsePersonaRequestResponseItem),

#[serde(rename = "loginWithoutChallenge")]
LoginWithoutChallenge(
WalletToDappInteractionAuthLoginWithoutChallengeRequestResponseItem,
),

#[serde(rename = "loginWithChallenge")]
LoginWithChallenge(
WalletToDappInteractionAuthLoginWithChallengeRequestResponseItem,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
pub struct WalletToDappInteractionAuthLoginWithChallengeRequestResponseItem {
pub persona: DappWalletInteractionPersona,
pub challenge: Exactly32Bytes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
pub struct WalletToDappInteractionAuthLoginWithoutChallengeRequestResponseItem {
pub persona: DappWalletInteractionPersona,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

#[serde_as]
#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct WalletToDappInteractionAuthProof {
#[serde_as(as = "DisplayFromStr")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
pub struct WalletToDappInteractionAuthUsePersonaRequestResponseItem {
pub persona: DappWalletInteractionPersona,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, uniffi::Record)]
#[serde(rename_all = "camelCase")]
pub struct WalletToDappInteractionAuthorizedRequestResponseItems {
pub auth: WalletToDappInteractionAuthRequestResponseItem,
Expand Down
Loading

0 comments on commit 7783f79

Please sign in to comment.