Skip to content

Documentation / @cashconnect-js/templates-dev / blockchain/blockchain-electrum

BlockchainElectrumEvents ​

ts
type BlockchainElectrumEvents = object;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:43

Properties ​

isConnectedUpdated ​

ts
isConnectedUpdated: boolean;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:44

blockHeightUpdated ​

ts
blockHeightUpdated: number | undefined;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:45


BlockchainElectrumOpts ​

ts
type BlockchainElectrumOpts = object;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:48

Properties ​

store? ​

ts
optional store: BaseStore;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:49

servers? ​

ts
optional servers: string[];

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:50

application? ​

ts
optional application: string;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:51

disableBrowserVisibilityHandling? ​

ts
optional disableBrowserVisibilityHandling: boolean;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:52

disableBrowserConnectivityHandling? ​

ts
optional disableBrowserConnectivityHandling: boolean;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:53


BlockchainElectrum ​

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:56

Abstract base class representing a blockchain interface implementation. Extends EventEmitter to provide blockchain-specific event handling capabilities.

This class serves as a template for concrete blockchain implementations, providing a standardized interface for common blockchain operations such as fetching transactions, managing UTXOs, and handling blockchain subscriptions.

Extends ​

Constructors ​

Constructor ​

ts
new BlockchainElectrum(opts): BlockchainElectrum;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:84

Parameters ​
ParameterType
optsBlockchainElectrumOpts
Returns ​

BlockchainElectrum

Overrides ​

BaseBlockchain.constructor

Methods ​

from() ​

ts
static from<T>(this, opts): Promise<InstanceType<T>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:102

Creates a new BlockchainElectrum instance.

Type Parameters ​
Type Parameter
T extends typeof BlockchainElectrum
Parameters ​
ParameterTypeDescription
thisT-
optsBlockchainElectrumOptsThe options for the BlockchainElectrum instance.
Returns ​

Promise<InstanceType<T>>

A new BlockchainElectrum instance.

start() ​

ts
start(): Promise<void>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:113

Initializes the blockchain connection and required resources. Should be called before performing any blockchain operations.

Returns ​

Promise<void>

A promise that resolves when the blockchain connection is established

Throws ​

If the connection cannot be established

Overrides ​

BaseBlockchain.start

stop() ​

ts
stop(): Promise<void>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:199

Gracefully terminates the blockchain connection and cleans up resources. Should be called when blockchain operations are no longer needed.

Returns ​

Promise<void>

A promise that resolves when the shutdown is complete

Overrides ​

BaseBlockchain.stop

fetchUTXO() ​

ts
fetchUTXO(outpoint): Promise<Output>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:204

Parameters ​
ParameterType
outpointstring | Outpoint
Returns ​

Promise<Output>

Overrides ​

BaseBlockchain.fetchUTXO

fetchUTXOs() ​

ts
fetchUTXOs(address): Promise<ExtMap<BlockchainUTXO>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:225

Retrieves all unspent transaction outputs (UTXOs) for a given address.

Parameters ​
ParameterTypeDescription
addressstringThe blockchain address to query
Returns ​

Promise<ExtMap<BlockchainUTXO>>

A promise that resolves with the unspent outputs

Throws ​

If the address is invalid or the query fails

Overrides ​

BaseBlockchain.fetchUTXOs

fetchTransaction() ​

ts
fetchTransaction(txHash): Promise<Transaction>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:291

Retrieves detailed information about a specific transaction.

Parameters ​
ParameterTypeDescription
txHashstring | Uint8Array<ArrayBufferLike>The transaction hash/ID to look up
Returns ​

Promise<Transaction>

A promise that resolves with the transaction details

Throws ​

If the transaction cannot be found or the query fails

Overrides ​

BaseBlockchain.fetchTransaction

fetchAddressHistory() ​

ts
fetchAddressHistory(address): Promise<ExtMap<BlockchainTransaction>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:319

Retrieves all transactions associated with a specific address.

Parameters ​
ParameterTypeDescription
addressstringThe blockchain address to query
Returns ​

