citahub / cita-sdk-java Goto Github PK
View Code? Open in Web Editor NEWJava library for working with Smart Contract and integrating with clients on CITA
License: Other
Java library for working with Smart Contract and integrating with clients on CITA
License: Other
I think we'd better not hard-code this kind of information in the codebase, even in the test.
I think it's better to move to a config file or read from the environment.
个别情况下,用javaSDK生成的sm2算法的秘钥,与在cita-cli 中结果对不上。
具体测试数据请与我联系
Transaction tx = Transaction.createFunctionCallTransaction(
contractAddress,
nonce,
this.quota,
this.validUntilBlock,
VERSION,
CHAINID,
value,
functionCallData);
String rawTx = tx.sign(privateKey, isEd25519AndBlake2b,false);
When the contractAddress has 0x prefix, something wrong, the tx can't send to the contractAddress.
After remove 0x in contractAddress, it's ok.
使用 DynamicArray(new Utf8String())传输string[]时,编码后结果没有看到偏移量,合约里面读不出来
SDK 部署合约 ABI 未能提供针对 SM2 链的部署方式
In org/web3j/tx/CitaTransactionManager.java
getNonce should use LATEST rather than DefaultBlockParameterName.PENDING.
Because there are no PENDING in CITA.
org/nervos/appchain/protocol/core/methods/response/AppMetaData.java
public void setChainId(int chainId) {
this.chainId = chainId;
}
public int getChainId() {
if (this.version == 0) {
return this.chainId;
} else if (this.version == 1) {
return Integer.parseInt(this.chainIdV1);
} else {
throw new IllegalArgumentException("version number can only be 1 or 2");
}
}
old chainId is u32
new chainIdV1 is u256
So I think getChainId should return BigInteger
or String?
Same to argument of setChainId
sdk.appGetTransactionByHash("0xabc...").send().getTransaction().decodeContent();
com.cryptape.cita.utils.TransactionUtil#decodeContent
中 public static Transaction
decodeContent(String content) throws InvalidProtocolBufferException {
Blockchain.UnverifiedTransaction unverifiedTx
= Blockchain.UnverifiedTransaction.parseFrom(
ConvertStrByte.hexStringToBytes(Numeric.cleanHexPrefix(content)));
Blockchain.Transaction blockChainTx = unverifiedTx.getTransaction();
int version = blockChainTx.getVersion();
String nonce = blockChainTx.getNonce();
long quota = blockChainTx.getQuota();
long validUntilBlock = blockChainTx.getValidUntilBlock();
String data = bytesToHexString(blockChainTx.getData());
String value = bytesToHexString(blockChainTx.getValue());
......
由于 value 不为零, String value = bytesToHexString(blockChainTx.getValue());
返回的 value 不是 0x 开头的, 后面转换的时候当 10 进制转换报错了
The statement txToCallContract
in Line 2 does not match the references txToDeployContract.sign
Line 3 :
1. //construct transaction
2. Transaction txToCallContract = Transction.createFunctionCallTransaction(to, nonce, quota, valid_until_block, version, chainId, value, init);
3. String signedTx = txToDeployContract.sign(this.config.getPrivateKey(), false, false);
4. AppSendTransaction appSendTx = service.sendRawTransaction(signedTx);
代码下载位置:https://github.com/cryptape/cita-sdk-java
编译工具:idea 2019.2
Java版本:1.8
问题截屏:
完成组管理合约接口
https://docs.citahub.com/zh-CN/cita/system-contract-interface/group-mng 中标准的方法:newGroup,deleteGroup,updateGroupName,addAccounts,deleteAccounts,checkScope,queryGroups。
Welcome to Gradle 5.4.1!
Here are the highlights of this release:
For more details see https://docs.gradle.org/5.4.1/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
Where:
Build file '/cita-sdk-java/console/build.gradle' line: 12
What went wrong:
A problem occurred evaluating project ':console'.
Could not get unknown property 'classesDir' for test classes of type org.gradle.api.internal.tasks.DefaultSourceSetOutput.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 27s
LTS: JDK8, JDK11
latest: JDK 14
to catch up with CITA v0.25.0 https://github.com/cryptape/cita/releases/tag/v0.25.0
合约地址:6060604052341561000f57600080fd5b60408051908101604052600b81527f68656c6c6f20776f726c640000000000000000000000000000000000000000006020820152600090805161005692916020019061005c565b506100f7565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009d57805160ff19168380011785556100ca565b828001600101855582156100ca579182015b828111156100ca5782518255916020019190600101906100af565b506100d69291506100da565b5090565b6100f491905b808211156100d657600081556001016100e0565b90565b6102c3806101066000396000f30060606040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166320965255811461005057806393a09352146100da575b600080fd5b341561005b57600080fd5b61006361012d565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561009f578082015183820152602001610087565b50505050905090810190601f1680156100cc5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156100e557600080fd5b61012b60046024813581810190830135806020601f820181900481020160405190810160405281815292919060208401838380828437509496506101d695505050505050565b005b6101356101ed565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101cb5780601f106101a0576101008083540402835291602001916101cb565b820191906000526020600020905b8154815290600101906020018083116101ae57829003601f168201915b505050505090505b90565b60008180516101e99291602001906101ff565b5050565b60206040519081016040526000815290565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061024057805160ff191683800117855561026d565b8280016001018555821561026d579182015b8281111561026d578251825591602001919060010190610252565b5061027992915061027d565b5090565b6101d391905b8082111561027957600081556001016102835600a165627a7a72305820086c5bf2a74080392d819fbd84ddb063f16da81bfcfa3e0d9d095fc2b99588220029
网络地址:http://121.196.200.225:1337
from:0x9866174664d0eec7988fcc223ee14f6398cd9448
to:0x5f78ee58f5900045d9149c051e12c3b401e8c4df
amount:1
txHash:0xda58810ab925c40d1eea2d60813e66fc5fccab0acda59973dc8e2b3e117fc75d
from地址是我随意生成的地址,肯定没有余额的,为什么这笔交易能直接成功?
ConvertStrByte.stringToHexString(content);
这个方法不支持中文字符串呀;
https://github.com/citahub/cita-sdk-java/blob/develop/tests/src/test/java/com/citahub/cita/tests/StoreRecordTest.java
使用citaj 调用合约查询接口,
接口返回hex数据
通过java解析把数据解析成UTF-8普通字符串时,出现乱码
查询方法1结果:
0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008f62616e6b5472616e736665725061797c2b7c307c2b7c307c2b7c3130303030307c2b7c3130303030307c2b7c307c2b7c3130303030307c2b7c50592d32643062663662662d313937302d343330362d383161382d3361343334663463356534377c2b7c7c2b7c323031392d30382d32382031353a32313a30377c2b7c317c2b7ce8aea2e58d95e5b7b2e7a1aee8aea40000000000000000000000000000000000
字符串结果1:
�bankTransferPay|+|0|+|0|+|100000|+|100000|+|0|+|100000|+|PY-2d0bf6bf-1970-4306-81a8-3a434f4c5e47|+||+|2019-08-28 15:21:07|+|1|+|订单已确认
查询方法2结果:
0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000457c2b7c7c2b7c7c2b7c7c2b7c323031392d30382d32382031353a32353a35327c2b7c7c2b7c7c2b7c7c2b7c323031392d30382d32382031353a32353a30387c2b7c357c2b7c000000000000000000000000000000000000000000000000000000
字符串结果2:
E|+||+||+||+|2019-08-28 15:25:52|+||+||+||+|2019-08-28 15:25:08|+|5|+|
JAVA 代码:
//hex转字符串
//16进制转换成为string类型字符串
private static String hexStringToString(String s) {
if (s == null || s.equals("")) {
return null;
}
s = s.replace(" ", "");
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
if (s.substring(i * 2, i * 2 + 2).equals("00")) {
continue;
}
baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
}
}
try {
s = new String(baKeyword, "UTF-8");
new String();
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
//把前导0的字符串转为正常字符串
private static String hexStringToStringOne(String hexStr) {
String bigInt = new BigInteger(hexStr, 16).toString(16);
return bigInt;
}
//链上取出的十六进制字符串转为普通字符串
private String hexToString(String hexStr) {
String BigIntToString = hexStringToStringOne(hexStr.substring(2));
String orderInfo = hexStringToString(BigIntToString).replace("\u0000", "").replace("\u0001", "").replace("\t", "");
return orderInfo;
}
//调用合约查询方法获取结果
public String callContractFunc() {
Call call = new Call('from', contractAddress, callData);
//该结果有乱码(多余字符)
String hexStr = citaJService.appCall(call, DefaultBlockParameterName.LATEST).send().getValue();
return hexToString(hexStr);
}
另外,采用Cita JS sdk 调用合约查询接口,会直接返回正确的字符串数据
代码如下:
(async () => {
var contractInstance = new citaSDK.base.Contract(abi, contractAddress);
var queryRes = await contractInstance.methods['myQueryFunc'](params).call();
//打印查询结果,该结果正确,不包含乱码(多余字符)
console.log('query res', queryRes);
})();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.