Authereum
Search…
Web3 provider

Web3 Provider

Authereum is available as a Web3 provider.
Initialize the Authereum SDK and pass the provider to Web3:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('kovan')
5
6
const provider = authereum.getProvider()
7
const web3 = new Web3(provider)
Copied!
Alternatively, the Authereum provider can be initialized directly.
1
import { AuthereumProvider } from 'authereum'
2
import Web3 from 'web3'
3
4
const provider = new AuthereumProvider('kovan')
5
const web3 = new Web3(provider)
Copied!

Switch Networks

To switch networks, the provider will need to be reinitialized with the network name:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const networkName = 'rinkeby'
5
const authereum = new Authereum(networkName)
6
7
const provider = authereum.getProvider()
8
const web3 = new Web3(provider)
Copied!

MetaMask and Web3 Browsers

Use web3 injected provider is available or use Authereum's web3 provider:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
let web3
5
6
if (window.ethereum) {
7
web3 = new Web3(window.ethereum)
8
} else if (window.web3) {
9
web3 = new Web3(window.web3.currentProvider)
10
} else {
11
const authereum = new Authereum('kovan')
12
web3 = new Web3(authereum.getProvider())
13
}
14
15
const accounts = await web3.eth.getAccounts()
16
console.log(accounts[0])
Copied!

Using Script Tags

Using Web3 and Authereum libraries from CDNs and injecting web3 and the provider into the global window object:
1
<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script>
2
<script src="https://unpkg.com/[email protected]/authereum.js"></script>
3
<script>
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
7
window.ethereum = provider
8
window.web3 = new Web3(provider)
9
</script>
Copied!

Provider Options

See the Authereum SDK guide to see the options available.

Examples

Logging in

Call the enable method on the provider to login:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('kovan')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
await provider.enable()
9
10
// Alternatively
11
// await web3.currentProvider.enable()
Copied!

Logging out

Call the disable method on the provider to logout:
1
const authereum = new Authereum('kovan')
2
const provider = authereum.getProvider()
3
const web3 = new Web3(provider)
4
5
await provider.disable()
6
7
// Alternatively
8
// await web3.currentProvider.disable()
Copied!

Is Connected

Check if user is connected to dapp.
1
const authereum = new Authereum('kovan')
2
const provider = authereum.getProvider()
3
const web3 = new Web3(provider)
4
5
const isConnected = provider.isConnected()
6
7
console.log(isConnected)
Copied!

Get Accounts

Get connected account address:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('kovan')
5
const web3 = new Web3(authereum.getProvider())
6
7
const accounts = await web3.eth.getAccounts()
8
console.log(accounts[0])
9
10
// Alternatively
11
// const account = await web3.eth.getCoinbase()
12
// console.log(account)
Copied!

Get Balance

Reading the account ETH balance:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('kovan')
5
const web3 = new Web3(authereum.getProvider())
6
7
const accounts = await web3.eth.getAccounts()
8
const balance = await web3.eth.getBalance(accounts[0])
9
console.log(balance)
Copied!

Detecting Authereum web3 provider

Example of how to check if web3 provider is an Authereum provider:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('kovan')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
console.log(provider.isAuthereum)
9
10
// Alternatively
11
// console.log(web3.currentProvider.isAuthereum)
Copied!

Sign Message

Example of how to sign a message:
1
import Authereum from 'authereum'
2
3
const authereum = new Authereum('kovan')
4
const provider = authereum.getProvider()
5
6
const account = (await provider.getAccounts())[0]
7
const message = 'hello world'
8
const signature = await web3.eth.sign(message, account)
9
console.log(signature)
Copied!

Sign Personal Message

Example of how to sign a personal message:
1
import Authereum from 'authereum'
2
3
const authereum = new Authereum('kovan')
4
const provider = authereum.getProvider()
5
6
const account = (await provider.getAccounts())[0]
7
const message = 'hello world'
8
const signature = await web3.eth.personal.sign(message, account)
9
console.log(signature)
Copied!

Sign Typed Data

