React Native Geth
Description
RNGeth makes using Go-Ethereum ( Official Go implementation of the Ethereum protocol ) with React Native simple.
What is react-native-geth?
Is a React-Native module allow you to build dApps (Decentralized Applications) transforms your mobile device into a light client node on the Ethereum Network and enables you to easily access Ethereum’s entire ecosystem. Interacting with Smart Contracts, make payment,....
What is Blockchain?
A blockchain, originally block chain, is a continuously growing list of records, called blocks, which are linked and secured using cryptography. Each block typically contains a hash pointer as a link to a previous block, a timestamp and transaction data. By design, blockchains are inherently resistant to modification of the data. It is "an open, distributed ledger that can record transactions between two parties efficiently and in a verifiable and permanent way". For use as a distributed ledger, a blockchain is typically managed by a peer-to-peer network collectively adhering to a protocol for validating new blocks. Once recorded, the data in any given block cannot be altered retroactively without the alteration of all subsequent blocks, which requires collusion of the network majority.
What is Ethereum?
Ethereum is a decentralized platform that runs smart contracts, applications that run exactly as programmed without possibility of downtime, censorship, fraud or third party interference.
What is Go Ethereum?
Go-Ethereum is one of the three original implementations (along with C++ and Python) of the Ethereum protocol. It is written in Go
What is Smart Contract?
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible.
Supported platforms
- Android
- iOS (Need PR)
Initial Setup
$ npm i react-native-geth --save
$ react-native link react-native-geth
JavaScript Usage
import Geth from 'react-native-geth';
// Ethereum Network Frontier
const Eth = async () => {
const geth = new Geth()
// start node
const start = await geth.start()
if (start) {
console.log('Start :', start)
// stop node
const stop = await geth.stop()
console.log('Stop :', stop)
}
}
// Custom Ethereum Network
const PrivateEth = async () => {
// Network ID
const chainID = 17
// genesis.json
const genesis = `{
"config": {
"chainId": ${chainID},
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"difficulty": "20",
"gasLimit": "10000000",
"alloc": {}
}`
const config = {
"chainID": chainID, // --networkid / Network identifier (integer, 0=Olympic (disused), 1=Frontier, 2=Morden (disused), 3=Ropsten) (default: 1)
"maxPeers": 0, // --maxpeers / Maximum number of network peers (network disabled if set to 0) (default: 25)
"genesis": genesis, // genesis.json file
"nodeDir": ".private-ethereum", // --datadir / Data directory for the databases and keystore
"keyStoreDir": "keystore", // --keystore / Directory for the keystore (default = inside the datadir)
"enodes": "enode://XXXX@X[::]:XXXX" // --bootnodes / Comma separated enode URLs for P2P discovery bootstrap
}
const geth = new Geth(config)
// start node
const start = await geth.start()
if (start) {
console.log('Start :', start)
const stop = await geth.stop()
console.log('Stop :', stop)
}
}
Documentation :
Table of Contents
Geth
Geth object
Parameters
config
Objectconfig.chainID
number Network identifier (integer, 0=Olympic (disused), 1=Frontier, 2=Morden (disused), 3=Ropsten) (default: 1)config.maxPeers
number Maximum number of network peers (network disabled if set to 0) (default: 25)config.genesis
string genesis.json fileconfig.nodeDir
string Data directory for the databases and keystoreconfig.keyStoreDir
string Directory for the keystore (default = inside the datadir)config.enodes
string Comma separated enode URLs for P2P discovery bootstrap
start
Start creates a live P2P node and starts running it.
Returns Boolean return true if started.
stop
Terminates a running node along with all it's services.
Returns Boolean return true if stopped.
newAccount
Create a new account with the specified encryption passphrase.
Parameters
passphrase
String Passphrase
Returns Object return new account object
setAccount
Sets the default account at the given index in the listAccounts.
Parameters
accID
Number index in the listAccounts
Returns Boolean return true if sets.
getAddress
Retrieves the address associated with the current account.
Returns String return address..
balanceAccount
Returns the wei balance of the current account.
Returns String return balance.
balanceAt
Returns the wei balance of the specified account.
Parameters
address
String Address of account being looked up.
Returns String Return balance.
syncProgress
Retrieves the current progress of the sync algorithm.
Returns Object Return object sync progress or null
subscribeNewHead
Subscribes to notifications about the current blockchain head
Returns Boolean Return true if subscribed
updateAccount
Changes the passphrase of current account.
Parameters
Returns Boolean Return true if passphrase changed
deleteAccount
Deletes the key matched by current account if the passphrase is correct.
Parameters
passphrase
string
Returns Boolean Return true if account deleted
exportKey
Exports as a JSON key of current account, encrypted with new passphrase.
Parameters
Returns String Return key
importKey
Stores the given encrypted JSON key into the key directory.
Parameters
Returns Object Return account object
listAccounts
Returns all key files present in the directory.
Returns Array Return array of accounts objects
createAndSendTransaction
Create and send transaction.
Parameters
passphrase
String Passphrasenonce
Number Account nonce (use -1 to use last known nonce)toAddress
String Address destinationamount
Number AmountgasLimit
Number Gas limitgasPrice
Number Gas pricedata
Number
Returns String Return transaction
suggestGasPrice
Retrieves the currently suggested gas price to allow a timely execution of a transaction.
Returns Double Return suggested gas price
getPendingNonce
Retrieves this account's pending nonce. This is the nonce you should use when creating a transaction.
Returns Double Return nonce
Press the "Watch" button to get updates. Do not forget the "Star" button 😀
React Native Geth is released under the MIT license