Skip to content

Commit

Permalink
Implement IFrameProvider (#46)
Browse files Browse the repository at this point in the history
* upgrade CrossWindowProvider and WindowManager and fix specs

* small fixes

* implement IFrameProvider

* refactoring

* use safewindow

* revert changes related to popup consent on cross window provider

* refactoring according to code review

* refactoring according to code review

* refactoring

* refactoring

* refactoring

* refactoring according to code review

* changelog update

* 0.3.0-alpha.0

* small refactoring
  • Loading branch information
CiprianDraghici authored Aug 1, 2024
1 parent d912b78 commit d690029
Show file tree
Hide file tree
Showing 18 changed files with 4,994 additions and 66 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Implement IFrameProvider](https://github.com/multiversx/mx-wallet-dapp/pull/46)

## [[0.1.3](https://github.com/multiversx/mx-wallet-dapp/pull/45)] - 2024-05-29
- [Changed lit library to vanilla](https://github.com/multiversx/mx-wallet-dapp/pull/44)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiversx/sdk-web-wallet-cross-window-provider",
"version": "0.2.0",
"version": "0.3.0-alpha.0",
"description": "Signing provider for dApps: Cross Window",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
47 changes: 29 additions & 18 deletions src/CrossWindowProvider/CrossWindowProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SignableMessage, Transaction } from '@multiversx/sdk-core';
import { safeWindow } from '@multiversx/sdk-dapp-utils/out/constants/crossWindowProviderConstants';
import {
CrossWindowProviderRequestEnums,
CrossWindowProviderResponseEnums,
SignMessageStatusEnum
} from '@multiversx/sdk-dapp-utils/out/enums/crossWindowProviderEnums';
import { WindowManager } from '../WindowManager';
import { safeWindow } from '../constants';
import {
ErrAccountNotConnected,
ErrCannotSignSingleTransaction,
Expand All @@ -17,10 +16,11 @@ import {
ErrProviderNotInitialized,
ErrTransactionCancelled
} from '../errors';
import { WindowManager } from '../WindowManager';
import { PopupConsent } from './PopupConsent';
import { confirmationDialogTag } from './PopupConsent/constants';

interface ICrossWindowWalletAccount {
export interface ICrossWindowWalletAccount {
address: string;
signature?: string;
multisig?: string;
Expand All @@ -29,46 +29,59 @@ interface ICrossWindowWalletAccount {

export class CrossWindowProvider {
public account: ICrossWindowWalletAccount = { address: '' };
private initialized = false;
private windowManager: WindowManager;
private static _instance: CrossWindowProvider = new CrossWindowProvider();
protected initialized = false;
protected windowManager: WindowManager;
protected static _instance: CrossWindowProvider | null = null;
private accessToken: string | undefined = undefined;
protected _shouldShowConsentPopup = false;

private constructor() {
if (CrossWindowProvider._instance) {
throw new ErrInstantiationFailed();
}
this.windowManager = WindowManager.getInstance();
CrossWindowProvider._instance = this;
public constructor() {
this.windowManager = new WindowManager();
}

public setShouldShowConsentPopup(shouldShow: boolean) {
this._shouldShowConsentPopup = shouldShow;
}

private ensureConnected() {
protected ensureConnected() {
if (!this.account.address) {
throw new ErrAccountNotConnected();
}
}

private disconnect() {
protected disconnect() {
this.account = { address: '' };
}

public static getInstance(): CrossWindowProvider {
if (!CrossWindowProvider._instance) {
CrossWindowProvider._instance = new CrossWindowProvider();
return CrossWindowProvider._instance;
}

return CrossWindowProvider._instance;
}

public getWindowManager(): WindowManager {
return this.windowManager;
}

public setAddress(address: string): CrossWindowProvider {
if (!CrossWindowProvider._instance) {
throw new ErrInstantiationFailed();
}

this.account.address = address;
return CrossWindowProvider._instance;
}

public setWalletUrl(url: string): CrossWindowProvider {
this.windowManager.setWalletUrl(url);
return CrossWindowProvider._instance;
return this;
}

public setWalletWindow(): Promise<void> {
return this.windowManager.setWalletWindow();
}

async init(): Promise<boolean> {
Expand Down Expand Up @@ -279,8 +292,6 @@ export class CrossWindowProvider {
CrossWindowProviderRequestEnums.cancelAction
);

console.log(isWalletOpened);

if (!isWalletOpened) {
return;
}
Expand All @@ -291,7 +302,7 @@ export class CrossWindowProvider {
});
}

protected async openPopupConsent(): Promise<boolean> {
public async openPopupConsent(): Promise<boolean> {
await import('./PopupConsent/PopupConsent');
const dialog = safeWindow.document?.createElement('div');
const document = safeWindow.document;
Expand Down
4 changes: 2 additions & 2 deletions src/CrossWindowProvider/PopupConsent/PopupConsent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { safeWindow } from '@multiversx/sdk-dapp-utils/out/constants/crossWindowProviderConstants';
import { safeWindow } from '../../constants';
import { confirmationDialogTag } from './constants';
import { getStyles } from './getStyles';

Expand Down Expand Up @@ -99,7 +99,7 @@ export class PopupConsent extends HTMLElement {
}
}

const customElements = safeWindow?.customElements;
const customElements = safeWindow.customElements;
if (customElements && !customElements.get(confirmationDialogTag)) {
customElements.define(confirmationDialogTag, PopupConsent);
}
5 changes: 2 additions & 3 deletions src/CrossWindowProvider/tests/Cancel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWalletWindowMock, WalletWindowMockType } from '../../test-utils';
import { WindowManager } from '../../WindowManager/WindowManager';
import { CrossWindowProvider } from '../CrossWindowProvider';

describe('CrossWindowProvider Login', () => {
Expand All @@ -9,11 +8,11 @@ describe('CrossWindowProvider Login', () => {

beforeEach(() => {
walletWindowMock = getWalletWindowMock();
WindowManager.getInstance().postMessage = jest
CrossWindowProvider.getInstance().getWindowManager().postMessage = jest
.fn()
.mockImplementation(() => undefined);

WindowManager.getInstance().isWalletOpened = jest
CrossWindowProvider.getInstance().getWindowManager().isWalletOpened = jest
.fn()
.mockImplementation(() => true);

Expand Down
3 changes: 1 addition & 2 deletions src/CrossWindowProvider/tests/Login.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWalletWindowMock, WalletWindowMockType } from '../../test-utils';
import { WindowManager } from '../../WindowManager/WindowManager';
import { CrossWindowProvider } from '../CrossWindowProvider';

describe('CrossWindowProvider Login', () => {
Expand All @@ -9,7 +8,7 @@ describe('CrossWindowProvider Login', () => {

beforeEach(() => {
walletWindowMock = getWalletWindowMock();
WindowManager.getInstance().postMessage = jest
CrossWindowProvider.getInstance().getWindowManager().postMessage = jest
.fn()
.mockImplementation(() => ({
payload: {
Expand Down
5 changes: 2 additions & 3 deletions src/CrossWindowProvider/tests/Logout.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getWalletWindowMock, WalletWindowMockType } from '../../test-utils';
import { WindowManager } from '../../WindowManager/WindowManager';
import { CrossWindowProvider } from '../CrossWindowProvider';

describe('CrossWindowProvider', () => {
Expand All @@ -9,11 +8,11 @@ describe('CrossWindowProvider', () => {

beforeEach(() => {
walletWindowMock = getWalletWindowMock();
WindowManager.getInstance().postMessage = jest
CrossWindowProvider.getInstance().getWindowManager().postMessage = jest
.fn()
.mockImplementation(() => true);

WindowManager.getInstance().closeConnection = jest
CrossWindowProvider.getInstance().getWindowManager().closeConnection = jest
.fn()
.mockImplementation(() => true);

Expand Down
3 changes: 1 addition & 2 deletions src/CrossWindowProvider/tests/SignMessage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SignableMessage } from '@multiversx/sdk-core/out';
import { getWalletWindowMock, WalletWindowMockType } from '../../test-utils';
import { WindowManager } from '../../WindowManager/WindowManager';
import { CrossWindowProvider } from '../CrossWindowProvider';

describe('CrossWindowProvider Login', () => {
Expand All @@ -10,7 +9,7 @@ describe('CrossWindowProvider Login', () => {

beforeEach(() => {
walletWindowMock = getWalletWindowMock();
WindowManager.getInstance().postMessage = jest
CrossWindowProvider.getInstance().getWindowManager().postMessage = jest
.fn()
.mockImplementation(() => ({
payload: {
Expand Down
3 changes: 1 addition & 2 deletions src/CrossWindowProvider/tests/SignTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
getWalletWindowMock,
WalletWindowMockType
} from '../../test-utils';
import { WindowManager } from '../../WindowManager/WindowManager';
import { CrossWindowProvider } from '../CrossWindowProvider';

describe('CrossWindowProvider Login', () => {
Expand All @@ -22,7 +21,7 @@ describe('CrossWindowProvider Login', () => {
senderUsername: 'sender'
});

WindowManager.getInstance().postMessage = jest
CrossWindowProvider.getInstance().getWindowManager().postMessage = jest
.fn()
.mockImplementation(() => ({
payload: { data: [mockTransaction.toPlainObject()] }
Expand Down
Loading

0 comments on commit d690029

Please sign in to comment.