Skip to content

Commit af66e96

Browse files
committed
feat: sessions swift demo
1 parent 9a834cd commit af66e96

File tree

102 files changed

+4360
-981
lines changed

Some content is hidden

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

102 files changed

+4360
-981
lines changed

.github/workflows/ci-swift.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ jobs:
9999
fi
100100
echo "Simulator UDID: $UDID"
101101
echo "SIMULATOR_UDID=$UDID" >> $GITHUB_ENV
102+
102103
- name: Install swiftformat
103104
run: brew install swiftformat
104105

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"contracts": {
3-
"accountFactory": "0xda635aa336e8f5c1c3c19e6a0db6d601fa1e3e05",
3+
"accountFactory": "0x7452d866cdbf84762b77a99849f00c26daaf2da8",
44
"passkey": "0xa472581ea2aca6e6bd8ea6cca95d3e1297aa5ae3",
5-
"session": "0x6e2eef457e8a4dc33f6259ff0a933a2f94d64a8e",
6-
"accountPaymaster": "0x18d0069ac0e0431f2af792ec425950427b775f1d",
7-
"recovery": "0xe72da237538a1854535e9565dbee0b414f382698"
5+
"session": "0x11f853c85e282a542d8ee8f9cdd8fe6ce61badf1",
6+
"accountPaymaster": "0x3ec8307648f31c494caeab625e5f2c99bfbaa560",
7+
"recovery": "0x3a34dab058fd71b2fb0d9d9e7d3dd205f9072fc3"
88
},
99
"nodeUrl": "http://localhost:8011/",
1010
"deployWallet": {
11-
"privateKeyHex": "f1010e4119b26f6ceccb5a42b8dc6b2a10650d4f5943e4eb8285951aba264b6c"
11+
"privateKeyHex": "0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a"
1212
}
1313
}

