Skip to content

Commit b87b9d7

Browse files
committed
feat(swift sdk): swift sdk implementation
1 parent cc5a00f commit b87b9d7

File tree

133 files changed

+11757
-669
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+11757
-669
lines changed

.github/workflows/ci-swift.yml

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,70 @@ jobs:
1717
- uses: actions/checkout@v4
1818
with:
1919
submodules: recursive
20+
- name: Checkout specific contracts submodule version
21+
run: |
22+
cd packages/contracts
23+
git fetch origin debug-logs:refs/remotes/origin/debug-logs
24+
git checkout origin/debug-logs # Rust/Swift SDK depends on an old version of contracts
25+
cd ../..
26+
2027
- name: Run sccache-cache
2128
uses: mozilla-actions/sccache-action@v0.0.4
22-
- run: rustup update stable && rustup default stable
29+
30+
- name: Install Rust
31+
run: rustup update stable && rustup default stable
32+
33+
- name: Install Anvil ZKsync Manually
34+
run: |
35+
SCRIPT_PATH=".github/workflows/scripts/install-anvil-zksync.sh"
36+
chmod +x "$SCRIPT_PATH"
37+
sh "$SCRIPT_PATH"
38+
39+
- name: Setup pnpm
40+
uses: pnpm/action-setup@v4
41+
with:
42+
version: 9.11.0
43+
44+
- name: Use Node.js
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: lts/Iron
48+
cache: pnpm
49+
50+
- name: Install dependencies
51+
run: pnpm install -r --frozen-lockfile
52+
53+
- name: Install contract dependencies
54+
run: pnpm install -r --frozen-lockfile
55+
working-directory: packages/contracts
56+
57+
- name: Build contracts
58+
run: pnpm build
59+
working-directory: packages/contracts
60+
61+
- name: Run rust tests
62+
run: cargo test
63+
working-directory: packages/sdk-platforms/rust/zksync-sso
64+
65+
- name: Start anvil-zksync node
66+
run: |
67+
anvil-zksync --cache=none run > anvil-zksync.log 2>&1 &
68+
# Wait a few seconds to ensure the node starts
69+
sleep 5
70+
# Verify it's running
71+
if ! pgrep -f "anvil-zksync"; then
72+
echo "Error: anvil-zksync failed to start"
73+
cat anvil-zksync.log
74+
exit 1
75+
fi
76+
77+
- name: Deploy contracts and create Swift config
78+
run: cargo run --bin cli -- deploy-contracts
79+
working-directory: packages/sdk-platforms/rust/zksync-sso
80+
2381
- name: Select Xcode 16.1
2482
run: sudo xcode-select -s /Applications/Xcode_16.1.app
83+
2584
- name: Select Simulator
2685
run: |
2786
UDID=$(xcrun simctl list devices | awk '/-- iOS 18.1 --/{flag=1; next} /--/{flag=0} flag' | grep "iPhone 16 Pro" | awk -F '[()]' '{print $2}' | head -1)
@@ -33,8 +92,10 @@ jobs:
3392
echo "SIMULATOR_UDID=$UDID" >> $GITHUB_ENV
3493
- name: Install swiftformat
3594
run: brew install swiftformat
95+
3696
- name: Build bindings
37-
run: sh packages/sdk-platforms/rust/zksync-sso/crates/ffi/build-swift-framework.sh
97+
run: sh packages/sdk-platforms/rust/zksync-sso/crates/ffi/build-swift-framework-ios-ci.sh
98+
3899
- name: Build & test Swift SDK
39100
run: |
40101
xcodebuild test \
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Define variables
6+
REPO_URL="https://github.com/matter-labs/anvil-zksync.git"
7+
RELEASE_VERSION="v0.3.2"
8+
RELEASE_FILE_NAME="anvil-zksync-${RELEASE_VERSION}-aarch64-apple-darwin.tar.gz"
9+
RELEASE_URL="https://github.com/matter-labs/anvil-zksync/releases/download/${RELEASE_VERSION}/${RELEASE_FILE_NAME}"
10+
INSTALL_DIR="/usr/local/bin"
11+
TEMP_DIR="$(mktemp -d)"
12+
13+
# Clone the anvil-zksync repository
14+
curl -L "$RELEASE_URL" -o "$TEMP_DIR/$RELEASE_FILE_NAME"
15+
16+
# Extract the binary
17+
echo "Extracting anvil-zksync..."
18+
tar -xzf "$TEMP_DIR/$RELEASE_FILE_NAME" -C "$INSTALL_DIR"
19+
20+
# Clean up temporary directory
21+
rm -rf "$TEMP_DIR"
22+
23+
# Verify installation
24+
echo "Verifying anvil-zksync installation..."
25+
if command -v anvil-zksync >/dev/null 2>&1; then
26+
echo "anvil-zksync installed successfully!"
27+
anvil-zksync --version
28+
else
29+
echo "Error: anvil-zksync not found in PATH" >&2
30+
exit 1
31+
fi

packages/sdk-platforms/rust/zksync-sso/Cargo.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,43 @@ rust-version = "1.83"
99
license = "Apache-2.0"
1010

