diff --git a/package-lock.json b/package-lock.json index 5521120..83445de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@multiversx/sdk-dapp-utils", - "version": "0.0.1", + "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@multiversx/sdk-dapp-utils", - "version": "0.0.1", + "version": "0.1.0", "license": "GPL-3.0-or-later", "devDependencies": { "@multiversx/sdk-core": ">= 12.1.0", diff --git a/package.json b/package.json index fd09ac6..ff958a2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/constants/browserConstants.ts b/src/constants/browserConstants.ts new file mode 100644 index 0000000..2da569a --- /dev/null +++ b/src/constants/browserConstants.ts @@ -0,0 +1,6 @@ +type SafeWindowType = { + [K in keyof T]?: T[K]; +}; + +export const safeWindow: SafeWindowType = + typeof window !== "undefined" ? window : ({} as SafeWindowType); diff --git a/src/constants/crossWindowProviderConstants.ts b/src/constants/crossWindowProviderConstants.ts deleted file mode 100644 index 2af8d86..0000000 --- a/src/constants/crossWindowProviderConstants.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ResponseTypeMap } from "../types/crossWindowProviderTypes"; -import { - CrossWindowProviderRequestEnums, - CrossWindowProviderResponseEnums, -} from "../enums/crossWindowProviderEnums"; - -export const responseTypeMap: ResponseTypeMap = { - [CrossWindowProviderRequestEnums.signTransactionsRequest]: - CrossWindowProviderResponseEnums.signTransactionsResponse, - [CrossWindowProviderRequestEnums.guardTransactionsRequest]: - CrossWindowProviderResponseEnums.guardTransactionsResponse, - [CrossWindowProviderRequestEnums.signMessageRequest]: - CrossWindowProviderResponseEnums.signMessageResponse, - [CrossWindowProviderRequestEnums.loginRequest]: - CrossWindowProviderResponseEnums.loginResponse, - [CrossWindowProviderRequestEnums.logoutRequest]: - CrossWindowProviderResponseEnums.disconnectResponse, - [CrossWindowProviderRequestEnums.cancelAction]: - CrossWindowProviderResponseEnums.cancelResponse, - [CrossWindowProviderRequestEnums.finalizeHandshakeRequest]: - CrossWindowProviderResponseEnums.noneResponse, - [CrossWindowProviderRequestEnums.finalizeResetStateRequest]: - CrossWindowProviderResponseEnums.resetStateResponse, -}; - -type SafeWindowType = { - [K in keyof T]?: T[K]; -}; - -export const safeWindow: SafeWindowType = - typeof window !== "undefined" ? window : ({} as SafeWindowType); diff --git a/src/constants/index.ts b/src/constants/index.ts index 9ea9e88..11d0bc5 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1 +1,2 @@ -export * from "./crossWindowProviderConstants"; +export * from "./windowProviderConstants"; +export * from "./browserConstants"; diff --git a/src/constants/windowProviderConstants.ts b/src/constants/windowProviderConstants.ts new file mode 100644 index 0000000..2cbff25 --- /dev/null +++ b/src/constants/windowProviderConstants.ts @@ -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, +}; \ No newline at end of file diff --git a/src/enums/index.ts b/src/enums/index.ts index c546acf..5e694de 100644 --- a/src/enums/index.ts +++ b/src/enums/index.ts @@ -1 +1,2 @@ -export * from "./crossWindowProviderEnums"; +export * from "./windowProviderEnums"; +export * from "./signMessageStatusEnum"; diff --git a/src/enums/signMessageStatusEnum.ts b/src/enums/signMessageStatusEnum.ts new file mode 100644 index 0000000..468d58a --- /dev/null +++ b/src/enums/signMessageStatusEnum.ts @@ -0,0 +1,6 @@ +export enum SignMessageStatusEnum { + pending = "pending", + failed = "failed", + signed = "signed", + cancelled = "cancelled", +} \ No newline at end of file diff --git a/src/enums/crossWindowProviderEnums.ts b/src/enums/windowProviderEnums.ts similarity index 78% rename from src/enums/crossWindowProviderEnums.ts rename to src/enums/windowProviderEnums.ts index 6e8a820..b0146e5 100644 --- a/src/enums/crossWindowProviderEnums.ts +++ b/src/enums/windowProviderEnums.ts @@ -1,4 +1,4 @@ -export enum CrossWindowProviderRequestEnums { +export enum WindowProviderRequestEnums { signTransactionsRequest = "SIGN_TRANSACTIONS_REQUEST", guardTransactionsRequest = "GUARD_TRANSACTIONS_REQUEST", signMessageRequest = "SIGN_MESSAGE_REQUEST", @@ -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", @@ -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", -} +} \ No newline at end of file diff --git a/src/models/dappProviderBase.ts b/src/models/dappProviderBase.ts index 0e30149..9770e4d 100644 --- a/src/models/dappProviderBase.ts +++ b/src/models/dappProviderBase.ts @@ -1,5 +1,5 @@ import type { Transaction, SignableMessage } from "@multiversx/sdk-core"; -import { Nullable } from "../types/nullable"; +import { Nullable } from "../types"; export interface IDAppProviderOptions { callbackUrl?: string; @@ -7,7 +7,13 @@ export interface IDAppProviderOptions { } export interface IDAppProviderBase { - login?(options?: IDAppProviderOptions): Promise; + login?(options?: IDAppProviderOptions): Promise; logout(options?: IDAppProviderOptions): Promise; signTransaction( transaction: Transaction, diff --git a/src/types/crossWindowProviderTypes.ts b/src/types/crossWindowProviderTypes.ts deleted file mode 100644 index af788c8..0000000 --- a/src/types/crossWindowProviderTypes.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { - CrossWindowProviderRequestEnums, - CrossWindowProviderResponseEnums, - SignMessageStatusEnum, -} from "../enums"; -import type { IPlainTransactionObject } from "@multiversx/sdk-core"; - -export type ReplyWithPostMessageObjectType = { - [CrossWindowProviderResponseEnums.handshakeResponse]: boolean; - [CrossWindowProviderResponseEnums.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; - }; - [CrossWindowProviderResponseEnums.disconnectResponse]: boolean; - [CrossWindowProviderResponseEnums.cancelResponse]: { - address: string; - }; - [CrossWindowProviderResponseEnums.signTransactionsResponse]: IPlainTransactionObject[]; - [CrossWindowProviderResponseEnums.guardTransactionsResponse]: IPlainTransactionObject[]; - [CrossWindowProviderResponseEnums.signMessageResponse]: { - signature?: string; - status: SignMessageStatusEnum; - }; - [CrossWindowProviderResponseEnums.noneResponse]: null; - [CrossWindowProviderResponseEnums.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; - }; -}[keyof ReplyWithPostMessageObjectType]; - -export type ResponseTypeMap = { - [CrossWindowProviderRequestEnums.signTransactionsRequest]: CrossWindowProviderResponseEnums.signTransactionsResponse; - [CrossWindowProviderRequestEnums.signMessageRequest]: CrossWindowProviderResponseEnums.signMessageResponse; - [CrossWindowProviderRequestEnums.loginRequest]: CrossWindowProviderResponseEnums.loginResponse; - [CrossWindowProviderRequestEnums.logoutRequest]: CrossWindowProviderResponseEnums.disconnectResponse; - [CrossWindowProviderRequestEnums.guardTransactionsRequest]: CrossWindowProviderResponseEnums.guardTransactionsResponse; - [CrossWindowProviderRequestEnums.cancelAction]: CrossWindowProviderResponseEnums.cancelResponse; - [CrossWindowProviderRequestEnums.finalizeHandshakeRequest]: CrossWindowProviderResponseEnums.noneResponse; - [CrossWindowProviderRequestEnums.finalizeResetStateRequest]: CrossWindowProviderResponseEnums.resetStateResponse; -}; - -export type RequestPayloadType = { - [CrossWindowProviderRequestEnums.loginRequest]: { - token: string | undefined; - }; - [CrossWindowProviderRequestEnums.logoutRequest]: undefined; - [CrossWindowProviderRequestEnums.signTransactionsRequest]: IPlainTransactionObject[]; - [CrossWindowProviderRequestEnums.guardTransactionsRequest]: IPlainTransactionObject[]; - [CrossWindowProviderRequestEnums.signMessageRequest]: { - message: string; - }; - [CrossWindowProviderRequestEnums.cancelAction]: undefined; - [CrossWindowProviderRequestEnums.finalizeHandshakeRequest]: undefined; - [CrossWindowProviderRequestEnums.finalizeResetStateRequest]: undefined; -}; - -export type RequestMessageType = { - [K in keyof RequestPayloadType]: { - type: K; - payload: RequestPayloadType[K]; - }; -}[keyof RequestPayloadType]; - -export type ReplyWithPostMessageEventType = { - [K in keyof ReplyWithPostMessageObjectType]: { - type: CrossWindowProviderResponseEnums; - payload: ReplyWithPostMessageObjectType[K]; - }; -}[keyof ReplyWithPostMessageObjectType]; - -export interface PostMessageParamsType< - T extends CrossWindowProviderRequestEnums -> { - type: T; - payload: RequestPayloadType[keyof RequestPayloadType]; -} - -export interface PostMessageReturnType< - T extends CrossWindowProviderRequestEnums -> { - type: ResponseTypeMap[T] | CrossWindowProviderResponseEnums.cancelResponse; - payload: ReplyWithPostMessagePayloadType; -} diff --git a/src/types/index.ts b/src/types/index.ts index 8e61def..7add308 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,2 +1,2 @@ -export * from "./crossWindowProviderTypes"; +export * from "./windowProviderTypes"; export * from "./nullable"; diff --git a/src/types/windowProviderTypes.ts b/src/types/windowProviderTypes.ts new file mode 100644 index 0000000..100d65d --- /dev/null +++ b/src/types/windowProviderTypes.ts @@ -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; + }; +}[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; +}