Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

157 create a better flow when a certificate is required for wallet connect #170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
07ef8bc
fix: fix opening logic
davidecarpini Jan 3, 2024
118aca4
fix: refactor of the dapp-kit-ui
davidecarpini Jan 3, 2024
005b59a
fix: fix conflicts
davidecarpini Jan 3, 2024
05e7312
fix: fix dispatch
davidecarpini Jan 3, 2024
ae34ebd
fix: styles
davidecarpini Jan 4, 2024
30d34ca
fix: add i18n and modalParent configuration
davidecarpini Jan 4, 2024
a4b8d89
fix: unit tests
davidecarpini Jan 4, 2024
5f9efa5
fix: react package
davidecarpini Jan 4, 2024
9afb2fc
fix: test js
davidecarpini Jan 5, 2024
989f554
fix: lint
davidecarpini Jan 5, 2024
9715d3e
fix: update all sample apps
davidecarpini Jan 5, 2024
d9096ef
feat: add onSourceClick configuration
davidecarpini Jan 5, 2024
95fdb19
fix: versions and imports
davidecarpini Jan 5, 2024
db55f0f
fix: rename components
davidecarpini Jan 8, 2024
2c7f40a
fix: readme
davidecarpini Jan 8, 2024
517d17d
fix: null coalish and theme variables
davidecarpini Jan 8, 2024
cda57ef
fix: add default i18n languages
davidecarpini Jan 8, 2024
d8f8c85
fix: minors
davidecarpini Jan 8, 2024
256e0d9
feat: return certificate for sync, sync2 and veworld
davidecarpini Jan 9, 2024
e074c68
feat: wip adding certificate request
davidecarpini Jan 9, 2024
e5e2026
feat: add signature of the connection certificate for wallet-connect …
davidecarpini Jan 10, 2024
55ece97
Merge branch 'main' into 157-create-a-better-flow-when-a-certificate-…
davidecarpini Jan 10, 2024
506b5b1
refactor: modal component
davidecarpini Jan 10, 2024
3ef307d
fix: refactor modal name
davidecarpini Jan 10, 2024
e42a180
refactor: dapp-kit
davidecarpini Jan 11, 2024
3105b65
fix: remove DEFAULT_SIGN_IN_MESSAGE
davidecarpini Jan 11, 2024
4d48e2b
Merge branch 'main' into 157-create-a-better-flow-when-a-certificate-…
davidecarpini Jan 11, 2024
71fc74f
fix: add loader
davidecarpini Jan 11, 2024
72f30b1
Merge branch '157-create-a-better-flow-when-a-certificate-is-required…
davidecarpini Jan 11, 2024
2656873
fix: add loader to other wallets
davidecarpini Jan 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/sample-angular-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class AppComponent implements OnInit {

handleConnected(DAppKitUI.wallet.state.address);

DAppKitUI.modal.onConnected(handleConnected);
DAppKitUI.modal.onConnectionStatusChange(handleConnected);
}
}

Expand Down
6 changes: 3 additions & 3 deletions apps/sample-next-app/src/app/pages/homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { WalletConnectOptions } from '@vechain/dapp-kit';

const Button = (): ReactElement => {
const { account } = useWallet();
const { open, onConnected } = useWalletModal();
const { open, onConnectionStatusChange } = useWalletModal();
const [buttonText, setButtonText] = useState('Connect Custom Button');

useEffect(() => {
Expand All @@ -29,8 +29,8 @@ const Button = (): ReactElement => {

handleConnected(account);

onConnected(handleConnected);
}, [account, onConnected]);
onConnectionStatusChange(handleConnected);
}, [account, onConnectionStatusChange]);

return (
<div className="container">
Expand Down
6 changes: 3 additions & 3 deletions apps/sample-react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useEffect, useState } from 'react';

function App() {
const { account } = useWallet();
const { open, onConnected } = useWalletModal();
const { open, onConnectionStatusChange } = useWalletModal();
const [buttonText, setButtonText] = useState('Connect Custom Button');

useEffect(() => {
Expand All @@ -25,8 +25,8 @@ function App() {

handleConnected(account);

onConnected(handleConnected);
}, [account, onConnected]);
onConnectionStatusChange(handleConnected);
}, [account, onConnectionStatusChange]);

