Adding new getTransactionHex api
This commit is contained in:
		
							parent
							
								
									4f3296566a
								
							
						
					
					
						commit
						584f443f56
					
				@ -3,6 +3,7 @@ import { IEsploraApi } from './esplora-api.interface';
 | 
			
		||||
export interface AbstractBitcoinApi {
 | 
			
		||||
  $getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
 | 
			
		||||
  $getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, lazyPrevouts?: boolean): Promise<IEsploraApi.Transaction>;
 | 
			
		||||
  $getTransactionHex(txId: string): Promise<string>;
 | 
			
		||||
  $getBlockHeightTip(): Promise<number>;
 | 
			
		||||
  $getBlockHashTip(): Promise<string>;
 | 
			
		||||
  $getTxIdsForBlock(hash: string): Promise<string[]>;
 | 
			
		||||
 | 
			
		||||
@ -57,6 +57,11 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
			
		||||
      });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  $getTransactionHex(txId: string): Promise<string> {
 | 
			
		||||
    return this.$getRawTransaction(txId, true)
 | 
			
		||||
      .then((tx) => tx.hex || '');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  $getBlockHeightTip(): Promise<number> {
 | 
			
		||||
    return this.bitcoindClient.getChainTips()
 | 
			
		||||
      .then((result: IBitcoinApi.ChainTips[]) => {
 | 
			
		||||
 | 
			
		||||
@ -250,7 +250,7 @@ class BitcoinRoutes {
 | 
			
		||||
   * the full parent transaction even with segwit inputs.
 | 
			
		||||
   * It will respond with a text/plain PSBT in the same format (hex|base64).
 | 
			
		||||
   */
 | 
			
		||||
  private async postPsbtCompletion(req: Request, res: Response) {
 | 
			
		||||
  private async postPsbtCompletion(req: Request, res: Response): Promise<void> {
 | 
			
		||||
    res.setHeader('content-type', 'text/plain');
 | 
			
		||||
    const notFoundError = `Couldn't get transaction hex for parent of input`;
 | 
			
		||||
    try {
 | 
			
		||||
@ -275,11 +275,11 @@ class BitcoinRoutes {
 | 
			
		||||
            .reverse()
 | 
			
		||||
            .toString('hex');
 | 
			
		||||
 | 
			
		||||
          let transaction: IEsploraApi.Transaction;
 | 
			
		||||
          let transactionHex: string;
 | 
			
		||||
          // If missing transaction, return 404 status error
 | 
			
		||||
          try {
 | 
			
		||||
            transaction = await bitcoinApi.$getRawTransaction(txid, true);
 | 
			
		||||
            if (!transaction.hex) {
 | 
			
		||||
            transactionHex = await bitcoinApi.$getTransactionHex(txid);
 | 
			
		||||
            if (!transactionHex) {
 | 
			
		||||
              throw new Error('');
 | 
			
		||||
            }
 | 
			
		||||
          } catch (err) {
 | 
			
		||||
@ -287,7 +287,7 @@ class BitcoinRoutes {
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          psbt.updateInput(index, {
 | 
			
		||||
            nonWitnessUtxo: Buffer.from(transaction.hex, 'hex'),
 | 
			
		||||
            nonWitnessUtxo: Buffer.from(transactionHex, 'hex'),
 | 
			
		||||
          });
 | 
			
		||||
          if (!isModified) {
 | 
			
		||||
            isModified = true;
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,11 @@ class ElectrsApi implements AbstractBitcoinApi {
 | 
			
		||||
      .then((response) => response.data);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  $getTransactionHex(txId: string): Promise<string> {
 | 
			
		||||
    return axios.get<string>(config.ESPLORA.REST_API_URL + '/tx/' + txId + '/hex', this.axiosConfig)
 | 
			
		||||
      .then((response) => response.data);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  $getBlockHeightTip(): Promise<number> {
 | 
			
		||||
    return axios.get<number>(config.ESPLORA.REST_API_URL + '/blocks/tip/height', this.axiosConfig)
 | 
			
		||||
      .then((response) => response.data);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user