UNPKG

@signumjs/wallets

Version:

Wallets communication package for DApps in the Signum Network

70 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WalletConnection = void 0; const messaging_1 = require("./messaging"); class WalletConnection { accountId; publicKey; watchOnly; availableNodeHosts; currentNodeHost; adapter; notificationListener; constructor(accountId, publicKey, watchOnly, availableNodeHosts, currentNodeHost, adapter) { this.accountId = accountId; this.publicKey = publicKey; this.watchOnly = watchOnly; this.availableNodeHosts = availableNodeHosts; this.currentNodeHost = currentNodeHost; this.adapter = adapter; } listen(notificationHandler) { this.notificationListener = this.adapter.onNotification((msg) => { const { onPermissionRemoved, onNetworkChanged, onAccountRemoved, onAccountChanged } = notificationHandler; const call = (fn, args = undefined) => fn && fn(args); switch (msg.type) { case messaging_1.ExtensionNotificationType.NetworkChanged: if (this.currentNodeHost !== msg.networkHost) { this.currentNodeHost = msg.networkHost; call(onNetworkChanged, { networkName: msg.networkName, networkHost: msg.networkHost, }); } break; case messaging_1.ExtensionNotificationType.AccountChanged: { if (this.accountId !== msg.accountId) { this.accountId = msg.accountId; this.publicKey = msg.accountPublicKey; this.watchOnly = msg.watchOnly; call(onAccountChanged, { accountId: msg.accountId, accountPublicKey: msg.accountPublicKey, watchOnly: msg.watchOnly }); } break; } case messaging_1.ExtensionNotificationType.PermissionRemoved: { if (window.location.origin === msg.url) { this.accountId = ''; this.publicKey = ''; this.availableNodeHosts = []; this.currentNodeHost = ''; this.watchOnly = false; call(onPermissionRemoved, { url: msg.url }); } break; } case messaging_1.ExtensionNotificationType.AccountRemoved: { if (this.accountId === msg.accountId) { this.accountId = ''; this.publicKey = ''; this.watchOnly = false; call(onAccountRemoved, { accountId: msg.accountId }); } break; } } }); return this.notificationListener; } } exports.WalletConnection = WalletConnection; //# sourceMappingURL=walletConnection.js.map