Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ArrayIndexOutOfBoundsException in TransactionEncoder.signMessage #420

Closed
badboy-tian opened this issue Mar 9, 2018 · 7 comments
Closed
Labels
awaiting-user-input Require more info or input from user bug A bug in behaviour or functionality stale no activity for 21 days

Comments

@badboy-tian
Copy link

fun signedEthTransactionData(to: String, nonce: Long, gasPrice: Long, gasLimit: Long, value: Long, privateKey: String): String {
        val realValue = value * Math.pow(10.0, 18.0)
        val rawTransaction = RawTransaction.createEtherTransaction(
                BigInteger.valueOf(nonce),
                BigInteger.valueOf(gasPrice),
                BigInteger.valueOf(gasLimit),
                to,
                BigInteger.valueOf(realValue.toLong()))

        val credentials = Credentials.create(privateKey)
     
        val signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials) //error
        return Numeric.toHexString(signedMessage)
    }

but error:

t W/System.err: java.lang.ArrayIndexOutOfBoundsException: src.length=64 srcPos=0 dst.length=32 dstPos=-32 length=64
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at java.lang.System.arraycopy(Native Method)
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at org.spongycastle.crypto.signers.HMacDSAKCalculator.init(HMacDSAKCalculator.java:58)
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at org.spongycastle.crypto.signers.ECDSASigner.generateSignature(ECDSASigner.java:96)
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at org.web3j.crypto.ECKeyPair.sign(ECKeyPair.java:46)
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at org.web3j.crypto.Sign.signMessage(Sign.java:51)
03-09 23:53:35.001 15474-15474/io.haobi.wallet W/System.err:     at org.web3j.crypto.Sign.signMessage(Sign.java:39)
03-09 23:53:35.002 15474-15474/io.haobi.wallet W/System.err:     at org.web3j.crypto.TransactionEncoder.signMessage(TransactionEncoder.java:21)
03-09 23:53:35.002 15474-15474/io.haobi.wallet W/System.err:     at com.i7play.qunwallet.utils.MyWalletUtils.signedEthTransactionData(MyWalletUtils.kt:306)

I do not know why, please help me, thank you very much

@badboy-tian
Copy link
Author

I have solved it , because use the

Credentials.create(privateKey)

can not work, should use "m/44'/60'/0'/0/0" to create Credentials

 val deterministicSeed = DeterministicSeed(Arrays.asList(*mnemonic.split("\\s+".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()), null, "", 0)
        val deterministicKeyChain = DeterministicKeyChain.builder().seed(deterministicSeed).build()

        val privKey = deterministicKeyChain.getKeyByPath(BIP44_ETH_ACCOUNT_ZERO_PATH, true).privKey

@qoire
Copy link
Contributor

qoire commented Mar 10, 2018

I was able to recreate this error by using the following:

    @Test
    public void testSignedEthTransactionData() {
        BigInteger nonce = BigInteger.ZERO;
        BigInteger gasPrice = BigInteger.ONE;
        BigInteger gasLimit = BigInteger.valueOf(21000);
        String to = "0x0000000000000000000000000000000000000000";
        BigInteger value = BigInteger.TEN.pow(19); // lets assume 1 * (10 ** 18)
        System.out.println(value.longValue());

        RawTransaction raw = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, value);
        Credentials creds = Credentials.create("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");

        byte[] signedMessage = TransactionEncoder.signMessage(raw, creds);
        System.out.println(Hex.toHexString(signedMessage));
    }

Essentially using a private key outside the bounds allowed by secp256k1, some more details here.

The odd thing is the signing operation still "works" (no exceptions) even if we go beyond the upper bounds of the range, for example:
privKey = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

However upon trying to feed the same value into MetaMask or MEW I was instantly rejected (MEW actually claimed to unlock the account, but no further info). Perhaps a bounds check is required?

Anyways, I'm not well versed in cryptography so maybe someone more experienced can chime in here.

@badboy-tian
Copy link
Author

@qoire if the privatekey was created by "m/44'/60'/0'/0/0" can work nice

https://iancoleman.io/bip39/#english

@iikirilov iikirilov reopened this Mar 14, 2018
@iikirilov
Copy link
Contributor

related #403

@TOTOROYang
Copy link
Contributor

@qoire u can use my code. i use bitcoinj to make it . EthMnemonic

@iikirilov
Copy link
Contributor

@badboy-tian @qoire Is this still an issue with web3j 4.0.0-alpha-1?

@iikirilov iikirilov added the awaiting-user-input Require more info or input from user label Nov 12, 2018
@AlexandrouR AlexandrouR added the stale no activity for 21 days label Jul 29, 2019
@AlexandrouR
Copy link
Contributor

AlexandrouR commented Jul 29, 2019

Closed due to inactivity. If this is still a problem please open a new issue and follow the instructions.

Conversation move to #919

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
awaiting-user-input Require more info or input from user bug A bug in behaviour or functionality stale no activity for 21 days
Projects
None yet
Development

No branches or pull requests

5 participants