collectEventData method

Future<Map<String, dynamic>> collectEventData(
  1. String identityDid
)

Collects all data from contract log for identityDid.

It returns a map containing the keys owners, attributes and delegates. Behind owners-key is a List

Implementation

Future<Map<String, dynamic>> collectEventData(String identityDid) async {
  if (!_matchesExpectedDid(identityDid)) {
    throw Exception(
        'Information about $identityDid do not belong to this network');
  }
  var didOwnerChangedEvent = _erc1056contract.event('DIDOwnerChanged');
  var didDelegateChangedEvent = _erc1056contract.event('DIDDelegateChanged');
  var didAttributeChangedEvent =
      _erc1056contract.event('DIDAttributeChanged');
  List<String> owners = [];
  var delegates = <String, List<String>>{};
  var attributes = <String, List<String>>{};
  List<String> revokedAttributes = [];
  int secNow = (DateTime.now().millisecondsSinceEpoch ~/ 1000);

  var lastChange = await changed(identityDid);

  while (lastChange != BigInt.zero) {
    var logs = await web3Client.getLogs(FilterOptions(
        fromBlock: BlockNum.exact(lastChange!.toInt()),
        toBlock: BlockNum.exact(lastChange.toInt()),
        topics: [
          [
            bytesToHex(didOwnerChangedEvent.signature, include0x: true),
            bytesToHex(didDelegateChangedEvent.signature, include0x: true),
            bytesToHex(didAttributeChangedEvent.signature, include0x: true)
          ],
          ['0x${_didToAddress(identityDid).hexNo0x.padLeft(64, '0')}']
        ]));
    List<BigInt?> listOfPreviousChanges = [];

    await Future.forEach(logs, (dynamic event) async {
      if (event.topics.first ==
          bytesToHex(didOwnerChangedEvent.signature, include0x: true)) {
        var decodedEvent =
            didOwnerChangedEvent.decodeResults(event.topics, event.data);
        var owner = decodedEvent[1] as EthereumAddress;
        var previousChange = decodedEvent[2] as BigInt?;

        listOfPreviousChanges.add(previousChange);
        owners.add(_addressToDid(owner));
      } else if (event.topics.first ==
          bytesToHex(didAttributeChangedEvent.signature, include0x: true)) {
        var decodedEvent =
            didAttributeChangedEvent.decodeResults(event.topics, event.data);
        var name = decodedEvent[1] as Uint8List;
        var value = decodedEvent[2] as Uint8List;
        var validTo = decodedEvent[3] as BigInt;
        var previousChange = decodedEvent[4] as BigInt?;
        var nameStr = _bytes32ToString(name);
        var valueStr = _utf8.decode(value);

        listOfPreviousChanges.add(previousChange);

        if (validTo < BigInt.from(secNow)) {
          revokedAttributes.add('$nameStr-$valueStr');
        } else {
          if (!revokedAttributes.contains('$nameStr-$valueStr')) {
            if (attributes.containsKey(nameStr) &&
                (!attributes[nameStr]!.contains(valueStr))) {
              attributes[nameStr]!.add(valueStr);
            } else {
              List<String> tmp = [];
              tmp.add(valueStr);
              attributes[nameStr] = tmp;
            }
          }
        }
      } else if (event.topics.first ==
          bytesToHex(didDelegateChangedEvent.signature, include0x: true)) {
        var decodedEvent =
            didDelegateChangedEvent.decodeResults(event.topics, event.data);
        var delegateType = decodedEvent[1] as Uint8List;
        var delegate = decodedEvent[2] as EthereumAddress;
        var previousChange = decodedEvent[4] as BigInt?;
        var delegateTypeString = _bytes32ToString(delegateType);

        listOfPreviousChanges.add(previousChange);

        var validDelegate = await this.validDelegate(
            identityDid, delegateTypeString, _addressToDid(delegate));
        if (validDelegate!) {
          if (delegates.containsKey(delegateTypeString) &&
              (!delegates[delegateTypeString]!
                  .contains(_addressToDid(delegate)))) {
            delegates[delegateTypeString]!.add(_addressToDid(delegate));
          } else {
            List<String> tmpList = [];
            tmpList.add(_addressToDid(delegate));
            delegates[delegateTypeString] = tmpList;
          }
        }
      } else {
        throw Exception('Unknown Event-Signature ${event.topics.first}');
      }
    });

    listOfPreviousChanges.sort();
    BigInt? lastChangeNew = listOfPreviousChanges.last;
    for (int i = listOfPreviousChanges.length - 1; i >= 0; i--) {
      if (lastChangeNew != lastChange &&
          (!(listOfPreviousChanges[i]! < lastChangeNew!))) {
        lastChangeNew = listOfPreviousChanges[i];
      }
    }

    if (lastChangeNew == lastChange) {
      break;
    } else {
      lastChange = lastChangeNew;
    }
  }

  var eventData = <String, dynamic>{};
  eventData['owners'] = owners;
  eventData['attributes'] = attributes;
  eventData['delegates'] = delegates;
  return eventData;
}