@defi-wonderland/keep3r-v2
Version:
The Keep3r Network is a decentralized network for projects that need external devops, and for external teams to find keeper jobs
51 lines (42 loc) • 1.44 kB
text/typescript
import { KMS } from 'aws-sdk';
import { ClientConfiguration } from 'aws-sdk/clients/kms';
import 'dotenv/config';
import _ from 'lodash';
const DEFAULT_CLIENT_CONFIGURATION: ClientConfiguration = {
apiVersion: '2014-11-01',
region: 'us-east-1',
};
const kms = new KMS(DEFAULT_CLIENT_CONFIGURATION);
const encrypt = async (stringToEncrypt: string): Promise<string> => {
const data = await kms
.encrypt({
KeyId: process.env.KMS_KEY_ID as string,
Plaintext: Buffer.from(stringToEncrypt),
})
.promise();
return data.CiphertextBlob!.toString('base64');
};
const encryptSeveral = async (plainStrings: string[]): Promise<string[]> => {
return await Promise.all(_.map(plainStrings, (plainString) => encrypt(plainString)));
};
const decrypt = async (encryptedString: string): Promise<string> => {
const decryptedInfo = await kms
.decrypt({
CiphertextBlob: Buffer.from(encryptedString, 'base64'),
})
.promise();
return decryptedInfo.Plaintext!.toString();
};
const decryptSeveral = async (encryptedStrings: string[]): Promise<string[]> => {
return await Promise.all(_.map(encryptedStrings, (encryptedString) => decrypt(encryptedString)));
};
const getDecryptedPrivateKey = async (): Promise<string> => {
return await decrypt(process.env.ENCRYPTED_PRIVATE_KEY as string);
};
export default {
getDecryptedPrivateKey,
encrypt,
encryptSeveral,
decrypt,
decryptSeveral,
};