Handle browser/node envs

Signed-off-by: James Zuccon <zuccon@gmail.com>
This commit is contained in:
James Zuccon
2024-09-13 07:54:24 +10:00
parent 2c9812200d
commit d9609e5b9f
14 changed files with 1211 additions and 307 deletions
+79 -1
View File
@@ -1,3 +1,9 @@
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.
@@ -12,4 +18,76 @@
* ])
* ```
*/
export class Blockchain {}
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');
}
}