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

chore: document wallet-kit #48

Merged
merged 1 commit into from
Nov 3, 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
83 changes: 82 additions & 1 deletion packages/wallet-kit/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
# `wallet-helpers`
# `@vecahin/dapp-kit`

## Why ?

- Allow easy interaction with all wallets.
- Connex is designed to play nice with one wallet at a time, this library provides a layer on top of Connex to easily
allow interaction with all wallets.
- Easy setup for wallet connect.
- Create your own

## Installation

- See the parent [README](../../README.md) for installation instructions.

### Build

```bash
yarn build
```

## Usage

```bash
yarn add @vechain/dapp-kit
```

- Create a function to handle remote wallet disconnections. Some wallets can disconnect remotely (eg. Wallet Connect)

```typescript
const onDisconnected = () => {
//TODO: handle disconnect
};
```

- Optional: Configure wallet connect options

```typescript
import type { WalletConnectOptions } from '@vechain/wallet-connect';

const walletConnectOptions: WalletConnectOptions = {
projectId: '<PROJECT_ID>', // Create your project here: https://cloud.walletconnect.com/sign-up
metadata: {
name: 'My dApp',
description: 'My dApp description',
url: window.location.origin, // Your app URL
icons: [`${window.location.origin}/images/my-dapp-icon.png`], // Your app Icon
},
};
```

- Create a new instance of `MultiWalletConnex` and pass in the options
- `thor` will be ready to use to interact with the chain, but calling any methods requiring a wallet will throw an
error. See the next step to finalise the setup.

```typescript
const { thor, vendor, wallet } = new MultiWalletConnex({
nodeUrl: 'https://sync-testnet.vechain.org/', //Required
genesis: 'main', //Optional - "main" | "test" | Connex.Thor.Block
walletConnectOptions,
onDisconnected,
});
```

- You can set the wallet source when the user selects a wallet, or if you want to default to a specific wallet.
- Connex is ready to use as normal

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

- Connect to the wallet. This will return the user's address
- `verified` indicates whether a certificate is signed by the user. If a sign in is required and the account is not
verified, you should request a subsequent certificate sign in

```typescript
const { account, verified } = await wallet.connect();
```

```typescript
const tx = await thor.account("0x...123").method(...).transact().request();
const certRes = await vendor.sign("cert", {...}).requset();
```
5 changes: 2 additions & 3 deletions packages/wallet-kit/src/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createWallet = ({
genesis,
walletConnectOptions,
onDisconnected,
}: ICreateWallet): ConnexWallet | undefined => {
}: ICreateWallet): ConnexWallet => {
const genesisId = normalizeGenesisId(genesis);

switch (source) {
Expand Down Expand Up @@ -47,8 +47,7 @@ export const createWallet = ({
}
case 'wallet-connect': {
if (!walletConnectOptions) {
onDisconnected();
return;
throw new Error('WalletConnect options are not provided');
}

const { projectId, metadata } = walletConnectOptions;
Expand Down
9 changes: 8 additions & 1 deletion packages/wallet-kit/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ interface WalletConfig {

export type Genesis = 'main' | 'test' | Connex.Thor.Block;

/**
* Options for the MultiWalletConnex class
* @param nodeUrl - The URL of the VeChain node to connect to
* @param genesis - Optional. The genesis block of the VeChain network you want to connect to. Eg, 'main', 'test', or a Connex.Thor.Block object
* @param onDisconnected - A callback that will be called when the session is disconnected
* @param walletConnectOptions - Optional. Options for the WalletConnect integration
*/
interface ConnexOptions {
nodeUrl: string;
genesis?: Genesis;
onDisconnected: () => void;
onDisconnected?: () => void;
walletConnectOptions?: WalletConnectOptions;
}

Expand Down
13 changes: 6 additions & 7 deletions packages/wallet-kit/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
WalletSource,
} from './types';
import { createWallet } from './create-wallet';
import { WalletSources } from './wallet';

class WalletManager implements ConnexWalletManager {
private wallets: Record<string, ConnexWallet | undefined> = {};
Expand All @@ -28,16 +29,14 @@ class WalletManager implements ConnexWalletManager {
let wallet = this.wallets[source];

if (!wallet) {
// If it's not a built-in wallet, we can't create it
if (!WalletSources.includes(source))
throw new Error(`No wallet found for: ${source}`);

const opts = { ...this.connexOptions, source };
wallet = createWallet(opts);

if (wallet) {
this.wallets[source] = wallet;
}
}

if (!wallet) {
throw new Error('Signer is not initialized');
this.wallets[source] = wallet;
}

return wallet;
Expand Down
Loading