-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlerUtils.ts
More file actions
73 lines (63 loc) · 2 KB
/
Copy pathhandlerUtils.ts
File metadata and controls
73 lines (63 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { BitGo, CustomSigningFunction, RequestTracer } from 'bitgo';
import { EnclavedExpressClient } from './clients/enclavedExpressClient';
/**
* Fetch wallet and signing keychain, with validation for source and pubkey.
* Throws with a clear error if not found or mismatched.
*/
export async function getWalletAndSigningKeychain({
bitgo,
coin,
walletId,
params,
reqId,
KeyIndices,
}: {
bitgo: BitGo;
coin: string;
walletId: string;
params: { source: 'user' | 'backup'; pubkey?: string; commonKeychain?: string };
reqId: RequestTracer;
KeyIndices: { USER: number; BACKUP: number; BITGO: number };
}) {
const baseCoin = bitgo.coin(coin);
const wallet = await baseCoin.wallets().get({ id: walletId, reqId });
if (!wallet) {
throw new Error(`Wallet ${walletId} not found`);
}
const keyIdIndex = params.source === 'user' ? KeyIndices.USER : KeyIndices.BACKUP;
const signingKeychain = await baseCoin.keychains().get({
id: wallet.keyIds()[keyIdIndex],
});
if (!signingKeychain) {
throw new Error(`Signing keychain for ${params.source} not found`);
}
if (params.pubkey && params.pubkey !== signingKeychain.pub) {
throw new Error(`Pub provided does not match the keychain on wallet for ${params.source}`);
}
if (params.commonKeychain && signingKeychain.commonKeychain !== params.commonKeychain) {
throw new Error(
`Common keychain provided does not match the keychain on wallet for ${params.source}`,
);
}
return { baseCoin, wallet, signingKeychain };
}
/**
* Create a custom signing function that delegates to enclavedExpressClient.signMultisig.
*/
export function makeCustomSigningFunction({
enclavedExpressClient,
source,
pub,
}: {
enclavedExpressClient: EnclavedExpressClient;
source: 'user' | 'backup';
pub: string;
}): CustomSigningFunction {
return async function customSigningFunction(signParams: any) {
return enclavedExpressClient.signMultisig({
txPrebuild: signParams.txPrebuild,
source,
pub,
});
};
}