org.fisco.bcos.web3j.utils.ByteUtil Java Examples

The following examples show how to use org.fisco.bcos.web3j.utils.ByteUtil. 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: SolidityType.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] encodeList(List l) {
    byte[][] elems;
    if (elementType.isDynamicType()) {
        elems = new byte[l.size() * 2 + 1][];
        elems[0] = IntType.encodeInt(l.size());
        int offset = l.size() * 32;
        for (int i = 0; i < l.size(); i++) {
            elems[i + 1] = IntType.encodeInt(offset);
            byte[] encoded = elementType.encode(l.get(i));
            elems[l.size() + i + 1] = encoded;
            offset += 32 * ((encoded.length - 1) / 32 + 1);
        }
    } else {
        elems = new byte[l.size() + 1][];
        elems[0] = IntType.encodeInt(l.size());

        for (int i = 0; i < l.size(); i++) {
            elems[i + 1] = elementType.encode(l.get(i));
        }
    }
    return ByteUtil.merge(elems);
}
 
Example #2
Source File: SignService.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * add sign.
 * @param req parameter
 */
public String sign(ReqEncodeInfoVo req) throws BaseException {
    String signUserId = req.getSignUserId();
    log.info("start sign. signUserId:{}", signUserId);
    Instant startTimeDB = Instant.now();
    // check exist
    UserInfoPo userRow = userService.findBySignUserId(signUserId);
   log.debug("end query db time: {}", Duration.between(startTimeDB, Instant.now()).toMillis());
    // check user name not exist.
    if (Objects.isNull(userRow)) {
        log.warn("fail sign, user not exists. signUserId:{}", signUserId);
        throw new BaseException(CodeMessageEnums.USER_NOT_EXISTS);
    }
    int encryptType = userRow.getEncryptType();
    // signature
    Credentials credentials = keyPairUtils.create(userRow.getPrivateKey(), encryptType);
    byte[] encodedData;
    try {
        encodedData = ByteUtil.hexStringToBytes(req.getEncodedDataStr());
    } catch (DecoderException e) {
        log.error("hexStringToBytes error: ", e);
        throw new BaseException(CodeMessageEnums.PARAM_ENCODED_DATA_INVALID);

    }
    Instant startTime = Instant.now();
    log.info("start sign. startTime:{}", startTime.toEpochMilli());
    // sign message by type
    SignatureData signatureData = signUtils.signMessageByType(
            encodedData, credentials.getEcKeyPair(), encryptType);
    log.info("end sign duration:{}", Duration.between(startTime, Instant.now()).toMillis());
    String signDataStr = CommonUtils.signatureDataToStringByType(signatureData, encryptType);
    log.info("end sign. signUserId:{}", signUserId);
    return signDataStr;
}
 
Example #3
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encodeList(List l) {
    if (l.size() != size)
        throw new RuntimeException(
                "List size (" + l.size() + ") != " + size + " for type " + getName());
    byte[][] elems = new byte[size][];
    for (int i = 0; i < l.size(); i++) {
        elems[i] = elementType.encode(l.get(i));
    }
    return ByteUtil.merge(elems);
}
 
Example #4
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode(Object value) {
    byte[] bb;
    if (value instanceof byte[]) {
        bb = (byte[]) value;
    } else if (value instanceof String) {
        bb = ((String) value).getBytes();
    } else {
        throw new RuntimeException("byte[] or String value is expected for type 'bytes'");
    }
    byte[] ret = new byte[((bb.length - 1) / 32 + 1) * 32]; // padding 32 bytes
    System.arraycopy(bb, 0, ret, 0, bb.length);

    return ByteUtil.merge(IntType.encodeInt(bb.length), ret);
}
 
Example #5
Source File: SolidityType.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encode(Object value) {
    if (!(value instanceof byte[]))
        throw new RuntimeException("Expected byte[] value for FunctionType");
    if (((byte[]) value).length != 24)
        throw new RuntimeException("Expected byte[24] for FunctionType");
    return super.encode(ByteUtil.merge((byte[]) value, new byte[8]));
}
 
Example #6
Source File: SignDataTest.java    From WeBASE-Front with Apache License 2.0 4 votes vote down vote up
public String getLocalSignedData(String pri) {
    Credentials credentials = GenCredential.create(pri);
    Sign.SignatureData signatureData = Sign.getSignInterface().signMessage(
            ByteUtil.hexStringToBytes(Numeric.toHexString(rawData.getBytes())), credentials.getEcKeyPair());
    return CommonUtils.signatureDataToString(signatureData);
}
 
Example #7
Source File: SolidityType.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@Override
public Object decode(byte[] encoded, int offset) {
    BigInteger bi = (BigInteger) super.decode(encoded, offset);
    return ByteUtil.bigIntegerToBytes(bi, 20);
}
 
Example #8
Source File: SolidityType.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static byte[] encodeInt(BigInteger bigInt) {
    return ByteUtil.bigIntegerToBytesSigned(bigInt, 32);
}
 
Example #9
Source File: SolidityType.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static byte[] encodeInt(BigInteger bigInt) {
    if (bigInt.signum() == -1) {
        throw new RuntimeException("Wrong value for uint type: " + bigInt);
    }
    return ByteUtil.bigIntegerToBytes(bigInt, 32);
}
 
Example #10
Source File: Abi.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public byte[] encode(Object... args) {
    return ByteUtil.merge(encodeSignature(), encodeArguments(args));
}