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

Integrate walletconnect #28

Closed
wants to merge 8 commits into from
Closed
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
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]

- [Iframe integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/29)
- [Walletconnect partial integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/28)
- [Metamask integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/27)
- [Extension integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/26)
- [Ledger integration](https://github.com/multiversx/mx-sdk-dapp-core/pull/22)
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
"@multiversx/sdk-native-auth-client": "^1.0.8",
"@multiversx/sdk-opera-provider": "1.0.0-alpha.1",
"@multiversx/sdk-wallet": "4.5.1",
"@multiversx/sdk-wallet-connect-provider": "4.1.2",
"@multiversx/sdk-wallet-connect-provider": "5.0.0",
"@multiversx/sdk-web-wallet-iframe-provider": "2.0.1",
"@multiversx/sdk-web-wallet-provider": "3.2.1",
"@types/qrcode": "1.5.5",
"isomorphic-fetch": "3.0.0",
"lodash": "4.17.21",
"lit": "3.2.1",
"protobufjs": "7.3.0",
"qrcode": "1.5.4",
"socket.io-client": "4.7.5",
"zustand": "4.4.7"
},
Expand Down
21 changes: 17 additions & 4 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { createIframeProvider } from './helpers/iframe/createIframeProvider';
import { IframeLoginTypes } from '@multiversx/sdk-web-wallet-iframe-provider/out/constants';
import { networkSelector } from 'store/selectors';
import { getState } from 'store/store';
import { createWalletconnectProvider } from './helpers/walletconnect/createWalletconnectProvider';

