Skip to content

Commit 964b408

Browse files
committed
Merge branch 'main' of github.com:LIT-Protocol/lit-rust-sdk
2 parents 1305a8e + 9206dd5 commit 964b408

File tree

5 files changed

+371
-160
lines changed

5 files changed

+371
-160
lines changed

README.md

Lines changed: 153 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Currently in Beta and only supports Datil, DatilDev, and DatilTest networks.
66

77
## Features
88

9+
- **Local Session Signatures**: Execute Lit Actions using only your Ethereum wallet (no PKP required)
910
- **PKP Management**: Mint and manage Programmable Key Pairs (PKPs)
1011
- **Session Signatures**: Generate and manage session signatures for authentication
1112
- **Lit Actions**: Execute JavaScript code on the Lit Network with access to PKP signing capabilities
@@ -53,7 +54,83 @@ async fn main() {
5354
}
5455
```
5556

56-
### Executing a Lit Action
57+
### Executing a Lit Action (Simple Approach - No PKP Required)
58+
59+
For most use cases, you can execute Lit Actions using only your Ethereum wallet without needing to mint PKPs:
60+
61+
```rust
62+
use lit_rust_sdk::{
63+
auth::load_wallet_from_env,
64+
types::{LitAbility, LitResourceAbilityRequest, LitResourceAbilityRequestResource},
65+
ExecuteJsParams, LitNetwork, LitNodeClient, LitNodeClientConfig,
66+
};
67+
use std::time::Duration;
68+
69+
const HELLO_WORLD_LIT_ACTION: &str = r#"
70+
const go = async () => {
71+
console.log("Hello from Lit Action!");
72+
Lit.Actions.setResponse({ response: "Hello World from Rust SDK!" });
73+
};
74+
go();
75+
"#;
76+
77+
#[tokio::main]
78+
async fn main() {
79+
// Load wallet from environment variable
80+
let wallet = load_wallet_from_env()
81+
.expect("Failed to load wallet from ETHEREUM_PRIVATE_KEY env var");
82+
83+
// Configure and connect to Lit Network
84+
let config = LitNodeClientConfig {
85+
lit_network: LitNetwork::DatilDev,
86+
alert_when_unauthorized: true,
87+
debug: true,
88+
connect_timeout: Duration::from_secs(30),
89+
check_node_attestation: false,
90+
};
91+
92+
let mut client = LitNodeClient::new(config)
93+
.await
94+
.expect("Failed to create client");
95+
96+
client.connect().await.expect("Failed to connect");
97+
98+
// Create resource ability requests for Lit Action execution
99+
let resource_ability_requests = vec![LitResourceAbilityRequest {
100+
resource: LitResourceAbilityRequestResource {
101+
resource: "*".to_string(),
102+
resource_prefix: "lit-litaction".to_string(),
103+
},
104+
ability: LitAbility::LitActionExecution.to_string(),
105+
}];
106+
107+
// Generate session signatures with your wallet (no PKP needed!)
108+
let expiration = (chrono::Utc::now() + chrono::Duration::minutes(10)).to_rfc3339();
109+
let session_sigs = client
110+
.get_local_session_sigs(&wallet, resource_ability_requests, &expiration)
111+
.await
112+
.expect("Failed to create local session signatures");
113+
114+
// Execute the Lit Action
115+
let execute_params = ExecuteJsParams {
116+
code: Some(HELLO_WORLD_LIT_ACTION.to_string()),
117+
ipfs_id: None,
118+
session_sigs,
119+
auth_methods: None,
120+
js_params: None,
121+
};
122+
123+
let response = client
124+
.execute_js(execute_params)
125+
.await
126+
.expect("Failed to execute Lit Action");
127+
128+
println!("Response: {:?}", response.response);
129+
println!("Logs: {}", response.logs);
130+
}
131+
```
132+
133+
### Executing a Lit Action (With PKP Signing)
57134

58135
```rust
59136
use lit_rust_sdk::{
@@ -179,6 +256,72 @@ let session_sigs = client
179256
.await?;
180257
```
181258

259+
### Local Session Signatures (No PKP Required)
260+
261+
For simpler use cases where you don't need PKP signing capabilities, you can create session signatures directly with your wallet. This allows you to execute Lit Actions without minting or managing PKPs.
262+
263+
```rust
264+
use lit_rust_sdk::{
265+
auth::load_wallet_from_env,
266+
types::{LitAbility, LitResourceAbilityRequest, LitResourceAbilityRequestResource},
267+
ExecuteJsParams, LitNetwork, LitNodeClient, LitNodeClientConfig,
268+
};
269+
270+
// Load your Ethereum wallet
271+
let wallet = load_wallet_from_env().expect("Failed to load wallet");
272+
273+
// Create resource ability requests for Lit Action execution
274+
let resource_ability_requests = vec![LitResourceAbilityRequest {
275+
resource: LitResourceAbilityRequestResource {
276+
resource: "*".to_string(),
277+
resource_prefix: "lit-litaction".to_string(),
278+
},
279+
ability: LitAbility::LitActionExecution.to_string(),
280+
}];
281+
282+
// Set expiration for session signatures
283+
let expiration = (chrono::Utc::now() + chrono::Duration::minutes(10)).to_rfc3339();
284+
285+
// Generate local session signatures using only your wallet
286+
let session_sigs = client
287+
.get_local_session_sigs(
288+
&wallet,
289+
resource_ability_requests,
290+
&expiration,
291+
)
292+
.await?;
293+
294+
// Execute Lit Actions with these session signatures
295+
let lit_action_code = r#"
296+
const go = async () => {
297+
console.log("Hello from Lit Action!");
298+
Lit.Actions.setResponse({ response: "Executed without PKP!" });
299+
};
300+
go();
301+
"#;
302+
303+
let execute_params = ExecuteJsParams {
304+
code: Some(lit_action_code.to_string()),
305+
ipfs_id: None,
306+
session_sigs,
307+
auth_methods: None,
308+
js_params: None,
309+
};
310+
311+
let response = client.execute_js(execute_params).await?;
312+
```
313+
314+
**Benefits of Local Session Signatures:**
315+
- **No PKP Required**: Execute Lit Actions using only your Ethereum wallet
316+
- **Simpler Setup**: No need to mint PKPs or manage distributed keys
317+
- **Cost Effective**: Avoid PKP minting costs for basic Lit Action execution
318+
- **Quick Start**: Ideal for testing and simple automation tasks
319+
320+
**Limitations:**
321+
- No access to PKP signing capabilities within Lit Actions
322+
- Cannot use `Lit.Actions.signEcdsa()` or other PKP-specific functions
323+
- Suitable for computation, data processing, and API calls, but not for signing operations
324+
182325
### Lit Actions
183326

184327
Lit Actions are JavaScript functions that execute on the Lit Network with access to PKP signing capabilities.
@@ -330,7 +473,8 @@ The main client for interacting with the Lit Network.
330473
- `connected_nodes() -> Vec<String>` - Get list of connected node URLs
331474
- `get_connection_state() -> ConnectionState` - Get detailed connection state
332475
- `execute_js(params: ExecuteJsParams) -> Result<ExecuteJsResponse>` - Execute a Lit Action
333-
- `get_pkp_session_sigs(...) -> Result<SessionSignatures>` - Generate session signatures
476+
- `get_pkp_session_sigs(...) -> Result<SessionSignatures>` - Generate session signatures with PKP
477+
- `get_local_session_sigs(wallet: &PrivateKeySigner, resource_ability_requests: Vec<LitResourceAbilityRequest>, expiration: &str) -> Result<SessionSignatures>` - Generate session signatures without PKP
334478

335479
### Authentication
336480

@@ -378,6 +522,13 @@ See `tests/execute_js_test.rs::test_execute_js_with_capacity_delegation_datil` f
378522
4. Generates session signatures
379523
5. Executes a Lit Action
380524

525+
### Local Session Signatures (No PKP)
526+
527+
See `tests/local_session_sigs_test.rs` for examples of executing Lit Actions without PKPs:
528+
529+
- `test_local_session_sigs_hello_world` - Basic Lit Action execution with local session signatures
530+
- `test_local_session_sigs_with_params` - Advanced Lit Action with computations and parameters
531+
381532
### Authentication Methods
382533

383534
See `tests/execute_js_test.rs::test_execute_js_with_auth_methods` for an example of passing multiple authentication methods to a Lit Action.

lit-rust-sdk/README.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Currently in Beta and only supports Datil, DatilDev, and DatilTest networks.
66

77
## Features
88

9+
- **Local Session Signatures**: Execute Lit Actions using only your Ethereum wallet (no PKP required)
910
- **PKP Management**: Mint and manage Programmable Key Pairs (PKPs)
1011
- **Session Signatures**: Generate and manage session signatures for authentication
1112
- **Lit Actions**: Execute JavaScript code on the Lit Network with access to PKP signing capabilities
@@ -25,12 +26,22 @@ tokio = { version = "1.40", features = ["full"] }
2526

2627
## Quick Start
2728

29+
Execute a Lit Action using only your Ethereum wallet (no PKP required):
30+
2831
```rust
29-
use lit_rust_sdk::{LitNetwork, LitNodeClient, LitNodeClientConfig};
32+
use lit_rust_sdk::{
33+
auth::load_wallet_from_env,
34+
types::{LitAbility, LitResourceAbilityRequest, LitResourceAbilityRequestResource},
35+
ExecuteJsParams, LitNetwork, LitNodeClient, LitNodeClientConfig,
36+
};
3037
use std::time::Duration;
3138

3239
#[tokio::main]
3340
async fn main() {
41+
// Load wallet from environment variable
42+
let wallet = load_wallet_from_env()
43+
.expect("Set ETHEREUM_PRIVATE_KEY environment variable");
44+
3445
// Configure and connect to Lit Network
3546
let config = LitNodeClientConfig {
3647
lit_network: LitNetwork::DatilDev,
@@ -46,7 +57,40 @@ async fn main() {
4657

4758
client.connect().await.expect("Failed to connect");
4859

49-
println!("Connected to {} nodes", client.connected_nodes().len());
60+
// Create session signatures without PKP
61+
let resource_ability_requests = vec![LitResourceAbilityRequest {
62+
resource: LitResourceAbilityRequestResource {
63+
resource: "*".to_string(),
64+
resource_prefix: "lit-litaction".to_string(),
65+
},
66+
ability: LitAbility::LitActionExecution.to_string(),
67+
}];
68+
69+
let expiration = (chrono::Utc::now() + chrono::Duration::minutes(10)).to_rfc3339();
70+
let session_sigs = client
71+
.get_local_session_sigs(&wallet, resource_ability_requests, &expiration)
72+
.await
73+
.expect("Failed to create session signatures");
74+
75+
// Execute Lit Action
76+
let execute_params = ExecuteJsParams {
77+
code: Some(r#"
78+
const go = async () => {
79+
console.log("Hello from Lit Action!");
80+
Lit.Actions.setResponse({ response: "Hello World!" });
81+
};
82+
go();
83+
"#.to_string()),
84+
ipfs_id: None,
85+
session_sigs,
86+
auth_methods: None,
87+
js_params: None,
88+
};
89+
90+
let response = client.execute_js(execute_params).await
91+
.expect("Failed to execute Lit Action");
92+
93+
println!("Response: {:?}", response.response);
5094
}
5195
```
5296

lit-rust-sdk/src/client/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::sync::Arc;
88
mod connect;
99
mod execute;
1010
mod pkp;
11+
mod session_sigs;
1112
mod state;
1213

1314
use crate::blockchain::staking::LibStakingStorage::Epoch;

0 commit comments

Comments
 (0)