UNPKG

@bsv/wallet-toolbox-client

Version:
163 lines 5.79 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.StorageReader = void 0; const sdk = __importStar(require("../sdk/index")); const utilityHelpers_1 = require("../utility/utilityHelpers"); const getSyncChunk_1 = require("./methods/getSyncChunk"); /** * The `StorageReader` abstract class is the base of the concrete wallet storage provider classes. * * It is the minimal interface required to read all wallet state records and is the base class for sync readers. * * The next class in the heirarchy is the `StorageReaderWriter` which supports sync readers and writers. * * The last class in the heirarchy is the `Storage` class which supports all active wallet operations. * * The ability to construct a properly configured instance of this class implies authentication. * As such there are no user specific authenticated access checks implied in the implementation of any of these methods. */ class StorageReader { get dbtype() { var _a; return (_a = this._settings) === null || _a === void 0 ? void 0 : _a.dbtype; } constructor(options) { this.chain = options.chain; } isAvailable() { return !!this._settings; } async makeAvailable() { if (this._settings) return this._settings; return (this._settings = await this.readSettings()); } getSettings() { if (!this._settings) throw new sdk.WERR_INVALID_OPERATION('must call "makeAvailable" before accessing "settings"'); return this._settings; } isStorageProvider() { return false; } async findUserByIdentityKey(key) { return (0, utilityHelpers_1.verifyOneOrNone)(await this.findUsers({ partial: { identityKey: key } })); } async getSyncChunk(args) { return (0, getSyncChunk_1.getSyncChunk)(this, args); } /** * Force dates to strings on SQLite and Date objects on MySQL * @param date * @returns */ validateEntityDate(date) { if (!this.dbtype) throw new sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first'); let r = this.validateDate(date); switch (this.dbtype) { case 'IndexedDB': case 'MySQL': break; case 'SQLite': r = r.toISOString(); break; default: throw new sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`); } return r; } /** * * @param date * @param useNowAsDefault if true and date is null or undefiend, set to current time. * @returns */ validateOptionalEntityDate(date, useNowAsDefault) { if (!this.dbtype) throw new sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first'); let r = this.validateOptionalDate(date); if (!r && useNowAsDefault) r = new Date(); switch (this.dbtype) { case 'IndexedDB': case 'MySQL': break; case 'SQLite': if (r) r = r.toISOString(); break; default: throw new sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`); } return r; } validateDate(date) { let r; if (date instanceof Date) r = date; else r = new Date(date); return r; } validateOptionalDate(date) { if (date === null || date === undefined) return undefined; return this.validateDate(date); } validateDateForWhere(date) { if (!this.dbtype) throw new sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first'); if (typeof date === 'number') date = (0, utilityHelpers_1.validateSecondsSinceEpoch)(date); const vdate = (0, utilityHelpers_1.verifyTruthy)(this.validateDate(date)); let r; switch (this.dbtype) { case 'IndexedDB': case 'MySQL': r = vdate; break; case 'SQLite': r = vdate.toISOString(); break; default: throw new sdk.WERR_INTERNAL(`Invalid dateScheme ${this.dbtype}`); } return r; } } exports.StorageReader = StorageReader; //# sourceMappingURL=StorageReader.js.map