@oasisprotocol/sapphire-wagmi-v2
Version:
Wagmi & Viem support for the Oasis Sapphire ParaTime.
130 lines • 4.62 kB
JavaScript
/**
* @license Apache-2.0
*/
import { NETWORKS, isWrappedEthereumProvider, wrapEthereumProvider, } from "@oasisprotocol/sapphire-paratime";
import { injected } from "@wagmi/core";
export * from "@oasisprotocol/sapphire-viem-v2";
const cachedProviders = new Map();
/**
* Wrap the `window.ethereum` with the Sapphire encryption layer.
* Used to provide encrypted transactions and calldata to Wagmi browser dApps.
*
* ```
* import { injectedWithSapphire } from '@oasisprotocol/sapphire-wagmi-v2';
*
* export const config = createConfig({
* connectors: [
* injectedWithSapphire()
* ],
* ...
* });
* ```
*
* @returns Same as injected()
*/
export function injectedWithSapphire(options) {
return injected({
target: () => {
return {
id: "injected-sapphire",
name: "Injected (Sapphire)",
provider(window) {
if (window?.ethereum) {
// Note: providers are cached as connectors are frequently retrieved
// it prevents sapphire wrapper being called multiple times
// which spams the RPC with oasis_callDataPublicKey requests
if (!cachedProviders.has(window.ethereum)) {
const wp = wrapEthereumProvider(window.ethereum, options);
cachedProviders.set(window.ethereum, wp);
return wp;
}
return cachedProviders.get(window.ethereum);
}
return undefined;
},
};
},
});
}
const SAPPHIRE_CHAIN_IDS = [
NETWORKS.mainnet.chainId,
NETWORKS.testnet.chainId,
NETWORKS.localnet.chainId,
NETWORKS.pontusXTestnet.chainId,
NETWORKS.pontusXDevnet.chainId,
];
/**
* Wrap any Wagmi connector with the Sapphire encryption layer.
* Used to provide encrypted transactions and calldata to any connector type (WalletConnect, MetaMask, etc.).
*
* ```typescript
* import { wrapConnectorWithSapphire } from '@oasisprotocol/sapphire-wagmi-v2';
* import { walletConnect } from '@wagmi/connectors';
*
* export const config = createConfig({
* connectors: [
* wrapConnectorWithSapphire(
* walletConnect({ projectId: 'your-project-id' }),
* {
* id: 'walletconnect-sapphire',
* name: 'WalletConnect (Sapphire)',
* }
* )
* ],
* ...
* });
* ```
*
* @returns A wrapped connector factory that provides Sapphire encryption
*/
export function wrapConnectorWithSapphire(connectorFactory, options) {
const cachedWrappedProviders = new Map();
return (...args) => {
const baseConnector = connectorFactory(...args);
if (options?.name) {
baseConnector.name = options.name;
}
if (options?.id) {
baseConnector.id = options.id;
}
if (options?.customWrapper) {
return options.customWrapper(baseConnector);
}
const originalGetProvider = baseConnector.getProvider?.bind(baseConnector);
if (originalGetProvider) {
baseConnector.getProvider = async () => {
const provider = await originalGetProvider();
if (!provider) {
return provider;
}
let chainId = null;
if (provider.isWalletConnect) {
if (!provider.connected) {
return provider;
}
chainId = provider.chainId;
}
else {
const chainIdResponse = await provider?.request({
method: "eth_chainId",
});
chainId = Number.parseInt(chainIdResponse, 16);
}
if (!SAPPHIRE_CHAIN_IDS.includes(chainId)) {
return provider;
}
if (isWrappedEthereumProvider(provider)) {
return provider;
}
if (!cachedWrappedProviders.has(provider)) {
const wrappedProvider = wrapEthereumProvider(provider, options?.sapphireWrapConfig);
cachedWrappedProviders.set(provider, wrappedProvider);
return wrappedProvider;
}
return cachedWrappedProviders.get(provider);
};
}
return baseConnector;
};
}
//# sourceMappingURL=index.js.map