Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static async import_entity(private_key) {
// Validate private_key format, this throws if private_key format is not valid
const public_key = EthCrypto.publicKeyByPrivateKey(private_key);
const address = EthCrypto.publicKey.toAddress(public_key);
// Try to get existing wallet by address.
// Errors indicate Wallet does not exist and needs to be created
try {
return await Wallet.getByAddress(address);
} catch (_err) {
const wallet = new Wallet();
const encryptedPrivateKey = await EncryptorContainer.defaultEncryptor.encrypt(private_key);
Object.assign(wallet, {
public_key: public_key,
private_key: encryptedPrivateKey,
address: address,
unlocked_private_key: private_key,
static async getInstance(): Promise {
if (!this._instance) {
let instance = new AwsPrivateKeyDBFieldEncryption();
instance.masterPrivateKey = await instance.getMasterPrivateKey();
instance.masterPublicKey = EthCrypto.publicKeyByPrivateKey(instance.masterPrivateKey);
this._instance = instance;
}
return this._instance;
}
static async getInstance(): Promise {
if (!this._instance) {
let instance = new PrivateKeyDBFieldEncryption();
instance.masterPrivateKey = await instance.getMasterPrivateKey();
instance.masterPublicKey = EthCrypto.publicKeyByPrivateKey(instance.masterPrivateKey);
this._instance = instance;
}
return this._instance;
}
function getAddressFromPrivateKey(privateKey: string): string {
try {
const publicKey = EthCrypto.publicKeyByPrivateKey(privateKey);
return EthCrypto.publicKey.toAddress(publicKey);
} catch (e) {
if (e.message === 'private key length is invalid') {
throw new Error('The private key must be a string representing 32 bytes');
}
throw e;
}
}