Adding new getTransactionHex api

This commit is contained in:
softsimon 2022-11-22 21:45:05 +09:00
parent 4f3296566a
commit 584f443f56
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
4 changed files with 16 additions and 5 deletions

View File

@ -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[]>;

View File

@ -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[]) => {

View File

@ -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;

View File

@ -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);