From c5bb3bc9c7a2e23ebea7afc609a0c3e87285e780 Mon Sep 17 00:00:00 2001 From: BingBong Date: Thu, 25 Jan 2024 21:05:12 +0000 Subject: [PATCH] feat: absract signer --- .../keygenMessages/abstractKeygenBroadcast.ts | 16 ---- .../abstractKeygenDirectMessage.ts | 13 ++++ src/mpc/keygen/keygenMessages/broadcasts.ts | 28 +++++-- .../keygen/keygenMessages/directMessages.ts | 3 + src/mpc/keygen/types.ts | 1 + .../signMessages/abstractDirectMessage.ts | 16 +--- .../signMessages/abstractSignBroadcast.ts | 18 +---- src/mpc/signing/signMessages/broadcasts.ts | 21 +++--- .../signing/signMessages/directMessages.ts | 18 ++--- src/p2p/server.ts | 28 ++++--- src/protocol/abstractProtocolHandler.ts | 25 +++---- src/protocol/keygenProtocol.ts | 28 +++---- src/protocol/protocolMessageParser.ts | 74 +++++++++++++++++++ src/protocol/signingProtocol.ts | 32 ++++---- 14 files changed, 186 insertions(+), 135 deletions(-) create mode 100644 src/mpc/keygen/keygenMessages/abstractKeygenDirectMessage.ts create mode 100644 src/protocol/protocolMessageParser.ts diff --git a/src/mpc/keygen/keygenMessages/abstractKeygenBroadcast.ts b/src/mpc/keygen/keygenMessages/abstractKeygenBroadcast.ts index 45e42a2..56228a5 100644 --- a/src/mpc/keygen/keygenMessages/abstractKeygenBroadcast.ts +++ b/src/mpc/keygen/keygenMessages/abstractKeygenBroadcast.ts @@ -13,22 +13,6 @@ import { } from "./broadcasts"; export class AbstractKeygenBroadcast { - public readonly from: PartyId; - public readonly type: number; - - constructor(from: PartyId, type: number) { - this.from = from; - this.type = type; - } - - public toJSON(): - | KeygenBroadcastForRound2JSON - | KeygenBroadcastForRound3JSON - | KeygenBroadcastForRound4JSON - | KeygenBroadcastForRound5JSON { - throw new Error("toJSON method must be implemented in derived classes"); - } - public static fromJSON( json: | KeygenBroadcastForRound2JSON diff --git a/src/mpc/keygen/keygenMessages/abstractKeygenDirectMessage.ts b/src/mpc/keygen/keygenMessages/abstractKeygenDirectMessage.ts new file mode 100644 index 0000000..7e77f7d --- /dev/null +++ b/src/mpc/keygen/keygenMessages/abstractKeygenDirectMessage.ts @@ -0,0 +1,13 @@ +import { KeygenDirectMessageForRound4JSON } from "../types"; +import { KeygenDirectMessageForRound4 } from "./directMessages"; + +export class AbstractKeygenDirectMessage { + public static fromJSON(json: KeygenDirectMessageForRound4JSON): AbstractKeygenDirectMessage { + switch (json.type) { + case 4: + return KeygenDirectMessageForRound4.fromJSON(json as KeygenDirectMessageForRound4JSON); + default: + throw new Error("Invalid round type"); + } + } +} diff --git a/src/mpc/keygen/keygenMessages/broadcasts.ts b/src/mpc/keygen/keygenMessages/broadcasts.ts index 29682fe..9f2dc75 100644 --- a/src/mpc/keygen/keygenMessages/broadcasts.ts +++ b/src/mpc/keygen/keygenMessages/broadcasts.ts @@ -15,12 +15,15 @@ import { KeygenBroadcastForRound5JSON, } from "../types"; -export class KeygenBroadcastForRound2 extends AbstractKeygenBroadcast { +export class KeygenBroadcastForRound2 { public readonly commitment: Uint8Array; + public readonly from: PartyId; + public readonly type: 2; constructor(from: PartyId, commitment: Uint8Array) { - super(from, 2); + this.from = from; this.commitment = commitment; + this.type = 2; } public toJSON(): KeygenBroadcastForRound2JSON { @@ -37,7 +40,9 @@ export class KeygenBroadcastForRound2 extends AbstractKeygenBroadcast { } } -export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast { +export class KeygenBroadcastForRound3 { + public readonly from: PartyId; + public readonly type: 3; public readonly RID: bigint; public readonly C: bigint; public readonly vssPolynomial: Exponent; @@ -56,7 +61,8 @@ export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast { pedersenPublic: PedersenParams, decommitment: Uint8Array ) { - super(from, 3); + this.from = from; + this.type = 3; this.RID = RID; this.C = C; this.vssPolynomial = vssPolynomial; @@ -107,12 +113,15 @@ export class KeygenBroadcastForRound3 extends AbstractKeygenBroadcast { } } -export class KeygenBroadcastForRound4 extends AbstractKeygenBroadcast { +export class KeygenBroadcastForRound4 { + public readonly from: PartyId; + public readonly type: 4; public readonly modProof: ZkModProof; public readonly prmProof: ZkPrmProof; constructor(from: PartyId, modProof: ZkModProof, prmProof: ZkPrmProof) { - super(from, 4); + this.from = from; + this.type = 4; this.modProof = modProof; this.prmProof = prmProof; } @@ -136,11 +145,14 @@ export class KeygenBroadcastForRound4 extends AbstractKeygenBroadcast { } } -export class KeygenBroadcastForRound5 extends AbstractKeygenBroadcast { +export class KeygenBroadcastForRound5 { + public readonly from: PartyId; + public readonly type: 5; public readonly SchnorrResponse: ZkSchResponse; constructor(from: PartyId, SchnorrResponse: ZkSchResponse) { - super(from, 5); + this.from = from; + this.type = 5; this.SchnorrResponse = SchnorrResponse; } diff --git a/src/mpc/keygen/keygenMessages/directMessages.ts b/src/mpc/keygen/keygenMessages/directMessages.ts index e5e39ca..89a780a 100644 --- a/src/mpc/keygen/keygenMessages/directMessages.ts +++ b/src/mpc/keygen/keygenMessages/directMessages.ts @@ -5,12 +5,14 @@ import { KeygenDirectMessageForRound4JSON } from "../types"; export class KeygenDirectMessageForRound4 { public readonly from: PartyId; public readonly to: PartyId; + public readonly type: 4; public readonly share: bigint; public readonly facProof: ZkFacProof; private constructor(from: PartyId, to: PartyId, share: bigint, facProof: ZkFacProof) { this.from = from; this.to = to; + this.type = 4; this.share = share; this.facProof = facProof; } @@ -37,6 +39,7 @@ export class KeygenDirectMessageForRound4 { to: this.to, shareHex: this.share.toString(16), facProof: this.facProof.toJSON(), + type: 4, }; } diff --git a/src/mpc/keygen/types.ts b/src/mpc/keygen/types.ts index eeab4c3..f9780d5 100644 --- a/src/mpc/keygen/types.ts +++ b/src/mpc/keygen/types.ts @@ -179,4 +179,5 @@ export type KeygenDirectMessageForRound4JSON = { to: string; shareHex: string; facProof: ZkFacProofJSON; + type: 4; }; diff --git a/src/mpc/signing/signMessages/abstractDirectMessage.ts b/src/mpc/signing/signMessages/abstractDirectMessage.ts index 8841fe9..1ff7588 100644 --- a/src/mpc/signing/signMessages/abstractDirectMessage.ts +++ b/src/mpc/signing/signMessages/abstractDirectMessage.ts @@ -2,21 +2,7 @@ import { PartyId } from "../../keygen/partyKey"; import { SignMessageForRound2JSON, SignMessageForRound3JSON, SignMessageForRound4JSON } from "../types"; import { SignMessageForRound2, SignMessageForRound3, SignMessageForRound4 } from "./directMessages"; -export abstract class AbstractSignDirectMessage { - public readonly from: PartyId; - public readonly type: number; - public readonly to: PartyId; - - constructor(from: PartyId, to: PartyId, type: number) { - this.from = from; - this.type = type; - this.to = to; - } - - public toJSON(): SignMessageForRound2JSON | SignMessageForRound3JSON | SignMessageForRound4JSON { - throw new Error("toJSON method must be implemented in derived classes"); - } - +export class AbstractSignDirectMessage { public static fromJSON( json: SignMessageForRound2JSON | SignMessageForRound3JSON | SignMessageForRound4JSON ): AbstractSignDirectMessage { diff --git a/src/mpc/signing/signMessages/abstractSignBroadcast.ts b/src/mpc/signing/signMessages/abstractSignBroadcast.ts index 0d2a624..9585795 100644 --- a/src/mpc/signing/signMessages/abstractSignBroadcast.ts +++ b/src/mpc/signing/signMessages/abstractSignBroadcast.ts @@ -12,23 +12,7 @@ import { SignBroadcastForRound5, } from "./broadcasts"; -export abstract class AbstractSignBroadcast { - public readonly from: PartyId; - public readonly type: number; - - constructor(from: PartyId, type: number) { - this.from = from; - this.type = type; - } - - public toJSON(): - | SignBroadcastForRound2JSON - | SignBroadcastForRound3JSON - | SignBroadcastForRound4JSON - | SignBroadcastForRound5JSON { - throw new Error("toJSON method must be implemented in derived classes"); - } - +export class AbstractSignBroadcast { public static fromJSON( json: | SignBroadcastForRound2JSON diff --git a/src/mpc/signing/signMessages/broadcasts.ts b/src/mpc/signing/signMessages/broadcasts.ts index 843e0f7..9b50baf 100644 --- a/src/mpc/signing/signMessages/broadcasts.ts +++ b/src/mpc/signing/signMessages/broadcasts.ts @@ -9,15 +9,15 @@ import { } from "../types"; import { AbstractSignBroadcast } from "./abstractSignBroadcast"; -export class SignBroadcastForRound2 extends AbstractSignBroadcast { +export class SignBroadcastForRound2 { public readonly from: PartyId; public readonly K: bigint; // Paillier ciphertext public readonly G: bigint; // Paillier ciphertext - public readonly t; + public readonly type: 2; private constructor(from: PartyId, K: bigint, G: bigint) { - super(from, 2); this.from = from; + this.type = 2; this.K = K; this.G = G; } @@ -45,13 +45,14 @@ export class SignBroadcastForRound2 extends AbstractSignBroadcast { }; } } -export class SignBroadcastForRound3 extends AbstractSignBroadcast { +export class SignBroadcastForRound3 { + public readonly type: 3; public readonly from: PartyId; public readonly BigGammaShare: AffinePoint; public constructor(from: PartyId, BigGammaShare: AffinePoint) { - super(from, 2); this.from = from; + this.type = 3; this.BigGammaShare = BigGammaShare; } @@ -83,14 +84,15 @@ export class SignBroadcastForRound3 extends AbstractSignBroadcast { } } -export class SignBroadcastForRound4 extends AbstractSignBroadcast { +export class SignBroadcastForRound4 { + public readonly type: 4; public readonly from: PartyId; public readonly DeltaShare: bigint; public readonly BigDeltaShare: AffinePoint; private constructor(from: PartyId, DeltaShare: bigint, BigDeltaShare: AffinePoint) { - super(from, 4); this.from = from; + this.type = 4; this.DeltaShare = DeltaShare; this.BigDeltaShare = BigDeltaShare; } @@ -130,13 +132,14 @@ export class SignBroadcastForRound4 extends AbstractSignBroadcast { } } -export class SignBroadcastForRound5 extends AbstractSignBroadcast { +export class SignBroadcastForRound5 { + public readonly type: 5; public readonly from: PartyId; public readonly SigmaShare: bigint; private constructor(from: PartyId, SigmaShare: bigint) { - super(from, 2); this.from = from; + this.type = 5; this.SigmaShare = SigmaShare; } diff --git a/src/mpc/signing/signMessages/directMessages.ts b/src/mpc/signing/signMessages/directMessages.ts index 14fc62c..05d24e6 100644 --- a/src/mpc/signing/signMessages/directMessages.ts +++ b/src/mpc/signing/signMessages/directMessages.ts @@ -5,16 +5,16 @@ import { ZkLogstarProof } from "../../zk/logstar"; import { SignMessageForRound2JSON, SignMessageForRound3JSON, SignMessageForRound4JSON } from "../types"; import { AbstractSignDirectMessage } from "./abstractDirectMessage"; -export class SignMessageForRound4 extends AbstractSignDirectMessage { +export class SignMessageForRound4 { public readonly from: PartyId; public readonly to: PartyId; public readonly ProofLog: ZkLogstarProof; - public readonly type; + public readonly type: 4; public constructor(from: PartyId, to: PartyId, ProofLog: ZkLogstarProof) { - super(from, to, 4); this.from = from; this.to = to; + this.type = 4; this.ProofLog = ProofLog; } @@ -48,7 +48,7 @@ export class SignMessageForRound4 extends AbstractSignDirectMessage { } } -export class SignMessageForRound3 extends AbstractSignDirectMessage { +export class SignMessageForRound3 { public readonly from: PartyId; public readonly to: PartyId; public readonly DeltaD: bigint; // Ciphertext @@ -58,7 +58,7 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage { public readonly ChiF: bigint; // Ciphertext public readonly ChiProof: ZkAffgProof; public readonly ProofLog: ZkLogstarProof; - public readonly type; + public readonly type: 3; private constructor( from: PartyId, @@ -71,9 +71,9 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage { ChiProof: ZkAffgProof, ProofLog: ZkLogstarProof ) { - super(from, to, 3); this.from = from; this.to = to; + this.type = 3; this.DeltaD = DeltaD; this.DeltaF = DeltaF; this.DeltaProof = DeltaProof; @@ -149,16 +149,16 @@ export class SignMessageForRound3 extends AbstractSignDirectMessage { } } -export class SignMessageForRound2 extends AbstractSignDirectMessage { +export class SignMessageForRound2 { public readonly from: PartyId; public readonly to: PartyId; public readonly proofEnc: ZkEncProof; - public readonly type; + public readonly type: 2; private constructor(from: PartyId, to: PartyId, proofEnc: ZkEncProof) { - super(from, to, 2); this.from = from; this.to = to; + this.type = 2; this.proofEnc = proofEnc; } diff --git a/src/p2p/server.ts b/src/p2p/server.ts index 8927db0..b76de92 100644 --- a/src/p2p/server.ts +++ b/src/p2p/server.ts @@ -61,8 +61,10 @@ class P2pServer extends AppLogger { this.updateReplica(Number(this.NODE_ID), "CONNECT"); new ValidatorsGroup(this.validator.toString()); - new KeygenSessionManager(this.validator, []); - // new SigningSessionManager(this.validator, [], ""); + // this.signSessionProcessor = new SigningSessionManager(); + // this.keygenSessionProcessor = new KeygenSessionManager(); + // this.keygenSessionProcessor = new KeygenSessionManager(this.validator); + // this.signSessionProcessor = new SigningSessionManager(this.validator); this.initState(); } @@ -306,17 +308,18 @@ class P2pServer extends AppLogger { //handle keygen & pBFT consensus for broadcasts // console.log(message); if (!this.signSessionProcessor && message.type === MESSAGE_TYPE.signSessionInit) { - this.signSessionProcessor = new SigningSessionManager( - this.validator, - this.validators, - "hello" - ); - this.signSessionProcessor.init(this.threshold, this.validators); + // this.keygenSessionProcessor = new KeygenSessionManager(this.validator); + this.signSessionProcessor = new SigningSessionManager(); + // this.keygenSessionProcessor = new KeygenSessionManager(); + this.signSessionProcessor.init(this.threshold, this.validator, this.validators); + await delay(1000); + } + if (!this.keygenSessionProcessor && message.type === MESSAGE_TYPE.keygenInit) { + // this.keygenSessionProcessor = new KeygenSessionManager(this.validator, this.validators); + this.keygenSessionProcessor = new KeygenSessionManager(); + this.keygenSessionProcessor.init(this.threshold, this.validator, this.validators); + await delay(1000); } - // if (!this.keygenSessionProcessor && message.type === MESSAGE_TYPE.keygenInit) { - // this.keygenSessionProcessor = new KeygenSessionManager(this.validator, this.validators); - // this.keygenSessionProcessor.init(this.threshold, this.validators); - // } await this.signSessionProcessor?.handleSignSessionConsensusMessage(message); await this.keygenSessionProcessor?.handleKeygenConsensusMessage(message); await this.chain.handleBlockchainConsensusMessage(message); @@ -330,6 +333,7 @@ class P2pServer extends AppLogger { //handle keygen & pBFT consensus for direcct msgs await this.signSessionProcessor?.handleSignSessionConsensusMessage(message); // await KeygenSessionManager.handleKeygenConsensusMessage(message); + await this.keygenSessionProcessor?.handleKeygenConsensusMessage(message); await this.chain.handleBlockchainConsensusMessage(message); await callback(); } catch (error) { diff --git a/src/protocol/abstractProtocolHandler.ts b/src/protocol/abstractProtocolHandler.ts index cfe6b15..d56d3dc 100644 --- a/src/protocol/abstractProtocolHandler.ts +++ b/src/protocol/abstractProtocolHandler.ts @@ -1,13 +1,10 @@ import { Logger } from "winston"; -import { AllSignSessionRounds } from "../mpc/signing/index"; -import { AbstractSignDirectMessage } from "../mpc/signing/signMessages/abstractDirectMessage"; -import { AbstractSignBroadcast } from "../mpc/signing/signMessages/abstractSignBroadcast"; -import { SignSession } from "../mpc/signing/signSession"; +import { AppLogger } from "../http/middleware/logger"; import { Message as Msg } from "./message/message"; import { MessageQueueArray, MessageQueueMap } from "./message/messageQueue"; +import { ProtocolMessageParser } from "./protocolMessageParser"; import { ServerDirectMessage, ServerMessage, SessionState } from "./types"; import Validator from "./validators/validator"; -import { AppLogger } from "../http/middleware/logger"; export interface BaseProtocolHnadlerInterface { // startNewSession(): boolean; @@ -26,7 +23,8 @@ export abstract class AbstractProcolManager protected validators: string[] = []; protected selfId: string; public validator: Validator; - public protocolId: "cmd/sigm"; + public protocolId: string = "cmp/sign"; + public protocol: "sign" | "keygen"; public sessionInitialized: boolean | undefined; public threshold: number | undefined; @@ -41,14 +39,12 @@ export abstract class AbstractProcolManager public log: Logger; // initiate new session and all rounds - constructor(validator: Validator, validators: string[]) { + constructor(protocol: "sign" | "keygen") { super(); - this.validator = validator; - this.selfId = validator.nodeId; - this.validators = validators; + this.protocol = protocol; } - public abstract init(threshold: number, validators: string[]): void; + public abstract init(threshold: number, validator: Validator, validators: string[]): Promise; // public abstract startNewSession(): boolean; public abstract finalizeCurrentRound(currentRound: number): Promise; public abstract startNewRound(): void; @@ -62,7 +58,7 @@ export abstract class AbstractProcolManager this.messages .getRoundValues(currentRound - 1) - .map((broadcast) => AbstractSignBroadcast.fromJSON(broadcast as any)) + .map((broadcast) => ProtocolMessageParser.fromJSONB(broadcast as any, this.protocol)) .forEach((broadcast) => activeRound.handleBroadcastMessage(broadcast)); } @@ -72,7 +68,7 @@ export abstract class AbstractProcolManager this.directMessages .getRoundValues(currentRound - 1) - .map((directMsg) => AbstractSignDirectMessage.fromJSON(directMsg)) + .map((directMsg) => ProtocolMessageParser.fromJSOND(directMsg, this.protocol)) .filter((directMsg: any) => directMsg.to === this.selfId) .forEach((directMsg) => activeRound.handleDirectMessage(directMsg)); } @@ -111,11 +107,14 @@ export abstract class AbstractProcolManager } protected storePeerDirectMessageResponse(newDirectMessage: Msg, round: any, currentRound: number) { + console.log("madeee ittt"); + if ( round.isDirectMessageRound && newDirectMessage && this.validator.canAccept(newDirectMessage, this.session as any, this.selfId) ) { + console.log("madeee ittt"); this.directMessages.set(currentRound, newDirectMessage.Data); } return this.directMessages.getNonNullValuesLength(currentRound); diff --git a/src/protocol/keygenProtocol.ts b/src/protocol/keygenProtocol.ts index a69a4c4..3b37c58 100644 --- a/src/protocol/keygenProtocol.ts +++ b/src/protocol/keygenProtocol.ts @@ -1,14 +1,11 @@ import assert from "assert"; import axios from "axios"; import { createHash } from "crypto"; -import Flatted from "flatted"; +import { Logger } from "winston"; import config from "../config/config"; import { redisClient } from "../db/redis"; -import { AppLogger } from "../http/middleware/logger"; import { AllKeyGenRounds } from "../mpc/keygen"; import { AbstractKeygenRound, GenericKeygenRoundBroadcast } from "../mpc/keygen/abstractRound"; -import { AbstractKeygenBroadcast } from "../mpc/keygen/keygenMessages/abstractKeygenBroadcast"; -import { KeygenDirectMessageForRound4 } from "../mpc/keygen/keygenMessages/directMessages"; import { KeygenSession } from "../mpc/keygen/keygenSession"; import { PartySecretKeyConfig } from "../mpc/keygen/partyKey"; import { @@ -24,14 +21,12 @@ import { MESSAGE_TYPE } from "../p2p/types"; import { tryNTimes } from "../rpc/utils/helpers"; import { ErrorWithCode, ProtocolError } from "../utils/errors"; import { extractError } from "../utils/extractError"; +import { AbstractProcolManager } from "./abstractProtocolHandler"; import { app } from "./index"; import { Message as Msg } from "./message/message"; import { MessageQueueArray, MessageQueueMap } from "./message/messageQueue"; -import { KeygenCurrentState, KeygenMessageData, Round, Rounds, ServerDirectMessage, ServerMessage } from "./types"; +import { KeygenMessageData, Round, ServerDirectMessage, ServerMessage } from "./types"; import Validator from "./validators/validator"; -import TransactionPool from "../wallet/transactionPool"; -import { Logger } from "winston"; -import { AbstractProcolManager, BaseProtocolHnadlerInterface } from "./abstractProtocolHandler"; const KeygenRounds = Object.values(AllKeyGenRounds); @@ -42,15 +37,14 @@ export class KeygenSessionManager extends AbstractProcolManager { private broadcastRoundHashes: Record = {}; private directMessageRoundHashes: Record = {}; - constructor(validator: Validator, validators: string[]) { - super(validator, validators); - this.validator = validator; - this.selfId = validator.nodeId; + constructor() { + super("keygen"); } - public async init(threshold: number, validators: string[], validator?: Validator) { - this.threshold = threshold; + public async init(threshold: number, validator: Validator, validators: string[]) { this.validator = validator; + this.selfId = validator.nodeId; + this.threshold = threshold; this.validators = validators; this.startNewSession({ selfId: app.p2pServer.NODE_ID, @@ -63,8 +57,6 @@ export class KeygenSessionManager extends AbstractProcolManager { if (this.sessionInitialized || this.currentRound > 0) { throw new Error(`there is already a keygen session n progress`); } - this.init(sessionConfig.threshold, sessionConfig.partyIds); - this.directMessages = new MessageQueueArray(this.finalRound + 1); this.messages = new MessageQueueMap(this.validators, this.finalRound + 1); this.broadcastRoundHashes[0] = this.hashMessageData("0x0"); @@ -118,6 +110,7 @@ export class KeygenSessionManager extends AbstractProcolManager { //if we are on a dm round. wait until all nodes have collected their dms const dmsLen = this.directMessages.getNonNullValuesLength(currentRound); + // console.log(round.isDirectMessageRound, currentRound, this.threshold, dmsLen); if (round.isDirectMessageRound && dmsLen < this.threshold - 1) { await delay(200); await this.sessionRoundProcessor(data); @@ -417,6 +410,3 @@ export class KeygenSessionManager extends AbstractProcolManager { // this.validator.PartyKeyShare = undefined; } } - -const v = new Validator(); -export const maager = new KeygenSessionManager(v, []); diff --git a/src/protocol/protocolMessageParser.ts b/src/protocol/protocolMessageParser.ts new file mode 100644 index 0000000..1c59073 --- /dev/null +++ b/src/protocol/protocolMessageParser.ts @@ -0,0 +1,74 @@ +import { AbstractKeygenBroadcast } from "../mpc/keygen/keygenMessages/abstractKeygenBroadcast"; +import { AbstractKeygenDirectMessage } from "../mpc/keygen/keygenMessages/abstractKeygenDirectMessage"; +import { + KeygenBroadcastForRound2JSON, + KeygenBroadcastForRound3JSON, + KeygenBroadcastForRound4JSON, + KeygenBroadcastForRound5JSON, + KeygenDirectMessageForRound4JSON, +} from "../mpc/keygen/types"; +import { AbstractSignDirectMessage } from "../mpc/signing/signMessages/abstractDirectMessage"; +import { AbstractSignBroadcast } from "../mpc/signing/signMessages/abstractSignBroadcast"; +import { + SignBroadcastForRound2JSON, + SignBroadcastForRound3JSON, + SignBroadcastForRound4JSON, + SignBroadcastForRound5JSON, + SignMessageForRound2JSON, + SignMessageForRound3JSON, + SignMessageForRound4JSON, +} from "../mpc/signing/types"; + +type BroadcastSignReturn = + | SignBroadcastForRound2JSON + | SignBroadcastForRound3JSON + | SignBroadcastForRound4JSON + | SignBroadcastForRound5JSON; + +type BroadcastKeygenReturn = + | KeygenBroadcastForRound2JSON + | KeygenBroadcastForRound3JSON + | KeygenBroadcastForRound4JSON + | KeygenBroadcastForRound5JSON; + +type DirectMessageSignReturnType = SignMessageForRound2JSON | SignMessageForRound3JSON | SignMessageForRound4JSON; +type DirectMessageKeygenReturnType = KeygenDirectMessageForRound4JSON; + +type ProtoclBroadcastReturnType = BroadcastSignReturn | BroadcastKeygenReturn; +type ProtoclDirectMessageReturnType = DirectMessageSignReturnType | DirectMessageKeygenReturnType; + +export class ProtocolMessageParser { + public static fromJSONB( + json: ProtoclBroadcastReturnType, + protocol: "keygen" | "sign" + ): ProtoclBroadcastReturnType { + switch (protocol) { + case "keygen": + return AbstractKeygenBroadcast.fromJSON( + json as BroadcastKeygenReturn + ) as BroadcastKeygenReturn; + case "sign": + return AbstractSignBroadcast.fromJSON(json as BroadcastSignReturn) as BroadcastSignReturn; + default: + throw new Error("Invalid round parser broadcast type"); + } + } + + public static fromJSOND( + json: ProtoclDirectMessageReturnType, + protocol: "keygen" | "sign" + ): ProtoclDirectMessageReturnType { + switch (protocol) { + case "keygen": + return AbstractKeygenDirectMessage.fromJSON( + json as DirectMessageKeygenReturnType + ) as ProtoclDirectMessageReturnType; + case "sign": + return AbstractSignDirectMessage.fromJSON( + json as DirectMessageSignReturnType + ) as DirectMessageSignReturnType; + default: + throw new Error("Invalid round parser direct message type"); + } + } +} diff --git a/src/protocol/signingProtocol.ts b/src/protocol/signingProtocol.ts index 5a9d539..670ed8a 100644 --- a/src/protocol/signingProtocol.ts +++ b/src/protocol/signingProtocol.ts @@ -31,6 +31,7 @@ import { Message as Msg } from "./message/message"; import { MessageQueueArray, MessageQueueMap } from "./message/messageQueue"; import { ServerDirectMessage, ServerMessage } from "./types"; import Validator from "./validators/validator"; + const SignRounds = Object.values(AllSignSessionRounds); export type SignSessionCurrentState = { @@ -56,32 +57,29 @@ export class SigningSessionManager extends AbstractProcolManager { // verify initiators party key // initiate new session and all rounds - constructor(validator: Validator, validators: string[], message: any) { - super(validator, validators); + constructor() { + super("sign"); + } + + public async init(threshold: number, validator: Validator, validators: string[]) { + if (this.sessionInitialized) { + throw new ErrorWithCode(`Session was not initialized correctly.`, ProtocolError.PARAMETER_ERROR); + } this.validator = validator; this.selfId = validator.nodeId; - - this.messageToSign = message; + this.threshold = threshold; + this.validators = validators; + this.messageToSign = this.messageToSign; this.signRequestSerialized = { - messageHex: bytesToHex(keccak_256(message)), + messageHex: bytesToHex(keccak_256(this.messageToSign)), signerIds: validators, }; this.signRequest = SignRequest.fromJSON(this.signRequestSerialized); - this.secretKeyConfig = validator.PartyKeyShare.toJSON(); - this.publicKeyConfig = validator.PartyKeyShare.publicPartyData[this.validator.nodeId].toJSON(); + this.secretKeyConfig = this.validator.PartyKeyShare.toJSON(); + this.publicKeyConfig = this.validator.PartyKeyShare.publicPartyData[this.validator.nodeId].toJSON(); this.partyKeyConfig = PartySecretKeyConfig.fromJSON(this.secretKeyConfig); - console.log(this.secretKeyConfig, this.publicKeyConfig); - } - - public async init(threshold: number, validators: string[]) { - if (this.sessionInitialized) { - throw new ErrorWithCode(`Session was not initialized correctly.`, ProtocolError.PARAMETER_ERROR); - } - this.threshold = threshold; - this.validators = validators; - this.checkPaillierFixture(this.publicKeyConfig.paillier, this.secretKeyConfig.paillier); this.checkCurvePointFixture(this.publicKeyConfig.ecdsa, this.secretKeyConfig.ecdsaHex); this.checkCurvePointFixture(this.publicKeyConfig.elgamal, this.secretKeyConfig.elgamalHex);