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 web3if (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/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 = providerwindow.web3 = new Web3(provider)</script>
See the Authereum SDK guide to see the options available.
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)
Example of how to sign typed data using EIP-712:
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 errorconsole.log(res.result)})
Example of how to sign a 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)
Example of how to send a 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 { transactionHash } = await web3.eth.sendTransaction(tx)console.log(transactionHash)
Example of how to send a transaction batch (2 ETH transfers):
import Authereum from 'authereum'import Web3 from 'web3'const authereum = new Authereum('mainnet')const provider = authereum.getProvider()const web3 = new Web3(provider)const txs = [{to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',value: '10000000000000000',gasLimit: '1000000'}, {to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',value: '10000000000000000',gasLimit: '1000000'}]const { transactionHash } = await provider.sendTransactionBatch(txs)console.log(transactionHash)
Example of how to send a transaction batch (DAI approve and transferFrom):
import Authereum from 'authereum'import Web3 from 'web3'const authereum = new Authereum('mainnet')const provider = authereum.getProvider()const web3 = new Web3(provider)const txs = [// Approve{to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',value: '0',gasLimit: '1000000',data: '0x095ea7b3000000000000000000000000e4c9194962532feb467dce8b3d42419641c6ed2e0000000000000000000000000000000000000000000000008c4cb6a94641cfbd'},// transferFrom{to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',value: '0',gasLimit: '1000000',data: '0x23b872dd0000000000000000000000003b18c73ed8ff086695b6b9b314843dfde9fad1c600000000000000000000000018d3d5d945353f46f0f5ca7b808c8a6443e6cbb40000000000000000000000000000000000000000000000008c4cb6a94641cfbd'}]const { transactionHash } = await provider.sendTransactionBatch(txs)console.log(transactionHash)
Example of how to estimate a transaction batch (2 ETH transfers):
import Authereum from 'authereum'import Web3 from 'web3'const authereum = new Authereum('mainnet')const provider = authereum.getProvider()const web3 = new Web3(provider)const txs = [{to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',value: '10000000000000000'}, {to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',value: '10000000000000000'}]const { transactionHash } = await provider.estimateGasBatch(txs)console.log(transactionHash) // [17894, 11845]
Example of how to estimate a transaction batch (DAI approve and transferFrom):
import Authereum from 'authereum'import Web3 from 'web3'const authereum = new Authereum('mainnet')const provider = authereum.getProvider()const web3 = new Web3(provider)const txs = [// Approve{to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',value: '0',data: '0x095ea7b3000000000000000000000000e4c9194962532feb467dce8b3d42419641c6ed2e0000000000000000000000000000000000000000000000008c4cb6a94641cfbd'},// transferFrom{to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',value: '0',data: '0x23b872dd0000000000000000000000003b18c73ed8ff086695b6b9b314843dfde9fad1c600000000000000000000000018d3d5d945353f46f0f5ca7b808c8a6443e6cbb40000000000000000000000000000000000000000000000008c4cb6a94641cfbd'}]const { transactionHash } = await provider.estimateGasBatch(txs)console.log(transactionHash) // [10955,7057]
Example of how to read 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)
Example of how to read 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)
Example of how to do a token approve call:
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": 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 instance.methods.approve(spender, value).send({ from })console.log(transactionHash)
Example of how to do a token transfer call:
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": 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 instance.methods.transfer(to, value).send({ from })console.log(transactionHash)
Example of how to do a token transfer from call:
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": 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 instance.methods.transferFrom(from, to, value).send({ from })console.log(transactionHash)
Example of how to check owner of NFT:
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)
Example of how to check if NFT is 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)
Example of how to do a NFT approve call:
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": 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 instance.methods.approve(to, tokenId).send({ from })console.log(transactionHash)
Example of how to do a NFT transfer call:
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": 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 instance.methods.transfer(from, to, tokenId).send({ from })console.log(transactionHash)
Example of how to do a NFT transfer from call:
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": 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 instance.methods.transferFrom(from, to, tokenId).send({ from })console.log(transactionHash)
Example of how to do a NFT safe transfer from call:
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": 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 instance.methods.safeTransferFrom(from, to, tokenId, data).send({ from })console.log(transactionHash)
See the Verifying Signatures page.