Skip to content

Commit

Permalink
refactor: dapp-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini committed Jan 11, 2024
1 parent 3ef307d commit e42a180
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 88 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="@vechain/connex" />
import type { DAppKitOptions, WalletManager } from '@vechain/dapp-kit';
import { DAppKit } from '@vechain/dapp-kit';
import { CustomWalletConnectModal, ConnectModalManager } from './class';
import { CustomWalletConnectModal, ConnectModalManager } from './classes';
import {
type CustomizedStyle,
dispatchCustomEvent,
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './client';
export * from './components';
export * from './constants';
export * from './utils';
export * from './class';
export * from './classes';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Certificate } from 'thor-devkit';
import type { BaseWallet, ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../certificates';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../constants';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
Expand Down
3 changes: 3 additions & 0 deletions packages/dapp-kit/src/classes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './wallet-manager';
export * from './certificate-wallet';
export * from './wc-wallet';
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import type {
DAppKitOptions,
WalletManagerState,
WalletSource,
} from './types';
import { createWallet } from './create-wallet';
import { WalletSources } from './wallet';
import { Storage } from './local-storage';
import { DAppKitLogger } from './utils';
import { DEFAULT_CONNECT_CERT_MESSAGE } from './certificates';
} from '../types';
import { DAppKitLogger, Storage, createWallet } from '../utils';
import { DEFAULT_CONNECT_CERT_MESSAGE, WalletSources } from '../constants';

