Skip to content

Commit 8b2af8d

Browse files
Merge pull request #83 from torusresearch/fix/build
Merge fix/build
2 parents 157ca38 + e0eb724 commit 8b2af8d

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

packages/tss-client/src/client.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Socket } from "socket.io-client";
88

99
import { DELIMITERS, WEB3_SESSION_HEADER_KEY } from "./constants";
1010
import { Msg } from "./interfaces";
11+
import { decodeMsgData, encodeMsgData } from "./msgEncoding";
1112
import { getEc } from "./utils";
1213

1314
const 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];
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const specialMessages = {
2+
ga1_array: { chunkSize: 64 },
3+
com_msg: { chunkSize: 32 },
4+
chal_msg: { chunkSize: 32 },
5+
msg_0_com: { chunkSize: 32 },
6+
msg_1_com: { chunkSize: 32 },
7+
};
8+
9+
function byteLenToBase64Len(byteLen: number): number {
10+
return Math.ceil(byteLen / 3) * 4;
11+
}
12+
13+
function encodeChunks(dataBase64: string, chunkSize: number): Buffer {
14+
const chunks: Buffer[] = [];
15+
for (let i = 0; i < dataBase64.length; i += chunkSize) {
16+
const chunkBase64 = dataBase64.substring(i, i + chunkSize);
17+
const chunk = Buffer.from(chunkBase64, "base64");
18+
chunks.push(chunk);
19+
}
20+
return Buffer.concat(chunks);
21+
}
22+
23+
export function encodeMsgData(msg_type: string, msg_data: string) {
24+
for (const [key, value] of Object.entries(specialMessages)) {
25+
if (msg_type.includes(key)) {
26+
const base64Len = byteLenToBase64Len(value.chunkSize);
27+
return encodeChunks(msg_data, base64Len);
28+
}
29+
}
30+
31+
return Buffer.from(msg_data, "base64");
32+
}
33+
34+
function decodeChunks(data: Buffer, chunkSize: number): string {
35+
let result = "";
36+
for (let i = 0; i < data.length; i += chunkSize) {
37+
const chunk = data.subarray(i, i + chunkSize);
38+
result += chunk.toString("base64");
39+
}
40+
return result;
41+
}
42+
43+
export function decodeMsgData(msg_type: string, encoded_msg_data: Buffer) {
44+
for (const [key, value] of Object.entries(specialMessages)) {
45+
if (msg_type.includes(key)) {
46+
return decodeChunks(encoded_msg_data, value.chunkSize);
47+
}
48+
}
49+
return encoded_msg_data.toString("base64");
50+
}

packages/web-example/src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export type TestConfigType = {
1818
userType: UserType;
1919
network: TORUS_SAPPHIRE_NETWORK_TYPE;
2020
verifier_id?: string;
21+
messageEncoding?: "bytes" | "none";
2122
};

packages/web-example/src/prod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ const runTest = async (testConfig: TestConfigType) => {
145145
const tssProcessingStart = window.performance.now();
146146
const lib = await tssLib.load();
147147

148-
const client = new Client(session, clientIndex, partyIndexes, endpoints, sockets, share, pubKey, true, lib);
148+
const client = new Client(session, clientIndex, partyIndexes, endpoints, sockets, share, pubKey, true, lib, testConfig.messageEncoding);
149149
client.log = (...args: unknown[]) => {
150150
log(testConfig.label, ...args);
151151
};
@@ -197,6 +197,7 @@ const runTest = async (testConfig: TestConfigType) => {
197197
label: "tss_signing_test",
198198
network: "sapphire_devnet",
199199
userType: "new",
200+
// messageEncoding: "bytes",
200201
});
201202
console.log("test succeeded");
202203
document.title = "Test succeeded";

0 commit comments

Comments
 (0)