@@ -8,6 +8,7 @@ import type { Socket } from "socket.io-client";
88
99import { DELIMITERS , WEB3_SESSION_HEADER_KEY } from "./constants" ;
1010import { Msg } from "./interfaces" ;
11+ import { decodeMsgData , encodeMsgData } from "./msgEncoding" ;
1112import { getEc } from "./utils" ;
1213
1314const MSG_READ_TIMEOUT = 10_000 ;
@@ -51,6 +52,8 @@ export class Client {
5152
5253 public _sLessThanHalf : boolean ;
5354
55+ public readonly _messageEncoding : "bytes" | "none" ;
56+
5457 private _precomputeComplete : number [ ] = [ ] ;
5558
5659 private _precomputeFailed : number [ ] = [ ] ;
@@ -78,7 +81,8 @@ export class Client {
7881 _share : string ,
7982 _pubKey : string ,
8083 _websocketOnly : boolean ,
81- _tssLib : WasmLib
84+ _tssLib : WasmLib ,
85+ _messageEncoding : "bytes" | "none" = "bytes"
8286 ) {
8387 if ( _parties . length !== _sockets . length ) {
8488 throw new Error ( "parties and sockets length must be equal, add null for client if necessary" ) ;
@@ -99,6 +103,7 @@ export class Client {
99103 this . _consumed = false ;
100104 this . _sLessThanHalf = true ;
101105 this . tssLib = _tssLib ;
106+ this . _messageEncoding = _messageEncoding ;
102107
103108 _sockets . forEach ( ( socket ) => {
104109 if ( socket ) {
@@ -108,12 +113,18 @@ export class Client {
108113
109114 // Add listener for incoming messages
110115 socket . on ( "send" , async ( data , cb ) => {
111- const { session, sender, recipient, msg_type, msg_data } = data ;
116+ const { session, sender, recipient, msg_type, msg_data, msg_data_encoded } = data ;
112117 if ( session !== this . session ) {
113118 this . log ( `ignoring message for a different session... client session: ${ this . session } , message session: ${ session } ` ) ;
114119 return ;
115120 }
116- this . pushMessage ( { session, sender, recipient, msg_type, msg_data } ) ;
121+ let msg_data_decoded = msg_data ;
122+ const messageEncoding = this . _messageEncoding ;
123+ if ( msg_data_encoded && messageEncoding === "bytes" ) {
124+ const buf = Buffer . from ( msg_data_encoded ) ;
125+ msg_data_decoded = decodeMsgData ( msg_type , buf ) ;
126+ }
127+ this . pushMessage ( { session, sender, recipient, msg_type, msg_data : msg_data_decoded } ) ;
117128 if ( cb ) cb ( ) ;
118129 } ) ;
119130 // Add listener for completion
@@ -219,6 +230,7 @@ export class Client {
219230 pubkey : this . pubKey ,
220231 notifyWebsocketId : this . sockets [ party ] . id ,
221232 sendWebsocket : this . sockets [ party ] . id ,
233+ message_encoding : this . _messageEncoding ,
222234 ...additionalParams ,
223235 } ) ,
224236 } )
@@ -444,13 +456,21 @@ if (globalThis.js_send_msg === undefined) {
444456 }
445457
446458 if ( tss_client . websocketOnly ) {
459+ let msgData : string | undefined = msg_data ;
460+ let encodedMsgData : Buffer | undefined ;
461+ if ( tss_client . _messageEncoding === "bytes" ) {
462+ msgData = undefined ;
463+ encodedMsgData = encodeMsgData ( msg_type , msg_data ) ;
464+ }
465+
447466 const socket = tss_client . sockets [ party ] ;
448467 socket . emit ( "send_msg" , {
449468 session,
450469 sender : self_index ,
451470 recipient : party ,
452471 msg_type,
453- msg_data,
472+ msg_data : msgData ,
473+ msg_data_encoded : encodedMsgData ,
454474 } ) ;
455475 } else {
456476 const sid = session . split ( DELIMITERS . Delimiter4 ) [ 1 ] ;
0 commit comments