class WalletManager {
public readonly state: WalletManagerState;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { WCSigner } from '../wallet-connect';
import type { ConnectResponse, ConnexWallet } from '../types';
import type { ConnectResponse, ConnexWallet, WCSigner } from '../types';

class WCWallet implements ConnexWallet {
constructor(private readonly signer: WCSigner) {}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Genesis } from './types';

type NetworkType = 'main' | 'test';

export const genesisBlocks: Record<NetworkType, Connex.Thor.Block> = {
Expand Down Expand Up @@ -48,22 +46,3 @@ export const genesisBlocks: Record<NetworkType, Connex.Thor.Block> = {
transactions: [],
},
};

const normalizeGenesisId = (genesis?: Genesis): string => {
if (!genesis) return genesisBlocks.main.id;

if (genesis === 'main' || genesis === 'test')
return genesisBlocks[genesis].id;

return genesis.id;
};

const normalizeGenesisBlock = (genesis?: Genesis): Connex.Thor.Block => {
if (!genesis) return genesisBlocks.main;

if (genesis === 'main' || genesis === 'test') return genesisBlocks[genesis];

return genesis;
};

export { normalizeGenesisId, normalizeGenesisBlock };
4 changes: 4 additions & 0 deletions packages/dapp-kit/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './certificates';
export * from './genesis-blocks';
export * from './wallet-settings';
export * from './wallet-connect';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Wallet types
*/

import type { WalletConfig, WalletSource } from './types';
import type { WalletConfig, WalletSource } from '../types';

const DEFAULT_CONFIG: WalletConfig = {
requiresCertificate: true,
Expand Down
5 changes: 2 additions & 3 deletions packages/dapp-kit/src/dapp-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import {
} from '@vechain/connex-driver/dist/index.js';
import { Framework } from '@vechain/connex-framework';
import { blake2b256 } from 'thor-devkit';
import { normalizeGenesisBlock } from './genesis';
import { WalletManager } from './wallet-manager';
import { DAppKitLogger } from './utils/logger';
import { WalletManager } from './classes';
import { DAppKitLogger, normalizeGenesisBlock } from './utils';
import type { DAppKitOptions } from './types';

const cache: Record<string, DriverNoVendor | undefined> = {};
Expand Down
18 changes: 3 additions & 15 deletions packages/dapp-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
export * from './dapp-kit';
export * from './wallet';
export * from './wallet-manager';
export * from './wallet-connect';
export * from './genesis';
export * from './constants';
export * from './classes';
export * from './utils';

export type {
DAppKitOptions,
ConnexWallet,
WalletSource,
ConnectResponse,
WalletConfig,
BaseWallet,
WalletManagerState,
Genesis,
} from './types';
export * from './types';
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { newWcSigner } from './signer';
import { createWcModal } from './web3-modal';
import { createWcClient } from './client';
export type {
DAppKitOptions,
ConnexWallet,
WalletSource,
ConnectResponse,
WalletConfig,
BaseWallet,
WalletManagerState,
Genesis,
} from './types';

export type {
WCSigner,
Expand All @@ -11,6 +18,4 @@ export type {
WCModal,
OpenOptions,
SubscribeModalState,
} from './types';

export { newWcSigner, createWcModal, createWcClient };
} from './wc-types';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Certificate } from 'thor-devkit';
import type { WalletConnectOptions } from '@vechain/dapp-kit';
import type { LogLevel } from './utils/logger';
import type { LogLevel } from '../utils/logger';

declare global {
interface Window {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DAppKitLogger } from './utils';
import { DAppKitLogger } from './logger';

export const convertVendorToSigner = (vendor: Connex.Vendor): Connex.Signer => {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Connex } from '@vechain/connex';
import type { WCClient, WCModal } from './wallet-connect';
import { createWcClient, createWcModal, newWcSigner } from './wallet-connect';
import type { ConnexWallet, DAppKitOptions, WalletSource } from './types';
import { CertificateBasedWallet } from './wallets/certificate-wallet';
import { WCWallet } from './wallets/wc-wallet';
import type {
ConnexWallet,
DAppKitOptions,
WalletSource,
WCClient,
WCModal,
} from '../types';
import { CertificateBasedWallet } from '../classes/certificate-wallet';
import { WCWallet } from '../classes/wc-wallet';
import { createWcClient } from './create-wc-client';
import { createWcModal } from './create-wc-modal';
import { createWcSigner } from './create-wc-signer';
import { convertVendorToSigner } from './convert-vendor-to-signer';
import { normalizeGenesisId } from './genesis';
import { convertVendorToSigner } from './vendor-signer';
import { DAppKitLogger } from './utils';
import { DAppKitLogger } from './logger';

type ICreateWallet = DAppKitOptions & {
source: WalletSource;
Expand Down Expand Up @@ -61,7 +68,7 @@ export const createWallet = ({

const web3Modal: WCModal = modal ?? createWcModal(projectId);

const wallet = newWcSigner({
const wallet = createWcSigner({
genesisId,
wcClient,
web3Modal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ResolvedSignClient,
WalletConnectOptions,
WCClient,
} from './types';
} from '../types/wc-types';

const _cachedClients: Record<string, WCClient | undefined> = {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WalletConnectModal } from '@walletconnect/modal';
import type { WCModal } from './types';
import type { WCModal } from '../types/wc-types';

const _cachedModals: Record<string, WalletConnectModal | undefined> = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type {
} from '@walletconnect/types';
import { getSdkError } from '@walletconnect/utils';
import type { SignClient } from '@walletconnect/sign-client/dist/types/client';
import { DAppKitLogger } from '../utils';
import type { WCSigner, WCSignerOptions } from './types';
import { DefaultMethods } from './constants';
import type { WCSigner, WCSignerOptions } from '../types/wc-types';
import { DefaultMethods } from '../constants/wallet-connect';
import { DAppKitLogger } from './logger';

interface SessionAccount {
networkIdentifier: string;
Expand All @@ -20,7 +20,7 @@ interface SessionAccount {
* @param options - The signer options. See {@link WCSignerOptions}
* @returns A new {@link WCSigner}
*/
export const newWcSigner = ({
export const createWcSigner = ({
genesisId,
wcClient,
web3Modal,
Expand Down
21 changes: 21 additions & 0 deletions packages/dapp-kit/src/utils/genesis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { genesisBlocks } from '../constants';
import type { Genesis } from '../types';

const normalizeGenesisId = (genesis?: Genesis): string => {
if (!genesis) return genesisBlocks.main.id;

if (genesis === 'main' || genesis === 'test')
return genesisBlocks[genesis].id;

return genesis.id;
};

const normalizeGenesisBlock = (genesis?: Genesis): Connex.Thor.Block => {
if (!genesis) return genesisBlocks.main;

if (genesis === 'main' || genesis === 'test') return genesisBlocks[genesis];

return genesis;
};

export { normalizeGenesisId, normalizeGenesisBlock };
7 changes: 7 additions & 0 deletions packages/dapp-kit/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export * from './logger';
export * from './convert-vendor-to-signer';
export * from './local-storage';
export * from './genesis';
export * from './create-wallet';
export * from './create-wc-client';
export * from './create-wc-signer';
export * from './create-wc-modal';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { WalletSource } from './types';
import { DAppKitLogger } from './utils';
import type { WalletSource } from '../types';
import { DAppKitLogger } from './logger';

const STORAGE_PREFIX = 'dappkit@vechain';
const WALLET_SOURCE_KEY = `${STORAGE_PREFIX}/source`;
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/test/create-wallet.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import type { Connex1 } from '@vechain/connex/esm/signer';
import { createWallet } from '../src/create-wallet';
import { createWallet } from '../src/utils';
import type {
DAppKitOptions,
WalletConnectOptions,
Expand Down
7 changes: 2 additions & 5 deletions packages/dapp-kit/test/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { describe, expect, it } from 'vitest';
import {
genesisBlocks,
normalizeGenesisBlock,
normalizeGenesisId,
} from '../src/genesis';
import { normalizeGenesisBlock, normalizeGenesisId } from '../src/utils';
import { genesisBlocks } from '../src/constants';

const customBlock = {
number: 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/test/helpers/mocked-sign-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from '@walletconnect/types/dist/types/sign-client/engine';
import type { SessionTypes } from '@walletconnect/types';
import type { ResolvedSignClient } from '../../src';
import { DefaultMethods } from '../../src/wallet-connect/constants';
import { DefaultMethods } from '../../src';
import { wcSessionStruct } from './wc-fixtures';
import { address, mockedConnexSigner } from './mocked-signer';
import { randomUUID } from 'node:crypto';
Expand Down
3 changes: 1 addition & 2 deletions packages/dapp-kit/test/helpers/wc-fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SessionTypes, SignClientTypes } from '@walletconnect/types';
import { normalizeGenesisBlock } from '../../src/genesis';
import { DefaultMethods } from '../../src/wallet-connect/constants';
import { normalizeGenesisBlock, DefaultMethods } from '../../src';
import { address } from './mocked-signer';

const wcMetadata: SignClientTypes.Options['metadata'] = {
Expand Down
File renamed without changes.
Loading

0 comments on commit e42a180

Please sign in to comment.