Skip to content

Commit a7ef80f

Browse files
fix: store private mpcv2 prv with local encryption
Ticket: WP-5150
1 parent 48935d9 commit a7ef80f

6 files changed

Lines changed: 34 additions & 10 deletions

File tree

src/api/enclaved/handlers/mpcV2Finalize.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from '../../../enclavedBitgoExpress/routers/enclavedApiSpec';
77
import { KmsClient } from '../../../kms/kmsClient';
88
import assert from 'assert';
9-
import { MPCv2PartiesEnum } from '@bitgo/sdk-core/dist/src/bitgo/utils/tss/ecdsa';
109

1110
export async function mpcV2Finalize(
1211
req: EnclavedApiSpecRouteRequest<'v1.mpcv2.finalize', 'post'>,
@@ -35,12 +34,7 @@ export async function mpcV2Finalize(
3534
throw new Error('Session data is missing for finalization');
3635
}
3736
sessionData.dkgSessionBytes = new Uint8Array(Object.values(sessionData.dkgSessionBytes));
38-
const session = await DklsDkg.Dkg.restoreSession(
39-
3,
40-
2,
41-
source === 'user' ? MPCv2PartiesEnum.USER : MPCv2PartiesEnum.BACKUP,
42-
sessionData,
43-
);
37+
const session = await DklsDkg.Dkg.restoreSession(3, 2, source === 'user' ? 0 : 1, sessionData);
4438

4539
// processing incoming messages
4640
const incomingMessages = await DklsComms.decryptAndVerifyIncomingMessages(
@@ -65,6 +59,17 @@ export async function mpcV2Finalize(
6559
'Source and Bitgo Common keychains do not match',
6660
);
6761

62+
await kms.postKey({
63+
coin: req.decoded.coin,
64+
source: req.decoded.source,
65+
pub: commonKeychain,
66+
prv: privateMaterial.toString('base64'),
67+
type: 'tss',
68+
options: {
69+
useLocalEncipherment: true,
70+
},
71+
});
72+
6873
return {
6974
source,
7075
commonKeychain,

src/api/enclaved/handlers/signMpcTransaction.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ export async function signMpcTransaction(req: EnclavedApiSpecRouteRequest<'v1.mp
8989

9090
const bitgo = req.bitgo;
9191
const coinInstance = bitgo.coin(coin);
92+
const options =
93+
coinInstance.getMPCAlgorithm() === 'ecdsa'
94+
? {
95+
useLocalEncipherment: true,
96+
}
97+
: undefined;
9298

9399
// Get private key from KMS
94-
const prv = await retrieveKmsPrvKey({ pub, source, cfg: req.config });
100+
const prv = await retrieveKmsPrvKey({ pub, source, cfg: req.config, options });
95101

96102
if (!prv) {
97103
const errorMsg = `Error while MPC signing, missing prv key for pub=${pub}, source=${source}`;

src/api/enclaved/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ export async function retrieveKmsPrvKey({
88
pub,
99
source,
1010
cfg,
11+
options,
1112
}: {
1213
pub: string;
1314
source: string;
1415
cfg: EnclavedConfig;
16+
options?: {
17+
useLocalEncipherment?: boolean;
18+
};
1519
}): Promise<string> {
1620
const kms = new KmsClient(cfg);
1721
// Retrieve the private key from KMS
1822
let prv: string;
1923
try {
20-
const res = await kms.getKey({ pub, source });
24+
const res = await kms.getKey({ pub, source, options });
2125
prv = res.prv;
2226
return prv;
2327
} catch (error: any) {

src/kms/kmsClient.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export class KmsClient {
6565
try {
6666
kmsResponse = await superagent
6767
.get(`${this.url}/key/${params.pub}`)
68-
.query({ source: params.source });
68+
.query({
69+
source: params.source,
70+
useLocalEncipherment: params.options?.useLocalEncipherment ?? false,
71+
});
6972
} catch (error: any) {
7073
console.log('Error getting key from KMS', error);
7174
throw error;

src/kms/types/getKey.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as z from 'zod';
33
export interface GetKeyParams {
44
pub: string;
55
source: string;
6+
options?: {
7+
useLocalEncipherment?: boolean;
8+
};
69
}
710

811
export interface GetKeyResponse {

src/kms/types/postKey.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface PostKeyParams {
77
source: string;
88
type: 'independent' | 'tss';
99
seed?: string; // Optional seed for key generation
10+
options?: {
11+
useLocalEncipherment?: boolean;
12+
};
1013
}
1114

1215
export interface PostKeyResponse {

0 commit comments

Comments
 (0)