From 5092082c7c9e06ce145bdd4c4db146ab1fd25df7 Mon Sep 17 00:00:00 2001 From: Hassan Malik <41640681+hmalik88@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:19:24 -0500 Subject: [PATCH] Update message types (#282) * update message types * lint fix * fix test * linto fixo --- src/wallet.test.ts | 15 ++++++++------- src/wallet.ts | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/wallet.test.ts b/src/wallet.test.ts index 86b33d6d..96cc5e2f 100644 --- a/src/wallet.test.ts +++ b/src/wallet.test.ts @@ -2,7 +2,7 @@ import { providerFromEngine } from '@metamask/eth-json-rpc-provider'; import { JsonRpcEngine } from '@metamask/json-rpc-engine'; import pify from 'pify'; -import type { TransactionParams, MessageParams } from '.'; +import type { TransactionParams, MessageParams, TypedMessageV1Params } from '.'; import { createWalletMiddleware } from '.'; const testAddresses = [ @@ -237,8 +237,8 @@ describe('wallet', () => { it('should sign with a valid address', async () => { const { engine } = createTestSetup(); const getAccounts = async () => testAddresses.slice(); - const witnessedMsgParams: MessageParams[] = []; - const processTypedMessage = async (msgParams: MessageParams) => { + const witnessedMsgParams: TypedMessageV1Params[] = []; + const processTypedMessage = async (msgParams: TypedMessageV1Params) => { witnessedMsgParams.push(msgParams); return testMsgSig; }; @@ -266,14 +266,15 @@ describe('wallet', () => { from: testAddresses[0], data: message, signatureMethod: 'eth_signTypedData', + version: 'V1', }); }); it('should throw with invalid address', async () => { const { engine } = createTestSetup(); const getAccounts = async () => testAddresses.slice(); - const witnessedMsgParams: MessageParams[] = []; - const processTypedMessage = async (msgParams: MessageParams) => { + const witnessedMsgParams: TypedMessageV1Params[] = []; + const processTypedMessage = async (msgParams: TypedMessageV1Params) => { witnessedMsgParams.push(msgParams); return testMsgSig; }; @@ -299,8 +300,8 @@ describe('wallet', () => { it('should throw with unknown address', async () => { const { engine } = createTestSetup(); const getAccounts = async () => testAddresses.slice(); - const witnessedMsgParams: MessageParams[] = []; - const processTypedMessage = async (msgParams: MessageParams) => { + const witnessedMsgParams: TypedMessageV1Params[] = []; + const processTypedMessage = async (msgParams: TypedMessageV1Params) => { witnessedMsgParams.push(msgParams); return testMsgSig; }; diff --git a/src/wallet.ts b/src/wallet.ts index c314ca97..190fba82 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -39,6 +39,10 @@ export type TypedMessageParams = MessageParams & { version: string; }; +export type TypedMessageV1Params = Omit & { + data: Record[]; +}; + export interface WalletMiddlewareOptions { getAccounts: (req: JsonRpcRequest) => Promise; processDecryptMessage?: ( @@ -66,7 +70,7 @@ export interface WalletMiddlewareOptions { req: JsonRpcRequest, ) => Promise; processTypedMessage?: ( - msgParams: MessageParams, + msgParams: TypedMessageV1Params, req: JsonRpcRequest, version: string, ) => Promise; @@ -234,16 +238,21 @@ WalletMiddlewareOptions): JsonRpcMiddleware { throw rpcErrors.invalidInput(); } - const params = req.params as [string, string, Record?]; + const params = req.params as [ + Record[], + string, + Record?, + ]; const message = params[0]; const address = await validateAndNormalizeKeyholder(params[1], req); const version = 'V1'; const extraParams = params[2] || {}; - const msgParams: MessageParams = { + const msgParams: TypedMessageV1Params = { ...extraParams, from: address, data: message, signatureMethod: 'eth_signTypedData', + version, }; res.result = await processTypedMessage(msgParams, req, version);