@bsv/sdk
Version:
BSV Blockchain Software Development Kit
63 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_js_1 = require("../http/index.js");
/**
* Represents an Teranode transaction broadcaster.
*/
class Teranode {
/**
* Constructs an instance of the Teranode broadcaster.
*
* @param {string} URL - The URL endpoint for the Teranode API.
* @param {HttpClient} httpClient - The HTTP client used to make requests to the API, binaryHttpClient by default.
*/
constructor(URL, httpClient = (0, index_js_1.binaryHttpClient)()) {
this.URL = URL;
this.httpClient = httpClient;
}
/**
* Broadcasts a transaction via Teranode.
*
* @param {Transaction} tx - The transaction to be broadcasted.
* @returns {Promise<BroadcastResponse | BroadcastFailure>} A promise that resolves to either a success or failure response.
*/
async broadcast(tx) {
const rawTx = tx.toEF();
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream'
},
data: new Blob([new Uint8Array(rawTx)])
};
try {
const response = await this.httpClient.request(this.URL, requestOptions);
if (response.ok) {
const txid = tx.id('hex');
return {
status: 'success',
txid,
message: 'broadcast successful'
};
}
else {
return {
status: 'error',
code: response.status.toString() ?? 'ERR_UNKNOWN',
description: response.data ?? 'Unknown error'
};
}
}
catch (error) {
return {
status: 'error',
code: '500',
description: typeof error.message === 'string'
? error.message
: 'Internal Server Error'
};
}
}
}
exports.default = Teranode;
//# sourceMappingURL=Teranode.js.map