-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsession.rs
186 lines (158 loc) · 5.59 KB
/
session.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use account_sdk::abigen::controller::OutsideExecutionV3;
use account_sdk::account::outside_execution::{
OutsideExecution, OutsideExecutionAccount, OutsideExecutionCaller,
};
use account_sdk::account::session::account::SessionAccount;
use account_sdk::account::AccountHashAndCallsSigner;
use account_sdk::provider::{CartridgeJsonRpcProvider, CartridgeProvider};
use account_sdk::signers::Signer;
use account_sdk::utils::time::get_current_timestamp;
use serde_wasm_bindgen::to_value;
use starknet::accounts::{Account, ConnectedAccount};
use starknet::signers::SigningKey;
use starknet_types_core::felt::Felt;
use url::Url;
use wasm_bindgen::prelude::*;
use crate::types::call::JsCall;
use crate::types::session::Session;
use crate::types::{Felts, JsFelt};
type Result<T> = std::result::Result<T, JsError>;
#[wasm_bindgen]
pub struct CartridgeSessionAccount(SessionAccount);
#[wasm_bindgen]
impl CartridgeSessionAccount {
pub fn new(
rpc_url: String,
signer: JsFelt,
address: JsFelt,
chain_id: JsFelt,
session_authorization: Vec<JsFelt>,
session: Session,
) -> Result<CartridgeSessionAccount> {
let rpc_url = Url::parse(&rpc_url)?;
let provider = CartridgeJsonRpcProvider::new(rpc_url.clone());
let signer = Signer::Starknet(SigningKey::from_secret_scalar(signer.0));
let address = address.0;
let chain_id = chain_id.0;
let session_authorization = session_authorization
.into_iter()
.map(|felt| felt.0)
.collect::<Vec<_>>();
let policies = session
.policies
.into_iter()
.map(TryFrom::try_from)
.collect::<std::result::Result<Vec<_>, _>>()?;
let session = account_sdk::account::session::hash::Session::new(
policies,
session.expires_at,
&signer.clone().into(),
Felt::ZERO,
)?;
Ok(CartridgeSessionAccount(SessionAccount::new(
provider,
signer,
address,
chain_id,
session_authorization,
session,
)))
}
pub fn new_as_registered(
rpc_url: String,
signer: JsFelt,
address: JsFelt,
owner_guid: JsFelt,
chain_id: JsFelt,
session: Session,
) -> Result<CartridgeSessionAccount> {
let rpc_url = Url::parse(&rpc_url)?;
let provider = CartridgeJsonRpcProvider::new(rpc_url.clone());
let signer = Signer::Starknet(SigningKey::from_secret_scalar(signer.0));
let address = address.0;
let chain_id = chain_id.0;
let policies = session
.policies
.into_iter()
.map(TryFrom::try_from)
.collect::<std::result::Result<Vec<_>, _>>()?;
let session = account_sdk::account::session::hash::Session::new(
policies,
session.expires_at,
&signer.clone().into(),
Felt::ZERO,
)?;
Ok(CartridgeSessionAccount(SessionAccount::new_as_registered(
provider,
signer,
address,
chain_id,
owner_guid.0,
session,
)))
}
pub async fn sign(&self, hash: JsFelt, calls: Vec<JsCall>) -> Result<Felts> {
let hash = hash.0;
let calls = calls
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?;
let res = self.0.sign_hash_and_calls(hash, &calls).await?;
Ok(Felts(res.into_iter().map(JsFelt).collect()))
}
pub async fn sign_transaction(&self, calls: Vec<JsCall>, max_fee: JsFelt) -> Result<Felts> {
let calls = calls
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?;
let nonce = self.0.get_nonce().await?;
let tx_hash = self
.0
.execute_v1(calls.clone())
.nonce(nonce)
.max_fee(max_fee.0)
.prepared()?
.transaction_hash(false);
let signature = self.0.sign_hash_and_calls(tx_hash, &calls).await?;
Ok(Felts(signature.into_iter().map(JsFelt).collect()))
}
pub async fn execute(&self, calls: Vec<JsCall>) -> Result<JsValue> {
let calls = calls
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?;
let res = self.0.execute_v1(calls).send().await.map_err(|e| {
JsError::new(&format!("Execution failed: {:?}", e))
})?;
Ok(to_value(&res)?)
}
pub async fn execute_from_outside(&self, calls: Vec<JsCall>) -> Result<JsValue> {
let caller = OutsideExecutionCaller::Any;
let calls = calls
.into_iter()
.map(TryInto::try_into)
.collect::<std::result::Result<Vec<_>, _>>()?;
let now = get_current_timestamp();
let outside_execution = OutsideExecutionV3 {
caller: caller.into(),
execute_after: 0_u64,
execute_before: now + 600,
calls,
nonce: (SigningKey::from_random().secret_scalar(), 1),
};
let signed = self
.0
.sign_outside_execution(OutsideExecution::V3(outside_execution.clone()))
.await?;
let res = self
.0
.provider()
.add_execute_outside_transaction(
OutsideExecution::V3(outside_execution),
self.0.address(),
signed.signature,
)
.await?;
Ok(to_value(&res)?)
}
}