Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Interacting with Ethereum via GETH

Once the TestRPC node starts or the full Ethereum node is synchronized to the blockchain, you can use the GETH program to connect to it and send commands and interactions to the network. All you need to do is to attach the GETH command to the node by specifying the node’s IP address. If the node is running locally (e.g., a TestRPC node on the local machine), you can simply use localhost for the IP address.

$ geth attach http://node.ip.addr:8545

GETH opens an interactive console in the new terminal, and you can use the Ethereum JavaScript API to access the blockchain. For instance, the following commands will create a new account to hold virtual currency on this network. Just repeat it a few times, and you will see a few accounts in the eth.accounts list. As mentioned earlier, each account consists of a pair of private and public keys. Only the public key is recorded on the blockchain in every transaction that involves this account.

> personal.newAccount()
Repeat passphrase:
> eth.accounts
["0x7631a9f5b7af9705eb7ce0679022d8174ae51ce0", ...]

When you create or unlock accounts from the GETH console, the private key of the account is stored in the keystore file on the attached node’s file system. On a live Ethereum node (i.e., not the TestRPC), you can start mining and deposit the ethers you mine to one of your accounts. For the TestRPC, you will have ETHs in your initial accounts, and you can skip this step.

> miner.setEtherbase(eth.accounts[0])
> miner.start(8)
> miner.stop()

Next, you can send some of your ethers from one account to another. If your GETH console is attached to a live Ethereum node, you will need access to the sender account’s private key via the keystore and passphrase on the node. If you are attached to TestRPC either via localhost or remotely, you can skip the account unlocking calls, as all accounts are unlocked by default in the TestRPC. On a console attached to a live Ethereum node, if you do not call the unlockAccount() method first, the sendTransaction() method will ask for your passphrase to unlock the account for you.

> personal.unlockAccount("0x7631a9f5b7af9705eb7ce0679022d8174ae51ce0")
Unlock account 0x7631a9f5b7af9705eb7ce0679022d8174ae51ce0
> eth.sendTransaction({from:"0x7631a9f5b7af9705eb7ce0679022d8174ae51ce0",
value: web3.toWei(0.05, "ether")})

The last line is the transaction ID for the transaction to send 0.05 ETH between the two accounts. Using a tool like Etherscan, you will be able to see a record of this transaction on the blockchain.

  • + Share This
  • 🔖 Save To Your Account