@btq-js/web3-provider
v0.0.38
Published
This library follows the [EIP-1193][eip-1193] proposal, providing access to `BTQ network`.
Downloads
20
Readme
@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://
orwss://
)
// 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');
});