APIs
Hyperledger Fabric提供了许多api来支持使用多种编程语言发智能契约(chaincode)。智能合约APIs可用于Go, Node.js和Java。
- Go contract-api.
- Node.js contract API and Node.js contract API documentation.
- Java contract API and Java contract API documentation.
有关智能合约的概念及编程模型的详细解释,请参阅Chaincode Tutorials section of the Hyperledger Fabric documentation.
有关商业票据和fabcar的Java链码样本,请参阅fabric-samples repository
Java 链码 maven:
<dependency>
<groupId>org.hyperledger.fabric-chaincode-java</groupId>
<artifactId>fabric-chaincode-shim</artifactId>
<version>VERSION</version>
</dependency>
SDKs
Fabric Gateway SDK允许应用程序与Fabric区块链网络进行交互。它提供了一个简单的API,可以用最少的代码向分类帐提交事务或查询分类帐的内容。
用法
下面展示了如何使用实例化的smart contract (fabcar示例)连接到fabric网络、提交事务和查询分类帐状态的完整代码示例。
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeoutException;
import org.hyperledger.fabric.gateway.Contract;
import org.hyperledger.fabric.gateway.ContractException;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
class Sample {
public static void main(String[] args) throws IOException {
// Load an existing wallet holding identities used to access the network.
Path walletDirectory = Paths.get("wallet");
Wallet wallet = Wallets.newFileSystemWallet(walletDirectory);
// Path to a common connection profile describing the network.
Path networkConfigFile = Paths.get("connection.json");
// 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("fabcar");
// Submit transactions that store state to the ledger.
byte[] createCarResult = contract.createTransaction("createCar")
.submit("CAR10", "VW", "Polo", "Grey", "Mary");
System.out.println(new String(createCarResult, StandardCharsets.UTF_8));
// Evaluate transactions that query state from the ledger.
byte[] queryAllCarsResult = contract.evaluateTransaction("queryAllCars");
System.out.println(new String(queryAllCarsResult, StandardCharsets.UTF_8));
} catch (ContractException | TimeoutException | InterruptedException e) {
e.printStackTrace();
}
}
}
API文档
Maven
<dependency>
<groupId>org.hyperledger.fabric</groupId>
<artifactId>fabric-gateway-java</artifactId>
<version>2.0.0</version>
</dependency>
下载二进制文件和docker文件可以通过scripts/bootstrap.sh,你也可以下载脚本并在本地执行:
# Fetch bootstrap.sh from fabric repository using
curl -sS https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh -o ./scripts/bootstrap.sh
# Change file mode to executable
chmod +x ./scripts/bootstrap.sh
# Download binaries and docker images
./scripts/bootstrap.sh [version] [ca version] [thirdparty_version]