Skip to content

Commit 05bba7e

Browse files
committed
better config
Signed-off-by: Hendrik Ebbers <[email protected]>
1 parent 9d3b4f8 commit 05bba7e

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.openelements.hiero.base.config.implementation;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.PrivateKey;
5+
import com.hedera.hashgraph.sdk.PublicKey;
6+
import com.openelements.hiero.base.config.ConsensusNode;
7+
import com.openelements.hiero.base.config.HieroConfig;
8+
import com.openelements.hiero.base.data.Account;
9+
import java.util.Optional;
10+
import java.util.Set;
11+
import org.jspecify.annotations.NonNull;
12+
13+
public class EnvBasedHieroConfig implements HieroConfig {
14+
15+
private final AccountId operatorAccountId;
16+
private final PublicKey operatorPublicKey;
17+
private final PrivateKey operatorPrivateKey;
18+
private final String mirrorNodeAddress;
19+
20+
private final String consensusNodeIp;
21+
private final String consensusNodePort;
22+
private final String consensusNodeAccount;
23+
24+
private final String relayUrl;
25+
26+
private final Long chainId;
27+
28+
public EnvBasedHieroConfig() {
29+
operatorAccountId = getEnv("HEDERA_OPERATOR_ACCOUNT_ID")
30+
.map(AccountId::fromString)
31+
.orElseThrow(() -> new IllegalStateException("HEDERA_OPERATOR_ACCOUNT_ID is not set"));
32+
operatorPublicKey = getEnv("HEDERA_OPERATOR_PUBLIC_KEY")
33+
.map(PublicKey::fromString)
34+
.orElseThrow(() -> new IllegalStateException("HEDERA_OPERATOR_PUBLIC_KEY is not set"));
35+
operatorPrivateKey = getEnv("HEDERA_OPERATOR_PRIVATE_KEY")
36+
.map(PrivateKey::fromString)
37+
.orElseThrow(() -> new IllegalStateException("HEDERA_OPERATOR_PRIVATE_KEY is not set"));
38+
mirrorNodeAddress = getEnv("HEDERA_MIRROR_NODE_ADDRESS")
39+
.orElse(null);
40+
consensusNodeIp = getEnv("HEDERA_CONSENSUS_NODE_IP")
41+
.orElseThrow(() -> new IllegalStateException("HEDERA_CONSENSUS_NODE_IP is not set"));
42+
consensusNodePort = getEnv("HEDERA_CONSENSUS_NODE_PORT")
43+
.orElseThrow(() -> new IllegalStateException("HEDERA_CONSENSUS_NODE_PORT is not set"));
44+
consensusNodeAccount = getEnv("HEDERA_CONSENSUS_NODE_ACCOUNT")
45+
.orElseThrow(() -> new IllegalStateException("HEDERA_CONSENSUS_NODE_ACCOUNT is not set"));
46+
relayUrl = getEnv("HEDERA_RELAY_URL").orElse(null);
47+
chainId = getEnv("HEDERA_CHAIN_ID")
48+
.map(Long::valueOf)
49+
.orElse(null);
50+
}
51+
52+
private Optional<String> getEnv(String key) {
53+
return Optional.ofNullable(System.getenv(key));
54+
}
55+
56+
@Override
57+
public @NonNull Account getOperatorAccount() {
58+
return new Account(operatorAccountId, operatorPublicKey, operatorPrivateKey);
59+
}
60+
61+
@Override
62+
public @NonNull Optional<String> getNetworkName() {
63+
return Optional.empty();
64+
}
65+
66+
@Override
67+
public @NonNull Set<String> getMirrorNodeAddresses() {
68+
if (mirrorNodeAddress == null) {
69+
return Set.of();
70+
}
71+
return Set.of(mirrorNodeAddress);
72+
}
73+
74+
@Override
75+
public @NonNull Set<ConsensusNode> getConsensusNodes() {
76+
ConsensusNode node = new ConsensusNode(
77+
consensusNodeIp,
78+
consensusNodePort,
79+
consensusNodeAccount
80+
);
81+
return Set.of(node);
82+
}
83+
84+
@Override
85+
public @NonNull Optional<Long> chainId() {
86+
return Optional.ofNullable(chainId);
87+
}
88+
89+
@Override
90+
public @NonNull Optional<String> relayUrl() {
91+
return Optional.ofNullable(relayUrl);
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.openelements.hiero.base.config.implementation;
2+
3+
import com.openelements.hiero.base.config.ConsensusNode;
4+
import com.openelements.hiero.base.config.HieroConfig;
5+
import com.openelements.hiero.base.config.NetworkSettings;
6+
import com.openelements.hiero.base.data.Account;
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
import java.util.Set;
10+
import org.jspecify.annotations.NonNull;
11+
12+
public class NetworkSettingsBasedHieroConfig implements HieroConfig {
13+
14+
private final Account operatorAccount;
15+
16+
private final NetworkSettings networkSetting;
17+
18+
public NetworkSettingsBasedHieroConfig(@NonNull final Account operatorAccount, @NonNull final String networkName) {
19+
this.operatorAccount = Objects.requireNonNull(operatorAccount, "operatorAccount must not be null");
20+
Objects.requireNonNull(networkName, "networkName must not be null");
21+
this.networkSetting = NetworkSettings.forIdentifier(networkName)
22+
.orElseThrow(() -> new IllegalStateException("Network settings for '" + networkName + "' not found"));
23+
}
24+
25+
@Override
26+
public @NonNull Account getOperatorAccount() {
27+
return operatorAccount;
28+
}
29+
30+
@Override
31+
public @NonNull Optional<String> getNetworkName() {
32+
return networkSetting.getNetworkName();
33+
}
34+
35+
@Override
36+
public @NonNull Set<String> getMirrorNodeAddresses() {
37+
return networkSetting.getMirrorNodeAddresses();
38+
}
39+
40+
@Override
41+
public @NonNull Set<ConsensusNode> getConsensusNodes() {
42+
return networkSetting.getConsensusNodes();
43+
}
44+
45+
@Override
46+
public @NonNull Optional<Long> chainId() {
47+
return networkSetting.chainId();
48+
}
49+
50+
@Override
51+
public @NonNull Optional<String> relayUrl() {
52+
return networkSetting.relayUrl();
53+
}
54+
}

0 commit comments

Comments
 (0)