1
1
use crate :: utils:: {
2
2
crypto:: { sha256hash, sign_bytes_recoverable} ,
3
3
get_current_time_nanos,
4
- payload:: TaskResponsePayload ,
5
4
} ;
6
5
use base64:: { prelude:: BASE64_STANDARD , Engine } ;
7
6
use core:: fmt;
@@ -10,19 +9,22 @@ use eyre::Result;
10
9
use libsecp256k1:: SecretKey ;
11
10
use serde:: { Deserialize , Serialize } ;
12
11
13
- /// A parsed message from gossipsub. When first received, the message data is simply a vector of bytes.
14
- /// We treat that bytearray as a stringified JSON object, and parse it into this struct.
15
- ///
16
- /// TODO: these are all available at protocol level as well
17
- /// - payload is the data itself
18
- /// - topic is available as TopicHash of Gossipsub
19
- /// - version is given within the Identify protocol
20
- /// - timestamp is available at protocol level via DataTransform
12
+ /// A message within Dria Knowledge Network.
21
13
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
22
14
pub struct DKNMessage {
15
+ /// Base64 encoded data
23
16
pub ( crate ) payload : String ,
17
+ /// The topic of the message, derived from `TopicHash`
18
+ ///
19
+ /// NOTE: This can be obtained via TopicHash in GossipSub
24
20
pub ( crate ) topic : String ,
21
+ /// The version of the Dria Compute Node
22
+ ///
23
+ /// NOTE: This can be obtained via Identify protocol version
25
24
pub ( crate ) version : String ,
25
+ /// The timestamp of the message, in nanoseconds
26
+ ///
27
+ /// NOTE: This can be obtained via DataTransform in GossipSub
26
28
pub ( crate ) timestamp : u128 ,
27
29
}
28
30
@@ -61,39 +63,6 @@ impl DKNMessage {
61
63
Self :: new ( signed_payload, topic)
62
64
}
63
65
64
- /// Creates the payload of a computation result.
65
- ///
66
- /// - Sign `task_id || payload` with node `self.secret_key`
67
- /// - Encrypt `result` with `task_public_key`
68
- /// TODO: this is not supposed to be here
69
- pub fn new_signed_encrypted_payload (
70
- payload : impl AsRef < [ u8 ] > ,
71
- task_id : & str ,
72
- encrypting_public_key : & [ u8 ] ,
73
- signing_secret_key : & SecretKey ,
74
- ) -> Result < TaskResponsePayload > {
75
- // create the message `task_id || payload`
76
- let mut preimage = Vec :: new ( ) ;
77
- preimage. extend_from_slice ( task_id. as_ref ( ) ) ;
78
- preimage. extend_from_slice ( payload. as_ref ( ) ) ;
79
-
80
- // sign the message
81
- // TODO: use `sign_recoverable` here instead?
82
- let digest = libsecp256k1:: Message :: parse ( & sha256hash ( preimage) ) ;
83
- let ( signature, recid) = libsecp256k1:: sign ( & digest, signing_secret_key) ;
84
- let signature: [ u8 ; 64 ] = signature. serialize ( ) ;
85
- let recid: [ u8 ; 1 ] = [ recid. serialize ( ) ] ;
86
-
87
- // encrypt payload itself
88
- let ciphertext = ecies:: encrypt ( encrypting_public_key, payload. as_ref ( ) ) ?;
89
-
90
- Ok ( TaskResponsePayload {
91
- ciphertext : hex:: encode ( ciphertext) ,
92
- signature : format ! ( "{}{}" , hex:: encode( signature) , hex:: encode( recid) ) ,
93
- task_id : task_id. to_string ( ) ,
94
- } )
95
- }
96
-
97
66
/// Decodes the base64 payload into bytes.
98
67
pub fn decode_payload ( & self ) -> Result < Vec < u8 > , base64:: DecodeError > {
99
68
BASE64_STANDARD . decode ( & self . payload )
@@ -161,6 +130,7 @@ impl TryFrom<libp2p::gossipsub::Message> for DKNMessage {
161
130
#[ cfg( test) ]
162
131
mod tests {
163
132
use super :: * ;
133
+ use crate :: utils:: payload:: TaskResponsePayload ;
164
134
use crate :: { utils:: crypto:: sha256hash, DriaComputeNodeConfig } ;
165
135
use ecies:: decrypt;
166
136
use libsecp256k1:: SecretKey ;
@@ -252,7 +222,7 @@ mod tests {
252
222
let task_public_key = PublicKey :: from_secret_key ( & task_secret_key) ;
253
223
254
224
// create payload
255
- let payload = DKNMessage :: new_signed_encrypted_payload (
225
+ let payload = TaskResponsePayload :: new (
256
226
RESULT ,
257
227
TASK_ID ,
258
228
& task_public_key. serialize ( ) ,
0 commit comments