@@ -15,13 +15,15 @@ import {
1515 CreateLibp2pOptions ,
1616 IEncoder ,
1717 ILightPush ,
18+ IMessage ,
1819} from "@waku/interfaces" ;
1920import { bootstrap } from "@libp2p/bootstrap" ;
2021import { EnrDecoder , TransportProtocol } from "@waku/enr" ;
2122import type { Multiaddr } from "@multiformats/multiaddr" ;
2223import type { ITestBrowser } from "../types/global.js" ;
2324import { Logger , StaticShardingRoutingInfo } from "@waku/utils" ;
2425import type { PeerId } from "@libp2p/interface" ;
26+ import { messageHashStr } from "@waku/core" ;
2527
2628const log = new Logger ( "waku-headless" ) ;
2729
@@ -32,6 +34,8 @@ export interface SerializableSDKProtocolResult {
3234 peerId ?: string ;
3335 } > ;
3436 myPeerId ?: string ;
37+ messageHash ?: string ;
38+ timestamp ?: number ;
3539}
3640
3741function makeSerializable ( result : { successes : PeerId [ ] , failures : Array < { error : any , peerId ?: PeerId } > } ) : SerializableSDKProtocolResult {
@@ -155,12 +159,9 @@ export class WakuHeadless {
155159 private async send (
156160 lightPush : ILightPush ,
157161 encoder : IEncoder ,
158- payload : Uint8Array ,
162+ message : IMessage ,
159163 ) {
160- return lightPush . send ( encoder , {
161- payload,
162- timestamp : new Date ( ) ,
163- } ) ;
164+ return lightPush . send ( encoder , message ) ;
164165 }
165166
166167 async pushMessageV3 (
@@ -192,6 +193,11 @@ export class WakuHeadless {
192193 processedPayload = new TextEncoder ( ) . encode ( payload ) ;
193194 }
194195
196+ const message : IMessage = {
197+ payload : processedPayload ,
198+ timestamp : new Date ( ) ,
199+ } ;
200+
195201 try {
196202 const lightPush = this . waku . lightPush ;
197203 if ( ! lightPush ) {
@@ -216,6 +222,11 @@ export class WakuHeadless {
216222 log . info ( "Pubsub topic:" , pubsubTopic ) ;
217223 log . info ( "Encoder pubsub topic:" , encoder . pubsubTopic ) ;
218224
225+ const protoObj = await encoder . toProtoObj ( message ) ;
226+ if ( ! protoObj ) {
227+ throw new Error ( "Failed to convert message to proto object" ) ;
228+ }
229+
219230 if ( pubsubTopic && pubsubTopic !== encoder . pubsubTopic ) {
220231 log . warn (
221232 `Explicit pubsubTopic ${ pubsubTopic } provided, but auto-sharding determined ${ encoder . pubsubTopic } . Using auto-sharding.` ,
@@ -229,7 +240,7 @@ export class WakuHeadless {
229240 this . lightpushNode ,
230241 ) ;
231242 if ( preferredPeerId ) {
232- result = await this . send ( lightPush , encoder , processedPayload ) ;
243+ result = await this . send ( lightPush , encoder , message ) ;
233244 log . info ( "✅ Message sent via preferred lightpush node" ) ;
234245 } else {
235246 throw new Error (
@@ -241,13 +252,22 @@ export class WakuHeadless {
241252 "Couldn't send message via preferred lightpush node:" ,
242253 error ,
243254 ) ;
244- result = await this . send ( lightPush , encoder , processedPayload ) ;
255+ result = await this . send ( lightPush , encoder , message ) ;
245256 }
246257 } else {
247- result = await this . send ( lightPush , encoder , processedPayload ) ;
258+ result = await this . send ( lightPush , encoder , message ) ;
248259 }
249260
250- const serializableResult = makeSerializable ( result ) ;
261+ let serializableResult = makeSerializable ( result ) ;
262+
263+ serializableResult . myPeerId = this . waku . libp2p . peerId . toString ( ) ;
264+
265+ const messageHash = '0x' + messageHashStr (
266+ encoder . pubsubTopic ,
267+ protoObj ,
268+ ) ;
269+
270+ serializableResult . messageHash = messageHash ;
251271
252272 return serializableResult ;
253273 } catch ( error ) {
0 commit comments