@exromany/lido-csm-sdk
Version:
[](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [](h
161 lines • 8.8 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;
};
import { CsmSDKModule } from '../common/class-primitives/csm-sdk-module.js';
import { Logger } from '../common/decorators/index.js';
import { normalizeTrimHex, toHexString, } from '../common/utils/is-hexadecimal-string.js';
import { cleanExpiredKeys, getFromLocalStorage, isKeyExpired, saveToLocalStorage, } from './storage.js';
let KeysCacheSDK = (() => {
var _a;
let _classSuper = 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() {
// FIXME: other value for CM? may be use module id?
return `lido-csm-keys-cache-${this.core.chainId}`;
}
/**
* Get all keys from storage for current chain
*/
getKeys() {
return getFromLocalStorage(this.storageKey) || {};
}
/**
* Save keys to storage for current chain
*/
setKeys(keys) {
saveToLocalStorage(this.storageKey, keys);
}
/**
* Add pubkeys to cache with current timestamp
* Automatically cleans expired keys
*/
addPubkeys(pubkeys) {
if (pubkeys.length === 0)
return;
const timestamp = Date.now();
const storedKeys = this.getKeys();
// Add new keys (normalized to lowercase for case-insensitive matching)
const updatedKeys = pubkeys.reduce((keys, pubkey) => {
const key = normalizeTrimHex(pubkey);
keys[key] = timestamp;
return keys;
}, { ...storedKeys });
// Clean expired keys and save
const cleanedKeys = cleanExpiredKeys(updatedKeys);
this.setKeys(cleanedKeys);
}
/**
* Remove specific pubkeys from cache
* Automatically cleans expired keys
*/
removePubkeys(pubkeys) {
if (pubkeys.length === 0)
return;
const storedKeys = this.getKeys();
// Remove specified keys (normalized for case-insensitive matching)
const updatedKeys = { ...storedKeys };
pubkeys.forEach((pubkey) => {
const key = normalizeTrimHex(pubkey);
delete updatedKeys[key];
});
// Clean expired keys and save
const cleanedKeys = cleanExpiredKeys(updatedKeys);
this.setKeys(cleanedKeys);
}
/**
* Clear all cached keys for current chain
*/
clearAllKeys() {
this.setKeys({});
}
/**
* Check if a specific pubkey exists in cache and is not expired
*/
hasCachedKey(pubkey) {
const key = normalizeTrimHex(pubkey);
const storedKeys = this.getKeys();
const timestamp = storedKeys[key];
if (!timestamp)
return false;
return !isKeyExpired(timestamp);
}
/**
* Get all valid (non-expired) cached keys
* Automatically cleans expired keys during retrieval
*/
getCachedKeys() {
const storedKeys = this.getKeys();
const cleanedKeys = cleanExpiredKeys(storedKeys);
// Update storage to remove expired keys
this.setKeys(cleanedKeys);
return Object.keys(cleanedKeys).map(toHexString);
}
/**
* Check if pubkey would be a duplicate (already exists in cache)
*/
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 = [Logger('Cache:')];
_removePubkeys_decorators = [Logger('Cache:')];
_clearAllKeys_decorators = [Logger('Cache:')];
_hasCachedKey_decorators = [Logger('Cache:')];
_getCachedKeys_decorators = [Logger('Cache:')];
_isDuplicate_decorators = [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;
})();
export { KeysCacheSDK };
//# sourceMappingURL=keys-cache-sdk.js.map