1111
[workspace.dependencies]
12+
13+
# Alloy
14+
alloy = { version = "0.11.0", default-features = false, features = [
15+
"rlp",
16+
"serde",
17+
"rpc-types",
18+
"signer-local",
19+
"reqwest",
20+
"contract",
21+
"eip712",
22+
] }
23+
alloy-zksync = { git = "https://github.com/jackpooleyml/alloy-zksync", rev = "e96bb14e3ca6c3a8031e59494cf05fcedd12f07a" }
24+
25+
# Http
26+
url = "2.5.4"
27+
28+
# Serialization
29+
serde = { version = "1.0", features = ["derive"] }
30+
serde_json = "1.0"
31+
32+
# Async
33+
tokio = { version = "1.43.0", features = ["full"] }
34+
async-trait = "0.1"
35+
futures = "0.3"
36+
37+
# Error handling
38+
eyre = "0.6"
39+
thiserror = "2.0.11"
40+
41+
# Randomness
42+
rand = "0.9"
43+
44+
# Hex
45+
hex = "0.4"
46+
47+
# i18n
48+
icu = { version = "1.5.0", features = ["default", "serde", "std"] }
49+
icu_decimal = { version = "1.5.0", features = ["default"] }
50+
fixed_decimal = "0.5.6"
51+
fixed = "1.28.0"

packages/sdk-platforms/rust/zksync-sso/crates/cli/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ rust-version.workspace = true
66
license.workspace = true
77

88
[dependencies]
9+
clap = { version = "4.5.1", features = ["derive"] }
910
sdk = { path = "../sdk" }
11+
url = { workspace = true }
12+
tokio = { workspace = true }
13+
eyre = { workspace = true }
14+
serde_json = { workspace = true }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use eyre::Result;
2+
use sdk::{config::Config, utils::deployment_utils::deploy_contracts};
3+
use std::{fs, path::PathBuf};
4+
use url::Url;
5+
6+
pub async fn deploy_contracts_and_update_swift_config(
7+
node_url: Url,
8+
config_path: Option<PathBuf>,
9+
) -> Result<()> {
10+
println!("Deploying contracts to node: {}", node_url);
11+
12+
// Deploy contracts
13+
let contracts = deploy_contracts(node_url.clone()).await?;
14+
println!("Contracts deployed successfully:");
15+
println!(" AAFactory: {}", contracts.account_factory);
16+
println!(" WebAuthValidator: {}", contracts.passkey);
17+
println!(" SessionKeyValidator: {}", contracts.session);
18+
println!(" ExampleAuthServerPaymaster: {}", contracts.account_paymaster);
19+
20+
// Get config path (use provided path or default)
21+
let config_path =
22+
config_path.unwrap_or_else(Config::get_default_swift_config_path);
23+
println!("\nWriting config to path: {:?}", config_path);
24+
25+
// Create config and write to file
26+
let config = Config::new(contracts, node_url);
27+
config.write_json(&config_path)?;
28+
29+
// Read back and verify the written config
30+
println!("\nVerifying written config:");
31+
let written_json = fs::read_to_string(&config_path)?;
32+
println!("Written JSON content:\n{}", written_json);
33+
34+
// Parse the JSON to verify it's valid
35+
let written_config: Config = serde_json::from_str(&written_json)?;
36+
println!("\nParsed config verification:");
37+
println!(" Node URL: {}", written_config.node_url);
38+
println!(" AAFactory: {}", written_config.contracts.account_factory);
39+
println!(" WebAuthValidator: {}", written_config.contracts.passkey);
40+
println!(" SessionKeyValidator: {}", written_config.contracts.session);
41+
println!(
42+
" ExampleAuthServerPaymaster: {}",
43+
written_config.contracts.account_paymaster
44+
);
45+
46+
println!("\nSuccessfully updated and verified Swift config values");
47+
Ok(())
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use clap::{Parser, Subcommand};
2+
use std::path::PathBuf;
3+
use url::Url;
4+
5+
#[derive(Parser)]
6+
#[command(author, version, about, long_about = None)]
7+
struct Cli {
8+
#[command(subcommand)]
9+
command: Commands,
10+
}
11+
12+
#[derive(Subcommand)]
13+
enum Commands {
14+
/// Deploy contracts and update Swift config values
15+
DeployContracts {
16+
/// The URL of the zkSync node (defaults to http://localhost:8011/)
17+
#[arg(long, default_value = "http://localhost:8011/")]
18+
node_url: Url,
19+
20+
/// Path to write the config file (defaults to swift/ZKsyncSSO/Sources/ZKsyncSSO/Config/config.json)
21+
#[arg(long)]
22+
config_path: Option<PathBuf>,
23+
},
24+
}
25+
26+
pub async fn handle_cli() -> eyre::Result<()> {
27+
let cli = Cli::parse();
28+
29+
match cli.command {
30+
Commands::DeployContracts { node_url, config_path } => {
31+
super::deploy_contracts::deploy_contracts_and_update_swift_config(
32+
node_url,
33+
config_path,
34+
)
35+
.await?;
36+
}
37+
}
38+
39+
Ok(())
40+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod deploy_contracts;
2+
pub mod handle_cli;
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
fn main() {
2-
println!("{}", format!("{}", sdk::add(1, 1)));
1+
use cli::handle_cli::handle_cli;
2+
3+
#[tokio::main]
4+
async fn main() -> eyre::Result<()> {
5+
handle_cli().await?;
6+
7+
Ok(())
38
}

packages/sdk-platforms/rust/zksync-sso/crates/ffi/Cargo.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@ license.workspace = true
99
crate-type = ["staticlib"]
1010

1111
[dependencies]
12-
uniffi = { version = "0.28.3", features = ["cli"] }
12+
uniffi = { version = "0.28.3", features = ["cli", "tokio"] }
1313
sdk = { path = "../sdk" }
1414

15+
# Serialization
16+
serde = { workspace = true }
17+
serde_json = { workspace = true }
18+
19+
# Async
20+
tokio = { workspace = true }
21+
futures = { workspace = true }
22+
23+
# Error handling
24+
eyre = { workspace = true }
25+
thiserror = { workspace = true }
26+
1527
[build-dependencies]
1628
uniffi = { version = "0.28.3", features = ["build", "cli"] }

0 commit comments

Comments
 (0)