return (
<div className="container">
Expand Down
2 changes: 1 addition & 1 deletion apps/sample-svelte-app/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ setTimeout(() => {

handleConnected(DAppKitUI.wallet.state.address);

DAppKitUI.modal.onConnected(handleConnected);
DAppKitUI.modal.onConnectionStatusChange(handleConnected);
}
}, 100);
2 changes: 1 addition & 1 deletion apps/sample-vanilla-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ const handleConnected = (address) => {

handleConnected(DAppKitUI.wallet.state.address);

DAppKitUI.modal.onConnected(handleConnected);
DAppKitUI.modal.onConnectionStatusChange(handleConnected);
2 changes: 1 addition & 1 deletion apps/sample-vue-app/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ setTimeout(() => {

handleConnected(DAppKitUI.wallet.state.address);

DAppKitUI.modal.onConnected(handleConnected);
DAppKitUI.modal.onConnectionStatusChange(handleConnected);
}
}, 100);

Expand Down
7 changes: 5 additions & 2 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
Expand All @@ -38,6 +39,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
Expand All @@ -51,6 +53,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
Expand Down Expand Up @@ -90,7 +93,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
}, []);
const onModalConnected = useCallback(
(callback: (address: string | null) => void) =>
DAppKitUI.modal.onConnected(callback),
DAppKitUI.modal.onConnectionStatusChange(callback),
[],
);

