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

Commit

Permalink
feat: Add noosphere crate-based Swift package (#131)
Browse files Browse the repository at this point in the history
* feat: Add `noosphere` crate-based Swift package

BREAKING CHANGE: The `noosphere-api` Client now holds an owned key
instead of a reference.
  • Loading branch information
cdata authored Nov 9, 2022
1 parent e20680e commit e1204c2
Show file tree
Hide file tree
Showing 41 changed files with 1,577 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/noosphere_apple_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
brew: protobuf cmake
- name: 'Generate the header'
run: |
cd rust/noosphere/include
cd rust/noosphere/include/noosphere
cargo run --example generate_header --features headers --locked
cd -
- uses: actions/upload-artifact@v3
Expand Down
30 changes: 30 additions & 0 deletions Cargo.lock

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

37 changes: 37 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
name: "SwiftNoosphere",
platforms: [
.iOS(.v13),
.macOS(.v11)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SwiftNoosphere",
targets: ["SwiftNoosphere"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SwiftNoosphere",
dependencies: ["LibNoosphere"],
path: "swift/Sources/SwiftNoosphere"),
.binaryTarget(
name: "LibNoosphere",
url: "https://github.com/subconsciousnetwork/swift-noosphere/releases/download/v0.1.2-alpha.1/libnoosphere.zip",
checksum: "462dc8c1c4207efbf71ead4e0dfe70a7d2153ebd4189b7eb5d9c0c9c3dd68d6e"),
.testTarget(
name: "SwiftNoosphereTests",
dependencies: ["SwiftNoosphere"],
path: "swift/Tests/SwiftNoosphereTests"),
]
)
22 changes: 11 additions & 11 deletions rust/noosphere-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,33 @@ use ucan::{
};
use url::Url;

pub struct Client<'a, K, S>
pub struct Client<K, S>
where
K: KeyMaterial,
K: KeyMaterial + 'static,
S: UcanStore,
{
pub session: IdentifyResponse,
pub sphere_identity: String,
pub api_base: Url,
pub credential: &'a K,
pub credential: K,
pub authorization: Authorization,
pub store: S,
client: reqwest::Client,
}

