Skip to content

Commit

Permalink
fix: not using internal connex libs
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 21, 2023
1 parent 0d5e8a1 commit 4aab3ad
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 44 deletions.
6 changes: 5 additions & 1 deletion packages/dapp-kit-ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config';
import { resolve } from 'node:path';
import { resolve } from 'node:path'; // eslint-disable-next-line import/no-default-export

// eslint-disable-next-line import/no-default-export
export default defineConfig({
Expand All @@ -19,6 +19,10 @@ export default defineConfig({
'text-summary',
'text',
],
lines: 80,
statements: 80,
functions: 60,
branches: 80,
},
globals: true,
},
Expand Down
45 changes: 31 additions & 14 deletions packages/dapp-kit/src/connex.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { createNoVendor, LazyDriver } 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 { DriverNoVendor, SimpleNet } from '@vechain/connex-driver';
import { Framework } from '@vechain/connex-framework';
import { blake2b256 } from 'thor-devkit';
import type { ConnexOptions } from './types';
import { normalizeGenesisBlock } from './genesis';
import { WalletManager } from './wallet-manager';

const cache: Record<string, DriverNoVendor | undefined> = {};

const createThorDriver = (
node: string,
genesis: Connex.Thor.Block,
): DriverNoVendor => {
const key = blake2b256(
JSON.stringify({
node,
genesis,
}),
).toString('hex');

let driver = cache[key];
if (!driver) {
driver = new DriverNoVendor(new SimpleNet(node), genesis);
cache[key] = driver;
}
return driver;
};

class MultiWalletConnex {
public readonly thor: Connex.Thor;
public readonly vendor: Connex.Vendor;
Expand All @@ -16,20 +36,17 @@ class MultiWalletConnex {

const genesisBlock = normalizeGenesisBlock(genesis);

const thorOnlyDriver: DriverNoVendor = createNoVendor(
nodeUrl,
genesisBlock,
);
const driver = createThorDriver(nodeUrl, genesisBlock);

const walletManager = new WalletManager(options);
const lazyDriver = new LazyDriver(Promise.resolve(walletManager));
lazyDriver.setNoVendor(thorOnlyDriver);

const thor = newThor(lazyDriver);
const vendor = newVendor(lazyDriver);
driver.signTx = walletManager.signTx.bind(walletManager);
driver.signCert = walletManager.signCert.bind(walletManager);

const framework = new Framework(driver);

this.thor = thor;
this.vendor = vendor;
this.thor = framework.thor;
this.vendor = framework.vendor;
this.wallet = walletManager;
}
}
Expand Down
13 changes: 7 additions & 6 deletions packages/dapp-kit/src/create-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createSync, createSync2 } from '@vechain/connex/esm/signer';
import { Connex } from '@vechain/connex';
import { createWcClient, createWcModal, newWcSigner } from './wallet-connect';
import type { ConnexOptions, ConnexWallet, WalletSource } from './types';
import { CertificateBasedWallet } from './wallets/certificate-wallet';
import { WCWallet } from './wallets/wc-wallet';
import { normalizeGenesisId } from './genesis';
import { convertVendorToSigner } from './vendor-signer';

