@metamask/message-manager
Advanced tools
+11
-1
@@ -10,2 +10,11 @@ # Changelog | ||
| ## [14.1.1] | ||
| ### Changed | ||
| - Bump `@metamask/base-controller` from `^9.0.0` to `^9.0.1` ([#8317](https://github.com/MetaMask/core/pull/8317)) | ||
| - Bump `@metamask/messenger` from `^0.3.0` to `^1.0.0` ([#8317](https://github.com/MetaMask/core/pull/8317)) | ||
| - Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511)) | ||
| - Bump `@metamask/controller-utils` from `^11.16.0` to `^11.19.0` ([#7534](https://github.com/MetaMask/core/pull/7534), [#7583](https://github.com/MetaMask/core/pull/7583), [#7995](https://github.com/MetaMask/core/pull/7995)) | ||
| ## [14.1.0] | ||
@@ -430,3 +439,4 @@ | ||
| [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/message-manager@14.1.0...HEAD | ||
| [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/message-manager@14.1.1...HEAD | ||
| [14.1.1]: https://github.com/MetaMask/core/compare/@metamask/message-manager@14.1.0...@metamask/message-manager@14.1.1 | ||
| [14.1.0]: https://github.com/MetaMask/core/compare/@metamask/message-manager@14.0.0...@metamask/message-manager@14.1.0 | ||
@@ -433,0 +443,0 @@ [14.0.0]: https://github.com/MetaMask/core/compare/@metamask/message-manager@13.0.2...@metamask/message-manager@14.0.0 |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"AbstractMessageManager.cjs","sourceRoot":"","sources":["../src/AbstractMessageManager.ts"],"names":[],"mappings":";;;AAAA,+DAImC;AAQnC,kEAAkE;AAClE,sDAAsD;AACtD,mCAAsC;AAEtC,+BAAoC;AAEpC,MAAM,aAAa,GAAG;IACpB,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,uBAAuB,EAAE;QACvB,kBAAkB,EAAE,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,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC;oBACpB,KAAK,QAAQ;wBACX,OAAO,OAAO,CAAC,IAAI,CAAC,MAAgB,CAAC,CAAC;oBACxC,KAAK,UAAU;wBACb,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,4CAA4C,CACpE,CACF,CAAC;oBACJ,KAAK,SAAS;wBACZ,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,eAAe,IAAI,CAAC,KAAe,EAAE,CAC7D,CACF,CAAC;oBACJ;wBACE,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,gCAAgC,IAAI,CAAC,SAAS,CACnE,aAAa,CACd,EAAE,CACJ,CACF,CAAC;gBACN,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAtaD,wDAsaC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ApprovalType } from '@metamask/controller-utils';\nimport type {\n Messenger,\n EventConstraint,\n ActionConstraint,\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport type { Draft } from 'immer';\nimport { v1 as random } from 'uuid';\n\nconst stateMetadata = {\n unapprovedMessages: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n unapprovedMessagesCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = () => ({\n unapprovedMessages: {},\n unapprovedMessagesCount: 0,\n});\n\n/**\n * Represents the request adding a message.\n */\nexport type MessageRequest = {\n id?: string | number;\n origin?: string;\n securityAlertResponse?: Record<string, Json>;\n};\n\n/**\n * Represents the request adding a message.\n *\n * @deprecated Please use `MessageRequest` instead.\n */\nexport type OriginalRequest = MessageRequest;\n\n/**\n * @type AbstractMessage\n *\n * Represents and contains data about a signing type signature request.\n *\n * @property id - An id to track and identify the message object\n * @property type - The json-prc signing method for which a signature request has been made.\n * A 'Message' which always has a signing type\n * @property rawSig - Raw data of the signature request\n * @property securityProviderResponse - Response from a security provider, whether it is malicious or not\n * @property metadata - Additional data for the message, for example external identifiers\n */\nexport type AbstractMessage = {\n id: string;\n time: number;\n status: string;\n type: string;\n rawSig?: string;\n securityProviderResponse?: Record<string, Json>;\n securityAlertResponse?: Record<string, Json>;\n metadata?: Json;\n error?: string;\n};\n\n/**\n * @type AbstractMessageParams\n *\n * Represents the parameters to pass to the signing method once the signature request is approved.\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n * @property requestId? - Original request id\n * @property deferSetAsSigned? - Whether to defer setting the message as signed immediately after the keyring is told to sign it\n */\nexport type AbstractMessageParams = {\n from: string;\n origin?: string;\n requestId?: string | number;\n deferSetAsSigned?: boolean;\n};\n\n/**\n * @type MessageParamsMetamask\n *\n * Represents the parameters to pass to the signing method once the signature request is approved\n * plus data added by MetaMask.\n *\n * @property metamaskId - Added for tracking and identification within MetaMask\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n */\nexport type AbstractMessageParamsMetamask = AbstractMessageParams & {\n metamaskId?: string;\n};\n\n/**\n * @type MessageManagerState\n *\n * Message Manager state\n *\n * @property unapprovedMessages - A collection of all Messages in the 'unapproved' state\n * @property unapprovedMessagesCount - The count of all Messages in this.unapprovedMessages\n */\nexport type MessageManagerState<Message extends AbstractMessage> = {\n unapprovedMessages: Record<string, Message>;\n unapprovedMessagesCount: number;\n};\n\nexport type UpdateBadgeEvent<Namespace extends string> = {\n type: `${Namespace}:updateBadge`;\n payload: [];\n};\n\n/**\n * A function for verifying a message, whether it is malicious or not\n */\nexport type SecurityProviderRequest = (\n requestData: AbstractMessage,\n messageType: string,\n) => Promise<Json>;\n\n/**\n * AbstractMessageManager constructor options.\n *\n * @property additionalFinishStatuses - Optional list of statuses that are accepted to emit a finished event.\n * @property messenger - Controller messaging system.\n * @property name - The name of the manager.\n * @property securityProviderRequest - A function for verifying a message, whether it is malicious or not.\n * @property state - Initial state to set on this controller.\n */\nexport type AbstractMessageManagerOptions<\n Name extends string,\n Message extends AbstractMessage,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> = {\n additionalFinishStatuses?: string[];\n messenger: MessageManagerMessenger;\n name: Name;\n securityProviderRequest?: SecurityProviderRequest;\n state?: MessageManagerState<Message>;\n};\n\n/**\n * Controller in charge of managing - storing, adding, removing, updating - Messages.\n */\nexport abstract class AbstractMessageManager<\n Name extends string,\n Message extends AbstractMessage,\n Params extends AbstractMessageParams,\n ParamsMetamask extends AbstractMessageParamsMetamask,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> extends BaseController<\n Name,\n MessageManagerState<Message>,\n MessageManagerMessenger\n> {\n protected messages: Message[];\n\n private readonly securityProviderRequest: SecurityProviderRequest | undefined;\n\n private readonly additionalFinishStatuses: string[];\n\n internalEvents = new EventEmitter();\n\n constructor({\n additionalFinishStatuses,\n messenger,\n name,\n securityProviderRequest,\n state = {} as MessageManagerState<Message>,\n }: AbstractMessageManagerOptions<Name, Message, MessageManagerMessenger>) {\n super({\n messenger,\n metadata: stateMetadata,\n name,\n state: {\n ...getDefaultState(),\n ...state,\n },\n });\n this.messages = [];\n this.securityProviderRequest = securityProviderRequest;\n this.additionalFinishStatuses = additionalFinishStatuses ?? [];\n }\n\n /**\n * Adds request props to the message params and returns a new messageParams object.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param req - The original request object.\n * @returns The messageParams with the request props added.\n */\n protected addRequestToMessageParams<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, req?: MessageRequest) {\n const updatedMessageParams = {\n ...messageParams,\n };\n\n if (req) {\n updatedMessageParams.requestId = req.id;\n updatedMessageParams.origin = req.origin;\n }\n\n return updatedMessageParams;\n }\n\n /**\n * Creates a new Message with a random id and an 'unapproved' status.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param type - The approval type of the message.\n * @param req - The original request object.\n * @returns The new unapproved message for a specified type.\n */\n protected createUnapprovedMessage<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, type: ApprovalType, req?: MessageRequest) {\n const messageId = random();\n\n return {\n id: messageId,\n messageParams,\n securityAlertResponse: req?.securityAlertResponse,\n status: 'unapproved',\n time: Date.now(),\n type,\n };\n }\n\n /**\n * Saves the unapproved messages, and their count to state.\n *\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected saveMessageList(emitUpdateBadge = true) {\n this.update((state) => {\n state.unapprovedMessages =\n this.getUnapprovedMessages() as unknown as Record<\n string,\n Draft<Message>\n >;\n state.unapprovedMessagesCount = this.getUnapprovedMessagesCount();\n });\n if (emitUpdateBadge) {\n this.messenger.publish(`${this.name}:updateBadge` as const);\n }\n }\n\n /**\n * Updates the status of a Message in this.messages.\n *\n * @param messageId - The id of the Message to update.\n * @param status - The new status of the Message.\n */\n protected setMessageStatus(messageId: string, status: string) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n const updatedMessage = {\n ...message,\n status,\n };\n this.updateMessage(updatedMessage);\n this.internalEvents.emit(`${messageId}:${status}`, updatedMessage);\n if (\n status === 'rejected' ||\n status === 'signed' ||\n status === 'errored' ||\n this.additionalFinishStatuses.includes(status)\n ) {\n this.internalEvents.emit(\n `${messageId as string}:finished`,\n updatedMessage,\n );\n }\n }\n\n /**\n * Sets a Message in this.messages to the passed Message if the ids are equal.\n * Then saves the unapprovedMessage list to storage.\n *\n * @param message - A Message that will replace an existing Message (with the id) in this.messages.\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected updateMessage(message: Message, emitUpdateBadge = true) {\n const index = this.messages.findIndex((msg) => message.id === msg.id);\n /* istanbul ignore next */\n if (index !== -1) {\n this.messages[index] = message;\n }\n this.saveMessageList(emitUpdateBadge);\n }\n\n /**\n * Verifies a message is malicious or not by checking it against a security provider.\n *\n * @param message - The message to verify.\n * @returns A promise that resolves to a secured message with additional security provider response data.\n */\n private async securityCheck(message: Message): Promise<Message> {\n if (this.securityProviderRequest) {\n const securityProviderResponse = await this.securityProviderRequest(\n message,\n message.type,\n );\n return {\n ...message,\n securityProviderResponse,\n };\n }\n return message;\n }\n\n clearUnapprovedMessages() {\n this.update((state) => {\n state.unapprovedMessages = {};\n state.unapprovedMessagesCount = 0;\n });\n }\n\n /**\n * A getter for the number of 'unapproved' Messages in this.messages.\n *\n * @returns The number of 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessagesCount() {\n return Object.keys(this.getUnapprovedMessages()).length;\n }\n\n /**\n * A getter for the 'unapproved' Messages in state messages.\n *\n * @returns An index of Message ids to Messages, for all 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessages() {\n return this.messages\n .filter((message) => message.status === 'unapproved')\n .reduce((result: Record<string, Message>, message) => {\n result[message.id] = message;\n return result;\n }, {});\n }\n\n /**\n * Adds a passed Message to this.messages, and calls this.saveMessageList() to save\n * the unapproved Messages from that list to this.messages.\n *\n * @param message - The Message to add to this.messages.\n */\n async addMessage(message: Message) {\n const securedMessage = await this.securityCheck(message);\n this.messages.push(securedMessage);\n this.saveMessageList();\n }\n\n /**\n * Returns a specified Message.\n *\n * @param messageId - The id of the Message to get.\n * @returns The Message with the id that matches the passed messageId, or undefined\n * if no Message has that id.\n */\n getMessage(messageId: string) {\n return this.messages.find((message) => message.id === messageId);\n }\n\n /**\n * Returns all the messages.\n *\n * @returns An array of messages.\n */\n getAllMessages() {\n return this.messages;\n }\n\n /**\n * Approves a Message. Sets the message status via a call to this.setMessageStatusApproved,\n * and returns a promise with any the message params modified for proper signing.\n *\n * @param messageParams - The messageParams to be used when signing method is called,\n * plus data added by MetaMask.\n * @returns Promise resolving to the messageParams with the metamaskId property removed.\n */\n approveMessage(messageParams: ParamsMetamask): Promise<Params> {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.setMessageStatusApproved(messageParams.metamaskId);\n return this.prepMessageForSigning(messageParams);\n }\n\n /**\n * Sets a Message status to 'approved' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to approve.\n */\n setMessageStatusApproved(messageId: string) {\n this.setMessageStatus(messageId, 'approved');\n }\n\n /**\n * Sets message status to inProgress in order to allow users to use extension\n * while waiting for a custodian signature.\n *\n * @param messageId - The id of the message to set to inProgress\n */\n setMessageStatusInProgress(messageId: string) {\n this.setMessageStatus(messageId, 'inProgress');\n }\n\n /**\n * Sets a Message status to 'signed' via a call to this.setMessageStatus and updates\n * that Message in this.messages by adding the raw signature data of the signature\n * request to the Message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The raw data of the signature request.\n */\n setMessageStatusSigned(messageId: string, rawSig: string) {\n this.setMessageStatusAndResult(messageId, rawSig, 'signed');\n }\n\n /**\n * Sets the message via a call to this.setResult and updates status of the message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The data to update rawSig in the message.\n * @param status - The new message status.\n */\n setMessageStatusAndResult(messageId: string, rawSig: string, status: string) {\n this.setResult(messageId, rawSig);\n this.setMessageStatus(messageId, status);\n }\n\n /**\n * Sets the message result.\n *\n * @param messageId - The id of the Message to sign.\n * @param result - The data to update result in the message.\n */\n setResult(messageId: string, result: string) {\n const message = this.getMessage(messageId);\n /* istanbul ignore if */\n if (!message) {\n return;\n }\n this.updateMessage(\n {\n ...message,\n rawSig: result,\n },\n false,\n );\n }\n\n /**\n * Sets the messsage metadata\n *\n * @param messageId - The id of the Message to update\n * @param metadata - The data with which to replace the metadata property in the message\n */\n setMetadata(messageId: string, metadata: Json) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n this.updateMessage(\n {\n ...message,\n metadata,\n },\n false,\n );\n }\n\n /**\n * Removes the metamaskId property from passed messageParams and returns a promise which\n * resolves the updated messageParams\n *\n * @param messageParams - The messageParams to modify\n * @returns Promise resolving to the messageParams with the metamaskId property removed\n */\n abstract prepMessageForSigning(\n messageParams: ParamsMetamask,\n ): Promise<Params>;\n\n /**\n * Creates a new Message with an 'unapproved' status using the passed messageParams.\n * this.addMessage is called to add the new Message to this.messages, and to save the\n * unapproved Messages.\n *\n * @param messageParams - Message parameters for the message to add\n * @param req - The original request object possibly containing the origin.\n * @param version? - The version of the JSON RPC protocol the request is using.\n * @returns The id of the newly created message.\n */\n abstract addUnapprovedMessage(\n messageParams: ParamsMetamask,\n request: MessageRequest,\n version?: string,\n ): Promise<string>;\n\n /**\n * Sets a Message status to 'rejected' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to reject.\n */\n rejectMessage(messageId: string) {\n this.setMessageStatus(messageId, 'rejected');\n }\n\n /**\n * Creates a promise which will resolve or reject when the message process is finished.\n *\n * @param messageParamsWithId - The params for the personal_sign call to be made after the message is approved.\n * @param messageName - The name of the message\n * @returns Promise resolving to the raw data of the signature request.\n */\n async waitForFinishStatus(\n messageParamsWithId: AbstractMessageParamsMetamask,\n messageName: string,\n ): Promise<string> {\n const { metamaskId: messageId, ...messageParams } = messageParamsWithId;\n return new Promise((resolve, reject) => {\n this.internalEvents.once(\n `${messageId as string}:finished`,\n (data: AbstractMessage) => {\n switch (data.status) {\n case 'signed':\n return resolve(data.rawSig as string);\n case 'rejected':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: User denied message signature.`,\n ),\n );\n case 'errored':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: ${data.error as string}`,\n ),\n );\n default:\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: Unknown problem: ${JSON.stringify(\n messageParams,\n )}`,\n ),\n );\n }\n },\n );\n });\n }\n}\n\nexport default AbstractMessageManager;\n"]} | ||
| {"version":3,"file":"AbstractMessageManager.cjs","sourceRoot":"","sources":["../src/AbstractMessageManager.ts"],"names":[],"mappings":";;;AAAA,+DAA2D;AAY3D,kEAAkE;AAClE,sDAAsD;AACtD,mCAAsC;AAEtC,+BAAoC;AAEpC,MAAM,aAAa,GAAG;IACpB,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,uBAAuB,EAAE;QACvB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,eAAe,GAAG,GAAG,EAAE,CAAC,CAAC;IAC7B,kBAAkB,EAAE,EAAE;IACtB,uBAAuB,EAAE,CAAC;CAC3B,CAAC,CAAC;AA8HH;;GAEG;AACH,MAAsB,sBAapB,SAAQ,gCAIT;IASC,YAAY,EACV,wBAAwB,EACxB,SAAS,EACT,IAAI,EACJ,uBAAuB,EACvB,KAAK,GAAG,EAAkC,GAC4B;QACtE,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,aAAa;YACvB,IAAI;YACJ,KAAK,EAAE;gBACL,GAAG,eAAe,EAAE;gBACpB,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAjBL,mBAAc,GAAG,IAAI,qBAAY,EAAE,CAAC;QAkBlC,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,uBAAuB,GAAG,uBAAuB,CAAC;QACvD,IAAI,CAAC,wBAAwB,GAAG,wBAAwB,IAAI,EAAE,CAAC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IAAI,CAAC,aAAa,CAChB;YACE,GAAG,OAAO;YACV,QAAQ;SACT,EACD,KAAK,CACN,CAAC;IACJ,CAAC;IA6BD;;;;OAIG;IACH,aAAa,CAAC,SAAiB;QAC7B,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,mBAAmB,CACvB,mBAAkD,EAClD,WAAmB;QAEnB,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,aAAa,EAAE,GAAG,mBAAmB,CAAC;QACxE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,cAAc,CAAC,IAAI,CACtB,GAAG,SAAmB,WAAW,EACjC,CAAC,IAAqB,EAAE,EAAE;gBACxB,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC;oBACpB,KAAK,QAAQ;wBACX,OAAO,OAAO,CAAC,IAAI,CAAC,MAAgB,CAAC,CAAC;oBACxC,KAAK,UAAU;wBACb,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,4CAA4C,CACpE,CACF,CAAC;oBACJ,KAAK,SAAS;wBACZ,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,eAAe,IAAI,CAAC,KAAe,EAAE,CAC7D,CACF,CAAC;oBACJ;wBACE,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,gCAAgC,IAAI,CAAC,SAAS,CACnE,aAAa,CACd,EAAE,CACJ,CACF,CAAC;gBACN,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAnaD,wDAmaC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ApprovalType } from '@metamask/controller-utils';\nimport type {\n Messenger,\n EventConstraint,\n ActionConstraint,\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport type { Draft } from 'immer';\nimport { v1 as random } from 'uuid';\n\nconst stateMetadata = {\n unapprovedMessages: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n unapprovedMessagesCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = () => ({\n unapprovedMessages: {},\n unapprovedMessagesCount: 0,\n});\n\n/**\n * Represents the request adding a message.\n */\nexport type MessageRequest = {\n id?: string | number;\n origin?: string;\n securityAlertResponse?: Record<string, Json>;\n};\n\n/**\n * Represents the request adding a message.\n *\n * @deprecated Please use `MessageRequest` instead.\n */\nexport type OriginalRequest = MessageRequest;\n\n/**\n * @type AbstractMessage\n *\n * Represents and contains data about a signing type signature request.\n *\n * @property id - An id to track and identify the message object\n * @property type - The json-prc signing method for which a signature request has been made.\n * A 'Message' which always has a signing type\n * @property rawSig - Raw data of the signature request\n * @property securityProviderResponse - Response from a security provider, whether it is malicious or not\n * @property metadata - Additional data for the message, for example external identifiers\n */\nexport type AbstractMessage = {\n id: string;\n time: number;\n status: string;\n type: string;\n rawSig?: string;\n securityProviderResponse?: Record<string, Json>;\n securityAlertResponse?: Record<string, Json>;\n metadata?: Json;\n error?: string;\n};\n\n/**\n * @type AbstractMessageParams\n *\n * Represents the parameters to pass to the signing method once the signature request is approved.\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n * @property requestId? - Original request id\n * @property deferSetAsSigned? - Whether to defer setting the message as signed immediately after the keyring is told to sign it\n */\nexport type AbstractMessageParams = {\n from: string;\n origin?: string;\n requestId?: string | number;\n deferSetAsSigned?: boolean;\n};\n\n/**\n * @type MessageParamsMetamask\n *\n * Represents the parameters to pass to the signing method once the signature request is approved\n * plus data added by MetaMask.\n *\n * @property metamaskId - Added for tracking and identification within MetaMask\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n */\nexport type AbstractMessageParamsMetamask = AbstractMessageParams & {\n metamaskId?: string;\n};\n\n/**\n * @type MessageManagerState\n *\n * Message Manager state\n *\n * @property unapprovedMessages - A collection of all Messages in the 'unapproved' state\n * @property unapprovedMessagesCount - The count of all Messages in this.unapprovedMessages\n */\nexport type MessageManagerState<Message extends AbstractMessage> = {\n unapprovedMessages: Record<string, Message>;\n unapprovedMessagesCount: number;\n};\n\nexport type UpdateBadgeEvent<Namespace extends string> = {\n type: `${Namespace}:updateBadge`;\n payload: [];\n};\n\n/**\n * A function for verifying a message, whether it is malicious or not\n */\nexport type SecurityProviderRequest = (\n requestData: AbstractMessage,\n messageType: string,\n) => Promise<Json>;\n\n/**\n * AbstractMessageManager constructor options.\n *\n * @property additionalFinishStatuses - Optional list of statuses that are accepted to emit a finished event.\n * @property messenger - Controller messaging system.\n * @property name - The name of the manager.\n * @property securityProviderRequest - A function for verifying a message, whether it is malicious or not.\n * @property state - Initial state to set on this controller.\n */\nexport type AbstractMessageManagerOptions<\n Name extends string,\n Message extends AbstractMessage,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> = {\n additionalFinishStatuses?: string[];\n messenger: MessageManagerMessenger;\n name: Name;\n securityProviderRequest?: SecurityProviderRequest;\n state?: MessageManagerState<Message>;\n};\n\n/**\n * Controller in charge of managing - storing, adding, removing, updating - Messages.\n */\nexport abstract class AbstractMessageManager<\n Name extends string,\n Message extends AbstractMessage,\n Params extends AbstractMessageParams,\n ParamsMetamask extends AbstractMessageParamsMetamask,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> extends BaseController<\n Name,\n MessageManagerState<Message>,\n MessageManagerMessenger\n> {\n protected messages: Message[];\n\n private readonly securityProviderRequest: SecurityProviderRequest | undefined;\n\n private readonly additionalFinishStatuses: string[];\n\n internalEvents = new EventEmitter();\n\n constructor({\n additionalFinishStatuses,\n messenger,\n name,\n securityProviderRequest,\n state = {} as MessageManagerState<Message>,\n }: AbstractMessageManagerOptions<Name, Message, MessageManagerMessenger>) {\n super({\n messenger,\n metadata: stateMetadata,\n name,\n state: {\n ...getDefaultState(),\n ...state,\n },\n });\n this.messages = [];\n this.securityProviderRequest = securityProviderRequest;\n this.additionalFinishStatuses = additionalFinishStatuses ?? [];\n }\n\n /**\n * Adds request props to the message params and returns a new messageParams object.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param req - The original request object.\n * @returns The messageParams with the request props added.\n */\n protected addRequestToMessageParams<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, req?: MessageRequest) {\n const updatedMessageParams = {\n ...messageParams,\n };\n\n if (req) {\n updatedMessageParams.requestId = req.id;\n updatedMessageParams.origin = req.origin;\n }\n\n return updatedMessageParams;\n }\n\n /**\n * Creates a new Message with a random id and an 'unapproved' status.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param type - The approval type of the message.\n * @param req - The original request object.\n * @returns The new unapproved message for a specified type.\n */\n protected createUnapprovedMessage<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, type: ApprovalType, req?: MessageRequest) {\n const messageId = random();\n\n return {\n id: messageId,\n messageParams,\n securityAlertResponse: req?.securityAlertResponse,\n status: 'unapproved',\n time: Date.now(),\n type,\n };\n }\n\n /**\n * Saves the unapproved messages, and their count to state.\n *\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected saveMessageList(emitUpdateBadge = true) {\n this.update((state) => {\n state.unapprovedMessages =\n this.getUnapprovedMessages() as unknown as Record<\n string,\n Draft<Message>\n >;\n state.unapprovedMessagesCount = this.getUnapprovedMessagesCount();\n });\n if (emitUpdateBadge) {\n this.messenger.publish(`${this.name}:updateBadge` as const);\n }\n }\n\n /**\n * Updates the status of a Message in this.messages.\n *\n * @param messageId - The id of the Message to update.\n * @param status - The new status of the Message.\n */\n protected setMessageStatus(messageId: string, status: string) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n const updatedMessage = {\n ...message,\n status,\n };\n this.updateMessage(updatedMessage);\n this.internalEvents.emit(`${messageId}:${status}`, updatedMessage);\n if (\n status === 'rejected' ||\n status === 'signed' ||\n status === 'errored' ||\n this.additionalFinishStatuses.includes(status)\n ) {\n this.internalEvents.emit(`${messageId}:finished`, updatedMessage);\n }\n }\n\n /**\n * Sets a Message in this.messages to the passed Message if the ids are equal.\n * Then saves the unapprovedMessage list to storage.\n *\n * @param message - A Message that will replace an existing Message (with the id) in this.messages.\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected updateMessage(message: Message, emitUpdateBadge = true) {\n const index = this.messages.findIndex((msg) => message.id === msg.id);\n /* istanbul ignore next */\n if (index !== -1) {\n this.messages[index] = message;\n }\n this.saveMessageList(emitUpdateBadge);\n }\n\n /**\n * Verifies a message is malicious or not by checking it against a security provider.\n *\n * @param message - The message to verify.\n * @returns A promise that resolves to a secured message with additional security provider response data.\n */\n private async securityCheck(message: Message): Promise<Message> {\n if (this.securityProviderRequest) {\n const securityProviderResponse = await this.securityProviderRequest(\n message,\n message.type,\n );\n return {\n ...message,\n securityProviderResponse,\n };\n }\n return message;\n }\n\n clearUnapprovedMessages() {\n this.update((state) => {\n state.unapprovedMessages = {};\n state.unapprovedMessagesCount = 0;\n });\n }\n\n /**\n * A getter for the number of 'unapproved' Messages in this.messages.\n *\n * @returns The number of 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessagesCount() {\n return Object.keys(this.getUnapprovedMessages()).length;\n }\n\n /**\n * A getter for the 'unapproved' Messages in state messages.\n *\n * @returns An index of Message ids to Messages, for all 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessages() {\n return this.messages\n .filter((message) => message.status === 'unapproved')\n .reduce((result: Record<string, Message>, message) => {\n result[message.id] = message;\n return result;\n }, {});\n }\n\n /**\n * Adds a passed Message to this.messages, and calls this.saveMessageList() to save\n * the unapproved Messages from that list to this.messages.\n *\n * @param message - The Message to add to this.messages.\n */\n async addMessage(message: Message) {\n const securedMessage = await this.securityCheck(message);\n this.messages.push(securedMessage);\n this.saveMessageList();\n }\n\n /**\n * Returns a specified Message.\n *\n * @param messageId - The id of the Message to get.\n * @returns The Message with the id that matches the passed messageId, or undefined\n * if no Message has that id.\n */\n getMessage(messageId: string) {\n return this.messages.find((message) => message.id === messageId);\n }\n\n /**\n * Returns all the messages.\n *\n * @returns An array of messages.\n */\n getAllMessages() {\n return this.messages;\n }\n\n /**\n * Approves a Message. Sets the message status via a call to this.setMessageStatusApproved,\n * and returns a promise with any the message params modified for proper signing.\n *\n * @param messageParams - The messageParams to be used when signing method is called,\n * plus data added by MetaMask.\n * @returns Promise resolving to the messageParams with the metamaskId property removed.\n */\n approveMessage(messageParams: ParamsMetamask): Promise<Params> {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.setMessageStatusApproved(messageParams.metamaskId);\n return this.prepMessageForSigning(messageParams);\n }\n\n /**\n * Sets a Message status to 'approved' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to approve.\n */\n setMessageStatusApproved(messageId: string) {\n this.setMessageStatus(messageId, 'approved');\n }\n\n /**\n * Sets message status to inProgress in order to allow users to use extension\n * while waiting for a custodian signature.\n *\n * @param messageId - The id of the message to set to inProgress\n */\n setMessageStatusInProgress(messageId: string) {\n this.setMessageStatus(messageId, 'inProgress');\n }\n\n /**\n * Sets a Message status to 'signed' via a call to this.setMessageStatus and updates\n * that Message in this.messages by adding the raw signature data of the signature\n * request to the Message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The raw data of the signature request.\n */\n setMessageStatusSigned(messageId: string, rawSig: string) {\n this.setMessageStatusAndResult(messageId, rawSig, 'signed');\n }\n\n /**\n * Sets the message via a call to this.setResult and updates status of the message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The data to update rawSig in the message.\n * @param status - The new message status.\n */\n setMessageStatusAndResult(messageId: string, rawSig: string, status: string) {\n this.setResult(messageId, rawSig);\n this.setMessageStatus(messageId, status);\n }\n\n /**\n * Sets the message result.\n *\n * @param messageId - The id of the Message to sign.\n * @param result - The data to update result in the message.\n */\n setResult(messageId: string, result: string) {\n const message = this.getMessage(messageId);\n /* istanbul ignore if */\n if (!message) {\n return;\n }\n this.updateMessage(\n {\n ...message,\n rawSig: result,\n },\n false,\n );\n }\n\n /**\n * Sets the messsage metadata\n *\n * @param messageId - The id of the Message to update\n * @param metadata - The data with which to replace the metadata property in the message\n */\n setMetadata(messageId: string, metadata: Json) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n this.updateMessage(\n {\n ...message,\n metadata,\n },\n false,\n );\n }\n\n /**\n * Removes the metamaskId property from passed messageParams and returns a promise which\n * resolves the updated messageParams\n *\n * @param messageParams - The messageParams to modify\n * @returns Promise resolving to the messageParams with the metamaskId property removed\n */\n abstract prepMessageForSigning(\n messageParams: ParamsMetamask,\n ): Promise<Params>;\n\n /**\n * Creates a new Message with an 'unapproved' status using the passed messageParams.\n * this.addMessage is called to add the new Message to this.messages, and to save the\n * unapproved Messages.\n *\n * @param messageParams - Message parameters for the message to add\n * @param req - The original request object possibly containing the origin.\n * @param version? - The version of the JSON RPC protocol the request is using.\n * @returns The id of the newly created message.\n */\n abstract addUnapprovedMessage(\n messageParams: ParamsMetamask,\n request: MessageRequest,\n version?: string,\n ): Promise<string>;\n\n /**\n * Sets a Message status to 'rejected' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to reject.\n */\n rejectMessage(messageId: string) {\n this.setMessageStatus(messageId, 'rejected');\n }\n\n /**\n * Creates a promise which will resolve or reject when the message process is finished.\n *\n * @param messageParamsWithId - The params for the personal_sign call to be made after the message is approved.\n * @param messageName - The name of the message\n * @returns Promise resolving to the raw data of the signature request.\n */\n async waitForFinishStatus(\n messageParamsWithId: AbstractMessageParamsMetamask,\n messageName: string,\n ): Promise<string> {\n const { metamaskId: messageId, ...messageParams } = messageParamsWithId;\n return new Promise((resolve, reject) => {\n this.internalEvents.once(\n `${messageId as string}:finished`,\n (data: AbstractMessage) => {\n switch (data.status) {\n case 'signed':\n return resolve(data.rawSig as string);\n case 'rejected':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: User denied message signature.`,\n ),\n );\n case 'errored':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: ${data.error as string}`,\n ),\n );\n default:\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: Unknown problem: ${JSON.stringify(\n messageParams,\n )}`,\n ),\n );\n }\n },\n );\n });\n }\n}\n\nexport default AbstractMessageManager;\n"]} |
| /// <reference types="node" /> | ||
| import { BaseController, type ControllerStateChangeEvent, type ControllerGetStateAction } from "@metamask/base-controller"; | ||
| import { BaseController } from "@metamask/base-controller"; | ||
| import type { ControllerStateChangeEvent, ControllerGetStateAction } from "@metamask/base-controller"; | ||
| import type { ApprovalType } from "@metamask/controller-utils"; | ||
@@ -4,0 +5,0 @@ import type { Messenger, EventConstraint, ActionConstraint } from "@metamask/messenger"; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"AbstractMessageManager.d.cts","sourceRoot":"","sources":["../src/AbstractMessageManager.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,cAAc,EACd,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC9B,kCAAkC;AACnC,OAAO,KAAK,EAAE,YAAY,EAAE,mCAAmC;AAC/D,OAAO,KAAK,EACV,SAAS,EACT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C,aAAa,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAO9D;;;;OAIG;IACH,wBAAwB,CAAC,SAAS,EAAE,MAAM;IAI1C;;;;;OAKG;IACH,0BAA0B,CAAC,SAAS,EAAE,MAAM;IAI5C;;;;;;;OAOG;IACH,sBAAsB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAIxD;;;;;;OAMG;IACH,yBAAyB,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAK3E;;;;;OAKG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;IAe3C;;;;;OAKG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI;IAgB7C;;;;;;OAMG;IACH,QAAQ,CAAC,qBAAqB,CAC5B,aAAa,EAAE,cAAc,GAC5B,OAAO,CAAC,MAAM,CAAC;IAElB;;;;;;;;;OASG;IACH,QAAQ,CAAC,oBAAoB,CAC3B,aAAa,EAAE,cAAc,EAC7B,OAAO,EAAE,cAAc,EACvB,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,MAAM,CAAC;IAElB;;;;OAIG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM;IAI/B;;;;;;OAMG;IACG,mBAAmB,CACvB,mBAAmB,EAAE,6BAA6B,EAClD,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,MAAM,CAAC;CAkCnB;AAED,eAAe,sBAAsB,CAAC"} |
| /// <reference types="node" /> | ||
| import { BaseController, type ControllerStateChangeEvent, type ControllerGetStateAction } from "@metamask/base-controller"; | ||
| import { BaseController } from "@metamask/base-controller"; | ||
| import type { ControllerStateChangeEvent, ControllerGetStateAction } from "@metamask/base-controller"; | ||
| import type { ApprovalType } from "@metamask/controller-utils"; | ||
@@ -4,0 +5,0 @@ import type { Messenger, EventConstraint, ActionConstraint } from "@metamask/messenger"; |
@@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AC;IA6BD;;;;OAIG;IACH,aAAa,CAAC,SAAiB;QAC7B,IAAI,CAAC,gBAAgB,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,mBAAmB,CACvB,mBAAkD,EAClD,WAAmB;QAEnB,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,GAAG,aAAa,EAAE,GAAG,mBAAmB,CAAC;QACxE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,CAAC,cAAc,CAAC,IAAI,CACtB,GAAG,SAAmB,WAAW,EACjC,CAAC,IAAqB,EAAE,EAAE;gBACxB,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC;oBACpB,KAAK,QAAQ;wBACX,OAAO,OAAO,CAAC,IAAI,CAAC,MAAgB,CAAC,CAAC;oBACxC,KAAK,UAAU;wBACb,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,4CAA4C,CACpE,CACF,CAAC;oBACJ,KAAK,SAAS;wBACZ,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,eAAe,IAAI,CAAC,KAAe,EAAE,CAC7D,CACF,CAAC;oBACJ;wBACE,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,gCAAgC,IAAI,CAAC,SAAS,CACnE,aAAa,CACd,EAAE,CACJ,CACF,CAAC;gBACN,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,eAAe,sBAAsB,CAAC","sourcesContent":["import {\n BaseController,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ApprovalType } from '@metamask/controller-utils';\nimport type {\n Messenger,\n EventConstraint,\n ActionConstraint,\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport type { Draft } from 'immer';\nimport { v1 as random } from 'uuid';\n\nconst stateMetadata = {\n unapprovedMessages: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n unapprovedMessagesCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = () => ({\n unapprovedMessages: {},\n unapprovedMessagesCount: 0,\n});\n\n/**\n * Represents the request adding a message.\n */\nexport type MessageRequest = {\n id?: string | number;\n origin?: string;\n securityAlertResponse?: Record<string, Json>;\n};\n\n/**\n * Represents the request adding a message.\n *\n * @deprecated Please use `MessageRequest` instead.\n */\nexport type OriginalRequest = MessageRequest;\n\n/**\n * @type AbstractMessage\n *\n * Represents and contains data about a signing type signature request.\n *\n * @property id - An id to track and identify the message object\n * @property type - The json-prc signing method for which a signature request has been made.\n * A 'Message' which always has a signing type\n * @property rawSig - Raw data of the signature request\n * @property securityProviderResponse - Response from a security provider, whether it is malicious or not\n * @property metadata - Additional data for the message, for example external identifiers\n */\nexport type AbstractMessage = {\n id: string;\n time: number;\n status: string;\n type: string;\n rawSig?: string;\n securityProviderResponse?: Record<string, Json>;\n securityAlertResponse?: Record<string, Json>;\n metadata?: Json;\n error?: string;\n};\n\n/**\n * @type AbstractMessageParams\n *\n * Represents the parameters to pass to the signing method once the signature request is approved.\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n * @property requestId? - Original request id\n * @property deferSetAsSigned? - Whether to defer setting the message as signed immediately after the keyring is told to sign it\n */\nexport type AbstractMessageParams = {\n from: string;\n origin?: string;\n requestId?: string | number;\n deferSetAsSigned?: boolean;\n};\n\n/**\n * @type MessageParamsMetamask\n *\n * Represents the parameters to pass to the signing method once the signature request is approved\n * plus data added by MetaMask.\n *\n * @property metamaskId - Added for tracking and identification within MetaMask\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n */\nexport type AbstractMessageParamsMetamask = AbstractMessageParams & {\n metamaskId?: string;\n};\n\n/**\n * @type MessageManagerState\n *\n * Message Manager state\n *\n * @property unapprovedMessages - A collection of all Messages in the 'unapproved' state\n * @property unapprovedMessagesCount - The count of all Messages in this.unapprovedMessages\n */\nexport type MessageManagerState<Message extends AbstractMessage> = {\n unapprovedMessages: Record<string, Message>;\n unapprovedMessagesCount: number;\n};\n\nexport type UpdateBadgeEvent<Namespace extends string> = {\n type: `${Namespace}:updateBadge`;\n payload: [];\n};\n\n/**\n * A function for verifying a message, whether it is malicious or not\n */\nexport type SecurityProviderRequest = (\n requestData: AbstractMessage,\n messageType: string,\n) => Promise<Json>;\n\n/**\n * AbstractMessageManager constructor options.\n *\n * @property additionalFinishStatuses - Optional list of statuses that are accepted to emit a finished event.\n * @property messenger - Controller messaging system.\n * @property name - The name of the manager.\n * @property securityProviderRequest - A function for verifying a message, whether it is malicious or not.\n * @property state - Initial state to set on this controller.\n */\nexport type AbstractMessageManagerOptions<\n Name extends string,\n Message extends AbstractMessage,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> = {\n additionalFinishStatuses?: string[];\n messenger: MessageManagerMessenger;\n name: Name;\n securityProviderRequest?: SecurityProviderRequest;\n state?: MessageManagerState<Message>;\n};\n\n/**\n * Controller in charge of managing - storing, adding, removing, updating - Messages.\n */\nexport abstract class AbstractMessageManager<\n Name extends string,\n Message extends AbstractMessage,\n Params extends AbstractMessageParams,\n ParamsMetamask extends AbstractMessageParamsMetamask,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> extends BaseController<\n Name,\n MessageManagerState<Message>,\n MessageManagerMessenger\n> {\n protected messages: Message[];\n\n private readonly securityProviderRequest: SecurityProviderRequest | undefined;\n\n private readonly additionalFinishStatuses: string[];\n\n internalEvents = new EventEmitter();\n\n constructor({\n additionalFinishStatuses,\n messenger,\n name,\n securityProviderRequest,\n state = {} as MessageManagerState<Message>,\n }: AbstractMessageManagerOptions<Name, Message, MessageManagerMessenger>) {\n super({\n messenger,\n metadata: stateMetadata,\n name,\n state: {\n ...getDefaultState(),\n ...state,\n },\n });\n this.messages = [];\n this.securityProviderRequest = securityProviderRequest;\n this.additionalFinishStatuses = additionalFinishStatuses ?? [];\n }\n\n /**\n * Adds request props to the message params and returns a new messageParams object.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param req - The original request object.\n * @returns The messageParams with the request props added.\n */\n protected addRequestToMessageParams<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, req?: MessageRequest) {\n const updatedMessageParams = {\n ...messageParams,\n };\n\n if (req) {\n updatedMessageParams.requestId = req.id;\n updatedMessageParams.origin = req.origin;\n }\n\n return updatedMessageParams;\n }\n\n /**\n * Creates a new Message with a random id and an 'unapproved' status.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param type - The approval type of the message.\n * @param req - The original request object.\n * @returns The new unapproved message for a specified type.\n */\n protected createUnapprovedMessage<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, type: ApprovalType, req?: MessageRequest) {\n const messageId = random();\n\n return {\n id: messageId,\n messageParams,\n securityAlertResponse: req?.securityAlertResponse,\n status: 'unapproved',\n time: Date.now(),\n type,\n };\n }\n\n /**\n * Saves the unapproved messages, and their count to state.\n *\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected saveMessageList(emitUpdateBadge = true) {\n this.update((state) => {\n state.unapprovedMessages =\n this.getUnapprovedMessages() as unknown as Record<\n string,\n Draft<Message>\n >;\n state.unapprovedMessagesCount = this.getUnapprovedMessagesCount();\n });\n if (emitUpdateBadge) {\n this.messenger.publish(`${this.name}:updateBadge` as const);\n }\n }\n\n /**\n * Updates the status of a Message in this.messages.\n *\n * @param messageId - The id of the Message to update.\n * @param status - The new status of the Message.\n */\n protected setMessageStatus(messageId: string, status: string) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n const updatedMessage = {\n ...message,\n status,\n };\n this.updateMessage(updatedMessage);\n this.internalEvents.emit(`${messageId}:${status}`, updatedMessage);\n if (\n status === 'rejected' ||\n status === 'signed' ||\n status === 'errored' ||\n this.additionalFinishStatuses.includes(status)\n ) {\n this.internalEvents.emit(\n `${messageId as string}:finished`,\n updatedMessage,\n );\n }\n }\n\n /**\n * Sets a Message in this.messages to the passed Message if the ids are equal.\n * Then saves the unapprovedMessage list to storage.\n *\n * @param message - A Message that will replace an existing Message (with the id) in this.messages.\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected updateMessage(message: Message, emitUpdateBadge = true) {\n const index = this.messages.findIndex((msg) => message.id === msg.id);\n /* istanbul ignore next */\n if (index !== -1) {\n this.messages[index] = message;\n }\n this.saveMessageList(emitUpdateBadge);\n }\n\n /**\n * Verifies a message is malicious or not by checking it against a security provider.\n *\n * @param message - The message to verify.\n * @returns A promise that resolves to a secured message with additional security provider response data.\n */\n private async securityCheck(message: Message): Promise<Message> {\n if (this.securityProviderRequest) {\n const securityProviderResponse = await this.securityProviderRequest(\n message,\n message.type,\n );\n return {\n ...message,\n securityProviderResponse,\n };\n }\n return message;\n }\n\n clearUnapprovedMessages() {\n this.update((state) => {\n state.unapprovedMessages = {};\n state.unapprovedMessagesCount = 0;\n });\n }\n\n /**\n * A getter for the number of 'unapproved' Messages in this.messages.\n *\n * @returns The number of 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessagesCount() {\n return Object.keys(this.getUnapprovedMessages()).length;\n }\n\n /**\n * A getter for the 'unapproved' Messages in state messages.\n *\n * @returns An index of Message ids to Messages, for all 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessages() {\n return this.messages\n .filter((message) => message.status === 'unapproved')\n .reduce((result: Record<string, Message>, message) => {\n result[message.id] = message;\n return result;\n }, {});\n }\n\n /**\n * Adds a passed Message to this.messages, and calls this.saveMessageList() to save\n * the unapproved Messages from that list to this.messages.\n *\n * @param message - The Message to add to this.messages.\n */\n async addMessage(message: Message) {\n const securedMessage = await this.securityCheck(message);\n this.messages.push(securedMessage);\n this.saveMessageList();\n }\n\n /**\n * Returns a specified Message.\n *\n * @param messageId - The id of the Message to get.\n * @returns The Message with the id that matches the passed messageId, or undefined\n * if no Message has that id.\n */\n getMessage(messageId: string) {\n return this.messages.find((message) => message.id === messageId);\n }\n\n /**\n * Returns all the messages.\n *\n * @returns An array of messages.\n */\n getAllMessages() {\n return this.messages;\n }\n\n /**\n * Approves a Message. Sets the message status via a call to this.setMessageStatusApproved,\n * and returns a promise with any the message params modified for proper signing.\n *\n * @param messageParams - The messageParams to be used when signing method is called,\n * plus data added by MetaMask.\n * @returns Promise resolving to the messageParams with the metamaskId property removed.\n */\n approveMessage(messageParams: ParamsMetamask): Promise<Params> {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.setMessageStatusApproved(messageParams.metamaskId);\n return this.prepMessageForSigning(messageParams);\n }\n\n /**\n * Sets a Message status to 'approved' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to approve.\n */\n setMessageStatusApproved(messageId: string) {\n this.setMessageStatus(messageId, 'approved');\n }\n\n /**\n * Sets message status to inProgress in order to allow users to use extension\n * while waiting for a custodian signature.\n *\n * @param messageId - The id of the message to set to inProgress\n */\n setMessageStatusInProgress(messageId: string) {\n this.setMessageStatus(messageId, 'inProgress');\n }\n\n /**\n * Sets a Message status to 'signed' via a call to this.setMessageStatus and updates\n * that Message in this.messages by adding the raw signature data of the signature\n * request to the Message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The raw data of the signature request.\n */\n setMessageStatusSigned(messageId: string, rawSig: string) {\n this.setMessageStatusAndResult(messageId, rawSig, 'signed');\n }\n\n /**\n * Sets the message via a call to this.setResult and updates status of the message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The data to update rawSig in the message.\n * @param status - The new message status.\n */\n setMessageStatusAndResult(messageId: string, rawSig: string, status: string) {\n this.setResult(messageId, rawSig);\n this.setMessageStatus(messageId, status);\n }\n\n /**\n * Sets the message result.\n *\n * @param messageId - The id of the Message to sign.\n * @param result - The data to update result in the message.\n */\n setResult(messageId: string, result: string) {\n const message = this.getMessage(messageId);\n /* istanbul ignore if */\n if (!message) {\n return;\n }\n this.updateMessage(\n {\n ...message,\n rawSig: result,\n },\n false,\n );\n }\n\n /**\n * Sets the messsage metadata\n *\n * @param messageId - The id of the Message to update\n * @param metadata - The data with which to replace the metadata property in the message\n */\n setMetadata(messageId: string, metadata: Json) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n this.updateMessage(\n {\n ...message,\n metadata,\n },\n false,\n );\n }\n\n /**\n * Removes the metamaskId property from passed messageParams and returns a promise which\n * resolves the updated messageParams\n *\n * @param messageParams - The messageParams to modify\n * @returns Promise resolving to the messageParams with the metamaskId property removed\n */\n abstract prepMessageForSigning(\n messageParams: ParamsMetamask,\n ): Promise<Params>;\n\n /**\n * Creates a new Message with an 'unapproved' status using the passed messageParams.\n * this.addMessage is called to add the new Message to this.messages, and to save the\n * unapproved Messages.\n *\n * @param messageParams - Message parameters for the message to add\n * @param req - The original request object possibly containing the origin.\n * @param version? - The version of the JSON RPC protocol the request is using.\n * @returns The id of the newly created message.\n */\n abstract addUnapprovedMessage(\n messageParams: ParamsMetamask,\n request: MessageRequest,\n version?: string,\n ): Promise<string>;\n\n /**\n * Sets a Message status to 'rejected' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to reject.\n */\n rejectMessage(messageId: string) {\n this.setMessageStatus(messageId, 'rejected');\n }\n\n /**\n * Creates a promise which will resolve or reject when the message process is finished.\n *\n * @param messageParamsWithId - The params for the personal_sign call to be made after the message is approved.\n * @param messageName - The name of the message\n * @returns Promise resolving to the raw data of the signature request.\n */\n async waitForFinishStatus(\n messageParamsWithId: AbstractMessageParamsMetamask,\n messageName: string,\n ): Promise<string> {\n const { metamaskId: messageId, ...messageParams } = messageParamsWithId;\n return new Promise((resolve, reject) => {\n this.internalEvents.once(\n `${messageId as string}:finished`,\n (data: AbstractMessage) => {\n switch (data.status) {\n case 'signed':\n return resolve(data.rawSig as string);\n case 'rejected':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: User denied message signature.`,\n ),\n );\n case 'errored':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: ${data.error as string}`,\n ),\n );\n default:\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: Unknown problem: ${JSON.stringify(\n messageParams,\n )}`,\n ),\n );\n }\n },\n );\n });\n }\n}\n\nexport default AbstractMessageManager;\n"]} | ||
| {"version":3,"file":"AbstractMessageManager.mjs","sourceRoot":"","sources":["../src/AbstractMessageManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAY3D,kEAAkE;AAClE,sDAAsD;AACtD,OAAO,EAAE,YAAY,EAAE,eAAe;AAEtC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,aAAa;AAEpC,MAAM,aAAa,GAAG;IACpB,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,uBAAuB,EAAE;QACvB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,eAAe,GAAG,GAAG,EAAE,CAAC,CAAC;IAC7B,kBAAkB,EAAE,EAAE;IACtB,uBAAuB,EAAE,CAAC;CAC3B,C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oBACJ,KAAK,SAAS;wBACZ,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,eAAe,IAAI,CAAC,KAAe,EAAE,CAC7D,CACF,CAAC;oBACJ;wBACE,OAAO,MAAM,CACX,IAAI,KAAK,CACP,YAAY,WAAW,gCAAgC,IAAI,CAAC,SAAS,CACnE,aAAa,CACd,EAAE,CACJ,CACF,CAAC;gBACN,CAAC;YACH,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,eAAe,sBAAsB,CAAC","sourcesContent":["import { BaseController } from '@metamask/base-controller';\nimport type {\n ControllerStateChangeEvent,\n ControllerGetStateAction,\n} from '@metamask/base-controller';\nimport type { ApprovalType } from '@metamask/controller-utils';\nimport type {\n Messenger,\n EventConstraint,\n ActionConstraint,\n} from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n// This package purposefully relies on Node's EventEmitter module.\n// eslint-disable-next-line import-x/no-nodejs-modules\nimport { EventEmitter } from 'events';\nimport type { Draft } from 'immer';\nimport { v1 as random } from 'uuid';\n\nconst stateMetadata = {\n unapprovedMessages: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n unapprovedMessagesCount: {\n includeInStateLogs: true,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = () => ({\n unapprovedMessages: {},\n unapprovedMessagesCount: 0,\n});\n\n/**\n * Represents the request adding a message.\n */\nexport type MessageRequest = {\n id?: string | number;\n origin?: string;\n securityAlertResponse?: Record<string, Json>;\n};\n\n/**\n * Represents the request adding a message.\n *\n * @deprecated Please use `MessageRequest` instead.\n */\nexport type OriginalRequest = MessageRequest;\n\n/**\n * @type AbstractMessage\n *\n * Represents and contains data about a signing type signature request.\n *\n * @property id - An id to track and identify the message object\n * @property type - The json-prc signing method for which a signature request has been made.\n * A 'Message' which always has a signing type\n * @property rawSig - Raw data of the signature request\n * @property securityProviderResponse - Response from a security provider, whether it is malicious or not\n * @property metadata - Additional data for the message, for example external identifiers\n */\nexport type AbstractMessage = {\n id: string;\n time: number;\n status: string;\n type: string;\n rawSig?: string;\n securityProviderResponse?: Record<string, Json>;\n securityAlertResponse?: Record<string, Json>;\n metadata?: Json;\n error?: string;\n};\n\n/**\n * @type AbstractMessageParams\n *\n * Represents the parameters to pass to the signing method once the signature request is approved.\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n * @property requestId? - Original request id\n * @property deferSetAsSigned? - Whether to defer setting the message as signed immediately after the keyring is told to sign it\n */\nexport type AbstractMessageParams = {\n from: string;\n origin?: string;\n requestId?: string | number;\n deferSetAsSigned?: boolean;\n};\n\n/**\n * @type MessageParamsMetamask\n *\n * Represents the parameters to pass to the signing method once the signature request is approved\n * plus data added by MetaMask.\n *\n * @property metamaskId - Added for tracking and identification within MetaMask\n * @property from - Address from which the message is processed\n * @property origin? - Added for request origin identification\n */\nexport type AbstractMessageParamsMetamask = AbstractMessageParams & {\n metamaskId?: string;\n};\n\n/**\n * @type MessageManagerState\n *\n * Message Manager state\n *\n * @property unapprovedMessages - A collection of all Messages in the 'unapproved' state\n * @property unapprovedMessagesCount - The count of all Messages in this.unapprovedMessages\n */\nexport type MessageManagerState<Message extends AbstractMessage> = {\n unapprovedMessages: Record<string, Message>;\n unapprovedMessagesCount: number;\n};\n\nexport type UpdateBadgeEvent<Namespace extends string> = {\n type: `${Namespace}:updateBadge`;\n payload: [];\n};\n\n/**\n * A function for verifying a message, whether it is malicious or not\n */\nexport type SecurityProviderRequest = (\n requestData: AbstractMessage,\n messageType: string,\n) => Promise<Json>;\n\n/**\n * AbstractMessageManager constructor options.\n *\n * @property additionalFinishStatuses - Optional list of statuses that are accepted to emit a finished event.\n * @property messenger - Controller messaging system.\n * @property name - The name of the manager.\n * @property securityProviderRequest - A function for verifying a message, whether it is malicious or not.\n * @property state - Initial state to set on this controller.\n */\nexport type AbstractMessageManagerOptions<\n Name extends string,\n Message extends AbstractMessage,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> = {\n additionalFinishStatuses?: string[];\n messenger: MessageManagerMessenger;\n name: Name;\n securityProviderRequest?: SecurityProviderRequest;\n state?: MessageManagerState<Message>;\n};\n\n/**\n * Controller in charge of managing - storing, adding, removing, updating - Messages.\n */\nexport abstract class AbstractMessageManager<\n Name extends string,\n Message extends AbstractMessage,\n Params extends AbstractMessageParams,\n ParamsMetamask extends AbstractMessageParamsMetamask,\n MessageManagerMessenger extends Messenger<\n Name,\n | ControllerGetStateAction<Name, MessageManagerState<Message>>\n | ActionConstraint,\n | ControllerStateChangeEvent<Name, MessageManagerState<Message>>\n | UpdateBadgeEvent<Name>\n | EventConstraint\n >,\n> extends BaseController<\n Name,\n MessageManagerState<Message>,\n MessageManagerMessenger\n> {\n protected messages: Message[];\n\n private readonly securityProviderRequest: SecurityProviderRequest | undefined;\n\n private readonly additionalFinishStatuses: string[];\n\n internalEvents = new EventEmitter();\n\n constructor({\n additionalFinishStatuses,\n messenger,\n name,\n securityProviderRequest,\n state = {} as MessageManagerState<Message>,\n }: AbstractMessageManagerOptions<Name, Message, MessageManagerMessenger>) {\n super({\n messenger,\n metadata: stateMetadata,\n name,\n state: {\n ...getDefaultState(),\n ...state,\n },\n });\n this.messages = [];\n this.securityProviderRequest = securityProviderRequest;\n this.additionalFinishStatuses = additionalFinishStatuses ?? [];\n }\n\n /**\n * Adds request props to the message params and returns a new messageParams object.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param req - The original request object.\n * @returns The messageParams with the request props added.\n */\n protected addRequestToMessageParams<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, req?: MessageRequest) {\n const updatedMessageParams = {\n ...messageParams,\n };\n\n if (req) {\n updatedMessageParams.requestId = req.id;\n updatedMessageParams.origin = req.origin;\n }\n\n return updatedMessageParams;\n }\n\n /**\n * Creates a new Message with a random id and an 'unapproved' status.\n *\n * @param messageParams - The messageParams to add the request props to.\n * @param type - The approval type of the message.\n * @param req - The original request object.\n * @returns The new unapproved message for a specified type.\n */\n protected createUnapprovedMessage<\n MessageParams extends AbstractMessageParams,\n >(messageParams: MessageParams, type: ApprovalType, req?: MessageRequest) {\n const messageId = random();\n\n return {\n id: messageId,\n messageParams,\n securityAlertResponse: req?.securityAlertResponse,\n status: 'unapproved',\n time: Date.now(),\n type,\n };\n }\n\n /**\n * Saves the unapproved messages, and their count to state.\n *\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected saveMessageList(emitUpdateBadge = true) {\n this.update((state) => {\n state.unapprovedMessages =\n this.getUnapprovedMessages() as unknown as Record<\n string,\n Draft<Message>\n >;\n state.unapprovedMessagesCount = this.getUnapprovedMessagesCount();\n });\n if (emitUpdateBadge) {\n this.messenger.publish(`${this.name}:updateBadge` as const);\n }\n }\n\n /**\n * Updates the status of a Message in this.messages.\n *\n * @param messageId - The id of the Message to update.\n * @param status - The new status of the Message.\n */\n protected setMessageStatus(messageId: string, status: string) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n const updatedMessage = {\n ...message,\n status,\n };\n this.updateMessage(updatedMessage);\n this.internalEvents.emit(`${messageId}:${status}`, updatedMessage);\n if (\n status === 'rejected' ||\n status === 'signed' ||\n status === 'errored' ||\n this.additionalFinishStatuses.includes(status)\n ) {\n this.internalEvents.emit(`${messageId}:finished`, updatedMessage);\n }\n }\n\n /**\n * Sets a Message in this.messages to the passed Message if the ids are equal.\n * Then saves the unapprovedMessage list to storage.\n *\n * @param message - A Message that will replace an existing Message (with the id) in this.messages.\n * @param emitUpdateBadge - Whether to emit the updateBadge event.\n */\n protected updateMessage(message: Message, emitUpdateBadge = true) {\n const index = this.messages.findIndex((msg) => message.id === msg.id);\n /* istanbul ignore next */\n if (index !== -1) {\n this.messages[index] = message;\n }\n this.saveMessageList(emitUpdateBadge);\n }\n\n /**\n * Verifies a message is malicious or not by checking it against a security provider.\n *\n * @param message - The message to verify.\n * @returns A promise that resolves to a secured message with additional security provider response data.\n */\n private async securityCheck(message: Message): Promise<Message> {\n if (this.securityProviderRequest) {\n const securityProviderResponse = await this.securityProviderRequest(\n message,\n message.type,\n );\n return {\n ...message,\n securityProviderResponse,\n };\n }\n return message;\n }\n\n clearUnapprovedMessages() {\n this.update((state) => {\n state.unapprovedMessages = {};\n state.unapprovedMessagesCount = 0;\n });\n }\n\n /**\n * A getter for the number of 'unapproved' Messages in this.messages.\n *\n * @returns The number of 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessagesCount() {\n return Object.keys(this.getUnapprovedMessages()).length;\n }\n\n /**\n * A getter for the 'unapproved' Messages in state messages.\n *\n * @returns An index of Message ids to Messages, for all 'unapproved' Messages in this.messages.\n */\n getUnapprovedMessages() {\n return this.messages\n .filter((message) => message.status === 'unapproved')\n .reduce((result: Record<string, Message>, message) => {\n result[message.id] = message;\n return result;\n }, {});\n }\n\n /**\n * Adds a passed Message to this.messages, and calls this.saveMessageList() to save\n * the unapproved Messages from that list to this.messages.\n *\n * @param message - The Message to add to this.messages.\n */\n async addMessage(message: Message) {\n const securedMessage = await this.securityCheck(message);\n this.messages.push(securedMessage);\n this.saveMessageList();\n }\n\n /**\n * Returns a specified Message.\n *\n * @param messageId - The id of the Message to get.\n * @returns The Message with the id that matches the passed messageId, or undefined\n * if no Message has that id.\n */\n getMessage(messageId: string) {\n return this.messages.find((message) => message.id === messageId);\n }\n\n /**\n * Returns all the messages.\n *\n * @returns An array of messages.\n */\n getAllMessages() {\n return this.messages;\n }\n\n /**\n * Approves a Message. Sets the message status via a call to this.setMessageStatusApproved,\n * and returns a promise with any the message params modified for proper signing.\n *\n * @param messageParams - The messageParams to be used when signing method is called,\n * plus data added by MetaMask.\n * @returns Promise resolving to the messageParams with the metamaskId property removed.\n */\n approveMessage(messageParams: ParamsMetamask): Promise<Params> {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n this.setMessageStatusApproved(messageParams.metamaskId);\n return this.prepMessageForSigning(messageParams);\n }\n\n /**\n * Sets a Message status to 'approved' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to approve.\n */\n setMessageStatusApproved(messageId: string) {\n this.setMessageStatus(messageId, 'approved');\n }\n\n /**\n * Sets message status to inProgress in order to allow users to use extension\n * while waiting for a custodian signature.\n *\n * @param messageId - The id of the message to set to inProgress\n */\n setMessageStatusInProgress(messageId: string) {\n this.setMessageStatus(messageId, 'inProgress');\n }\n\n /**\n * Sets a Message status to 'signed' via a call to this.setMessageStatus and updates\n * that Message in this.messages by adding the raw signature data of the signature\n * request to the Message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The raw data of the signature request.\n */\n setMessageStatusSigned(messageId: string, rawSig: string) {\n this.setMessageStatusAndResult(messageId, rawSig, 'signed');\n }\n\n /**\n * Sets the message via a call to this.setResult and updates status of the message.\n *\n * @param messageId - The id of the Message to sign.\n * @param rawSig - The data to update rawSig in the message.\n * @param status - The new message status.\n */\n setMessageStatusAndResult(messageId: string, rawSig: string, status: string) {\n this.setResult(messageId, rawSig);\n this.setMessageStatus(messageId, status);\n }\n\n /**\n * Sets the message result.\n *\n * @param messageId - The id of the Message to sign.\n * @param result - The data to update result in the message.\n */\n setResult(messageId: string, result: string) {\n const message = this.getMessage(messageId);\n /* istanbul ignore if */\n if (!message) {\n return;\n }\n this.updateMessage(\n {\n ...message,\n rawSig: result,\n },\n false,\n );\n }\n\n /**\n * Sets the messsage metadata\n *\n * @param messageId - The id of the Message to update\n * @param metadata - The data with which to replace the metadata property in the message\n */\n setMetadata(messageId: string, metadata: Json) {\n const message = this.getMessage(messageId);\n if (!message) {\n throw new Error(\n `${this.name as string}: Message not found for id: ${messageId}.`,\n );\n }\n this.updateMessage(\n {\n ...message,\n metadata,\n },\n false,\n );\n }\n\n /**\n * Removes the metamaskId property from passed messageParams and returns a promise which\n * resolves the updated messageParams\n *\n * @param messageParams - The messageParams to modify\n * @returns Promise resolving to the messageParams with the metamaskId property removed\n */\n abstract prepMessageForSigning(\n messageParams: ParamsMetamask,\n ): Promise<Params>;\n\n /**\n * Creates a new Message with an 'unapproved' status using the passed messageParams.\n * this.addMessage is called to add the new Message to this.messages, and to save the\n * unapproved Messages.\n *\n * @param messageParams - Message parameters for the message to add\n * @param req - The original request object possibly containing the origin.\n * @param version? - The version of the JSON RPC protocol the request is using.\n * @returns The id of the newly created message.\n */\n abstract addUnapprovedMessage(\n messageParams: ParamsMetamask,\n request: MessageRequest,\n version?: string,\n ): Promise<string>;\n\n /**\n * Sets a Message status to 'rejected' via a call to this.setMessageStatus.\n *\n * @param messageId - The id of the Message to reject.\n */\n rejectMessage(messageId: string) {\n this.setMessageStatus(messageId, 'rejected');\n }\n\n /**\n * Creates a promise which will resolve or reject when the message process is finished.\n *\n * @param messageParamsWithId - The params for the personal_sign call to be made after the message is approved.\n * @param messageName - The name of the message\n * @returns Promise resolving to the raw data of the signature request.\n */\n async waitForFinishStatus(\n messageParamsWithId: AbstractMessageParamsMetamask,\n messageName: string,\n ): Promise<string> {\n const { metamaskId: messageId, ...messageParams } = messageParamsWithId;\n return new Promise((resolve, reject) => {\n this.internalEvents.once(\n `${messageId as string}:finished`,\n (data: AbstractMessage) => {\n switch (data.status) {\n case 'signed':\n return resolve(data.rawSig as string);\n case 'rejected':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: User denied message signature.`,\n ),\n );\n case 'errored':\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: ${data.error as string}`,\n ),\n );\n default:\n return reject(\n new Error(\n `MetaMask ${messageName} Signature: Unknown problem: ${JSON.stringify(\n messageParams,\n )}`,\n ),\n );\n }\n },\n );\n });\n }\n}\n\nexport default AbstractMessageManager;\n"]} |
+9
-10
| { | ||
| "name": "@metamask/message-manager", | ||
| "version": "14.1.0", | ||
| "version": "14.1.1", | ||
| "description": "Stores and manages interactions with signing requests", | ||
@@ -43,3 +43,2 @@ "keywords": [ | ||
| "changelog:validate": "../../scripts/validate-changelog.sh @metamask/message-manager", | ||
| "publish:preview": "yarn npm publish --tag preview", | ||
| "since-latest-release": "../../scripts/since-latest-release.sh", | ||
@@ -52,7 +51,7 @@ "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter", | ||
| "dependencies": { | ||
| "@metamask/base-controller": "^9.0.0", | ||
| "@metamask/controller-utils": "^11.16.0", | ||
| "@metamask/base-controller": "^9.0.1", | ||
| "@metamask/controller-utils": "^11.19.0", | ||
| "@metamask/eth-sig-util": "^8.2.0", | ||
| "@metamask/messenger": "^0.3.0", | ||
| "@metamask/utils": "^11.8.1", | ||
| "@metamask/messenger": "^1.0.0", | ||
| "@metamask/utils": "^11.9.0", | ||
| "@types/uuid": "^8.3.0", | ||
@@ -65,7 +64,7 @@ "jsonschema": "^1.4.1", | ||
| "@ts-bridge/cli": "^0.6.4", | ||
| "@types/jest": "^27.4.1", | ||
| "@types/jest": "^29.5.14", | ||
| "deepmerge": "^4.2.2", | ||
| "jest": "^27.5.1", | ||
| "ts-jest": "^27.1.4", | ||
| "typedoc": "^0.24.8", | ||
| "jest": "^29.7.0", | ||
| "ts-jest": "^29.2.5", | ||
| "typedoc": "^0.25.13", | ||
| "typedoc-plugin-missing-exports": "^2.0.0", | ||
@@ -72,0 +71,0 @@ "typescript": "~5.3.3" |
237190
0.32%- Removed
Updated
Updated