Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Implement inner functionality for RFC-0001 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
KendallWeihe authored Jun 25, 2024
1 parent eabfc1b commit f02e0a1
Show file tree
Hide file tree
Showing 72 changed files with 10,325 additions and 1,627 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
members = [
"bindings/tbdex_uniffi",
"bindings/tbdex_uniffi", "crates/pfi_exemplar_integration_test",
"crates/tbdex",
]
default-members = [
Expand All @@ -17,4 +17,4 @@ license-file = "LICENSE"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
thiserror = "1.0.50"
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "c6db872c0f7c3ab20970ca610e57481a0649f726" }
web5 = { git = "https://github.com/TBD54566975/web5-rs", rev = "f4de14fbada87543a335e1b8dd4442cd21c04db7" }
3 changes: 2 additions & 1 deletion bindings/tbdex_uniffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ repository.workspace = true
license-file.workspace = true

[dependencies]
serde_json = { workspace = true }
tbdex = { path = "../../crates/tbdex" }
thiserror = { workspace = true }
uniffi = { version = "0.27.1", features = ["cli"] }
web5 = { workspace = true }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "c6db872c0f7c3ab20970ca610e57481a0649f726" }
web5_uniffi_wrapper = { git = "https://github.com/TBD54566975/web5-rs", rev = "f4de14fbada87543a335e1b8dd4442cd21c04db7" }

[build-dependencies]
uniffi = { version = "0.27.1", features = ["build"] }
Expand Down
22 changes: 18 additions & 4 deletions bindings/tbdex_uniffi/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;
use serde_json::Error as SerdeJsonError;
use std::sync::{Arc, PoisonError};
use std::{any::type_name, fmt::Debug};
use tbdex::http_client::TbdexHttpClientError;
use tbdex::http_client::HttpClientError;
use tbdex::messages::MessageError;
use tbdex::resources::ResourceError;
use thiserror::Error;
Expand All @@ -16,6 +17,13 @@ pub enum RustCoreError {
}

impl RustCoreError {
pub fn from_poison_error<T>(error: PoisonError<T>, error_type: &str) -> Arc<Self> {
Arc::new(RustCoreError::Error {
r#type: error_type.to_string(),
variant: "PoisonError".to_string(),
message: error.to_string(),
})
}
fn new<T>(error: T) -> Self
where
T: std::error::Error + 'static,
Expand Down Expand Up @@ -76,8 +84,14 @@ impl From<MessageError> for RustCoreError {
}
}

