Java Code Examples for com.google.protobuf.ByteString#toByteArray()

The following examples show how to use com.google.protobuf.ByteString#toByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionWrapper.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
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;
    }
}
 
Example 2
Source File: TrieService.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: EncryptionUtil.java    From Android with MIT License 6 votes vote down vote up
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;
}
 
Example 4
Source File: KeyUtil.java    From snowblossom with Apache License 2.0 5 votes vote down vote up
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);
  }

}
 
Example 5
Source File: GlobalSearchProviderIconStore.java    From Android-Remote with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: RpcClientProcessServiceImpl.java    From redtorch with MIT License 5 votes vote down vote up
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;
}
 
Example 7
Source File: Util.java    From jelectrum with MIT License 5 votes vote down vote up
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);
}
 
Example 8
Source File: SmartContractProvider.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
* 在世界状态数据库中获取智能合约包
* 依赖couchDB
   */

  public static byte[] extractStateDBArtifactsFromSCPackage(ISmartContractPackage scPack) throws JulongChainException {
      SmartContractPackage.SmartContractDeploymentSpec sds = scPack.getDepSpec();
      ByteString bytes = sds.getCodePackage();
      return bytes.toByteArray();
  }
 
Example 9
Source File: Snapshot.java    From tikv-client-lib-java with Apache License 2.0 4 votes vote down vote up
public byte[] get(byte[] key) {
  ByteString keyString = ByteString.copyFrom(key);
  ByteString value = get(keyString);
  return value.toByteArray();
}
 
Example 10
Source File: TransactionUtils.java    From tron-wallet-android with Apache License 2.0 4 votes vote down vote up
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;
  }
}
 
Example 11
Source File: StringUtils.java    From Tok-Android with GNU General Public License v3.0 4 votes vote down vote up
public static String byte2Str(ByteString byteString) {
    if (byteString != null) {
        return new String(byteString.toByteArray());
    }
    return "";
}
 
Example 12
Source File: ProtoUtil.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
@Nullable
public static byte[] byteArrayOrNullFromProto(ByteString proto) {
    return proto.isEmpty() ? null : proto.toByteArray();
}
 
Example 13
Source File: LifecycleQueryChaincodeDefinitionsResult.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 14
Source File: PBTransactions.java    From WavesJ with MIT License 4 votes vote down vote up
public static com.wavesplatform.wavesj.ByteString toVanillaByteString(final ByteString bs) {
    return new com.wavesplatform.wavesj.ByteString(bs.toByteArray());
}
 
Example 15
Source File: CodecDataInput.java    From client-java with Apache License 2.0 4 votes vote down vote up
public CodecDataInput(ByteString data) {
  this(data.toByteArray());
}
 
Example 16
Source File: Grib1Index.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Grib1SectionGridDefinition readGds(Grib1IndexProto.Grib1GdsSection proto) {
  ByteString bytes = proto.getGds();
  return new Grib1SectionGridDefinition(bytes.toByteArray());
}
 
Example 17
Source File: Grib2Index.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Grib2SectionGridDefinition readGds(Grib2IndexProto.GribGdsSection proto) {
  ByteString bytes = proto.getGds();
  return new Grib2SectionGridDefinition(bytes.toByteArray());
}
 
Example 18
Source File: ByteStringConverters.java    From OpenYOLO-Android with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] convert(ByteString value) {
    return value.toByteArray();
}
 
Example 19
Source File: Bytes.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public Bytes(ByteString byteString) {
  this(byteString.toByteArray());
}
 
Example 20
Source File: GRPCUtils.java    From reef with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}