diff --git a/packages/dapp-kit-ui/vite.config.ts b/packages/dapp-kit-ui/vite.config.ts index 3c24d5b5..39c466e4 100644 --- a/packages/dapp-kit-ui/vite.config.ts +++ b/packages/dapp-kit-ui/vite.config.ts @@ -1,6 +1,6 @@ /// 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({ @@ -19,6 +19,10 @@ export default defineConfig({ 'text-summary', 'text', ], + lines: 80, + statements: 80, + functions: 60, + branches: 80, }, globals: true, }, diff --git a/packages/dapp-kit/src/connex.ts b/packages/dapp-kit/src/connex.ts index 87e3c45c..7c1c50dd 100644 --- a/packages/dapp-kit/src/connex.ts +++ b/packages/dapp-kit/src/connex.ts @@ -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 = {}; + +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; @@ -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; } } diff --git a/packages/dapp-kit/src/create-wallet.ts b/packages/dapp-kit/src/create-wallet.ts index 0d25f134..3aed4269 100644 --- a/packages/dapp-kit/src/create-wallet.ts +++ b/packages/dapp-kit/src/create-wallet.ts @@ -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; @@ -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) { @@ -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) { diff --git a/packages/dapp-kit/src/genesis.ts b/packages/dapp-kit/src/genesis.ts index 7a38e739..8dc91acb 100644 --- a/packages/dapp-kit/src/genesis.ts +++ b/packages/dapp-kit/src/genesis.ts @@ -1,6 +1,54 @@ -import { genesisBlocks } from '@vechain/connex/esm/config'; import type { Genesis } from './types'; +type NetworkType = 'main' | 'test'; + +export const genesisBlocks: Record = { + 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; diff --git a/packages/dapp-kit/src/index.ts b/packages/dapp-kit/src/index.ts index 2af3eaf9..11d7dd55 100644 --- a/packages/dapp-kit/src/index.ts +++ b/packages/dapp-kit/src/index.ts @@ -2,6 +2,7 @@ export * from './connex'; export * from './wallet'; export * from './wallet-manager'; export * from './wallet-connect'; +export * from './genesis'; export type { ConnexOptions, diff --git a/packages/dapp-kit/src/types.d.ts b/packages/dapp-kit/src/types.d.ts index 155c61f1..4a91641e 100644 --- a/packages/dapp-kit/src/types.d.ts +++ b/packages/dapp-kit/src/types.d.ts @@ -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'; @@ -7,7 +6,7 @@ declare global { vechain?: { newConnexSigner: (genesisId: string) => Connex.Signer; }; - connex?: Connex1; + connex?: unknown; } } diff --git a/packages/dapp-kit/src/vendor-signer.ts b/packages/dapp-kit/src/vendor-signer.ts new file mode 100644 index 00000000..ad316bdd --- /dev/null +++ b/packages/dapp-kit/src/vendor-signer.ts @@ -0,0 +1,58 @@ +export const convertVendorToSigner = (vendor: Connex.Vendor): Connex.Signer => { + return { + signTx: (msg, options): Promise => { + 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 => { + 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(); + }, + }; +}; diff --git a/packages/dapp-kit/src/wallets/certificate-wallet.ts b/packages/dapp-kit/src/wallets/certificate-wallet.ts index 2d8b02a4..f7b4afa6 100644 --- a/packages/dapp-kit/src/wallets/certificate-wallet.ts +++ b/packages/dapp-kit/src/wallets/certificate-wallet.ts @@ -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) {} + constructor(private readonly wallet: BaseWallet) {} connect = async (): Promise => { const cert = DEFAULT_CONNECT_CERT_MESSAGE; @@ -41,16 +41,14 @@ class CertificateBasedWallet implements ConnexWallet { msg: Connex.Vendor.CertMessage, options: Connex.Signer.CertOptions, ): Promise => - this.wallet.then((w) => w.signCert(msg, options)); + this.wallet.signCert(msg, options); signTx = ( msg: Connex.Vendor.TxMessage, options: Connex.Signer.TxOptions, - ): Promise => - this.wallet.then((w) => w.signTx(msg, options)); + ): Promise => this.wallet.signTx(msg, options); - disconnect = async (): Promise => - this.wallet.then((w) => w.disconnect?.()); + disconnect = async (): Promise => this.wallet.disconnect?.(); } export { CertificateBasedWallet }; diff --git a/packages/dapp-kit/test/genesis.test.ts b/packages/dapp-kit/test/genesis.test.ts index 8ebc321d..2c449044 100644 --- a/packages/dapp-kit/test/genesis.test.ts +++ b/packages/dapp-kit/test/genesis.test.ts @@ -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, diff --git a/packages/dapp-kit/test/wallet-tests/sync.test.ts b/packages/dapp-kit/test/wallet-tests/sync.test.ts index bdbe2d44..b99b4669 100644 --- a/packages/dapp-kit/test/wallet-tests/sync.test.ts +++ b/packages/dapp-kit/test/wallet-tests/sync.test.ts @@ -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 => - 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'), + }, }; }); diff --git a/packages/dapp-kit/test/wallet-tests/sync2.test.ts b/packages/dapp-kit/test/wallet-tests/sync2.test.ts index 5f54be16..dcba5bb4 100644 --- a/packages/dapp-kit/test/wallet-tests/sync2.test.ts +++ b/packages/dapp-kit/test/wallet-tests/sync2.test.ts @@ -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 => - Promise.resolve(mockedConnexSigner), + sign: (type, msg) => { + if (type === 'tx') { + return { + request: () => { + return mockedConnexSigner.signTx(msg, {}); + }, + }; + } else { + return { + request: () => { + return mockedConnexSigner.signCert(msg, {}); + }, + }; + } + }, }; }); diff --git a/packages/dapp-kit/vite.config.ts b/packages/dapp-kit/vite.config.ts index f0bf9c60..4b12fd57 100644 --- a/packages/dapp-kit/vite.config.ts +++ b/packages/dapp-kit/vite.config.ts @@ -1,6 +1,6 @@ /// import { resolve } from 'node:path'; -import { defineConfig } from 'vitest/config'; +import { defineConfig } from 'vitest/config'; // eslint-disable-next-line import/no-default-export // eslint-disable-next-line import/no-default-export export default defineConfig({ @@ -10,7 +10,19 @@ export default defineConfig({ setupFiles: [resolve(__dirname, 'test/setup/setup.ts')], reporters: 'dot', coverage: { - reporter: ['text', 'json', 'html'], + reporter: [ + 'text', + 'json', + 'html', + 'lcov', + 'json-summary', + 'text-summary', + 'text', + ], + lines: 80, + statements: 80, + functions: 80, + branches: 80, }, globals: true, },