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

The following examples show how to use org.fisco.bcos.web3j.utils.Numeric#toBigInt() . 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: TransactionResource.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public TransactionWithProof getTransactionWithProof(String transactionHash, String rootHash)
        throws IOException {
    TransactionWithProof transactionWithProof =
            web3j.getTransactionByHashWithProof(transactionHash).send();
    if (transactionWithProof.getTransactionWithProof() == null) {
        return null;
    }

    Transaction transaction = transactionWithProof.getTransactionWithProof().getTransaction();
    logger.debug("Transaction:{}", transaction);

    // transaction index
    String index = transaction.getTransactionIndexRaw();
    BigInteger indexValue = Numeric.toBigInt(index);
    byte[] byteIndex = RlpEncoder.encode(RlpString.create(indexValue));
    String input = Numeric.toHexString(byteIndex) + transactionHash.substring(2);
    logger.info("TransWithIndex:{}", input);

    String proof =
            Merkle.calculateMerkleRoot(
                    transactionWithProof.getTransactionWithProof().getTxProof(), input);
    //        System.out.println("MerkleRoot: " + proof);

    if (!proof.equals(rootHash)) {
        logger.debug("MerkleRoot:{}", proof);
        logger.debug("TransRoot :{}", rootHash);
        return null;
    }
    return transactionWithProof;
}
 
Example 2
Source File: ECDSASign.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Verify the ECDSA signature
 *
 * @param hash
 * @param publicKey
 * @param signatureData
 * @return
 */
public boolean verify(byte[] hash, BigInteger publicKey, Sign.SignatureData signatureData) {
    ECDSASignature sig =
            new ECDSASignature(
                    Numeric.toBigInt(signatureData.getR()),
                    Numeric.toBigInt(signatureData.getS()));

    BigInteger k = Sign.recoverFromSignature(signatureData.getV() - 27, sig, hash);
    return publicKey.equals(k);
}
 
Example 3
Source File: Keys.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static ECKeyPair deserialize(byte[] input) {
    if (input.length != PRIVATE_KEY_SIZE + PUBLIC_KEY_SIZE) {
        throw new RuntimeException("Invalid input key size");
    }

    BigInteger privateKey = Numeric.toBigInt(input, 0, PRIVATE_KEY_SIZE);
    BigInteger publicKey = Numeric.toBigInt(input, PRIVATE_KEY_SIZE, PUBLIC_KEY_SIZE);

    return new ECKeyPair(privateKey, publicKey);
}
 
Example 4
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 5
Source File: TypeDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
static Bool decodeBool(String rawInput, int offset) {
    String input = rawInput.substring(offset, offset + MAX_BYTE_LENGTH_FOR_HEX_STRING);
    BigInteger numericValue = Numeric.toBigInt(input);
    boolean value = numericValue.equals(BigInteger.ONE);
    return new Bool(value);
}
 
Example 6
Source File: Address.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public Address(String hexValue) {
    this(Numeric.toBigInt(hexValue));
}