packages/sdk-platforms/rust/zksync-sso/crates/cli/src/deploy_contracts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub async fn deploy_contracts_and_update_example_configs(
2020
println!(" ExampleAuthServerPaymaster: {}", contracts.account_paymaster);
2121
println!(" Recovery: {}", contracts.recovery);
2222

23-
let deploy_wallet = Some(DeployWallet::random());
23+
let deploy_wallet = Some(DeployWallet::rich_wallet());
2424
let config = Config::new(contracts, node_url.clone(), deploy_wallet);
2525

2626
for path in config_paths {

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ mod balance;
22
mod deployment;
33
mod fetch;
44
mod fund;
5+
mod owners;
56
mod send;
67
mod session;
78
mod transaction;
9+
mod validators;
810

911
#[derive(Debug, Clone, uniffi::Record)]
1012
pub struct Account {

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account/deployment.rs

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
use crate::config;
2+
use sdk::api::{
3+
account::{
4+
passkey::{
5+
passkey_parameters::PasskeyParameters as SdkPasskeyParameters,
6+
rp_id::{AndroidRpId as SdkAndroidRpId, RpId as SdkRpId},
7+
},
8+
session::decode_session_config,
9+
},
10+
utils::parse_address,
11+
};
212

313
#[derive(Debug, uniffi::Record)]
414
pub struct AndroidRpId {
515
pub origin: String,
616
pub rp_id: String,
717
}
818

9-
impl From<AndroidRpId> for sdk::api::account::deployment::AndroidRpId {
19+
impl From<AndroidRpId> for SdkAndroidRpId {
1020
fn from(android_rp_id: AndroidRpId) -> Self {
11-
sdk::api::account::deployment::AndroidRpId {
21+
SdkAndroidRpId {
1222
origin: android_rp_id.origin,
1323
rp_id: android_rp_id.rp_id,
1424
}
@@ -45,16 +55,12 @@ impl RpId {
4555
}
4656
}
4757

48-
impl From<RpId> for sdk::api::account::deployment::RpId {
58+
impl From<RpId> for SdkRpId {
4959
fn from(rp_id: RpId) -> Self {
5060
match rp_id {
51-
RpId::Apple(rp_id) => {
52-
sdk::api::account::deployment::RpId::Apple(rp_id)
53-
}
61+
RpId::Apple(rp_id) => SdkRpId::Apple(rp_id),
5462
RpId::Android(android_rp_id) => {
55-
sdk::api::account::deployment::RpId::Android(
56-
android_rp_id.into(),
57-
)
63+
SdkRpId::Android(android_rp_id.into())
5864
}
5965
}
6066
}
@@ -68,11 +74,9 @@ pub struct PasskeyParameters {
6874
pub rp_id: RpId,
6975
}
7076

71-
impl From<PasskeyParameters>
72-
for sdk::api::account::deployment::PasskeyParameters
73-
{
77+
impl From<PasskeyParameters> for SdkPasskeyParameters {
7478
fn from(passkey_parameters: PasskeyParameters) -> Self {
75-
sdk::api::account::deployment::PasskeyParameters {
79+
SdkPasskeyParameters {
7680
credential_raw_attestation_object: passkey_parameters
7781
.credential_raw_attestation_object,
7882
credential_raw_client_data_json: passkey_parameters
@@ -103,15 +107,44 @@ pub enum DeployAccountError {
103107

104108
#[error("Account already deployed")]
105109
AccountAlreadyDeployed,
110+
111+
#[error("Invalid session config: {0}")]
112+
InvalidSessionConfig(String),
113+
114+
#[error("Invalid K1 owners: {0}")]
115+
InvalidK1Owners(String),
106116
}
107117

108118
#[uniffi::export(async_runtime = "tokio")]
109119
pub async fn deploy_account(
110120
passkey_parameters: PasskeyParameters,
121+
initial_k1_owners: Option<Vec<String>>,
122+
initial_session_config_json: Option<String>,
111123
config: config::Config,
112124
) -> Result<super::Account, DeployAccountError> {
125+
let initial_k1_owners = initial_k1_owners
126+
.map(|k1_owners| {
127+
k1_owners
128+
.into_iter()
129+
.map(|k1_owner| parse_address(&k1_owner))
130+
.collect::<Result<Vec<_>, _>>()
131+
})
132+
.transpose()
133+
.map_err(|e| DeployAccountError::InvalidK1Owners(e.to_string()))?;
134+
135+
let initial_session = initial_session_config_json
136+
.map(|session_config| {
137+
decode_session_config(&session_config).map_err(|e| {
138+
DeployAccountError::InvalidSessionConfig(e.to_string())
139+
})
140+
})
141+
.transpose()
142+
.map_err(|e| DeployAccountError::InvalidSessionConfig(e.to_string()))?;
143+
113144
sdk::api::account::deployment::deploy_account(
114145
passkey_parameters.into(),
146+
initial_k1_owners,
147+
initial_session,
115148
&(config.try_into()
116149
as Result<sdk::config::Config, config::ConfigError>)
117150
.map_err(|e: config::ConfigError| {
@@ -127,11 +160,34 @@ pub async fn deploy_account(
127160
pub async fn deploy_account_with_unique_id(
128161
passkey_parameters: PasskeyParameters,
129162
unique_account_id: String,
163+
initial_k1_owners: Option<Vec<String>>,
164+
initial_session_config_json: Option<String>,
130165
config: config::Config,
131166
) -> Result<super::Account, DeployAccountError> {
167+
let initial_k1_owners = initial_k1_owners
168+
.map(|k1_owners| {
169+
k1_owners
170+
.into_iter()
171+
.map(|k1_owner| parse_address(&k1_owner))
172+
.collect::<Result<Vec<_>, _>>()
173+
})
174+
.transpose()
175+
.map_err(|e| DeployAccountError::InvalidK1Owners(e.to_string()))?;
176+
177+
let initial_session = initial_session_config_json
178+
.map(|session_config| {
179+
decode_session_config(&session_config).map_err(|e| {
180+
DeployAccountError::InvalidSessionConfig(e.to_string())
181+
})
182+
})
183+
.transpose()
184+
.map_err(|e| DeployAccountError::InvalidSessionConfig(e.to_string()))?;
185+
132186
sdk::api::account::deployment::deploy_account_with_unique_id(
133187
passkey_parameters.into(),
134188
unique_account_id,
189+
initial_k1_owners,
190+
initial_session,
135191
&(config.try_into()
136192
as Result<sdk::config::Config, config::ConfigError>)
137193
.map_err(|e: config::ConfigError| {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::config;
2+
use sdk::api::{
3+
account::owners::is_k1_owner as is_k1_owner_sdk, utils::parse_address,
4+
};
5+
6+
#[derive(Debug, uniffi::Record)]
7+
pub struct IsK1OwnerArgs {
8+
pub account: String,
9+
pub owner_address: String,
10+
}
11+
12+
#[derive(Debug, thiserror::Error, uniffi::Error)]
13+
pub enum IsK1OwnerError {
14+
#[error("{0}")]
15+
IsK1Owner(String),
16+
#[error("Invalid address: {0}")]
17+
InvalidAccountAddress(String),
18+
#[error("Invalid owner address: {0}")]
19+
InvalidOwnerAddress(String),
20+
#[error("Invalid config: {0}")]
21+
InvalidConfig(String),
22+
}
23+
24+
#[uniffi::export(async_runtime = "tokio")]
25+
pub async fn is_k1_owner(
26+
args: IsK1OwnerArgs,
27+
config: config::Config,
28+
) -> Result<bool, IsK1OwnerError> {
29+
let account = parse_address(&args.account)
30+
.map_err(|e| IsK1OwnerError::InvalidAccountAddress(e.to_string()))?;
31+
let owner_address = parse_address(&args.owner_address)
32+
.map_err(|e| IsK1OwnerError::InvalidOwnerAddress(e.to_string()))?;
33+
let result = is_k1_owner_sdk(
34+
account,
35+
owner_address,
36+
&(config.try_into()
37+
as Result<sdk::config::Config, config::ConfigError>)
38+
.map_err(|e: config::ConfigError| {
39+
IsK1OwnerError::InvalidConfig(e.to_string())
40+
})?,
41+
)
42+
.await
43+
.map_err(|e| IsK1OwnerError::IsK1Owner(e.to_string()))?;
44+
Ok(result)
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
pub mod client;
22
pub mod create;
3+
pub mod hash;
4+
pub mod revoke;
5+
pub mod state;

packages/sdk-platforms/rust/zksync-sso/crates/ffi/src/account/session/create.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ pub enum CreateSessionError {
4141
InvalidAddress(String),
4242
#[error("Invalid session config: {0}")]
4343
InvalidSessionConfig(String),
44-
#[error("Invalid session key: {0}")]
45-
InvalidSessionKey(String),
44+
#[error("Invalid config: {0}")]
45+
InvalidConfig(String),
46+
#[error("Invalid private key: {0}")]
47+
InvalidPrivateKey(String),
4648
#[error("Invalid paymaster: {0}")]
4749
InvalidPaymaster(String),
4850
}
@@ -68,15 +70,15 @@ pub async fn create_session(
6870
let args = SdkCreateSessionArgs { account, session_config, paymaster };
6971

7072
let sign_fn = sign_fn_from_private_key_hex(owner_private_key)
71-
.map_err(|e| CreateSessionError::CreateSession(e.to_string()))?;
73+
.map_err(|e| CreateSessionError::InvalidPrivateKey(e.to_string()))?;
7274

7375
let result = sdk::api::account::session::create::create_session(
7476
args,
7577
sign_fn,
7678
&(config.try_into()
7779
as Result<sdk::config::Config, config::ConfigError>)
7880
.map_err(|e: config::ConfigError| {
79-
CreateSessionError::CreateSession(e.to_string())
81+
CreateSessionError::InvalidConfig(e.to_string())
8082
})?,
8183
)
8284
.await
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use sdk::api::account::session::{
2+
decode_session_config, hash::get_session_hash as sdk_get_session_hash,
3+
};
4+
5+
#[derive(Debug, thiserror::Error, uniffi::Error)]
6+
pub enum GetSessionHashError {
7+
#[error("{0}")]
8+
GetSessionHash(String),
9+
#[error("Invalid session config: {0}")]
10+
InvalidSessionConfig(String),
11+
}
12+
13+
#[uniffi::export]
14+
pub fn get_session_hash(
15+
session_config_json: String,
16+
) -> Result<String, GetSessionHashError> {
17+
let session_config =
18+
decode_session_config(&session_config_json).map_err(|e| {
19+
GetSessionHashError::InvalidSessionConfig(e.to_string())
20+
})?;
21+
22+
let hash = sdk_get_session_hash(session_config)
23+
.map_err(|e| GetSessionHashError::GetSessionHash(e.to_string()))?;
24+
25+
Ok(format!("{:#x}", hash.fixed_bytes()))
26+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use crate::config;
2+
use sdk::api::{
3+
account::session::revoke::RevokeSessionArgs as SdkRevokeSessionArgs,
4+
utils::{parse_address, sign_fn_from_private_key_hex},
5+
};
6+
7+
#[derive(Debug, uniffi::Record)]
8+
pub struct RevokeSessionArgs {
9+
pub account: String,
10+
pub session_id: String,
11+
pub owner_private_key: String,
12+
}
13+
14+
#[derive(Debug, uniffi::Record)]
15+
pub struct RevokeSessionReturnType {
16+
pub transaction_receipt_json: String,
17+
}
18+
19+
impl From<RevokeSessionReturnType>
20+
for sdk::api::account::session::revoke::RevokeSessionReturnType
21+
{
22+
fn from(revoke_session_return_type: RevokeSessionReturnType) -> Self {
23+
sdk::api::account::session::revoke::RevokeSessionReturnType {
24+
transaction_receipt_json: revoke_session_return_type
25+
.transaction_receipt_json,
26+
}
27+
}
28+
}
29+
30+
#[derive(Debug, thiserror::Error, uniffi::Error)]
31+
pub enum RevokeSessionError {
32+
#[error("{0}")]
33+
RevokeSession(String),
34+
#[error("Invalid address: {0}")]
35+
InvalidAddress(String),
36+
}
37+
38+
#[uniffi::export(async_runtime = "tokio")]
39+
pub async fn revoke_session(
40+
args: RevokeSessionArgs,
41+
config: config::Config,
42+
) -> Result<RevokeSessionReturnType, RevokeSessionError> {
43+
let account_address = parse_address(&args.account)
44+
.map_err(|e| RevokeSessionError::InvalidAddress(e.to_string()))?;
45+
46+
let sign_fn = sign_fn_from_private_key_hex(&args.owner_private_key)
47+
.map_err(|e| RevokeSessionError::RevokeSession(e.to_string()))?;
48+
49+
let sdk_args = SdkRevokeSessionArgs { session_id: args.session_id };
50+
51+
let result = sdk::api::account::session::revoke::revoke_session(
52+
sdk_args,
53+
account_address,
54+
sign_fn,
55+
&(config.try_into()
56+
as Result<sdk::config::Config, config::ConfigError>)
57+
.map_err(|e: config::ConfigError| {
58+
RevokeSessionError::RevokeSession(e.to_string())
59+
})?,
60+
)
61+
.await
62+
.map_err(|e| RevokeSessionError::RevokeSession(e.to_string()))?;
63+
64+
Ok(RevokeSessionReturnType {
65+
transaction_receipt_json: result.transaction_receipt_json,
66+
})
67+
}

0 commit comments

Comments
 (0)