比特币/usdt异常:Error: running with an unencrypted wallet, but walletpassphrase was called.
比特币或usdt调用转账接口时,抛出类似以下异常:
com.googlecode.jsonrpc4j.JsonRpcClientException: Error: running with an unencrypted wallet, but walletpassphrase was called. at com.googlecode.jsonrpc4j.DefaultExceptionResolver.createJsonRpcClientException(DefaultExceptionResolver.java:53) at com.googlecode.jsonrpc4j.DefaultExceptionResolver.resolveException(DefaultExceptionResolver.java:28) at com.googlecode.jsonrpc4j.JsonRpcClient.handleErrorResponse(JsonRpcClient.java:258) at com.googlecode.jsonrpc4j.JsonRpcClient.readResponse(JsonRpcClient.java:184) at com.googlecode.jsonrpc4j.JsonRpcClient.readResponse(JsonRpcClient.java:517) at com.googlecode.jsonrpc4j.JsonRpcHttpClient.invoke(JsonRpcHttpClient.java:155) at com.googlecode.jsonrpc4j.ProxyUtil$3.invoke(ProxyUtil.java:213) at com.sun.proxy.$Proxy69.walletpassphrase(Unknown Source) at com.dc3658.usdt.service.impl.CornServiceImpl.batchSendBtc(CornServiceImpl.java:493) at com.dc3658.usdt.service.impl.CornServiceImpl.sendBtcOrUsdt(CornServiceImpl.java:226) at com.dc3658.usdt.service.impl.CornServiceImpl.walletBtcBalanceCheck(CornServiceImpl.java:149) at com.dc3658.usdt.job.WalletBalanceJob.btcTransfer(WalletBalanceJob.java:69) at sun.reflect.GeneratedMethodAccessor221.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
最主要的原因是钱包为加密,而对其调用了解密的操作。
如果直接调用如下命令:
./omnicore-cli -conf=/mnt/usdt_data/bitcoin.conf walletpassphrase 'password' 60
则会在命令行中返回如下错误提示:
code:-15 message Error: running with an unencrypted wallet, but walletpassphrase was called.
加密操作
那么,如何解决此问题呢,首先对比特币或usdt进行加密操作,对应的命令为:
./omnicore-cli -conf=/mnt/usdt_data/bitcoin.conf encryptwallet "password" // 或 ./bitcoin-cli -conf=/mnt/btc_data/bitcoin.conf encryptwallet "password"
其中-conf指明了配置文件的路径,encryptwallet指明了加密的密码。
一般情况,加密之后需要重新启动区块链节点。
经过操作之后,再执行上面的命令便不会出现异常。
锁定钱包
WalletLock
调用从内存中移除钱包的加密密钥,锁定钱包。
在这个调用之后,需要再次调用walletpassphrase
解锁钱包,才能继续 调用那些需要钱包解锁的RPC API。
首先解锁钱包2分钟以便执行交易:
~$ bitcoin-cli walletpassphrase "my pass phrase" 120
然后发送一个交易,这个操作需要钱包处于解锁状态:
~$ bitcoin-cli sendtoaddress "1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd" 1.0
现在清除内存中的密码:
~$ bitcoin-cli walletlock
解锁操作
walletpassphrase
调用在内存中保存钱包的解密密钥,并在 指定的超时时间后自动锁定钱包。
在需要私钥的交易操作调用时,需要先解锁钱包。
如果钱包已经处于解锁状态,那么再次发送walletpassphrase
命令将充值钱包的锁定超时时间。
- passphrase:钱包密码,字符串
- timeout:超时锁定秒数,上限为 100000000 (~3 年)
解锁钱包60秒:
~$ bitcoin-cli walletpassphrase "my pass phrase" 60
在60秒超时之前手动锁定:
~$ bitcoin-cli walletlock
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接
本文链接:https://choupangxia.com/2019/11/16/btc-unencrypted-wallet-walletpassphrase/