Skip to content

Commit dd47914

Browse files
committed
fix(mbe): fix/tested mpcv2 signing with custom fns
Ticket: WP-5232
1 parent c02a2d6 commit dd47914

File tree

2 files changed

+52
-68
lines changed

2 files changed

+52
-68
lines changed

src/api/master/clients/enclavedExpressClient.ts

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -597,30 +597,29 @@ export class EnclavedExpressClient {
597597
}
598598
}
599599

600-
/**
601-
* Create custom MPCv2 Round 1 signing function for enclaved express client
602-
*/
603-
export function signMPCv2Round1(
604-
enclavedExpressClient: EnclavedExpressClient,
605-
source: 'user' | 'backup',
606-
pub: string,
607-
): (params: SignMpcV2Round1Params) => Promise<SignMpcV2Round1Response> {
608-
return async function (params): Promise<SignMpcV2Round1Response> {
609-
if (!enclavedExpressClient['coin']) {
600+
/**
601+
* Sign MPCv2 Round 1
602+
*/
603+
async signMPCv2Round1(
604+
source: 'user' | 'backup',
605+
pub: string,
606+
params: SignMpcV2Round1Params,
607+
): Promise<SignMpcV2Round1Response> {
608+
if (!this['coin']) {
610609
throw new Error('Coin must be specified to sign an MPCv2 Round 1');
611610
}
612611

613612
try {
614-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
615-
coin: enclavedExpressClient['coin'],
613+
let request = this['apiClient']['v1.mpc.sign'].post({
614+
coin: this['coin'],
616615
shareType: 'mpcv2round1',
617616
...params,
618617
source,
619618
pub,
620619
});
621620

622-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
623-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
621+
if (this['tlsMode'] === TlsMode.MTLS) {
622+
request = request.agent(this['createHttpsAgent']());
624623
}
625624
const response = await request.decodeExpecting(200);
626625
return response.body;
@@ -629,33 +628,31 @@ export function signMPCv2Round1(
629628
debugLogger('Failed to sign mpcv2 round 1: %s', err.message);
630629
throw err;
631630
}
632-
};
633-
}
631+
}
634632

635-
/**
636-
* Create custom MPCv2 Round 2 signing function for enclaved express client
637-
*/
638-
export function signMPCv2Round2(
639-
enclavedExpressClient: EnclavedExpressClient,
640-
source: 'user' | 'backup',
641-
pub: string,
642-
): (params: SignMpcV2Round2Params) => Promise<SignMpcV2Round2Response> {
643-
return async function (params): Promise<SignMpcV2Round2Response> {
644-
if (!enclavedExpressClient['coin']) {
633+
/**
634+
* Sign MPCv2 Round 2
635+
*/
636+
async signMPCv2Round2(
637+
source: 'user' | 'backup',
638+
pub: string,
639+
params: SignMpcV2Round2Params,
640+
): Promise<SignMpcV2Round2Response> {
641+
if (!this['coin']) {
645642
throw new Error('Coin must be specified to sign an MPCv2 Round 2');
646643
}
647644

648645
try {
649-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
650-
coin: enclavedExpressClient['coin'],
646+
let request = this['apiClient']['v1.mpc.sign'].post({
647+
coin: this['coin'],
651648
shareType: 'mpcv2round2',
652649
...params,
653650
source,
654651
pub,
655652
});
656653

657-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
658-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
654+
if (this['tlsMode'] === TlsMode.MTLS) {
655+
request = request.agent(this['createHttpsAgent']());
659656
}
660657
const response = await request.decodeExpecting(200);
661658
return response.body;
@@ -664,33 +661,31 @@ export function signMPCv2Round2(
664661
debugLogger('Failed to sign mpcv2 round 2: %s', err.message);
665662
throw err;
666663
}
667-
};
668-
}
664+
}
669665

670-
/**
671-
* Create custom MPCv2 Round 3 signing function for enclaved express client
672-
*/
673-
export function signMPCv2Round3(
674-
enclavedExpressClient: EnclavedExpressClient,
675-
source: 'user' | 'backup',
676-
pub: string,
677-
): (params: SignMpcV2Round3Params) => Promise<SignMpcV2Round3Response> {
678-
return async function (params): Promise<SignMpcV2Round3Response> {
679-
if (!enclavedExpressClient['coin']) {
666+
/**
667+
* Sign MPCv2 Round 3
668+
*/
669+
async signMPCv2Round3(
670+
source: 'user' | 'backup',
671+
pub: string,
672+
params: SignMpcV2Round3Params,
673+
): Promise<SignMpcV2Round3Response> {
674+
if (!this['coin']) {
680675
throw new Error('Coin must be specified to sign an MPCv2 Round 3');
681676
}
682677

683678
try {
684-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
685-
coin: enclavedExpressClient['coin'],
679+
let request = this['apiClient']['v1.mpc.sign'].post({
680+
coin: this['coin'],
686681
shareType: 'mpcv2round3',
687682
...params,
688683
source,
689684
pub,
690685
});
691686

692-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
693-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
687+
if (this['tlsMode'] === TlsMode.MTLS) {
688+
request = request.agent(this['createHttpsAgent']());
694689
}
695690
const response = await request.decodeExpecting(200);
696691
return response.body;
@@ -699,7 +694,7 @@ export function signMPCv2Round3(
699694
debugLogger('Failed to sign mpcv2 round 3: %s', err.message);
700695
throw err;
701696
}
702-
};
697+
}
703698
}
704699

705700
/**

src/api/master/handlers/ecdsa.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import {
1111
EnclavedExpressClient,
1212
SignMpcV2Round1Response,
1313
SignMpcV2Round2Response,
14-
signMPCv2Round1,
15-
signMPCv2Round2,
16-
signMPCv2Round3,
1714
} from '../clients/enclavedExpressClient';
1815

1916
export async function handleEcdsaSigning(
@@ -33,13 +30,13 @@ export async function handleEcdsaSigning(
3330
let round2Response: SignMpcV2Round2Response;
3431

3532
// Create custom signing methods that maintain state
36-
const customRound1Signer = async (params: { txRequest: TxRequest }) => {
37-
const response = await signMPCv2Round1(enclavedExpressClient, source, commonKeychain)(params);
33+
const customMPCv2Round1Generator = async (params: { txRequest: TxRequest }) => {
34+
const response = await enclavedExpressClient.signMPCv2Round1(source, commonKeychain, params);
3835
round1Response = response;
3936
return response;
4037
};
4138

42-
const customRound2Signer = async (params: {
39+
const customMPCv2Round2Generator = async (params: {
4340
txRequest: TxRequest;
4441
encryptedUserGpgPrvKey: string;
4542
encryptedRound1Session: string;
@@ -48,11 +45,7 @@ export async function handleEcdsaSigning(
4845
if (!round1Response) {
4946
throw new Error('Round 1 must be completed before Round 2');
5047
}
51-
const response = await signMPCv2Round2(
52-
enclavedExpressClient,
53-
source,
54-
commonKeychain,
55-
)({
48+
const response = await enclavedExpressClient.signMPCv2Round2(source, commonKeychain, {
5649
...params,
5750
encryptedDataKey: round1Response.encryptedDataKey,
5851
encryptedRound1Session: round1Response.encryptedRound1Session,
@@ -63,20 +56,16 @@ export async function handleEcdsaSigning(
6356
return response;
6457
};
6558

66-
const customRound3Signer = async (params: {
59+
const customMPCv2Round3Generator = async (params: {
6760
txRequest: TxRequest;
6861
encryptedUserGpgPrvKey: string;
6962
encryptedRound2Session: string;
7063
bitgoPublicGpgKey: string;
7164
}) => {
7265
if (!round2Response) {
73-
throw new Error('Round 1 must be completed before Round 3');
66+
throw new Error('Round 2 must be completed before Round 3');
7467
}
75-
return await signMPCv2Round3(
76-
enclavedExpressClient,
77-
source,
78-
commonKeychain,
79-
)({
68+
return await enclavedExpressClient.signMPCv2Round3(source, commonKeychain, {
8069
...params,
8170
encryptedDataKey: round1Response.encryptedDataKey,
8271
encryptedRound2Session: round2Response.encryptedRound2Session,
@@ -88,9 +77,9 @@ export async function handleEcdsaSigning(
8877
// Use the existing signEcdsaMPCv2TssUsingExternalSigner method with our custom signers
8978
return await ecdsaMPCv2Utils.signEcdsaMPCv2TssUsingExternalSigner(
9079
{ txRequest, reqId },
91-
customRound1Signer,
92-
customRound2Signer,
93-
customRound3Signer,
80+
customMPCv2Round1Generator,
81+
customMPCv2Round2Generator,
82+
customMPCv2Round3Generator,
9483
RequestType.tx,
9584
);
9685
}

0 commit comments

Comments
 (0)