- opb-sdk-java
opb-sdk-java (open alliance chain SDK java)
https://mvnrepository.com/artifact/io.github.bianjieai
<dependency>
<groupId>io.github.bianjieai</groupId>
<artifactId>opb-sdk</artifactId>
<version>${version}</version>
</dependency>
从 Asset 页面下载jar包,并手动将jar包上传到私服
KeyManager km=KeyManagerFactory.createDefault();
String mnemonic=km.add();
System.out.println("助记词:"+mnemonic);
System.out.println("地址:"+km.getCurrentKeyInfo().getAddress());
String mnemonic="opera xxx ...";
KeyManager km=KeyManagerFactory.createDefault();
km.recover(mnemonic);
BigInteger privKey=new BigInteger("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec",16);
KeyManager km=KeyManagerFactory.createKeyManger(AlgoEnum.SM2);
km.recover(privKey);
String keystore="-----BEGIN TENDERMINT PRIVATE KEY-----\n"+
"salt: 183EF9B57DEF8EF8C3AD9D21DE672E1B\n"+
"type: sm2\n"+
"kdf: bcrypt\n"+
"\n"+
"cpreEPwi0X3yIdsAIf94fR6s8L1TnDAQd/r4ifID6GmQX5a+4ehMmnTp2JjDpUe5\n"+
"kpgRI7CzF0DjKpPLvY9V9ZSXJFN42LHWscxqQ1E=\n"+
"=nJvd\n"+
"-----END TENDERMINT PRIVATE KEY-----";
InputStream input=new ByteArrayInputStream(keystore.getBytes(StandardCharsets.UTF_8));
KeyManager km=KeyManagerFactory.createDefault();
km.recover(input,"123456");
BigInteger privKey=new BigInteger("3c49175daf981965679bf88d2690e22144424e16c84e9d397ddb58b63603eeec",16);
KeyManager km=KeyManagerFactory.createDefault();
km.recover(privKey);
String keystore=km.export("123456");
System.out.println(keystore);
String mnemonic="opera vivid pride shallow brick crew found resist decade neck expect apple chalk belt sick author know try tank detail tree impact hand best";
KeyManager km=KeyManagerFactory.createDefault();
km.recover(mnemonic);
// projectID: 填你的 projectID
String nodeUri="http://testnet.bianjie.ai:26657";
String grpcAddr="testnet.bianjie.ai:9090";
String chainId="testing";
ClientConfig clientConfig=new ClientConfig(nodeUri,grpcAddr,chainId);
OpbConfig opbConfig=null;
OpbClient client=new OpbClient(clientConfig,opbConfig,km);
assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3",km.getCurrentKeyInfo().getAddress());
String mnemonic="opera vivid pride shallow brick crew found resist decade neck expect apple chalk belt sick author know try tank detail tree impact hand best";
KeyManager km=KeyManagerFactory.createDefault();
km.recover(mnemonic);
// projectID: 填你的 projectID
String nodeUri="https://opbningxia.bsngate.com:18602/api/${projectID}/rpc";
String grpcAddr="opbningxia.bsngate.com:18603";
String chainId="wenchangchain";
ClientConfig clientConfig=new ClientConfig(nodeUri,grpcAddr,chainId);
OpbConfig opbConfig=new OpbConfig(${projectID},${projectKey},km.getCurrentKeyInfo().getAddress()); // 如果没有 projectKey 传null
// 开启 TLS 连接
opbConfig.setRequireTransportSecurity(true);
OpbClient client=new OpbClient(clientConfig,opbConfig,km);
assertEquals("iaa1ytemz2xqq2s73ut3ys8mcd6zca2564a5lfhtm3",km.getCurrentKeyInfo().getAddress());
// tips: 连接大唐链需要修改bench32前缀
km.setHrp("dtc");
// 该 client 类型:opbClient
BaseClient baseClient=client.getBaseClient();
TxEngine txEngine=baseClient.getTxEngine();
// 以 editNft 交易为例构造 txBody
Tx.MsgEditNFT msg=Tx.MsgEditNFT
.newBuilder()
.setDenomId(denomID)
.setId(id)
.setName(name)
.setUri(uri)
.setData(data)
.setSender(baseClient.getCurrentAddr())
.build();
List<GeneratedMessageV3> msgs=Collections.singletonList(msg);
TxOuterClass.TxBody txBody=txEngine.buildTxBody(msgs);
byte[]txBodyBz=txBody.toByteArray();
参见前一下小结,需要先构造 txBody (see previous of this content, construct txBody first)
TxOuterClass.TxBody txBody=txEngine.buildTxBody(msgs);
TxOuterClass.Tx tx=txEngine.signTx(txBody,baseTx,baseClient.queryAccount());
TxOuterClass.AuthInfo authInfo=tx.getAuthInfo();
byte[]authInfoBz=authInfo.toByteArray();
// 使用 txEngine 签名交易
TxOuterClass.Tx tx=txEngine.signTx(txBody,baseTx,baseClient.queryAccount());
byte[]txBz=tx.toByteArray();
// 使用 rpcClient 将签名好的 txBz 直接发送到区块链
RpcClient rpcClient=baseClient.getRpcClient();
ResultTx resultTx=rpcClient.broadcastTx(txBz,BroadcastMode.Commit);
// ---
// 当然也可以自己使用 httpClient 进行发送,将 txBz 进行 base64 编码,并使用 jsonRpc 发送, 代码如下
JsonRpc jsonRpc=JsonRpc.WrapTxBytes(txBytes,"broadcast_tx_commit");
String str=httpUtils.post(rpcUri,JSON.toJSONString(jsonRpc)); // rpcUri: 链uri,第二个参数post 的body
String txHash=client.getBaseClient().buildTxHash(msg,baseTx,account);
完整 Demo 示例详见: NftDemo.java
//「创建NFT/MT类别」为 40 万能量值(等值人民币:0.1元)
BaseTx baseTx=new BaseTx(400000,new Fee("400000","ugas"),BroadcastMode.Commit);
String denomID="denomid"+new Random().nextInt(1000);
String denomName="test_name"+new Random().nextInt(1000);
String schema="no shcema";
IssueDenomRequest req=new IssueDenomRequest()
.setId(denomID)
.setName(denomName)
.setSchema(schema);
ResultTx resultTx=nftClient.issueDenom(req,baseTx);
//「发行NFT/MT」为 40 万能量值(等值人民币:0.1元)
BaseTx baseTx=new BaseTx(400000,new Fee("400000","ugas"),BroadcastMode.Commit);
MintNFTRequest mintReq=new MintNFTRequest()
.setDenom(denomID)
.setId(nftID)
.setName(nftName)
.setUri(uri)
.setData(data)
.setRecipient(km.getAddr());
ResultTx resultTx=nftClient.mintNft(mintReq,baseTx);
//「编辑修改NFT/MT」20 万能量值(等值人民币:0.05元)
BaseTx baseTx=new BaseTx(200000,new Fee("200000","ugas"),BroadcastMode.Commit);
String newName="new_name";
String newUri="http://xx.com";
String newData="new_data";
EditNFTRequest editReq=new EditNFTRequest()
.setDenom(denomID)
.setName(newName)
.setId(nftID)
.setUri(newUri)
.setData(newData);
ResultTx resultTx=nftClient.editNft(editReq,baseTx);
//「销毁NFT/MT」20 万能量值(等值人民币:0.05元)
BaseTx baseTx=new BaseTx(200000,new Fee("200000","ugas"),BroadcastMode.Commit);
BurnNFTRequest burnNFTReq=new BurnNFTRequest()
.setDenom(denomID)
.setId(nftID);
Result resultTx=nftClient.burnNft(burnNFTReq,baseTx);
QueryDenomResp denom=nftClient.queryDenom(denomID);
// next is findAll
List<QueryDenomResp> denoms=nftClient.queryDenoms(null);
QueryOwnerResp owner=nftClient.queryOwner(denomID,km.getAddr());
BaseTx baseTx=new BaseTx(200000,new Fee("300000","stake"),BroadcastMode.Commit);
String class_="denom01";
String id="nft26";
String destChainName="iris-test1";
String relayerChainName="";
String receiver="iaa14u80vaseg99lxej9cvlmfz96xe8mvv6p6g469p";
ResultTx resultTx=tibcClient.nftTransfer(class_,id,receiver,destChainName,relayerChainName,baseTx);
BaseTx baseTx=new BaseTx(200000,new Fee("300000","stake"),BroadcastMode.Commit);
String destChain="iris-test1";
String sourceChain="iris-test2";
long sequence=2;
QueryOuterClass.QueryPacketCommitmentResponse commitment=tibcClient.packetCommitment(destChain,sourceChain,sequence);
Perm.Role role=Perm.Role.BLACKLIST_ADMIN;
ResultTx resultTx=permClient.assignRoles(acc,Collections.singletonList(role),baseTx);
assertNotNull(resultTx.getResult().getHash());
List<Perm.Role>roles=permClient.queryRoles(acc);
permClient.unAssignRoles(acc,Collections.singletonList(role),baseTx);
查看 permClient 下的方法 (see method in perClient.java)
BaseTx baseTx=new BaseTx(200000,new Fee("300000","ugas"),BroadcastMode.Commit);
baseTx.setFeeGranter("FeeGranter 地址");
GrantAllowance(String granter, String grantee, BaseTx baseTx):授予全部额度
GrantAllowance(String granter, String grantee, String denom, String amount, Timestamp timestamp, BaseTx baseTx) :授予指定额度
RevokeAllowance(String granter, String grantee, BaseTx baseTx) :撤销授权
详见 FeeGrantTest.java 按@Order(1,2,3)依次运行