Introduction
The Electrum Cash Protocol (@electrum-cash/protocol
) provides fully typed functions and events for all Electrum Cash methods.
For complete information on all typings, arguments and options available, read to the documentation generated from the source code.
Usage
Installation
Installation is easy, just get the library from NPM:
npm install @electrum-cash/protocol
Setup
Before you can make requests you first need to set up and connect to a server.
import { initializeElectrumClient } from '@electrum-cash/protocol';
const electrumClient = await initializeElectrumClient('Electrum Protocol Test', 'bch.imaginary.cash');
Event handling
After you have initialized an electrum client and subscribe to new information, it will emit fully typed subscription events.
electrumClient.on('blockchain.headers.subscribe', handleNewBlockchainHeaders);
electrumClient.on('blockchain.address.subscribe', handleAddressStatusUpdates);
electrumClient.on('blockchain.transaction.subscribe', handleTransactionStatusUpdates);
electrumClient.on('blockchain.transaction.dsproof.subscribe', handleTransactionDoublespendEvents);
await subscribeToBlockheaderUpdates(electrumClient);
await subscribeToAddressUpdates(electrumClient, someAddress);
await subscribeToTransactionUpdates(electrumClient, someTransactionHash);
await subscribeToDoublespendUpdates(electrumClient, someTransactionHash);
Requesting data
After you have initialized an electrum client you can request data with the following functions.
Address
import { fetchHistory, fetchPendingTransactions, fetchBalance, fetchUnspentTransactionOutputs } from '@electrum-cash/protocol';
const currentAddressHistory = await fetchHistory(electrumClient, someAddress);
console.log('history', currentAddressHistory);
const pendingTransactions = await fetchPendingTransactions(electrumClient, someAddress);
console.log('pending transactions', pendingTransactions);
const currentTrustedBalance = await fetchBalance(electrumClient, someAddress);
console.log('trusted balance', currentTrustedBalance);
const currentUnspentOutputs = await fetchUnspentTransactionOutputs(electrumClient, someAddress);
console.log('unspent outputs', currentUnspentOutputs);
Blockchain
import { fetchBlockHeaderFromBlockHeight, fetchBlockHeaders, fetchBlockHeaderWithProofFromBlockHeight, fetchCurrentChainTip } from '@electrum-cash/protocol';
const checkpointBlockHeader = await fetchBlockHeaderFromBlockHeight(electrumClient, someBlockHeight);
console.log('header from height', checkpointBlockHeader);
const blockHeaders = await fetchBlockHeaders(electrumClient, electrumCheckpoint.height, 3);
console.log('list of headers', blockHeaders);
const checkpointBlockHeaderWithProof = await fetchBlockHeaderWithProofFromBlockHeight(electrumClient, electrumCheckpoint.height, someBlockHeight);
console.log('header with proof', checkpointBlockHeaderWithProof);
const currentChainTip = await fetchCurrentChainTip(electrumClient);
console.log('current chaintip', currentChainTip);
Transaction
import { broadcastTransaction, fetchDoublespendProof, fetchTransaction, fetchTransactionBlockHeight, fetchTransactionProof } from '@electrum-cash/protocol';
const transactionHash = await broadcastTransaction(electrumClient, someTransactionHash);
console.log('txhash', transactionHash);
const doublespendProof = await fetchDoublespendProof(electrumClient, someTransactionHash);
console.log('dsproof', doublespendProof);
const transactionHex = await fetchTransaction(electrumClient, someTransactionHash);
console.log('fetch transaction', transactionHex);
const transactionBlockHeight= await fetchTransactionBlockHeight(electrumClient, someTransactionHash);
console.log('fetch transaction height', transactionBlockHeight);
const transactionProof = await fetchTransactionProof(electrumClient, someTransactionHash, 800649);
console.log('fetch transaction proof', transactionProof);
Support
If you find bugs or have problems using this library, you can file issues on the Gitlab.