import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { Block, Transaction, Address, Outspend, Recent, Asset } from '../interfaces/electrs.interface'; import { StateService } from './state.service'; @Injectable({ providedIn: 'root' }) export class ElectrsApiService { private apiBaseUrl: string; // base URL is protocol, hostname, and port private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet constructor( private httpClient: HttpClient, private stateService: StateService, ) { this.apiBaseUrl = ''; // use relative URL by default if (!stateService.isBrowser) { // except when inside AU SSR process this.apiBaseUrl = this.stateService.env.NGINX_PROTOCOL + '://' + this.stateService.env.NGINX_HOSTNAME + ':' + this.stateService.env.NGINX_PORT; } this.apiBasePath = ''; // assume mainnet by default this.stateService.networkChanged$.subscribe((network) => { if (network === 'bisq') { network = ''; } this.apiBasePath = network ? '/' + network : ''; }); } getBlock$(hash: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash); } listBlocks$(height?: number): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/blocks/' + (height || '')); } getTransaction$(txId: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + txId); } getRecentTransaction$(): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/mempool/recent'); } getOutspend$(hash: string, vout: number): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspend/' + vout); } getOutspends$(hash: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspends'); } getBlockTransactions$(hash: string, index: number = 0): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash + '/txs/' + index); } getBlockHashFromHeight$(height: number): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/block-height/' + height, {responseType: 'text'}); } getAddress$(address: string): Observable
{ return this.httpClient.get
(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address); } getAddressTransactions$(address: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address + '/txs'); } getAddressTransactionsFromHash$(address: string, txid: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address + '/txs/chain/' + txid); } getAsset$(assetId: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId); } getAssetTransactions$(assetId: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs'); } getAssetTransactionsFromHash$(assetId: string, txid: string): Observable { return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs/chain/' + txid); } getAddressesByPrefix$(prefix: string): Observable { if (prefix.toLowerCase().indexOf('bc1') === 0) { prefix = prefix.toLowerCase(); } return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/address-prefix/' + prefix); } }