Promise<ExtMap<BlockchainTransaction>>

A promise that resolves with the list of transactions

Throws ​

If the address is invalid or the query fails

Overrides ​

BaseBlockchain.fetchAddressHistory

fetchBlockHeader() ​

ts
fetchBlockHeader(blockHeight): Promise<BlockHeader>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:366

Retrieves the Block Header at the given height.

Parameters ​
ParameterTypeDescription
blockHeightnumberThe block height of the header to retrieve
Returns ​

Promise<BlockHeader>

A promise that resolves with the Block Header

Throws ​

If the block number is invalid or the query fails

Overrides ​

BaseBlockchain.fetchBlockHeader

fetchChainTip() ​

ts
fetchChainTip(): Promise<{
  height: number;
  blockHeader: BlockHeader;
}>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:394

Retrieves the current chain tip block.

Returns ​

Promise<{ height: number; blockHeader: BlockHeader; }>

A promise that resolves with the current chain tip

Throws ​

If the chain tip cannot be retrieved

Overrides ​

BaseBlockchain.fetchChainTip

broadcastTransaction() ​

ts
broadcastTransaction(tx, opts): Promise<Transaction>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:423

Broadcasts a raw transaction to the blockchain network.

Parameters ​
ParameterTypeDescription
tx| string | Transaction | Uint8Array<ArrayBufferLike> | TransactionBCHThe serialized transaction data to broadcast
optsBroadcastOpts-
Returns ​

Promise<Transaction>

A promise that resolves when the transaction is successfully broadcast

Throws ​

If the transaction is invalid or broadcasting fails

Overrides ​

BaseBlockchain.broadcastTransaction

subscribeLockscript() ​

ts
subscribeLockscript(lockscript, callback): Promise<() => Promise<boolean>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:450

Subscribes to updates for a specific lockscript. The provided callback will be invoked when relevant events occur for the address.

Parameters ​
ParameterTypeDescription
lockscriptstring | Uint8Array<ArrayBufferLike>The blockchain lockscript to monitor
callbackBlockchainCallback<AddressStatusPayload>The callback function to handle address-related events
Returns ​

Promise<() => Promise<boolean>>

A promise that resolves when the subscription is established

Throws ​

If the subscription cannot be created

Overrides ​

BaseBlockchain.subscribeLockscript

unsubscribeLockscript() ​

ts
unsubscribeLockscript(lockscript, callback): Promise<boolean>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:464

Removes a subscription for a specific blockchain address.

Parameters ​
ParameterTypeDescription
lockscriptstring | Uint8Array<ArrayBufferLike>The blockchain lockscript to unsubscribe from
callbackBlockchainCallback<AddressStatusPayload>The callback function to remove
Returns ​

Promise<boolean>

A promise that resolves when the subscription is removed

Throws ​

If the subscription cannot be removed

Overrides ​

BaseBlockchain.unsubscribeLockscript

subscribeAddress() ​

ts
subscribeAddress(address, callback): Promise<() => Promise<boolean>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:478

Subscribes to updates for a specific blockchain address. The provided callback will be invoked when relevant events occur for the address.

Parameters ​
ParameterTypeDescription
addressstringThe blockchain address to monitor
callbackBlockchainCallback<AddressStatusPayload>The callback function to handle address-related events
Returns ​

Promise<() => Promise<boolean>>

A promise that resolves when the subscription is established

Throws ​

If the subscription cannot be created

Overrides ​

BaseBlockchain.subscribeAddress

unsubscribeAddress() ​

ts
unsubscribeAddress(address, callback): Promise<boolean>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:510

Removes a subscription for a specific blockchain address.

Parameters ​
ParameterTypeDescription
addressstringThe blockchain address to unsubscribe from
callbackBlockchainCallback<AddressStatusPayload>The callback function to remove
Returns ​

Promise<boolean>

A promise that resolves when the subscription is removed

Throws ​

If the subscription cannot be removed

Overrides ​

BaseBlockchain.unsubscribeAddress

subscribeTransaction() ​

