|
5 | 5 | import com.hedera.hashgraph.sdk.PrivateKey; |
6 | 6 | import com.hedera.hashgraph.sdk.PublicKey; |
7 | 7 | import com.openelements.hiero.base.HieroContext; |
| 8 | +import com.openelements.hiero.base.config.NetworkSettings; |
8 | 9 | import com.openelements.hiero.base.data.Account; |
9 | 10 | import io.github.cdimascio.dotenv.Dotenv; |
10 | 11 | import java.util.HashMap; |
11 | | -import java.util.List; |
12 | 12 | import java.util.Map; |
13 | | -import java.util.Objects; |
| 13 | +import java.util.Optional; |
14 | 14 | import org.jspecify.annotations.NonNull; |
| 15 | +import org.slf4j.Logger; |
15 | 16 |
|
16 | 17 | public class HieroTestContext implements HieroContext { |
17 | 18 |
|
| 19 | + private final static Logger log = org.slf4j.LoggerFactory.getLogger(HieroTestContext.class); |
| 20 | + |
18 | 21 | private final Account operationalAccount; |
19 | 22 |
|
20 | 23 | private final Client client; |
21 | 24 |
|
22 | 25 | public HieroTestContext() { |
23 | | - final String hieroAccountIdByEnv = System.getenv("HEDERA_ACCOUNT_ID"); |
24 | | - final String hieroPrivateKeyByEnv = System.getenv("HEDERA_PRIVATE_KEY"); |
25 | | - final String hieroNetwork = System.getenv("HEDERA_NETWORK"); |
| 26 | + final Dotenv dotenv = Dotenv.load(); |
| 27 | + |
| 28 | + final String hieroAccountIdByEnv = Optional.ofNullable(System.getenv("HEDERA_ACCOUNT_ID")) |
| 29 | + .orElse(dotenv.get("hiero.accountId")); |
| 30 | + final String hieroPrivateKeyByEnv = Optional.ofNullable(System.getenv("HEDERA_PRIVATE_KEY")) |
| 31 | + .orElse(dotenv.get("hiero.privateKey")); |
| 32 | + final String hieroNetwork = Optional.ofNullable(System.getenv("HEDERA_NETWORK")) |
| 33 | + .or(() -> Optional.ofNullable(dotenv.get("hiero.network"))) |
| 34 | + .orElse("hedera-testnet"); |
| 35 | + |
| 36 | + if (hieroAccountIdByEnv == null) { |
| 37 | + throw new IllegalStateException( |
| 38 | + "AccountId for operator account is not set. Please set 'HEDERA_ACCOUNT_ID' or 'hiero.accountId' in .env file."); |
| 39 | + } |
| 40 | + if (hieroPrivateKeyByEnv == null) { |
| 41 | + throw new IllegalStateException( |
| 42 | + "PrivateKey for operator account is not set. Please set 'HEDERA_PRIVATE_KEY' or 'hiero.privateKey' in .env file."); |
| 43 | + } |
| 44 | + |
| 45 | + log.info("Using operator account: {}", hieroAccountIdByEnv); |
| 46 | + log.info("Using network: {}", hieroNetwork); |
| 47 | + |
| 48 | + final AccountId accountId = AccountId.fromString(hieroAccountIdByEnv); |
| 49 | + final PrivateKey privateKey = PrivateKey.fromString(hieroPrivateKeyByEnv); |
| 50 | + final PublicKey publicKey = privateKey.getPublicKey(); |
| 51 | + operationalAccount = new Account(accountId, publicKey, privateKey); |
| 52 | + |
| 53 | + final NetworkSettings networkSettings = NetworkSettings.forIdentifier(hieroNetwork) |
| 54 | + .orElseThrow(() -> new IllegalStateException("ENV 'HEDERA_NETWORK' is set to '" + hieroNetwork |
| 55 | + + "' but no network settings are available for this network.")); |
26 | 56 |
|
27 | | - if (hieroAccountIdByEnv != null && hieroPrivateKeyByEnv != null) { |
28 | | - final AccountId accountId = AccountId.fromString(hieroAccountIdByEnv); |
29 | | - final PrivateKey privateKey = PrivateKey.fromString(hieroPrivateKeyByEnv); |
30 | | - final PublicKey publicKey = privateKey.getPublicKey(); |
31 | | - operationalAccount = new Account(accountId, publicKey, privateKey); |
32 | | - if (Objects.equals(hieroNetwork, "testnet")) { |
33 | | - client = Client.forTestnet(); |
34 | | - client.setOperator(accountId, privateKey); |
35 | | - } else { |
36 | | - //SOLO |
37 | | - final Map<String, AccountId> nodes = new HashMap<>(); |
38 | | - nodes.put("127.0.0.1:50211", AccountId.fromString("0.0.3")); |
39 | | - client = Client.forNetwork(nodes); |
40 | | - try { |
41 | | - client.setMirrorNetwork(List.of("localhost:8080")); |
42 | | - } catch (Exception e) { |
43 | | - throw new IllegalStateException("Error setting mirror network", e); |
44 | | - } |
45 | | - client.setOperator(accountId, privateKey); |
| 57 | + final Map<String, AccountId> nodes = new HashMap<>(); |
| 58 | + networkSettings.getConsensusNodes() |
| 59 | + .forEach(consensusNode -> nodes.put(consensusNode.getAddress(), consensusNode.getAccountId())); |
| 60 | + client = Client.forNetwork(nodes); |
| 61 | + if (!networkSettings.getMirrorNodeAddresses().isEmpty()) { |
| 62 | + try { |
| 63 | + client.setMirrorNetwork(networkSettings.getMirrorNodeAddresses().stream().toList()); |
| 64 | + } catch (InterruptedException e) { |
| 65 | + throw new RuntimeException("Error in configuring Mirror Node", e); |
46 | 66 | } |
47 | | - } else { |
48 | | - final Dotenv dotenv = Dotenv.load(); |
49 | | - final String accountIdAsString = dotenv.get("hiero.accountId"); |
50 | | - final String privateKeyAsString = dotenv.get("hiero.privateKey"); |
51 | | - final AccountId accountId = AccountId.fromString(accountIdAsString); |
52 | | - final PrivateKey privateKey = PrivateKey.fromString(privateKeyAsString); |
53 | | - final PublicKey publicKey = privateKey.getPublicKey(); |
54 | | - operationalAccount = new Account(accountId, publicKey, privateKey); |
55 | | - client = Client.forTestnet(); |
56 | | - client.setOperator(accountId, privateKey); |
57 | 67 | } |
| 68 | + client.setOperator(accountId, privateKey); |
58 | 69 | } |
59 | 70 |
|
60 | 71 | @Override |
|
0 commit comments