11use crate :: { Client , Error , IndexQuery , Query } ;
2- use alto_types:: { Block , Finalized , Kind , Notarized , Seed } ;
2+ use alto_types:: { Block , Finalized , Kind , Notarized , NAMESPACE } ;
33use bytes:: Bytes ;
4+ use commonware_codec:: DecodeExt ;
5+ use commonware_consensus:: threshold_simplex:: types:: { Seed , Viewable } ;
6+ use commonware_cryptography:: Digestible ;
47use futures:: { channel:: mpsc:: unbounded, Stream , StreamExt } ;
58use tokio_tungstenite:: { connect_async, tungstenite:: Message as TMessage } ;
69
@@ -76,18 +79,21 @@ impl Client {
7679 return Err ( Error :: Failed ( result. status ( ) ) ) ;
7780 }
7881 let bytes = result. bytes ( ) . await . map_err ( Error :: Reqwest ) ?;
79- let result = Seed :: deserialize ( Some ( & self . public ) , & bytes) . ok_or ( Error :: InvalidData ) ?;
82+ let seed = Seed :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
83+ if !seed. verify ( NAMESPACE , self . public . as_ref ( ) ) {
84+ return Err ( Error :: InvalidSignature ) ;
85+ }
8086
8187 // Verify the seed matches the query
8288 match query {
8389 IndexQuery :: Latest => { }
8490 IndexQuery :: Index ( index) => {
85- if result . view != index {
86- return Err ( Error :: InvalidData ) ;
91+ if seed . view ( ) != index {
92+ return Err ( Error :: UnexpectedResponse ) ;
8793 }
8894 }
8995 }
90- Ok ( result )
96+ Ok ( seed )
9197 }
9298
9399 pub async fn notarization_upload ( & self , notarized : Bytes ) -> Result < ( ) , Error > {
@@ -116,19 +122,21 @@ impl Client {
116122 return Err ( Error :: Failed ( result. status ( ) ) ) ;
117123 }
118124 let bytes = result. bytes ( ) . await . map_err ( Error :: Reqwest ) ?;
119- let result =
120- Notarized :: deserialize ( Some ( & self . public ) , & bytes) . ok_or ( Error :: InvalidData ) ?;
125+ let notarized = Notarized :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
126+ if !notarized. verify ( self . public . as_ref ( ) ) {
127+ return Err ( Error :: InvalidSignature ) ;
128+ }
121129
122130 // Verify the notarization matches the query
123131 match query {
124132 IndexQuery :: Latest => { }
125133 IndexQuery :: Index ( index) => {
126- if result . proof . view != index {
127- return Err ( Error :: InvalidData ) ;
134+ if notarized . proof . view ( ) != index {
135+ return Err ( Error :: UnexpectedResponse ) ;
128136 }
129137 }
130138 }
131- Ok ( result )
139+ Ok ( notarized )
132140 }
133141
134142 pub async fn finalization_upload ( & self , finalized : Bytes ) -> Result < ( ) , Error > {
@@ -157,19 +165,21 @@ impl Client {
157165 return Err ( Error :: Failed ( result. status ( ) ) ) ;
158166 }
159167 let bytes = result. bytes ( ) . await . map_err ( Error :: Reqwest ) ?;
160- let result =
161- Finalized :: deserialize ( Some ( & self . public ) , & bytes) . ok_or ( Error :: InvalidData ) ?;
168+ let finalized = Finalized :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
169+ if !finalized. verify ( self . public . as_ref ( ) ) {
170+ return Err ( Error :: InvalidSignature ) ;
171+ }
162172
163173 // Verify the finalization matches the query
164174 match query {
165175 IndexQuery :: Latest => { }
166176 IndexQuery :: Index ( index) => {
167- if result . proof . view != index {
168- return Err ( Error :: InvalidData ) ;
177+ if finalized . proof . view ( ) != index {
178+ return Err ( Error :: UnexpectedResponse ) ;
169179 }
170180 }
171181 }
172- Ok ( result )
182+ Ok ( finalized )
173183 }
174184
175185 pub async fn block_get ( & self , query : Query ) -> Result < Payload , Error > {
@@ -188,22 +198,26 @@ impl Client {
188198 // Verify the block matches the query
189199 let result = match query {
190200 Query :: Latest => {
191- let result =
192- Finalized :: deserialize ( Some ( & self . public ) , & bytes) . ok_or ( Error :: InvalidData ) ?;
201+ let result = Finalized :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
202+ if !result. verify ( self . public . as_ref ( ) ) {
203+ return Err ( Error :: InvalidSignature ) ;
204+ }
193205 Payload :: Finalized ( Box :: new ( result) )
194206 }
195207 Query :: Index ( index) => {
196- let result =
197- Finalized :: deserialize ( Some ( & self . public ) , & bytes) . ok_or ( Error :: InvalidData ) ?;
208+ let result = Finalized :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
209+ if !result. verify ( self . public . as_ref ( ) ) {
210+ return Err ( Error :: InvalidSignature ) ;
211+ }
198212 if result. block . height != index {
199- return Err ( Error :: InvalidData ) ;
213+ return Err ( Error :: UnexpectedResponse ) ;
200214 }
201215 Payload :: Finalized ( Box :: new ( result) )
202216 }
203217 Query :: Digest ( digest) => {
204- let result = Block :: deserialize ( & bytes) . ok_or ( Error :: InvalidData ) ?;
218+ let result = Block :: decode ( bytes. as_ref ( ) ) . map_err ( Error :: InvalidData ) ?;
205219 if result. digest ( ) != digest {
206- return Err ( Error :: InvalidData ) ;
220+ return Err ( Error :: UnexpectedResponse ) ;
207221 }
208222 Payload :: Block ( result)
209223 }
0 commit comments