@particle-network/wallet-plugin
Version:
198 lines (193 loc) • 7.12 kB
JavaScript
// src/index.ts
export * from "@particle-network/wallet";
// src/version.ts
var version = "2.1.1";
// src/wallet.ts
import { isEVMChain, isSolanaChain } from "@particle-network/connector-core";
import { createPlugin } from "@particle-network/plugin-core";
import {
walletEntryPlugin
} from "@particle-network/wallet";
// src/createSolanaWallet.ts
import { getChain, sendSolanaTransaction } from "@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 = getChain(chainId);
const module = await import("@solana/web3.js");
const { Connection } = module.default || module;
const connection = new Connection(chain.rpcUrls.default.http[0]);
return await 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 createPlugin((config) => ({
get id() {
return "wallet";
},
get name() {
return "Particle Wallet";
},
getPlugin() {
return 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" ? isEVMChain(chain) : 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" ? isEVMChain(chain) : 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);
}
}));
};
export {
version,
wallet
};
//# sourceMappingURL=index.mjs.map