|
18 | 18 | import jakarta.enterprise.context.ApplicationScoped; |
19 | 19 | import jakarta.enterprise.inject.Produces; |
20 | 20 | import jakarta.inject.Inject; |
21 | | -import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import org.eclipse.microprofile.config.inject.ConfigProperties; |
22 | 26 | import org.jspecify.annotations.NonNull; |
23 | 27 |
|
24 | 28 | public class ClientProvider { |
25 | 29 |
|
26 | 30 | @Inject |
27 | | - @ConfigProperty(name = "hedera.accountId") |
28 | | - private String accountIdAsString; |
| 31 | + @ConfigProperties |
| 32 | + private HieroOperatorConfiguration configuration; |
29 | 33 |
|
30 | 34 | @Inject |
31 | | - @ConfigProperty(name = "hedera.privateKey") |
32 | | - private String privateKeyAsString; |
| 35 | + @ConfigProperties |
| 36 | + private HieroNetworkConfiguration networkConfiguration; |
33 | 37 |
|
34 | | - @Inject |
35 | | - @ConfigProperty(name = "hedera.network") |
36 | | - private String network; |
37 | 38 |
|
38 | 39 | private AccountId getAccountId() { |
39 | | - if (network == null) { |
| 40 | + if (configuration == null) { |
| 41 | + throw new IllegalStateException("configuration is null"); |
| 42 | + } |
| 43 | + final String accountId = configuration.getAccountId(); |
| 44 | + if (accountId == null) { |
40 | 45 | throw new IllegalStateException("accountId value is null"); |
41 | 46 | } |
42 | 47 | try { |
43 | | - return AccountId.fromString(accountIdAsString); |
| 48 | + return AccountId.fromString(accountId); |
44 | 49 | } catch (Exception e) { |
45 | 50 | throw new IllegalArgumentException( |
46 | | - "Can not parse 'hedera.newAccountId' property: '" + accountIdAsString + "'", e); |
| 51 | + "Can not parse 'hedera.newAccountId' property: '" + accountId + "'", e); |
47 | 52 | } |
48 | 53 | } |
49 | 54 |
|
50 | 55 | private PrivateKey getPrivateKey() { |
51 | | - if (network == null) { |
| 56 | + if (configuration == null) { |
| 57 | + throw new IllegalStateException("configuration is null"); |
| 58 | + } |
| 59 | + final String privateKey = configuration.getPrivateKey(); |
| 60 | + if (privateKey == null) { |
52 | 61 | throw new IllegalStateException("privateKey value is null"); |
53 | 62 | } |
54 | 63 | try { |
55 | | - return PrivateKey.fromString(privateKeyAsString); |
| 64 | + return PrivateKey.fromString(privateKey); |
56 | 65 | } catch (Exception e) { |
57 | 66 | throw new IllegalArgumentException( |
58 | | - "Can not parse 'hedera.privateKey' property: '" + privateKeyAsString + "'", e); |
| 67 | + "Can not parse 'hedera.privateKey' property: '" + privateKey + "'", e); |
59 | 68 | } |
60 | 69 | } |
61 | 70 |
|
62 | 71 | private HederaNetwork getHederaNetwork() { |
63 | | - if (network == null) { |
| 72 | + if (networkConfiguration == null) { |
64 | 73 | throw new IllegalStateException("network value is null"); |
65 | 74 | } |
66 | | - return HederaNetwork.findByName(network) |
| 75 | + final String networkName = networkConfiguration.getName(); |
| 76 | + if (networkName == null) { |
| 77 | + throw new IllegalStateException("networkName is null"); |
| 78 | + } |
| 79 | + return HederaNetwork.findByName(networkName) |
67 | 80 | .orElse(HederaNetwork.CUSTOM); |
68 | 81 | } |
69 | 82 |
|
70 | 83 | private Client createClient() { |
71 | 84 | final AccountId accountId = getAccountId(); |
72 | 85 | final PrivateKey privateKey = getPrivateKey(); |
73 | 86 | final HederaNetwork hederaNetwork = getHederaNetwork(); |
74 | | - return Client.forName(hederaNetwork.getName()) |
75 | | - .setOperator(accountId, privateKey); |
| 87 | + if (Objects.equals(HederaNetwork.CUSTOM, hederaNetwork)) { |
| 88 | + final Map<String, AccountId> nodes = new HashMap<>(); |
| 89 | + networkConfiguration.getNodes() |
| 90 | + .forEach(node -> nodes.put(node.ip() + ":" + node.port(), AccountId.fromString(node.account()))); |
| 91 | + Client client = Client.forNetwork(nodes); |
| 92 | + try { |
| 93 | + client.setMirrorNetwork(List.of(networkConfiguration.getMirrornode())); |
| 94 | + } catch (InterruptedException e) { |
| 95 | + throw new RuntimeException("Error setting mirror network", e); |
| 96 | + } |
| 97 | + client.setOperator(accountId, privateKey); |
| 98 | + return client; |
| 99 | + } else { |
| 100 | + return Client.forName(hederaNetwork.getName()) |
| 101 | + .setOperator(accountId, privateKey); |
| 102 | + } |
76 | 103 | } |
77 | 104 |
|
78 | 105 | @Produces |
|
0 commit comments