1

I've made a web3js script in a wordpress website, but when executing the following code, I get an invalid JSON RPC response.

var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("https://ropsten.infura.io/xxxxxxxxxxx"));
var accounts = web3.eth.accounts;
var abi=[{"constant":false,"inputs":[{"name":"document","type":"string"},{"name":"timestamp","type":"uint256"}],"name":"notarize2","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"document","type":"string"}],"name":"checkDocument","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"proof","type":"bytes32"},{"name":"timestamp","type":"uint256"}],"name":"storeProof","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"source","type":"string"}],"name":"stringToBytes32","outputs":[{"name":"result","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"proof","type":"bytes32"}],"name":"hasProof","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
web3.eth.defaultAccount="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var poe = web3.eth.contract(abi).at("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");
console.log(poe.address);
result="test";
var today = new Date();
var todayI = today.getFullYear()*10000+ (today.getMonth()+1)*100+today.getDate();
console.log(todayI);                     
poe.notarize2(result,todayI,{gas:2000000});

The setup works perfect through testrpc (localhost) and through Remix / Metamask I could verify that the contract is correctly deployed on Ropsten and is working fine.

Any ideas? this is the first time I'm trying this.

2 Answers 2

1

Try setting the account to a string address instead. As Smarx,public node as infura don't handle private method like accounts. Also when using metamask this account set aswell as web.accounts[0]

0

I'm not sure this is your only problem, but you haven't specified an account from which to make this transaction. Infura, as a public node, can't hold anyone's private keys, so it knows of no accounts.

If you want the viewer of the page to make the transaction, you'll need to use something like MetaMask instead of directly connecting to a node.

If you want to somehow make the transaction with your own account, that would have to happen server-side, since you can't give viewers of your website access to your private key.

https://programtheblockchain.com/posts/2017/12/13/building-decentralized-apps-with-ethereum-and-javascript/ might help, particularly the "Connecting to a Node" section, which explains the role of MetaMask.

4
  • Thanks for the link. Through the info provided there, I could get it working with Metamask. That means that every user uses his own ether wallet. I'm now looking to set it up so every user uses the same predetermined wallet. Dec 30, 2017 at 14:34
  • What are you trying to do? If you let your private key leak out, what's to stop the first user from just stealing all the funds?
    – user19510
    Dec 30, 2017 at 15:16
  • The idea is that a normal user who doesn't know about wallets, metamask etcetera, is able to use the Dapp. Think like paying x USD for a service that consumes ethers from the site's wallet. I'm not sure this is actually possible though. Dec 30, 2017 at 15:32
  • If you make the transactions (or at least sign them) server-side, this is possible.
    – user19510
    Dec 30, 2017 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.