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

veworld cleanup and create new components #109

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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-react-app/src/Components/AccountDetailBody.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, HStack, Image, Text, VStack } from '@chakra-ui/react';
import React, { useMemo } from 'react';
import type { WalletSource } from '@vechainfoundation/dapp-kit';
import { WalletSources } from '../Constants';
import { WalletSources } from '@vechainfoundation/dapp-kit-ui';
import { AddressButton } from './AddressButton';

interface AccountDetailBodyProps {
Expand Down
30 changes: 0 additions & 30 deletions apps/sample-react-app/src/Constants/Constants.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/sample-react-app/src/Constants/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/sample-vue-app/src/connex/ConnexProvider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const initWallets = (hasWcOptions: boolean) => {
}

if (window.vechain) {
wallets.push('veworld-extension');
wallets.push('veworld');
}

if (hasWcOptions) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build:deps": "turbo build --no-daemon --filter='@vechainfoundation/*'",
"clean": "rm -rf .turbo .parcel-cache build && npx turbo@latest run clean",
"cucumber": "yarn && cucumber-js",
"dev": "turbo run dev",
"dev": "turbo run dev --filter='@vechainfoundation/*'",
"format": "prettier --write \"**/*.{ts,tsx,md,json,js,jsx}\"",
"install:all": "yarn && yarn run build:deps",
"lint": "turbo run lint",
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export const App = (): JSX.Element => {
import { useWallet, useConnex } from '@vechainfoundation/dapp-kit-react';
import type { WalletSource } from '@vechainfoundation/dapp-kit';

// type WalletSource = 'wallet-connect' | 'veworld-extension' | 'sync2' | 'sync';
const mySource: WalletSource = 'veworld-extension';
// type WalletSource = 'wallet-connect' | 'veworld' | 'sync2' | 'sync';
const mySource: WalletSource = 'veworld';

const { connect, setSource } = useWallet();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import React, { useMemo } from 'react';
import React, { useCallback, useMemo } from 'react';
import { createComponent } from '@lit/react';
import type { SourceInfo } from '@vechainfoundation/dapp-kit-ui';
import { ConnectModal } from '@vechainfoundation/dapp-kit-ui';
import type { ConnectResponse } from '@vechainfoundation/dapp-kit/src';
import { useWallet } from '../../ConnexProvider';

export const createButtonWithModal = () =>
const createButtonWithModal = () =>
createComponent({
tagName: 'vwk-connect-modal',
elementClass: ConnectModal,
react: React,
});

export const ConnectWalletModal: React.FC = () => {
interface ConnectWalletProps {
onConnectError?: (err: unknown) => void;
onConnected?: (res: ConnectResponse) => void;
}

/**
* ConnectWalletModal
*
* This component allows the user to select a wallet and then connect. The account address should be available after the connection is successful.
*/

export const ConnectWalletModal: React.FC<ConnectWalletProps> = ({
onConnectError,
onConnected,
}) => {
const Modal = useMemo(() => createButtonWithModal(), []);

return <Modal />;
const { setSource, connect } = useWallet();

const onSourceClick = useCallback(
(source?: SourceInfo) => {
if (source) {
setSource(source.id);
connect().then(onConnected).catch(onConnectError);
}
},
[onConnectError, onConnected, connect, setSource],
);

return <Modal onSourceClick={onSourceClick} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useCallback, useMemo } from 'react';
import { createComponent } from '@lit/react';
import type { SourceInfo } from '@vechainfoundation/dapp-kit-ui';
import { ConnectModal } from '@vechainfoundation/dapp-kit-ui';
import type { WalletSource } from '@vechainfoundation/dapp-kit/src';
import { useWallet } from '../../ConnexProvider';

const createButtonWithModal = () =>
createComponent({
tagName: 'vwk-connect-modal',
elementClass: ConnectModal,
react: React,
});

interface SelectWalletProps {
onSelected?: (source: WalletSource) => void;
}

/**
* SelectWalletModal
*
* This component is used to select the wallet source. It will not attempt to connect to the wallet
*/
export const SelectWalletModal: React.FC<SelectWalletProps> = ({
onSelected,
}) => {
const Modal = useMemo(() => createButtonWithModal(), []);

const { setSource } = useWallet();

const onSourceClick = useCallback(
(source?: SourceInfo) => {
if (source) {
setSource(source.id);
onSelected?.(source.id);
}
},
[onSelected, setSource],
);

return <Modal onSourceClick={onSourceClick} />;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SelectWalletModal';
1 change: 1 addition & 0 deletions packages/dapp-kit-react/src/Components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ConnectWalletButtonWithModal';
export * from './ConnectWalletModal';
export * from './SelectWalletModal';
28 changes: 13 additions & 15 deletions packages/dapp-kit-ui/src/components/vwk-connect-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,12 @@ export class ConnectModal extends LitElement {
private get availableSources(): SourceInfo[] {
const availableSources = DAppKit.connex.wallet.getAvailableSources();

return WalletSources.filter((src) => {
return availableSources.includes(src.id);
});
return availableSources.map((source) => WalletSources[source]);
}

@property({ type: Function })
onClose: () => void = () => nothing;

private onBack = (): void => {
this.walletConnectQRcode = undefined;
};

private handleClose = (): void => {
// this timeout is to avoid flickering on close modal animation when the user is in the wallet connect modal
setTimeout(() => {
this.onBack();
}, 500);
this.onClose();
};

override render(): TemplateResult {
return html`
<vwk-fonts></vwk-fonts>
Expand Down Expand Up @@ -190,6 +176,18 @@ export class ConnectModal extends LitElement {
</vwk-base-modal>
`;
}

private onBack = (): void => {
this.walletConnectQRcode = undefined;
};

private handleClose = (): void => {
// this timeout is to avoid flickering on close modal animation when the user is in the wallet connect modal
setTimeout(() => {
this.onBack();
}, 500);
this.onClose();
};
}

declare global {
Expand Down
30 changes: 12 additions & 18 deletions packages/dapp-kit-ui/src/constants/sources.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import type { WalletSource } from '@vechainfoundation/dapp-kit';
import { Sync2Logo, SyncLogo, VeWorldLogo, WalletConnectLogo } from '../assets';

enum WalletSource {
WalletConnect = 'wallet-connect',
VeWorldExtension = 'veworld-extension',
Sync2 = 'sync2',
Sync = 'sync',
}

export interface SourceInfo {
id: WalletSource;
name: string;
logo: string;
}

export const WalletSources: SourceInfo[] = [
{
id: WalletSource.WalletConnect,
export const WalletSources: Record<WalletSource, SourceInfo> = {
'wallet-connect': {
id: 'wallet-connect',
name: 'Wallet Connect',
logo: WalletConnectLogo,
},
{
id: WalletSource.VeWorldExtension,
name: 'VeWorld Extension',
veworld: {
id: 'veworld',
name: 'VeWorld',
logo: VeWorldLogo,
},
{
id: WalletSource.Sync,
sync: {
id: 'sync',
name: 'Sync',
logo: SyncLogo,
},
{
id: WalletSource.Sync2,
sync2: {
id: 'sync2',
name: 'Sync 2',
logo: Sync2Logo,
},
];
};
6 changes: 3 additions & 3 deletions packages/dapp-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ const { thor, vendor, wallet } = new MultiWalletConnex({
```typescript
import type { WalletSource } from '@vechainfoundation/dapp-kit';

// type WalletSource = 'wallet-connect' | 'veworld-extension' | 'sync2' | 'sync';
const mySource: WalletSource = 'veworld-extension';
// type WalletSource = 'wallet-connect' | 'veworld' | 'sync2' | 'sync';
const mySource: WalletSource = 'veworld';

wallet.setSource('veworld-extension');
wallet.setSource('veworld');
```

- Connect to the wallet. This will return the user's address
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/src/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const createWallet = ({

return new CertificateBasedWallet(convertVendorToSigner(vendor));
}
case 'veworld-extension': {
case 'veworld': {
if (!window.vechain) {
throw new Error('VeWorld Extension is not installed');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare global {
}
}

type WalletSource = 'wallet-connect' | 'veworld-extension' | 'sync2' | 'sync';
type WalletSource = 'wallet-connect' | 'veworld' | 'sync2' | 'sync';

interface WalletConfig {
requiresCertificate: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class WalletManager {
throw new Error('WalletConnect options are not provided');
}

if (src === 'veworld-extension' && !window.vechain) {
if (src === 'veworld' && !window.vechain) {
throw new Error('VeWorld Extension is not installed');
}

Expand All @@ -102,7 +102,7 @@ class WalletManager {
const wallets: WalletSource[] = ['sync2'];

if (window.vechain) {
wallets.push('veworld-extension');
wallets.push('veworld');
}

if (window.connex) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const WalletMapping: Record<WalletSource, WalletConfig> = {
'wallet-connect': {
requiresCertificate: false,
},
'veworld-extension': DEFAULT_CONFIG,
veworld: DEFAULT_CONFIG,
sync2: DEFAULT_CONFIG,
sync: DEFAULT_CONFIG,
};
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp-kit/test/create-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ describe('createWallet', () => {
});
});

describe('veworld-extension', () => {
describe('veworld', () => {
it('is not installed', () => {
window.vechain = undefined;

expect(() => {
createWallet(createOptions('veworld-extension'));
createWallet(createOptions('veworld'));
}).toThrowError('VeWorld Extension is not installed');
});

Expand All @@ -56,7 +56,7 @@ describe('createWallet', () => {
newConnexSigner: () => ({} as Connex.Signer),
};

const wallet = createWallet(createOptions('veworld-extension'));
const wallet = createWallet(createOptions('veworld'));

expect(wallet).toBeDefined();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/dapp-kit/test/wallet-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('WalletManager', () => {
describe('signTx', () => {
it('should sign the tx', async () => {
const walletManager = newWalletManager();
walletManager.setSource('veworld-extension');
walletManager.setSource('veworld');
const res = await walletManager.signTx([], {});

expect(res.txid).toBeDefined();
Expand All @@ -55,7 +55,7 @@ describe('WalletManager', () => {
describe('signCert', () => {
it('should sign the cert', async () => {
const walletManager = newWalletManager();
walletManager.setSource('veworld-extension');
walletManager.setSource('veworld');
const res = await walletManager.signCert(
{
payload: { content: 'Hello world', type: 'text' },
Expand All @@ -80,7 +80,7 @@ describe('WalletManager', () => {
it('from remote', async () => {
const walletManager = newWalletManager();

walletManager.setSource('veworld-extension');
walletManager.setSource('veworld');

await walletManager.disconnect(true);

Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/test/wallet-tests/sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('sync', () => {
);
});

it('get available sources - should not include veworld-extension', () => {
it('get available sources - should not include veworld', () => {
const connex = createUnitTestConnex();

const sources = connex.wallet.getAvailableSources();
Expand Down
Loading
Loading