export class ProviderFactory {
public async create({
Expand All @@ -18,10 +19,12 @@ export class ProviderFactory {
customProvider
}: IProviderFactory): Promise<IProvider | undefined> {
let createdProvider: IProvider | undefined;
const { metamaskSnapWalletAddress = '', walletAddress } = {
const network = {
...networkSelector(getState()),
...config.network
};
const { metamaskSnapWalletAddress = '', walletAddress } = network;
const address = config.account?.address;

switch (type) {
case ProviderTypeEnum.extension: {
Expand All @@ -36,7 +39,7 @@ export class ProviderFactory {
case ProviderTypeEnum.crossWindow: {
const provider = await createCrossWindowProvider({
walletAddress,
address: config.account?.address
address
});
createdProvider = provider as unknown as IProvider;

Expand All @@ -61,7 +64,7 @@ export class ProviderFactory {

case ProviderTypeEnum.metamask: {
const provider = await createIframeProvider({
address: config.account?.address,
address,
metamaskSnapWalletAddress,
type: IframeLoginTypes.metamask
});
Expand All @@ -79,7 +82,7 @@ export class ProviderFactory {

case ProviderTypeEnum.passkey: {
const provider = await createIframeProvider({
address: config.account?.address,
address,
metamaskSnapWalletAddress,
type: IframeLoginTypes.passkey
});
Expand All @@ -95,6 +98,16 @@ export class ProviderFactory {
break;
}

case ProviderTypeEnum.walletconnect: {
createdProvider = createWalletconnectProvider({
network
});

createdProvider.getType = () => ProviderTypeEnum.passkey;

break;
}

case ProviderTypeEnum.custom: {
createdProvider = customProvider;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core/providers/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function getProviderType<TProvider extends object>(
case WalletProvider:
return ProviderTypeEnum.webhook;
case WalletConnectV2Provider:
return ProviderTypeEnum.walletConnect;
return ProviderTypeEnum.walletconnect;
case HWProvider:
return ProviderTypeEnum.ledger;
case ExtensionProvider:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import QRCode from 'qrcode';

@customElement('wallet-connect-modal')
export class WalletconnectModalComponent extends LitElement {
@property({ type: Boolean }) isOpen = false;
@property({ type: String }) qrCodeData = '';

static styles = css`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it mandatory to pe statically defined here? Do you think it could be better to extract in a separate file ?

.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 500px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
`;

render() {
return html`
<div class="modal" style="display: ${this.isOpen ? 'block' : 'none'}">
<div class="modal-content">
<span class="close" @click=${this.close}>&times;</span>
<h4>Connect using xPortal on your phone</h4>
<div id="qrContainer"></div>
<button @click=${this.close}>Close</button>
</div>
</div>
`;
}

async open(connectorUri: string) {
this.isOpen = true;
this.qrCodeData = await QRCode.toString(connectorUri, { type: 'svg' });
this.requestUpdate();
await this.updateComplete;
const qrContainer = this.shadowRoot!.getElementById('qrContainer');
if (qrContainer) {
qrContainer.innerHTML = this.qrCodeData;
}
}

close() {
this.isOpen = false;
}
}

export function createModalFunctions() {
const modalElement = document.createElement(
'wallet-connect-modal'
) as WalletconnectModalComponent;
document.body.appendChild(modalElement);

return {
openModal: async (connectorUri: string) => {
await modalElement.open(connectorUri);
},
closeModal: () => {
modalElement.close();
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { WalletConnectV2Provider } from '@multiversx/sdk-wallet-connect-provider';
import { IProvider } from 'core/providers/types/providerFactory.types';
import { createModalFunctions } from './components/WalletconnectModalComponent';
import { CurrentNetworkType } from 'types';

interface IWalletconnectProvider {
openModal?: (connectorUri: string) => Promise<void>;
closeModal?: () => void;
onClientLogin?: () => Promise<void>;
onClientLogout?: () => void;
onClientEvent?: (event: any) => void;
network: CurrentNetworkType;
}

export function createWalletconnectProvider(
props: IWalletconnectProvider
): IProvider {
const modalFunctions = createModalFunctions();
const openModal = props.openModal ?? modalFunctions.openModal;
const closeModal = props.closeModal ?? modalFunctions.closeModal;

const provider = new WalletConnectV2Provider(
prepareCallbacks(),
props.network.chainId,
props.network.walletConnectV2RelayAddress,
String(props.network.walletConnectV2ProjectId)
);

const walletconnectLogin = provider.login;

const createdProvider = provider as unknown as IProvider;

function prepareCallbacks() {
return {
onClientLogin: async function () {
closeModal();
const address = provider.getAddress();
console.log(`onClientLogin(), address: ${address}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still need this log ?

},
onClientLogout: function () {
console.log('onClientLogout()');
Copy link
Contributor

@CiprianDraghici CiprianDraghici Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe// TODO instead of logs ?

},
onClientEvent: function (event: any) {
console.log('onClientEvent()', event);
}
};
}

createdProvider.login = async (options?: {
callbackUrl?: string;
token?: string;
}): Promise<{ address: string; signature: string }> => {
await provider.init();
const { uri, approval } = await provider.connect();

if (!uri) {
throw 'URI not found';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this throw will be caught outside, where the login function is called since the throw inside the catch will be caught in the catch block here, in the function...is it intended? Do you think is better to have a uniform way to handle the error exceptions?

}

await openModal?.(uri);

try {
const account = await walletconnectLogin({
approval,
token: options?.token
});

const address = account?.address;
const signature = account?.signature;

if (!account) {
throw new Error(`Connection Proposal Refused ${account}`);
Copy link
Contributor

@CiprianDraghici CiprianDraghici Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this throw will be caught at the line:80

}

return {
address: address || '',
signature: signature || ''
};
} catch (err) {
throw new Error(`Connection Proposal Refused, ${err}`);
}
};

return createdProvider;
}
3 changes: 1 addition & 2 deletions src/core/providers/types/providerFactory.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { IDAppProviderBase } from '@multiversx/sdk-dapp-utils';
import { CustomNetworkType } from 'types/network.types';

// @ts-ignore
export interface IProvider extends IDAppProviderBase {
init: () => Promise<boolean>;
login: (options?: { callbackUrl?: string; token?: string }) => Promise<{
Expand Down Expand Up @@ -31,7 +30,7 @@ export enum ProviderTypeEnum {
iframe = 'iframe',
crossWindow = 'crossWindow',
extension = 'extension',
walletConnect = 'walletConnect',
walletconnect = 'walletconnect',
ledger = 'ledger',
opera = 'opera',
metamask = 'metamask',
Expand Down
Loading