encrypt method

  1. @override
Future<String> encrypt(
  1. String message,
  2. String symKey, {
  3. int? type,
  4. String? iv,
  5. String? senderPublicKey,
})
override

Implementation

@override
Future<String> encrypt(
  String message,
  String symKey, {
  int? type,
  String? iv,
  String? senderPublicKey,
}) async {
  final int decodedType = type ?? EncodeOptions.TYPE_0;
  // print(message);
  // print(symKey);

  // Check for type 1 envelope, throw an error if data is invalid
  if (decodedType == EncodeOptions.TYPE_1 && senderPublicKey == null) {
    throw const ReownCoreError(
      code: -1,
      message: 'Missing sender public key for type 1 envelope',
    );
  }

  // final String senderPublicKey = senderPublicKey !=
  final Uint8List usedIV =
      (iv != null ? hex.decode(iv) : randomBytes(IV_LENGTH)) as Uint8List;

  final chacha = dc.Chacha20.poly1305Aead();
  dc.SecretBox b = await chacha.encrypt(
    utf8.encode(message),
    secretKey: dc.SecretKey(
      hex.decode(symKey),
    ),
    nonce: usedIV,
  );

  return serialize(
    decodedType,
    b.concatenation(),
    usedIV,
    senderPublicKey: senderPublicKey != null
        ? hex.decode(senderPublicKey) as Uint8List
        : null,
  );
}