Skip to content

Commit

Permalink
[ABW-3230] Update Wallet Interaction models (#162)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* change WalletInteractionId from UUID to String

* Revert "wip"

This reverts commit 3ea7600.

* wip

* wip

* wip

* fix kotlin tests

* wip

* wip

* wip

* wip

* revert sample values

* fix

* address comments

* address comments

* address comments

* address comments

* Bump to 1.0.11

* export new_wallet_interaction_version_current
  • Loading branch information
danvleju-rdx authored Jun 14, 2024
1 parent b1fb6d6 commit 4152cd3
Show file tree
Hide file tree
Showing 49 changed files with 827 additions and 129 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 = "1.0.10"
version = "1.0.11"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension DappToWalletInteractionUnvalidated {
public func toJSONString(prettyPrinted: Bool = false) -> String {
dappToWalletInteractionUnvalidatedToJsonString(interactionUnvalidated: self, prettyPrinted: prettyPrinted)
}

public init(jsonString: String) throws {
self = try newDappToWalletInteractionUnvalidatedFromJsonString(jsonStr: jsonString)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import SargonUniFFI

extension UnvalidatedTransactionManifest {
public init(manifest: TransactionManifest) {
self = newUnvalidatedTransactionManifestFromTransactionManifest(transactionManifest: manifest)
}

public func transactionManifest(onNetwork networkID: NetworkID) throws -> TransactionManifest {
try newTransactionManifestFromUnvalidatedTransactionManifest(
unvalidatedTransactionManifest: self,
networkId: networkID
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension WalletToDappInteractionResponse {
public init(jsonData: some DataProtocol) throws {
self = try newWalletToDappInteractionResponseFromJsonBytes(jsonBytes: Data(jsonData))
}

public func jsonData() -> Data {
walletToDappInteractionResponseToJsonBytes(walletToDappInteractionResponse: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import SargonUniFFI

extension WalletInteractionVersion {
public static let current: Self = newWalletInteractionVersionCurrent()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import SargonUniFFI

extension WalletInteractionWalletAccount {
public init(jsonData: some DataProtocol) throws {
self = try newWalletInteractionWalletAccountFromJsonBytes(jsonBytes: Data(jsonData))
}

public func jsonData() -> Data {
walletInteractionWalletAccountToJsonBytes(walletInteractionWalletAccount: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import SargonUniFFI

#if DEBUG
extension DappToWalletInteractionUnvalidated {
public static let sample: Self = newDappToWalletInteractionUnvalidatedSample()
public static let sampleOther: Self = newDappToWalletInteractionUnvalidatedSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import SargonUniFFI

#if DEBUG
extension UnvalidatedTransactionManifest {
public static let sample: Self = newUnvalidatedTransactionManifestSample()
public static let sampleOther: Self = newUnvalidatedTransactionManifestSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import SargonUniFFI

#if DEBUG
extension WalletToDappInteractionResponse {
public static let sample: Self = newWalletToDappInteractionResponseSample()
public static let sampleOther: Self = newWalletToDappInteractionResponseSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
import SargonUniFFI

#if DEBUG
extension WalletInteractionWalletAccount {
public static let sample: Self = newWalletInteractionWalletAccountSample()
public static let sampleOther: Self = newWalletInteractionWalletAccountSampleOther()
}
#endif // DEBUG
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foundation
import SargonUniFFI

extension DappToWalletInteractionUnvalidated: SargonModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Foundation
import SargonUniFFI

extension UnvalidatedTransactionManifest: SargonModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation
import SargonUniFFI

extension WalletToDappInteractionResponse: SargonModel {}
extension WalletToDappInteractionResponse: SargonObjectCodable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation
import SargonUniFFI

extension WalletInteractionWalletAccount: SargonModel {}
extension WalletInteractionWalletAccount: SargonObjectCodable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class DappToWalletInteractionUnvalidatedTests: Test<DappToWalletInteractionUnvalidated> {
func test_json_roundtrip() throws {
func doTest(sut: SUT, json: String) throws {
let encoded = sut.toJSONString(prettyPrinted: false)
XCTAssertEqual(encoded, json)
let decoded = try SUT(jsonString: json)
XCTAssertEqual(decoded, sut)
}

try SUT.sampleValues.forEach { sample in
let json = sample.toJSONString(prettyPrinted: false)
try doTest(sut: sample, json: json)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class UnvalidatedTransactionManifestTests: Test<UnvalidatedTransactionManifest> {
func testTransactionManifestOnNetwork() throws {
let instance = SUT(manifest: .sample)
let result = try instance.transactionManifest(onNetwork: .sample)
XCTAssertEqual(result, TransactionManifest.sample)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class WalletToDappInteractionResponseTests: Test<WalletToDappInteractionResponse> {
func test_codable() throws {
let json = try openFile(subPath: "vector", "wallet_interactions_wallet_to_dapp", extension: "json")
let sut = try JSONDecoder().decode([SUT].self, from: json)
let encoded = try JSONEncoder().encode(sut)
try XCTAssertEqual(JSONDecoder().decode([SUT].self, from: encoded), sut)
}

/// Cyon: We might be able remove this function once we have converted to `swift-testing` which has much more
/// powerful discovery than XCTest, and maybe `eachSampleCodableRoundtripTest` will be picked up as
/// a test directly.
func testJSONRoundtripAllSamples() throws {
try eachSampleCodableRoundtripTest()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import CustomDump
import Foundation
import Sargon
import SargonUniFFI
import XCTest

final class WalletInteractionWalletAccountTests: Test<WalletInteractionWalletAccount> {
func test_codable() throws {
let raw = """
{
"address" : "account_rdx128y6j78mt0aqv6372evz28hrxp8mn06ccddkr7xppc88hyvynvjdwr",
"label" : "Spending Account",
"appearanceId" : 0
}
""".data(using: .utf8)!

// test decoding
let sut = try JSONDecoder().decode(SUT.self, from: raw)
XCTAssertEqual(sut, SUT.sample)

// test encoding
let encoded = try JSONEncoder().encode(sut)
try XCTAssertEqual(JSONDecoder().decode(SUT.self, from: encoded), sut)
}

/// Cyon: We might be able remove this function once we have converted to `swift-testing` which has much more
/// powerful discovery than XCTest, and maybe `eachSampleCodableRoundtripTest` will be picked up as
/// a test directly.
func testJSONRoundtripAllSamples() throws {
try eachSampleCodableRoundtripTest()
}
}
10 changes: 6 additions & 4 deletions fixtures/vector/wallet_interactions_dapp_to_wallet.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@
"items" : {
"discriminator" : "transaction",
"send" : {
"version" : 1,
"message" : "test message",
"blobs": ["0061736d0100000001c8011c60037f7f7f0060027f7f0060027f7f017f60017f0060037f7f7f017f60017f017f60047f7f7f7f0060017f017e60037f7f7f017e60057f7f7f7f7f0060057f7f7f7f7f017f60027f7e017f60037f7f7e0"],
"transactionManifest" : "CALL_FUNCTION Address(\"package_tdx_2_1pkgxxxxxxxxxplxxxxxxxxxxxxx020379220524xxxxxxxxxe4r780\") \n \"OneResourcePool\"\n \"instantiate\"\n Enum<OwnerRole::Fixed>(Enum<AccessRule::AllowAll>())\n Enum<AccessRule::AllowAll>() \n Address(\"resource_tdx_2_1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxtfd2jc\")\n None;"
"version" : 2,
"message" : "message_other",
"blobs": [
"deadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafedeadbeefabbafadecafe"
],
"transactionManifest" : "CALL_METHOD\n Address(\"account_sim1cyvgx33089ukm2pl97pv4max0x40ruvfy4lt60yvya744cve475w0q\")\n \"lock_fee\"\n Decimal(\"500\")\n;\nCALL_METHOD\n Address(\"account_sim1cyvgx33089ukm2pl97pv4max0x40ruvfy4lt60yvya744cve475w0q\")\n \"withdraw\"\n Address(\"resource_sim1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxakj8n3\")\n Decimal(\"330\")\n;\nTAKE_FROM_WORKTOP\n Address(\"resource_sim1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxakj8n3\")\n Decimal(\"150\")\n Bucket(\"bucket1\")\n;\nCALL_METHOD\n Address(\"account_sim1c8mulhl5yrk6hh4jsyldps5sdrp08r5v9wusupvzxgqvhlp4c4nwjz\")\n \"try_deposit_or_abort\"\n Bucket(\"bucket1\")\n Enum<0u8>()\n;\nTAKE_FROM_WORKTOP\n Address(\"resource_sim1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxakj8n3\")\n Decimal(\"130\")\n Bucket(\"bucket2\")\n;\nCALL_METHOD\n Address(\"account_sim1c8s2hass5g62ckwpv78y8ykdqljtetv4ve6etcz64gveykxznj36tr\")\n \"try_deposit_or_abort\"\n Bucket(\"bucket2\")\n Enum<0u8>()\n;\nTAKE_FROM_WORKTOP\n Address(\"resource_sim1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxakj8n3\")\n Decimal(\"50\")\n Bucket(\"bucket3\")\n;\nCALL_METHOD\n Address(\"account_sim1c8ct6jdcwqrg3gzskyxuy0z933fe55fyjz6p56730r95ulzwl3ppva\")\n \"try_deposit_or_abort\"\n Bucket(\"bucket3\")\n Enum<0u8>()\n;\n"
}
},
"metadata" : {
Expand Down
6 changes: 3 additions & 3 deletions fixtures/vector/wallet_interactions_wallet_to_dapp.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"accounts" : [
{
"address" : "account_tdx_2_129qeystv8tufmkmjrry2g6kadhhfh4f7rd0x3t9yagcvfhspt62paz",
"label" : "Dff",
"label" : "Spending Account",
"appearanceId" : 0
},
{
"address" : "account_tdx_2_128928hvf6pjr3rx2xvdw6ulf7pc8g88ya8ma3j8dtjmntckz09fr3n",
"label" : "Ghhvgfvf",
"label" : "Savings Account",
"appearanceId" : 1
}
],
Expand Down Expand Up @@ -74,7 +74,7 @@
"accounts" : [
{
"address" : "account_tdx_2_129qeystv8tufmkmjrry2g6kadhhfh4f7rd0x3t9yagcvfhspt62paz",
"label" : "Dff",
"label" : "Spending Account",
"appearanceId" : 0
}
]
Expand Down
24 changes: 14 additions & 10 deletions src/radix_connect/interaction_id.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
use crate::prelude::*;

uniffi::custom_newtype!(WalletInteractionId, Uuid);
uniffi::custom_newtype!(WalletInteractionId, String);

/// We ought to make `WalletInteractionId` a UUID.
/// Temporarily, it will be a String because the iOS wallet has specific logic that uses custom IDs for wallet interactions.
#[derive(
Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Ord, PartialOrd, Hash,
)]
pub struct WalletInteractionId(pub(crate) Uuid);
pub struct WalletInteractionId(pub(crate) String);

impl FromStr for WalletInteractionId {
type Err = CommonError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Uuid::from_str(s).map(WalletInteractionId).map_err(|_| {
CommonError::RadixMobileInvalidInteractionID {
if s.is_empty() {
Err(CommonError::RadixMobileInvalidInteractionID {
bad_value: s.to_owned(),
}
})
})
} else {
Ok(WalletInteractionId(s.to_owned()))
}
}
}

impl HasSampleValues for WalletInteractionId {
fn sample() -> Self {
WalletInteractionId(Uuid::from_bytes([0xff; 16]))
WalletInteractionId(Uuid::from_bytes([0xff; 16]).to_string())
}

fn sample_other() -> Self {
WalletInteractionId(Uuid::from_bytes([0xde; 16]))
WalletInteractionId(Uuid::from_bytes([0xde; 16]).to_string())
}
}

Expand All @@ -44,9 +48,9 @@ mod tests {
#[test]
fn inequafrom_invalid_str() {
assert_eq!(
"bad".parse::<SUT>(),
"".parse::<SUT>(),
Err(CommonError::RadixMobileInvalidInteractionID {
bad_value: "bad".to_owned()
bad_value: "".to_owned()
})
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/radix_connect/interaction_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl From<u64> for WalletInteractionVersion {

impl WalletInteractionVersion {
pub fn current() -> Self {
Self(1)
Self(2)
}
}

Expand Down Expand Up @@ -47,6 +47,6 @@ mod tests {

#[test]
fn current() {
assert_eq!(SUT::current(), 1.into());
assert_eq!(SUT::current(), 2.into());
}
}
19 changes: 19 additions & 0 deletions src/radix_connect/interaction_version_uniffi_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::prelude::*;

#[uniffi::export]
pub fn new_wallet_interaction_version_current() -> WalletInteractionVersion {
WalletInteractionVersion::current()
}

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

#[test]
fn current() {
assert_eq!(
new_wallet_interaction_version_current(),
WalletInteractionVersion::current()
)
}
}
2 changes: 1 addition & 1 deletion src/radix_connect/mobile/deep_link_parsing/dapp_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ mod tests {
#[test]
fn try_with_invalid_interaction_id() {
let session_id = Uuid::new_v4().to_string();
let interaction_id = "bad";
let interaction_id = "";
assert_eq!(
SUT::try_with_interaction_id_and_session_id(
interaction_id,
Expand Down
Loading

0 comments on commit 4152cd3

Please sign in to comment.