7

I have used web3 to interact with my private chain by using :

web3.setProvider(new web3.providers.HttpProvider("http://localhost:8012"));

But How can I connect to Public Network using web3?

I am trying to make a web interface for interaction with my contract( which deployes, where you can use all the functions provided by my contract using my web interface. I have done this on private network and everything worked seamlessly.
But now I have doubt that whether I can replicate same on public network.
For example let my contract address be x and contract ABI be y and I have a function named transfer to transfer coins. I have done this on private network:

web3.setProvider(new web3.providers.HttpProvider("http://localhost:8012"));
var contract1=web3.eth.contract(y).at(x);
contract1.transfer(to, amount, {from: frm});

Also can I unlock my account using RPC ? I mean if my users want to create an address, If I can ask for password from them and use web3.personal.newAccount(password);.

To summarise:

  1. Can I access my contract on Public network using web3? If yes, How?
  2. Is personal exposed over RPC? I mean if I have to generate addresses or unlock account using web3, how can I do that?
  3. There are api that provide Ethereum addresses, how do they work?
Prashant Prabhakar Singh
  • 8,026
  • 5
  • 40
  • 79
  • Answer to the question about the personal API: http://ethereum.stackexchange.com/questions/1413/how-can-i-make-new-account-by-json-rpc. Also, your clients should be managing their own keys; you shouldn't have access to them – Tjaden Hess Dec 09 '16 at 01:08

2 Answers2

9

This is how you access ehtereum public network

new Web3.providers.HttpProvider('https://api.myetherapi.com/eth')
Usama Ahmed
  • 151
  • 1
  • 3
4
  1. Yes, you can access to your contract on the Ethereum mainnet or testnet (public chains) after publishing a contract, exactly like you do for the private chain. You have to connect your client code (Web3 JS) to a running Ethereum node through RPC. This running node can be:

    • A node running locally on the client machine (http://localhost:8545)
    • A remote node (provided by your organisation)
    • A 3rd party node
  2. Personal is not exposed. The accounts and transactions are handled most of the time by an Ethereum browser such as Mist or Google Chome (+plugin Metamask). The Ethereum browser allows to:

    • Connect to a node
    • Create an account
    • Fund an account
    • Send transaction

For example, every time your client will try to execute a sendTransaction or a contract written functions like you wrote

var contract1=web3.eth.contract(y).at(x);
contract1.transfer(to, amount, {from: frm});

it will open a popup asking the user to approve the transaction.

Examples of Metamask

enter image description here enter image description here

  1. If you want to manage yourself all this part, I haven't tried but I guess you can use a library like eth-lightwallet

Hope this helps you.

Greg Jeanmart
  • 7,207
  • 2
  • 19
  • 35
  • Thanks for the answer. So if I want to write an api to generate ethereum addresses or carry out transactions, the only way I have is to connect to ethereum node via IPC, right? I am not sure if web3_extended can help? Does web3_extended provides support of using perosnal? – Prashant Prabhakar Singh Dec 09 '16 at 04:57
  • Also if I can't unlock account via RPC, how can I use web3 to connect to a ethereum node voia IPC. Please update the answer. Thanks – Prashant Prabhakar Singh Dec 09 '16 at 05:06