-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
122 lines (113 loc) · 3.06 KB
/
Copy pathutils.ts
File metadata and controls
122 lines (113 loc) · 3.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { createMessage, decrypt, encrypt, readKey, readMessage, readPrivateKey } from 'openpgp';
import { KmsClient } from '../../kms/kmsClient';
import { GenerateDataKeyResponse } from '../../kms/types/dataKey';
import { AdvancedWalletManagerConfig } from '../../shared/types';
export async function retrieveKmsPrvKey({
pub,
source,
cfg,
}: {
pub: string;
source: string;
cfg: AdvancedWalletManagerConfig;
}): Promise<string> {
const kms = new KmsClient(cfg);
// Retrieve the private key from KMS
let prv: string;
try {
const res = await kms.getKey({ pub, source });
prv = res.prv;
return prv;
} catch (error: any) {
throw {
status: error.status || 500,
message: error.message || 'Failed to retrieve key from KMS',
};
}
}
/**
* Helper function to encrypt text using OpenPGP
*
* @param text {string} The message to encrypt
* @param key {string} The encryption key
*
* @return {string} encrypted string
*/
export async function gpgEncrypt(text: string, key: string): Promise<string> {
return (
await encrypt({
message: await createMessage({ text }),
encryptionKeys: await readKey({ armoredKey: key }),
format: 'armored',
config: {
rejectCurves: new Set(),
showVersion: false,
showComment: false,
},
})
).toString();
}
/**
* Helper function to a decrypt text using OpenPGP
*
* @param text {string} The message to decrypt
* @param key {string} The decryption key
*
* @return {string} The decrypted message
*/
export async function gpgDecrypt(text: string, key: string): Promise<string> {
const message = await readMessage({
armoredMessage: text,
});
const gpgPrivateKey = await readPrivateKey({ armoredKey: key });
const decryptedPrivateShare = (
await decrypt({
message,
decryptionKeys: [gpgPrivateKey],
format: 'utf8',
})
).data;
return decryptedPrivateShare.toString();
}
export async function generateDataKey({
keyType,
cfg,
}: {
keyType: 'AES-256' | 'RSA-2048' | 'ECDSA-P256';
cfg: AdvancedWalletManagerConfig;
}): Promise<GenerateDataKeyResponse> {
try {
const kms = new KmsClient(cfg);
return await kms.generateDataKey({ keyType });
} catch (error: any) {
throw {
status: error.status || 500,
message: error.message || 'Failed to generate data key from KMS',
};
}
}
export async function decryptDataKey({
encryptedDataKey,
cfg,
}: {
encryptedDataKey: string;
cfg: AdvancedWalletManagerConfig;
}): Promise<string> {
try {
const kms = new KmsClient(cfg);
const decryptedDataKey = await kms.decryptDataKey({ encryptedKey: encryptedDataKey });
return decryptedDataKey.plaintextKey;
} catch (error: any) {
throw {
status: error.status || 500,
message: error.message || 'Failed to decrypt data key from KMS',
};
}
}
export function checkRecoveryMode(config: AdvancedWalletManagerConfig) {
if (!config.recoveryMode) {
throw new Error(
'Recovery operations are not enabled. The server must be in recovery mode to perform this action.',
);
}
}