Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial mobile FFI #109

Merged
merged 48 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c801206
Initial FFI with flutter rust bridge
sgreenbury Feb 8, 2023
85ae082
Merge branch 'main' into 62-ffi-v1
sgreenbury Mar 29, 2023
e914df2
Use trustchain-ffi crate from #63
sgreenbury Mar 29, 2023
91ab006
Move mobile bridge to FFI crate
sgreenbury Mar 29, 2023
587be78
Add dep
sgreenbury Mar 29, 2023
3bc4a61
Merge branch '79-refactor-api' into 62-ffi-v1
sgreenbury Mar 31, 2023
5e75370
Merge branch '79-refactor-api' into 62-ffi-v1
sgreenbury Apr 21, 2023
9ac5fff
Merge branch '79-refactor-api' into 62-ffi-v1
sgreenbury Apr 27, 2023
7957702
Merge branch '62-ffi-v1' of github.com:alan-turing-institute/trustcha…
sgreenbury Apr 27, 2023
5ab83ce
WIP: verify DID and credential
sgreenbury Apr 27, 2023
23ee620
Merge branch '79-refactor-api' into 62-ffi-v1
sgreenbury Jun 9, 2023
d260ebb
Merge branch 'main' into 62-ffi-v1
sgreenbury Jul 7, 2023
dc54bf7
WIP: mobile vc verification
sgreenbury Jul 7, 2023
ec4079e
Merge branch '101-type-state-verifier' into 62-ffi-v1
sgreenbury Jul 14, 2023
a35ec73
Initial VC verification with light verifier
sgreenbury Jul 14, 2023
2fcca11
Merge branch '101-type-state-verifier' into 62-ffi-v1
sgreenbury Jul 17, 2023
ad5c35d
Merge branch '101-type-state-verifier' into 62-ffi-v1
sgreenbury Jul 17, 2023
f0b7aee
Remove override of verify for light client
sgreenbury Jul 17, 2023
042ea33
Add FFI bridge modules
sgreenbury Jul 17, 2023
a214f2a
Remove unnecessary fetch
sgreenbury Jul 17, 2023
1238585
Add FFI module for passing endpoint and proof options
sgreenbury Jul 17, 2023
769b5be
Remove function, add todo
sgreenbury Jul 17, 2023
a6ee872
Refactor mobile API with options
sgreenbury Jul 17, 2023
651c1d7
Refactor mobile FFI interface with mobile options
sgreenbury Jul 18, 2023
8c2ca39
Merge branch '79-refactor-api' into 62-ffi-v1
sgreenbury Jul 26, 2023
9714a4b
Merge branch '101-type-state-verifier' into 62-ffi-v1
sgreenbury Jul 27, 2023
8f1b123
Add tests, use config module, remove options module
sgreenbury Jul 28, 2023
607e7a2
Remove route from bundle endpoint test
sgreenbury Jul 28, 2023
45ebe40
Merge branch 'main' into 62-ffi-v1
sgreenbury Jul 31, 2023
989826e
Refactor with trustchain-api
sgreenbury Aug 1, 2023
723c8d1
Add Send + Sync bound for error variant
sgreenbury Aug 2, 2023
394fbb0
Add FFI mobile error
sgreenbury Aug 2, 2023
b31d609
Refactor test variable
sgreenbury Aug 7, 2023
06ea345
Add check for current time compared to created time in proof
sgreenbury Aug 7, 2023
5b71008
Revise method to handle case where http specified
sgreenbury Aug 14, 2023
66be391
Merge branch 'main' into 62-ffi-v1
sgreenbury Sep 7, 2023
275f55c
Add issue presentation FFI functionality
sgreenbury Sep 7, 2023
dae595e
Improve error handling for FFI presenation issuing
sgreenbury Sep 9, 2023
5d6728c
Add did:key resolution temporarily in resolver
sgreenbury Sep 11, 2023
5592bd8
Fix mobile bridge FFI
sgreenbury Sep 11, 2023
d1ab841
Add additional vp_issue_presentation test with did:key
sgreenbury Sep 11, 2023
5012023
Add issue number
sgreenbury Sep 28, 2023
5468008
Add FromStr implementation for FFIConfig, refactor option getters
sgreenbury Sep 28, 2023
524a29f
Add README for FFI crate
sgreenbury Sep 28, 2023
affd385
Update README
sgreenbury Sep 28, 2023
4059a72
clippy
sgreenbury Sep 28, 2023
ec6e4e7
Remove obsolete bridge modules
sgreenbury Sep 28, 2023
a61af33
Pin flutter_rust_bridge to codegen version used
sgreenbury Sep 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions trustchain-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ async-trait = "0.1"
base64 = "0.13"
canonical_json = "0.4.0"
chrono = "0.4"
did-method-key = "0.1.3"
futures = "0.3.21"
petgraph = {version = "0.6"}
serde = { version = "1.0", features = ["derive"] }
Expand Down
8 changes: 8 additions & 0 deletions trustchain-core/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! DID resolution and `DIDResolver` implementation.
use async_trait::async_trait;
use did_method_key::DIDKey;
use serde_json::Value;
use ssi::did::{DIDMethod, Document, Service, ServiceEndpoint};
use ssi::did_resolve::{
Expand Down Expand Up @@ -108,6 +109,13 @@ impl<T: DIDResolver + Sync + Send> DIDResolver for Resolver<T> {
Option<Document>,
Option<DocumentMetadata>,
) {
// TODO: remove upon handling with DIDMethods
if did.starts_with("did:key:") {
let did_key_resolver = DIDKey;
return did_key_resolver
.resolve(did, &ResolutionInputMetadata::default())
.await;
}
// Consider using ResolutionInputMetadata to optionally not perform transform.
// Resolve with the wrapped DIDResolver and then transform to Trustchain format.
self.transform(self.wrapped_resolver.resolve(did, input_metadata).await)
Expand Down
9 changes: 9 additions & 0 deletions trustchain-core/src/vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub enum PresentationError {
/// Wrapped variant for Verifier Error.
#[error("A wrapped Verifier error: {0}")]
VerifierError(VerifierError),
/// Wrapped variant for SSI Error.
#[error("A wrapped SSI error: {0}")]
SSIError(ssi::error::Error),
/// Credentials verified, but holder failed to authenticate with invalid or missing presentation
/// proof.
#[error("Credentials verified for an unauthenticated holder: {0:?}")]
Expand All @@ -44,3 +47,9 @@ impl From<VerifierError> for PresentationError {
PresentationError::VerifierError(err)
}
}

impl From<ssi::error::Error> for PresentationError {
fn from(err: ssi::error::Error) -> Self {
PresentationError::SSIError(err)
}
}
4 changes: 3 additions & 1 deletion trustchain-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ trustchain-ion = { path = "../trustchain-ion" }
trustchain-api = { path = "../trustchain-api" }

anyhow = "1.0"
chrono = "0.4.26"
did-ion="0.1.0"
flutter_rust_bridge = "1"
# Fixed to same version used to generate bridge: `[email protected]`
flutter_rust_bridge = "=1.64.0"
lazy_static="1.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
3 changes: 3 additions & 0 deletions trustchain-ffi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Trustchain FFI

FFI via [`flutter_rust_bridge`](https://cjycode.com/flutter_rust_bridge/index.html). Example usage can be found in [`trustchain-mobile`](https://github.com/alan-turing-institute/trustchain-mobile/blob/dev/install_trustchain_mobile.md#9-build-trustchain-targets).
45 changes: 30 additions & 15 deletions trustchain-ffi/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use anyhow::anyhow;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use ssi::vc::LinkedDataProofOptions;
use std::fs;
use std::{fs, str::FromStr};
use trustchain_core::TRUSTCHAIN_CONFIG;
use trustchain_ion::{Endpoint, URL};

use crate::mobile::FFIMobileError;

lazy_static! {
/// Lazy static reference to ION configuration loaded from `trustchain_config.toml`.
pub static ref FFI_CONFIG: FFIConfig = parse_toml(
Expand All @@ -14,7 +16,7 @@ lazy_static! {
}

/// Parses and maps ION subfields to a new type.
fn parse_toml(toml_str: &str) -> FFIConfig {
pub(crate) fn parse_toml(toml_str: &str) -> FFIConfig {
toml::from_str::<Config>(toml_str)
.expect("Error parsing trustchain_config.toml")
.ffi
Expand Down Expand Up @@ -76,19 +78,33 @@ pub struct FFIConfig {

impl FFIConfig {
pub fn endpoint(&self) -> anyhow::Result<&EndpointOptions> {
self.endpoint_options
Ok(self
.endpoint_options
.as_ref()
.ok_or_else(|| anyhow!("Expected endpoint options."))
.ok_or(anyhow!("Expected endpoint options."))
.map_err(FFIMobileError::NoConfig)?)
}
pub fn trustchain(&self) -> anyhow::Result<&TrustchainOptions> {
self.trustchain_options
Ok(self
.trustchain_options
.as_ref()
.ok_or_else(|| anyhow!("Expected trustchain options."))
.ok_or(anyhow!("Expected trustchain options."))
.map_err(FFIMobileError::NoConfig)?)
}
pub fn linked_data_proof(&self) -> anyhow::Result<&LinkedDataProofOptions> {
self.linked_data_proof_options
Ok(self
.linked_data_proof_options
.as_ref()
.ok_or_else(|| anyhow!("Expected linked data proof options."))
.ok_or(anyhow!("Expected linked data proof options."))
.map_err(FFIMobileError::NoConfig)?)
}
}

impl FromStr for FFIConfig {
type Err = FFIMobileError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s).map_err(FFIMobileError::FailedToDeserialize)
}
}

Expand Down Expand Up @@ -160,20 +176,19 @@ mod tests {
let test_mobile_options: String = format!(
r#"
{{
"endpointOptions": {},
"trustchainOptions": {},
"linkedDataProofOptions": {}
"endpointOptions": {TEST_ENDPOINT_OPTIONS},
"trustchainOptions": {TEST_TRUSTCHAIN_OPTIONS},
"linkedDataProofOptions": {TEST_LINKED_DATA_PROOF_OPTIONS}
}}
"#,
TEST_ENDPOINT_OPTIONS, TEST_TRUSTCHAIN_OPTIONS, TEST_LINKED_DATA_PROOF_OPTIONS
);
serde_json::from_str::<FFIConfig>(&test_mobile_options).unwrap();
}
#[test]
fn test_ffi_options_from_toml() {
println!("{:?}", parse_toml(&TEST_FFI_OPTIONS));
println!("{:?}", parse_toml(TEST_FFI_OPTIONS));
assert_eq!(
parse_toml(&TEST_FFI_OPTIONS)
parse_toml(TEST_FFI_OPTIONS)
.endpoint()
.unwrap()
.trustchain_endpoint()
Expand All @@ -182,7 +197,7 @@ mod tests {
8081
);
assert_eq!(
parse_toml(&TEST_FFI_OPTIONS)
parse_toml(TEST_FFI_OPTIONS)
.linked_data_proof()
.unwrap()
.proof_purpose
Expand Down
4 changes: 2 additions & 2 deletions trustchain-ffi/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ fn attest(did: String, controlled_did: String, verbose: bool) -> anyhow::Result<
/// Resolves a given DID using a resolver available at localhost:3000
fn resolve(did: String, verbose: bool) -> anyhow::Result<String> {
let rt = Runtime::new().unwrap();
let resolver = get_ion_resolver("http://localhost:3000/");
rt.block_on(async {
let (res_meta, doc, doc_meta) =
TrustchainAPI::resolve(&did, "http://localhost:3000/".into()).await?;
let (res_meta, doc, doc_meta) = TrustchainAPI::resolve(&did, &resolver).await?;
// TODO: refactor conversion into trustchain-core resolve module
Ok(serde_json::to_string_pretty(&ResolutionResult {
context: Some(serde_json::Value::String(
Expand Down
4 changes: 2 additions & 2 deletions trustchain-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod config;
// TODO: uncomment on gui and mobile feature branches
// pub mod gui;
// pub mod mobile;
pub mod mobile;
mod mobile_bridge;
Loading
Loading