diff --git a/.github/workflows/publish-wallet-kit.yaml b/.github/workflows/publish-wallet-kit.yaml index 257fc4bb..120e091f 100644 --- a/.github/workflows/publish-wallet-kit.yaml +++ b/.github/workflows/publish-wallet-kit.yaml @@ -17,7 +17,7 @@ jobs: packages: write strategy: matrix: - package: [ wallet-kit,react-wallet-kit,wallet-connect,vanilla-wallet-kit ] + package: [ wallet-kit,react-wallet-kit,vanilla-wallet-kit ] steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/apps/sample-react-app/package.json b/apps/sample-react-app/package.json index 004fbfa1..b1408c86 100644 --- a/apps/sample-react-app/package.json +++ b/apps/sample-react-app/package.json @@ -42,7 +42,6 @@ "@vechain/web3-providers-connex": "^1.1.2", "@vechainfoundation/react-wallet-kit": "*", "@vechainfoundation/vanilla-wallet-kit": "*", - "@vechainfoundation/wallet-connect": "*", "@vechainfoundation/wallet-kit": "*", "buffer": "^6.0.3", "crypto-browserify": "^3.12.0", diff --git a/apps/sample-react-app/src/App.tsx b/apps/sample-react-app/src/App.tsx index e001d0c9..956397e6 100644 --- a/apps/sample-react-app/src/App.tsx +++ b/apps/sample-react-app/src/App.tsx @@ -1,7 +1,7 @@ import type { JSX } from 'react'; import React from 'react'; import type { Options } from '@vechain/connex'; -import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect'; +import type { WalletConnectOptions } from '@vechainfoundation/wallet-kit'; import { ConnexProvider } from '@vechainfoundation/react-wallet-kit'; import { ChakraProvider } from '@chakra-ui/react'; import { NavBar, StyledContainer } from './Components/layout'; diff --git a/apps/sample-vanilla-app/index.js b/apps/sample-vanilla-app/index.js index 903e15db..450f20b1 100644 --- a/apps/sample-vanilla-app/index.js +++ b/apps/sample-vanilla-app/index.js @@ -1,6 +1,6 @@ // eslint-disable-next-line eslint-comments/disable-enable-pair /* eslint-disable no-undef */ -import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit'; +import { DAppKit } from '@vechainfoundation/vanilla-wallet-kit'; const walletConnectOptions = { projectId: 'a0b855ceaf109dbc8426479a4c3d38d8', @@ -18,4 +18,7 @@ const vechainWalletKitOptions = { walletConnectOptions, }; -configureThorModal(vechainWalletKitOptions); +const connex = DAppKit.configure(vechainWalletKitOptions); + +// eslint-disable-next-line no-console +console.log(`Connex configured`, connex.thor.genesis.id); diff --git a/apps/sample-vanilla-app/package.json b/apps/sample-vanilla-app/package.json index cec15176..f6fe0b00 100644 --- a/apps/sample-vanilla-app/package.json +++ b/apps/sample-vanilla-app/package.json @@ -8,7 +8,7 @@ "scripts": { "build": "parcel build index.html --public-url /vechain-dapp-kit/vanilla", "clean": "rm -rf dist", - "dev": "parcel index.html --open --port 3001", + "dev": "parcel index.html --open --no-cache --port 3001", "purge": "yarn clean && rm -rf node_modules", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/apps/sample-vue-app/src/connex/ConnexProvider.vue b/apps/sample-vue-app/src/connex/ConnexProvider.vue index e5cae1fc..ed1d5b84 100644 --- a/apps/sample-vue-app/src/connex/ConnexProvider.vue +++ b/apps/sample-vue-app/src/connex/ConnexProvider.vue @@ -2,7 +2,7 @@ import { defineComponent, provide, reactive, readonly, toRefs } from 'vue'; import { ConnectResponse, - MultiWalletConnex, + WalletConnectOptions, WalletSource, } from '@vechainfoundation/wallet-kit'; import type Connex from '@vechain/connex'; @@ -12,8 +12,7 @@ import { WalletStateSymbol, } from '@/connex/keys'; import { WalletActions, WalletState } from '@/connex/types'; -import { WalletConnectOptions } from '@vechainfoundation/wallet-connect'; -import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit'; +import { DAppKit } from '@vechainfoundation/vanilla-wallet-kit'; const initWallets = (hasWcOptions: boolean) => { const wallets: WalletSource[] = ['sync2']; @@ -51,7 +50,7 @@ export default defineComponent({ account: null, }); - const connex = new MultiWalletConnex({ + const connex = DAppKit.configure({ nodeUrl: 'https://mainnet.vechain.org/', walletConnectOptions, }); @@ -68,8 +67,6 @@ export default defineComponent({ connex.wallet.onDisconnected(onDisconnected); connex.wallet.onSourceChanged(onSourceChanged); - configureThorModal(connex); - const setAccount = (addr: string) => { walletState.account = addr; }; diff --git a/packages/react-wallet-kit/README.md b/packages/react-wallet-kit/README.md index dab5ae3f..e573cc68 100644 --- a/packages/react-wallet-kit/README.md +++ b/packages/react-wallet-kit/README.md @@ -37,7 +37,7 @@ const nodeOptions: Omit = { - Optional: Configure wallet connect options ```typescript -import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect'; +import type { WalletConnectOptions } from '@vechainfoundation/wallet-kit'; const walletConnectOptions: WalletConnectOptions = { projectId: '', // Create your project here: https://cloud.walletconnect.com/sign-up diff --git a/packages/react-wallet-kit/package.json b/packages/react-wallet-kit/package.json index 29695986..0d885496 100644 --- a/packages/react-wallet-kit/package.json +++ b/packages/react-wallet-kit/package.json @@ -20,7 +20,6 @@ "@vechain/connex-framework": "2.1.0", "@vechain/picasso": "^2.1.1", "@vechainfoundation/vanilla-wallet-kit": "*", - "@vechainfoundation/wallet-connect": "*", "@vechainfoundation/wallet-kit": "*", "react-icons": "^4.11.0", "styled-components": "^6.1.0", diff --git a/packages/react-wallet-kit/src/ConnectWalletButton/Components/ConnectButtonWithModal.tsx b/packages/react-wallet-kit/src/ConnectWalletButton/Components/ConnectButtonWithModal.tsx index be192aec..0b02dff7 100644 --- a/packages/react-wallet-kit/src/ConnectWalletButton/Components/ConnectButtonWithModal.tsx +++ b/packages/react-wallet-kit/src/ConnectWalletButton/Components/ConnectButtonWithModal.tsx @@ -14,7 +14,7 @@ export const ConnectButtonWithModal = ({ }: ConnectButtonWithModalProps) => { const { theme } = useContext(ThemeContext); - const handleSourceClick = (e: SourceInfo) => { + const handleSourceClick = (e: SourceInfo): void => { _connect(e.id); }; diff --git a/packages/react-wallet-kit/src/ConnectWalletButton/ConnectWalletButton.tsx b/packages/react-wallet-kit/src/ConnectWalletButton/ConnectWalletButton.tsx index 90fc6141..0fbae4b6 100644 --- a/packages/react-wallet-kit/src/ConnectWalletButton/ConnectWalletButton.tsx +++ b/packages/react-wallet-kit/src/ConnectWalletButton/ConnectWalletButton.tsx @@ -1,9 +1,8 @@ import type { HTMLChakraProps } from '@chakra-ui/react'; import React from 'react'; - import GlobalFonts from '../../assets/fonts/fonts'; -import { ThemeSelector } from './Components/ThemeSelector'; import { ThemeProvider } from '../provider/ThemeProvider'; +import { ThemeSelector } from './Components/ThemeSelector'; import { ConnectButtonWithModal } from './Components/ConnectButtonWithModal'; interface ConnectWalletButtonProps { diff --git a/packages/react-wallet-kit/src/types.ts b/packages/react-wallet-kit/src/types.ts index 319cf6ec..56291b34 100644 --- a/packages/react-wallet-kit/src/types.ts +++ b/packages/react-wallet-kit/src/types.ts @@ -1,8 +1,8 @@ import type { Options } from '@vechain/connex'; import type React from 'react'; -import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect'; import type { ConnectResponse, + WalletConnectOptions, WalletSource, } from '@vechainfoundation/wallet-kit'; diff --git a/packages/vanilla-wallet-kit/README.md b/packages/vanilla-wallet-kit/README.md index 5c5bdc74..cf383f58 100644 --- a/packages/vanilla-wallet-kit/README.md +++ b/packages/vanilla-wallet-kit/README.md @@ -27,7 +27,7 @@ yarn add @vechainfoundation/vanilla-wallet-kit - In your root `index.ts` ```typescript -import type { WalletConnectOptions } from '@vechainfoundation/wallet-connect'; +import type { WalletConnectOptions } from '@vechainfoundation/wallet-kit'; import type { Options } from '@vechain/connex'; import { configureThorModal } from '@vechainfoundation/vanilla-wallet-kit'; diff --git a/packages/vanilla-wallet-kit/index.js b/packages/vanilla-wallet-kit/index.js index 4e51aff2..a0b6578f 100644 --- a/packages/vanilla-wallet-kit/index.js +++ b/packages/vanilla-wallet-kit/index.js @@ -1,6 +1,6 @@ // eslint-disable-next-line eslint-comments/disable-enable-pair /* eslint-disable no-undef */ -import { configureThorModal } from './dist'; +import { DAppKit } from './dist'; const walletConnectOptions = { projectId: 'a0b855ceaf109dbc8426479a4c3d38d8', @@ -18,4 +18,4 @@ const vechainWalletKitOptions = { walletConnectOptions, }; -configureThorModal(vechainWalletKitOptions); +DAppKit.configure(vechainWalletKitOptions); diff --git a/packages/vanilla-wallet-kit/package.json b/packages/vanilla-wallet-kit/package.json index ac01a9f6..5e6fcbe6 100644 --- a/packages/vanilla-wallet-kit/package.json +++ b/packages/vanilla-wallet-kit/package.json @@ -17,14 +17,13 @@ "scripts": { "build": "tsup", "clean": "rm -rf dist .turbo", - "dev": "parcel index.html", + "dev": "parcel --no-cache index.html", "format": "prettier \"**/*.{cjs,html,js,json,md,ts}\" --ignore-path ./.eslintignore --write", "lint": "eslint 'src/**/*.ts'", "purge": "yarn clean && rm -rf node_modules", "watch": "tsup --watch" }, "dependencies": { - "@vechainfoundation/wallet-connect": "*", "@vechainfoundation/wallet-kit": "*", "@wagmi/core": "^1.4.5", "@web3modal/ethereum": "^2.7.1", diff --git a/packages/vanilla-wallet-kit/src/client.ts b/packages/vanilla-wallet-kit/src/client.ts index 7f3ceb01..95b6e19d 100644 --- a/packages/vanilla-wallet-kit/src/client.ts +++ b/packages/vanilla-wallet-kit/src/client.ts @@ -1,94 +1,33 @@ -import type { WalletSource } from '@vechainfoundation/wallet-kit'; +import type { ConnexOptions } from '@vechainfoundation/wallet-kit'; import { MultiWalletConnex } from '@vechainfoundation/wallet-kit'; -import type { ConnexOptions } from '@vechainfoundation/wallet-kit/src'; -import type { WCModal } from '@vechainfoundation/wallet-connect'; -import type { OpenOptions } from '@vechainfoundation/wallet-connect/src/types'; -import './components'; -import type { SourceInfo } from './constants'; +import { CustomWalletConnectModal } from './modal'; -export type VechainWalletKitOptions = MultiWalletConnex | ConnexOptions; +let connex: MultiWalletConnex | null = null; -class CustomWalletConnectModal implements WCModal { - openModal(options: OpenOptions): Promise { - const { uri } = options; - dispatchEvent(new CustomEvent('vwk-open-wc-modal', { detail: uri })); - return Promise.resolve(); - } - closeModal(): void { - // NOT USED because it is controlled from inside the component - // dispatchEvent(new CustomEvent('vwk-close-wc-modal')); - } - subscribeModal() { - // NOT USED because it is controlled from inside the component - // eslint-disable-next-line @typescript-eslint/no-empty-function - return (): void => {}; - } -} - -class VechainWalletKit { - connex: MultiWalletConnex; - account: string | null = null; - - constructor(options: VechainWalletKitOptions) { - if ('thor' in options) { - this.connex = options; - } else { - this.connex = new MultiWalletConnex( - this.addCustomWalletConnectModalIfNotPresent(options), - ); +const DAppKit = { + configure(options: ConnexOptions): MultiWalletConnex { + if (!options.customWcModal) { + options.customWcModal = CustomWalletConnectModal.getInstance(); } - } - - addCustomWalletConnectModalIfNotPresent( - options: ConnexOptions, - ): ConnexOptions { - return { - ...options, - customWcModal: - options.customWcModal || new CustomWalletConnectModal(), - }; - } - setSource = (wallet: WalletSource): void => { - this.connex.wallet.setSource(wallet); - }; -} + connex = new MultiWalletConnex(options); -class VechainWalletKitModal { - public walletKit: VechainWalletKit; + return connex; + }, - constructor(walletKit: VechainWalletKit) { - this.walletKit = walletKit; - } + get connex(): MultiWalletConnex { + if (!connex) { + // eslint-disable-next-line no-console + console.error('🚨🚨🚨 DAppKit not configured 🚨🚨🚨'); + throw new Error('DAppKit not configured'); + } - initModalListeners(): void { - addEventListener('vwk-source-card-clicked', (event) => { - const source = (event as CustomEvent).detail as SourceInfo; - this.walletKit.setSource(source.id); - this.walletKit.connex.wallet - .connect() - .then(({ account }) => { - this.walletKit.account = account; - }) - .catch((error) => { - // eslint-disable-next-line no-console - console.error(error); - }); - }); - const disconnect = (): void => { - this.walletKit.connex.wallet.disconnect().catch((error) => { - // eslint-disable-next-line no-console - console.error(error); - }); - }; - addEventListener('vwk-close-wc-modal', disconnect); - } -} + return connex; + }, -export const configureThorModal = ( - walletKitOptions: VechainWalletKitOptions, -): void => { - const vechainWalletKit = new VechainWalletKit(walletKitOptions); - const vechainWalletKitModal = new VechainWalletKitModal(vechainWalletKit); - vechainWalletKitModal.initModalListeners(); + get modal(): CustomWalletConnectModal { + return CustomWalletConnectModal.getInstance(); + }, }; + +export { DAppKit }; diff --git a/packages/vanilla-wallet-kit/src/components/vwk-connect-button-with-modal/index.ts b/packages/vanilla-wallet-kit/src/components/vwk-connect-button-with-modal/index.ts index 54a7cc3a..5079763f 100644 --- a/packages/vanilla-wallet-kit/src/components/vwk-connect-button-with-modal/index.ts +++ b/packages/vanilla-wallet-kit/src/components/vwk-connect-button-with-modal/index.ts @@ -2,7 +2,8 @@ import type { TemplateResult } from 'lit'; import { html, LitElement } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { Theme, ThemeMode } from '@vechainfoundation/wallet-kit'; -import { SourceInfo } from '../../constants'; +import type { SourceInfo } from '../../constants'; +import { DAppKit } from '../../client'; @customElement('vwk-connect-button-with-modal') export class ConnectButtonWithModal extends LitElement { @@ -43,7 +44,9 @@ export class ConnectButtonWithModal extends LitElement { } private handleOpen = (): void => { - this.open = true; + DAppKit.connex.wallet.disconnect().finally(() => { + this.open = true; + }); }; private handleClose = (): void => { diff --git a/packages/vanilla-wallet-kit/src/components/vwk-connect-modal/index.ts b/packages/vanilla-wallet-kit/src/components/vwk-connect-modal/index.ts index ca980247..09843a0e 100644 --- a/packages/vanilla-wallet-kit/src/components/vwk-connect-modal/index.ts +++ b/packages/vanilla-wallet-kit/src/components/vwk-connect-modal/index.ts @@ -2,27 +2,23 @@ import type { TemplateResult } from 'lit'; import { css, html, LitElement, nothing } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { Theme, ThemeMode } from '@vechainfoundation/wallet-kit'; +import type { OpenOptions } from '@vechainfoundation/wallet-kit/src'; import { + DarkChevronLeftSvg, DarkCloseSvg, - LightCloseSvg, LightChevronLeftSvg, - DarkChevronLeftSvg, + LightCloseSvg, } from '../../assets'; import type { SourceInfo } from '../../constants'; import { Colors, WalletSources } from '../../constants'; +import { + dispatchCustomEvent, + subscribeToCustomEvent, +} from '../../utils/events'; +import { DAppKit } from '../../client'; @customElement('vwk-connect-modal') export class ConnectModal extends LitElement { - constructor() { - super(); - addEventListener('vwk-open-wc-modal', (event) => { - const uri = (event as CustomEvent).detail as string; - this.walletConnectQRcode = uri; - }); - addEventListener('vwk-close-wc-modal', () => { - this.walletConnectQRcode = undefined; - }); - } static override styles = css` .modal-container { padding: 20px; @@ -42,6 +38,14 @@ export class ConnectModal extends LitElement { transition: width 2s, height 4s; } + .modal-footer { + display: flex; + justify-content: flex-end; + align-items: center; + padding-top: 10px; + font-family: 'Inter', sans-serif; + } + .icon { cursor: pointer; width: 25px; @@ -53,11 +57,34 @@ export class ConnectModal extends LitElement { .icon.LIGHT:hover { background-color: ${Colors.LightGrey}; } + .icon.DARK:hover { background-color: ${Colors.DarkGrey}; } - `; + button { + cursor: pointer; + display: block; + border: none; + border-radius: 12px; + padding: 8px 12px; + font-family: 'Inter', sans-serif; + } + + button:hover { + opacity: 0.9; + } + + button.LIGHT { + background-color: ${Colors.LightGrey}; + color: ${Colors.Dark}; + } + + button.DARK { + background-color: ${Colors.Dark}; + color: ${Colors.LightGrey}; + } + `; @property({ type: Boolean }) open = false; @property({ type: Function }) @@ -68,30 +95,46 @@ export class ConnectModal extends LitElement { theme = Theme.Default; @property() walletConnectQRcode?: string = undefined; + + constructor() { + super(); + + subscribeToCustomEvent('vwk-open-wc-modal', (options: OpenOptions) => { + this.open = true; + this.walletConnectQRcode = options.uri; + }); + + subscribeToCustomEvent('vwk-close-wc-modal', () => { + this.open = false; + this.walletConnectQRcode = undefined; + }); + } + + private get availableSources(): SourceInfo[] { + const availableSources = DAppKit.connex.wallet.getAvailableSources(); + + return WalletSources.filter((src) => { + return availableSources.includes(src.id); + }); + } + @property({ type: Function }) onClose: () => void = () => nothing; - private onBack = (): void => { - dispatchEvent(new CustomEvent('vwk-close-wc-modal')); - }; - private handleClose = (): void => { - this.onBack(); - this.onClose(); - }; - override render(): TemplateResult { return html` - - + -