Example of how to sign typed data using EIP-712:
1
import Authereum from 'authereum'
2
3
const authereum = new Authereum('kovan')
4
const provider = authereum.getProvider()
5
6
const from = (await provider.getAccounts())[0]
7
const payload = [
8
{
9
type: 'string',
10
name: 'username',
11
value: 'alice'
12
},
13
{
14
type: 'uint32',
15
name: 'userId',
16
value: '12345'
17
}
18
]
19
20
21
const params = [payload, from]
22
const method = 'eth_signTypedData'
23
24
provider.sendAsync({
25
id: 1,
26
method,
27
params,
28
from
29
}, (error, res) => {
30
if (error) throw error
31
console.log(res.result)
32
})
Copied!

Sign Transaction

Example of how to sign a transaction:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const tx = {
9
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
10
value: '10000000000000000'
11
}
12
13
const { raw } = await web3.eth.signTransaction(tx)
14
const transactionHash = web3.utils.sha3(raw)
15
16
console.log(transactionHash)
Copied!

Send Transaction

Example of how to send a transaction:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const tx = {
9
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
10
value: '10000000000000000'
11
}
12
13
const { transactionHash } = await web3.eth.sendTransaction(tx)
14
15
console.log(transactionHash)
Copied!

Send Transaction Batch

Example of how to send a transaction batch (2 ETH transfers):
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const txs = [
9
{
10
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
11
value: '10000000000000000',
12
gasLimit: '1000000'
13
}, {
14
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
15
value: '10000000000000000',
16
gasLimit: '1000000'
17
}
18
]
19
20
const { transactionHash } = await provider.sendTransactionBatch(txs)
21
22
console.log(transactionHash)
Copied!
Example of how to send a transaction batch (DAI approve and transferFrom):
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const txs = [
9
// Approve
10
{
11
to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
12
value: '0',
13
gasLimit: '1000000',
14
data: '0x095ea7b3000000000000000000000000e4c9194962532feb467dce8b3d42419641c6ed2e0000000000000000000000000000000000000000000000008c4cb6a94641cfbd'
15
},
16
// transferFrom
17
{
18
to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
19
value: '0',
20
gasLimit: '1000000',
21
data: '0x23b872dd0000000000000000000000003b18c73ed8ff086695b6b9b314843dfde9fad1c600000000000000000000000018d3d5d945353f46f0f5ca7b808c8a6443e6cbb40000000000000000000000000000000000000000000000008c4cb6a94641cfbd'
22
}
23
]
24
25
const { transactionHash } = await provider.sendTransactionBatch(txs)
26
27
console.log(transactionHash)
Copied!

Estimate Gas Batch

Example of how to estimate a transaction batch (2 ETH transfers):
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const txs = [
9
{
10
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
11
value: '10000000000000000'
12
}, {
13
to: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
14
value: '10000000000000000'
15
}
16
]
17
18
const { transactionHash } = await provider.estimateGasBatch(txs)
19
20
console.log(transactionHash) // [17894, 11845]
Copied!
Example of how to estimate a transaction batch (DAI approve and transferFrom):
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const txs = [
9
// Approve
10
{
11
to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
12
value: '0',
13
data: '0x095ea7b3000000000000000000000000e4c9194962532feb467dce8b3d42419641c6ed2e0000000000000000000000000000000000000000000000008c4cb6a94641cfbd'
14
},
15
// transferFrom
16
{
17
to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
18
value: '0',
19
data: '0x23b872dd0000000000000000000000003b18c73ed8ff086695b6b9b314843dfde9fad1c600000000000000000000000018d3d5d945353f46f0f5ca7b808c8a6443e6cbb40000000000000000000000000000000000000000000000008c4cb6a94641cfbd'
20
}
21
]
22
23
const { transactionHash } = await provider.estimateGasBatch(txs)
24
25
console.log(transactionHash) // [10955,7057]
Copied!

ERC20: Token Balance