ts
subscribeTransaction(transactionHash, callback): Promise<() => Promise<boolean>>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:551

Subscribes to updates for a specific transaction. The provided callback will be invoked when relevant events occur for the transaction.

Parameters ​
ParameterTypeDescription
transactionHashstringThe transaction hash to monitor
callbackBlockchainCallback<BlockHeightPayload>The callback function to handle transaction-related events
Returns ​

Promise<() => Promise<boolean>>

A promise that resolves when the subscription is established

Throws ​

If the subscription cannot be created

Overrides ​

BaseBlockchain.subscribeTransaction

unsubscribeTransaction() ​

ts
unsubscribeTransaction(transactionHash, callback): Promise<boolean>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:580

Removes a subscription for a specific transaction.

Parameters ​
ParameterTypeDescription
transactionHashstringThe transaction hash to unsubscribe from
callbackBlockchainCallback<BlockHeightPayload>The callback function to remove
Returns ​

Promise<boolean>

A promise that resolves when the subscription is removed

Throws ​

If the subscription cannot be removed

Overrides ​

BaseBlockchain.unsubscribeTransaction

request() ​

ts
request(method, ...parameters): Promise<RequestResponse>;

Defined in: packages/templates-dev/src/blockchain/blockchain-electrum.ts:617

Parameters ​
ParameterType
methodstring
...parametersRPCParameter[]
Returns ​

Promise<RequestResponse>

broadcastTransactions() ​

ts
broadcastTransactions(transactions): Promise<Transaction[]>;

Defined in: packages/templates-dev/src/blockchain/base-blockchain.ts:210

Parameters ​
ParameterType
transactions( | string | Transaction | Uint8Array<ArrayBufferLike> | TransactionBCH)[]
Returns ​

Promise<Transaction[]>

Inherited from ​

BaseBlockchain.broadcastTransactions

waitForTransaction() ​

ts
waitForTransaction(tx, timeoutMs): Promise<void>;

Defined in: packages/templates-dev/src/blockchain/base-blockchain.ts:232

Waits for a transaction to be seen by the node.

Parameters ​
ParameterTypeDefault valueDescription
tx| string | Transaction | Uint8Array<ArrayBufferLike> | TransactionBCHundefinedThe transaction to wait for. Can be a Uint8Array, Transaction instance, or a string.
timeoutMsnumber10_000The maximum time to wait in milliseconds before timing out. Defaults to 10,000ms (10 seconds).
Returns ​

Promise<void>

A Promise that resolves when the transaction is seen by the node or rejects if the timeout is reached.

Throws ​

Error if the transaction is not seen by the node within the specified timeout.

Inherited from ​

BaseBlockchain.waitForTransaction

waitForTransactionHash() ​

ts
waitForTransactionHash(txHash, timeoutMs): Promise<void>;

Defined in: packages/templates-dev/src/blockchain/base-blockchain.ts:242

Parameters ​
ParameterTypeDefault value
txHashstring | TransactionHash | Uint8Array<ArrayBufferLike>undefined
timeoutMsnumber10_000
Returns ​

Promise<void>

Inherited from ​

BaseBlockchain.waitForTransactionHash

on() ​

ts
on<K>(
   type, 
   listener, 
   debounceMilliseconds?): OffCallback;

Defined in: packages/core/dist/primitives.d.ts:593

Registers a listener for the specified event type.

Type Parameters ​
Type ParameterDescription
K extends keyof BlockchainEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<BlockchainEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener

Example ​
typescript
const off = emitter.on('data-change', (data) => {
  console.log('Data updated:', data);
}, 300); // Debounce for 300ms

// Later, unregister the listener
off();
Inherited from ​

BaseBlockchain.on

once() ​

ts
once<K>(
   type, 
   listener, 
   debounceMilliseconds?): OffCallback;

Defined in: packages/core/dist/primitives.d.ts:611

Registers a one-time listener for the specified event type. The listener will be automatically removed after being called once.

