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