UNPKG

walletlink-e

Version:
74 lines (73 loc) 2.67 kB
"use strict"; // Copyright (c) 2018-2020 WalletLink.org <https://www.walletlink.org/> // Copyright (c) 2018-2020 Coinbase, Inc. <https://www.coinbase.com/> // Licensed under the Apache License, version 2.0 var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = void 0; const crypto_1 = __importDefault(require("crypto")); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const STORAGE_KEY_SESSION_ID = "session:id"; const STORAGE_KEY_SESSION_SECRET = "session:secret"; const STORAGE_KEY_SESSION_LINKED = "session:linked"; class Session { constructor(storage, id, secret, linked) { this._storage = storage; this._id = id || crypto_1.default.randomBytes(16).toString("hex"); this._secret = secret || crypto_1.default.randomBytes(32).toString("hex"); this._key = crypto_1.default .createHash("sha256") .update(`${this._id}, ${this._secret} WalletLink`, "ascii") .digest("hex"); this._linked = !!linked; } static load(storage) { const id = storage.getItem(STORAGE_KEY_SESSION_ID); const linked = storage.getItem(STORAGE_KEY_SESSION_LINKED); const secret = storage.getItem(STORAGE_KEY_SESSION_SECRET); if (id && secret) { return new Session(storage, id, secret, linked === "1"); } return null; } static clear(storage) { storage.removeItem(STORAGE_KEY_SESSION_SECRET); storage.removeItem(STORAGE_KEY_SESSION_ID); storage.removeItem(STORAGE_KEY_SESSION_LINKED); } static get persistedSessionIdChange$() { return rxjs_1.fromEvent(window, "storage").pipe(operators_1.filter(evt => evt.key === STORAGE_KEY_SESSION_ID), operators_1.map(evt => ({ oldValue: evt.oldValue || null, newValue: evt.newValue || null }))); } get id() { return this._id; } get secret() { return this._secret; } get key() { return this._key; } get linked() { return this._linked; } set linked(val) { this._linked = val; this.persistLinked(); } save() { this._storage.setItem(STORAGE_KEY_SESSION_ID, this._id); this._storage.setItem(STORAGE_KEY_SESSION_SECRET, this._secret); this.persistLinked(); return this; } persistLinked() { this._storage.setItem(STORAGE_KEY_SESSION_LINKED, this._linked ? "1" : "0"); } } exports.Session = Session;