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

fix: add configurable connection certificate #192

Merged
merged 3 commits into from
Feb 8, 2024
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
3 changes: 3 additions & 0 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}): React.ReactElement => {
const connex = useMemo(
() =>
Expand All @@ -47,6 +48,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
language,
modalParent,
onSourceClick,
connectionCertificate: connectionCertificateData,
}),
[
nodeUrl,
Expand All @@ -61,6 +63,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
language,
modalParent,
onSourceClick,
connectionCertificateData,
],
);

Expand Down
2 changes: 2 additions & 0 deletions packages/dapp-kit-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type React from 'react';
import * as ThorDevkit from 'thor-devkit';
import type { ConnectResponse, WalletSource } from '@vechain/dapp-kit';
import { type DAppKitUIOptions } from '@vechain/dapp-kit-ui';
export type { WalletConnectOptions, DAppKitOptions } from '@vechain/dapp-kit';
export type { DAppKitUIOptions } from '@vechain/dapp-kit-ui';

export interface AccountState {
address: string | null;
Expand Down
18 changes: 14 additions & 4 deletions packages/dapp-kit/src/classes/certificate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import { DEFAULT_CONNECT_CERT_MESSAGE } from '../constants';
* A `ConnexWallet` for wallet's that use a certificate connection
*/
class CertificateBasedWallet implements ConnexWallet {
constructor(private readonly wallet: BaseWallet) {}
constructor(
private readonly wallet: BaseWallet,
private readonly connectionCertificateData?: {
message?: Connex.Vendor.CertMessage;
options?: Connex.Signer.CertOptions;
},
) {}

connect = async (): Promise<ConnectResponse> => {
const cert = DEFAULT_CONNECT_CERT_MESSAGE;
const certificateMessage =
this.connectionCertificateData?.message ||
DEFAULT_CONNECT_CERT_MESSAGE;
const certificateOptions =
this.connectionCertificateData?.options || {};
const {
annex: { domain, signer, timestamp },
signature,
} = await this.signCert(cert, {});
} = await this.signCert(certificateMessage, certificateOptions);

const connectionCertificate = {
...cert,
...certificateMessage,
signature,
signer,
domain,
Expand Down
10 changes: 7 additions & 3 deletions packages/dapp-kit/src/classes/wallet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ class WalletManager {

// this is needed for wallet connect connections when a connection certificate is required
signConnectionCertificate = async (): Promise<ConnectResponse> => {
const cert = DEFAULT_CONNECT_CERT_MESSAGE;
const certificateMessage =
this.options.connectionCertificate?.message ||
DEFAULT_CONNECT_CERT_MESSAGE;
const certificateOptions =
this.options.connectionCertificate?.options || {};
const {
annex: { domain, signer, timestamp },
signature,
} = await this.wallet.signCert(cert, {});
} = await this.wallet.signCert(certificateMessage, certificateOptions);

const connectionCertificate = {
...cert,
...certificateMessage,
signature,
signer,
domain,
Expand Down
4 changes: 4 additions & 0 deletions packages/dapp-kit/src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ interface DAppKitOptions {
useFirstDetectedSource?: boolean;
logLevel?: LogLevel;
requireCertificate?: boolean;
connectionCertificate?: {
message?: Connex.Vendor.CertMessage;
options?: Connex.Signer.CertOptions;
};
}

type BaseWallet = Connex.Signer & {
Expand Down
13 changes: 10 additions & 3 deletions packages/dapp-kit/src/utils/create-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const createWallet = ({
genesis,
walletConnectOptions,
onDisconnected,
connectionCertificate,
}: ICreateWallet): ConnexWallet => {
const genesisId = normalizeGenesisId(genesis);

Expand All @@ -38,12 +39,18 @@ export const createWallet = ({

const vendor = new ConnexLib.Connex.Vendor(genesisId, 'sync');

return new CertificateBasedWallet(convertVendorToSigner(vendor));
return new CertificateBasedWallet(
convertVendorToSigner(vendor),
connectionCertificate,
);
}
case 'sync2': {
const vendor = new ConnexLib.Connex.Vendor(genesisId, 'sync2');

return new CertificateBasedWallet(convertVendorToSigner(vendor));
return new CertificateBasedWallet(
convertVendorToSigner(vendor),
connectionCertificate,
);
}
case 'veworld': {
if (!window.vechain) {
Expand All @@ -52,7 +59,7 @@ export const createWallet = ({

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

return new CertificateBasedWallet(signer);
return new CertificateBasedWallet(signer, connectionCertificate);
}
case 'wallet-connect': {
if (!walletConnectOptions) {
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@vechain/repo-config/src/tsconfig/base.json"
}
Loading