1

In my application i must follow the use case:

I need to record any document hash on the Ropsten (eth testnet) blockchain. The hash is unique and i'm using a mapping in my smart contract to accomplish this. Got no problem with that.

Since the hash is unique, if the user tries to put an already document hash registered on the blockchain, he will get a treated catch error by me. At this moment, below the treated message, I want to show the hash of the transaction that has the document hash already registered.

If it is possible, maybe i will have to encode the document hash to match with the encode input data of transaction.

I know how to get a transaction data by his hash transaction (getTransaction(txHash, callback)).

The question is: Is there another way to get a transaction datas other than by his own hash (txHash)?

Currently i'm using web3js 1.0.0 for eth client.

Sorry for my bad english :|

EDIT:

Here is what i'm thinking to do.

In my case, if the document hash already was registered in the input data field of transaction, i want to show the transaction hash.

So to begin, a little test is get a transaction list of a block. I'm passing each tx hash to (getTransaction(txHash, callback)), but i'm facing a problem that i can't get the callback of getTransaction.

My code:

async function getInputTransaction(tx) {
    web3.eth.getTransaction(tx, function(err, cb){
        console.log('got here');
        let decodeInput = web3.utils.hexToAscii(cb.input);
        decodeInput = decodeInput.split("_").pop();
        if(!err){
            console.log(cb);
            console.error('\nInput decoded: ' + '\u001b[1;32m' + decodeInput + '\u001b[0m');
        }else{
            console.error('error: ' + error);
    }}
    )
}

var tx;

web3.eth.getBlock(6339515, function(err, result){
    for(var i = 0; i <= result.transactions.length; i++){
        tx = result.transactions[i];
        getInputTransaction(tx).
        then(function() {
        })
        .catch(function(error) {
            console.log('error: \n' + error);
        });
    }
})

When i debug, the callback "cb" is not been triggered.

2 Answers 2

0

In that case try add a function like getById in your smart contract which will return all the data of your transaction. Then you compare or do anything before new transaction.

0

You asked another way to get transaction data other than querying by its hash.

Here is one :

You can get transaction details by providing the block number/block hash containing the transaction and the positional index of the transaction in the block.

getTransactionFromBlock(hashStringOrNumber, indexNumber [, callback])

Get Transaction From Block

2
  • i'll get a try on this. Commented Apr 8, 2020 at 23:03
  • Since in my case i do not know the transaction hash of where my document hash is, i think i cant' use this. I edited the original post with what i'm trying to do. Can you see it? Commented Apr 24, 2020 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.