Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change getproofs logic to parallel for efficiency #370

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 96 additions & 79 deletions lib/iden3comm/domain/use_cases/get_iden3comm_proofs_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,94 +132,111 @@ class GetIden3commProofsUseCase
requests.isEmpty) {
/// We got [ProofRequestEntity], let's find the associated [ClaimEntity]
/// and generate [ProofEntity]
List<Future<Iden3commProofEntity?> Function()> getProofEntityClosures =
[];
for (int i = 0; i < requests.length; i++) {
ProofRequestEntity request = requests[i];
ClaimEntity? claim = claims[i];
getProofEntityClosures.add(() async {
ProofRequestEntity request = requests[i];
ClaimEntity? claim = claims[i];

if (claim == null) {
continue;
}

if (claim.expiration != null) {
claim = await _checkCredentialExpirationAndTryRefreshIfExpired(
claim: claim,
param: param,
);
}

bool isCircuitSupported = await _isProofCircuitSupported.execute(
param: request.scope.circuitId,
);
bool isCorrectType = claim.type == request.scope.query.type;

if (isCorrectType && isCircuitSupported) {
String circuitId = request.scope.circuitId;
CircuitDataEntity circuitData =
await _proofRepository.loadCircuitFiles(circuitId);

String? challenge;
String? privKey;
if (circuitId == CircuitType.mtponchain.name ||
circuitId == CircuitType.sigonchain.name ||
circuitId == CircuitType.circuitsV3onchain.name) {
privKey = param.privateKey;
challenge = param.challenge;
if (claim == null) {
return null;
}

var identityEntity = await _getIdentityUseCase.execute(
param: GetIdentityParam(
genesisDid: param.genesisDid,
privateKey: param.privateKey));

BigInt claimSubjectProfileNonce = identityEntity.profiles.keys
.firstWhere((k) => identityEntity.profiles[k] == claim!.did,
orElse: () => GENESIS_PROFILE_NONCE);

int? groupId = request.scope.query.groupId;
String linkNonce = "0";
// Check if groupId exists in the map
if (groupId != null) {
if (groupIdLinkNonceMap.containsKey(groupId)) {
// Use the existing linkNonce for this groupId
linkNonce = groupIdLinkNonceMap[groupId]!;
} else {
// Generate a new linkNonce for this groupId
linkNonce =
generateLinkNonce(); // Replace this with your linkNonce generation logic
groupIdLinkNonceMap[groupId] = linkNonce;
}
if (claim.expiration != null) {
claim = await _checkCredentialExpirationAndTryRefreshIfExpired(
claim: claim,
param: param,
);
}

_proofGenerationStepsStreamManager
.add("Generating proof for ${claim.type}");

// Generate proof param
GenerateIden3commProofParam proofParam =
GenerateIden3commProofParam(
did: param.genesisDid,
profileNonce: param.profileNonce,
claimSubjectProfileNonce: claimSubjectProfileNonce,
credential: claim,
request: request.scope,
circuitData: circuitData,
privateKey: privKey,
challenge: challenge,
ethereumUrl: param.ethereumUrl,
stateContractAddr: param.stateContractAddr,
ipfsNodeURL: param.ipfsNodeUrl,
verifierId: param.message.from,
linkNonce: linkNonce,
transactionData: param.transactionData,
bool isCircuitSupported = await _isProofCircuitSupported.execute(
param: request.scope.circuitId,
);
bool isCorrectType = claim.type == request.scope.query.type;

if (isCorrectType && isCircuitSupported) {
String circuitId = request.scope.circuitId;
CircuitDataEntity circuitData =
await _proofRepository.loadCircuitFiles(circuitId);

String? challenge;
String? privKey;
if (circuitId == CircuitType.mtponchain.name ||
circuitId == CircuitType.sigonchain.name ||
circuitId == CircuitType.circuitsV3onchain.name) {
privKey = param.privateKey;
challenge = param.challenge;
}

// Generate proof
Iden3commProofEntity proof =
await _generateIden3commProofUseCase.execute(
param: proofParam,
);
var identityEntity = await _getIdentityUseCase.execute(
param: GetIdentityParam(
genesisDid: param.genesisDid,
privateKey: param.privateKey));

BigInt claimSubjectProfileNonce = identityEntity.profiles.keys
.firstWhere((k) => identityEntity.profiles[k] == claim!.did,
orElse: () => GENESIS_PROFILE_NONCE);

int? groupId = request.scope.query.groupId;
String linkNonce = "0";
// Check if groupId exists in the map
if (groupId != null) {
if (groupIdLinkNonceMap.containsKey(groupId)) {
// Use the existing linkNonce for this groupId
linkNonce = groupIdLinkNonceMap[groupId]!;
} else {
// Generate a new linkNonce for this groupId
linkNonce =
generateLinkNonce(); // Replace this with your linkNonce generation logic
groupIdLinkNonceMap[groupId] = linkNonce;
}
}

proofs.add(proof);
}
_proofGenerationStepsStreamManager
.add("Generating proof for ${claim.type}");

// Generate proof param
GenerateIden3commProofParam proofParam =
GenerateIden3commProofParam(
did: param.genesisDid,
profileNonce: param.profileNonce,
claimSubjectProfileNonce: claimSubjectProfileNonce,
credential: claim,
request: request.scope,
circuitData: circuitData,
privateKey: privKey,
challenge: challenge,
ethereumUrl: param.ethereumUrl,
stateContractAddr: param.stateContractAddr,
ipfsNodeURL: param.ipfsNodeUrl,
verifierId: param.message.from,
linkNonce: linkNonce,
transactionData: param.transactionData,
);

// Generate proof
Iden3commProofEntity proof =
await _generateIden3commProofUseCase.execute(
param: proofParam,
);

return proof;
}
return null;
});

List<Iden3commProofEntity?> getProofEntityClosuresResults =
await Future.wait(
getProofEntityClosures.map((closure) => closure()));

// Filter out null values
List<Iden3commProofEntity> nonNullResults =
getProofEntityClosuresResults
.where((result) => result != null)
.map((result) => result!)
.toList();
proofs.addAll(nonNullResults);
}
} else {
_stacktraceManager.addTrace(
Expand Down
Loading