@@ -9,13 +9,15 @@ import {
99 AutoSharding ,
1010 DEFAULT_CLUSTER_ID ,
1111 DEFAULT_NUM_SHARDS ,
12+ IMessage ,
1213 ShardId ,
1314 StaticSharding ,
1415} from "@waku/interfaces" ;
1516import { bootstrap } from "@libp2p/bootstrap" ;
1617import { EnrDecoder , TransportProtocol } from "@waku/enr" ;
1718import type { ITestBrowser } from "../types/global.js" ;
1819import { StaticShardingRoutingInfo } from "@waku/utils" ;
20+ import { messageHashStr } from "@waku/core" ;
1921
2022export interface SerializableSDKProtocolResult {
2123 successes : string [ ] ;
@@ -24,6 +26,7 @@ export interface SerializableSDKProtocolResult {
2426 peerId ?: string ;
2527 } > ;
2628 myPeerId ?: string ;
29+ messageHash ?: string ;
2730}
2831
2932function makeSerializable ( result : any ) : SerializableSDKProtocolResult {
@@ -173,6 +176,11 @@ export class WakuHeadless {
173176 processedPayload = new TextEncoder ( ) . encode ( payload ) ;
174177 }
175178
179+ const message : IMessage = {
180+ payload : processedPayload ,
181+ timestamp : new Date ( ) ,
182+ } ;
183+
176184 try {
177185 const lightPush = this . waku . lightPush ;
178186 if ( ! lightPush ) {
@@ -197,6 +205,11 @@ export class WakuHeadless {
197205 console . log ( "Pubsub topic:" , pubsubTopic ) ;
198206 console . log ( "Encoder pubsub topic:" , encoder . pubsubTopic ) ;
199207
208+ const protoObj = await encoder . toProtoObj ( message ) ;
209+ if ( ! protoObj ) {
210+ throw new Error ( "Failed to convert message to proto object" ) ;
211+ }
212+
200213 if ( pubsubTopic && pubsubTopic !== encoder . pubsubTopic ) {
201214 console . warn (
202215 `Explicit pubsubTopic ${ pubsubTopic } provided, but auto-sharding determined ${ encoder . pubsubTopic } . Using auto-sharding.` ,
@@ -210,10 +223,7 @@ export class WakuHeadless {
210223 this . lightpushNode ,
211224 ) ;
212225 if ( preferredPeerId ) {
213- result = await lightPush . send ( encoder , {
214- payload : processedPayload ,
215- timestamp : new Date ( ) ,
216- } ) ;
226+ result = await lightPush . send ( encoder , message ) ;
217227 console . log ( "✅ Message sent via preferred lightpush node" ) ;
218228 } else {
219229 throw new Error (
@@ -225,20 +235,25 @@ export class WakuHeadless {
225235 "Couldn't send message via preferred lightpush node:" ,
226236 error ,
227237 ) ;
228- result = await lightPush . send ( encoder , {
229- payload : processedPayload ,
230- timestamp : new Date ( ) ,
231- } ) ;
238+ result = await lightPush . send ( encoder , message ) ;
232239 }
233240 } else {
234- result = await lightPush . send ( encoder , {
235- payload : processedPayload ,
236- timestamp : new Date ( ) ,
237- } ) ;
241+ result = await lightPush . send ( encoder , message ) ;
238242 }
239243
240244 const serializableResult = makeSerializable ( result ) ;
241245
246+ serializableResult . myPeerId = this . waku . libp2p . peerId . toString ( ) ;
247+
248+ const messageHash = '0x' + messageHashStr (
249+ encoder . pubsubTopic ,
250+ protoObj ,
251+ ) ;
252+
253+ console . log ( "Message hash:" , messageHash ) ;
254+
255+ serializableResult . messageHash = messageHash ;
256+
242257 return serializableResult ;
243258 } catch ( error ) {
244259 console . error ( "Error sending message via v3 lightpush:" , error ) ;
0 commit comments