Example of how to read token balance:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": true,
10
"inputs": [
11
{
12
"name": "_owner",
13
"type": "address"
14
}
15
],
16
"name": "balanceOf",
17
"outputs": [
18
{
19
"name": "balance",
20
"type": "uint256"
21
}
22
],
23
"payable": false,
24
"stateMutability": "view",
25
"type": "function"
26
}]
27
28
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
29
const instance = await new web3.eth.Contract(abi, token)
30
31
const account = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
32
const amount = await instance.methods.balanceOf(account).call()
33
34
console.log(amount)
Copied!

ERC20: Token Allowance

Example of how to read token allowance:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": true,
10
"inputs": [
11
{
12
"name": "_owner",
13
"type": "address"
14
},
15
{
16
"name": "_spender",
17
"type": "address"
18
}
19
],
20
"name": "allowance",
21
"outputs": [
22
{
23
"name": "",
24
"type": "uint256"
25
}
26
],
27
"payable": false,
28
"stateMutability": "view",
29
"type": "function"
30
}]
31
32
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
33
const instance = await new web3.eth.Contract(abi, token)
34
35
const owner = (await web3.eth.getAccounts())[0]
36
const spender = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
37
const allowance = await instance.methods.allowance(owner, spender).call()
38
39
console.log(allowance)
Copied!

ERC20: Token Approve

Example of how to do a token approve call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "_spender",
13
"type": "address"
14
},
15
{
16
"name": "_value",
17
"type": "uint256"
18
}
19
],
20
"name": "approve",
21
"outputs": [
22
{
23
"name": "",
24
"type": "bool"
25
}
26
],
27
"payable": false,
28
"stateMutability": "nonpayable",
29
"type": "function"
30
}]
31
32
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
33
const instance = await new web3.eth.Contract(abi, token)
34
35
const from = (await web3.eth.getAccounts())[0]
36
const spender = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
37
const value = '1000000000000000000'
38
const { transactionHash } = await instance.methods.approve(spender, value).send({ from })
39
40
console.log(transactionHash)
Copied!

ERC20: Token Transfer

Example of how to do a token transfer call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "_to",
13
"type": "address"
14
},
15
{
16
"name": "_value",
17
"type": "uint256"
18
}
19
],
20
"name": "transfer",
21
"outputs": [
22
{
23
"name": "",
24
"type": "bool"
25
}
26
],
27
"payable": false,
28
"stateMutability": "nonpayable",
29
"type": "function"
30
}]
31
32
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
33
const instance = await new web3.eth.Contract(abi, token)
34
35
const from = (await web3.eth.getAccounts())[0]
36
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
37
const value = '1000000000000000000'
38
const { transactionHash } = await instance.methods.transfer(to, value).send({ from })
39
40
console.log(transactionHash)
Copied!

ERC20: Token Transfer From

Example of how to do a token transfer from call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "_from",
13
"type": "address"
14
},
15
{
16
"name": "_to",
17
"type": "address"
18
},
19
{
20
"name": "_value",
21
"type": "uint256"
22
}
23
],
24
"name": "transferFrom",
25
"outputs": [
26
{
27
"name": "",
28
"type": "bool"
29
}
30
],
31
"payable": false,
32
"stateMutability": "nonpayable",
33
"type": "function"
34
}]
35
36
const token = '0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359'
37
const instance = await new web3.eth.Contract(abi, token)
38
39
const from = (await web3.eth.getAccounts())[0]
40
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
41
const value = '1000000000000000000'
42
const { transactionHash } = await instance.methods.transferFrom(from, to, value).send({ from })
43
44
console.log(transactionHash)
Copied!

ERC721: Owner Of

Example of how to check owner of NFT:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": true,
10
"inputs": [
11
{
12
"name": "tokenId",
13
"type": "uint256"
14
}
15
],
16
"name": "ownerOf",
17
"outputs": [
18
{
19
"name": "",
20
"type": "address"
21
}
22
],
23
"payable": false,
24
"stateMutability": "view",
25
"type": "function"
26
}]
27
28
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
29
const instance = await new web3.eth.Contract(abi, token)
30
31
const tokenId = '580767'
32
const owner = await instance.methods.ownerOf(tokenId).call()
33
34
console.log(owner)
Copied!

ERC721: Get Approved

