Skip to content

Commit

Permalink
Update message types (#282)
Browse files Browse the repository at this point in the history
* update message types

* lint fix

* fix test

* linto fixo
  • Loading branch information
hmalik88 authored Jan 22, 2024
1 parent 9a73980 commit 5092082
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down
15 changes: 12 additions & 3 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export type TypedMessageParams = MessageParams & {
version: string;
};

export type TypedMessageV1Params = Omit<TypedMessageParams, 'data'> & {
data: Record<string, unknown>[];
};

export interface WalletMiddlewareOptions {
getAccounts: (req: JsonRpcRequest) => Promise<string[]>;
processDecryptMessage?: (
Expand Down Expand Up @@ -66,7 +70,7 @@ export interface WalletMiddlewareOptions {
req: JsonRpcRequest,
) => Promise<string>;
processTypedMessage?: (
msgParams: MessageParams,
msgParams: TypedMessageV1Params,
req: JsonRpcRequest,
version: string,
) => Promise<string>;
Expand Down Expand Up @@ -234,16 +238,21 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
throw rpcErrors.invalidInput();
}

const params = req.params as [string, string, Record<string, string>?];
const params = req.params as [
Record<string, unknown>[],
string,
Record<string, string>?,
];
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);
Expand Down

0 comments on commit 5092082

Please sign in to comment.