@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
131 lines • 7.92 kB
JavaScript
;
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeysCacheSDK = void 0;
const csm_sdk_module_js_1 = require("../common/class-primitives/csm-sdk-module.js");
const index_js_1 = require("../common/decorators/index.js");
const is_hexadecimal_string_js_1 = require("../common/utils/is-hexadecimal-string.js");
const storage_js_1 = require("./storage.js");
let KeysCacheSDK = (() => {
var _a;
let _classSuper = csm_sdk_module_js_1.CsmSDKModule;
let _instanceExtraInitializers = [];
let _addPubkeys_decorators;
let _removePubkeys_decorators;
let _clearAllKeys_decorators;
let _hasCachedKey_decorators;
let _getCachedKeys_decorators;
let _isDuplicate_decorators;
return _a = class KeysCacheSDK extends _classSuper {
get storageKey() {
return `lido-csm-keys-cache-${this.core.chainId}`;
}
getKeys() {
return (0, storage_js_1.getFromLocalStorage)(this.storageKey) || {};
}
setKeys(keys) {
(0, storage_js_1.saveToLocalStorage)(this.storageKey, keys);
}
addPubkeys(pubkeys) {
if (pubkeys.length === 0)
return;
const timestamp = Date.now();
const storedKeys = this.getKeys();
const updatedKeys = pubkeys.reduce((keys, pubkey) => {
const key = (0, is_hexadecimal_string_js_1.normalizeTrimHex)(pubkey);
keys[key] = timestamp;
return keys;
}, { ...storedKeys });
const cleanedKeys = (0, storage_js_1.cleanExpiredKeys)(updatedKeys);
this.setKeys(cleanedKeys);
}
removePubkeys(pubkeys) {
if (pubkeys.length === 0)
return;
const storedKeys = this.getKeys();
const updatedKeys = { ...storedKeys };
pubkeys.forEach((pubkey) => {
const key = (0, is_hexadecimal_string_js_1.normalizeTrimHex)(pubkey);
delete updatedKeys[key];
});
const cleanedKeys = (0, storage_js_1.cleanExpiredKeys)(updatedKeys);
this.setKeys(cleanedKeys);
}
clearAllKeys() {
this.setKeys({});
}
hasCachedKey(pubkey) {
const key = (0, is_hexadecimal_string_js_1.normalizeTrimHex)(pubkey);
const storedKeys = this.getKeys();
const timestamp = storedKeys[key];
if (!timestamp)
return false;
return !(0, storage_js_1.isKeyExpired)(timestamp);
}
getCachedKeys() {
const storedKeys = this.getKeys();
const cleanedKeys = (0, storage_js_1.cleanExpiredKeys)(storedKeys);
this.setKeys(cleanedKeys);
return Object.keys(cleanedKeys).map(is_hexadecimal_string_js_1.toHexString);
}
isDuplicate(pubkey) {
return this.hasCachedKey(pubkey);
}
constructor() {
super(...arguments);
__runInitializers(this, _instanceExtraInitializers);
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_addPubkeys_decorators = [(0, index_js_1.Logger)('Cache:')];
_removePubkeys_decorators = [(0, index_js_1.Logger)('Cache:')];
_clearAllKeys_decorators = [(0, index_js_1.Logger)('Cache:')];
_hasCachedKey_decorators = [(0, index_js_1.Logger)('Cache:')];
_getCachedKeys_decorators = [(0, index_js_1.Logger)('Cache:')];
_isDuplicate_decorators = [(0, index_js_1.Logger)('Cache:')];
__esDecorate(_a, null, _addPubkeys_decorators, { kind: "method", name: "addPubkeys", static: false, private: false, access: { has: obj => "addPubkeys" in obj, get: obj => obj.addPubkeys }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _removePubkeys_decorators, { kind: "method", name: "removePubkeys", static: false, private: false, access: { has: obj => "removePubkeys" in obj, get: obj => obj.removePubkeys }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _clearAllKeys_decorators, { kind: "method", name: "clearAllKeys", static: false, private: false, access: { has: obj => "clearAllKeys" in obj, get: obj => obj.clearAllKeys }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _hasCachedKey_decorators, { kind: "method", name: "hasCachedKey", static: false, private: false, access: { has: obj => "hasCachedKey" in obj, get: obj => obj.hasCachedKey }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _getCachedKeys_decorators, { kind: "method", name: "getCachedKeys", static: false, private: false, access: { has: obj => "getCachedKeys" in obj, get: obj => obj.getCachedKeys }, metadata: _metadata }, null, _instanceExtraInitializers);
__esDecorate(_a, null, _isDuplicate_decorators, { kind: "method", name: "isDuplicate", static: false, private: false, access: { has: obj => "isDuplicate" in obj, get: obj => obj.isDuplicate }, metadata: _metadata }, null, _instanceExtraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
exports.KeysCacheSDK = KeysCacheSDK;
//# sourceMappingURL=keys-cache-sdk.js.map