Web3 Provider

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)

Switch Networks

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)

MetaMask and Web3 Browsers

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])

Provider Options

See the Authereum SDK guide to see the options available.

Examples

Logging in:

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()

Logging out:

const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
await provider.disable()
// Alternatively
// await web3.currentProvider.disable()

Reading the balance:

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('kovan')
const web3 = new Web3(authereum.getProvider())
const balance = await web3.eth.getBalance()
console.log(balance)

Reading the enabled account:

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])

Detecting Authereum web3 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)

Sign Typed Data

import Authereum from 'authereum'
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const from = (await provider.getAccounts())[0]
const payload = [
{
type: 'string',
name: 'username',
value: 'alice'
},
{
type: 'uint32',
name: 'userId',
value: '12345'
}
]
const params = [payload, from]
const method = 'eth_signTypedData'
provider.sendAsync({
id: 1,
method,
params,
from
}, (error, res) => {
if (error) throw error
console.log(res.result)
})