Type Parameters ​
Type ParameterDescription
K extends keyof BlockchainEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to listen for
listenerListener<BlockchainEvents[K]>The function to call when the event is emitted
debounceMilliseconds?numberOptional debounce time in milliseconds
Returns ​

OffCallback

A callback function that can be used to unregister this listener before it fires

Example ​
typescript
emitter.once('user-login', ({ userId }) => {
  console.log(`Welcome ${userId}! This will only show once.`);
});
Inherited from ​

BaseBlockchain.once

off() ​

ts
off<K>(type, listener): void;

Defined in: packages/core/dist/primitives.d.ts:626

Removes a specific listener for the given event type.

Type Parameters ​
Type ParameterDescription
K extends keyof BlockchainEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to remove the listener from
listenerListener<BlockchainEvents[K]>The listener function to remove
Returns ​

void

Example ​
typescript
const handler = (data) => console.log(data);
emitter.on('test', handler);
emitter.off('test', handler); // Removes the listener
Inherited from ​

BaseBlockchain.off

emit() ​

ts
emit<K>(type, payload): boolean;

Defined in: packages/core/dist/primitives.d.ts:648

Emits an event of the specified type with the given payload. All registered listeners for this event type will be called.

Type Parameters ​
Type ParameterDescription
K extends keyof BlockchainEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to emit
payloadBlockchainEvents[K]The data to pass to all listeners
Returns ​

boolean

true if there were listeners for this event, false otherwise

Example ​
typescript
const hasListeners = emitter.emit('user-login', {
  userId: '123',
  timestamp: Date.now()
});

if (!hasListeners) {
  console.log('No one was listening for user-login events');
}
Inherited from ​

BaseBlockchain.emit

removeAllListeners() ​

ts
removeAllListeners(): void;

Defined in: packages/core/dist/primitives.d.ts:659

Removes all listeners for all events. This effectively resets the EventEmitter to its initial state.

Returns ​

void

Example ​
typescript
emitter.removeAllListeners();
// All previously registered listeners are now gone
Inherited from ​

BaseBlockchain.removeAllListeners

waitFor() ​

ts
waitFor<K>(
   type, 
   predicate, 
   timeoutMs?): Promise<BlockchainEvents[K]>;

Defined in: packages/core/dist/primitives.d.ts:685

Returns a Promise that resolves when an event of the specified type is emitted and matches the given predicate.

Type Parameters ​
Type ParameterDescription
K extends keyof BlockchainEventsThe event type key
Parameters ​
ParameterTypeDescription
typeKThe event type to wait for
predicate(payload) => booleanA function that determines if the event payload matches what we're waiting for
timeoutMs?numberOptional timeout in milliseconds. If specified, the promise will reject after this time
Returns ​

Promise<BlockchainEvents[K]>

A Promise that resolves with the event payload when the condition is met

Throws ​

If the timeout is reached before a matching event occurs

Example ​
typescript
try {
  const loginData = await emitter.waitFor(
    'user-login',
    ({ userId }) => userId === 'admin',
    5000 // Wait up to 5 seconds
  );
  console.log('Admin logged in:', loginData);
} catch (error) {
  console.log('Timeout: Admin never logged in');
}
Inherited from ​

BaseBlockchain.waitFor

Properties ​

PropertyModifierTypeDefault valueDefined in
electrumClientpublicElectrumClient<ElectrumProtocolEvents>undefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:61
isConnectedpublicbooleanfalsepackages/templates-dev/src/blockchain/blockchain-electrum.ts:64
blockHeightpublicnumber | undefinedundefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:65
addressSubscriptionspublicMap<string, Set<BlockchainCallback<AddressStatusPayload>>>undefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:68
transactionSubscriptionspublicMap<string, Set<BlockchainCallback<BlockHeightPayload>>>undefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:74
storepublicBaseStoreundefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:80
serverspublicstring[]undefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:81
applicationpublicstringundefinedpackages/templates-dev/src/blockchain/blockchain-electrum.ts:82
optsreadonlyBlockchainElectrumOpts{}packages/templates-dev/src/blockchain/blockchain-electrum.ts:84