Skip to content

Commit ba2f0c0

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

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
@@ -599,30 +599,29 @@ export class EnclavedExpressClient {
599599
}
600600
}
601601

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

615614
try {
616-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
617-
coin: enclavedExpressClient['coin'],
615+
let request = this['apiClient']['v1.mpc.sign'].post({
616+
coin: this['coin'],
618617
shareType: 'mpcv2round1',
619618
...params,
620619
source,
621620
pub,
622621
});
623622

624-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
625-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
623+
if (this['tlsMode'] === TlsMode.MTLS) {
624+
request = request.agent(this['createHttpsAgent']());
626625
}
627626
const response = await request.decodeExpecting(200);
628627
return response.body;
@@ -631,33 +630,31 @@ export function signMPCv2Round1(
631630
debugLogger('Failed to sign mpcv2 round 1: %s', err.message);
632631
throw err;
633632
}
634-
};
635-
}
633+
}
636634

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

650647
try {
651-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
652-
coin: enclavedExpressClient['coin'],
648+
let request = this['apiClient']['v1.mpc.sign'].post({
649+
coin: this['coin'],
653650
shareType: 'mpcv2round2',
654651
...params,
655652
source,
656653
pub,
657654
});
658655

659-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
660-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
656+
if (this['tlsMode'] === TlsMode.MTLS) {
657+
request = request.agent(this['createHttpsAgent']());
661658
}
662659
const response = await request.decodeExpecting(200);
663660
return response.body;
@@ -666,33 +663,31 @@ export function signMPCv2Round2(
666663
debugLogger('Failed to sign mpcv2 round 2: %s', err.message);
667664
throw err;
668665
}
669-
};
670-
}
666+
}
671667

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

685680
try {
686-
let request = enclavedExpressClient['apiClient']['v1.mpc.sign'].post({
687-
coin: enclavedExpressClient['coin'],
681+
let request = this['apiClient']['v1.mpc.sign'].post({
682+
coin: this['coin'],
688683
shareType: 'mpcv2round3',
689684
...params,
690685
source,
691686
pub,
692687
});
693688

694-
if (enclavedExpressClient['tlsMode'] === TlsMode.MTLS) {
695-
request = request.agent(enclavedExpressClient['createHttpsAgent']());
689+
if (this['tlsMode'] === TlsMode.MTLS) {
690+
request = request.agent(this['createHttpsAgent']());
696691
}
697692
const response = await request.decodeExpecting(200);
698693
return response.body;
@@ -701,7 +696,7 @@ export function signMPCv2Round3(
701696
debugLogger('Failed to sign mpcv2 round 3: %s', err.message);
702697
throw err;
703698
}
704-
};
699+
}
705700
}
706701

707702
/**

src/api/master/handlers/ecdsa.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import {
1313
EnclavedExpressClient,
1414
SignMpcV2Round1Response,
1515
SignMpcV2Round2Response,
16-
signMPCv2Round1,
17-
signMPCv2Round2,
18-
signMPCv2Round3,
1916
} from '../clients/enclavedExpressClient';
2017

2118
export async function handleEcdsaSigning(
@@ -35,13 +32,13 @@ export async function handleEcdsaSigning(
3532
let round2Response: SignMpcV2Round2Response;
3633

3734
// Create custom signing methods that maintain state
38-
const customRound1Signer = async (params: { txRequest: TxRequest }) => {
39-
const response = await signMPCv2Round1(enclavedExpressClient, source, commonKeychain)(params);
35+
const customMPCv2Round1Generator = async (params: { txRequest: TxRequest }) => {
36+
const response = await enclavedExpressClient.signMPCv2Round1(source, commonKeychain, params);
4037
round1Response = response;
4138
return response;
4239
};
4340

44-
const customRound2Signer = async (params: {
41+
const customMPCv2Round2Generator = async (params: {
4542
txRequest: TxRequest;
4643
encryptedUserGpgPrvKey: string;
4744
encryptedRound1Session: string;
@@ -50,11 +47,7 @@ export async function handleEcdsaSigning(
5047
if (!round1Response) {
5148
throw new Error('Round 1 must be completed before Round 2');
5249
}
53-
const response = await signMPCv2Round2(
54-
enclavedExpressClient,
55-
source,
56-
commonKeychain,
57-
)({
50+
const response = await enclavedExpressClient.signMPCv2Round2(source, commonKeychain, {
5851
...params,
5952
encryptedDataKey: round1Response.encryptedDataKey,
6053
encryptedRound1Session: round1Response.encryptedRound1Session,
@@ -65,20 +58,16 @@ export async function handleEcdsaSigning(
6558
return response;
6659
};
6760

68-
const customRound3Signer = async (params: {
61+
const customMPCv2Round3Generator = async (params: {
6962
txRequest: TxRequest;
7063
encryptedUserGpgPrvKey: string;
7164
encryptedRound2Session: string;
7265
bitgoPublicGpgKey: string;
7366
}) => {
7467
if (!round2Response) {
75-
throw new Error('Round 1 must be completed before Round 3');
68+
throw new Error('Round 2 must be completed before Round 3');
7669
}
77-
return await signMPCv2Round3(
78-
enclavedExpressClient,
79-
source,
80-
commonKeychain,
81-
)({
70+
return await enclavedExpressClient.signMPCv2Round3(source, commonKeychain, {
8271
...params,
8372
encryptedDataKey: round1Response.encryptedDataKey,
8473
encryptedRound2Session: round2Response.encryptedRound2Session,
@@ -90,9 +79,9 @@ export async function handleEcdsaSigning(
9079
// Use the existing signEcdsaMPCv2TssUsingExternalSigner method with our custom signers
9180
return await ecdsaMPCv2Utils.signEcdsaMPCv2TssUsingExternalSigner(
9281
{ txRequest, reqId },
93-
customRound1Signer,
94-
customRound2Signer,
95-
customRound3Signer,
82+
customMPCv2Round1Generator,
83+
customMPCv2Round2Generator,
84+
customMPCv2Round3Generator,
9685
RequestType.tx,
9786
);
9887
}

0 commit comments

Comments
 (0)