import { IBlockchain, AddressCallback, TransactionCallback } from './interface'; import { BlockchainElectrum } from './blockchain-electrum.js'; import { BlockchainLocal } from './blockchain-local.js'; import { Transaction } from '@xo/primitives'; /** * The idea here is that this will implement the IBlockchain interface. * But, it will allow the dev to specify multiple child IBlockchain interfaces for configuration. * So, if the network is not available, it can fallback to the local and sync later. * * @example * ```ts * // Actual interface will be more complex, but this is the rough idea. * const blockchain = new Blockchain([ * new BlockchainLocal(...), * new BlockchainElectrum(...) * ]) * ``` */ export class Blockchain implements IBlockchain { blockchainLocal: BlockchainLocal; blockchainElectrum: BlockchainElectrum; constructor() { this.blockchainLocal = new BlockchainLocal(); this.blockchainElectrum = new BlockchainElectrum( ['bch.imaginary.cash', 'cashnode.bch.ninja', 'electrum.imaginary.cash'], 'BCHApp', '1.5' ); } async start() { await Promise.all([ this.blockchainLocal.start(), this.blockchainElectrum.start(), ]); } async stop() { await Promise.all([ this.blockchainLocal.stop(), this.blockchainElectrum.stop(), ]); } async fetchUnspents(address: string) { if (this.blockchainElectrum.isConnected) { return await this.blockchainElectrum.fetchUnspents(address); } else { return await this.blockchainLocal.fetchUnspents(address); } } async fetchTransactions(address: string) { if (this.blockchainElectrum.isConnected) { return await this.blockchainElectrum.fetchTransactions(address); } else { return await this.blockchainLocal.fetchTransactions(address); } } async broadcastTransaction(tx: Uint8Array | Transaction | string) { if (this.blockchainElectrum.isConnected) { return await this.blockchainElectrum.broadcastTransaction(tx); } else { return await this.blockchainLocal.broadcastTransaction(tx); } } async subscribeAddress(address: string, callback: AddressCallback) { throw new Error('Not yet implemented'); } async unsubscribeAddress(address: string, callback: AddressCallback) { throw new Error('Not yet implemented'); } async subscribeTransaction( transactionHash: string, callback: TransactionCallback ) { throw new Error('Not yet implemented'); } async unsubscribeTransaction( transactionHash: string, callback: TransactionCallback ) { throw new Error('Not yet implemented'); } }