|
5 | 5 | import co.nstant.in.cbor.model.UnicodeString;
|
6 | 6 | import com.bloxbean.cardano.client.exception.AddressExcepion;
|
7 | 7 | import com.bloxbean.cardano.client.exception.CborSerializationException;
|
| 8 | +import java.util.Optional; |
8 | 9 | import lombok.RequiredArgsConstructor;
|
9 | 10 | import lombok.extern.slf4j.Slf4j;
|
10 | 11 | import org.cardanofoundation.rosetta.api.block.model.entity.ProtocolParams;
|
| 12 | +import org.cardanofoundation.rosetta.common.enumeration.NetworkIdentifierType; |
11 | 13 | import org.cardanofoundation.rosetta.common.mapper.DataMapper;
|
12 | 14 | import org.cardanofoundation.rosetta.common.enumeration.AddressType;
|
13 |
| -import org.cardanofoundation.rosetta.common.model.cardano.ConstructionDeriveRequestMetadata; |
14 | 15 |
|
15 | 16 | import org.cardanofoundation.rosetta.common.enumeration.NetworkEnum;
|
16 | 17 | import org.cardanofoundation.rosetta.common.services.CardanoAddressService;
|
17 | 18 | import org.cardanofoundation.rosetta.common.services.CardanoService;
|
18 | 19 | import org.cardanofoundation.rosetta.api.construction.service.ConstructionApiService;
|
19 | 20 | import org.cardanofoundation.rosetta.common.services.LedgerDataProviderService;
|
| 21 | +import org.cardanofoundation.rosetta.common.util.Constants; |
20 | 22 | import org.openapitools.client.model.*;
|
21 | 23 | import org.springframework.stereotype.Service;
|
22 | 24 |
|
23 | 25 | import java.io.IOException;
|
24 |
| -import java.util.HashMap; |
25 | 26 | import java.util.Map;
|
26 | 27 |
|
27 | 28 | @Service
|
28 | 29 | @Slf4j
|
29 | 30 | @RequiredArgsConstructor
|
30 | 31 | public class ConstructionApiServiceImpl implements ConstructionApiService {
|
31 | 32 |
|
32 |
| - private final CardanoAddressService cardanoAddressService; |
33 |
| - private final CardanoService cardanoService; |
34 |
| - private final LedgerDataProviderService ledgerService; |
35 |
| - private final DataMapper dataMapper; |
| 33 | + private final CardanoAddressService cardanoAddressService; |
| 34 | + private final CardanoService cardanoService; |
| 35 | + private final LedgerDataProviderService ledgerService; |
| 36 | + private final DataMapper dataMapper; |
36 | 37 |
|
37 |
| - @Override |
38 |
| - public ConstructionDeriveResponse constructionDeriveService(ConstructionDeriveRequest constructionDeriveRequest) throws IllegalAccessException, CborSerializationException { |
39 |
| - PublicKey publicKey = constructionDeriveRequest.getPublicKey(); |
40 |
| - log.info("Deriving address for public key: {}", publicKey); |
41 |
| - NetworkEnum network = NetworkEnum.fromValue(constructionDeriveRequest.getNetworkIdentifier().getNetwork()); |
42 |
| - if (network == null) |
43 |
| - throw new IllegalAccessException("Invalid network"); |
| 38 | + @Override |
| 39 | + public ConstructionDeriveResponse constructionDeriveService( |
| 40 | + ConstructionDeriveRequest constructionDeriveRequest) |
| 41 | + throws IllegalAccessException { |
| 42 | + PublicKey publicKey = constructionDeriveRequest.getPublicKey(); |
| 43 | + log.info("Deriving address for public key: {}", publicKey); |
| 44 | + NetworkEnum network = Optional.ofNullable(NetworkEnum.fromValue( |
| 45 | + constructionDeriveRequest.getNetworkIdentifier().getNetwork())).orElseThrow(() -> new IllegalAccessException("Invalid network")); |
| 46 | + // casting unspecific rosetta specification to cardano specific metadata |
| 47 | + ConstructionDeriveMetadata metadata = constructionDeriveRequest.getMetadata(); |
| 48 | + // Default address type is enterprise |
| 49 | + AddressType addressType = |
| 50 | + metadata.getAddressType() != null ? AddressType.findByValue(metadata.getAddressType()) |
| 51 | + : null; |
| 52 | + addressType = addressType != null ? addressType : AddressType.ENTERPRISE; |
44 | 53 |
|
45 |
| - ConstructionDeriveRequestMetadata metadata = ConstructionDeriveRequestMetadata.fromHashMap((HashMap<String,Object>) constructionDeriveRequest.getMetadata()); |
46 |
| - // Default address type is enterprise |
47 |
| - AddressType addressType = metadata != null ? AddressType.findByValue(metadata.getAddressType()) : null; |
48 |
| - addressType = addressType != null ? addressType : AddressType.ENTERPRISE; |
49 |
| - |
50 |
| - PublicKey stakingCredential = null; |
51 |
| - if(addressType == AddressType.BASE) { |
52 |
| - if(metadata.getStakingCredential() == null) |
53 |
| - throw new IllegalAccessException("Staking credential is required for base address"); |
54 |
| - stakingCredential = metadata.getStakingCredential(); |
55 |
| - } |
56 |
| - String address = cardanoAddressService.getCardanoAddress(addressType, stakingCredential, publicKey, network); |
57 |
| - return new ConstructionDeriveResponse(null, new AccountIdentifier(address, null, null), null); |
| 54 | + PublicKey stakingCredential = null; |
| 55 | + if (addressType == AddressType.BASE) { |
| 56 | + stakingCredential = Optional.ofNullable(metadata.getStakingCredential()) |
| 57 | + .orElseThrow(() -> new IllegalAccessException("Staking credential is required for base address")); |
58 | 58 | }
|
| 59 | + String address = cardanoAddressService.getCardanoAddress(addressType, stakingCredential, |
| 60 | + publicKey, network); |
| 61 | + return new ConstructionDeriveResponse(null, new AccountIdentifier(address, null, null), null); |
| 62 | + } |
59 | 63 |
|
60 |
| - @Override |
61 |
| - public ConstructionPreprocessResponse constructionPreprocessService(ConstructionPreprocessRequest constructionPreprocessRequest) throws IOException, AddressExcepion, CborSerializationException, CborException { |
62 |
| - return null; |
63 |
| - } |
| 64 | + @Override |
| 65 | + public ConstructionPreprocessResponse constructionPreprocessService( |
| 66 | + ConstructionPreprocessRequest constructionPreprocessRequest) |
| 67 | + throws IOException, AddressExcepion, CborSerializationException, CborException { |
| 68 | + NetworkIdentifier networkIdentifier = constructionPreprocessRequest.getNetworkIdentifier(); |
| 69 | + ConstructionPreprocessMetadata metadata = constructionPreprocessRequest.getMetadata(); |
| 70 | + Double relativeTtl = cardanoService.checkOrReturnDefaultTtl(metadata.getRelativeTtl()); |
| 71 | + Double transactionSize = cardanoService.calculateTxSize( |
| 72 | + NetworkIdentifierType.findByName(networkIdentifier.getNetwork()), |
| 73 | + constructionPreprocessRequest.getOperations(), 0, |
| 74 | + metadata.getDepositParameters()); |
| 75 | + Map<String, Double> response = Map.of(Constants.RELATIVE_TTL, relativeTtl, |
| 76 | + Constants.TRANSACTION_SIZE, |
| 77 | + transactionSize); |
| 78 | + return new ConstructionPreprocessResponse(response, null); |
| 79 | + } |
64 | 80 |
|
65 |
| - @Override |
66 |
| - public ConstructionMetadataResponse constructionMetadataService(ConstructionMetadataRequest constructionMetadataRequest) throws CborException, CborSerializationException { |
67 |
| - Map<String, Object> options = (Map<String, Object>) constructionMetadataRequest.getOptions(); |
68 |
| - Double relativeTtl = (Double) options.get("relative_ttl"); |
69 |
| - Double txSize = (Double) options.get("transaction_size"); |
70 |
| - log.debug("[constructionMetadata] Calculating ttl based on {} relative ttl", relativeTtl); |
71 |
| - Long ttl = cardanoService.calculateTtl(relativeTtl.longValue()); |
72 |
| - log.debug("[constructionMetadata] ttl is {}", ttl); |
73 |
| - log.debug("[constructionMetadata] updating tx size from {}", txSize); |
74 |
| - Long updatedTxSize = cardanoService.updateTxSize(txSize.longValue(), 0L, ttl); |
75 |
| - log.debug("[constructionMetadata] updated txSize size is ${updatedTxSize}"); |
76 |
| - ProtocolParams protocolParams = ledgerService.findProtocolParametersFromIndexerAndConfig(); |
77 |
| - log.debug("[constructionMetadata] received protocol parameters from block-service {}", |
78 |
| - protocolParams); |
79 |
| - Long suggestedFee = cardanoService.calculateTxMinimumFee(updatedTxSize, |
80 |
| - protocolParams); |
81 |
| - log.debug("[constructionMetadata] suggested fee is ${suggestedFee}"); |
82 |
| - return DataMapper.mapToMetadataResponse(protocolParams, ttl, suggestedFee); |
83 |
| - } |
| 81 | + @Override |
| 82 | + public ConstructionMetadataResponse constructionMetadataService( |
| 83 | + ConstructionMetadataRequest constructionMetadataRequest) |
| 84 | + throws CborException, CborSerializationException { |
| 85 | + Map<String, Object> options = (Map<String, Object>) constructionMetadataRequest.getOptions(); |
| 86 | + Double relativeTtl = (Double) options.get(Constants.RELATIVE_TTL); |
| 87 | + Double txSize = (Double) options.get(Constants.TRANSACTION_SIZE); |
| 88 | + log.debug("[constructionMetadata] Calculating ttl based on {} relative ttl", relativeTtl); |
| 89 | + Long ttl = cardanoService.calculateTtl(relativeTtl.longValue()); |
| 90 | + log.debug("[constructionMetadata] ttl is {}", ttl); |
| 91 | + log.debug("[constructionMetadata] updating tx size from {}", txSize); |
| 92 | + Long updatedTxSize = cardanoService.updateTxSize(txSize.longValue(), 0L, ttl); |
| 93 | + log.debug("[constructionMetadata] updated txSize size is ${updatedTxSize}"); |
| 94 | + ProtocolParams protocolParams = ledgerService.findProtocolParametersFromIndexerAndConfig(); |
| 95 | + log.debug("[constructionMetadata] received protocol parameters from block-service {}", |
| 96 | + protocolParams); |
| 97 | + Long suggestedFee = cardanoService.calculateTxMinimumFee(updatedTxSize, protocolParams); |
| 98 | + log.debug("[constructionMetadata] suggested fee is ${suggestedFee}"); |
| 99 | + return DataMapper.mapToMetadataResponse(protocolParams, ttl, suggestedFee); |
| 100 | + } |
84 | 101 |
|
85 |
| - @Override |
86 |
| - public ConstructionPayloadsResponse constructionPayloadsService(ConstructionPayloadsRequest constructionPayloadsRequest) { |
87 |
| - return null; |
88 |
| - } |
| 102 | + @Override |
| 103 | + public ConstructionPayloadsResponse constructionPayloadsService( |
| 104 | + ConstructionPayloadsRequest constructionPayloadsRequest) { |
| 105 | + return null; |
| 106 | + } |
89 | 107 |
|
90 |
| - @Override |
91 |
| - public ConstructionParseResponse constructionParseService(ConstructionParseRequest constructionParseRequest) { |
92 |
| - return null; |
93 |
| - } |
| 108 | + @Override |
| 109 | + public ConstructionParseResponse constructionParseService( |
| 110 | + ConstructionParseRequest constructionParseRequest) { |
| 111 | + return null; |
| 112 | + } |
94 | 113 |
|
95 |
| - @Override |
96 |
| - public ConstructionCombineResponse constructionCombineService(ConstructionCombineRequest constructionCombineRequest) { |
97 |
| - return null; |
98 |
| - } |
| 114 | + @Override |
| 115 | + public ConstructionCombineResponse constructionCombineService( |
| 116 | + ConstructionCombineRequest constructionCombineRequest) { |
| 117 | + return null; |
| 118 | + } |
99 | 119 |
|
100 |
| - @Override |
101 |
| - public TransactionIdentifierResponse constructionHashService(ConstructionHashRequest constructionHashRequest) { |
102 |
| - Array array = cardanoService.decodeExtraData(constructionHashRequest.getSignedTransaction()); |
103 |
| - log.info("[constructionHash] About to get hash of signed transaction"); |
104 |
| - String transactionHash = cardanoService.getHashOfSignedTransaction( |
105 |
| - ((UnicodeString) array.getDataItems().get(0)).getString()); |
106 |
| - log.info("[constructionHash] About to return hash of signed transaction"); |
107 |
| - return new TransactionIdentifierResponse(new TransactionIdentifier(transactionHash), null); |
108 |
| - } |
| 120 | + @Override |
| 121 | + public TransactionIdentifierResponse constructionHashService( |
| 122 | + ConstructionHashRequest constructionHashRequest) { |
| 123 | + Array array = cardanoService.decodeExtraData(constructionHashRequest.getSignedTransaction()); |
| 124 | + log.info("[constructionHash] About to get hash of signed transaction"); |
| 125 | + String transactionHash = cardanoService.getHashOfSignedTransaction( |
| 126 | + ((UnicodeString) array.getDataItems().getFirst()).getString()); |
| 127 | + log.info("[constructionHash] About to return hash of signed transaction"); |
| 128 | + return new TransactionIdentifierResponse(new TransactionIdentifier(transactionHash), null); |
| 129 | + } |
109 | 130 |
|
110 |
| - @Override |
111 |
| - public TransactionIdentifierResponse constructionSubmitService(ConstructionSubmitRequest constructionSubmitRequest) { |
112 |
| - return null; |
113 |
| - } |
| 131 | + @Override |
| 132 | + public TransactionIdentifierResponse constructionSubmitService( |
| 133 | + ConstructionSubmitRequest constructionSubmitRequest) { |
| 134 | + return null; |
| 135 | + } |
114 | 136 | }
|
0 commit comments