service
public interface CitaService {
/**
* 获取cita前置条件
* @param
* @return
*/
CiTaEntity getCita() throws IOException;
/**
* 交易存证
* @param data
* @return
* @throws CommonException
*/
String storeRecord(String data) throws CommonException, IOException, InterruptedException;
/**
* 查询交易内容
* @param hash
* @return
*/
String selectDcContent(String hash) throws IOException;
}
impl
@Slf4j
@Service
public class CitaServiceImpl implements CitaService {
private static Transaction.CryptoTx cryptoTx;
public CitaServiceImpl() throws IOException {
}
@Override
public CiTaEntity getCita() throws IOException {
CiTaEntity ciTaEntity=new CiTaEntity();
ciTaEntity.setPrivateKey("cita生成的私钥");
ciTaEntity.setService(CITAj.build(new HttpService("服务器地址")));
//ciTaEntity.setService(CITAj.build(new HttpService("https://testnet.citahub.com")));
ciTaEntity.setAppMetaData(ciTaEntity.getService().appMetaData(DefaultBlockParameterName.PENDING).send());
String chainIdHex = ciTaEntity.getAppMetaData().getAppMetaDataResult().getChainIdV1();
ciTaEntity.setChainId(new BigInteger(chainIdHex.substring(2), 16));
//ciTaEntity.getAppMetaData().getAppMetaDataResult().getVersion()
ciTaEntity.setVersion(2);
long currentHeight = ciTaEntity.getService().appBlockNumber().send().getBlockNumber().longValue();
ciTaEntity.setValidUntilBlock(currentHeight + 80);
Random random = new Random(System.currentTimeMillis());
ciTaEntity.setNonce(String.valueOf(Math.abs(random.nextLong())));
ciTaEntity.setQuota(1000000);
return ciTaEntity;
}
@Override
public String storeRecord(String data) throws CommonException, IOException, InterruptedException {
CiTaEntity ciTaEntity=getCita();
String sampleDataToStore = stringToHexString(data);
CITAjSystemContract sysContract = new CITAjSystemContract(ciTaEntity.getService());
Transaction txToStoreData = sysContract
.constructStoreTransaction(sampleDataToStore, ciTaEntity.getVersion(), ciTaEntity.getChainId());
String signedTx = txToStoreData.sign(ciTaEntity.getPrivateKey(), cryptoTx, false);
AppSendTransaction appSendTransaction
= ciTaEntity.getService().appSendRawTransaction(signedTx).send();
String txHash = appSendTransaction.getSendTransactionResult().getHash();
// System.out.println("Wait 6s for transaction written into block.");
// TimeUnit.SECONDS.sleep(6);
// TransactionReceipt txReceipt = ciTaEntity.getService().appGetTransactionReceipt(txHash).send().getTransactionReceipt();
// String contractAddr = txReceipt.getContractAddress();
// //对交易签名并且发送
// List<Type> inputParameters = Arrays.asList(new Uint(BigInteger.valueOf(4l)));
// String funcData = CITASystemContract.encodeFunction("set", inputParameters);
// txToStoreData = new Transaction(contractAddr, ciTaEntity.getNonce(), ciTaEntity.getQuota(), ciTaEntity.getValidUntilBlock(), ciTaEntity.getVersion(), ciTaEntity.getChainId(), "0", funcData);
// signedTx = txToStoreData.sign(ciTaEntity.getPrivateKey());
// txHash = ciTaEntity.getService().appSendRawTransaction(signedTx).send().getSendTransactionResult().getHash();
return txHash;
}
@Override
public String selectDcContent(String hash) throws IOException {
CITAj service=CITAj.build(new HttpService("服务器地址"));
String serializedTx = service
.appGetTransactionByHash(hash).send()
.getTransaction().getContent();
System.out.println(
"Unverified transaction(Content transaction): \n"
+ serializedTx);
byte[] byteSerializedTx = ConvertStrByte
.hexStringToBytes(Numeric.cleanHexPrefix(serializedTx));
Blockchain.UnverifiedTransaction unverifiedTransaction
= Blockchain.UnverifiedTransaction
.parseFrom(byteSerializedTx);
System.out.println(unverifiedTransaction);
String dataStored = unverifiedTransaction.getTransaction().getData().toStringUtf8();
return dataStored;
}
public static String stringToHexString(String strPart){
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder();
byte[] bs = strPart.getBytes();
int bit;
for (int i = 0;
i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return sb.toString().trim();
}
}
工具类
@Data
public class CiTaEntity {
/**
* 私钥
*/
private String privateKey;
/**
* citaService
*/
private CITAj service;
/**
*获取数据对象
*/
private AppMetaData appMetaData;
/**
* chain Id
*/
private BigInteger chainId;
/**
* 版本
*/
private int version;
/**
* 当前块高度
*/
private long validUntilBlock;
/**
*随机数
*/
private String nonce;
/**
*交易执行费用
*/
private long quota;
public String getPrivateKey() {
return privateKey;
}
public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
}
public CITAj getService() {
return service;
}
public void setService(CITAj service) {
this.service = service;
}
public AppMetaData getAppMetaData() {
return appMetaData;
}
public void setAppMetaData(AppMetaData appMetaData) {
this.appMetaData = appMetaData;
}
public BigInteger getChainId() {
return chainId;
}
public void setChainId(BigInteger chainId) {
this.chainId = chainId;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public long getValidUntilBlock() {
return validUntilBlock;
}
public void setValidUntilBlock(long validUntilBlock) {
this.validUntilBlock = validUntilBlock;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public long getQuota() {
return quota;
}
public void setQuota(long quota) {
this.quota = quota;
}
}
依赖
<!-- 引入cita依赖 -->
<dependency>
<groupId>com.citahub.cita</groupId>
<artifactId>core</artifactId>
<version>20.2.0</version>
</dependency>