# Web3 provider

## **Web3 Provider**

Authereum is available as a Web3 provider.

Initialize the Authereum SDK and pass the provider to Web3:

```javascript
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.

```javascript
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:

```javascript
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**

Use web3 injected provider is available or use Authereum's web3 provider:

```javascript
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:

```markup
<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](/sdk.md) to see the options available.

## **Examples**

### **Logging in**

&#x20;Call the enable method on the provider to login:

```javascript
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**

&#x20;Call the disable method on the provider to logout:

```javascript
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)

await provider.disable()

// Alternatively
// await web3.currentProvider.disable()
```

### **Is Connected**

Check if user is connected to dapp.

```javascript
const authereum = new Authereum('kovan')
const provider = authereum.getProvider()
const web3 = new Web3(provider)

const isConnected = provider.isConnected()

console.log(isConnected)
```

### **Get Accounts**

Get connected account address:

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

### **Get Balance**

&#x20;Reading the account ETH balance:

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

### Detecting Authereum web3 provider

Example of how to check if web3 provider is an Authereum provider:

```javascript
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

Example of how to sign a message:

```javascript
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

Example of how to sign a personal message:

```javascript
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

Example of how to sign typed data using [EIP-712](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md):

```javascript
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

Example of how to sign a transaction:

```javascript
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

Example of how to send a transaction:

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

### Send Transaction Batch

Example of how to send a transaction batch (2 ETH transfers):

```javascript
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):

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

### Estimate Gas Batch

Example of how to estimate a transaction batch (2 ETH transfers):

```javascript
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):

```javascript
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]
```

### ERC20: Token Balance

Example of how to read token balance:

```javascript
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

Example of how to read token allowance:

```javascript
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

Example of how to do a token approve call:

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

### ERC20: Token Transfer

Example of how to do a token transfer call:

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

### ERC20: Token Transfer From

Example of how to do a token transfer from call:

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

### ERC721: Owner Of

Example of how to check owner of NFT:

```javascript
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

Example of how to check if NFT is approved:

```javascript
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

Example of how to do a NFT approve call:

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

### ERC721: Transfer

Example of how to do a NFT transfer call:

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

### ERC721: Transfer From

Example of how to do a NFT transfer from call:

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

### ERC721: Safe Transfer From

Example of how to do a NFT safe transfer from call:

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

### Validating Signatures (ecrecover)

See the [Verifying Signatures](/verifying-signatures.md) page.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.authereum.com/web3-provider.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
