Skip to content

Commit

Permalink
feat: add support for connecting with a custom message
Browse files Browse the repository at this point in the history
  • Loading branch information
HiiiiD committed Jan 14, 2025
1 parent 37ac3f3 commit e89b12c
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 3,168 deletions.
8 changes: 4 additions & 4 deletions packages/dapp-kit-react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/// <reference types="@vechain/connex" />
import type React from 'react';
import type {
ConnectResponse,
ConnectCallback,
WalletManager,
WalletSource,
} from '@vechain/dapp-kit';
import { type DAppKitUIOptions } from '@vechain/dapp-kit-ui';
import { CertificateData } from '@vechain/sdk-core';
import type React from 'react';

export type { WalletConnectOptions, DAppKitOptions } from '@vechain/dapp-kit';
export type { DAppKitOptions, WalletConnectOptions } from '@vechain/dapp-kit';
export type { DAppKitUIOptions } from '@vechain/dapp-kit-ui';

export interface AccountState {
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface DAppKitContext {
setSource: (source: WalletSource) => void;
availableWallets: WalletSource[];
disconnect: () => void;
connect: () => Promise<ConnectResponse>;
connect: ConnectCallback;
account: string | null;
accountDomain: string | null;
isAccountDomainLoading: boolean;
Expand Down
33 changes: 22 additions & 11 deletions packages/dapp-kit/src/classes/certificate-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
import { Certificate } from '@vechain/sdk-core';
import type { BaseWallet, ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../constants';
import { ethers } from 'ethers';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../constants';
import type {
BaseWallet,
CertificateArgs,
ConnectResponse,
ConnexWallet,
} from '../types';
import { SignTypedDataOptions } from '../types/types';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
*/
class CertificateBasedWallet implements ConnexWallet {
private readonly certificateData: Required<CertificateArgs>;
constructor(
private readonly wallet: BaseWallet,
private readonly connectionCertificateData?: {
message?: Connex.Vendor.CertMessage;
options?: Connex.Signer.CertOptions;
},
) {}
connectionCertificateData?: CertificateArgs,
) {
this.certificateData = {
message:
connectionCertificateData?.message ??
DEFAULT_CONNECT_CERT_MESSAGE,
options: connectionCertificateData?.options ?? {},
};
}

connect = async (): Promise<ConnectResponse> => {
connect = async (
_certificate?: CertificateArgs,
): Promise<ConnectResponse> => {
const certificateMessage =
this.connectionCertificateData?.message ||
DEFAULT_CONNECT_CERT_MESSAGE;
_certificate?.message || this.certificateData.message;
const certificateOptions =
this.connectionCertificateData?.options || {};
_certificate?.options ?? this.certificateData.options;
const {
annex: { domain, signer, timestamp },
signature,
Expand Down
23 changes: 14 additions & 9 deletions packages/dapp-kit/src/classes/wallet-manager.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { proxy, subscribe } from 'valtio/vanilla';
import { subscribeKey } from 'valtio/vanilla/utils';
import { type DriverNoVendor } from '@vechain/connex-driver';
import { Certificate } from '@vechain/sdk-core';
import { type ethers } from 'ethers';
import { type DriverNoVendor } from '@vechain/connex-driver';
import { type SignTypedDataOptions } from '../types/types';
import { proxy, subscribe } from 'valtio/vanilla';
import { subscribeKey } from 'valtio/vanilla/utils';
import { DEFAULT_CONNECT_CERT_MESSAGE, WalletSources } from '../constants';
import type {
ConnectResponse,
ConnexWallet,
DAppKitOptions,
WalletManagerState,
WalletSource,
} from '../types';
import { CertificateArgs, type SignTypedDataOptions } from '../types/types';
import { DAppKitLogger, Storage, createWallet } from '../utils';
import { DEFAULT_CONNECT_CERT_MESSAGE, WalletSources } from '../constants';
import { getAccountDomain } from '../utils/get-account-domain';

class WalletManager {
Expand Down Expand Up @@ -118,12 +118,17 @@ class WalletManager {
* Sign a connection certificate
* this is needed for wallet connect connections when a connection certificate is required
*/
signConnectionCertificate = async (): Promise<ConnectResponse> => {
signConnectionCertificate = async (
_certificate?: CertificateArgs,
): Promise<ConnectResponse> => {
const certificateMessage =
_certificate?.message ||
this.options.connectionCertificate?.message ||
DEFAULT_CONNECT_CERT_MESSAGE;
const certificateOptions =
this.options.connectionCertificate?.options || {};
_certificate?.options ||
this.options.connectionCertificate?.options ||
{};
const {
annex: { domain, signer, timestamp },
signature,
Expand Down Expand Up @@ -157,9 +162,9 @@ class WalletManager {
}
};

connect = (): Promise<ConnectResponse> =>
connect = (_certificate?: CertificateArgs): Promise<ConnectResponse> =>
this.wallet
.connect()
.connect(_certificate)
.then((res) => {
if (
this.state.source === 'wallet-connect' &&
Expand Down
22 changes: 12 additions & 10 deletions packages/dapp-kit/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
export type {
DAppKitOptions,
ConnexWallet,
WalletSource,
BaseWallet,
CertificateArgs,
ConnectCallback,
ConnectResponse,
ConnexWallet,
DAppKitOptions,
Genesis,
WalletConfig,
BaseWallet,
WalletManagerState,
Genesis,
WalletSource,
} from './types';

export type {
WCSigner,
WCSignerOptions,
WalletConnectOptions,
OpenOptions,
ResolvedSignClient,
SubscribeModalState,
WalletConnectOptions,
WCClient,
WCModal,
OpenOptions,
SubscribeModalState,
WCSigner,
WCSignerOptions,
} from './wc-types';
40 changes: 27 additions & 13 deletions packages/dapp-kit/src/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { WalletConnectOptions } from '@vechain/dapp-kit';
import { CertificateData } from '@vechain/sdk-core';
import type { LogLevel } from '../utils';
import { WalletConnectOptions } from './wc-types';

declare global {
interface Window {
Expand Down Expand Up @@ -29,6 +29,21 @@ interface WalletConfig {

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

/**
* Simple Certificate Args
*/
type CertificateArgs = {
message: Connex.Vendor.CertMessage;
options?: Connex.Signer.CertOptions;
};

/**
* Callback used by the DAppKit `connect` function
*/
type ConnectCallback = (
_certificate?: CertificateArgs,
) => Promise<ConnectResponse>;

/**
* Options for the DAppKit class
* @param nodeUrl - The URL of the VeChain node to connect to
Expand All @@ -51,10 +66,7 @@ interface DAppKitOptions {
useFirstDetectedSource?: boolean;
logLevel?: LogLevel;
requireCertificate?: boolean;
connectionCertificate?: {
message?: Connex.Vendor.CertMessage;
options?: Connex.Signer.CertOptions;
};
connectionCertificate?: CertificateArgs;
customNet?: Net;
allowedWallets?: WalletSource[];
}
Expand All @@ -67,7 +79,7 @@ type BaseWallet = ExpandedConnexSigner & {
* Modifies the Connex.Signer interface to include a disconnect method
*/
type ConnexWallet = BaseWallet & {
connect: () => Promise<ConnectResponse>;
connect: ConnectCallback;
};

interface ConnectResponse {
Expand All @@ -91,14 +103,16 @@ interface SignTypedDataOptions {

export type {
BaseWallet,
DAppKitOptions,
ConnexWallet,
WalletConfig,
WalletSource,
WalletManagerState,
CertificateArgs,
ConnectCallback,
ConnectResponse,
ConnexWallet,
DAppKitOptions,
DriverSignedTypedData,
ExpandedConnexSigner,
Genesis,
SignTypedDataOptions,
ExpandedConnexSigner,
DriverSignedTypedData,
WalletConfig,
WalletManagerState,
WalletSource,
};
26 changes: 24 additions & 2 deletions packages/dapp-kit/test/wallet-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { describe, expect, it, vi } from 'vitest';
import type { WalletConnectOptions } from '../src';
import { WalletManager } from '../src';
import { mockedConnexSigner } from './helpers/mocked-signer';
import { typedData } from './fixture';
import { mockedConnexSigner } from './helpers/mocked-signer';

const newWalletManager = (wcOptions?: WalletConnectOptions): WalletManager => {
return new WalletManager(
{
nodeUrl: 'https://testnet.veblocks.net/',
walletConnectOptions: wcOptions,
genesis: 'main',
genesis: 'test',
},
{} as any,
);
Expand Down Expand Up @@ -37,6 +37,28 @@ describe('WalletManager', () => {
'No wallet has been selected',
);
});
it('connect with custom message', async () => {
const walletManager = newWalletManager();
walletManager.setSource('veworld');
await walletManager.connect({
message: {
payload: {
type: 'text',
content: 'TEST1',
},
purpose: 'identification',
},
});

expect(walletManager.state.connectionCertificate).toEqual(
expect.objectContaining({
payload: {
type: 'text',
content: 'TEST1',
},
}),
);
});
});

describe('signTx', () => {
Expand Down
Loading

0 comments on commit e89b12c

Please sign in to comment.