-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathGatewayConfig.java
101 lines (94 loc) · 3.88 KB
/
GatewayConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package hlf.java.rest.client.config;
import hlf.java.rest.client.service.HFClientWrapper;
import hlf.java.rest.client.service.impl.HFClientWrapperImpl;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.hyperledger.fabric.gateway.impl.identity.X509IdentityProvider;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/** Configure Gateway connection for the Fabric network. */
@Slf4j
@Configuration
public class GatewayConfig {
@Autowired private FabricProperties fabricProperties;
/**
* Create the gateway connection for connecting to the peer.
*
* @return gateway Gateway object to connect to Fabric network
* @throws IOException
*/
@Bean
@RefreshScope
public Gateway gateway(Wallet wallet) throws IOException {
// Load the Network Connection Configuration path
Path networkConfigPath =
Paths.get(
fabricProperties.getOrgConnectionConfig().getPath(),
fabricProperties.getOrgConnectionConfig().getFilename());
// Create the gateway builder based on the path to the org configuration file
// using the specified user, then connect
Gateway.Builder builder = Gateway.createBuilder();
builder
.identity(wallet, fabricProperties.getWallet().getClientUser().getName())
.networkConfig(networkConfigPath)
.discovery(fabricProperties.isDiscoveryEnabled());
return builder.connect();
}
/**
* Obtain the Wallet details containing the user id information.
*
* @return wallet Wallet pull credentials from wallet
* @throws IOException
*/
@Bean
@RefreshScope
public Wallet wallet() throws IOException {
log.info("Obtain the Wallet containing Admin and Client user information");
// Load a file system based wallet for managing identities.
Path walletPath = Paths.get(fabricProperties.getWallet().getPath());
return Wallets.newFileSystemWallet(walletPath);
}
/**
* Hf client for operations APIs provided by fabric sdk.
*
* @param gateway the gateway
* @return the hf client
* @throws InvalidArgumentException the invalid argument exception
* @throws CryptoException the crypto exception
* @throws ClassNotFoundException the class not found exception
* @throws InvocationTargetException the invocation target exception
* @throws IllegalAccessException the illegal access exception
* @throws InstantiationException the instantiation exception
* @throws NoSuchMethodException the no such method exception
*/
@Bean
@RefreshScope
public HFClientWrapper hfClient(Gateway gateway)
throws InvalidArgumentException, CryptoException, ClassNotFoundException,
InvocationTargetException, IllegalAccessException, InstantiationException,
NoSuchMethodException {
log.info("Setting up HFClient for operations APIs.");
HFClient hfClient = HFClient.createNewInstance();
hfClient.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
X509IdentityProvider.INSTANCE.setUserContext(hfClient, gateway.getIdentity(), "hlf-connector");
return new HFClientWrapperImpl(hfClient);
}
@Bean
@RefreshScope
public User user(HFClientWrapper hfClient) throws IOException {
return hfClient.getHfClient().getUserContext();
}
}