@thirdweb-dev/wallets
Version:
<p align="center"> <br /> <a href="https://thirdweb.com"><img src="https://github.com/thirdweb-dev/js/blob/main/legacy_packages/sdk/logo.svg?raw=true" width="200" alt=""/></a> <br /> </p> <h1 align="center">thirdweb Wallet SDK</h1> <p align="center"> <a h
195 lines (184 loc) • 6.89 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var defineProperty = require('../../../../dist/defineProperty-9051a5d3.cjs.dev.js');
var connector = require('../../../../dist/connector-a63dd9e7.cjs.dev.js');
var evm_wallets_base_dist_thirdwebDevWalletsEvmWalletsBase = require('../../../../dist/base-23fd36a9.cjs.dev.js');
var walletIds = require('../../../../dist/walletIds-a0be5020.cjs.dev.js');
var wc = require('../../../../dist/wc-6f95118d.cjs.dev.js');
var getInjectedCryptoDefiWalletProvider = require('../../../../dist/getInjectedCryptoDefiWalletProvider-0c32c7a6.cjs.dev.js');
require('eventemitter3');
require('@thirdweb-dev/chains');
require('../../abstract/dist/thirdweb-dev-wallets-evm-wallets-abstract.cjs.dev.js');
require('ethers');
require('@thirdweb-dev/sdk');
require('../../../../dist/headers-2be346e5.cjs.dev.js');
require('../../../../dist/assertWindowEthereum-0709ee9e.cjs.dev.js');
/**
* Wallet interface to connect [Crypto.com Defi wallet](https://crypto.com/defi-wallet) extension or mobile app.
*
* @example
* ```ts
* import { CryptoDefiWallet } from "@thirdweb-dev/wallets";
*
* const wallet = new CryptoDefiWallet();
*
* wallet.connect();
* ```
*
* @wallet
*/
class CryptoDefiWallet extends evm_wallets_base_dist_thirdwebDevWalletsEvmWalletsBase.AbstractClientWallet {
/**
* @internal
*/
get walletName() {
return "Crypto Defi wallet";
}
/**
* Create instance of `CryptoDefiWallet`
*
* @param options - The `options` object contains the following properties:
* ### clientId (recommended)
*
* Provide `clientId` to use the thirdweb RPCs for given `chains`
*
* You can create a client ID for your application from [thirdweb dashboard](https://thirdweb.com/create-api-key).
*
* ### projectId (recommended)
*
* This is only relevant if you want to use [WalletConnect](https://walletconnect.com/) for connecting to Zerion wallet mobile app when MetaMask is not injected.
*
* This `projectId` can be obtained at [cloud.walletconnect.com](https://cloud.walletconnect.com/). It is highly recommended to use your own project id and only use the default one for testing purposes.
*
* ### chains (optional)
* Provide an array of chains you want to support.
*
* Must be an array of `Chain` objects, from the [`@thirdweb-dev/chains`](https://www.npmjs.com/package/\@thirdweb-dev/chains) package.
*
* Defaults to our [default chains](/react/react.thirdwebprovider#default-chains).
*
* ### dappMetadata (optional)
* Information about your app that the wallet will display when your app tries to connect to it.
*
* Must be an object containing `name`, `url`, and optionally `description` and `logoUrl` properties.
*
* ### qrcode
* Whether to display the Wallet Connect QR code Modal or not.
*
* Must be a `boolean`. Defaults to `true`.
*
* ### qrModalOptions
* WalletConnect's [options](https://docs.walletconnect.com/advanced/walletconnectmodal/options) to customize the QR Code Modal.
*
*/
constructor(options) {
super(CryptoDefiWallet.id, options);
this.isInjected = !!getInjectedCryptoDefiWalletProvider.getInjectedCryptoDefiWalletProvider();
}
async getConnector() {
if (!this.connector) {
// if Defi wallet is injected, use the injected connector
// otherwise, use the wallet connect connector for using the Defi app on mobile via QR code scan
if (this.isInjected) {
// import the connector dynamically
const {
CryptoDefiWalletConnector
} = await Promise.resolve().then(function () { return require('../../../connectors/crypto-defi-wallet/dist/thirdweb-dev-wallets-evm-connectors-crypto-defi-wallet.cjs.dev.js'); });
this.CryptoDefiWalletConnector = new CryptoDefiWalletConnector({
chains: this.chains,
connectorStorage: this.walletStorage,
options: {
shimDisconnect: true
}
});
this.connector = new connector.WagmiAdapter(this.CryptoDefiWalletConnector);
} else {
const {
WalletConnectConnector
} = await Promise.resolve().then(function () { return require('../../../connectors/wallet-connect/dist/thirdweb-dev-wallets-evm-connectors-wallet-connect.cjs.dev.js'); });
const walletConnectConnector = new WalletConnectConnector({
chains: this.chains,
options: {
projectId: this.options?.projectId || wc.TW_WC_PROJECT_ID,
// TODO,
storage: this.walletStorage,
qrcode: this.options?.qrcode,
dappMetadata: this.dappMetadata,
qrModalOptions: this.options?.qrModalOptions
}
});
walletConnectConnector.getProvider().then(provider => {
provider.signer.client.on("session_request_sent", () => {
this.emit("wc_session_request_sent");
});
});
// need to save this for getting the QR code URI
this.walletConnectConnector = walletConnectConnector;
this.connector = new connector.WagmiAdapter(walletConnectConnector);
}
}
return this.connector;
}
/**
* Connect to the wallet using a QR code.
* You can use this method to display a QR code. The user can scan this QR code using the Defi Wallet mobile app to connect to your dapp.
*
* @example
* ```typescript
* wallet.connectWithQrCode({
* chainId: 1,
* onQrCodeUri(qrCodeUri) {
* // render the QR code with `qrCodeUri`
* },
* onConnected(accountAddress) {
* // update UI to show connected state
* },
* })
* ```
*
* @param options -
* The options object contains the following properties/method:
*
* ### chainId (optional)
* If provided, wallet will prompt the user to switch to the network with the given `chainId` after connecting.
*
* ### onQrCodeUri
* A callback to get the QR code URI to display to the user.
*
* ### onConnected
* A callback that is called when the user has connected their wallet using the QR code.
*/
async connectWithQrCode(options) {
await this.getConnector();
const wcConnector = this.walletConnectConnector;
if (!wcConnector) {
throw new Error("WalletConnect connector not found");
}
const wcProvider = await wcConnector.getProvider();
// set a listener for display_uri event
wcProvider.on("display_uri", uri => {
options.onQrCodeUri(uri);
});
// trigger connect flow
this.connect({
chainId: options.chainId
}).then(options.onConnected);
}
}
/**
* @internal
*/
/**
* @internal
*/
/**
* @internal
*/
/**
* @internal
*/
/**
* @internal
*/
defineProperty._defineProperty(CryptoDefiWallet, "id", walletIds.walletIds.cryptoDefiWallet);
exports.CryptoDefiWallet = CryptoDefiWallet;