@@ -5,6 +5,7 @@ use std::{
5
5
time:: Duration ,
6
6
} ;
7
7
8
+ use cosmrs:: AccountId ;
8
9
use futures_util:: StreamExt ;
9
10
use k256:: ecdsa:: SigningKey ;
10
11
use quartz_contract_core:: {
@@ -15,10 +16,11 @@ use quartz_contract_core::{
15
16
} ,
16
17
instantiate:: CoreInstantiate ,
17
18
} ,
18
- state:: { Config , LightClientOpts , Nonce , Session } ,
19
+ state:: { Config , LightClientOpts , Nonce , Session , SESSION_KEY } ,
19
20
} ;
20
21
use quartz_cw_proof:: proof:: {
21
22
cw:: { CwProof , RawCwProof } ,
23
+ key:: CwAbciKey ,
22
24
Proof ,
23
25
} ;
24
26
use quartz_proto:: quartz:: {
@@ -106,13 +108,19 @@ impl QuartzServer {
106
108
pub fn new < A > (
107
109
config : Config ,
108
110
sk : Arc < Mutex < Option < SigningKey > > > ,
111
+ contract : Arc < Mutex < Option < AccountId > > > ,
109
112
attestor : A ,
110
113
ws_config : WsListenerConfig ,
111
114
) -> Self
112
115
where
113
116
A : Attestor + Clone ,
114
117
{
115
- let core_service = CoreServer :: new ( CoreService :: new ( config, sk. clone ( ) , attestor. clone ( ) ) ) ;
118
+ let core_service = CoreServer :: new ( CoreService :: new (
119
+ config,
120
+ contract. clone ( ) ,
121
+ sk. clone ( ) ,
122
+ attestor. clone ( ) ,
123
+ ) ) ;
116
124
117
125
Self {
118
126
router : Server :: builder ( ) . add_service ( core_service) ,
@@ -184,6 +192,7 @@ impl QuartzServer {
184
192
pub struct CoreService < A > {
185
193
config : Config ,
186
194
nonce : Arc < Mutex < Nonce > > ,
195
+ contract : Arc < Mutex < Option < AccountId > > > ,
187
196
sk : Arc < Mutex < Option < SigningKey > > > ,
188
197
attestor : A ,
189
198
}
@@ -192,10 +201,16 @@ impl<A> CoreService<A>
192
201
where
193
202
A : Attestor ,
194
203
{
195
- pub fn new ( config : Config , sk : Arc < Mutex < Option < SigningKey > > > , attestor : A ) -> Self {
204
+ pub fn new (
205
+ config : Config ,
206
+ contract : Arc < Mutex < Option < AccountId > > > ,
207
+ sk : Arc < Mutex < Option < SigningKey > > > ,
208
+ attestor : A ,
209
+ ) -> Self {
196
210
Self {
197
211
config,
198
212
nonce : Arc :: new ( Mutex :: new ( [ 0u8 ; 32 ] ) ) ,
213
+ contract,
199
214
sk,
200
215
attestor,
201
216
}
@@ -226,12 +241,19 @@ where
226
241
227
242
async fn session_create (
228
243
& self ,
229
- _request : Request < RawSessionCreateRequest > ,
244
+ request : Request < RawSessionCreateRequest > ,
230
245
) -> TonicResult < Response < RawSessionCreateResponse > > {
231
246
// FIXME(hu55a1n1) - disallow calling more than once
247
+ let deployed_contract: AccountId = serde_json:: from_str ( & request. into_inner ( ) . message )
248
+ . map_err ( |e| Status :: invalid_argument ( e. to_string ( ) ) ) ?;
249
+
250
+ let mut contract = self . contract . lock ( ) . unwrap ( ) ;
251
+ * contract = Some ( deployed_contract. clone ( ) ) ;
252
+
232
253
let mut nonce = self . nonce . lock ( ) . unwrap ( ) ;
233
254
* nonce = rand:: thread_rng ( ) . gen :: < Nonce > ( ) ;
234
- let msg = SessionCreate :: new ( * nonce) ;
255
+
256
+ let msg = SessionCreate :: new ( * nonce, deployed_contract. to_string ( ) ) ;
235
257
236
258
let attestation = self
237
259
. attestor
@@ -253,8 +275,15 @@ where
253
275
serde_json:: from_str ( & request. into_inner ( ) . message )
254
276
. map_err ( |e| Status :: invalid_argument ( e. to_string ( ) ) ) ?;
255
277
278
+ let contract = self . contract . lock ( ) . unwrap ( ) . clone ( ) ;
279
+
256
280
let ( value, _msg) = proof
257
- . verify ( self . config . light_client_opts ( ) )
281
+ . verify (
282
+ self . config . light_client_opts ( ) ,
283
+ contract. expect ( "contract not set" ) ,
284
+ SESSION_KEY . to_string ( ) ,
285
+ None ,
286
+ )
258
287
. map_err ( Status :: failed_precondition) ?;
259
288
260
289
let session: Session = serde_json:: from_slice ( & value) . unwrap ( ) ;
@@ -290,7 +319,13 @@ pub struct ProofOfPublication<M> {
290
319
}
291
320
292
321
impl < M > ProofOfPublication < M > {
293
- pub fn verify ( self , light_client_opts : & LightClientOpts ) -> Result < ( Vec < u8 > , M ) , String > {
322
+ pub fn verify (
323
+ self ,
324
+ light_client_opts : & LightClientOpts ,
325
+ contract_address : AccountId ,
326
+ storage_key : String ,
327
+ storage_namespace : Option < String > ,
328
+ ) -> Result < ( Vec < u8 > , M ) , String > {
294
329
let config_trust_threshold = light_client_opts. trust_threshold ( ) ;
295
330
let trust_threshold =
296
331
TrustThreshold :: new ( config_trust_threshold. 0 , config_trust_threshold. 1 ) . unwrap ( ) ;
@@ -322,6 +357,11 @@ impl<M> ProofOfPublication<M> {
322
357
. and_then ( |mut primary| primary. verify_to_height ( target_height) )
323
358
. map_err ( |e| e. to_string ( ) ) ?;
324
359
360
+ let key = CwAbciKey :: new ( contract_address, storage_key, storage_namespace) ;
361
+ if key. into_vec ( ) != self . merkle_proof . key ( ) {
362
+ return Err ( "Merkle proof key mismatch" . to_string ( ) ) ;
363
+ }
364
+
325
365
let proof = CwProof :: from ( self . merkle_proof ) ;
326
366
proof
327
367
. verify (
0 commit comments