Web3j Writing operations on a contract always throw java lang ArrayIndexOutOfBoundsException

0 votes

Read operations are successfull. Writing returns the following error:

java.lang.ArrayIndexOutOfBoundsException
 at java.lang.System.arraycopy(Native Method)
 at org.bouncycastle.crypto.signers.HMacDSAKCalculator.init(Unknown Source)
 at org.bouncycastle.crypto.signers.ECDSASigner.generateSignature(Unknown Source)
 at org.web3j.crypto.ECKeyPair.sign(ECKeyPair.java:46)
 at org.web3j.crypto.Sign.signMessage(Sign.java:52)
 at org.web3j.crypto.Sign.signMessage(Sign.java:40)
 at org.web3j.crypto.TransactionEncoder.signMessage(TransactionEncoder.java:21)
 at org.web3j.tx.RawTransactionManager.signAndSend(RawTransactionManager.java:102)
 at org.web3j.tx.RawTransactionManager.sendTransaction(RawTransactionManager.java:91)
 at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:49)
 

I am using the following code:

String hexPrivateKey = String.format("%040x", new BigInteger(1, Config.privateKey.getBytes()));
String hexPublicKey = String.format("%040x", new BigInteger(1, Config.publicKey.getBytes()));
Credentials credentials = Credentials.create(hexPrivateKey, hexPublicKey);
DataSaver contract = DataSaver.load(
address, web3j, credentials, Contract.GAS_PRICE, Contract.GAS_LIMIT);

RemoteCall transaction2 = contract.updateDocumentPayload(
BigInteger.valueOf(1), "Test");
transaction2.send();

Can you tell me, what I am doing wrong?

Oct 1, 2018 in Blockchain by digger
• 26,740 points
528 views

1 answer to this question.

0 votes

That exception is thrown when you pass an invalid private key, with the wrong length, to web3j to sign the transaction. Take a look in the example below.

Working code (with valid private key):

BigInteger privateKey = new BigInteger("f9c8a5d689736d881cf9b4117bbae6d935b2368f8260a25677e35d4d1eea7231", 16);
BigInteger publicKey = new BigInteger("72445fcfdeb1fff79496d7ce66089d663ff90e26", 16);
ECKeyPair pair = new ECKeyPair(privateKey, publicKey);
pair.sign("".getBytes());

Same code using invalid private key (one more digit added) and throwing exactly the same exception as yours:

BigInteger privateKey = new BigInteger("f9c8a5d689736d881cf9b4117bbae6d935b2368f8260a25677e35d4d1eea7231a", 16);
BigInteger publicKey = new BigInteger("72445fcfdeb1fff79496d7ce66089d663ff90e26a", 16);
ECKeyPair pair = new ECKeyPair(privateKey, publicKey);
pair.sign("".getBytes());

Always check if the private key and address are valid using WalletUtils.isValidPrivateKey() and WalletUtils.isValidAddress()

answered Oct 1, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

+1 vote
3 answers

What is the pattern for handling throw on a Solidity contract in tests

In my opinion the cleanest way is: it("should ...READ MORE

answered Sep 25, 2018 in Blockchain by Lupin
1,555 views
0 votes
0 answers
0 votes
1 answer

Solidity: Code to hide message sender address (msg.sender) when writing on a contract

Hey @Wilson, You can use a smart contract ...READ MORE

answered Sep 14, 2020 in Blockchain by Rajiv
• 8,910 points
1,671 views
+1 vote
1 answer

How is a smart contract stored on a Blockchain?

Smart contract template and transaction events are ...READ MORE

answered Apr 4, 2018 in Blockchain by Christine
• 15,790 points
901 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,702 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
967 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,350 points
3,677 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP