@ledgerhq/coin-hedera
Version:
Ledger Hedera Coin integration
70 lines • 2.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rpcClient = void 0;
const sdk_1 = require("@hashgraph/sdk");
const utils_1 = require("../logic/utils");
async function broadcastTransaction({ configOrCurrencyId, transaction, }) {
return transaction.execute(await getInstance(configOrCurrencyId));
}
const _hederaClients = new Map();
function getClientCacheKey(config) {
if (config.sdkClientOptions) {
return `${config.networkType}:${JSON.stringify(config.sdkClientOptions)}`;
}
return config.networkType;
}
function applySdkClientOptions(client, config) {
if (typeof config.sdkClientOptions?.maxAttempts === "number") {
client.setMaxAttempts(config.sdkClientOptions.maxAttempts);
}
if (typeof config.sdkClientOptions?.requestTimeout === "number") {
client.setRequestTimeout(config.sdkClientOptions.requestTimeout);
}
if (typeof config.sdkClientOptions?.minBackoff === "number") {
client.setMinBackoff(config.sdkClientOptions.minBackoff);
}
if (typeof config.sdkClientOptions?.maxBackoff === "number") {
client.setMaxBackoff(config.sdkClientOptions.maxBackoff);
}
}
async function createClient(config) {
const client = config.networkType === "mainnet"
? await sdk_1.Client.forMainnetAsync()
: await sdk_1.Client.forTestnetAsync();
// limit max nodes per transaction to 1 to avoid multiple signatures
client.setMaxNodesPerTransaction(1);
applySdkClientOptions(client, config);
return client;
}
async function getInstance(configOrCurrencyId) {
const config = (0, utils_1.resolveConfig)(configOrCurrencyId);
const cacheKey = getClientCacheKey(config);
if (!_hederaClients.has(cacheKey)) {
const promise = createClient(config).catch(error => {
_hederaClients.delete(cacheKey);
throw error;
});
_hederaClients.set(cacheKey, promise);
}
return _hederaClients.get(cacheKey);
}
// for testing purposes only, used to reset singleton client instances
async function _resetInstance() {
const promises = [..._hederaClients.values()];
for (const promise of promises) {
try {
const client = await promise;
client?.close();
}
catch {
// intentionally ignored during clean up
}
}
_hederaClients.clear();
}
exports.rpcClient = {
getInstance,
broadcastTransaction,
_resetInstance,
};
//# sourceMappingURL=rpc.js.map