Skip to content

Commit

Permalink
fix: align with main
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed Aug 19, 2024
2 parents 1b96294 + 79cdc48 commit 50d86fc
Show file tree
Hide file tree
Showing 31 changed files with 6,984 additions and 2,251 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/sonar-scan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: PR Sonar Scan

on:
push:
branches:
- main
pull_request:

jobs:
sonar-scan:
name: Sonar Scan
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.verbose=false
-Dsonar.branch.name=${{ github.head_ref || github.ref_name }}
3 changes: 1 addition & 2 deletions examples/sample-angular-app/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Angular modules
import { NgModule } from '@angular/core';
import { Routes } from '@angular/router';
import { RouterModule } from '@angular/router';
import { type Routes, RouterModule } from '@angular/router';

const routes: Routes = [];

Expand Down
9 changes: 2 additions & 7 deletions examples/sample-angular-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Angular modules
import { HttpClient } from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { DatePipe } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// External modules
import { TranslateModule } from '@ngx-translate/core';
import { TranslateLoader } from '@ngx-translate/core';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { AngularSvgIconModule } from 'angular-svg-icon';

// Internal modules
import { AppRoutingModule } from './app-routing.module';

// Components
import { AppComponent } from './app.component';

Expand Down
2 changes: 1 addition & 1 deletion examples/sample-react-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineConfig(({ mode }) => {
//vitest
test: {
globals: true,
environment: 'jsdom',
environment: 'happy-dom',
setupFiles: [
resolve(__dirname, 'test/setup/setup.ts'),
resolve(__dirname, 'test/setup/resizeObserverMock.ts'),
Expand Down
5 changes: 2 additions & 3 deletions packages/dapp-kit-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
"@vechain/connex-framework": "2.1.0",
"@vechain/dapp-kit": "*",
"@vechain/dapp-kit-ui": "*",
"@vechain/sdk-core": "1.0.0-beta.12",
"thor-devkit": "2.0.5",
"@vechain/sdk-core": "1.0.0-beta.24",
"valtio": "1.11.2"
},
"devDependencies": {
"@testing-library/react": "^14.1.2",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@vechain/repo-config": "https://github.com/vechain/repo-config#v0.0.1",
"eslint": "*",
"eslint": "^8.15.0",
"react": "^18.2.0",
"tsup": "*",
"typescript": "*",
Expand Down
6 changes: 2 additions & 4 deletions packages/dapp-kit-react/src/DAppKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { WalletSource } from '@vechain/dapp-kit';
import { DAppKitUI } from '@vechain/dapp-kit-ui';
import { subscribeKey } from 'valtio/vanilla/utils';
import type { DAppKitProviderOptions, DAppKitContext } from './types';
import * as ThorDevkit from 'thor-devkit';
import { type Certificate } from '@vechain/sdk-core';

/**
* Context
Expand Down Expand Up @@ -74,9 +74,7 @@ export const DAppKitProvider: React.FC<DAppKitProviderOptions> = ({
connex.wallet.state.source,
);
const [connectionCertificate, setConnectionCertificate] =
useState<ThorDevkit.Certificate | null>(
connex.wallet.state.connectionCertificate,
);
useState<Certificate | null>(connex.wallet.state.connectionCertificate);

useEffect(() => {
const addressSub = subscribeKey(connex.wallet.state, 'address', (v) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/dapp-kit-react/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference types="@vechain/connex" />
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';
import { type Certificate } from '@vechain/sdk-core';

export type { WalletConnectOptions, DAppKitOptions } from '@vechain/dapp-kit';
export type { DAppKitUIOptions } from '@vechain/dapp-kit-ui';

Expand Down Expand Up @@ -37,7 +38,7 @@ export interface DAppKitContext {
connect: () => Promise<ConnectResponse>;
account: string | null;
source: WalletSource | null;
connectionCertificate: ThorDevkit.Certificate | null;
connectionCertificate: Certificate | null;
};
modal: {
open: () => void;
Expand Down
28 changes: 18 additions & 10 deletions packages/dapp-kit-react/test/helpers/mocked-signer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
/// <reference types="@vechain/connex" />
import { blake2b256, Certificate, HDNode, secp256k1 } from 'thor-devkit';
import {
addressUtils,
blake2b256,
Certificate,
HDNode,
secp256k1,
certificate,
Hex0x,
} from '@vechain/sdk-core';

const mnemonicWords =
'denial kitchen pet squirrel other broom bar gas better priority spoil cross';

const hdNode = HDNode.fromMnemonic(mnemonicWords.split(' '));

const firstAccount = hdNode.derive(0);
const firstAccount = hdNode.deriveChild(0);

const privateKey: Buffer = firstAccount.privateKey!;
const address = firstAccount.address;
const privateKey = firstAccount.privateKey!;
const address = addressUtils.fromPrivateKey(privateKey);

const mockedConnexSigner: Connex.Signer = {
signTx() {
return Promise.resolve({ txid: '0x1234', signer: address });
},

signCert(msg) {
const certificate: Certificate = {
const newCertificate: Certificate = {
domain: ' localhost:3000',
timestamp: 12341234,
signer: address,
Expand All @@ -26,17 +34,17 @@ const mockedConnexSigner: Connex.Signer = {
};

const signature = secp256k1.sign(
blake2b256(Certificate.encode(certificate)),
blake2b256(certificate.encode(newCertificate)),
privateKey,
);

return Promise.resolve({
annex: {
domain: certificate.domain,
timestamp: certificate.timestamp,
signer: certificate.signer,
domain: newCertificate.domain,
timestamp: newCertificate.timestamp,
signer: newCertificate.signer,
},
signature: `0x${signature.toString('hex')}`,
signature: Hex0x.of(signature),
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-react/test/useWallet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('useWallet', () => {
await waitFor(() => {
expect(result.current.source).toBe('sync2');
expect(result.current.account).toBe(
'0xf077b491b355e64048ce21e3a6fc4751eeea77fa',
'0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa',
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['test/**/*.test.ts', 'test/**/*.test.tsx'],
environment: 'jsdom',
environment: 'happy-dom',
reporters: 'dot',
setupFiles: [resolve(__dirname, 'test/setup/setup.ts')],
coverage: {
Expand Down
30 changes: 14 additions & 16 deletions packages/dapp-kit-ui/src/components/buttons/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,20 @@ export class Button extends LitElement {
mobile = false;

override render(): TemplateResult {
return html`
${this.address
? html`<vdk-address-button
.mode=${this.mode}
.address=${this.address}
.disabled=${this.disabled}
.mobile=${this.mobile}
></vdk-address-button>`
: html`<vdk-connect-button
.mode=${this.mode}
.i18n=${this.i18n}
.language=${this.language}
.disabled=${this.disabled}
.mobile=${this.mobile}
></vdk-connect-button>`}
`;
return this.address
? html`<vdk-address-button
.mode=${this.mode}
.address=${this.address}
.disabled=${this.disabled}
.mobile=${this.mobile}
></vdk-address-button>`
: html`<vdk-connect-button
.mode=${this.mode}
.i18n=${this.i18n}
.language=${this.language}
.disabled=${this.disabled}
.mobile=${this.mobile}
></vdk-connect-button>`;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit-ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolve } from 'node:path'; // eslint-disable-next-line import/no-defau
export default defineConfig({
test: {
include: ['test/**/*.test.ts'],
environment: 'jsdom',
environment: 'happy-dom',
reporters: 'dot',
setupFiles: [resolve(__dirname, 'test/setup/setup.ts')],
coverage: {
Expand Down
7 changes: 3 additions & 4 deletions packages/dapp-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
"@vechain/connex-driver": "2.1.0",
"@vechain/connex-framework": "2.1.0",
"@vechain/connex-types": "^2.1.0",
"@vechain/sdk-core": "1.0.0-beta.12",
"@vechain/sdk-core": "1.0.0-beta.24",
"@walletconnect/modal": "2.6.2",
"@walletconnect/sign-client": "2.10.2",
"@walletconnect/utils": "2.10.2",
"events": "^3.3.0",
"thor-devkit": "2.0.5",
"valtio": "1.11.2"
},
"devDependencies": {
Expand All @@ -41,8 +40,8 @@
"@vechain/repo-config": "https://github.com/vechain/repo-config#v0.0.1",
"@vitest/coverage-v8": "^0.34.6",
"@walletconnect/types": "2.10.2",
"eslint": "*",
"happy-dom": "^12.10.3",
"eslint": "^8.15.0",
"happy-dom": "^14.12.3",
"lokijs": "^1.5.12",
"tsup": "*",
"typescript": "*",
Expand Down
31 changes: 24 additions & 7 deletions packages/dapp-kit/src/dapp-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,40 @@ import {
SimpleNet,
} from '@vechain/connex-driver/dist/index.js';
import { Framework } from '@vechain/connex-framework';
import * as ThorDevkit from 'thor-devkit';
import { blake2b256, Hex } from '@vechain/sdk-core';
import { WalletManager } from './classes';
import { DAppKitLogger, normalizeGenesisBlock } from './utils';
import type { DAppKitOptions } from './types';

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

/**
* START: TEMPORARY COMMENT
* For hashing we will improve SDK conversion and encoding later
* END: TEMPORARY COMMENT
*
* Create a new Thor driver
*
* @param node - The node URL
* @param genesis - The genesis block
*/
const createThorDriver = (
node: string,
genesis: Connex.Thor.Block,
): DriverNoVendor => {
const key = ThorDevkit.blake2b256(
JSON.stringify({
node,
genesis,
}),
).toString('hex');
// Stringify the certificate to hash
const certificateToHash = JSON.stringify({
node,
genesis,
});

// Encode the certificate to hash
const encodedCertificateToHash = new TextEncoder().encode(
certificateToHash.normalize(),
);

// Get the key (the hash of the certificate) without 0x prefix
const key: string = Hex.canon(blake2b256(encodedCertificateToHash, 'hex'));

let driver = cache[key];
if (!driver) {
Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit/src/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Certificate } from '@vechain/sdk-core';
import type { WalletConnectOptions } from '@vechain/dapp-kit';
import type { LogLevel } from '../utils/logger';
import { type Certificate } from '@vechain/sdk-core';

Check warning on line 2 in packages/dapp-kit/src/types/types.d.ts

View workflow job for this annotation

GitHub Actions / Lint, Build & Test

`@vechain/sdk-core` import should occur before type import of `@vechain/dapp-kit`
import type { LogLevel } from '../utils';

declare global {
interface Window {
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/src/utils/create-wc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
ResolvedSignClient,
WalletConnectOptions,
WCClient,
} from '../types/wc-types';
} from '../types';

const _cachedClients: Record<string, WCClient | undefined> = {};

Expand Down
2 changes: 1 addition & 1 deletion packages/dapp-kit/src/utils/create-wc-modal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WalletConnectModal } from '@walletconnect/modal';
import type { WCModal } from '../types/wc-types';
import type { WCModal } from '../types';

const _cachedModals: Record<string, WalletConnectModal | undefined> = {};

Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-kit/src/utils/create-wc-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
} from '@walletconnect/types';
import { getSdkError } from '@walletconnect/utils';
import type { SignClient } from '@walletconnect/sign-client/dist/types/client';
import type { WCSigner, WCSignerOptions } from '../types/wc-types';
import { DefaultMethods } from '../constants/wallet-connect';
import type { WCSigner, WCSignerOptions } from '../types';
import { DefaultMethods } from '../constants';
import { DAppKitLogger } from './logger';

interface SessionAccount {
Expand Down
10 changes: 4 additions & 6 deletions packages/dapp-kit/src/utils/local-storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as ThorDevkit from 'thor-devkit';
import { type Certificate } from '@vechain/sdk-core';
import type { WalletSource } from '../types';
import { DAppKitLogger } from './logger';

Expand Down Expand Up @@ -26,9 +26,7 @@ const setAccount = (account: string | null): void => {
}
};

const setConnectionCertificate = (
certificate: ThorDevkit.Certificate | null,
): void => {
const setConnectionCertificate = (certificate: Certificate | null): void => {
DAppKitLogger.debug(
'LocalStorage',
'setConnectionCertificate',
Expand Down Expand Up @@ -61,14 +59,14 @@ const getAccount = (): string | null => {
return account;
};

const getConnectionCertificate = (): ThorDevkit.Certificate | null => {
const getConnectionCertificate = (): Certificate | null => {
const connectionCertificate = localStorage.getItem(CERTIFICATE_KEY);

if (!connectionCertificate) {
return null;
}

return JSON.parse(connectionCertificate) as ThorDevkit.Certificate;
return JSON.parse(connectionCertificate) as Certificate;
};

export const Storage = {
Expand Down
Loading

0 comments on commit 50d86fc

Please sign in to comment.