[Ethereum] ethers.js connect to private net RPC

go-ethereumjson-rpckeystoreprivate-blockchainwallets

I'm trying to work on ethereum and currently I'm using ethers npm package and I was trying to connect it to my private net which I will be using it for testing.

I've run my geth with this command.

geth --port 3000 --networkid 5  --rpc --rpcport 8545 --rpcaddr 0.0.0.0 --rpccorsdomain "*" --rpcapi "eth,net,web3,personal" 

Now, I have assigned one public IP to that machine and accessing from outside through RPC. But that's not correctly working.

I'm running this code and I'm able to generate address, but on geth console, I'm not able to get any log about the created wallet.

var ethers = require('ethers');
var providers = require('ethers').providers;
var p = new providers.JsonRpcProvider('http://MY_RPC_PUBLIC_IP:8545', false, 5);
ethers.Wallet.provider=p;

var privateKey = "0x0123456789012345678901234567890123456789012345678901234567890123";
var wallet = new ethers.Wallet(privateKey);
console.log("Address: " + wallet.address);

Can anyone point out the mistake I'm doing here?

Best Answer

The wallet you have created in the above code, using the private key is only on the client, in the JavaScript. Geth has no knowledge of the private key nor the account.

The advantage of ethers.js is that Signers and Providers are kept isolated. In the above case, Geth is merely a provider. The Signer (the wallet) can use the provider:

// Connect the wallet to the provider
wallet.provider = provider;

// Ask the provider for the wallet's balance
wallet.getBalance().then(function(balance) {
    console.log(balance);
});

var sendPromise = wallet.sendTransaction({ to: targetAddress, value: amountWei})
sendPromise.then(function(tx) {
    console.log(tx);
});

But the wallet, while using the Geth node to communicate with the Ethereum network, never reveals the private key to Geth.

Related Topic