LogoLogo
BloqStakeBloqCloudGitHub
  • What is Bloq?
    • Introduction
    • Bloq Account Setup
    • Create Client Keys
    • Accounts Overview
  • Bloq Services
    • BloqNodes
      • CLI
      • Nodes Usage
    • BloqStake
      • Authenticate to Bloq API
        • Authentication
        • API Routes
      • Ethereum
        • Stake ETH
        • Withdraw ETH
        • API Routes
      • Avalanche
        • Stake AVAX
        • API Routes
  • Advanced Documentation
    • Developers Guide
      • Client Tokens
      • BloqNodes Setup
      • Authentication
    • Technical Reference
      • CLI
      • Accounts API
      • Nodes API
      • Blockchain Node API
      • Javascript SDK
    • Knowledge Base
      • How do I connect my Ethereum Node using the Web3 interface?
Powered by GitBook
On this page
  • Requirements
  • 1. Setting up Parameters
  • 2. Creating a Web3 http provider with credentials
  • 3. Creating a Web3 websocket provider with credentials
  • 4. Disregarding Self-Signed Certificates
  • Additional Resources

Was this helpful?

  1. Advanced Documentation
  2. Knowledge Base

How do I connect my Ethereum Node using the Web3 interface?

PreviousKnowledge Base

Last updated 1 year ago

Was this helpful?

The web3.js library makes it simple to connect and interact with Ethereum nodes.

This knowledgebase article contains sample JS Code that should help explain how to use the web3.js library to connect to your Bloq Ethereum node.

If you haven't done so already, please see the page to spin up your Ethereum node.

Requirements

First, ensure the Web3 libraries are installed by following this .

Once the libraries have been installed, you can connect to your Ethereum node with the sample code. Some of the key steps within the sample code includes:

  1. Setting up parameters

  2. Creating a Web3 http provider with credentials

  3. Disregarding self-signed certificates

  4. Creating the Web3 instance

  5. Using the Web3 interface to fetch the current block number

1. Setting up Parameters

'use strict'

const https = require('https')
const Web3 = require('web3')

const nodeUrl = 'https://<BLOQ_ETH_NODE_IP_ADDRESS>:8545'

2. Creating a Web3 http provider with credentials

Option 1: Basic Authentication

const user = 'USER'
const password = 'PASSWORD'

// Create a Web3 HTTP provider with the basic auth credentials set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: [
    {
      name: 'Authorization',
      value: `Basic ${Buffer.from(`${user}:${password}`).toString('base64')}`
    }
  ]
})

Option 2: Using JWT

const token = 'TOKEN'

// Create a Web3 HTTP provider with JWT set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: [
    {
      name: 'Authorization',
      value: `Bearer ${token}`
    }
  ]
})

3. Creating a Web3 websocket provider with credentials

Option 1: Basic Authentication

const user = 'USER'
const password = 'PASSWORD'

// Create a Web3 WS provider with the basic auth credentials set
const web3Provider = new Web3.providers.WebsocketProvider(nodeUrl, {
  headers: {
    authorization: `Basic ${Buffer.from(`${user}:${password}`).toString(
      'base64'
    )}`
  }
})

Option 2: Using JWT

const token = 'TOKEN'

// Create a Web3 WS provider with JWT set
const web3Provider = new Web3.providers.HttpProvider(nodeUrl, {
  headers: { authorization: `Bearer ${token}` }
})

4. Disregarding Self-Signed Certificates

// Create a Web3 WS provider with basic auth credentials
// that disregards self-signed certificates
const web3Provider = new Web3.providers.WebsocketProvider(nodeUrl, {
  requestOptions: { rejectUnauthorized: false },
  headers: {
    authorization: `Basic ${Buffer.from(`${user}:${password}`).toString(
      'base64'
    )}`
  }
})

// Set the HTTPS agent in that provider to avoid complaining about
// self-signed certificates
web3Provider.httpsAgent = new https.Agent({ rejectUnauthorized: false })

Additional Resources

Web3 Documentation:

Bloq Node Setup
guide
https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html