UNPKG

@signumjs/wallets

Version:

Wallets communication package for DApps in the Signum Network

93 lines 4.13 kB
"use strict"; /** * Original work Copyright (c) 2022 Signum Network */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtensionWalletConnection = void 0; const messaging_1 = require("./messaging"); /** * Wallet Connection object returned from {@link ExtensionWallet.connect} * You can use this to listen to events in the wallet, i.e. network changes, or permission removals */ class ExtensionWalletConnection { accountId; publicKey; watchOnly; availableNodeHosts; currentNodeHost; adapter; notificationListener; /** * @param accountId The connected account * @param publicKey The accounts public key * @param watchOnly Indicates whether this account is watch only, or not. Watch Only accounts cannot sign messages. * @param availableNodeHosts The available nodeHosts for given network, which are registered in the wallet. * @param currentNodeHost The currently selected nodeHost in the wallet * @param adapter the extension adapter with its internal implementation */ 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; } /** * Listens to changes in the wallet * * Only one listener instance is allowed, previous listener will be removed/overwritten * * @param notificationHandler The object with the event handler * @return The listener instance, needed to unlisten */ 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.ExtensionWalletConnection = ExtensionWalletConnection; //# sourceMappingURL=extensionWalletConnection.js.map