Skip to content

Commit

Permalink
feat(wc): tidy up for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 2, 2023
1 parent b5c0887 commit 1f38345
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 58 deletions.
4 changes: 2 additions & 2 deletions apps/sample-vue-app/src/connex/ConnexProvider.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { defineComponent, provide, reactive, readonly, toRefs } from 'vue';
import {
createConnexInstance,
MultiWalletConnex,
WalletSource,
WalletSources,
} from '@vechain/wallet-kit';
Expand Down Expand Up @@ -45,7 +45,7 @@ export default defineComponent({
walletState.source = null;
};
const connex = createConnexInstance({
const connex = new MultiWalletConnex({
nodeUrl: 'https://mainnet.vechain.org/',
onDisconnected,
});
Expand Down
7 changes: 3 additions & 4 deletions packages/react-wallet-kit/src/ConnexProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
useReducer,
} from 'react';
import type { ConnexInstance } from '@vechain/wallet-kit';
import { createConnexInstance, WalletSources } from '@vechain/wallet-kit';
import { MultiWalletConnex, WalletSources } from '@vechain/wallet-kit';
import type { WalletSource } from '@vechain/wallet-kit/src/types';
import { accountReducer, defaultAccountState } from './AccountReducer';
import type { ConnexContext, ConnexProviderOptions } from './types';
Expand All @@ -33,14 +33,13 @@ export const ConnexProvider: React.FC<ConnexProviderOptions> = ({

const connex: ConnexInstance = useMemo(
() =>
createConnexInstance({
new MultiWalletConnex({
nodeUrl: nodeOptions.node,
genesis: nodeOptions.network,
source: accountState.source ?? undefined,
walletConnectOptions,
onDisconnected,
}),
//eslint-disable-next-line react-hooks/exhaustive-deps

[
nodeOptions.node,
nodeOptions.network,
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-kit/src/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DEFAULT_CONNECT_CERT_MESSAGE: Connex.Vendor.CertMessage = {
purpose: 'identification',
payload: {
type: 'text',
content: `Sign a certificate to prove your identity for: ${window.origin}`,
content: `The following dApp would like to see your public address: ${window.origin}`,
},
};

Expand Down
42 changes: 23 additions & 19 deletions packages/wallet-kit/src/connex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ import { createNoVendor } from '@vechain/connex/esm/driver';
import { newThor } from '@vechain/connex-framework/dist/thor';
import type { DriverNoVendor } from '@vechain/connex-driver';
import { newVendor } from '@vechain/connex-framework';
import type { ConnexInstance, ConnexOptions } from './types';
import type { ConnexOptions, ConnexWalletManager } from './types';
import { normalizeGenesisBlock } from './genesis';
import { FullDriver } from './full-driver';
import { WalletManager } from './wallet-manager';

const createConnexInstance = (options: ConnexOptions): ConnexInstance => {
const { nodeUrl, genesis } = options;
class MultiWalletConnex {
public readonly thor: Connex.Thor;
public readonly vendor: Connex.Vendor;
public readonly wallet: ConnexWalletManager;

const genesisBlock = normalizeGenesisBlock(genesis);
constructor(options: ConnexOptions) {
const { nodeUrl, genesis } = options;

const thorOnlyDriver: DriverNoVendor = createNoVendor(
nodeUrl,
genesisBlock,
);
const genesisBlock = normalizeGenesisBlock(genesis);

const walletManager = new WalletManager(options);
const fullDriver = new FullDriver(thorOnlyDriver, walletManager);
const thorOnlyDriver: DriverNoVendor = createNoVendor(
nodeUrl,
genesisBlock,
);

const thor = newThor(fullDriver);
const vendor = newVendor(fullDriver);
const walletManager = new WalletManager(options);
const fullDriver = new FullDriver(thorOnlyDriver, walletManager);

return {
thor,
vendor,
wallet: walletManager,
};
};
const thor = newThor(fullDriver);
const vendor = newVendor(fullDriver);

export { createConnexInstance };
this.thor = thor;
this.vendor = vendor;
this.wallet = walletManager;
}
}

export { MultiWalletConnex };
33 changes: 16 additions & 17 deletions packages/wallet-kit/src/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import {
newWeb3Modal,
} from '@vechain/wallet-connect/dist';
import { createSync, createSync2 } from '@vechain/connex/esm/signer';
import type { ConnexOptions, ConnexWallet } from './types';
import type { ConnexOptions, ConnexWallet, WalletSource } from './types';
import { CertificateBasedWallet } from './wallets/certificate-wallet';
import { WCWallet } from './wallets/wc-wallet';
import { normalizeGenesisId } from './genesis';
import { CertificateConnectionWallet } from './wallets/certificate-connection';
import { WcConnectionWallet } from './wallets/wc-connection';

export const createWallet = (
params: ConnexOptions,
): ConnexWallet | undefined => {
const { source, genesis } = params;

if (!source) {
return;
}
type ICreateWallet = ConnexOptions & {
source: WalletSource;
};

export const createWallet = ({
source,
genesis,
walletConnectOptions,
onDisconnected,
}: ICreateWallet): ConnexWallet | undefined => {
const genesisId = normalizeGenesisId(genesis);

switch (source) {
Expand All @@ -28,12 +29,12 @@ export const createWallet = (

const signer = createSync(genesisId);

return new CertificateConnectionWallet(signer);
return new CertificateBasedWallet(signer);
}
case 'sync2': {
const signer = createSync2(genesisId);

return new CertificateConnectionWallet(signer);
return new CertificateBasedWallet(signer);
}
case 'veworld-extension': {
if (!window.vechain) {
Expand All @@ -42,11 +43,9 @@ export const createWallet = (

const signer = window.vechain.newConnexSigner(genesisId);

return new CertificateConnectionWallet(Promise.resolve(signer));
return new CertificateBasedWallet(Promise.resolve(signer));
}
case 'wallet-connect': {
const { walletConnectOptions, onDisconnected } = params;

if (!walletConnectOptions) {
onDisconnected();
return;
Expand All @@ -68,7 +67,7 @@ export const createWallet = (
onDisconnected,
});

return new WcConnectionWallet(wallet);
return new WCWallet(wallet);
}
}
};
1 change: 0 additions & 1 deletion packages/wallet-kit/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ interface ConnexOptions {
nodeUrl: string;
genesis?: Genesis;
onDisconnected: () => void;
source?: WalletSource;
walletConnectOptions?: WalletConnectOptions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Certificate } from 'thor-devkit';
import type { BaseWallet, ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../certificates';
import {
DEFAULT_CONNECT_CERT_MESSAGE,
DEFAULT_SIGN_IN_MESSAGE,
} from '../certificates';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
*/
export class CertificateConnectionWallet implements ConnexWallet {
class CertificateBasedWallet implements ConnexWallet {
constructor(private readonly wallet: Promise<BaseWallet>) {}

connect = async (): Promise<ConnectResponse> => {
Expand Down Expand Up @@ -47,7 +50,7 @@ export class CertificateConnectionWallet implements ConnexWallet {
msg?: Connex.Vendor.CertMessage | undefined,
options?: Connex.Signer.CertOptions | undefined,
): Promise<Connex.Vendor.CertResponse> => {
const _msg = msg || DEFAULT_CONNECT_CERT_MESSAGE;
const _msg = msg || DEFAULT_SIGN_IN_MESSAGE;
const _options = options || {};

return this.signCert(_msg, _options);
Expand All @@ -59,9 +62,8 @@ export class CertificateConnectionWallet implements ConnexWallet {
): Promise<Connex.Vendor.TxResponse> =>
this.wallet.then((w) => w.signTx(msg, options));

async disconnect(): Promise<void> {
const _wallet = await this.wallet;

return _wallet.disconnect?.() || Promise.resolve();
}
disconnect = async (): Promise<void> =>
this.wallet.then((w) => w.disconnect?.());
}

export { CertificateBasedWallet };
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { WCSigner } from '@vechain/wallet-connect';
import type { ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../certificates';
import { DEFAULT_SIGN_IN_MESSAGE } from '../certificates';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
*/
export class WcConnectionWallet implements ConnexWallet {
class WCWallet implements ConnexWallet {
constructor(private readonly signer: WCSigner) {}

connect = async (): Promise<ConnectResponse> => {
Expand All @@ -27,7 +24,7 @@ export class WcConnectionWallet implements ConnexWallet {
msg?: Connex.Vendor.CertMessage | undefined,
options?: Connex.Signer.CertOptions | undefined,
): Promise<Connex.Vendor.CertResponse> => {
const _msg = msg || DEFAULT_CONNECT_CERT_MESSAGE;
const _msg = msg || DEFAULT_SIGN_IN_MESSAGE;
const _options = options || {};

return this.signCert(_msg, _options);
Expand All @@ -40,3 +37,5 @@ export class WcConnectionWallet implements ConnexWallet {

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

export { WCWallet };

0 comments on commit 1f38345

Please sign in to comment.