Skip to content

Commit 1715fbb

Browse files
feat(mbe): fix comments
1 parent ba4ee84 commit 1715fbb

4 files changed

Lines changed: 70 additions & 69 deletions

File tree

src/api/master/handlerUtils.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { BitGo, RequestTracer } from 'bitgo';
2+
import { EnclavedExpressClient } from './clients/enclavedExpressClient';
3+
4+
/**
5+
* Fetch wallet and signing keychain, with validation for source and pubkey.
6+
* Throws with a clear error if not found or mismatched.
7+
*/
8+
9+
export async function getWalletAndSigningKeychain({
10+
bitgo,
11+
coin,
12+
walletId,
13+
params,
14+
reqId,
15+
KeyIndices,
16+
}: {
17+
bitgo: BitGo;
18+
coin: string;
19+
walletId: string;
20+
params: { source: 'user' | 'backup'; pubkey?: string };
21+
reqId: RequestTracer;
22+
KeyIndices: { USER: number; BACKUP: number; BITGO: number };
23+
}) {
24+
const baseCoin = bitgo.coin(coin);
25+
26+
const wallet = await baseCoin.wallets().get({ id: walletId, reqId });
27+
28+
if (!wallet) {
29+
throw new Error(`Wallet ${walletId} not found`);
30+
}
31+
32+
const keyIdIndex = params.source === 'user' ? KeyIndices.USER : KeyIndices.BACKUP;
33+
const signingKeychain = await baseCoin.keychains().get({
34+
id: wallet.keyIds()[keyIdIndex],
35+
});
36+
37+
if (!signingKeychain || !signingKeychain.pub) {
38+
throw new Error(`Signing keychain for ${params.source} not found`);
39+
}
40+
41+
if (params.pubkey && params.pubkey !== signingKeychain.pub) {
42+
throw new Error(`Pub provided does not match the keychain on wallet for ${params.source}`);
43+
}
44+
45+
return { baseCoin, wallet, signingKeychain };
46+
}
47+
/**
48+
* Create a custom signing function that delegates to enclavedExpressClient.signMultisig.
49+
*/
50+
51+
export function makeCustomSigningFunction({
52+
enclavedExpressClient,
53+
source,
54+
pub,
55+
}: {
56+
enclavedExpressClient: EnclavedExpressClient;
57+
source: 'user' | 'backup';
58+
pub: string;
59+
}) {
60+
return async function customSigningFunction(signParams: any) {
61+
return enclavedExpressClient.signMultisig({
62+
txPrebuild: signParams.txPrebuild,
63+
source,
64+
pub,
65+
});
66+
};
67+
}

src/api/master/handlers/handleAccelerate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RequestTracer, KeyIndices } from '@bitgo/sdk-core';
22
import logger from '../../../logger';
33
import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec';
4-
import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../../../shared/coinUtils';
4+
import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../handlerUtils';
55

66
export async function handleAccelerate(
77
req: MasterApiSpecRouteRequest<'v1.wallet.accelerate', 'post'>,

src/api/master/handlers/handleConsolidateUnspents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RequestTracer, KeyIndices } from '@bitgo/sdk-core';
22
import logger from '../../../logger';
33
import { MasterApiSpecRouteRequest } from '../routers/masterApiSpec';
4-
import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../../../shared/coinUtils';
4+
import { getWalletAndSigningKeychain, makeCustomSigningFunction } from '../handlerUtils';
55

66
export async function handleConsolidateUnspents(
77
req: MasterApiSpecRouteRequest<'v1.wallet.consolidateunspents', 'post'>,

src/shared/coinUtils.ts

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { FormattedOfflineVaultTxInfo, BackupKeyRecoveryTransansaction } from '@bitgo/abstract-utxo';
22
import { AbstractEthLikeNewCoins } from '@bitgo/abstract-eth';
33
import { CoinFamily } from '@bitgo/statics';
4-
import { BaseCoin, BitGo } from 'bitgo';
4+
import { BaseCoin } from 'bitgo';
55
import { AbstractUtxoCoin, Eos, Stx, Xtz } from 'bitgo/dist/types/src/v2/coins';
6-
import { RequestTracer } from '@bitgo/sdk-core';
7-
import { EnclavedExpressClient } from '../api/master/clients/enclavedExpressClient';
86

97
export function isEthLikeCoin(coin: BaseCoin): coin is AbstractEthLikeNewCoins {
108
const isEthPure = isFamily(coin, CoinFamily.ETH);
@@ -63,67 +61,3 @@ export function isFormattedOfflineVaultTxInfo(
6361
): obj is FormattedOfflineVaultTxInfo {
6462
return obj && 'txInfo' in obj && 'txHex' in obj && 'feeInfo' in obj;
6563
}
66-
67-
/**
68-
* Fetch wallet and signing keychain, with validation for source and pubkey.
69-
* Throws with a clear error if not found or mismatched.
70-
*/
71-
export async function getWalletAndSigningKeychain({
72-
bitgo,
73-
coin,
74-
walletId,
75-
params,
76-
reqId,
77-
KeyIndices,
78-
}: {
79-
bitgo: BitGo;
80-
coin: string;
81-
walletId: string;
82-
params: { source: 'user' | 'backup'; pubkey?: string };
83-
reqId: RequestTracer;
84-
KeyIndices: { USER: number; BACKUP: number; BITGO: number };
85-
}) {
86-
const baseCoin = bitgo.coin(coin);
87-
88-
const wallet = await baseCoin.wallets().get({ id: walletId, reqId });
89-
90-
if (!wallet) {
91-
throw new Error(`Wallet ${walletId} not found`);
92-
}
93-
94-
const keyIdIndex = params.source === 'user' ? KeyIndices.USER : KeyIndices.BACKUP;
95-
const signingKeychain = await baseCoin.keychains().get({
96-
id: wallet.keyIds()[keyIdIndex],
97-
});
98-
99-
if (!signingKeychain || !signingKeychain.pub) {
100-
throw new Error(`Signing keychain for ${params.source} not found`);
101-
}
102-
103-
if (params.pubkey && params.pubkey !== signingKeychain.pub) {
104-
throw new Error(`Pub provided does not match the keychain on wallet for ${params.source}`);
105-
}
106-
107-
return { baseCoin, wallet, signingKeychain };
108-
}
109-
110-
/**
111-
* Create a custom signing function that delegates to enclavedExpressClient.signMultisig.
112-
*/
113-
export function makeCustomSigningFunction({
114-
enclavedExpressClient,
115-
source,
116-
pub,
117-
}: {
118-
enclavedExpressClient: EnclavedExpressClient;
119-
source: 'user' | 'backup';
120-
pub: string;
121-
}) {
122-
return async function customSigningFunction(signParams: any) {
123-
return enclavedExpressClient.signMultisig({
124-
txPrebuild: signParams.txPrebuild,
125-
source,
126-
pub,
127-
});
128-
};
129-
}

0 commit comments

Comments
 (0)