Web3 Provider

⚠️ The web3 provider is in beta

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

Using Script Tags

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/web3.js@1.0.0-beta.34/dist/web3.min.js"></script>
<script src="https://unpkg.com/authereum@latest/authereum.js"></script>
<script>
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
window.ethereum = provider
window.web3 = new Web3(provider)
</script>

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

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

Get Accounts

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)

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

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

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

Sign Transaction

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const tx = {
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
value: '10000000000000000'
}
const { raw } = await web3.eth.signTransaction(tx)
const transactionHash = web3.utils.sha3(raw)
console.log(transactionHash)

Send Transaction

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const tx = {
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
value: '10000000000000000'
}
const transactionHash = await pify(web3.eth.sendTransaction)(tx)
console.log(transactionHash)

ERC20: Token Balance

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}]
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
const instance = await new web3.eth.Contract(abi, token)
const account = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const amount = await instance.methods.balanceOf(account).call()
console.log(amount)

ERC20: Token Allowance

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}]
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
const instance = await new web3.eth.Contract(abi, token)
const owner = (await web3.eth.getAccounts())[0]
const spender = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const allowance = await instance.methods.allowance(owner, spender).call()
console.log(allowance)

ERC20: Token Approve

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const spender = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const value = '1000000000000000000'
const transactionHash = await pify(instance.methods.approve(spender, value).send)({ from })
console.log(transactionHash)

ERC20: Token Transfer

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const value = '1000000000000000000'
const transactionHash = await pify(instance.methods.transfer(to, value).send)({ from })
console.log(transactionHash)

ERC20: Token Transfer From

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const value = '1000000000000000000'
const transactionHash = await pify(instance.methods.transferFrom(from, to, value).send)({ from })
console.log(transactionHash)

ERC721: Owner Of

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": true,
"inputs": [
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "ownerOf",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}]
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
const instance = await new web3.eth.Contract(abi, token)
const tokenId = '580767'
const owner = await instance.methods.ownerOf(tokenId).call()
console.log(owner)

ERC721: Get Approved

import Authereum from 'authereum'
import Web3 from 'web3'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": true,
"inputs": [
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "getApproved",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}]
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
const instance = await new web3.eth.Contract(abi, token)
const tokenId = '580767'
const to = await instance.methods.getApproved(tokenId).call()
console.log(to)

ERC721: Approve

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "approve",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x06012c8cf97bead5deae237070f9587f8e7a266d'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
const tokenId = '580767'
const transactionHash = await pify(instance.methods.approve(to, tokenId).send)({ from })
console.log(transactionHash)

ERC721: Transfer

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "to",
"type": "address"
},
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x06012c8cf97bead5deae237070f9587f8e7a266d'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const tokenId = '1667849'
const transactionHash = await pify(instance.methods.transfer(from, to, tokenId).send)({ from })
console.log(transactionHash)

ERC721: Transfer From

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "from",
"type": "address"
},
{
"name": "to",
"type": "address"
},
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const tokenId = '580767'
const transactionHash = await pify(instance.methods.transferFrom(from, to, tokenId).send)({ from })
console.log(transactionHash)

ERC721: Safe Transfer From

import Authereum from 'authereum'
import Web3 from 'web3'
import pify from 'pify'
const authereum = new Authereum('mainnet')
const provider = authereum.getProvider()
const web3 = new Web3(provider)
const abi = [{
"constant": false,
"inputs": [
{
"name": "from",
"type": "address"
},
{
"name": "to",
"type": "address"
},
{
"name": "tokenId",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
const instance = await new web3.eth.Contract(abi, token)
const from = (await web3.eth.getAccounts())[0]
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
const tokenId = '580767'
const data = '0x00'
const transactionHash = await pify(instance.methods.safeTransferFrom(from, to, tokenId, data).send)({ from })
console.log(transactionHash)

Validating Signatures (ecrecover)

See the Verifying Signatures page.