Home > Blockchain >  Off-line signature, call RawTransactionManager. SendTransaction throws TxHashMismatchException
Off-line signature, call RawTransactionManager. SendTransaction throws TxHashMismatchException

Time:09-23

In a recent attempt to use web3j - android library offline signature and send a trading, found that send and transaction is successful, but in the end will throw an exception in the android end
Throw an exception code is as follows:
 
Public EthSendTransaction signAndSend (RawTransaction RawTransaction)
Throws IOException {
String hexValue=https://bbs.csdn.net/topics/sign (rawTransaction);
EthSendTransaction EthSendTransaction=web3j. EthSendRawTransaction (hexValue). The send ();

If (ethSendTransaction!=null & amp; & ! EthSendTransaction. HasError ()) {
String txHashLocal=Hash. Sha3 (hexValue);
String txHashRemote=ethSendTransaction. GetTransactionHash ();
if (! TxHashVerifier. Verify (txHashLocal txHashRemote)) {
Throw new TxHashMismatchException (txHashLocal txHashRemote);
}
}

Return ethSendTransaction;
}


I write method is as follows:
 
String account1PrivateKey="f4a21456d314d1fafd9aa2dbf18e5df4a846c91570952b8c0058433508ee1256";
Credentials Credentials=Credentials. The create (account1PrivateKey);
Web3j Web3j=EthService. InitWeb3j ();
RawTransactionManager transactionManager=new RawTransactionManager (web3j, credentials);
New Thread (new Runnable () {
@ Override
Public void the run () {
Try {
EthSendTransaction EthSendTransaction=transactionManager. SendTransaction (BigInteger. The valueOf (1000 l), BigInteger. The valueOf (30000 l), acount2, "", BigInteger. The valueOf (1000 l));
String transactionHash=ethSendTransaction. GetTransactionHash ();
System. The out. Println (" transactionHash: "+ transactionHash);
} the catch (Exception e) {
e.printStackTrace();
}

}
}).start();

CodePudding user response:

Solution:
1, your offline encapsulation, don't call the contract object
/* *
* eth offline signature
*
* @ param contractAddress//contract address
* @ param to/address/bank transfer wallet
* @ param nonce//get the number of transactions
* @ param gasPrice
* @ param gasLimit
* @ param value//transfer the value of the
* @ return
*/
Public static String signedEthContractTransactionData (String privateKey, String contractAddress, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, a Double value, Double decimal) throws the Exception {

//for each tokens can stipulate the decimal places, so the actual numerical * 10 ^ small digital transfer value=
BigDecimal realValue=https://bbs.csdn.net/topics/BigDecimal.valueOf (value * math.h pow (10.0, decimal));

//hex 0 xa9059cbb represents a token transfer method (transfer) + the other side of the transfer address hex + transfer of the value of the hex
String data="https://bbs.csdn.net/topics/0xa9059cbb" + Numeric. ToHexStringNoPrefixZeroPadded (Numeric. ToBigInt (to), 64) + Numeric. ToHexStringNoPrefixZeroPadded (realValue. ToBigInteger (), 64);
RawTransaction RawTransaction=RawTransaction. CreateTransaction (nonce, gasPrice gasLimit, contractAddress, data);
//fee=(gasPrice * gasLimit)/10 ^ 18 Mr

Credentials Credentials=Credentials. The create (privateKey);
//use TransactionEncoder signature on RawTransaction for operation
Byte signedMessage=TransactionEncoder. SignMessage (rawTransaction, credentials);
//into 0 x at the beginning of the string
Return Numeric. ToHexString (signedMessage);
}

2, modify the test environment, with https://www.npmjs.com/package/ganache-cli
  • Related