isConformingNamespaces function

ErrorObject? isConformingNamespaces(
  1. ProposalRequiredNamespaces requiredNamespaces,
  2. SessionNamespaces namespaces,
  3. String context
)

Implementation

ErrorObject? isConformingNamespaces(
  ProposalRequiredNamespaces requiredNamespaces,
  SessionNamespaces namespaces,
  String context,
) {
  ErrorObject? error;
  final requiredNamespaceKeys = requiredNamespaces.keys.toList();
  final namespaceKeys = namespaces.keys.toList();

  if (!hasOverlap(requiredNamespaceKeys, namespaceKeys)) {
    error = getInternalError(
      InternalErrorKey.NON_CONFORMING_NAMESPACES,
      context: '$context namespaces keys don\'t satisfy requiredNamespaces',
    );
  } else {
    requiredNamespaceKeys.forEach((key) {
      if (error != null) return;

      final requiredNamespaceChains = requiredNamespaces[key]!.chains;
      final namespaceChains = getAccountsChains(namespaces[key]!.accounts);

      if (!hasOverlap(requiredNamespaceChains, namespaceChains)) {
        error = getInternalError(
          InternalErrorKey.NON_CONFORMING_NAMESPACES,
          context:
              '$context namespaces accounts don\'t satisfy requiredNamespaces chains for $key',
        );
      } else if (!hasOverlap(
          requiredNamespaces[key]!.methods, namespaces[key]!.methods)) {
        error = getInternalError(
          InternalErrorKey.NON_CONFORMING_NAMESPACES,
          context:
              '$context namespaces methods don\'t satisfy requiredNamespaces methods for $key',
        );
      } else if (!hasOverlap(
          requiredNamespaces[key]!.events, namespaces[key]!.events)) {
        error = getInternalError(
          InternalErrorKey.NON_CONFORMING_NAMESPACES,
          context:
              '$context namespaces events don\'t satisfy requiredNamespaces events for $key',
        );
      } else if (requiredNamespaces[key]!.extension != null &&
          namespaces[key]!.extension == null) {
        error = getInternalError(
          InternalErrorKey.NON_CONFORMING_NAMESPACES,
          context:
              '$context namespaces extension doesn\'t satisfy requiredNamespaces extension for $key',
        );
      } else if (requiredNamespaces[key]!.extension != null &&
          namespaces[key]!.extension != null) {
        requiredNamespaces[key]!.extension!.forEach((namespace) {
          final methods = namespace.methods;
          final events = namespace.events;
          final chains = namespace.chains;

          if (error != null) return;
          final isOverlap = namespaces[key]!.extension!.any((namespace) {
            final accChains = getAccountsChains(namespace.accounts);
            return (hasOverlap(chains, accChains) &&
                hasOverlap(events, namespace.events) &&
                hasOverlap(methods, namespace.methods));
          });

          if (!isOverlap) {
            error = getInternalError(
              InternalErrorKey.NON_CONFORMING_NAMESPACES,
              context:
                  '$context namespaces extension doesn\'t satisfy requiredNamespaces extension for $key',
            );
          }
        });
      }
    });
  }

  return error;
}