lit_rust_sdk/
types.rs

1use blsful::{Bls12381G2Impl, SignatureShare as BlsfulSignatureShare};
2use serde::{Deserialize, Serialize};
3use std::{collections::HashMap, fmt};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct HandshakeRequest {
7    #[serde(rename = "clientPublicKey")]
8    pub client_public_key: String,
9    pub challenge: String,
10}
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct HandshakeResponse {
14    #[serde(rename = "serverPublicKey")]
15    pub server_pub_key: String,
16    #[serde(rename = "subnetPublicKey")]
17    pub subnet_pub_key: String,
18    #[serde(rename = "networkPublicKey")]
19    pub network_pub_key: String,
20    #[serde(rename = "networkPublicKeySet")]
21    pub network_pub_key_set: String,
22    #[serde(rename = "hdRootPubkeys")]
23    pub hd_root_pubkeys: Vec<String>,
24    #[serde(rename = "latestBlockhash")]
25    pub latest_blockhash: String,
26    #[serde(rename = "clientSdkVersion", skip_serializing_if = "Option::is_none")]
27    pub client_sdk_version: Option<String>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub attestation: Option<serde_json::Value>,
30}
31
32#[derive(Debug, Clone)]
33pub struct NodeConnectionInfo {
34    pub url: String,
35    pub handshake_response: HandshakeResponse,
36}
37
38#[derive(Debug, Clone)]
39pub struct ConnectionState {
40    pub connected_nodes: Vec<String>,
41    pub server_keys: HashMap<String, HandshakeResponse>,
42    pub subnet_pub_key: Option<String>,
43    pub network_pub_key: Option<String>,
44    pub network_pub_key_set: Option<String>,
45    pub hd_root_pubkeys: Option<Vec<String>>,
46    pub latest_blockhash: Option<String>,
47}
48
49// PKP related types
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct PKP {
52    #[serde(rename = "tokenId")]
53    pub token_id: String,
54    #[serde(rename = "publicKey")]
55    pub public_key: String,
56    #[serde(rename = "ethAddress")]
57    pub eth_address: String,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct AuthSig {
62    pub sig: String,
63    #[serde(rename = "derivedVia")]
64    pub derived_via: String,
65    #[serde(rename = "signedMessage")]
66    pub signed_message: String,
67    pub address: String,
68    pub algo: Option<String>,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct AuthMethod {
73    #[serde(rename = "authMethodType")]
74    pub auth_method_type: u32,
75    #[serde(rename = "accessToken")]
76    pub access_token: String,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct LitResourceAbilityRequest {
81    pub resource: LitResourceAbilityRequestResource,
82    pub ability: String,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct LitResourceAbilityRequestResource {
88    /// The resource ID
89    pub resource: String,
90    /// The resource prefix
91    pub resource_prefix: String,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
95pub enum LitAbility {
96    // Used by top level auth sigs
97    AccessControlConditionDecryption,
98    AccessControlConditionSigning,
99    PKPSigning,
100    RateLimitIncreaseAuth,
101    LitActionExecution,
102}
103
104impl fmt::Display for LitAbility {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        match self {
107            LitAbility::AccessControlConditionDecryption => {
108                write!(f, "access-control-condition-decryption")
109            }
110            LitAbility::AccessControlConditionSigning => {
111                write!(f, "access-control-condition-signing")
112            }
113            LitAbility::PKPSigning => write!(f, "pkp-signing"),
114            LitAbility::RateLimitIncreaseAuth => write!(f, "rate-limit-increase-auth"),
115            LitAbility::LitActionExecution => write!(f, "lit-action-execution"),
116        }
117    }
118}
119
120// TODO: use these with helpers for creating resource ability requests
121// USER DEFINED
122// const LIT_RESOURCE_PREFIX_ACC: &str = "lit-accesscontrolcondition";
123// const LIT_RESOURCE_PREFIX_PKP: &str = "lit-pkp";
124// const LIT_RESOURCE_PREFIX_RLI: &str = "lit-ratelimitincrease";
125// const LIT_RESOURCE_PREFIX_LA: &str = "lit-litaction";
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct SessionSignature {
129    pub sig: String,
130    #[serde(rename = "derivedVia")]
131    pub derived_via: String,
132    #[serde(rename = "signedMessage")]
133    pub signed_message: String,
134    pub address: String,
135    pub algo: Option<String>,
136}
137
138pub type SessionSignatures = HashMap<String, SessionSignature>;
139
140#[derive(Debug, Clone, Serialize, Deserialize)]
141pub struct SignSessionKeyRequest {
142    #[serde(rename = "sessionKey")]
143    pub session_key: String,
144    #[serde(rename = "authMethods")]
145    pub auth_methods: Vec<AuthMethod>,
146    #[serde(rename = "pkpPublicKey")]
147    pub pkp_public_key: String,
148    #[serde(rename = "siweMessage")]
149    pub siwe_message: String,
150    #[serde(rename = "curveType")]
151    pub curve_type: String,
152    #[serde(rename = "epoch", skip_serializing_if = "Option::is_none")]
153    pub epoch: Option<u64>,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct CapacityDelegationRequest {
158    #[serde(rename = "capacityTokenId")]
159    pub capacity_token_id: String,
160    #[serde(rename = "delegateeAddresses")]
161    pub delegatee_addresses: Vec<String>,
162    pub uses: String,
163}
164
165#[derive(Debug, Clone, Serialize, Deserialize)]
166#[serde(rename_all = "camelCase")]
167pub struct SessionKeySignedMessage {
168    pub session_key: String,
169    pub resource_ability_requests: Vec<LitResourceAbilityRequest>,
170    pub capabilities: Vec<AuthSig>,
171    pub issued_at: String,
172    pub expiration: String,
173    pub node_address: String,
174}
175
176// Execute JS types
177#[derive(Debug, Clone, Serialize, Deserialize)]
178pub struct ExecuteJsParams {
179    pub code: Option<String>,
180    #[serde(rename = "ipfsId")]
181    pub ipfs_id: Option<String>,
182    #[serde(rename = "sessionSigs")]
183    pub session_sigs: SessionSignatures,
184    #[serde(rename = "authMethods")]
185    pub auth_methods: Option<Vec<AuthMethod>>,
186    #[serde(rename = "jsParams")]
187    pub js_params: Option<serde_json::Value>,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct ExecuteJsResponse {
192    pub claims: HashMap<String, serde_json::Value>,
193    pub signatures: Option<serde_json::Value>,
194    pub decryptions: Vec<serde_json::Value>,
195    pub response: serde_json::Value,
196    pub logs: String,
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct NodeShare {
201    pub success: bool,
202    #[serde(rename = "signedData")]
203    pub signed_data: HashMap<String, SignedData>,
204    #[serde(rename = "claimData")]
205    pub claim_data: HashMap<String, serde_json::Value>,
206    pub response: serde_json::Value,
207    pub logs: String,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
211#[serde(rename_all = "camelCase")]
212pub struct SignedData {
213    pub sig_type: String,
214    pub data_signed: String,
215    pub signature_share: String,
216    pub share_index: u32,
217    pub big_r: String,
218    pub public_key: String,
219    pub sig_name: String,
220}
221
222#[derive(Debug, Serialize, Deserialize, Clone)]
223#[serde(rename_all = "camelCase")]
224pub struct JsonSignSessionKeyResponseV1 {
225    pub result: String,
226    pub signature_share: BlsfulSignatureShare<Bls12381G2Impl>,
227    pub share_index: u32,
228    pub curve_type: String,
229    pub siwe_message: String,
230    pub data_signed: String,
231    pub bls_root_pubkey: String,
232}