Address page mostly working.

This commit is contained in:
softsimon
2020-12-22 06:04:31 +07:00
parent 4593a76675
commit 86c4119e1c
9 changed files with 195 additions and 25 deletions

View File

@@ -1,4 +1,5 @@
import { MempoolInfo, Transaction, Block, MempoolEntries, MempoolEntry, Address } from '../../interfaces';
import { MempoolInfo, Transaction, Block, MempoolEntries, MempoolEntry, Address, AddressInformation,
ScriptHashBalance, ScriptHashHistory } from '../../interfaces';
export interface AbstractBitcoinApi {
$getMempoolInfo(): Promise<MempoolInfo>;
@@ -10,6 +11,9 @@ export interface AbstractBitcoinApi {
$getBlock(hash: string): Promise<Block>;
$getMempoolEntry(txid: string): Promise<MempoolEntry>;
$getAddress(address: string): Promise<Address>;
$validateAddress(address: string): Promise<AddressInformation>;
$getScriptHashBalance(scriptHash: string): Promise<ScriptHashBalance>;
$getScriptHashHistory(scriptHash: string): Promise<ScriptHashHistory[]>;
// Custom
$getRawMempoolVerbose(): Promise<MempoolEntries>;

View File

@@ -1,8 +1,10 @@
import config from '../../config';
import { Transaction, Block, MempoolInfo, RpcBlock, MempoolEntries, MempoolEntry, Address } from '../../interfaces';
import { Transaction, Block, MempoolInfo, RpcBlock, MempoolEntries, MempoolEntry, Address,
AddressInformation, ScriptHashBalance, ScriptHashHistory } from '../../interfaces';
import * as bitcoin from '@mempool/bitcoin';
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
class BitcoindApi {
class BitcoindApi implements AbstractBitcoinApi {
bitcoindClient: any;
constructor() {
@@ -82,6 +84,18 @@ class BitcoindApi {
$getAddress(address: string): Promise<Address> {
throw new Error('Method not implemented.');
}
$validateAddress(address: string): Promise<AddressInformation> {
return this.bitcoindClient.validateAddress(address);
}
$getScriptHashBalance(scriptHash: string): Promise<ScriptHashBalance> {
throw new Error('Method not implemented.');
}
$getScriptHashHistory(scriptHash: string): Promise<ScriptHashHistory[]> {
throw new Error('Method not implemented.');
}
}
export default BitcoindApi;

View File

@@ -1,11 +1,13 @@
import config from '../../config';
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
import { Transaction, Block, MempoolInfo, RpcBlock, MempoolEntries, MempoolEntry, Address } from '../../interfaces';
import { Transaction, Block, MempoolInfo, RpcBlock, MempoolEntries, MempoolEntry, Address,
AddressInformation, ScriptHashBalance, ScriptHashHistory } from '../../interfaces';
import * as bitcoin from '@mempool/bitcoin';
import * as ElectrumClient from '@codewarriorr/electrum-client-js';
import logger from '../../logger';
import transactionUtils from '../transaction-utils';
import * as sha256 from 'crypto-js/sha256';
import * as hexEnc from 'crypto-js/enc-hex';
class BitcoindElectrsApi implements AbstractBitcoinApi {
bitcoindClient: any;
electrumClient: any;
@@ -117,6 +119,23 @@ class BitcoindElectrsApi implements AbstractBitcoinApi {
throw new Error(e);
}
}
$validateAddress(address: string): Promise<AddressInformation> {
return this.bitcoindClient.validateAddress(address);
}
$getScriptHashBalance(scriptHash: string): Promise<ScriptHashBalance> {
return this.electrumClient.blockchain_scripthash_getBalance(this.encodeScriptHash(scriptHash));
}
$getScriptHashHistory(scriptHash: string): Promise<ScriptHashHistory[]> {
return this.electrumClient.blockchain_scripthash_getHistory(this.encodeScriptHash(scriptHash));
}
private encodeScriptHash(scriptPubKey: string): string {
const addrScripthash = hexEnc.stringify(sha256(hexEnc.parse(scriptPubKey)));
return addrScripthash.match(/.{2}/g).reverse().join('');
}
}
export default BitcoindElectrsApi;

View File

@@ -1,6 +1,7 @@
import config from '../../config';
import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
import { Transaction, Block, MempoolInfo, MempoolEntry, MempoolEntries, Address } from '../../interfaces';
import { Transaction, Block, MempoolInfo, MempoolEntry, MempoolEntries, Address,
AddressInformation, ScriptHashBalance, ScriptHashHistory } from '../../interfaces';
import axios from 'axios';
class ElectrsApi implements AbstractBitcoinApi {
@@ -63,6 +64,19 @@ class ElectrsApi implements AbstractBitcoinApi {
$getAddress(address: string): Promise<Address> {
throw new Error('Method not implemented.');
}
$getScriptHashBalance(scriptHash: string): Promise<ScriptHashBalance> {
throw new Error('Method not implemented.');
}
$getScriptHashHistory(scriptHash: string): Promise<ScriptHashHistory[]> {
throw new Error('Method not implemented.');
}
$validateAddress(address: string): Promise<AddressInformation> {
throw new Error('Method not implemented.');
}
}
export default ElectrsApi;