Web3 provider
Authereum is available as a Web3 provider.
Initialize the Authereum SDK and pass the provider to Web3:
import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
Alternatively, the Authereum provider can be initialized directly.
import { AuthereumProvider } from 'authereum'
import Web3 from 'web3'
const provider = new AuthereumProvider('kovan')
const web3 = new Web3(provider)
To switch networks, the provider will need to be reinitialized with the network name:
import Authereum from 'authereum'
import Web3 from 'web3'
const networkName = 'rinkeby'
const authereum = new Authereum(networkName)
const provider = authereum.getProvider()
const web3 = new Web3(provider)
Use web3 injected provider is available or use Authereum's web3 provider:
import Authereum from 'authereum'
import Web3 from 'web3'
let web3
if (window.ethereum) {
web3 = new Web3(window.ethereum)
} else if (window.web3) {
web3 = new Web3(window.web3.currentProvider)
} else {
const authereum = new Authereum('kovan')
web3 = new Web3(authereum.getProvider())
}
const accounts = await web3.eth.getAccounts()
console.log(accounts[0])
Using Web3 and Authereum libraries from CDNs and injecting web3 and the provider into the global window object:
<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script>
<script src="https://unpkg.com/[email protected]/authereum.js"></script>
<script>
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
window.ethereum = provider
window.web3 = new Web3(provider)
</script>
Call the enable method on the provider to login:
import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
await provider.enable()
// Alternatively
// await web3.currentProvider.enable()
Call the disable method on the provider to logout:
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
await provider.disable()
// Alternatively
// await web3.currentProvider.disable()
Check if user is connected to dapp.
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const isConnected = provider.isConnected()
console.log(isConnected)
Get connected account address:
import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const web3 = new Web3(authereum.getProvider())
const accounts = await web3.eth.getAccounts()
console.log(accounts[0])
// Alternatively
// const account = await web3.eth.getCoinbase()
// console.log(account)
Reading the account ETH balance:
import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const web3 = new Web3(authereum.getProvider())
const accounts = await web3.eth.getAccounts()
const balance = await web3.eth.getBalance(accounts[0])
console.log(balance)
Example of how to check if web3 provider is an Authereum provider:
import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
console.log(provider.isAuthereum)
// Alternatively
// console.log(web3.currentProvider.isAuthereum)
Example of how to sign a message:
import Authereum from 'authereum'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const account = (await provider.getAccounts())[0]
const message = 'hello world'
const signature = await web3.eth.sign(message, account)
console.log(signature)
Example of how to sign a personal message:
import Authereum from 'authereum'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const account = (await provider.getAccounts())[0]
const message = 'hello world'
const signature = await web3.eth.personal.sign(message, account)
console.log(signature)
import Authereum from 'authereum'