springBoot集成fabric-gateway-java,实现区块链交易及查询
- 添加依赖
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.2.3</version>
</dependency>
- 准备配置文件
在resources下创建connection.json文件
{
"name": "basic-network",
"version": "1.0.0",
"client": {
"organization": "Org1",
"connection": {
"timeout": {
"peer": {
"endorser": "300"
},
"orderer": "300"
}
}
},
"channels": {
"mychannel": {
"orderers": [
"orderer.example.com"
],
"peers": {
"peer0.org1.example.com": {
"endorsingPeer": true,
"chaincodeQuery": true,
"ledgerQuery": true,
"eventSource": true
},
"peer0.org2.example.com": {
"endorsingPeer": true,
"chaincodeQuery": true,
"ledgerQuery": true,
"eventSource": true
}
}
}
},
"organizations": {
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
],
"certificateAuthorities": [
"ca-org1"
],
"adminPrivateKeyPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
}
},
"Org2": {
"mspid": "Org2MSP",
"peers": [
"peer0.org2.example.com"
],
"certificateAuthorities": [
"ca-org2"
],
"adminPrivateKeyPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp/signcerts/Admin@org2.example.com-cert.pem"
}
}
},
"orderers": {
"orderer.example.com": {
"url": "grpcs://192.168.5.20:7050",
"mspid": "OrdererMSP",
"grpcOptions": {
"ssl-target-name-override": "orderer.example.com",
"hostnameOverride": "orderer.example.com"
},
"tlsCACerts": {
"path": "src/main/resources/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/ca.crt"
},
"adminPrivateKeyPEM": {
"path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/priv_sk"
},
"signedCertPEM": {
"path": "src/main/resources/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem"
}
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpcs://192.168.5.20:7051",
"grpcOptions": {
"ssl-target-name-override": "peer0.org1.example.com",
"hostnameOverride": "peer0.org1.example.com",
"request-timeout": 120001
},
"tlsCACerts": {
"path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"
}
},
"peer0.org2.example.com": {
"url": "grpcs://192.168.5.20:9051",
"grpcOptions": {
"ssl-target-name-override": "peer0.org2.example.com",
"hostnameOverride": "peer0.org2.example.com",
"request-timeout": 120001
},
"tlsCACerts": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt"
}
}
}
,
"certificateAuthorities": {
"ca-org1": {
"url": "https://192.168.5.20:7054",
"grpcOptions": {
"verify": true
},
"tlsCACerts": {
"path": "src/main/resources/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem"
},
"registrar": [
{
"enrollId": "admin",
"enrollSecret": "adminpw"
}
]
},
"ca-org2": {
"url": "https://192.168.5.20:8054",
"grpcOptions": {
"verify": true
},
"tlsCACerts": {
"path": "src/main/resources/crypto-config/peerOrganizations/org2.example.com/ca/ca.org2.example.com-cert.pem"
},
"registrar": [
{
"enrollId": "admin",
"enrollSecret": "adminpw"
}
]
}
}
}
- 代码实现
import org.hyperledger.fabric.gateway.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import static org.hyperledger.fabric.gateway.Identities.readPrivateKey;
import static org.hyperledger.fabric.gateway.Identities.readX509Certificate;
public class TestNetwork {
public static void main(String[] args) throws IOException, CertificateException, InvalidKeyException {
// Load an existing wallet holding identities used to access the network.
Path walletDirectory = Paths.get("wallet");
System.out.println(walletDirectory.toAbsolutePath());
Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);
# fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem
X509Certificate certificate = readX509Certificate("-----BEGIN CERTIFICATE-----\n" +
"MIICKTCCAdCgAwIBAgIQY0JK/oF6qn3C4o9JOoLJ0jAKBggqhkjOPQQDAjBzMQsw\n" +
"CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy\n" +
"YW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu\n" +
"b3JnMS5leGFtcGxlLmNvbTAeFw0yMjAzMTUwMjQyMDBaFw0zMjAzMTIwMjQyMDBa\n" +
"MGwxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T\n" +
"YW4gRnJhbmNpc2NvMQ8wDQYDVQQLEwZjbGllbnQxHzAdBgNVBAMMFlVzZXIxQG9y\n" +
"ZzEuZXhhbXBsZS5jb20wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAATdUrVekTmp\n" +
"P0PKxK4ORVvKWPXd4tudA+en7pYnvZGcGYE3gf3u0QG3HaNDt8h43xevbzyDdfCB\n" +
"cuR1dQcnUnKKo00wSzAOBgNVHQ8BAf8EBAMCB4AwDAYDVR0TAQH/BAIwADArBgNV\n" +
"HSMEJDAigCDCYrmFJxMQcoXFUSb7GEvTTDLic4TmW3P1ZjPj+w845jAKBggqhkjO\n" +
"PQQDAgNHADBEAiBN9xgvfNRCzEAIp9pfVN7Rp0DpRo3rOjNlTkhcZNu8rwIgEA4F\n" +
"WhOr+VId8IzW7UzdqDJHUVP8jQlLfzkk75B4t0M=\n" +
"-----END CERTIFICATE-----\n");
# fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk
PrivateKey privateKey = readPrivateKey("-----BEGIN PRIVATE KEY-----\n" +
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgGw26poNrIR5HLMbT\n" +
"q+HdLw0Qjz1JMEn17qw9WPyyMo+hRANCAATdUrVekTmpP0PKxK4ORVvKWPXd4tud\n" +
"A+en7pYnvZGcGYE3gf3u0QG3HaNDt8h43xevbzyDdfCBcuR1dQcnUnKK\n" +
"-----END PRIVATE KEY-----\n");
wallet.put("user1", Identities.newX509Identity("Org1MSP", certificate, privateKey));
System.out.println(walletDirectory.toAbsolutePath());
// Path to a common connection profile describing the network.
Path networkConfigFile = Paths.get("src/main/resources/crypto-config/connection.json");
System.out.println(networkConfigFile.toAbsolutePath());
// Configure the gateway connection used to access the network.
Gateway.Builder builder = Gateway.createBuilder()
.identity(wallet, "user1")
.networkConfig(networkConfigFile);
// Create a gateway connection
try (Gateway gateway = builder.connect()) {
// Obtain a smart contract deployed on the network.
Network network = gateway.getNetwork("mychannel");
Contract contract = network.getContract("basic");
// Evaluate transactions that query state from the ledger.
byte[] queryAllCarsResult = contract.evaluateTransaction("GetAllAssets");
System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
}
}
网友评论