Expand All @@ -111,7 +114,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
modal: {
open: openModal,
close: closeModal,
onConnected: onModalConnected,
onConnectionStatusChange: onModalConnected,
},
};
}, [connex, account, source, closeModal, openModal, onModalConnected]);
Expand Down
4 changes: 3 additions & 1 deletion packages/dapp-kit-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface DAppKitContext {
modal: {
open: () => void;
close: () => void;
onConnected: (callback: (address: string | null) => void) => void;
onConnectionStatusChange: (
callback: (address: string | null) => void,
) => void;
};
}
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ const handleConnected = (address) => {

handleConnected(DAppKitUI.wallet.state.address);

DAppKitUI.modal.onConnected(handleConnected);
DAppKitUI.modal.onConnectionStatusChange(handleConnected);
19 changes: 15 additions & 4 deletions packages/dapp-kit-ui/src/assets/styles/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,42 @@ export const buttonStyle = css`
background: var(--vdk-color-light-primary, ${Colors.Light.Primary});
color: var(--vdk-color-light-tertiary, ${Colors.Light.Tertiary});
}
button.LIGHT:hover {
button.LIGHT:hover:not(:disabled) {
background: var(
--vdk-color-light-primary-hover,
${Colors.Light.PrimaryHover}
);
}
button.LIGHT:active {
button.LIGHT:active:not(:disabled) {
background: var(
--vdk-color-light-primary-active,
${Colors.Light.PrimaryActive}
);
}
button.LIGHT:disabled {
opacity: 0.8;
}

button.DARK {
background: var(--vdk-color-dark-primary, ${Colors.Dark.Primary});
color: var(--vdk-color-dark-tertiary, ${Colors.Dark.Tertiary});
}
button.DARK:hover {
button.DARK:hover:not(:disabled) {
background: var(
--vdk-color-dark-primary-hover,
${Colors.Dark.PrimaryHover}
);
}
button.DARK:active {
button.DARK:active:not(:disabled) {
background: var(
--vdk-color-dark-primary-active,
${Colors.Dark.PrimaryActive}
);
}

button.DARK:disabled {
opacity: 0.8;
}
`;

export const iconButtonStyle = css`
Expand Down Expand Up @@ -83,4 +90,8 @@ export const iconButtonStyle = css`
.icon-button.DARK:active {
background: var(--vdk-color-dark-primary, ${Colors.Dark.PrimaryActive});
}

button:disabled {
opacity: 0.8;
}
`;
1 change: 0 additions & 1 deletion packages/dapp-kit-ui/src/class/index.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/dapp-kit-ui/src/class/responsive.ts

This file was deleted.

72 changes: 72 additions & 0 deletions packages/dapp-kit-ui/src/classes/connect-modal-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { WalletManager, WalletSource } from '@vechain/dapp-kit';
import { DAppKitLogger } from '@vechain/dapp-kit';
import { subscribeKey } from 'valtio/vanilla/utils';
import { createModalIfNotPresent, dispatchCustomEvent } from '../utils';

export interface ConnectModalManagerOptions {
modalParent?: HTMLElement;
}

export class ConnectModalManager {
private static instance: ConnectModalManager | null = null;

private constructor(
private walletManager: WalletManager,
options?: ConnectModalManagerOptions,
) {
createModalIfNotPresent(options);
}

public static getInstance(
walletManager: WalletManager,
options?: ConnectModalManagerOptions,
): ConnectModalManager {
if (!ConnectModalManager.instance) {
ConnectModalManager.instance = new ConnectModalManager(
walletManager,
options,
);
}

return ConnectModalManager.instance;
}

open(): void {
DAppKitLogger.debug('ConnectModalManager', 'opening the modal');
dispatchCustomEvent('vdk-open-wallet-modal');
}

close(): void {
DAppKitLogger.debug('ConnectModalManager', 'closing the modal');
dispatchCustomEvent('vdk-close-wallet-modal');
}

closeWalletConnect(): void {
DAppKitLogger.debug('ConnectModalManager', 'closing wallet connect');
dispatchCustomEvent('vdk-close-wc-qrcode');
}

closeConnectionCertificateRequest(): void {
DAppKitLogger.debug(
'ConnectModalManager',
'closing connection certificate request',
);
dispatchCustomEvent('vdk-close-connection-certificate-request');
}

onConnectionStatusChange(
callback: (address: string | null) => void,
): () => void {
return subscribeKey(this.walletManager.state, 'address', (address) => {
callback(address);
});
}

onWalletSelected(
callback: (source: WalletSource | null) => void,
): () => void {
return subscribeKey(this.walletManager.state, 'source', (source) => {
callback(source);
});
}
}
85 changes: 85 additions & 0 deletions packages/dapp-kit-ui/src/classes/custom-wallet-connect-modal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { EventEmitter } from 'events';
import type {
OpenOptions,
SubscribeModalState,
WCModal,
} from '@vechain/dapp-kit';
import { DAppKitLogger } from '@vechain/dapp-kit';
import { dispatchCustomEvent, subscribeToCustomEvent } from '../utils';

const MODAL_STATE_EVENT = 'vdk-modal-state-change';

export class CustomWalletConnectModal implements WCModal {
private static instance: CustomWalletConnectModal | null = null;

private eventEmitter = new EventEmitter();

private constructor() {
subscribeToCustomEvent('vdk-close-wc-qrcode', () => {
this.updateModalState({ open: false });
});
subscribeToCustomEvent('vdk-open-wc-qrcode', () => {
this.updateModalState({ open: true });
});
}

static getInstance(): CustomWalletConnectModal {
if (!CustomWalletConnectModal.instance) {
CustomWalletConnectModal.instance = new CustomWalletConnectModal();
}

return CustomWalletConnectModal.instance;
}

/**
* WalletConnect
*/
openModal(options: OpenOptions): Promise<void> {
DAppKitLogger.debug('CustomWalletConnectModal', 'opening the wc modal');
dispatchCustomEvent('vdk-open-wc-qrcode', options);
return Promise.resolve();
}

closeModal(): void {
DAppKitLogger.debug('CustomWalletConnectModal', 'closing the modal');
dispatchCustomEvent('vdk-close-wc-qrcode');
}

askForConnectionCertificate(): void {
DAppKitLogger.debug(
'CustomWalletConnectModal',
'ask for connection certificate',
);
dispatchCustomEvent('vdk-close-wc-qrcode');
dispatchCustomEvent('vdk-request-connection-certificate');
}

onConnectionCertificateSigned(): void {
DAppKitLogger.debug(
'CustomWalletConnectModal',
'connection certificate signed',
);
dispatchCustomEvent('vdk-close-wallet-modal');
dispatchCustomEvent('vdk-close-wc-qrcode');
dispatchCustomEvent('vdk-close-connection-certificate-request');
}

subscribeModal(
callback: (newState: SubscribeModalState) => void,
): () => void {
DAppKitLogger.debug(
'CustomWalletConnectModal',
'subscribing to modal state',
);

this.eventEmitter.on(MODAL_STATE_EVENT, callback);

return () => {
this.eventEmitter.off(MODAL_STATE_EVENT, callback);
};
}

private updateModalState(state: SubscribeModalState): void {
this.eventEmitter.emit(MODAL_STATE_EVENT, state);
}
}
2 changes: 2 additions & 0 deletions packages/dapp-kit-ui/src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './connect-modal-manager';
export * from './custom-wallet-connect-modal';
Loading
Loading