Skip to content

Commit

Permalink
refactor: remove signIn method
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 8, 2023
1 parent 404dd65 commit 685e46a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 47 deletions.
6 changes: 5 additions & 1 deletion apps/sample-vue-app/src/connex/ConnexProvider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ export default defineComponent({
walletState.account = null;
};
const onSourceChanged = (source: WalletSource | null) => {
walletState.source = source;
};
connex.wallet.onDisconnected(onDisconnected);
connex.wallet.onSourceChanged(onSourceChanged);
configureThorModal(connex);
Expand All @@ -68,7 +73,6 @@ export default defineComponent({
const setSource = (source: WalletSource) => {
connex.wallet.setSource(source);
walletState.source = source;
};
const connect = async (): Promise<ConnectResponse> => {
Expand Down
24 changes: 14 additions & 10 deletions packages/react-wallet-kit/src/ConnexProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const ConnexProvider: React.FC<ConnexProviderOptions> = ({
}, [connex.wallet]);

useEffect(() => {
const listener = (): void => {
const onDisconnected = (): void => {
setAccount(null);
setSource(null);

Expand All @@ -78,10 +78,20 @@ export const ConnexProvider: React.FC<ConnexProviderOptions> = ({
}
};

connex.wallet.onDisconnected(listener);
const onSourceChanged = (_src: WalletSource | null): void => {
setSource(_src);

if (persistState) {
_src ? persist('source', _src) : remove('source');
}
};

connex.wallet.onDisconnected(onDisconnected);
connex.wallet.onSourceChanged(onSourceChanged);

return () => {
connex.wallet.removeOnDisconnected(listener);
connex.wallet.removeOnDisconnected(onDisconnected);
connex.wallet.removeOnSourceChanged(onSourceChanged);
};
}, [connex.wallet, persistState]);

Expand Down Expand Up @@ -112,14 +122,8 @@ export const ConnexProvider: React.FC<ConnexProviderOptions> = ({
const updateSource = useCallback(
(_source: WalletSource) => {
connex.wallet.setSource(_source);

setSource(_source);

if (persistState) {
persist('source', _source);
}
},
[connex.wallet, persistState],
[connex.wallet],
);

const updateAccount = useCallback(
Expand Down
4 changes: 0 additions & 4 deletions packages/wallet-kit/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ type BaseWallet = Connex.Signer & {
*/
type ConnexWallet = BaseWallet & {
connect: () => Promise<ConnectResponse>;
signIn: (
msg?: Connex.Vendor.CertMessage,
options?: Connex.Signer.CertOptions,
) => Promise<Connex.Vendor.CertResponse>;
};

export interface ConnectResponse {
Expand Down
21 changes: 14 additions & 7 deletions packages/wallet-kit/src/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createWallet } from './create-wallet';
import { WalletSources } from './wallet';

const DISCONNECTED = 'disconnected';
const SOURCE_CHANGED = 'source-changed';

class WalletManager {
private wallets: Record<string, ConnexWallet | undefined> = {};
Expand Down Expand Up @@ -47,11 +48,6 @@ class WalletManager {

connect = (): Promise<ConnectResponse> => this.wallet.connect();

signIn = (
msg?: Connex.Vendor.CertMessage,
options?: Connex.Signer.CertOptions,
): Promise<Connex.Vendor.CertResponse> => this.wallet.signIn(msg, options);

disconnect = async (remote = false): Promise<void> => {
if (!this._source) {
return;
Expand All @@ -63,9 +59,9 @@ class WalletManager {
await wallet.disconnect?.();
}

this.eventEmitter.emit(DISCONNECTED);

this._source = null;
this.eventEmitter.emit(SOURCE_CHANGED, null);
this.eventEmitter.emit(DISCONNECTED);
};

signTx = (
Expand Down Expand Up @@ -96,6 +92,7 @@ class WalletManager {
}

this._source = src;
this.eventEmitter.emit(SOURCE_CHANGED, src);
};

getSource = (): WalletSource | null => this._source;
Expand Down Expand Up @@ -125,6 +122,16 @@ class WalletManager {
removeOnDisconnected(listener: () => void): void {
this.eventEmitter.off(DISCONNECTED, listener);
}

onSourceChanged(listener: (source: WalletSource | null) => void): void {
this.eventEmitter.on(SOURCE_CHANGED, listener);
}

removeOnSourceChanged(
listener: (source: WalletSource | null) => void,
): void {
this.eventEmitter.off(SOURCE_CHANGED, listener);
}
}

export { WalletManager };
15 changes: 1 addition & 14 deletions packages/wallet-kit/src/wallets/certificate-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Certificate } from 'thor-devkit';
import type { BaseWallet, ConnectResponse, ConnexWallet } from '../types';
import {
DEFAULT_CONNECT_CERT_MESSAGE,
DEFAULT_SIGN_IN_MESSAGE,
} from '../certificates';
import { DEFAULT_CONNECT_CERT_MESSAGE } from '../certificates';

/**
* A `ConnexWallet` for wallet's that use a certificate connection
Expand Down Expand Up @@ -46,16 +43,6 @@ class CertificateBasedWallet implements ConnexWallet {
): Promise<Connex.Vendor.CertResponse> =>
this.wallet.then((w) => w.signCert(msg, options));

signIn = (
msg?: Connex.Vendor.CertMessage | undefined,
options?: Connex.Signer.CertOptions | undefined,
): Promise<Connex.Vendor.CertResponse> => {
const _msg = msg || DEFAULT_SIGN_IN_MESSAGE;
const _options = options || {};

return this.signCert(_msg, _options);
};

signTx = (
msg: Connex.Vendor.TxMessage,
options: Connex.Signer.TxOptions,
Expand Down
11 changes: 0 additions & 11 deletions packages/wallet-kit/src/wallets/wc-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { WCSigner } from '@vechain/wallet-connect';
import type { ConnectResponse, ConnexWallet } from '../types';
import { DEFAULT_SIGN_IN_MESSAGE } from '../certificates';

class WCWallet implements ConnexWallet {
constructor(private readonly signer: WCSigner) {}
Expand All @@ -20,16 +19,6 @@ class WCWallet implements ConnexWallet {
): Promise<Connex.Vendor.CertResponse> =>
this.signer.signCert(msg, options);

signIn = (
msg?: Connex.Vendor.CertMessage | undefined,
options?: Connex.Signer.CertOptions | undefined,
): Promise<Connex.Vendor.CertResponse> => {
const _msg = msg || DEFAULT_SIGN_IN_MESSAGE;
const _options = options || {};

return this.signCert(_msg, _options);
};

signTx = (
msg: Connex.Vendor.TxMessage,
options: Connex.Signer.TxOptions,
Expand Down

0 comments on commit 685e46a

Please sign in to comment.