2020-11-22 23:47:27 +07:00
|
|
|
import { Injectable } from '@angular/core';
|
2023-07-13 16:57:36 +09:00
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
2023-11-12 06:38:18 +00:00
|
|
|
import { BehaviorSubject, Observable, catchError, filter, from, of, shareReplay, switchMap, take, tap } from 'rxjs';
|
2023-07-22 17:51:45 +09:00
|
|
|
import { Transaction, Address, Outspend, Recent, Asset, ScriptHash } from '../interfaces/electrs.interface';
|
2020-05-09 20:37:50 +07:00
|
|
|
import { StateService } from './state.service';
|
2022-02-04 12:51:45 +09:00
|
|
|
import { BlockExtended } from '../interfaces/node-api.interface';
|
2023-07-22 17:51:45 +09:00
|
|
|
import { calcScriptHash$ } from '../bitcoin.utils';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ElectrsApiService {
|
2020-11-27 23:01:47 +09:00
|
|
|
private apiBaseUrl: string; // base URL is protocol, hostname, and port
|
|
|
|
private apiBasePath: string; // network path is /testnet, etc. or '' for mainnet
|
2020-05-09 20:37:50 +07:00
|
|
|
|
2023-11-12 06:38:18 +00:00
|
|
|
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
constructor(
|
|
|
|
private httpClient: HttpClient,
|
2020-05-09 20:37:50 +07:00
|
|
|
private stateService: StateService,
|
2020-02-16 22:15:07 +07:00
|
|
|
) {
|
2020-11-27 23:01:47 +09:00
|
|
|
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;
|
2020-11-07 04:30:52 +07:00
|
|
|
}
|
2020-11-27 23:01:47 +09:00
|
|
|
this.apiBasePath = ''; // assume mainnet by default
|
2020-05-09 20:37:50 +07:00
|
|
|
this.stateService.networkChanged$.subscribe((network) => {
|
2020-07-25 21:59:52 +07:00
|
|
|
if (network === 'bisq') {
|
2020-07-03 23:45:19 +07:00
|
|
|
network = '';
|
|
|
|
}
|
2020-11-27 23:01:47 +09:00
|
|
|
this.apiBasePath = network ? '/' + network : '';
|
2020-05-09 20:37:50 +07:00
|
|
|
});
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2023-11-12 06:38:18 +00:00
|
|
|
private generateCacheKey(functionName: string, params: any[]): string {
|
|
|
|
return functionName + JSON.stringify(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete expired cache entries
|
|
|
|
private cleanExpiredCache(): void {
|
|
|
|
this.requestCache.forEach((value, key) => {
|
|
|
|
if (value.expiry < Date.now()) {
|
|
|
|
this.requestCache.delete(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedRequest<T, F extends (...args: any[]) => Observable<T>>(
|
|
|
|
apiFunction: F,
|
|
|
|
expireAfter: number, // in ms
|
|
|
|
...params: Parameters<F>
|
|
|
|
): Observable<T> {
|
|
|
|
this.cleanExpiredCache();
|
|
|
|
|
|
|
|
const cacheKey = this.generateCacheKey(apiFunction.name, params);
|
|
|
|
if (!this.requestCache.has(cacheKey)) {
|
|
|
|
const subject = new BehaviorSubject<T | null>(null);
|
|
|
|
this.requestCache.set(cacheKey, { subject, expiry: Date.now() + expireAfter });
|
|
|
|
|
|
|
|
apiFunction.bind(this)(...params).pipe(
|
|
|
|
tap(data => {
|
|
|
|
subject.next(data as T);
|
|
|
|
}),
|
|
|
|
catchError((error) => {
|
|
|
|
subject.error(error);
|
|
|
|
return of(null);
|
|
|
|
}),
|
|
|
|
shareReplay(1),
|
|
|
|
).subscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.requestCache.get(cacheKey).subject.asObservable().pipe(filter(val => val !== null), take(1));
|
|
|
|
}
|
|
|
|
|
2022-02-04 12:51:45 +09:00
|
|
|
getBlock$(hash: string): Observable<BlockExtended> {
|
|
|
|
return this.httpClient.get<BlockExtended>(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2022-02-04 12:51:45 +09:00
|
|
|
listBlocks$(height?: number): Observable<BlockExtended[]> {
|
|
|
|
return this.httpClient.get<BlockExtended[]>(this.apiBaseUrl + this.apiBasePath + '/api/blocks/' + (height || ''));
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getTransaction$(txId: string): Observable<Transaction> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Transaction>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + txId);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getRecentTransaction$(): Observable<Recent[]> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Recent[]>(this.apiBaseUrl + this.apiBasePath + '/api/mempool/recent');
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getOutspend$(hash: string, vout: number): Observable<Outspend> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Outspend>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspend/' + vout);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getOutspends$(hash: string): Observable<Outspend[]> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Outspend[]>(this.apiBaseUrl + this.apiBasePath + '/api/tx/' + hash + '/outspends');
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2023-08-16 02:25:48 +09:00
|
|
|
getOutspendsBatched$(txids: string[]): Observable<Outspend[][]> {
|
|
|
|
let params = new HttpParams();
|
|
|
|
params = params.append('txids', txids.join(','));
|
|
|
|
return this.httpClient.get<Outspend[][]>(this.apiBaseUrl + this.apiBasePath + '/api/txs/outspends', { params });
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getBlockTransactions$(hash: string, index: number = 0): Observable<Transaction[]> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/block/' + hash + '/txs/' + index);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2020-05-10 14:32:27 +07:00
|
|
|
getBlockHashFromHeight$(height: number): Observable<string> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get(this.apiBaseUrl + this.apiBasePath + '/api/block-height/' + height, {responseType: 'text'});
|
2020-05-10 14:32:27 +07:00
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getAddress$(address: string): Observable<Address> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Address>(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address);
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:51:45 +09:00
|
|
|
getPubKeyAddress$(pubkey: string): Observable<Address> {
|
2023-07-28 16:35:42 +09:00
|
|
|
const scriptpubkey = (pubkey.length === 130 ? '41' : '21') + pubkey + 'ac';
|
|
|
|
return this.getScriptHash$(scriptpubkey).pipe(
|
2023-07-22 17:51:45 +09:00
|
|
|
switchMap((scripthash: ScriptHash) => {
|
|
|
|
return of({
|
|
|
|
...scripthash,
|
|
|
|
address: pubkey,
|
|
|
|
is_pubkey: true,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getScriptHash$(script: string): Observable<ScriptHash> {
|
|
|
|
return from(calcScriptHash$(script)).pipe(
|
|
|
|
switchMap(scriptHash => this.httpClient.get<ScriptHash>(this.apiBaseUrl + this.apiBasePath + '/api/scripthash/' + scriptHash))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-13 16:57:36 +09:00
|
|
|
getAddressTransactions$(address: string, txid?: string): Observable<Transaction[]> {
|
|
|
|
let params = new HttpParams();
|
|
|
|
if (txid) {
|
|
|
|
params = params.append('after_txid', txid);
|
|
|
|
}
|
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/address/' + address + '/txs', { params });
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2023-07-22 17:51:45 +09:00
|
|
|
getScriptHashTransactions$(script: string, txid?: string): Observable<Transaction[]> {
|
|
|
|
let params = new HttpParams();
|
|
|
|
if (txid) {
|
|
|
|
params = params.append('after_txid', txid);
|
|
|
|
}
|
|
|
|
return from(calcScriptHash$(script)).pipe(
|
|
|
|
switchMap(scriptHash => this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/scripthash/' + scriptHash + '/txs', { params })),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:10:31 +07:00
|
|
|
getAsset$(assetId: string): Observable<Asset> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Asset>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId);
|
2020-04-28 17:10:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAssetTransactions$(assetId: string): Observable<Transaction[]> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs');
|
2020-04-28 17:10:31 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getAssetTransactionsFromHash$(assetId: string, txid: string): Observable<Transaction[]> {
|
2020-11-27 23:01:47 +09:00
|
|
|
return this.httpClient.get<Transaction[]>(this.apiBaseUrl + this.apiBasePath + '/api/asset/' + assetId + '/txs/chain/' + txid);
|
2020-04-28 17:10:31 +07:00
|
|
|
}
|
|
|
|
|
2020-07-24 22:37:35 +07:00
|
|
|
getAddressesByPrefix$(prefix: string): Observable<string[]> {
|
2021-02-03 17:13:29 +07:00
|
|
|
if (prefix.toLowerCase().indexOf('bc1') === 0) {
|
|
|
|
prefix = prefix.toLowerCase();
|
|
|
|
}
|
|
|
|
return this.httpClient.get<string[]>(this.apiBaseUrl + this.apiBasePath + '/api/address-prefix/' + prefix);
|
2020-07-24 22:37:35 +07:00
|
|
|
}
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|