encode method

  1. @override
Future<String?> encode(
  1. String topic,
  2. Map<String, dynamic> payload, {
  3. EncodeOptions? options,
})
override

Implementation

@override
Future<String?> encode(
  String topic,
  Map<String, dynamic> payload, {
  EncodeOptions? options,
}) async {
  _checkInitialized();

  EncodingValidation params;
  if (options == null) {
    params = utils.validateEncoding();
  } else {
    params = utils.validateEncoding(
      type: options.type,
      senderPublicKey: options.senderPublicKey,
      receiverPublicKey: options.receiverPublicKey,
    );
  }

  final String message = jsonEncode(payload);

  if (utils.isTypeTwoEnvelope(params)) {
    return utils.encodeTypeTwoEnvelope(message: message);
  }

  if (utils.isTypeOneEnvelope(params)) {
    final String selfPublicKey = params.senderPublicKey!;
    final String peerPublicKey = params.receiverPublicKey!;
    topic = await generateSharedKey(selfPublicKey, peerPublicKey);
  }

  final String? symKey = _getSymKey(topic);
  if (symKey == null) {
    return null;
  }

  final String result = await utils.encrypt(
    message,
    symKey,
    type: params.type,
    senderPublicKey: params.senderPublicKey,
  );

  return result;
}