impl<'a, K, S> Client<'a, K, S>
impl<K, S> Client<K, S>
where
K: KeyMaterial,
K: KeyMaterial + 'static,
S: UcanStore,
{
pub async fn identify(
sphere_identity: &str,
api_base: &Url,
credential: &'a K,
credential: K,
authorization: &Authorization,
did_parser: &mut DidParser,
store: S,
) -> Result<Client<'a, K, S>> {
) -> Result<Client<K, S>> {
debug!("Initializing Noosphere API client");
debug!("Client represents sphere {}", sphere_identity);
debug!("Client targetting API at {}", api_base);
Expand All @@ -64,7 +64,7 @@ where

let (jwt, ucan_headers) = Self::make_bearer_token(
&gateway_identity,
credential,
&credential,
authorization,
&Capability {
with: With::Resource {
Expand Down Expand Up @@ -107,7 +107,7 @@ where

async fn make_bearer_token(
gateway_identity: &str,
credential: &'a K,
credential: &K,
authorization: &Authorization,
capability: &Capability<SphereReference, SphereAction>,
store: &S,
Expand Down Expand Up @@ -180,7 +180,7 @@ where

let (token, ucan_headers) = Self::make_bearer_token(
&self.session.gateway_identity,
self.credential,
&self.credential,
&self.authorization,
&capability,
&self.store,
Expand Down Expand Up @@ -219,7 +219,7 @@ where

let (token, ucan_headers) = Self::make_bearer_token(
&self.session.gateway_identity,
self.credential,
&self.credential,
&self.authorization,
&capability,
&self.store,
Expand Down
8 changes: 4 additions & 4 deletions rust/noosphere-cli/src/native/commands/serve/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod tests {
let client = Client::identify(
&client_sphere_identity,
&api_base,
&client_key,
client_key.clone(),
&client_authorization,
&mut did_parser,
client_db,
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {
let client = Client::identify(
&client_sphere_identity,
&api_base,
&client_key,
client_key.clone(),
&client_authorization,
&mut did_parser,
client_db.clone(),
Expand Down Expand Up @@ -395,7 +395,7 @@ mod tests {
let client = Client::identify(
&client_sphere_identity,
&api_base,
&client_key,
client_key.clone(),
&client_authorization,
&mut did_parser,
client_db.clone(),
Expand Down Expand Up @@ -556,7 +556,7 @@ mod tests {
let client = Client::identify(
&client_sphere_identity,
&api_base,
&client_key,
client_key.clone(),
&client_authorization,
&mut did_parser,
client_db.clone(),
Expand Down
12 changes: 6 additions & 6 deletions rust/noosphere-cli/src/native/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn sync(workspace: &Workspace) -> Result<()> {
let client = Client::identify(
&sphere_identity,
&gateway_url,
&key,
key.clone(),
&authorization,
&mut did_parser,
db.clone(),
Expand Down Expand Up @@ -75,9 +75,9 @@ pub async fn sync(workspace: &Workspace) -> Result<()> {

/// Attempts to push the latest local lineage to the gateway, causing the
/// gateway to update its own pointer to the tip of the local sphere's history
pub async fn push_local_changes<'a, S, K>(
pub async fn push_local_changes<S, K>(
local_sphere_identity: &str,
client: &Client<'a, K, SphereDb<S>>,
client: &Client<K, SphereDb<S>>,
db: &mut SphereDb<S>,
) -> Result<()>
where
Expand Down Expand Up @@ -172,10 +172,10 @@ where

/// Fetches the latest changes from a gateway and updates the local lineage
/// using a conflict-free rebase strategy
pub async fn sync_remote_changes<'a, K, S>(
pub async fn sync_remote_changes<K, S>(
local_sphere_identity: &str,
client: &Client<'a, K, SphereDb<S>>,
credential: &'a K,
client: &Client<K, SphereDb<S>>,
credential: &K,
authorization: Option<&Authorization>,
db: &mut SphereDb<S>,
) -> Result<()>
Expand Down
2 changes: 1 addition & 1 deletion rust/noosphere-cli/src/native/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ The available keys are:
/// Creates all the directories needed to start rendering a sphere in the
/// configured working file tree root
pub async fn initialize_local_directories(&self) -> Result<()> {
if let Ok(_) = self.expect_local_directories() {
if self.expect_local_directories().is_ok() {
return Err(anyhow!(
r#"Cannot initialize the sphere; a sphere is already initialized in {:?}
Unexpected (bad) things will happen if you try to nest spheres this way!"#,
Expand Down
12 changes: 12 additions & 0 deletions rust/noosphere-core/src/data/headers/version.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use std::{convert::Infallible, fmt::Display, str::FromStr};

pub enum Version {
Expand Down Expand Up @@ -26,3 +27,14 @@ impl FromStr for Version {
})
}
}

impl TryFrom<Version> for u32 {
type Error = anyhow::Error;

fn try_from(value: Version) -> Result<Self, Self::Error> {
match value {
Version::V0 => Ok(0),
Version::Unknown(version) => Err(anyhow!("Unrecognized version: {}", version)),
}
}
}
5 changes: 4 additions & 1 deletion rust/noosphere-core/src/view/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
},
data::{
AuthorityIpld, Bundle, CidKey, ContentType, DelegationIpld, Header, MemoIpld,
RevocationIpld, SphereIpld, TryBundle,
RevocationIpld, SphereIpld, TryBundle, Version,
},
view::{Links, SphereMutation, SphereRevision, Timeline},
};
Expand Down Expand Up @@ -352,6 +352,9 @@ impl<S: BlockStore> Sphere<S> {
ContentType::Sphere.to_string(),
));

memo.headers
.push((Header::Version.to_string(), Version::V0.to_string()));

let capability = Capability {
with: With::Resource {
kind: Resource::Scoped(SphereReference {
Expand Down
Loading

0 comments on commit e1204c2

Please sign in to comment.