UNPKG

@exromany/lido-csm-sdk

Version:

[![GitHub license](https://img.shields.io/github/license/lidofinance/lido-csm-sdk?color=limegreen)](https://github.com/lidofinance/lido-csm-sdk/blob/main/LICENSE.txt) [![Version npm](https://img.shields.io/npm/v/@lidofinance/lido-csm-sdk?label=version)](h

172 lines 10.5 kB
"use strict"; 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.DepositDataSDK = 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 index_js_2 = require("../common/index.js"); const index_js_3 = require("../common/utils/index.js"); const constants_js_1 = require("./constants.js"); const parser_js_1 = require("./parser.js"); const types_js_1 = require("./types.js"); const validator_js_1 = require("./validator.js"); let DepositDataSDK = (() => { var _a; let _classSuper = csm_sdk_module_js_1.CsmSDKModule; let _instanceExtraInitializers = []; let _parseDepositData_decorators; let _removeKey_decorators; let _validateDepositData_decorators; let _validateDepositDataSync_decorators; let _checkUploadedKeys_decorators; let _checkCachedKeys_decorators; let _checkClKeys_decorators; return _a = class DepositDataSDK extends _classSuper { parseDepositData(json) { return (0, parser_js_1.parseDepositData)(json); } removeKey(json, index) { return (0, parser_js_1.removeKey)(json, index); } async validateDepositData(depositData) { const chainId = this.core.chainId; const wc = this.core.getContractAddress(index_js_2.CONTRACT_NAMES.withdrawalVault); const blockNumber = await this.core.publicClient.getBlockNumber(); const errors = await (0, validator_js_1.validateDepositData)(depositData, { chainId, withdrawalCredentials: wc, currentBlockNumber: Number(blockNumber), }); const pubkeys = depositData.map((data) => data.pubkey); const duplicateErrors = this.checkCachedKeys(pubkeys); const uploadedDuplicateErrors = await this.checkUploadedKeys(pubkeys); const clErrors = await this.checkClKeys(pubkeys.map(index_js_3.toHexString)); return [ ...errors, ...duplicateErrors, ...uploadedDuplicateErrors, ...clErrors, ]; } validateDepositDataSync(depositData) { const chainId = this.core.chainId; const wc = this.core.getContractAddress(index_js_2.CONTRACT_NAMES.withdrawalVault); return (0, validator_js_1.validateDepositDataSync)(depositData, { chainId, withdrawalCredentials: wc, }); } async checkUploadedKeys(pubkeys) { const keys = await this.bus.keysWithStatus?.getApiKeys(pubkeys); const errors = []; if (!keys) return errors; pubkeys.forEach((pubkey, index) => { const isUploaded = keys.find((key) => (0, index_js_3.compareLowercase)(key.key, pubkey)); if (isUploaded) { errors.push({ index, message: `pubkey was previously submitted`, field: 'pubkey', code: types_js_1.ValidationErrorCode.PREVIOUSLY_SUBMITTED, }); } }); return errors; } checkCachedKeys(pubkeys) { const keysCache = this.bus.keysCache; const errors = []; if (!keysCache) return errors; pubkeys.forEach((pubkey, index) => { if (keysCache.isDuplicate(pubkey)) { errors.push({ index, message: `pubkey already exists in cache`, field: 'pubkey', code: types_js_1.ValidationErrorCode.DUPLICATE_PUBKEY, }); } }); return errors; } async checkClKeys(pubkeys) { const validPubkeys = pubkeys.filter((p) => (0, index_js_3.isHexadecimalString)(p, constants_js_1.PUBKEY_LENGTH)); const clKeys = await this.bus.keysWithStatus?.getClKeys(validPubkeys); const errors = []; if (!clKeys) return errors; pubkeys.forEach((pubkey, index) => { const exists = clKeys.find((k) => (0, index_js_3.compareLowercase)(k.pubkey, pubkey)); if (exists) { errors.push({ index, message: `pubkey already exists as validator on CL`, field: 'pubkey', code: types_js_1.ValidationErrorCode.VALIDATOR_EXISTS, }); } }); return errors; } constructor() { super(...arguments); __runInitializers(this, _instanceExtraInitializers); } }, (() => { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0; _parseDepositData_decorators = [(0, index_js_1.Logger)('Utils:')]; _removeKey_decorators = [(0, index_js_1.Logger)('Utils:')]; _validateDepositData_decorators = [(0, index_js_1.Logger)('Utils:')]; _validateDepositDataSync_decorators = [(0, index_js_1.Logger)('Utils:')]; _checkUploadedKeys_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)(), (0, index_js_1.Cache)(index_js_2.CACHE_MID)]; _checkCachedKeys_decorators = [(0, index_js_1.Logger)('Utils:'), (0, index_js_1.ErrorHandler)()]; _checkClKeys_decorators = [(0, index_js_1.Logger)('API:'), (0, index_js_1.ErrorHandler)()]; __esDecorate(_a, null, _parseDepositData_decorators, { kind: "method", name: "parseDepositData", static: false, private: false, access: { has: obj => "parseDepositData" in obj, get: obj => obj.parseDepositData }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _removeKey_decorators, { kind: "method", name: "removeKey", static: false, private: false, access: { has: obj => "removeKey" in obj, get: obj => obj.removeKey }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _validateDepositData_decorators, { kind: "method", name: "validateDepositData", static: false, private: false, access: { has: obj => "validateDepositData" in obj, get: obj => obj.validateDepositData }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _validateDepositDataSync_decorators, { kind: "method", name: "validateDepositDataSync", static: false, private: false, access: { has: obj => "validateDepositDataSync" in obj, get: obj => obj.validateDepositDataSync }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _checkUploadedKeys_decorators, { kind: "method", name: "checkUploadedKeys", static: false, private: false, access: { has: obj => "checkUploadedKeys" in obj, get: obj => obj.checkUploadedKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _checkCachedKeys_decorators, { kind: "method", name: "checkCachedKeys", static: false, private: false, access: { has: obj => "checkCachedKeys" in obj, get: obj => obj.checkCachedKeys }, metadata: _metadata }, null, _instanceExtraInitializers); __esDecorate(_a, null, _checkClKeys_decorators, { kind: "method", name: "checkClKeys", static: false, private: false, access: { has: obj => "checkClKeys" in obj, get: obj => obj.checkClKeys }, metadata: _metadata }, null, _instanceExtraInitializers); if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); })(), _a; })(); exports.DepositDataSDK = DepositDataSDK; //# sourceMappingURL=deposit-data-sdk.js.map