重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“怎么实现JAVA离线签名”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
沧县网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。成都创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
import com.alibaba.fastjson.JSON; import com.google.protobuf.Any; import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import java.util.Map; import org.bouncycastle.util.encoders.Hex; import org.tron.common.crypto.ECKey; import org.tron.common.crypto.Sha256Hash; import org.tron.common.utils.ByteArray; import org.tron.core.exception.CancelException; import org.tron.protos.Contract; import org.tron.protos.Protocol.Block; import org.tron.protos.Protocol.Transaction; import org.tron.walletserver.WalletApi; public class TransactionSignDemo { public static Transaction createTransaction(byte[] from, byte[] to, long amount,long blockTimestamp,long blockHeight,byte[] blockHash) { Transaction.Builder transactionBuilder = Transaction.newBuilder(); //Block newestBlock = WalletApi.getBlock(-1); Transaction.Contract.Builder contractBuilder = Transaction.Contract.newBuilder(); Contract.TransferContract.Builder transferContractBuilder = Contract.TransferContract.newBuilder(); transferContractBuilder.setAmount(amount); ByteString bsTo = ByteString.copyFrom(to); ByteString bsOwner = ByteString.copyFrom(from); transferContractBuilder.setToAddress(bsTo); transferContractBuilder.setOwnerAddress(bsOwner); try { Any any = Any.pack(transferContractBuilder.build()); contractBuilder.setParameter(any); } catch (Exception e) { return null; } contractBuilder.setType(Transaction.Contract.ContractType.TransferContract); transactionBuilder.getRawDataBuilder().addContract(contractBuilder).setTimestamp(System.currentTimeMillis()) .setExpiration(blockTimestamp + 10 * 60 * 60 * 1000); Transaction transaction = transactionBuilder.build(); Transaction refTransaction = setReference(transaction, blockHeight,blockHash); return refTransaction; } public static Transaction setReference(Transaction transaction, long blockHeight,byte[] blockHash) { //long blockHeight = newestBlock.getBlockHeader().getRawData().getNumber(); //byte[] blockHash = getBlockHash(newestBlock).getBytes(); byte[] refBlockNum = ByteArray.fromLong(blockHeight); Transaction.raw rawData = transaction.getRawData().toBuilder() .setRefBlockHash(ByteString.copyFrom(ByteArray.subArray(blockHash, 8, 16))) .setRefBlockBytes(ByteString.copyFrom(ByteArray.subArray(refBlockNum, 6, 8))).build(); return transaction.toBuilder().setRawData(rawData).build(); } public static Sha256Hash getBlockHash(Block block) { return Sha256Hash.of(block.getBlockHeader().getRawData().toByteArray()); } public static String getTransactionHash(Transaction transaction) { String txid = ByteArray.toHexString(Sha256Hash.hash(transaction.getRawData().toByteArray())); return txid; } private static byte[] signTransaction2Byte(byte[] transaction, byte[] privateKey) throws InvalidProtocolBufferException { ECKey ecKey = ECKey.fromPrivate(privateKey); Transaction transaction1 = Transaction.parseFrom(transaction); byte[] rawdata = transaction1.getRawData().toByteArray(); byte[] hash = Sha256Hash.hash(rawdata); byte[] sign = ecKey.sign(hash).toByteArray(); return transaction1.toBuilder().addSignature(ByteString.copyFrom(sign)).build().toByteArray(); } public static void main(String[] args) throws InvalidProtocolBufferException, CancelException { String privateStr = "你的私钥"; byte[] privateBytes = ByteArray.fromHexString(privateStr); ECKey ecKey = ECKey.fromPrivate(privateBytes); byte[] from = ecKey.getAddress(); byte[] to = WalletApi.decodeFromBase58Check("地址"); long amount = 100_000_000L; String blockStr = HttpUtil.get("https://apilist.tronscan.org/api/block/latest"); MapblockInfo=JSON.parseObject(blockStr); System.out.println(blockInfo.toString()); Long blockTimestamp = Long.parseLong(blockInfo.get("timestamp").toString()); Long blockHeight = Long.parseLong(blockInfo.get("number").toString()); byte[] blockHash = Hex.decode(blockInfo.get("hash").toString()); //开始看官方demo改写的blockHashd,但是错的,签名没的问题,但是广播就报错TAPOS_ERROR,说块不在链上。 byte[] blockHash3=Sha256Hash.of(blockInfo.get("hash").toString().getBytes()).getBytes(); System.out.println("===blockHash:"+ByteArray.toHexString(blockHash)); System.out.println("===blockHash3:"+ByteArray.toHexString(blockHash3)); Transaction transaction = createTransaction(from, to, amount, blockTimestamp, blockHeight, blockHash); byte[] transactionBytes = transaction.toByteArray(); byte[] transaction4 = signTransaction2Byte(transactionBytes, privateBytes); System.out.println("transaction4 ::::: " + ByteArray.toHexString(transaction4)); //广播的URL:https://apilist.tronscan.org/api/broadcast } }
“怎么实现JAVA离线签名”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!