0.0.38 โ€ข Published 8 months ago

@btq-js/web3-provider v0.0.38

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

@btq-js/web3-provider

This library follows the EIP-1193 proposal, providing access to BTQ network.

What is BTQ Network

Traditional blockchains like Bitcoin and Ethereum are vulnerable to quantum attacks. The BTQ Network is a quantum-resistant blockchain designed to protect your assets against the threats of quantum computing.

Signature Scheme and Quantum Security

In order to become quantum-resistant, we at BTQ use Falcon, a post-quantum digital signature, to replace the current ECDSA signature in Ethereum. Falcon is selected by NIST as a post-quantum cryptography standard for digital signatures, which ensures compactness and efficiency with provable security.

BTQ technologies are used to maintain the same level of efficiency as Ethereum, even with the use of post-quantum cryptography.

๐Ÿ“ We use different signature scheme then standard Ethereum, the HDWalletProvider reflects these change.

๐Ÿงช Try out Sign transaction to see our falcon signature.

Please note that:

This package depends on @btq-js/falcon-js by default. If we want to change the underlying falcon implementation from javascript to web assembly, please follw these simple steps

Getting started

JSON-RPC Provider

Provides access to JSON-RPC server. Currently support HTTP and Websocket.

import { JsonRpcProvider } from '@btq-js/web3-provider';

const jsonRpcProvider = new JsonRpcProvider({
  url: 'https://your-rpc.com:port' /* or ws:// */,
});

jsonRpcProvider
  .request({ method: 'eth_chainId' })
  .then((chainId) => {
    console.log('chainId (dec): ', Number.parseInt(chainId, 16)); // 222
    console.log('chainId (hex): ', chainId); // 0xde
  })
  .then(() => process.exit(0));

HD Wallet Provider

Provides basic HD wallet functionality on top of JSON-RPC server.

import { HDWalletProvider } from '@btq-js/web3-provider';

const nodeUrl = 'https://your-rpc.com';
const mnemonic = 'nasty rose intact edge chicken vanish ghost orient laugh ...';

async function signTransaction() {
  const hdWalletProvider = await HDWalletProvider.fromMnemonic(mnemonic, {
    url: nodeUrl,
    chainId: 222 /* btq */,
  });

  const signature = await hdWalletProvider.request({
    method: 'eth_signTransaction',
    params: {
      from: '0x0000000000000000000000000000000000000001',
      to: '0x0000000000000000000000000000000000000002',
      value: 123,
      nonce: 6,
      gasPrice: 24,
      gas: 39752,
    },
  });

  console.log(signature);
}

signTransaction().then(() => process.exit(0));

Web3 initialization

Wrap the provider with Web3.js for ease-of-use.

import { JsonRpcProvider } from '@btq-js/web3-provider';
import Web3 from 'web3';

const web3 = new Web3(new JsonRpcProvider({ url: 'ws://3.113.200.235:8546' }));

web3.eth
  .getGasPrice()
  .then((gasPrice) => {
    console.log(`Gas price: ${gasPrice} wei`);
  })
  .then(() => process.exit());

๐Ÿ“ Note: You can check which RPC method is called to fulfill each function call by looking their definitions.

Examples

Example 1: Log chainId

// with JsonRpcProvider
jsonRpcProvider.request({ method: 'eth_chainId' }).then((chainId) => {
  console.log('chainId (dec): ', Number.parseInt(chainId, 16)); // chainId (hex):  222
  console.log('chainId (hex): ', chainId); // chainId (hex):  0xde
});

// or with web3.js
web3.eth.getChainId().then((chainId) => {
  console.log('chainId (dec): ', chainId); // chainId (hex):  222
  console.log('chainId (hex): ', web3.utils.toHex(chainId)); // chainId (hex):  0xde
});

Example 2: Log latest block

// with JsonRpcProvider
provider.request({ method: 'eth_getBlockByNumber', params: ['latest', false] }).then((block) => {
  console.log(block);
});

// or with web3.js
web3.eth.getBlock('latest').then((block) => {
  console.log(block);
});

Example 3: Sign transaction

// with HDWalletProvider
const signed = await hdWalletProvider.request({
  method: 'eth_signTransaction',
  params: {
    from: '0x5407286398cc7dd22215cff9625075e58745b043',
    to: '0xfa30e62eedcf80d47d42947fbcc034beed5c09fe',
    value: 0,
    nonce: 6,
    gasPrice: 24,
    gas: 39752,
  },
});

// or with web3.js
const signed = await web3.eth.signTransaction({
  from: '0x5407286398cc7dd22215cff9625075e58745b043',
  to: '0xfa30e62eedcf80d47d42947fbcc034beed5c09fe',
  value: 0,
  nonce: 6,
  gasPrice: 24,
  gas: 39752,
});

Example 4: Log available accounts

// with HDWalletProvider
const accounts = await provider.request({ method: 'eth_accounts' });

// or with web3.js
const accounts = await web3.eth.getAccounts();

Example 5: Listen for new block headers

๐Ÿ“ Note: This requires WebSocket connection (ws:// or wss://)

// subscribe to new block headers
const subscriptionId = await provider.request({
  method: 'eth_subscribe',
  params: ['newHeads'],
});

// add event listener for new block headers
provider.on('message', (message) => {
  console.log(message.data.result);
});

// Unsubscribe from new block headers
provider.request({ method: 'eth_unsubscribe', params: [subscriptionId] }).then((success) => {
  if (success) console.log('unsubscribe newHeads');
});

Or with web3.js

// Subscribe to new block headers
const subscription = await web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
  if (error) return;
  console.log(blockHeader);
});

// Unsubscribe from new block headers
subscription.unsubscribe((error, success) => {
  if (success) console.log('unsubscribe newHeads');
});
0.0.37

9 months ago

0.0.38

8 months ago

0.0.35

9 months ago

0.0.36

9 months ago

0.0.30

11 months ago

0.0.31

11 months ago

0.0.32

11 months ago

0.0.33

11 months ago

0.0.34

11 months ago

0.0.28

11 months ago

0.0.29

11 months ago

0.0.25

12 months ago

0.0.26

11 months ago

0.0.27

11 months ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago