UNPKG

@particle-network/wallet-plugin

Version:
224 lines (218 loc) 8.72 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { version: () => version, wallet: () => wallet }); module.exports = __toCommonJS(src_exports); __reExport(src_exports, require("@particle-network/wallet"), module.exports); // src/version.ts var version = "2.1.1"; // src/wallet.ts var import_connector_core2 = require("@particle-network/connector-core"); var import_plugin_core = require("@particle-network/plugin-core"); var import_wallet = require("@particle-network/wallet"); // src/createSolanaWallet.ts var import_connector_core = require("@particle-network/connector-core"); var createSolanaWallet = (provider, chainId) => { return { provider, publicKey: provider.publicKey, chainId: chainId || 101, on(event, listener) { provider.on(event, listener); return this; }, removeListener(event, listener) { provider.removeListener(event, listener); return this; }, async connect() { await provider.connect(); }, async signTransaction(transaction) { return await provider.signTransaction(transaction); }, async signAllTransactions(transactions) { return await provider.signAllTransactions(transactions); }, async signAndSendTransaction(transaction) { const chain = (0, import_connector_core.getChain)(chainId); const module2 = await import("@solana/web3.js"); const { Connection } = module2.default || module2; const connection = new Connection(chain.rpcUrls.default.http[0]); return await (0, import_connector_core.sendSolanaTransaction)(provider, transaction, connection); }, async signMessage(message) { return await provider.signMessage(message); }, async switchChain(chainId2) { throw new Error("Wallet not support switch chain."); } }; }; // src/wallet.ts var wallet = (parameters = {}) => { const { widgetIntegration, entryPosition, visible, preload, themeType, customStyle, walletUrl } = parameters; return (0, import_plugin_core.createPlugin)((config) => ({ get id() { return "wallet"; }, get name() { return "Particle Wallet"; }, getPlugin() { return import_wallet.walletEntryPlugin; }, async onConnect(connection) { var _a, _b, _c, _d, _e, _f, _g, _h; const connector = connection[0].connector; const provider = await connector.getProvider(); const walletCore = {}; if (connector.type === "particleAuth") { if (connector.chainType === "evm") { walletCore.ethereum = provider; } else { walletCore.solana = provider; } } else { if (connector.chainType === "evm") { walletCore.ethereum = provider; } else { if (((_b = (_a = this.getPlugin().walletCore) == null ? void 0 : _a.solana) == null ? void 0 : _b.provider) == provider) { walletCore.solana = (_c = this.getPlugin().walletCore) == null ? void 0 : _c.solana; } else { const chainId = await connector.getChainId(); walletCore.solana = createSolanaWallet(provider, chainId); } } } if (((_d = this.getPlugin().walletCore) == null ? void 0 : _d.ethereum) != walletCore.ethereum || ((_e = this.getPlugin().walletCore) == null ? void 0 : _e.solana) != walletCore.solana) { if (connector.type === "particleAuth") { const customEventHandler = async (messageType) => { var _a2, _b2, _c2; if (messageType === "open-account-and-security" /* OpenAccountAndSecurity */) { (_b2 = (_a2 = window.particle) == null ? void 0 : _a2._internal) == null ? void 0 : _b2.openAccountAndSecurity(); } else if (messageType === "get-user-info" /* GetUserInfo */) { return await ((_c2 = window.particle) == null ? void 0 : _c2._internal.getUserInfo()); } }; customEventHandler.type = "auth"; walletCore.customEventHandler = customEventHandler; } else if (provider.isPasskey && provider.getPasskeyOption) { const customEventHandler = async (messageType) => { if (messageType === "get-passkey-option" /* GetPasskeyOption */) { const option = await await provider.getPasskeyOption(); return option; } }; customEventHandler.type = "passkey"; walletCore.customEventHandler = customEventHandler; } const validChains = config.chains.filter( (chain) => connector.chainType === "evm" ? (0, import_connector_core2.isEVMChain)(chain) : (0, import_connector_core2.isSolanaChain)(chain) ); this.getPlugin().overrideWalletOption({ entryPosition, visible, preload, customStyle: { ...(_f = this.getPlugin().walletOptions) == null ? void 0 : _f.customStyle, supportChains: validChains.map((chain) => ({ id: chain.id, chainType: connector.chainType })) } }); this.getPlugin().setWalletCore(walletCore); this.getPlugin().walletEntryCreate(); } else { if (!this.getPlugin().walletCreated) { const validChains = config.chains.filter( (chain) => connector.chainType === "evm" ? (0, import_connector_core2.isEVMChain)(chain) : (0, import_connector_core2.isSolanaChain)(chain) ); this.getPlugin().overrideWalletOption({ customStyle: { ...(_h = (_g = this.getPlugin()) == null ? void 0 : _g.walletOptions) == null ? void 0 : _h.customStyle, supportChains: validChains.map((chain) => ({ id: chain.id, chainType: connector.chainType })) } }); this.getPlugin().walletEntryCreate(); } } }, async onDisconnect() { this.getPlugin().walletEntryDestroy(); }, getWalletIFrame(parameters2) { return this.getPlugin().getWalletIFrame(parameters2); }, openWallet(parameters2) { return this.getPlugin().openWallet(parameters2); }, destory() { this.getPlugin().destroy(); }, init(parameters2) { this.getPlugin().init(config.projectConfig, { entryPosition, visible, preload, ...parameters2, widgetIntegration, walletUrl, themeType: themeType != null ? themeType : parameters2 == null ? void 0 : parameters2.themeType, customStyle: { ...parameters2 == null ? void 0 : parameters2.customStyle, ...customStyle } }); }, overrideWalletOption(parameters2) { var _a, _b; const mergeOptions = { entryPosition, visible, preload, ...parameters2, widgetIntegration, themeType: themeType != null ? themeType : parameters2 == null ? void 0 : parameters2.themeType, customStyle: { ...parameters2 == null ? void 0 : parameters2.customStyle, ...customStyle, supportChains: ((_a = parameters2 == null ? void 0 : parameters2.customStyle) == null ? void 0 : _a.supportChains) || ((_b = this.getPlugin().walletOptions.customStyle) == null ? void 0 : _b.supportChains) } }; this.getPlugin().overrideWalletOption(mergeOptions); } })); }; //# sourceMappingURL=index.cjs.map