Skip to content

Commit

Permalink
Merge branch 'main' into allow-custom-net
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecarpini authored Oct 21, 2024
2 parents 8ac2140 + dc1c15c commit 41dddd5
Show file tree
Hide file tree
Showing 31 changed files with 513 additions and 287 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ yarn test:e2e:headless

## Publishing

Before publishing create the release on github called `X.Y.Z` and then `git pull` the tag.

```bash
git clone [email protected]:vechainfoundation/vechain-dapp-kit.git
cd vechain-dapp-kit
Expand Down
2 changes: 1 addition & 1 deletion examples/sample-next-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@vechain/dapp-kit": "*",
"@vechain/dapp-kit-react": "*",
"@vechain/dapp-kit-ui": "*",
"next": "14.1.4",
"next": "14.2.10",
"react": "^18",
"react-dom": "^18"
},
Expand Down
8 changes: 7 additions & 1 deletion examples/sample-next-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"dist/types/**/*.ts"
],
"exclude": ["node_modules"]
}
2 changes: 1 addition & 1 deletion examples/sample-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@vechain/dapp-kit-ui": "*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"vite": "^5.0.12"
"vite": "^5.3.6"
},
"devDependencies": {
"@originjs/vite-plugin-commonjs": "^1.0.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/sample-remix-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"typescript": "^5.1.6",
"vite": "^5.1.0",
"vite": "^5.3.6",
"vite-plugin-node-polyfills": "^0.21.0",
"vite-tsconfig-paths": "^4.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/sample-svelte-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.5.2",
"vite": "^4.5.5",
"vite-plugin-node-polyfills": "^0.16.0"
}
}
2 changes: 1 addition & 1 deletion examples/sample-vanilla-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
},
"devDependencies": {
"typescript": "^5.2.2",
"vite": "^5.2.0"
"vite": "^5.3.6"
}
}
2 changes: 1 addition & 1 deletion examples/sample-vue-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
"typescript": "^5.2.2",
"vite": "^5.0.12",
"vite": "^5.3.6",
"vite-plugin-node-polyfills": "^0.17.0",
"vue-tsc": "^2.0.6"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit-react",
"version": "1.0.13",
"version": "1.0.14",
"private": false,
"homepage": "https://github.com/vechain/vechain-dapp-kit",
"repository": "github:vechain/vechain-dapp-kit",
Expand Down Expand Up @@ -41,7 +41,7 @@
"react": "^18.2.0",
"tsup": "*",
"typescript": "*",
"vite": "^4.5.2",
"vite": "^4.5.5",
"vitest": "^0.34.6"
}
}
116 changes: 66 additions & 50 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,24 @@ import React, {
useMemo,
useState,
} from 'react';
import type { WalletSource } from '@vechain/dapp-kit';
import type { DAppKit, WalletSource } from '@vechain/dapp-kit';
import { DAppKitUI } from '@vechain/dapp-kit-ui';
import { subscribeKey } from 'valtio/vanilla/utils';
import type { DAppKitProviderOptions, DAppKitContext } from './types';
import { type Certificate } from '@vechain/sdk-core';
import type { DAppKitProviderOptions, DAppKitContext } from './types';

/**
* Context
*/
const Context = createContext<DAppKitContext | undefined>(undefined);

export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
export const DAppKitDataProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
connex,
}: {
children: React.ReactNode;
connex: DAppKit;
}): React.ReactElement => {
const connex = useMemo(
() =>
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
[
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
],
);

const [account, setAccount] = useState<string | null>(
connex.wallet.state.address,
);
Expand Down Expand Up @@ -125,6 +82,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
account,
source,
connectionCertificate,
signTypedData: connex.wallet.signTypedData,
},
modal: {
open: openModal,
Expand All @@ -145,6 +103,64 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
return <Context.Provider value={context}>{children}</Context.Provider>;
};

export const DAppKitProvider = ({
children,
nodeUrl,
genesis,
walletConnectOptions,
usePersistence = false,
logLevel,
requireCertificate,
themeMode,
themeVariables,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}: DAppKitProviderOptions): React.ReactElement | null => {
const [connex, setConnex] = useState<DAppKit | null>(null);
useEffect(() => {
setConnex(
DAppKitUI.configure({
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
);
}, [
nodeUrl,
genesis,
walletConnectOptions,
usePersistence,
logLevel,
requireCertificate,
themeVariables,
themeMode,
i18n,
language,
modalParent,
onSourceClick,
connectionCertificateData,
]);
if (!connex) {
return null;
}
return (
<DAppKitDataProvider connex={connex}>{children}</DAppKitDataProvider>
);
};

export const useConnex = (): DAppKitContext['connex'] => {
const context = useContext(Context);

Expand Down
7 changes: 6 additions & 1 deletion packages/dapp-kit-react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/// <reference types="@vechain/connex" />
import type React from 'react';
import type { ConnectResponse, WalletSource } from '@vechain/dapp-kit';
import type {
ConnectResponse,
WalletManager,
WalletSource,
} from '@vechain/dapp-kit';
import { type DAppKitUIOptions } from '@vechain/dapp-kit-ui';
import { type Certificate } from '@vechain/sdk-core';

Expand Down Expand Up @@ -39,6 +43,7 @@ export interface DAppKitContext {
account: string | null;
source: WalletSource | null;
connectionCertificate: Certificate | null;
signTypedData: WalletManager['signTypedData'];
};
modal: {
open: () => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit-ui",
"version": "1.0.13",
"version": "1.0.14",
"private": false,
"description": "Vanilla JS DAppKit",
"keywords": [
Expand Down Expand Up @@ -58,7 +58,7 @@
"tsup": "^7.2.0",
"typechain": "^8.3.2",
"typescript": "~5.2.0",
"vite": "^4.5.2",
"vite": "^4.5.5",
"vitest": "^0.34.6"
}
}
5 changes: 3 additions & 2 deletions packages/dapp-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vechain/dapp-kit",
"version": "1.0.13",
"version": "1.0.14",
"private": false,
"homepage": "https://github.com/vechain/vechain-dapp-kit",
"repository": "github:vechain/vechain-dapp-kit",
Expand Down Expand Up @@ -31,6 +31,7 @@
"@walletconnect/modal": "2.6.2",
"@walletconnect/sign-client": "2.10.2",
"@walletconnect/utils": "2.10.2",
"ethers": "^6.13.3",
"events": "^3.3.0",
"valtio": "1.11.2"
},
Expand All @@ -45,7 +46,7 @@
"lokijs": "^1.5.12",
"tsup": "*",
"typescript": "*",
"vite": "^4.5.2",
"vite": "^4.5.5",
"vitest": "^0.34.6"
}
}
10 changes: 10 additions & 0 deletions packages/dapp-kit/src/classes/certificate-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { certificate } from '@vechain/sdk-core';
import type { BaseWallet, ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../constants';
import { ethers } from 'ethers';

Check warning on line 4 in packages/dapp-kit/src/classes/certificate-wallet.ts

View workflow job for this annotation

GitHub Actions / Lint, Build & Test

All imports in the declaration are only used as types. Use `import type`
import { SignTypedDataOptions } from '../types/types';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
Expand Down Expand Up @@ -60,6 +62,14 @@ class CertificateBasedWallet implements ConnexWallet {
options: Connex.Signer.TxOptions,
): Promise<Connex.Vendor.TxResponse> => this.wallet.signTx(msg, options);

signTypedData = (
_domain: ethers.TypedDataDomain,
_types: Record<string, ethers.TypedDataField[]>,
_value: Record<string, unknown>,
_options?: SignTypedDataOptions,
): Promise<string> =>
this.wallet.signTypedData(_domain, _types, _value, _options);

disconnect = async (): Promise<void> => this.wallet.disconnect?.();
}

Expand Down
13 changes: 13 additions & 0 deletions packages/dapp-kit/src/classes/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
import { DAppKitLogger, Storage, createWallet } from '../utils';
import { DEFAULT_CONNECT_CERT_MESSAGE, WalletSources } from '../constants';
import { certificate } from '@vechain/sdk-core';
import { ethers } from 'ethers';
import { SignTypedDataOptions } from '../types/types';

class WalletManager {
public readonly state: WalletManagerState;
Expand Down Expand Up @@ -194,6 +196,17 @@ class WalletManager {
throw e;
});

signTypedData = (
domain: ethers.TypedDataDomain,
types: Record<string, ethers.TypedDataField[]>,
value: Record<string, unknown>,
options?: SignTypedDataOptions,
): Promise<string> =>
this.wallet.signTypedData(domain, types, value, options).catch((e) => {
DAppKitLogger.error('WalletManager', 'signTypedData', e);
throw e;
});

setSource = (src: WalletSource): void => {
if (this.state.source === src) {
return;
Expand Down
10 changes: 10 additions & 0 deletions packages/dapp-kit/src/classes/wc-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ethers } from 'ethers';
import type { ConnectResponse, ConnexWallet, WCSigner } from '../types';
import { SignTypedDataOptions } from '../types/types';

class WCWallet implements ConnexWallet {
constructor(private readonly signer: WCSigner) {}
Expand All @@ -23,6 +25,14 @@ class WCWallet implements ConnexWallet {
options: Connex.Signer.TxOptions,
): Promise<Connex.Vendor.TxResponse> => this.signer.signTx(msg, options);

signTypedData = async (
_domain: ethers.TypedDataDomain,
_types: Record<string, ethers.TypedDataField[]>,
_value: Record<string, unknown>,
_options?: SignTypedDataOptions,
): Promise<string> =>
this.signer.signTypedData(_domain, _types, _value, _options);

disconnect = (): Promise<void> => this.signer.disconnect();
}

Expand Down
1 change: 1 addition & 0 deletions packages/dapp-kit/src/constants/wallet-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
export enum DefaultMethods {
RequestTransaction = 'thor_sendTransaction',
SignCertificate = 'thor_signCertificate',
SignTypedData = 'thor_signTypedData',
}
Loading

0 comments on commit 41dddd5

Please sign in to comment.