Search in sources :

Example 1 with Transaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.Transaction in project web3sdk by FISCO-BCOS.

the class Web3jApITest method getTransactionByBlockHashAndIndex.

@Test
public void getTransactionByBlockHashAndIndex() throws IOException {
    BcosTransaction bcosTransaction = web3j.getTransactionByBlockHashAndIndex(blockHash, new BigInteger("0")).send();
    Transaction transaction = bcosTransaction.getTransaction().get();
    assertNotNull(transaction);
}
Also used : BcosTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction) Transaction(org.fisco.bcos.web3j.protocol.core.methods.response.Transaction) BcosTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 2 with Transaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.Transaction in project web3sdk by FISCO-BCOS.

the class Web3jApITest method getTransactionByBlockNumberAndIndex.

@Test
public void getTransactionByBlockNumberAndIndex() throws IOException {
    BcosTransaction bcosTransaction = web3j.getTransactionByBlockNumberAndIndex(DefaultBlockParameter.valueOf(blockNumber), new BigInteger("0")).send();
    Transaction transaction = bcosTransaction.getTransaction().get();
    assertNotNull(transaction);
}
Also used : BcosTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction) Transaction(org.fisco.bcos.web3j.protocol.core.methods.response.Transaction) BcosTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 3 with Transaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.Transaction in project web3sdk by FISCO-BCOS.

the class TransactionResource method getTransactionWithProof.

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);
    if (!proof.equals(rootHash)) {
        logger.debug("MerkleRoot:{}", proof);
        logger.debug("TransRoot :{}", rootHash);
        return null;
    }
    return transactionWithProof;
}
Also used : Transaction(org.fisco.bcos.web3j.protocol.core.methods.response.Transaction) TransactionWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof) BigInteger(java.math.BigInteger) RlpString(org.fisco.bcos.web3j.rlp.RlpString)

Example 4 with Transaction

use of org.fisco.bcos.web3j.protocol.core.methods.response.Transaction in project web3sdk by FISCO-BCOS.

the class MerkleProofUtility method verifyTransaction.

/**
 * Verify transaction merkle proof
 *
 * @param transactionRoot
 * @param transAndProof
 * @return
 */
public static boolean verifyTransaction(String transactionRoot, TransactionWithProof.TransAndProof transAndProof) {
    // transaction index
    Transaction transaction = transAndProof.getTransaction();
    BigInteger index = transaction.getTransactionIndex();
    String input = Numeric.toHexString(RlpEncoder.encode(RlpString.create(index))) + transaction.getHash().substring(2);
    String proof = Merkle.calculateMerkleRoot(transAndProof.getTxProof(), input);
    logger.debug(" transaction hash: {}, transaction index: {}, root: {}, proof: {}", transaction.getHash(), transaction.getTransactionIndex(), transactionRoot, proof);
    return proof.equals(transactionRoot);
}
Also used : Transaction(org.fisco.bcos.web3j.protocol.core.methods.response.Transaction) BigInteger(java.math.BigInteger) RlpString(org.fisco.bcos.web3j.rlp.RlpString)

Aggregations

BigInteger (java.math.BigInteger)4 Transaction (org.fisco.bcos.web3j.protocol.core.methods.response.Transaction)4 BcosTransaction (org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction)2 RlpString (org.fisco.bcos.web3j.rlp.RlpString)2 Test (org.junit.Test)2 TransactionWithProof (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof)1