我想返回一个包含provider对象的web3实例。我希望将该对象返回给UI,这样我就可以在UI中使用该web3实例。这有可能实现吗?我尝试将web3转换为JSON.stringify(web3),但抛出错误无法将循环对象转换为字符串。
下面是我的nodejs代码
const provider = new HDWalletProvider(
'dress steel phrase album average asd dd room exile web eree cause',
'https://rinkeby.infura.io/I7P2ErGiQjuq4jNp41OE',
);
web3 = new Web3(provider);我想将web3实例从节点返回到UI,如下所示
app.get('/getWeb3', async (req, res) => {
console.log('web3 instance', web3);
res.json( JSON.stringify(web3)); // this is throwing error.
})我尝试过使用第三方库将对象转换为json,就像warp库一样,但仍然面临问题。任何建议都会对我有帮助。谢谢
发布于 2018-06-13 07:34:59
您不能发送web3实例,这没有多大意义,因为您可以直接在客户端获取web3实例。
通常在客户端,您使用Metamask,这将允许您在不运行完整节点的情况下与以太网络进行交互。
客户端
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// Interact with your contract
const Contract = web3.eth.contract(ABI);
const contract = Contract.at('address');
contract.someMethod((err, res) => console.log(res));现在,用户将使用自己的地址和密钥与以太网络进行交互,而不是使用您的私钥。
服务器上的web3实例必须保持私有,并且不向客户端公开。
与其尝试将实例发送给客户端,不如告诉我们您试图对该实例做什么,这才是需要解决的实际问题。
https://stackoverflow.com/questions/50824227
复制相似问题