type ICreateWallet = ConnexOptions & {
source: WalletSource;
Expand All @@ -25,14 +26,14 @@ export const createWallet = ({
throw new Error('User is not in a Sync wallet');
}

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

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

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

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

return new CertificateBasedWallet(Promise.resolve(signer));
return new CertificateBasedWallet(signer);
}
case 'wallet-connect': {
if (!walletConnectOptions) {
Expand Down
50 changes: 49 additions & 1 deletion packages/dapp-kit/src/genesis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
import { genesisBlocks } from '@vechain/connex/esm/config';
import type { Genesis } from './types';

type NetworkType = 'main' | 'test';

export const genesisBlocks: Record<NetworkType, Connex.Thor.Block> = {
main: {
number: 0,
id: '0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a',
size: 170,
parentID:
'0xffffffff53616c757465202620526573706563742c20457468657265756d2100',
timestamp: 1530316800,
gasLimit: 10000000,
beneficiary: '0x0000000000000000000000000000000000000000',
gasUsed: 0,
totalScore: 0,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x09bfdf9e24dd5cd5b63f3c1b5d58b97ff02ca0490214a021ed7d99b93867839c',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
signer: '0x0000000000000000000000000000000000000000',
isTrunk: true,
transactions: [],
},
test: {
number: 0,
id: '0x000000000b2bce3c70bc649a02749e8687721b09ed2e15997f466536b20bb127',
size: 170,
parentID:
'0xffffffff00000000000000000000000000000000000000000000000000000000',
timestamp: 1530014400,
gasLimit: 10000000,
beneficiary: '0x0000000000000000000000000000000000000000',
gasUsed: 0,
totalScore: 0,
txsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
txsFeatures: 0,
stateRoot:
'0x4ec3af0acbad1ae467ad569337d2fe8576fe303928d35b8cdd91de47e9ac84bb',
receiptsRoot:
'0x45b0cfc220ceec5b7c1c62c4d4193d38e4eba48e8815729ce75f9c0ab0e4c1c0',
signer: '0x0000000000000000000000000000000000000000',
isTrunk: true,
transactions: [],
},
};

const normalizeGenesisId = (genesis?: Genesis): string => {
if (!genesis) return genesisBlocks.main.id;

Expand Down
1 change: 1 addition & 0 deletions packages/dapp-kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './connex';
export * from './wallet';
export * from './wallet-manager';
export * from './wallet-connect';
export * from './genesis';

export type {
ConnexOptions,
Expand Down
3 changes: 1 addition & 2 deletions packages/dapp-kit/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Connex1 } from '@vechain/connex/esm/signer';
import type { WalletConnectOptions } from '@vechainfoundation/dapp-kit';
import type { WCModal } from './wallet-connect';

Expand All @@ -7,7 +6,7 @@ declare global {
vechain?: {
newConnexSigner: (genesisId: string) => Connex.Signer;
};
connex?: Connex1;
connex?: unknown;
}
}

Expand Down
58 changes: 58 additions & 0 deletions packages/dapp-kit/src/vendor-signer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export const convertVendorToSigner = (vendor: Connex.Vendor): Connex.Signer => {
return {
signTx: (msg, options): Promise<Connex.Vendor.TxResponse> => {
const service = vendor.sign('tx', msg);

if (options.gas) {
service.gas(options.gas);
}

if (options.signer) {
service.signer(options.signer);
}

if (options.dependsOn) {
service.dependsOn(options.dependsOn);
}

if (options.link) {
service.link(options.link);
}

if (options.comment) {
service.comment(options.comment);
}

if (options.delegator) {
service.delegate(
options.delegator.url,
options.delegator.signer,
);
}

if (options.onAccepted) {
service.accepted(options.onAccepted);
}

return service.request();
},

signCert: (msg, options): Promise<Connex.Vendor.CertResponse> => {
const service = vendor.sign('cert', msg);

if (options.signer) {
service.signer(options.signer);
}

if (options.link) {
service.link(options.link);
}

if (options.onAccepted) {
service.accepted(options.onAccepted);
}

return service.request();
},
};
};
10 changes: 4 additions & 6 deletions packages/dapp-kit/src/wallets/certificate-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DEFAULT_CONNECT_CERT_MESSAGE } from '../certificates';
* A `ConnexWallet` for wallet's that use a certificate connection
*/
class CertificateBasedWallet implements ConnexWallet {
constructor(private readonly wallet: Promise<BaseWallet>) {}
constructor(private readonly wallet: BaseWallet) {}

connect = async (): Promise<ConnectResponse> => {
const cert = DEFAULT_CONNECT_CERT_MESSAGE;
Expand Down Expand Up @@ -41,16 +41,14 @@ class CertificateBasedWallet implements ConnexWallet {
msg: Connex.Vendor.CertMessage,
options: Connex.Signer.CertOptions,
): Promise<Connex.Vendor.CertResponse> =>
this.wallet.then((w) => w.signCert(msg, options));
this.wallet.signCert(msg, options);

signTx = (
msg: Connex.Vendor.TxMessage,
options: Connex.Signer.TxOptions,
): Promise<Connex.Vendor.TxResponse> =>
this.wallet.then((w) => w.signTx(msg, options));
): Promise<Connex.Vendor.TxResponse> => this.wallet.signTx(msg, options);

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

export { CertificateBasedWallet };
7 changes: 5 additions & 2 deletions packages/dapp-kit/test/genesis.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, expect, it } from 'vitest';
import { genesisBlocks } from '@vechain/connex/esm/config';
import { normalizeGenesisBlock, normalizeGenesisId } from '../src/genesis';
import {
genesisBlocks,
normalizeGenesisBlock,
normalizeGenesisId,
} from '../src/genesis';

const customBlock = {
number: 0,
Expand Down
34 changes: 28 additions & 6 deletions packages/dapp-kit/test/wallet-tests/sync.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
import { beforeEach, expect, vi } from 'vitest';
import { mockedConnexSigner } from '../helpers/mocked-signer';
import { createUnitTestConnex } from '../helpers/connex-helper';
import { genesisBlocks } from '../../src';
import { Connex } from '@vechain/connex';

vi.mock('@vechain/connex/esm/signer', () => {
vi.mock('@vechain/connex');

vi.mocked(Connex.Vendor).mockImplementation((): Connex.Vendor => {
return {
createSync: (): Promise<Connex.Signer> =>
Promise.resolve(mockedConnexSigner),
sign: (type, msg) => {
if (type === 'tx') {
return {
request: () => {
return mockedConnexSigner.signTx(msg, {});
},
};
} else {
return {
request: () => {
return mockedConnexSigner.signCert(msg, {});
},
};
}
},
};
});

describe('sync', () => {
describe('is in sync browser', () => {
beforeEach(() => {
// @ts-ignore
window.connex = {
//eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-expect-error
test: 'hello world',
thor: {
genesis: genesisBlocks.main,
},
vendor: {
// @ts-ignore
sign: () => Promise.reject('Not implemented'),
},
};
});

Expand Down
24 changes: 20 additions & 4 deletions packages/dapp-kit/test/wallet-tests/sync2.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { expect, vi } from 'vitest';
import { mockedConnexSigner } from '../helpers/mocked-signer';
import { createUnitTestConnex } from '../helpers/connex-helper';
import { Connex } from '@vechain/connex';
import { mockedConnexSigner } from '../helpers/mocked-signer';

vi.mock('@vechain/connex');

vi.mock('@vechain/connex/esm/signer', () => {
vi.mocked(Connex.Vendor).mockImplementation((): Connex.Vendor => {
return {
createSync2: (): Promise<Connex.Signer> =>
Promise.resolve(mockedConnexSigner),
sign: (type, msg) => {
if (type === 'tx') {
return {
request: () => {
return mockedConnexSigner.signTx(msg, {});
},
};
} else {
return {
request: () => {
return mockedConnexSigner.signCert(msg, {});
},
};
}
},
};
});

Expand Down
Loading

0 comments on commit 4aab3ad

Please sign in to comment.