impl From<TbdexHttpClientError> for RustCoreError {
fn from(error: TbdexHttpClientError) -> Self {
impl From<HttpClientError> for RustCoreError {
fn from(error: HttpClientError) -> Self {
RustCoreError::new(error)
}
}

impl From<SerdeJsonError> for RustCoreError {
fn from(error: SerdeJsonError) -> Self {
RustCoreError::new(error)
}
}
Expand Down
16 changes: 16 additions & 0 deletions bindings/tbdex_uniffi/src/http_client/balances.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::{errors::Result, resources::balance::Balance};
use std::sync::{Arc, RwLock};
use web5_uniffi_wrapper::dids::bearer_did::BearerDid;

pub fn get_balances(pfi_did_uri: String, bearer_did: Arc<BearerDid>) -> Result<Vec<Arc<Balance>>> {
let inner_balances =
tbdex::http_client::balances::get_balances(&pfi_did_uri, &bearer_did.0.clone())
.map_err(|e| Arc::new(e.into()))?;

let balances = inner_balances
.into_iter()
.map(|b| Arc::new(Balance(Arc::new(RwLock::new(b)))))
.collect();

Ok(balances)
}
80 changes: 80 additions & 0 deletions bindings/tbdex_uniffi/src/http_client/exchanges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::{
errors::Result,
messages::{close::Close, order::Order, order_status::OrderStatus, quote::Quote, rfq::Rfq},
};
use std::sync::{Arc, RwLock};
use tbdex::http_client::exchanges::Exchange as InnerExchange;
use web5_uniffi_wrapper::dids::bearer_did::BearerDid;

pub struct Exchange {
pub rfq: Arc<Rfq>,
pub quote: Option<Arc<Quote>>,
pub order: Option<Arc<Order>>,
pub order_statuses: Option<Vec<Arc<OrderStatus>>>,
pub close: Option<Arc<Close>>,
}

impl Exchange {
pub fn from_inner(inner: InnerExchange) -> Self {
Self {
rfq: Arc::new(Rfq(Arc::new(RwLock::new(inner.rfq.clone())))),
quote: inner
.quote
.as_ref()
.map(|q| Arc::new(Quote(Arc::new(RwLock::new(q.clone()))))),
order: inner
.order
.as_ref()
.map(|o| Arc::new(Order(Arc::new(RwLock::new(o.clone()))))),
order_statuses: inner.order_statuses.as_ref().map(|os| {
os.iter()
.map(|o| Arc::new(OrderStatus(Arc::new(RwLock::new(o.clone())))))
.collect::<Vec<_>>()
}),
close: inner
.close
.as_ref()
.map(|c| Arc::new(Close(Arc::new(RwLock::new(c.clone()))))),
}
}
}

pub fn create_exchange(rfq: Arc<Rfq>, reply_to: Option<String>) -> Result<()> {
tbdex::http_client::exchanges::create_exchange(&rfq.to_inner()?, reply_to)
.map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn submit_order(order: Arc<Order>) -> Result<()> {
tbdex::http_client::exchanges::submit_order(&order.get_data()?)
.map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn submit_close(close: Arc<Close>) -> Result<()> {
tbdex::http_client::exchanges::submit_close(&close.get_data()?)
.map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn get_exchange(
pfi_did_uri: String,
bearer_did: Arc<BearerDid>,
exchange_id: String,
) -> Result<Exchange> {
let inner_exchange = tbdex::http_client::exchanges::get_exchange(
&pfi_did_uri,
&bearer_did.0.clone(),
&exchange_id,
)
.map_err(|e| Arc::new(e.into()))?;

Ok(Exchange::from_inner(inner_exchange))
}

pub fn get_exchanges(pfi_did_uri: String, bearer_did: Arc<BearerDid>) -> Result<Vec<String>> {
let exchange_ids =
tbdex::http_client::exchanges::get_exchanges(&pfi_did_uri, &bearer_did.0.clone())
.map_err(|e| Arc::new(e.into()))?;
Ok(exchange_ids)
}
76 changes: 3 additions & 73 deletions bindings/tbdex_uniffi/src/http_client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,3 @@
use crate::{
errors::Result,
messages::{close::Close, order::Order, rfq::Rfq, Message, OuterMessage},
resources::{balance::Balance, offering::Offering},
};
use std::sync::Arc;
use tbdex::http_client::TbdexHttpClient;
use web5_uniffi_wrapper::dids::bearer_did::BearerDid;

pub fn get_offerings(pfi_did: String) -> Result<Vec<Arc<Offering>>> {
let inner_offerings =
TbdexHttpClient::get_offerings(pfi_did).map_err(|e| Arc::new(e.into()))?;

let offerings = inner_offerings
.into_iter()
.map(|o| Arc::new(Offering(o)))
.collect();

Ok(offerings)
}

pub fn get_balances(pfi_did: String, requestor_did: Arc<BearerDid>) -> Result<Vec<Arc<Balance>>> {
let inner_balances = TbdexHttpClient::get_balances(pfi_did, requestor_did.0.clone())
.map_err(|e| Arc::new(e.into()))?;

let balances = inner_balances
.into_iter()
.map(|o| Arc::new(Balance(o)))
.collect();

Ok(balances)
}

pub fn create_exchange(rfq: Arc<Rfq>, reply_to: Option<String>) -> Result<()> {
TbdexHttpClient::create_exchange(rfq.0.clone(), reply_to).map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn submit_order(order: Arc<Order>) -> Result<()> {
TbdexHttpClient::submit_order(order.0.clone()).map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn submit_close(close: Arc<Close>) -> Result<()> {
TbdexHttpClient::submit_close(close.0.clone()).map_err(|e| Arc::new(e.into()))?;
Ok(())
}

pub fn get_exchange(
pfi_did: String,
requestor_did: Arc<BearerDid>,
exchange_id: String,
) -> Result<Vec<Arc<dyn Message>>> {
let inner_messages =
TbdexHttpClient::get_exchange(pfi_did, requestor_did.0.clone(), exchange_id)
.map_err(|e| Arc::new(e.into()))?;

let messages = inner_messages
.into_iter()
.map(|m| {
let outer_message: Arc<dyn Message> = Arc::new(OuterMessage(m));
outer_message
})
.collect();

Ok(messages)
}

pub fn get_exchanges(pfi_did: String, requestor_did: Arc<BearerDid>) -> Result<Vec<String>> {
let exchange_ids = TbdexHttpClient::get_exchanges(pfi_did, requestor_did.0.clone())
.map_err(|e| Arc::new(e.into()))?;
Ok(exchange_ids)
}
pub mod balances;
pub mod exchanges;
pub mod offerings;
14 changes: 14 additions & 0 deletions bindings/tbdex_uniffi/src/http_client/offerings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::{errors::Result, resources::offering::Offering};
use std::sync::{Arc, RwLock};

pub fn get_offerings(pfi_did_uri: String) -> Result<Vec<Arc<Offering>>> {
let inner_offerings = tbdex::http_client::offerings::get_offerings(&pfi_did_uri)
.map_err(|e| Arc::new(e.into()))?;

let offerings = inner_offerings
.into_iter()
.map(|o| Arc::new(Offering(Arc::new(RwLock::new(o)))))
.collect();

Ok(offerings)
}
68 changes: 46 additions & 22 deletions bindings/tbdex_uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,56 @@ mod errors;
use crate::{
errors::RustCoreError,
http_client::{
create_exchange, get_balances, get_exchange, get_exchanges, get_offerings, submit_close,
submit_order,
balances::get_balances,
exchanges::{
create_exchange, get_exchange, get_exchanges, submit_close, submit_order,
Exchange as ExchangeData,
},
offerings::get_offerings,
},
messages::{
close::Close, order::Order, order_status::OrderStatus, quote::Quote, rfq::Rfq, Message,
close::Close,
order::Order,
order_status::OrderStatus,
quote::Quote,
rfq::{
data::{
CreateRfqData as CreateRfqDataData,
CreateSelectedPayinMethod as CreateSelectedPayinMethodData,
CreateSelectedPayoutMethod as CreateSelectedPayoutMethodData,
PrivatePaymentDetails as PrivatePaymentDetailsData, Rfq as RfqData,
RfqData as RfqDataData, RfqPrivateData as RfqPrivateDataData,
SelectedPayinMethod as SelectedPayinMethodData,
SelectedPayoutMethod as SelectedPayoutMethodData,
},
Rfq,
},
},
resources::{
balance::Balance,
offering::{
data::{
Offering as OfferingData, OfferingData as OfferingDataData,
PayinDetails as PayinDetailsData, PayinMethod as PayinMethodData,
PayoutDetails as PayoutDetailsData, PayoutMethod as PayoutMethodData,
},
Offering,
},
},
resources::{balance::Balance, offering::Offering, Resource},
};
use tbdex::{
messages::{
close::{Close as CloseData, CloseData as CloseDataData},
order::Order as OrderData,
order::{Order as OrderData, OrderData as OrderDataData},
order_status::{OrderStatus as OrderStatusData, OrderStatusData as OrderStatusDataData},
quote::{
PaymentInstructions as PaymentInstructionsData, Quote as QuoteData,
QuoteData as QuoteDataData, QuoteDetails as QuoteDetailsData,
},
rfq::{
CreateRfqData as CreateRfqDataData,
CreateSelectedPayinMethod as CreateSelectedPayinMethodData,
CreateSelectedPayoutMethod as CreateSelectedPayoutMethodData,
PrivatePaymentDetails as PrivatePaymentDetailsData, Rfq as RfqData,
RfqData as RfqDataData, RfqPrivateData as RfqPrivateDataData,
SelectedPayinMethod as SelectedPayinMethodData,
SelectedPayoutMethod as SelectedPayoutMethodData,
},
MessageKind, MessageMetadata as MessageMetadataData,
},
resources::{
balance::{Balance as BalanceData, BalanceData as BalanceDataData},
offering::{
Offering as OfferingData, OfferingData as OfferingDataData,
PayinDetails as PayinDetailsData, PayinMethod as PayinMethodData,
PayoutDetails as PayoutDetailsData, PayoutMethod as PayoutMethodData,
},
ResourceKind, ResourceMetadata as ResourceMetadataData,
},
};
Expand All @@ -62,14 +77,23 @@ use web5::apid::{
};
use web5_uniffi_wrapper::{
credentials::presentation_definition::PresentationDefinition,
crypto::key_manager::KeyManager,
crypto::{in_memory_key_manager::InMemoryKeyManager, key_manager::KeyManager},
dids::bearer_did::{BearerDid, BearerDidData},
dsa::Signer,
errors::RustCoreError as Web5RustCoreError,
};

pub fn hello_world() {
println!("Hello world")
// 🚧 TODO temporary hack in place while did:dht resolution is incomplete
pub fn tmp_hack_bearer_did(
did: DidData,
document: DocumentData,
key_manager: std::sync::Arc<dyn KeyManager>,
) -> std::sync::Arc<BearerDid> {
std::sync::Arc::new(BearerDid(web5::apid::dids::bearer_did::BearerDid {
did,
document,
key_manager: key_manager.to_inner(),
}))
}

uniffi::include_scaffolding!("tbdex");
Loading

0 comments on commit f02e0a1

Please sign in to comment.