您现在的位置是:首页 > 文章详情

web3j智能合约错误:java.lang.ArrayIndexOutOfBoundsException

日期:2018-07-19点击:254

web3j:智能合约写操作时总是抛出java.lang.ArrayIndexOutOfBoundsException错误,不知道什么问题。

我的代码如下:

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();

读取操作是成功的。写入返回以下错误:

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) at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:83) at org.web3j.tx.Contract.executeTransaction(Contract.java:242) at org.web3j.tx.Contract.executeTransaction(Contract.java:226) at org.web3j.tx.Contract.executeTransaction(Contract.java:220) at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:259) at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30) at com.company.api.v1.contract.ContractDocumentPayload.postDocumentPayload(ContractDocumentPayload.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161) at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205) at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99) at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347) at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102) at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at org.glassfish.jersey.internal.Errors.process(Errors.java:315) at org.glassfish.jersey.internal.Errors.process(Errors.java:297) at org.glassfish.jersey.internal.Errors.process(Errors.java:267) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305) at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154) at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473) at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341) at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:409) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)

问题的解决

应该是将错误的私钥传递给webb3j来签署交易时,会将引发该异常。请看下面的例子。

正确的工作代码(带有有效私钥):

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

相同的代码使用无效的私钥(随便添加一个数字比如a),就会抛出与你完全相同的异常:

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

始终通过WalletUtils.isValidPrivateKey()WalletUtils.isValidAddress()来检查私钥和地址是否有效。

原文《以太坊常见问题和错误》中的:
http://cw.hubwiz.com/card/c/ethereum-FAQ/1/2/9/

另外推荐几个很受欢迎全网稀缺的互动教程:

  • web3j,主要是针对java和android程序员围绕web3j库进行区块链以太坊开发的讲解。
  • python以太坊,主要是针对python围绕web3.py进行区块链以太坊应用开发的讲解。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。
原文链接:https://yq.aliyun.com/articles/617030
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章