Skip to content

Commit

Permalink
fix(BlockstreamProvider): rename Esplora
Browse files Browse the repository at this point in the history
rename blockstream provider to esplora provider to eventually allow for other esplora instances to be used
  • Loading branch information
KayBeSee committed Sep 24, 2022
1 parent 1db5a2b commit ef6b70f
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 27 deletions.
27 changes: 17 additions & 10 deletions apps/electron/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
LND,
OnchainBaseProvider,
BitcoinCoreProvider,
BlockstreamProvider,
EsploraProvider,
ElectrumProvider
} from '@lily/shared-server';

Expand Down Expand Up @@ -153,20 +153,27 @@ const setupInitialNodeConfig = async () => {
}
} catch (e) {
console.log('Failed to retrieve remote Bitcoin Core connection data.');
const defaultElectrumEndpoint = 'electrum.emzy.de';
try {
console.log('Connecting to Electrum...');
OnchainDataProvider = new ElectrumProvider('electrum.emzy.de', 50002, 'tcp', isTestnet);
console.log(`Connecting to ${defaultElectrumEndpoint}...`);
OnchainDataProvider = new ElectrumProvider(
defaultElectrumEndpoint,
50002,
'tcp',
isTestnet
);
await OnchainDataProvider.initialize();
console.log('Connected to Electrum');
console.log(`Connected to ${defaultElectrumEndpoint}`);
} catch (e) {
console.log('Failed to connect to Electrum');
console.log(`Failed to connect to ${defaultElectrumEndpoint}`);
const defaultEsploraEndpoint = 'https://blockstream.info';
try {
console.log('Connecting to Blockstream...');
OnchainDataProvider = new BlockstreamProvider('https://blockstream.info', isTestnet);
console.log(`Connecting to ${defaultEsploraEndpoint}...`);
OnchainDataProvider = new EsploraProvider(defaultEsploraEndpoint, isTestnet);
OnchainDataProvider.initialize();
console.log('Connected to Blockstream');
console.log(`Connected to ${defaultEsploraEndpoint}`);
} catch (e) {
console.log('Failed to connect to Blockstream');
console.log(`Failed to connect to ${defaultEsploraEndpoint}`);
}
}
}
Expand Down Expand Up @@ -558,7 +565,7 @@ ipcMain.handle('/changeNodeConfig', async (event, args) => {
);
await OnchainDataProvider.initialize();
} else {
OnchainDataProvider = new BlockstreamProvider('https://blockstream.info', isTestnet);
OnchainDataProvider = new EsploraProvider('https://blockstream.info', isTestnet);
await OnchainDataProvider.initialize();
}
const config = OnchainDataProvider.getConfig();
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/src/pages/Settings/NetworkSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const NetworkSettings = ({
const connectToBlockstream = async () => {
setNodeConfig(undefined);
const response = await platform.changeNodeConfig({
provider: 'Blockstream'
provider: 'Esplora'
});
setNodeConfig(response);
};
Expand Down Expand Up @@ -79,9 +79,9 @@ const NetworkSettings = ({
onClick: async () => await connectToBitcoinCore()
});
}
if (nodeConfig && nodeConfig.provider !== 'Blockstream') {
if (nodeConfig && nodeConfig.provider !== 'Esplora') {
nodeConfigDropdownItems.push({
label: 'Connect to Blockstream',
label: 'Connect to Blockstream.info',
onClick: async () => await connectToBlockstream()
});
}
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/src/utils/accountMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ const decorateTx = (
};

export const serializeTransactions = (
transactionsFromBlockstream: EsploraTransactionResponse[],
transactions: EsploraTransactionResponse[],
addresses: Address[],
changeAddresses: Address[]
): Transaction[] => {
transactionsFromBlockstream.sort((a, b) => a.status.block_time - b.status.block_time);
transactions.sort((a, b) => a.status.block_time - b.status.block_time);

const addressesMap = createMap(addresses, 'address');
const changeAddressesMap = createMap(changeAddresses, 'address');

const txMap = createMap(transactionsFromBlockstream, 'txid');
const txMap = createMap(transactions, 'txid');
const decoratedTxs = Object.values(txMap).map((tx) =>
decorateTx(tx, addressesMap, changeAddressesMap)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import {
EsploraTransactionResponse
} from '@lily/types';

// TODO: this should really be renamed "EsploraProvider" and generalize to allow ports, protocols, etc like ElectrumProvider
export class BlockstreamProvider extends OnchainBaseProvider {
export class EsploraProvider extends OnchainBaseProvider {
constructor(url: string, testnet: boolean) {
super('Blockstream', testnet, { url });
super('Esplora', testnet, { url });
}

async initialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@lily/types';
import { Network, networks } from 'bitcoinjs-lib';

type Providers = 'Blockstream' | 'Electrum' | 'Bitcoin Core';
type Providers = 'Esplora' | 'Electrum' | 'Bitcoin Core';

export interface OnchainProviderInterface {
getAccountData: (account: OnChainConfig) => Promise<LilyOnchainAccount>;
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-server/src/OnchainProviders/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './OnchainBaseProvider';
export * from './Blockstream';
export * from './Esplora';
export * from './BitcoinCore';
export * from './Electrum';
6 changes: 3 additions & 3 deletions packages/shared-server/src/utils/accountMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ const decorateTx = (
};

export const serializeTransactions = (
transactionsFromBlockstream: EsploraTransactionResponse[],
transactions: EsploraTransactionResponse[],
addresses: Address[],
changeAddresses: Address[]
): Transaction[] => {
transactionsFromBlockstream.sort((a, b) => a.status.block_time - b.status.block_time);
transactions.sort((a, b) => a.status.block_time - b.status.block_time);

const addressesMap = createMap(addresses, 'address');
const changeAddressesMap = createMap(changeAddresses, 'address');

const txMap = createMap(transactionsFromBlockstream, 'txid');
const txMap = createMap(transactions, 'txid');
const decoratedTxs = Object.values(txMap).map((tx) =>
decorateTx(tx, addressesMap, changeAddressesMap)
);
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export interface BitcoinCoreNodeConfig extends ClientOption {
provider: 'Bitcoin Core';
}

export interface BlockstreamNodeConfig {
provider: 'Blockstream';
export interface EsploraNodeConfig {
provider: 'Esplora';
}

export interface ElectrumNodeConfig {
Expand All @@ -98,7 +98,7 @@ export interface OnchainProviderConnectionDetails {
export interface NodeConfigWithBlockchainInfo {
provider:
| BitcoinCoreNodeConfig['provider']
| BlockstreamNodeConfig['provider']
| EsploraNodeConfig['provider']
| ElectrumNodeConfig['provider'];
connectionDetails: OnchainProviderConnectionDetails;
connected: boolean;
Expand Down

0 comments on commit ef6b70f

Please sign in to comment.