Skip to content

Commit

Permalink
Make types and enums more generic (#1)
Browse files Browse the repository at this point in the history
* make types and enums more generic and fix login response for dapp provider base type

* 0.1.0
  • Loading branch information
CiprianDraghici authored Aug 29, 2024
1 parent e160215 commit 97ce670
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 156 deletions.
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-dapp-utils",
"version": "0.0.1",
"version": "0.1.0",
"description": "SDK for DApp utilities",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
6 changes: 6 additions & 0 deletions src/constants/browserConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type SafeWindowType<T extends Window = Window> = {
[K in keyof T]?: T[K];
};

export const safeWindow: SafeWindowType =
typeof window !== "undefined" ? window : ({} as SafeWindowType);
31 changes: 0 additions & 31 deletions src/constants/crossWindowProviderConstants.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./crossWindowProviderConstants";
export * from "./windowProviderConstants";
export * from "./browserConstants";
24 changes: 24 additions & 0 deletions src/constants/windowProviderConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ResponseTypeMap } from "../types";
import {
WindowProviderRequestEnums,
WindowProviderResponseEnums,
} from "../enums";

export const responseTypeMap: ResponseTypeMap = {
[WindowProviderRequestEnums.signTransactionsRequest]:
WindowProviderResponseEnums.signTransactionsResponse,
[WindowProviderRequestEnums.guardTransactionsRequest]:
WindowProviderResponseEnums.guardTransactionsResponse,
[WindowProviderRequestEnums.signMessageRequest]:
WindowProviderResponseEnums.signMessageResponse,
[WindowProviderRequestEnums.loginRequest]:
WindowProviderResponseEnums.loginResponse,
[WindowProviderRequestEnums.logoutRequest]:
WindowProviderResponseEnums.disconnectResponse,
[WindowProviderRequestEnums.cancelAction]:
WindowProviderResponseEnums.cancelResponse,
[WindowProviderRequestEnums.finalizeHandshakeRequest]:
WindowProviderResponseEnums.noneResponse,
[WindowProviderRequestEnums.finalizeResetStateRequest]:
WindowProviderResponseEnums.resetStateResponse,
};
3 changes: 2 additions & 1 deletion src/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./crossWindowProviderEnums";
export * from "./windowProviderEnums";
export * from "./signMessageStatusEnum";
6 changes: 6 additions & 0 deletions src/enums/signMessageStatusEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum SignMessageStatusEnum {
pending = "pending",
failed = "failed",
signed = "signed",
cancelled = "cancelled",
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum CrossWindowProviderRequestEnums {
export enum WindowProviderRequestEnums {
signTransactionsRequest = "SIGN_TRANSACTIONS_REQUEST",
guardTransactionsRequest = "GUARD_TRANSACTIONS_REQUEST",
signMessageRequest = "SIGN_MESSAGE_REQUEST",
Expand All @@ -9,7 +9,7 @@ export enum CrossWindowProviderRequestEnums {
finalizeResetStateRequest = "FINALIZE_RESET_STATE_REQUEST",
}

export enum CrossWindowProviderResponseEnums {
export enum WindowProviderResponseEnums {
handshakeResponse = "HANDSHAKE_RESPONSE",
guardTransactionsResponse = "GUARD_TRANSACTIONS_RESPONSE",
loginResponse = "LOGIN_RESPONSE",
Expand All @@ -19,11 +19,4 @@ export enum CrossWindowProviderResponseEnums {
signMessageResponse = "SIGN_MESSAGE_RESPONSE",
noneResponse = "NONE_RESPONSE",
resetStateResponse = "RESET_STATE_RESPONSE",
}

export enum SignMessageStatusEnum {
pending = "pending",
failed = "failed",
signed = "signed",
cancelled = "cancelled",
}
}
10 changes: 8 additions & 2 deletions src/models/dappProviderBase.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { Transaction, SignableMessage } from "@multiversx/sdk-core";
import { Nullable } from "../types/nullable";
import { Nullable } from "../types";

export interface IDAppProviderOptions {
callbackUrl?: string;
[key: string]: any;
}

export interface IDAppProviderBase {
login?(options?: IDAppProviderOptions): Promise<string | boolean>;
login?(options?: IDAppProviderOptions): Promise<string | boolean | {
address: string;
signature: string;
multisig?: string;
impersonate?: string;
[key: string]: unknown;
}>;
logout(options?: IDAppProviderOptions): Promise<boolean>;
signTransaction(
transaction: Transaction,
Expand Down
107 changes: 0 additions & 107 deletions src/types/crossWindowProviderTypes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./crossWindowProviderTypes";
export * from "./windowProviderTypes";
export * from "./nullable";
107 changes: 107 additions & 0 deletions src/types/windowProviderTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
WindowProviderRequestEnums,
WindowProviderResponseEnums,
SignMessageStatusEnum,
} from "../enums";
import type { IPlainTransactionObject } from "@multiversx/sdk-core";

export type ReplyWithPostMessageObjectType = {
[WindowProviderResponseEnums.handshakeResponse]: boolean;
[WindowProviderResponseEnums.loginResponse]: {
address: string;
accessToken?: string;
/**
* used in De-Fi wallet extension as wallet name
* */
name?: string;
signature?: string;
/**
* contract address for alternate multisig login
* */
multisig?: string;
/**
* custom address for alternate login
* */
impersonate?: string;
};
[WindowProviderResponseEnums.disconnectResponse]: boolean;
[WindowProviderResponseEnums.cancelResponse]: {
address: string;
};
[WindowProviderResponseEnums.signTransactionsResponse]: IPlainTransactionObject[];
[WindowProviderResponseEnums.guardTransactionsResponse]: IPlainTransactionObject[];
[WindowProviderResponseEnums.signMessageResponse]: {
signature?: string;
status: SignMessageStatusEnum;
};
[WindowProviderResponseEnums.noneResponse]: null;
[WindowProviderResponseEnums.resetStateResponse]: boolean;
};

export type ReplyWithPostMessagePayloadType<
K extends keyof ReplyWithPostMessageObjectType
> = {
data?: ReplyWithPostMessageObjectType[K];
error?: string;
};

export type ReplyWithPostMessageType = {
[K in keyof ReplyWithPostMessageObjectType]: {
type: K;
payload: ReplyWithPostMessagePayloadType<K>;
};
}[keyof ReplyWithPostMessageObjectType];

export type ResponseTypeMap = {
[WindowProviderRequestEnums.signTransactionsRequest]: WindowProviderResponseEnums.signTransactionsResponse;
[WindowProviderRequestEnums.signMessageRequest]: WindowProviderResponseEnums.signMessageResponse;
[WindowProviderRequestEnums.loginRequest]: WindowProviderResponseEnums.loginResponse;
[WindowProviderRequestEnums.logoutRequest]: WindowProviderResponseEnums.disconnectResponse;
[WindowProviderRequestEnums.guardTransactionsRequest]: WindowProviderResponseEnums.guardTransactionsResponse;
[WindowProviderRequestEnums.cancelAction]: WindowProviderResponseEnums.cancelResponse;
[WindowProviderRequestEnums.finalizeHandshakeRequest]: WindowProviderResponseEnums.noneResponse;
[WindowProviderRequestEnums.finalizeResetStateRequest]: WindowProviderResponseEnums.resetStateResponse;
};

export type RequestPayloadType = {
[WindowProviderRequestEnums.loginRequest]: {
token: string | undefined;
};
[WindowProviderRequestEnums.logoutRequest]: undefined;
[WindowProviderRequestEnums.signTransactionsRequest]: IPlainTransactionObject[];
[WindowProviderRequestEnums.guardTransactionsRequest]: IPlainTransactionObject[];
[WindowProviderRequestEnums.signMessageRequest]: {
message: string;
};
[WindowProviderRequestEnums.cancelAction]: undefined;
[WindowProviderRequestEnums.finalizeHandshakeRequest]: undefined;
[WindowProviderRequestEnums.finalizeResetStateRequest]: undefined;
};

export type RequestMessageType = {
[K in keyof RequestPayloadType]: {
type: K;
payload: RequestPayloadType[K];
};
}[keyof RequestPayloadType];

export type ReplyWithPostMessageEventType = {
[K in keyof ReplyWithPostMessageObjectType]: {
type: WindowProviderResponseEnums;
payload: ReplyWithPostMessageObjectType[K];
};
}[keyof ReplyWithPostMessageObjectType];

export interface PostMessageParamsType<
T extends WindowProviderRequestEnums
> {
type: T;
payload: RequestPayloadType[keyof RequestPayloadType];
}

export interface PostMessageReturnType<
T extends WindowProviderRequestEnums
> {
type: ResponseTypeMap[T] | WindowProviderResponseEnums.cancelResponse;
payload: ReplyWithPostMessagePayloadType<ResponseTypeMap[T]>;
}

0 comments on commit 97ce670

Please sign in to comment.