Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update message types #282

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
req: JsonRpcRequest,
) => Promise<string>;
processTypedMessage?: (
msgParams: MessageParams,
msgParams: TypedMessageV1Params,
req: JsonRpcRequest,
version: string,
) => Promise<string>;
Expand Down Expand Up @@ -234,16 +238,21 @@
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 Expand Up @@ -449,7 +458,7 @@
*
* @param address - The address to validate and normalize.
* @param req - The request object.
* @returns {string} - The normalized address, if valid. Otherwise, throws

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (16.x)

There must be no hyphen before @returns description

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (16.x)

JSDoc description does not satisfy the regex pattern

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (16.x)

Types are not permitted on @returns

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

There must be no hyphen before @returns description

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

JSDoc description does not satisfy the regex pattern

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (18.x)

Types are not permitted on @returns

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

There must be no hyphen before @returns description

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

JSDoc description does not satisfy the regex pattern

Check warning on line 461 in src/wallet.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (20.x)

Types are not permitted on @returns
* an error
*/
async function validateAndNormalizeKeyholder(
Expand Down
Loading