我的主要目标是使用RPC API、Web3.py或Web3.js调用D1函数。当我使用web3.shh时,我可以调用geth attach函数;这是我最后的选择。
我正在跟踪这个回答。
1.8.0-unstablegeth标志和--rpcapi "admin,eth,net,web3,debug,shh"运行--shhconsole.log(web3.version);返回: api:'0.20.5'。当我运行以下脚本时:
Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
if(!web3.isConnected()){ //web3@0.20.5
//if(!web3.eth.net.isListening().then(console.log)){ //web3@1.0.0-beta.34
console.log("notconnected");
process.exit();
}
var kId = web3.shh.newSymKey(); //Error occurs. var kId = web3.shh.newSymKey();在web3@'0.20.5'中给出了以下错误:
Error: The method shh_newSymKey does not exist/is not available
at Object.InvalidResponse (/home/alper/eBlocBroker/node_modules/web3/lib/web3/errors.js:38:16)
at RequestManager.send (/home/alper/eBlocBroker/node_modules/web3/lib/web3/requestmanager.js:61:22)
at Shh.send [as newSymKey] (/home/alper/eBlocBroker/node_modules/web3/lib/web3/method.js:145:58)
at Object. (/home/alper/eBlocBroker/dd.js:9:20)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:501:12)
at Function.Module._load (module.js:493:3)
at Function.Module.runMain (module.js:690:10)Please注意,我已经尝试过使用 web3@1.0.0-beta.34 that也给出了类似于相同错误的错误:
Promise { }
true
(node:16162) UnhandledPromiseRejectionWarning: Error: Returned error: The method shh_newSymKey does not exist/is not available
at Object.ErrorResponse (/home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-helpers/src/errors.js:29:16)
at /home/alper/eBlocBroker/node_modules/web3-shh/node_modules/web3-core-requestmanager/src/index.js:140:36
at XMLHttpRequest.request.onreadystatechange (/home/alper/eBlocBroker/node_modules/web3/node_modules/web3-providers-http/src/index.js:77:13)
at XMLHttpRequestEventTarget.dispatchEvent (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage. (/home/alper/eBlocBroker/node_modules/xhr2/lib/xhr2.js:469:24)
at IncomingMessage.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)
(node:16162) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16162) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.<#>如何修复此错误?我做错什么了?
请注意,可用的shh函数可以看到这里;console.log(web3.shh)的输出。
发布于 2018-06-25 14:21:52
newSymKey();函数返回一个承诺(需要解析)以获得返回的值。
这就是为什么版本web3@1.0.0-beta.34中的错误为您提供了带有UnhandledPromiseRejectionWarning的promise pending消息,而在web3@0.20.5版本中,错误状态为shh_newSymKey does not exist/is not available。
所以试试var kId = web3.shh.newSymKey().then(console.log);吧
或var kId = web3.shh.newSymKey().then(function(result) { console.log(result) //will log results. })
我更喜欢使用更新的异步/等待函数,而不是承诺(读起来更容易):
Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
async function connect() { //note async function declaration
if(!await web3.isConnected()){ //await web3@0.20.5
//if(!await web3.eth.net.isListening()){ //await web3@1.0.0-beta.34
console.log("notconnected");
process.exit();
}
var kId = await web3.shh.newSymKey(); //note await
console.log(kId);
}
connect();https://ethereum.stackexchange.com/questions/51585
复制相似问题