|
| 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 | +} |
0 commit comments