decryptPGPKey function
Implementation
Future<String> decryptPGPKey({
required String encryptedPGPPrivateKey,
required Wallet wallet,
bool? toUpgrade = false,
dynamic additionalMeta,
Function? progressHook,
}) async {
try {
if (wallet.signer == null && wallet.address == null) {
throw Exception('At least one from account or signer is necessary!');
}
final encryptionType = jsonDecode(encryptedPGPPrivateKey)['version'];
String? privateKey;
// Report Progress
progressHook?.call(PROGRESSHOOK['PUSH-DECRYPT-01']);
switch (encryptionType) {
// case Constants.ENC_TYPE_V1:
// {}
case Constants.ENC_TYPE_V2:
{
final input = jsonDecode(encryptedPGPPrivateKey)['preKey'];
final enableProfileMessage = 'Enable Push Chat Profile \n$input';
List<int> encodedPrivateKey;
try {
final secret = (await getEip712Signature(
wallet,
enableProfileMessage,
true,
))['verificationProof'];
encodedPrivateKey = await decryptV2(
encryptedData: jsonDecode(encryptedPGPPrivateKey),
secret: hexToBytes(secret ?? ''),
);
} catch (err) {
final secret = (await getEip712Signature(
wallet,
enableProfileMessage,
false,
))['verificationProof'];
encodedPrivateKey = await decryptV2(
encryptedData: jsonDecode(encryptedPGPPrivateKey),
secret: hexToBytes(secret ?? ''),
);
}
final dec = utf8.decoder;
privateKey = dec.convert(encodedPrivateKey);
break;
}
case Constants.ENC_TYPE_V3:
{
final input = jsonDecode(encryptedPGPPrivateKey)['preKey'];
final enableProfileMessage = 'Enable Push Profile \n$input';
final signed = await getEip191Signature(
wallet,
enableProfileMessage,
);
final secret = signed['verificationProof'];
final secretBytes = hexToBytesInternal(secret ?? '');
// final secretBytes = utf8.encode(secret); // hexToBytes(secret ?? '');
final encodedPrivateKey = await decryptV2(
encryptedData: EncryptedPrivateKeyModel.fromJson(
jsonDecode(encryptedPGPPrivateKey)),
secret: secretBytes,
);
final dec = utf8.decoder;
privateKey = dec.convert(encodedPrivateKey);
break;
}
case Constants.ENC_TYPE_V4:
{
String? password;
if (additionalMeta?.containsKey('NFTPGP_V1') == true) {
password = additionalMeta['NFTPGP_V1']['password'];
} else {
final encryptedPassword =
jsonDecode(encryptedPGPPrivateKey)['encryptedPassword'];
password = await decryptPGPKey(
encryptedPGPPrivateKey: jsonEncode(encryptedPassword),
wallet: wallet,
);
}
final encodedPrivateKey = await decryptV2(
encryptedData: jsonDecode(encryptedPGPPrivateKey),
secret: hexToBytes(stringToHex(password ?? '')),
);
final dec = utf8.decoder;
privateKey = dec.convert(encodedPrivateKey);
break;
}
default:
throw Exception('Invalid Encryption Type');
}
// TODO: Add key upgradation logic (not urgent)
// Report Progress
progressHook?.call(PROGRESSHOOK['PUSH-DECRYPT-02'] as ProgressHookType);
return privateKey;
} catch (err) {
// Report Progress
// final errorProgressHook =
// PROGRESSHOOK['PUSH-ERROR-00'] as ProgressHookTypeFunction;
// progressHook?.call(errorProgressHook(['decryptPGPKey', err]));
throw Exception('[Push SDK] - API - Error - API decryptPGPKey -: $err');
}
}