Example of how to check if NFT is approved:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": true,
10
"inputs": [
11
{
12
"name": "tokenId",
13
"type": "uint256"
14
}
15
],
16
"name": "getApproved",
17
"outputs": [
18
{
19
"name": "",
20
"type": "address"
21
}
22
],
23
"payable": false,
24
"stateMutability": "view",
25
"type": "function"
26
}]
27
28
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
29
const instance = await new web3.eth.Contract(abi, token)
30
31
const tokenId = '580767'
32
const to = await instance.methods.getApproved(tokenId).call()
33
34
console.log(to)
Copied!

ERC721: Approve

Example of how to do a NFT approve call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "to",
13
"type": "address"
14
},
15
{
16
"name": "tokenId",
17
"type": "uint256"
18
}
19
],
20
"name": "approve",
21
"outputs": [],
22
"payable": false,
23
"stateMutability": "nonpayable",
24
"type": "function"
25
}]
26
27
const token = '0x06012c8cf97bead5deae237070f9587f8e7a266d'
28
const instance = await new web3.eth.Contract(abi, token)
29
30
const from = (await web3.eth.getAccounts())[0]
31
const to = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
32
const tokenId = '580767'
33
const { transactionHash } = await instance.methods.approve(to, tokenId).send({ from })
34
35
console.log(transactionHash)
Copied!

ERC721: Transfer

Example of how to do a NFT transfer call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "to",
13
"type": "address"
14
},
15
{
16
"name": "tokenId",
17
"type": "uint256"
18
}
19
],
20
"name": "transfer",
21
"outputs": [],
22
"payable": false,
23
"stateMutability": "nonpayable",
24
"type": "function"
25
}]
26
27
const token = '0x06012c8cf97bead5deae237070f9587f8e7a266d'
28
const instance = await new web3.eth.Contract(abi, token)
29
30
const from = (await web3.eth.getAccounts())[0]
31
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
32
const tokenId = '1667849'
33
const { transactionHash } = await instance.methods.transfer(from, to, tokenId).send({ from })
34
35
console.log(transactionHash)
Copied!

ERC721: Transfer From

Example of how to do a NFT transfer from call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "from",
13
"type": "address"
14
},
15
{
16
"name": "to",
17
"type": "address"
18
},
19
{
20
"name": "tokenId",
21
"type": "uint256"
22
}
23
],
24
"name": "transferFrom",
25
"outputs": [],
26
"payable": false,
27
"stateMutability": "nonpayable",
28
"type": "function"
29
}]
30
31
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
32
const instance = await new web3.eth.Contract(abi, token)
33
34
const from = (await web3.eth.getAccounts())[0]
35
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
36
const tokenId = '580767'
37
const { transactionHash } = await instance.methods.transferFrom(from, to, tokenId).send({ from })
38
39
console.log(transactionHash)
Copied!

ERC721: Safe Transfer From

Example of how to do a NFT safe transfer from call:
1
import Authereum from 'authereum'
2
import Web3 from 'web3'
3
4
const authereum = new Authereum('mainnet')
5
const provider = authereum.getProvider()
6
const web3 = new Web3(provider)
7
8
const abi = [{
9
"constant": false,
10
"inputs": [
11
{
12
"name": "from",
13
"type": "address"
14
},
15
{
16
"name": "to",
17
"type": "address"
18
},
19
{
20
"name": "tokenId",
21
"type": "uint256"
22
}
23
],
24
"name": "transferFrom",
25
"outputs": [],
26
"payable": false,
27
"stateMutability": "nonpayable",
28
"type": "function"
29
}]
30
31
const token = '0x6EbeAf8e8E946F0716E6533A6f2cefc83f60e8Ab'
32
const instance = await new web3.eth.Contract(abi, token)
33
34
const from = (await web3.eth.getAccounts())[0]
35
const to = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'
36
const tokenId = '580767'
37
const data = '0x00'
38
const { transactionHash } = await instance.methods.safeTransferFrom(from, to, tokenId, data).send({ from })
39
40
console.log(transactionHash)
Copied!

Validating Signatures (ecrecover)

See the Verifying Signatures page.
Last modified 1yr ago