Skip to content

Commit ace6bcd

Browse files
committed
fix: implementing ConnectionRequest
1 parent c73d5f8 commit ace6bcd

16 files changed

Lines changed: 861 additions & 653 deletions

File tree

packages/sdk-multichain-ui/src/components.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import { HTMLStencilElement, JSXBase } from "@stencil/core/internal";
88
export namespace Components {
99
interface MmInstallModal {
10-
"link": any;
10+
"expiresIn": number;
11+
"link": string;
1112
"preferDesktop": boolean;
1213
"sdkVersion"?: string;
1314
}
@@ -33,6 +34,7 @@ declare global {
3334
"close": { shouldTerminate?: boolean };
3435
"startDesktopOnboarding": any;
3536
"updateLink": any;
37+
"updateExpiresIn": any;
3638
}
3739
interface HTMLMmInstallModalElement extends Components.MmInstallModal, HTMLStencilElement {
3840
addEventListener<K extends keyof HTMLMmInstallModalElementEventMap>(type: K, listener: (this: HTMLMmInstallModalElement, ev: MmInstallModalCustomEvent<HTMLMmInstallModalElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
@@ -74,9 +76,11 @@ declare global {
7476
}
7577
declare namespace LocalJSX {
7678
interface MmInstallModal {
77-
"link"?: any;
79+
"expiresIn"?: number;
80+
"link"?: string;
7881
"onClose"?: (event: MmInstallModalCustomEvent<{ shouldTerminate?: boolean }>) => void;
7982
"onStartDesktopOnboarding"?: (event: MmInstallModalCustomEvent<any>) => void;
83+
"onUpdateExpiresIn"?: (event: MmInstallModalCustomEvent<any>) => void;
8084
"onUpdateLink"?: (event: MmInstallModalCustomEvent<any>) => void;
8185
"preferDesktop"?: boolean;
8286
"sdkVersion"?: string;

packages/sdk-multichain-ui/src/components/mm-install-modal/mm-install-modal.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import SVG from '../../assets/fox.svg'
2323
shadow: true,
2424
})
2525
export class InstallModal {
26-
@Prop() link: any
26+
@Prop() link: string
27+
28+
@Prop() expiresIn: number;
2729

2830
@Prop() sdkVersion?: string;
2931

@@ -38,6 +40,8 @@ export class InstallModal {
3840

3941
@Event() updateLink: EventEmitter;
4042

43+
@Event() updateExpiresIn: EventEmitter;
44+
4145
@Element() el: HTMLElement;
4246

4347
@State() private translationsLoaded: boolean = false;
@@ -115,6 +119,11 @@ export class InstallModal {
115119
this.generateQRCode(link);
116120
}
117121

122+
@Watch('expiresIn')
123+
updateExpiresInHandler(expiresIn: number) {
124+
console.debug('QRCode expires in:', expiresIn);
125+
}
126+
118127
render() {
119128
if (!this.translationsLoaded) {
120129
return null; // or a loading state

packages/sdk-multichain/src/domain/multichain/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { StoreClient } from '../store';
22
import type { MultichainCore } from '.';
33
import type { RPC_URLS_MAP } from './api/types';
44
import type { ModalFactory } from '../../ui';
5+
import type { SessionRequest } from '@metamask/mobile-wallet-protocol-core';
6+
import type { PlatformType } from '../platform';
57

68
export type { SessionData } from '@metamask/multichain-api-client';
79

@@ -17,6 +19,14 @@ export type DappSettings = {
1719
url?: string;
1820
} & ({ iconUrl?: string } | { base64Icon?: string });
1921

22+
export type ConnectionRequest = {
23+
sessionRequest: SessionRequest;
24+
metadata: {
25+
dapp: DappSettings;
26+
sdk: { version: string; platform: PlatformType };
27+
};
28+
};
29+
2030
/**
2131
* Constructor options for creating a Multichain SDK instance.
2232
*
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import type { SessionRequest } from '@metamask/mobile-wallet-protocol-core';
21
import type { Components } from '@metamask/sdk-multichain-ui';
2+
import type { ConnectionRequest } from '../multichain';
33

44
export type OTPCode = string;
5+
export type QRLink = string;
56

67
export interface InstallWidgetProps extends Components.MmInstallModal {
78
parentElement?: Element;
9+
connectionRequest: ConnectionRequest;
810
onClose: (shouldTerminate?: boolean) => void;
911
startDesktopOnboarding: () => void;
10-
createSessionRequest: () => Promise<SessionRequest>;
11-
updateSessionRequest: (sessionRequest: SessionRequest) => void;
12+
createConnectionRequest: () => Promise<ConnectionRequest>;
13+
generateQRCode: (connectionRequest: ConnectionRequest) => Promise<QRLink>;
1214
}
1315

1416
export interface OTPCodeWidgetProps extends Components.MmOtpModal {
@@ -19,10 +21,12 @@ export interface OTPCodeWidgetProps extends Components.MmOtpModal {
1921
updateOTPCode: (otpValue: string) => void;
2022
}
2123

24+
type ModalData = { link: QRLink } | { otpCode: OTPCode };
25+
2226
/**
2327
* Abstract Modal class with shared functionality across all models
2428
*/
25-
export abstract class Modal<R = unknown, T = unknown> {
29+
export abstract class Modal<R extends OTPCode | QRLink = OTPCode | QRLink, T extends ModalData = ModalData> {
2630
protected abstract instance?: HTMLMmInstallModalElement | HTMLMmOtpModalElement | undefined;
2731

2832
abstract mount(): void;
@@ -31,30 +35,24 @@ export abstract class Modal<R = unknown, T = unknown> {
3135
constructor(protected readonly options: T) {}
3236

3337
get data() {
34-
// biome-ignore lint/suspicious/noExplicitAny: expected
35-
if ('sessionRequest' in (this.options as any)) {
36-
// biome-ignore lint/suspicious/noExplicitAny: expected
37-
return (this.options as any).sessionRequest;
38+
if ('link' in this.options) {
39+
return this.options.link as R;
3840
}
39-
// biome-ignore lint/suspicious/noExplicitAny: expected
40-
if ('otpCode' in (this.options as any)) {
41-
// biome-ignore lint/suspicious/noExplicitAny: expected
42-
return (this.options as any).otpCode;
41+
42+
if ('otpCode' in this.options) {
43+
return this.options.otpCode as R;
4344
}
4445

4546
throw new Error('Invalid options');
4647
}
4748

48-
set data(data: R extends OTPCode ? OTPCode : R extends SessionRequest ? SessionRequest : R) {
49-
// biome-ignore lint/suspicious/noExplicitAny: expected
50-
if ('sessionRequest' in (this.options as any)) {
51-
// biome-ignore lint/suspicious/noExplicitAny: expected
52-
(this.options as any).sessionRequest = data;
49+
set data(data: R) {
50+
if ('link' in this.options) {
51+
this.options.link = data;
5352
}
54-
// biome-ignore lint/suspicious/noExplicitAny: expected
55-
if ('otpCode' in (this.options as any)) {
56-
// biome-ignore lint/suspicious/noExplicitAny: expected
57-
(this.options as any).otpCode = data;
53+
54+
if ('otpCode' in this.options) {
55+
this.options.otpCode = data;
5856
}
5957
}
6058
}

packages/sdk-multichain/src/multichain/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CaipAccountId, Json } from '@metamask/utils';
44
import packageJson from '../../package.json';
55
import { type InvokeMethodOptions, type ModalFactoryConnectOptions, type MultichainOptions, type RPCAPI, type Scope, TransportType } from '../domain';
66
import { createLogger, enableDebug, isEnabled as isLoggerEnabled } from '../domain/logger';
7-
import { MultichainCore, type SDKState } from '../domain/multichain';
7+
import { type ConnectionRequest, MultichainCore, type SDKState } from '../domain/multichain';
88
import { getPlatformType, PlatformType } from '../domain/platform';
99
import { MWPTransport } from './mwp';
1010
import { RPCClient } from './rpc/client';
@@ -260,9 +260,19 @@ export class MultichainSDK extends MultichainCore {
260260
this.options.ui.factory.renderInstallModal(
261261
desktopPreferred,
262262
() => {
263-
return new Promise<SessionRequest>((resolveSession) => {
263+
return new Promise<ConnectionRequest>((resolveConnectionRequest) => {
264264
this.dappClient.on('session_request', (sessionRequest: SessionRequest) => {
265-
resolveSession(sessionRequest);
265+
const connectionRequest: ConnectionRequest = {
266+
sessionRequest,
267+
metadata: {
268+
dapp: this.options.dapp,
269+
sdk: {
270+
version: getVersion(),
271+
platform: getPlatformType(),
272+
},
273+
},
274+
};
275+
resolveConnectionRequest(connectionRequest);
266276
});
267277
this.transport.connect().catch((err) => {
268278
if (err instanceof ProtocolError) {
@@ -283,9 +293,6 @@ export class MultichainSDK extends MultichainCore {
283293
reject(error);
284294
}
285295
},
286-
(sessionRequest: SessionRequest, modal: AbstractInstallModal) => {
287-
modal.updateSessionRequest(sessionRequest);
288-
},
289296
);
290297
} catch (error) {
291298
reject(error);

0 commit comments

Comments
 (0)