11package com .openelements .hedera .microprofile ;
22
3- import com .hedera .hashgraph .sdk .AccountId ;
4- import com .hedera .hashgraph .sdk .Client ;
5- import com .hedera .hashgraph .sdk .PrivateKey ;
6- import com .openelements .hedera .base .Account ;
73import com .openelements .hedera .base .AccountClient ;
84import com .openelements .hedera .base .ContractVerificationClient ;
95import com .openelements .hedera .base .FileClient ;
106import com .openelements .hedera .base .SmartContractClient ;
7+ import com .openelements .hedera .base .config .HieroConfig ;
118import com .openelements .hedera .base .implementation .AccountClientImpl ;
129import com .openelements .hedera .base .implementation .FileClientImpl ;
13- import com .openelements .hedera .base .implementation .HederaNetwork ;
1410import com .openelements .hedera .base .implementation .ProtocolLayerClientImpl ;
1511import com .openelements .hedera .base .implementation .SmartContractClientImpl ;
1612import com .openelements .hedera .base .protocol .ProtocolLayerClient ;
1713import com .openelements .hedera .microprofile .implementation .ContractVerificationClientImpl ;
14+ import com .openelements .hedera .microprofile .implementation .HieroConfigImpl ;
1815import jakarta .enterprise .context .ApplicationScoped ;
1916import jakarta .enterprise .inject .Produces ;
2017import jakarta .inject .Inject ;
21- import java .util .List ;
22- import java .util .Map ;
23- import java .util .Objects ;
24- import java .util .stream .Collectors ;
2518import org .eclipse .microprofile .config .inject .ConfigProperties ;
2619import org .jspecify .annotations .NonNull ;
2720
@@ -35,78 +28,16 @@ public class ClientProvider {
3528 @ ConfigProperties
3629 private HieroNetworkConfiguration networkConfiguration ;
3730
38-
39- private AccountId getAccountId () {
40- if (configuration == null ) {
41- throw new IllegalStateException ("configuration is null" );
42- }
43- final String accountId = configuration .getAccountId ();
44- if (accountId == null ) {
45- throw new IllegalStateException ("accountId value is null" );
46- }
47- try {
48- return AccountId .fromString (accountId );
49- } catch (Exception e ) {
50- throw new IllegalArgumentException (
51- "Can not parse 'hedera.newAccountId' property: '" + accountId + "'" , e );
52- }
53- }
54-
55- private PrivateKey getPrivateKey () {
56- if (configuration == null ) {
57- throw new IllegalStateException ("configuration is null" );
58- }
59- final String privateKey = configuration .getPrivateKey ();
60- if (privateKey == null ) {
61- throw new IllegalStateException ("privateKey value is null" );
62- }
63- try {
64- return PrivateKey .fromString (privateKey );
65- } catch (Exception e ) {
66- throw new IllegalArgumentException (
67- "Can not parse 'hedera.privateKey' property: '" + privateKey + "'" , e );
68- }
69- }
70-
71- private HederaNetwork getHederaNetwork () {
72- if (networkConfiguration == null ) {
73- throw new IllegalStateException ("network value is null" );
74- }
75- return networkConfiguration .getName ()
76- .map (n -> HederaNetwork .findByName (n ).orElse (HederaNetwork .CUSTOM ))
77- .orElse (HederaNetwork .CUSTOM );
78- }
79-
80- private Client createClient () {
81- final AccountId accountId = getAccountId ();
82- final PrivateKey privateKey = getPrivateKey ();
83- final HederaNetwork hederaNetwork = getHederaNetwork ();
84- if (Objects .equals (HederaNetwork .CUSTOM , hederaNetwork )) {
85- final Map <String , AccountId > nodes = networkConfiguration .getNodes ()
86- .stream ().collect (Collectors .toMap (n -> n .getAddress (), n -> n .getAccountId ()));
87- Client client = Client .forNetwork (nodes );
88- networkConfiguration .getMirrornode ()
89- .map (mirrorNode -> List .of (mirrorNode ))
90- .ifPresent (mirrorNodes -> {
91- try {
92- client .setMirrorNetwork (mirrorNodes );
93- } catch (InterruptedException e ) {
94- throw new RuntimeException ("Error setting mirror network" , e );
95- }
96- });
97- client .setOperator (accountId , privateKey );
98- return client ;
99- } else {
100- return Client .forName (hederaNetwork .getName ())
101- .setOperator (accountId , privateKey );
102- }
31+ @ Produces
32+ @ ApplicationScoped
33+ HieroConfig createHieroConfig () {
34+ return new HieroConfigImpl (configuration , networkConfiguration );
10335 }
10436
10537 @ Produces
10638 @ ApplicationScoped
107- ProtocolLayerClient createProtocolLayerClient () {
108- final Account operator = Account .of (getAccountId (), getPrivateKey ());
109- return new ProtocolLayerClientImpl (createClient (), operator );
39+ ProtocolLayerClient createProtocolLayerClient (@ NonNull final HieroConfig hieroConfig ) {
40+ return new ProtocolLayerClientImpl (hieroConfig .createClient (), hieroConfig .getOperatorAccount ());
11041 }
11142
11243 @ Produces
@@ -130,8 +61,7 @@ AccountClient createAccountClient(@NonNull final ProtocolLayerClient protocolLay
13061
13162 @ Produces
13263 @ ApplicationScoped
133- ContractVerificationClient createContractVerificationClient (@ NonNull final ProtocolLayerClient protocolLayerClient ,
134- @ NonNull final FileClient fileClient ) {
135- return new ContractVerificationClientImpl (getHederaNetwork ());
64+ ContractVerificationClient createContractVerificationClient (@ NonNull final HieroConfig hieroConfig ) {
65+ return new ContractVerificationClientImpl (hieroConfig .getNetwork ());
13666 }
13767}
0 commit comments