下面列出了com.google.protobuf.ByteString#toByteArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Connect.GcmData encodeAESGCM(SupportKeyUril.EcdhExts exts, byte[] ecdhKey, byte[] encodes) {
//ecdhkey extension
ecdhKey = SupportKeyUril.ecdhKeyExtends(exts, ecdhKey);
byte[] ab = "ConnectEncrypted".getBytes();
ByteString iv = ByteString.copyFrom(SupportKeyUril.cdJNISeed());
byte[] ib = iv.toByteArray();
GCMModel gc = AllNativeMethod.cdxtalkEncodeAESGCM(encodes, encodes.length,
ab, ab.length, ecdhKey, ecdhKey.length, ib, ib.length);
ByteString enc = ByteString.copyFrom(gc.encrypt);
ByteString mytag = ByteString.copyFrom(gc.tag);
Connect.GcmData gcmData = Connect.GcmData.newBuilder().setIv(iv).
setAad(ByteString.copyFrom(ab)).setCiphertext(enc).setTag(mytag).build();
return gcmData;
}
private byte[] getAccountStateRootHash(long blockNumber) {
long latestNumber = blockNumber;
byte[] rootHash = null;
try {
BlockWrapper blockWrapper = manager.getBlockByNum(latestNumber);
ByteString value = blockWrapper.getInstance().getBlockHeader().getRawData()
.getAccountStateRoot();
rootHash = value == null ? null : value.toByteArray();
if (Arrays.equals(rootHash, Internal.EMPTY_BYTE_ARRAY)) {
rootHash = Hash.EMPTY_TRIE_HASH;
}
} catch (Exception e) {
logger.error("Get the {} block error.", latestNumber, e);
}
return rootHash;
}
public static byte[] getToAddress(Transaction.Contract contract) {
ByteString to;
try {
Any contractParameter = contract.getParameter();
switch (contract.getType()) {
case TransferAssetContract:
to = contractParameter.unpack(TransferAssetContract.class).getToAddress();
break;
case TransferContract:
to = contractParameter.unpack(TransferContract.class).getToAddress();
break;
case ParticipateAssetIssueContract:
to = contractParameter.unpack(ParticipateAssetIssueContract.class).getToAddress();
break;
// todo add other contract
default:
return null;
}
return to.toByteArray();
} catch (Exception ex) {
logger.error(ex.getMessage());
return null;
}
}
public boolean sendLz4CoreRpc(int targetNodeId, ByteString content, String reqId, RpcId rpcId) {
ByteString contentByteString = ByteString.EMPTY;
long beginTime = System.currentTimeMillis();
try (InputStream in = new ByteArrayInputStream(content.toByteArray()); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); LZ4FrameOutputStream lzOut = new LZ4FrameOutputStream(bOut);) {
final byte[] buffer = new byte[10240];
int n = 0;
while (-1 != (n = in.read(buffer))) {
lzOut.write(buffer, 0, n);
}
lzOut.close();
in.close();
contentByteString = ByteString.copyFrom(bOut.toByteArray());
logger.info("发送RPC记录,目标节点ID:{},请求ID:{},RPC:{},压缩耗时{}ms,原始数据大小{},压缩后数据大小{},压缩率{}", targetNodeId, reqId, rpcId.getValueDescriptor().getName(), System.currentTimeMillis() - beginTime,
content.size(), contentByteString.size(), contentByteString.size() / (double) content.size());
} catch (Exception e) {
logger.error("发送RPC错误,压缩异常,目标节点ID:{},请求ID:{},RPC:{}", targetNodeId, reqId, rpcId.getValueDescriptor().getName(), e);
return false;
}
DataExchangeProtocol.Builder depBuilder = DataExchangeProtocol.newBuilder() //
.setRpcId(rpcId.getNumber()) //
.setReqId(reqId) //
.setContentType(ContentType.COMPRESSED_LZ4) //
.setSourceNodeId(nodeId) //
.setTargetNodeId(targetNodeId) //
.setTimestamp(System.currentTimeMillis()) //
.setContentBytes(contentByteString);
if (!webSocketClientHandler.sendData(depBuilder.build().toByteArray())) {
logger.error("发送RPC错误,目标节点ID:{},请求ID:{},RPC:{}", targetNodeId, reqId, rpcId.getValueDescriptor().getName());
return false;
}
return true;
}
/**
* 在世界状态数据库中获取智能合约包
* 依赖couchDB
*/
public static byte[] extractStateDBArtifactsFromSCPackage(ISmartContractPackage scPack) throws JulongChainException {
SmartContractPackage.SmartContractDeploymentSpec sds = scPack.getDepSpec();
ByteString bytes = sds.getCodePackage();
return bytes.toByteArray();
}
public static ByteString reverse(ByteString in)
{
byte b[]=in.toByteArray();
byte o[]=new byte[b.length];
for(int i=0; i<b.length; i++)
{
o[i]=b[b.length-1-i];
}
return ByteString.copyFrom(o);
}
public void insertProvider(String name, ByteString buf) {
if (buf == null || buf.size() == 0) {
return;
}
byte[] b = buf.toByteArray();
Bitmap icon = BitmapFactory.decodeByteArray(b, 0, b.length);
mProvider.put(name, icon);
}
public static PrivateKey decodePrivateKey(ByteString encoded, String algo)
throws ValidationException
{
try
{
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(encoded.toByteArray());
KeyFactory fact = KeyFactory.getInstance(algo, Globals.getCryptoProviderName());
return fact.generatePrivate(spec);
}
catch(java.security.GeneralSecurityException e)
{
throw new ValidationException("Error decoding key", e);
}
}
public static String byte2Str(ByteString byteString) {
if (byteString != null) {
return new String(byteString.toByteArray());
}
return "";
}
public Bytes(ByteString byteString) {
this(byteString.toByteArray());
}
@Override
public byte[] convert(ByteString value) {
return value.toByteArray();
}
private Grib2SectionGridDefinition readGds(Grib2IndexProto.GribGdsSection proto) {
ByteString bytes = proto.getGds();
return new Grib2SectionGridDefinition(bytes.toByteArray());
}
private Grib1SectionGridDefinition readGds(Grib1IndexProto.Grib1GdsSection proto) {
ByteString bytes = proto.getGds();
return new Grib1SectionGridDefinition(bytes.toByteArray());
}
public CodecDataInput(ByteString data) {
this(data.toByteArray());
}
public static com.wavesplatform.wavesj.ByteString toVanillaByteString(final ByteString bs) {
return new com.wavesplatform.wavesj.ByteString(bs.toByteArray());
}
/**
* The validation parameter bytes that were set when the chaincode was defined.
* @return A validation parameter.
*/
public byte[] getValidationParameter() {
final ByteString payloadBytes = chaincodeDefinition.getValidationParameter();
return payloadBytes == null ? null : payloadBytes.toByteArray();
}
@Nullable
public static byte[] byteArrayOrNullFromProto(ByteString proto) {
return proto.isEmpty() ? null : proto.toByteArray();
}
public byte[] get(byte[] key) {
ByteString keyString = ByteString.copyFrom(key);
ByteString value = get(keyString);
return value.toByteArray();
}
public static byte[] getOwner(Transaction.Contract contract) {
ByteString owner;
try {
switch (contract.getType()) {
case AccountCreateContract:
owner = unpackContract(contract, org.tron.protos.Contract.AccountCreateContract.class).getOwnerAddress();
break;
case TransferContract:
owner = unpackContract(contract, org.tron.protos.Contract.TransferContract.class).getOwnerAddress();
break;
case TransferAssetContract:
owner = unpackContract(contract, org.tron.protos.Contract.TransferAssetContract.class).getOwnerAddress();
break;
case VoteAssetContract:
owner = unpackContract(contract, org.tron.protos.Contract.VoteAssetContract.class).getOwnerAddress();
break;
case VoteWitnessContract:
owner = unpackContract(contract, org.tron.protos.Contract.VoteWitnessContract.class).getOwnerAddress();
break;
case WitnessCreateContract:
owner = unpackContract(contract, org.tron.protos.Contract.WitnessCreateContract.class).getOwnerAddress();
break;
case AssetIssueContract:
owner = unpackContract(contract, org.tron.protos.Contract.AssetIssueContract.class).getOwnerAddress();
break;
case WitnessUpdateContract:
owner = unpackContract(contract, org.tron.protos.Contract.WitnessUpdateContract.class).getOwnerAddress();
break;
case ParticipateAssetIssueContract:
owner = unpackContract(contract, org.tron.protos.Contract.ParticipateAssetIssueContract.class).getOwnerAddress();
break;
case AccountUpdateContract:
owner = unpackContract(contract, org.tron.protos.Contract.AccountUpdateContract.class).getOwnerAddress();
break;
case FreezeBalanceContract:
owner = unpackContract(contract, org.tron.protos.Contract.FreezeBalanceContract.class).getOwnerAddress();
break;
case UnfreezeBalanceContract:
owner = unpackContract(contract, org.tron.protos.Contract.UnfreezeBalanceContract.class).getOwnerAddress();
break;
case UnfreezeAssetContract:
owner = unpackContract(contract, org.tron.protos.Contract.UnfreezeAssetContract.class).getOwnerAddress();
break;
case WithdrawBalanceContract:
owner = unpackContract(contract, org.tron.protos.Contract.WithdrawBalanceContract.class).getOwnerAddress();
break;
case UpdateAssetContract:
owner = unpackContract(contract, org.tron.protos.Contract.UpdateAssetContract.class).getOwnerAddress();
break; // -----
case ProposalCreateContract:
owner = unpackContract(contract, org.tron.protos.Contract.ProposalCreateContract.class).getOwnerAddress();
break;
case ProposalApproveContract:
owner = unpackContract(contract, org.tron.protos.Contract.ProposalApproveContract.class).getOwnerAddress();
break;
case ProposalDeleteContract:
owner = unpackContract(contract, org.tron.protos.Contract.ProposalDeleteContract.class).getOwnerAddress();
break;
case SetAccountIdContract:
owner = unpackContract(contract, org.tron.protos.Contract.SetAccountIdContract.class).getOwnerAddress();
break;
//case CustomContract:
//owner = unpackContract(contract, org.tron.protos.Contract.CustomContract.class).getOwnerAddress();
//break;
case CreateSmartContract:
owner = unpackContract(contract, org.tron.protos.Contract.CreateSmartContract.class).getOwnerAddress();
break;
case TriggerSmartContract:
owner = unpackContract(contract, org.tron.protos.Contract.TriggerSmartContract.class).getOwnerAddress();
break;
//case GetContract:
//owner = unpackContract(contract, org.tron.protos.Contract.GetContract.class).getOwnerAddress();
//break;
case UpdateSettingContract:
owner = unpackContract(contract, org.tron.protos.Contract.UpdateSettingContract.class).getOwnerAddress();
break;
case ExchangeCreateContract:
owner = unpackContract(contract, org.tron.protos.Contract.ExchangeCreateContract.class).getOwnerAddress();
break;
case ExchangeInjectContract:
owner = unpackContract(contract, org.tron.protos.Contract.ExchangeInjectContract.class).getOwnerAddress();
break;
case ExchangeWithdrawContract:
owner = unpackContract(contract, org.tron.protos.Contract.ExchangeWithdrawContract.class).getOwnerAddress();
break;
case ExchangeTransactionContract:
owner = unpackContract(contract, org.tron.protos.Contract.ExchangeTransactionContract.class).getOwnerAddress();
break;
default:
return null;
}
return owner.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
/**
* Converts ByteString to byte array.
* @param bs ByteString
* @return byte array or null if not present
*/
public static byte[] toByteArray(final ByteString bs) {
return bs == null || bs.isEmpty() ? null : bs.toByteArray();
}