Java Code Examples for org.fisco.bcos.web3j.utils.Numeric#toHexString()

The following examples show how to use org.fisco.bcos.web3j.utils.Numeric#toHexString() . 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: TransactionEncoderTest.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testGMSignMessage() {
    EncryptType encryptType = new EncryptType(1);

    Credentials credentials =
            GenCredential.create(
                    "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    System.out.println(credentials.getEcKeyPair().getPublicKey().toString(16));

    Credentials credentials1 =
            Credentials.create(
                    "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    System.out.println(credentials1.getEcKeyPair().getPublicKey().toString(16));

    byte[] signedMessage =
            TransactionEncoder.signMessage(createContractTransaction(), credentials);
    String hexMessage = Numeric.toHexString(signedMessage);
    assertThat(
            hexMessage,
            is(
                    "0xf8948201f4010a8201f5800a850000000000b8408234c544a9f3ce3b401a92cc7175602ce2a1e29b1ec135381c7d2a9e8f78f3edc9c06ee55252857c9a4560cb39e9d70d40f4331cace4d2b3121b967fa7a829f0a03d8627050f6688f27e2b5b89c9c141d3a48603029849e088486d1c7ea079ea7fa037024ed35d2c099d7eb68fb133e57735b03605ec32ded39ab305c3b56e5d99e7"));
}
 
Example 2
Source File: TransactionDecoderTest.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoding() throws Exception {
    BigInteger gasPrice = BigInteger.ONE;
    BigInteger gasLimit = BigInteger.TEN;
    String to = "0x0add5355";
    BigInteger value = BigInteger.valueOf(Long.MAX_VALUE);
    BigInteger randomid = new BigInteger("500");
    BigInteger blockLimit = new BigInteger("501");
    RawTransaction rawTransaction =
            RawTransaction.createContractTransaction(
                    randomid, gasPrice, gasLimit, blockLimit, value, "0x0000000000");
    byte[] encodedMessage = TransactionEncoder.encode(rawTransaction);
    String hexMessage = Numeric.toHexString(encodedMessage);

    RawTransaction result = TransactionDecoder.decode(hexMessage);
    assertNotNull(result);
    assertEquals(blockLimit, result.getBlockLimit());
    assertEquals(gasPrice, result.getGasPrice());
    assertEquals(gasLimit, result.getGasLimit());
    assertEquals("0x", result.getTo());
    assertEquals(value, result.getValue());
    assertEquals("0000000000", result.getData());
}
 
Example 3
Source File: RawTransactionManager.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public SendTransaction signAndSend(
        RawTransaction rawTransaction, TransactionSucCallback callback) throws IOException {

    byte[] signedMessage;

    if (chainId > ChainId.NONE) {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
    } else {
        signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    }

    String hexValue = Numeric.toHexString(signedMessage);
    Request<?, SendTransaction> request = web3j.sendRawTransaction(hexValue);
    request.setNeedTransCallback(true);
    request.setTransactionSucCallback(callback);
    request.sendOnly();

    return null;
}
 
Example 4
Source File: RawTransactionManager.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public SendTransaction signAndSend(RawTransaction rawTransaction) throws IOException {

        byte[] signedMessage;

        if (chainId > ChainId.NONE) {
            signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
        } else {
            signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        }

        String hexValue = Numeric.toHexString(signedMessage);
        SendTransaction sendTransaction = web3j.sendRawTransaction(hexValue).send();
        if (sendTransaction != null && !sendTransaction.hasError()) {
            String txHashLocal = Hash.sha3(hexValue);
            String txHashRemote = sendTransaction.getTransactionHash();
            if (!txHashVerifier.verify(txHashLocal, txHashRemote)) {
                throw new TxHashMismatchException(txHashLocal, txHashRemote);
            }
        }
        return sendTransaction;
    }
 
Example 5
Source File: ContractTypeUtil.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
static String decodeBytes(BytesType bytesType) {
    byte[] value = bytesType.getValue();
    int length = value.length;
    int mod = length % MAX_BYTE_LENGTH;

    byte[] dest;
    if (mod != 0) {
        int padding = MAX_BYTE_LENGTH - mod;
        dest = new byte[length + padding];
        System.arraycopy(value, 0, dest, 0, length);
    } else {
        dest = value;
    }
    return Numeric.toHexString(dest);
}
 
Example 6
Source File: SM3PasswordEncoder.java    From WeBASE-Node-Manager with Apache License 2.0 5 votes vote down vote up
@Override
public String encode(CharSequence rawPassword) {
    SM3Digest sm3Digest = new SM3Digest();
    byte[] pwdInput = rawPassword.toString().getBytes();
    byte[] hashed = sm3Digest.hash(pwdInput);
    return Numeric.toHexString(hashed);
}
 
Example 7
Source File: Tools.java    From evidenceSample with Apache License 2.0 5 votes vote down vote up
static public  String  signatureDataToString(Sign.SignatureData signatureData)
{
    byte[] byte_3 = new byte[1+signatureData.getR().length+signatureData.getS().length];
    byte_3[0] = signatureData.getV();
    System.arraycopy(signatureData.getR(), 0, byte_3, 1, signatureData.getR().length);
    System.arraycopy(signatureData.getS(), 0, byte_3, signatureData.getR().length+1, signatureData.getS().length);
    return  Numeric.toHexString(byte_3,0,byte_3.length,false);
}
 
Example 8
Source File: CommonUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * signatureDataToString by type
 * @param signatureData
 * @param encryptType
 * @return
 */
public static String signatureDataToStringByType(SignatureData signatureData, int encryptType) {
    byte[] byteArr;
    if(encryptType == 1) {
        byteArr = sigData2ByteArrGuomi(signatureData);
    } else {
        byteArr = sigData2ByteArrECDSA(signatureData);
    }
    return Numeric.toHexString(byteArr, 0, byteArr.length, false);
}
 
Example 9
Source File: MerkleProofUtility.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Verify transaction receipt merkle proof
 *
 * @param receiptRoot
 * @param transactionReceipt
 * @param receiptProof
 * @return
 */
public static boolean verifyTransactionReceipt(
        String receiptRoot,
        TransactionReceipt transactionReceipt,
        List<MerkleProofUnit> receiptProof) {

    if (!transactionReceipt.getGasUsedRaw().startsWith("0x")) {
        transactionReceipt.setGasUsed("0x" + transactionReceipt.getGasUsed().toString(16));
    }

    // transaction index
    byte[] byteIndex =
            RlpEncoder.encode(RlpString.create(transactionReceipt.getTransactionIndex()));

    String receiptRlp = ReceiptEncoder.encode(transactionReceipt);
    String rlpHash = Hash.sha3(receiptRlp);
    String input = Numeric.toHexString(byteIndex) + rlpHash.substring(2);

    String proof = Merkle.calculateMerkleRoot(receiptProof, input);

    logger.debug(
            " transaction hash: {}, transactionReceipt: {}, receiptProof: {}, receiptRoot: {}, proof: {}",
            transactionReceipt.getTransactionHash(),
            transactionReceipt,
            receiptProof,
            receiptRoot,
            proof);

    return proof.equals(receiptRoot);
}
 
Example 10
Source File: MerkleProofUtility.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Verify transaction receipt merkle proof
 *
 * @param receiptRoot
 * @param receiptAndProof
 * @return
 */
public static boolean verifyTransactionReceipt(
        String receiptRoot, TransactionReceiptWithProof.ReceiptAndProof receiptAndProof) {

    TransactionReceipt transactionReceipt = receiptAndProof.getTransactionReceipt();

    // transaction index
    byte[] byteIndex =
            RlpEncoder.encode(RlpString.create(transactionReceipt.getTransactionIndex()));

    if (!transactionReceipt.getGasUsedRaw().startsWith("0x")) {
        transactionReceipt.setGasUsed("0x" + transactionReceipt.getGasUsed().toString(16));
    }

    String receiptRlp = ReceiptEncoder.encode(transactionReceipt);
    String rlpHash = Hash.sha3(receiptRlp);
    String input = Numeric.toHexString(byteIndex) + rlpHash.substring(2);

    String proof = Merkle.calculateMerkleRoot(receiptAndProof.getReceiptProof(), input);

    logger.debug(
            " transaction hash: {}, receipt index: {}, root: {}, proof: {}, receipt: {}",
            transactionReceipt.getTransactionHash(),
            transactionReceipt.getTransactionIndex(),
            receiptRoot,
            proof,
            receiptAndProof.getTransactionReceipt());

    return proof.equals(receiptRoot);
}
 
Example 11
Source File: RlpString.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public String asString() {
    return Numeric.toHexString(value);
}
 
Example 12
Source File: NameHash.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String nameHash(String ensName) {
    String normalisedEnsName = normalise(ensName);
    return Numeric.toHexString(nameHash(normalisedEnsName.split("\\.")));
}
 
Example 13
Source File: SHA3Digest.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
@Override
public String hash(String hexInput) {
    byte[] bytes = Numeric.hexStringToByteArray(hexInput);
    byte[] result = hash(bytes);
    return Numeric.toHexString(result);
}
 
Example 14
Source File: TopicTools.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String bytesToTopic(byte[] b) {
    byte[] hash = Hash.sha3(b);
    return Numeric.toHexString(hash);
}
 
Example 15
Source File: TopicTools.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String stringToTopic(String s) {
    byte[] hash = Hash.sha3(s.getBytes());
    return Numeric.toHexString(hash);
}
 
Example 16
Source File: TransactionResource.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public TransactionReceiptWithProof getTransactionReceiptWithProof(
        String transactionHash, String rootHash) throws IOException {
    TransactionReceiptWithProof transactionReceiptWithProof =
            web3j.getTransactionReceiptByHashWithProof(transactionHash).send();

    if (transactionReceiptWithProof.getTransactionReceiptWithProof() == null) {
        return null;
    }
    TransactionReceipt transactionReceipt =
            transactionReceiptWithProof
                    .getTransactionReceiptWithProof()
                    .getTransactionReceipt();
    logger.debug("Receipt {}", transactionReceipt.toString());

    // transaction index
    String index = transactionReceipt.getTransactionIndexRaw();
    BigInteger indexValue = Numeric.toBigInt(index);
    byte[] byteIndex = RlpEncoder.encode(RlpString.create(indexValue));

    String receiptRlp = ReceiptEncoder.encode(transactionReceipt);
    logger.debug("ReceiptRlp:{}", receiptRlp);

    String rlpHash = Hash.sha3(receiptRlp);
    logger.debug("ReceiptRlpHash:{}", rlpHash);

    String input = Numeric.toHexString(byteIndex) + rlpHash.substring(2);
    logger.info("ReceiptWithIndex:{}", input);

    String proof =
            Merkle.calculateMerkleRoot(
                    transactionReceiptWithProof
                            .getTransactionReceiptWithProof()
                            .getReceiptProof(),
                    input);

    //        System.out.println("MerkleRoot: " + proof);
    if (!proof.equals(rootHash)) {
        logger.debug("MerkleRoot:{}", proof);
        logger.debug("TransRoot :{}", rootHash);
        return null;
    }
    return transactionReceiptWithProof;
}
 
Example 17
Source File: ReceiptEncoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public static String encode(TransactionReceipt transactionReceipt) {
    List<RlpType> values = asRlpValues(transactionReceipt);
    RlpList rlpList = new RlpList(values);
    byte[] rlpBytes = RlpEncoder.encode(rlpList);
    return Numeric.toHexString(rlpBytes);
}
 
Example 18
Source File: FiscoBcosBroker4ProducerTest.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
private String signData(ExtendedRawTransaction rawTransaction, Credentials credentials) {
    byte[] signedMessage = ExtendedTransactionEncoder.signMessage(rawTransaction, credentials);
    return Numeric.toHexString(signedMessage);
}
 
Example 19
Source File: TransactionUtils.java    From web3sdk with Apache License 2.0 2 votes vote down vote up
/**
 * Utility method to provide the transaction hash for a given transaction.
 *
 * @param rawTransaction we wish to send
 * @param chainId of the intended chain
 * @param credentials of the sender
 * @return transaction hash as a hex encoded string
 */
public static String generateTransactionHashHexEncoded(
        RawTransaction rawTransaction, byte chainId, Credentials credentials) {
    return Numeric.toHexString(generateTransactionHash(rawTransaction, chainId, credentials));
}
 
Example 20
Source File: TransactionUtils.java    From web3sdk with Apache License 2.0 2 votes vote down vote up
/**
 * Utility method to provide the transaction hash for a given transaction.
 *
 * @param rawTransaction we wish to send
 * @param credentials of the sender
 * @return transaction hash as a hex encoded string
 */
public static String generateTransactionHashHexEncoded(
        RawTransaction rawTransaction, Credentials credentials) {
    return Numeric.toHexString(generateTransactionHash(rawTransaction, credentials));
}