decodeBundle method

  1. @override
EncryptedBundle decodeBundle(
  1. Mapping encodedKeys,
  2. ID receiver,
  3. Iterable<String>? terminals
)
override

Decode key data from 'message.keys'

encodedKeys is the encoded key data with targets (ID + terminals). receiver is the user ID. terminals is the visa terminals (null to decode all terminals).

Returns encrypted key data with targets (ID terminals).

Implementation

@override
EncryptedBundle decodeBundle(Mapping encodedKeys, ID receiver, Iterable<String>? terminals) {
  if (terminals == null) {
    // decode full bundle
    return _decodeBundle(encodedKeys, receiver);
  }
  final bundle = UserEncryptedBundle();
  //
  //  0. ID string without terminal
  //
  assert(receiver.terminal == null, 'ID should not contain terminal here: $receiver');
  final String identifier = receiver.withoutTerminal().toString();
  String target;
  for (final item in terminals) {
    //
    //  1. get encoded data with target (ID + terminal)
    //
    Object? base64;
    if (item.isEmpty || item == '/') {
      // Naked ID
      base64 = encodedKeys[identifier];
      target = '/';
    } else if (item.startsWith('/')) {
      assert(false, 'terminal error: $item');
      base64 = encodedKeys[identifier + item];
      target = item.substring(1);
    } else {
      // Dressed ID
      base64 = encodedKeys['$identifier/$item'];
      target = item;
    }
    if (base64 == null) {
      // key data not found
      continue;
    }
    //
    //  2. decode data
    //
    final ted = TransportableData.parse(base64);
    final data = ted?.bytes;
    if (data == null || data.isEmpty) {
      assert(false, 'key data error: $item -> $base64');
      continue;
    }
    //
    //  3. put data for target (ID terminal)
    //
    assert(bundle[target] == null, 'duplicated terminal: $item, $encodedKeys');
    bundle[target] = data;
  }
  // OK
  return bundle;
}