Skip to content

Commit 1836116

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

File tree

79 files changed

+2728
-259
lines changed

Some content is hidden

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

79 files changed

+2728
-259
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": "4efff3175ef36dbca4d150b0ad168b4c7aef57f1631f4d25f7abc626d2fe9f89"
1212
}
1313
}

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::config;
2+
use sdk::api::account::session::decode_session_config;
3+
use sdk::api::utils::parse_address;
24

35
#[derive(Debug, uniffi::Record)]
46
pub struct AndroidRpId {
@@ -103,15 +105,44 @@ pub enum DeployAccountError {
103105

104106
#[error("Account already deployed")]
105107
AccountAlreadyDeployed,
108+
109+
#[error("Invalid session config: {0}")]
110+
InvalidSessionConfig(String),
111+
112+
#[error("Invalid K1 owners: {0}")]
113+
InvalidK1Owners(String),
106114
}
107115

108116
#[uniffi::export(async_runtime = "tokio")]
109117
pub async fn deploy_account(
110118
passkey_parameters: PasskeyParameters,
119+
initial_k1_owners: Option<Vec<String>>,
120+
initial_session_config_json: Option<String>,
111121
config: config::Config,
112122
) -> Result<super::Account, DeployAccountError> {
123+
let initial_k1_owners = initial_k1_owners
124+
.map(|k1_owners| {
125+
k1_owners
126+
.into_iter()
127+
.map(|k1_owner| parse_address(&k1_owner))
128+
.collect::<Result<Vec<_>, _>>()
129+
})
130+
.transpose()
131+
.map_err(|e| DeployAccountError::InvalidK1Owners(e.to_string()))?;
132+
133+
let initial_session = initial_session_config_json
134+
.map(|session_config| {
135+
decode_session_config(&session_config).map_err(|e| {
136+
DeployAccountError::InvalidSessionConfig(e.to_string())
137+
})
138+
})
139+
.transpose()
140+
.map_err(|e| DeployAccountError::InvalidSessionConfig(e.to_string()))?;
141+
113142
sdk::api::account::deployment::deploy_account(
114143
passkey_parameters.into(),
144+
initial_k1_owners,
145+
initial_session,
115146
&(config.try_into()
116147
as Result<sdk::config::Config, config::ConfigError>)
117148
.map_err(|e: config::ConfigError| {
@@ -127,11 +158,34 @@ pub async fn deploy_account(
127158
pub async fn deploy_account_with_unique_id(
128159
passkey_parameters: PasskeyParameters,
129160
unique_account_id: String,
161+
initial_k1_owners: Option<Vec<String>>,
162+
initial_session_config_json: Option<String>,
130163
config: config::Config,
131164
) -> Result<super::Account, DeployAccountError> {
165+
let initial_k1_owners = initial_k1_owners
166+
.map(|k1_owners| {
167+
k1_owners
168+
.into_iter()
169+
.map(|k1_owner| parse_address(&k1_owner))
170+
.collect::<Result<Vec<_>, _>>()
171+
})
172+
.transpose()
173+
.map_err(|e| DeployAccountError::InvalidK1Owners(e.to_string()))?;
174+
175+
let initial_session = initial_session_config_json
176+
.map(|session_config| {
177+
decode_session_config(&session_config).map_err(|e| {
178+
DeployAccountError::InvalidSessionConfig(e.to_string())
179+
})
180+
})
181+
.transpose()
182+
.map_err(|e| DeployAccountError::InvalidSessionConfig(e.to_string()))?;
183+
132184
sdk::api::account::deployment::deploy_account_with_unique_id(
133185
passkey_parameters.into(),
134186
unique_account_id,
187+
initial_k1_owners,
188+
initial_session,
135189
&(config.try_into()
136190
as Result<sdk::config::Config, config::ConfigError>)
137191
.map_err(|e: config::ConfigError| {
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use crate::config;
2+
use sdk::api::{
3+
account::session::{
4+
decode_session_config,
5+
state::GetSessionStateArgs as SdkGetSessionStateArgs,
6+
},
7+
utils::parse_address,
8+
};
9+
10+
#[derive(Debug, uniffi::Record)]
11+
pub struct GetSessionStateArgs {
12+
pub account: String,
13+
pub session_config: String,
14+
}
15+
16+
#[derive(Debug, uniffi::Record)]
17+
pub struct GetSessionStateReturnType {
18+
pub session_state_json: String,
19+
}
20+
21+
#[derive(Debug, thiserror::Error, uniffi::Error)]
22+
pub enum GetSessionStateError {
23+
#[error("{0}")]
24+
GetSessionState(String),
25+
#[error("Invalid address: {0}")]
26+
InvalidAddress(String),
27+
#[error("Invalid session config: {0}")]
28+
InvalidSessionConfig(String),
29+
}
30+
31+
#[uniffi::export(async_runtime = "tokio")]
32+
pub async fn get_session_state(
33+
args: GetSessionStateArgs,
34+
config: config::Config,
35+
) -> Result<GetSessionStateReturnType, GetSessionStateError> {
36+
let session_config =
37+
decode_session_config(&args.session_config).map_err(|e| {
38+
GetSessionStateError::InvalidSessionConfig(e.to_string())
39+
})?;
40+
let account = parse_address(&args.account)
41+
.map_err(|e| GetSessionStateError::InvalidAddress(e.to_string()))?;
42+
43+
let sdk_args = SdkGetSessionStateArgs { account, session_config };
44+
45+
let result = sdk::api::account::session::state::get_session_state(
46+
sdk_args,
47+
&(config.try_into()
48+
as Result<sdk::config::Config, config::ConfigError>)
49+
.map_err(|e: config::ConfigError| {
50+
GetSessionStateError::GetSessionState(e.to_string())
51+
})?,
52+
)
53+
.await
54+
.map_err(|e| GetSessionStateError::GetSessionState(e.to_string()))?;
55+
56+
let session_state_json = serde_json::to_string(&result.session_state)
57+
.map_err(|e| GetSessionStateError::GetSessionState(e.to_string()))?;
58+
59+
Ok(GetSessionStateReturnType { session_state_json })
60+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::session::session_lib::session_spec::SessionSpec;
12
use crate::{
23
client::passkey::{
34
account_factory::{AccountParams, create_account},
@@ -56,6 +57,8 @@ pub struct DeployedAccountDetails {
5657

5758
pub async fn deploy_account(
5859
passkey_parameters: PasskeyParameters,
60+
initial_k1_owners: Option<Vec<Address>>,
61+
initial_session: Option<SessionSpec>,
5962
config: &Config,
6063
) -> eyre::Result<DeployedAccountDetails> {
6164
debug!("XDB deploy_account - passkey_parameters: {passkey_parameters:?}");
@@ -78,6 +81,8 @@ pub async fn deploy_account(
7881
expected_origin: Some(parsed_params.expected_origin),
7982
contracts: config.contracts,
8083
paymaster,
84+
initial_k1_owners,
85+
initial_session,
8186
..Default::default()
8287
};
8388

@@ -107,6 +112,8 @@ pub async fn deploy_account(
107112
pub async fn deploy_account_with_unique_id(
108113
passkey_parameters: PasskeyParameters,
109114
unique_account_id: String,
115+
initial_k1_owners: Option<Vec<Address>>,
116+
initial_session: Option<SessionSpec>,
110117
config: &Config,
111118
) -> eyre::Result<DeployedAccountDetails> {
112119
let parsed_params = parse_passkey_parameters(&passkey_parameters).await?;
@@ -127,6 +134,8 @@ pub async fn deploy_account_with_unique_id(
127134
&AccountParams {
128135
passkey_expected_origin: parsed_params.expected_origin,
129136
},
137+
initial_k1_owners,
138+
initial_session,
130139
paymaster,
131140
config,
132141
)

packages/sdk-platforms/rust/zksync-sso/crates/sdk/src/client/ecdsa/actions/deploy.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ mod tests {
323323
#[tokio::test]
324324
async fn test_deploy_account_with_initial_k1_owners_and_send_transaction()
325325
-> Result<()> {
326+
// Add delay to avoid test run timing out
327+
tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;
328+
326329
// Arrange
327330
let (anvil_zksync, config, _) =
328331
spawn_node_and_deploy_contracts().await?;

0 commit comments

Comments
 (0)