Skip to content
This repository was archived by the owner on Feb 13, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/business-partner-agent
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hyperledger.bpa.api.aries;

import io.micronaut.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hyperledger.acy_py.generated.model.TransactionRecord;
import org.hyperledger.bpa.impl.util.AriesStringUtil;
import org.hyperledger.bpa.impl.util.Converter;
import org.hyperledger.bpa.impl.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class TransactionAPI {

private UUID transactionId;

private UUID connectionId;

private Boolean endorserWriteTxn;

private List<Object> formats;

private List<Object> messagesAttach;

private List<Object> signatureRequest;

private List<Object> signatureResponse;

private String state;

private UUID threadId;

private Object timing;

private Long createdAt;

private Long updatedAt;

public static TransactionAPI from(TransactionRecord s) {
TransactionAPIBuilder builder = TransactionAPI.builder()
.transactionId(UUID.fromString(s.getTransactionId()))
.connectionId(UUID.fromString(s.getConnectionId()))
.endorserWriteTxn(s.getEndorserWriteTxn())
// TODO convert from List<Map<String, String>> to appropriate object
// .formats(s.getFormats())
// .messagesAttach(s.getMessagesAttach())
// .signatureRequest(s.getSignatureRequest())
// .signatureResponse(s.getSignatureResponse())
.state(s.getState())
// .threadId(UUID.fromString(s.getThreadId()))
.timing(s.getTiming())
.createdAt(TimeUtil.fromISOInstant(s.getCreatedAt()).toEpochMilli())
.updatedAt(TimeUtil.fromISOInstant(s.getUpdatedAt()).toEpochMilli());
return builder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/acapy-java-client
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.bpa.api.aries;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;

public enum TransactionRole {
@JsonProperty("Author")
@SerializedName("Author")
AUTHOR,

@JsonProperty("Endorser")
@SerializedName("Endorser")
ENDORSER,
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.hyperledger.aries.AriesClient;
import org.hyperledger.bpa.api.aries.TransactionRole;
import org.hyperledger.bpa.impl.StartupTasks;
import org.hyperledger.bpa.impl.activity.DidResolver;

Expand Down Expand Up @@ -87,6 +89,9 @@ public class RuntimeConfig implements ApplicationEventListener<StartupTasks.AcaP
@Value("${bpa.i18n.fallbackLocale}")
String fallbackLocale;

@Value("${bpa.endorser.role}")
String endorserRole;

/** only set when running from .jar */
String buildVersion;

Expand Down Expand Up @@ -122,4 +127,16 @@ public void onApplicationEvent(StartupTasks.AcaPyReady event) {
log.warn("aca-py is not reachable");
}
}

public boolean hasEndorserRole() {
return (StringUtils.isNotBlank(getEndorserRole()));
}

public boolean isEndorser() {
return (hasEndorserRole() && endorserRole.equalsIgnoreCase(TransactionRole.ENDORSER.name()));
}

public boolean isAuthor() {
return (hasEndorserRole() && endorserRole.equalsIgnoreCase(TransactionRole.AUTHOR.name()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/business-partner-agent
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hyperledger.bpa.controller.api.endorser;

public class Transaction {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hyperledger.aries.api.connection.ConnectionState;
import org.hyperledger.bpa.api.PartnerAPI;
Expand All @@ -45,6 +46,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Slf4j
@Singleton
public class PartnerManager {

Expand Down Expand Up @@ -142,6 +144,7 @@ public Optional<PartnerAPI> updatePartner(@NonNull UUID id, @NonNull UpdatePartn
p.setAlias(req.getAlias());
p.setTrustPing(req.getTrustPing());
tagRepo.updateAllPartnerToTagMappings(id, req.getTag());
cm.addConnectionEndorserMetadata(p);
repo.updateAlias(id, req.getAlias(), req.getTrustPing());
if (StringUtils.isNotBlank(req.getAlias())) {
holderCredExRepo.updateIssuerByPartnerId(dbP.get().getId(), req.getAlias());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.bpa.api.TagAPI;
import org.hyperledger.bpa.api.aries.TransactionRole;
import org.hyperledger.bpa.api.exception.EntityNotFoundException;
import org.hyperledger.bpa.api.exception.WrongApiUsageException;
import org.hyperledger.bpa.config.BPAMessageSource;
import org.hyperledger.bpa.config.RuntimeConfig;
import org.hyperledger.bpa.config.TagConfig;
import org.hyperledger.bpa.model.Tag;
import org.hyperledger.bpa.repository.TagRepository;
Expand All @@ -40,6 +42,9 @@ public class TagService {
@Inject
TagRepository tagRepo;

@Inject
RuntimeConfig config;

@Inject
TagConfig configuredTags;

Expand Down Expand Up @@ -107,5 +112,17 @@ public void createDefaultTags() {
});
}

// if an endorser/autor role is explicitely set ...
if (TransactionRole.ENDORSER.name().equalsIgnoreCase(config.getEndorserRole())) {
// if BPA is an Endorser, can set connected partners as Authors
if (tagRepo.contByName(TransactionRole.AUTHOR.name()) == 0) {
addTag(TransactionRole.AUTHOR.name());
}
} else if (TransactionRole.AUTHOR.name().equalsIgnoreCase(config.getEndorserRole())) {
// if BPA is an Author, can set connected partners as Endorsers
if (tagRepo.contByName(TransactionRole.ENDORSER.name()) == 0) {
addTag(TransactionRole.ENDORSER.name());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2020-2021 - for information on the respective copyright owner
* see the NOTICE file and/or the repository at
* https://github.com/hyperledger-labs/business-partner-agent
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hyperledger.bpa.impl;

import com.google.gson.Gson;
import io.micronaut.core.annotation.Nullable;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hyperledger.aries.AriesClient;
import org.hyperledger.aries.api.credential_definition.CredentialDefinition;
import org.hyperledger.aries.api.credential_definition.CredentialDefinition.CredentialDefinitionRequest;
import org.hyperledger.aries.api.credentials.Credential;
import org.hyperledger.aries.api.credentials.CredentialAttributes;
import org.hyperledger.aries.api.credentials.CredentialPreview;
import org.hyperledger.aries.api.endorser.EndorseTransactionRecord;
import org.hyperledger.aries.api.endorser.TransactionState;
import org.hyperledger.aries.api.endorser.TransactionType;
import org.hyperledger.aries.api.issue_credential_v1.CredentialExchangeRole;
import org.hyperledger.aries.api.issue_credential_v1.CredentialExchangeState;
import org.hyperledger.aries.api.issue_credential_v1.V1CredentialExchange;
import org.hyperledger.aries.api.issue_credential_v1.V1CredentialProposalRequest;
import org.hyperledger.aries.api.schema.SchemaSendResponse;
import org.hyperledger.bpa.api.CredentialType;
import org.hyperledger.bpa.api.aries.SchemaAPI;
import org.hyperledger.bpa.api.aries.TransactionRole;
import org.hyperledger.bpa.api.exception.IssuerException;
import org.hyperledger.bpa.api.exception.NetworkException;
import org.hyperledger.bpa.api.exception.WrongApiUsageException;
import org.hyperledger.bpa.config.RuntimeConfig;
import org.hyperledger.bpa.controller.api.endorser.Transaction;
import org.hyperledger.bpa.impl.util.Converter;
import org.hyperledger.bpa.impl.util.TimeUtil;
import org.hyperledger.bpa.model.BPATransaction;
import org.hyperledger.bpa.model.Partner;
import org.hyperledger.bpa.repository.BPATransactionRepository;
import org.hyperledger.bpa.repository.PartnerRepository;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Singleton
public class TransactionManager {

@Inject
AriesClient ac;

@Inject
BPATransactionRepository transactionRepo;

@Inject
PartnerRepository partnerRepo;

@Inject
Converter conv;

@Inject
RuntimeConfig config;

public void handleEndorserTransactionEvent(@NonNull EndorseTransactionRecord transaction) {
// first check if we already have this transaction in our DB
Optional<BPATransaction> result = Optional.empty();
result = transactionRepo.findByTransactionId(transaction.getTransactionId());

result.ifPresentOrElse(
txn -> {
// if yes the update the state
transactionRepo.updateState(txn.getId(), transaction.getState());
},
() -> {
// if no then add it
Optional<Partner> partner = Optional.empty();
partner = partnerRepo.findByConnectionId(transaction.getConnectionId());
partner.ifPresentOrElse(
p -> {
BPATransaction txn = BPATransaction
.builder()
.createdAt(TimeUtil.fromISOInstant(transaction.getCreatedAt()))
.partner(p)
.role(config.isAuthor() ? TransactionRole.AUTHOR : TransactionRole.ENDORSER)
.threadId(transaction.getThreadId())
.transactionId(transaction.getTransactionId())
.state(transaction.getState())
.endorserWriteTransaction(transaction.getEndorserWriteTxn() != null
? transaction.getEndorserWriteTxn()
: Boolean.FALSE)
.updatedAt(TimeUtil.fromISOInstant(transaction.getUpdatedAt()))
.build();
transactionRepo.save(txn);
},
() -> {
// TODO raise exception if partner is not found

});
});

}

public void listTransactions() {
}

public void readTransaction(@NonNull UUID transactionId) {
}
}
Loading