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

Use input as data in eth_sendTransaction #300

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
105 changes: 105 additions & 0 deletions src/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,109 @@ describe('wallet', () => {
);
});

it('processes transaction with data field but without input field', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice(0, 2);
const witnessedTxParams: TransactionParams[] = [];
const processTransaction = async (_txParams: TransactionParams) => {
witnessedTxParams.push(_txParams);
return testTxHash;
};
engine.push(createWalletMiddleware({ getAccounts, processTransaction }));
const txParams = {
from: testAddresses[0],
data: '0x0',
};

const payload = { method: 'eth_sendTransaction', params: [txParams] };
const sendTxResponse = await pify(engine.handle).call(engine, payload);
const sendTxResult = sendTxResponse.result;
expect(sendTxResult).toBeDefined();
expect(sendTxResult).toStrictEqual(testTxHash);
expect(witnessedTxParams).toHaveLength(1);
expect(witnessedTxParams[0]).toStrictEqual({
from: testAddresses[0],
data: '0x0',
input: '0x0',
});
});

it('processes transaction with input field but without data field', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice(0, 2);
const witnessedTxParams: TransactionParams[] = [];
const processTransaction = async (_txParams: TransactionParams) => {
witnessedTxParams.push(_txParams);
return testTxHash;
};
engine.push(createWalletMiddleware({ getAccounts, processTransaction }));
const txParams = {
from: testAddresses[0],
input: '0x0',
};

const payload = { method: 'eth_sendTransaction', params: [txParams] };
const sendTxResponse = await pify(engine.handle).call(engine, payload);
const sendTxResult = sendTxResponse.result;
expect(sendTxResult).toBeDefined();
expect(sendTxResult).toStrictEqual(testTxHash);
expect(witnessedTxParams).toHaveLength(1);
expect(witnessedTxParams[0]).toStrictEqual({
from: testAddresses[0],
data: '0x0',
input: '0x0',
});
});

it('processes transaction with matching input and data field', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice(0, 2);
const witnessedTxParams: TransactionParams[] = [];
const processTransaction = async (_txParams: TransactionParams) => {
witnessedTxParams.push(_txParams);
return testTxHash;
};
engine.push(createWalletMiddleware({ getAccounts, processTransaction }));
const txParams = {
from: testAddresses[0],
data: '0x0',
input: '0x0',
};

const payload = { method: 'eth_sendTransaction', params: [txParams] };
const sendTxResponse = await pify(engine.handle).call(engine, payload);
const sendTxResult = sendTxResponse.result;
expect(sendTxResult).toBeDefined();
expect(sendTxResult).toStrictEqual(testTxHash);
expect(witnessedTxParams).toHaveLength(1);
expect(witnessedTxParams[0]).toStrictEqual({
from: testAddresses[0],
data: '0x0',
input: '0x0',
});
});

it('throws when input and data fields are both defined but do not match', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice(0, 2);
const witnessedTxParams: TransactionParams[] = [];
const processTransaction = async (_txParams: TransactionParams) => {
witnessedTxParams.push(_txParams);
return testTxHash;
};
engine.push(createWalletMiddleware({ getAccounts, processTransaction }));
const txParams = {
from: testAddresses[0],
data: '0x0',
input: '0x1',
};

const payload = { method: 'eth_sendTransaction', params: [txParams] };
await expect(pify(engine.handle).call(engine, payload)).rejects.toThrow(
new Error('Invalid input.'),
);
});

it('should not override other request params', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice(0, 2);
Expand All @@ -129,6 +232,8 @@ describe('wallet', () => {
const txParams = {
from: testAddresses[0],
to: testAddresses[1],
data: '0x0',
input: '0x0',
};

const payload = { method: 'eth_sendTransaction', params: [txParams] };
Expand Down
23 changes: 19 additions & 4 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,25 @@
throw rpcErrors.invalidInput();
}

const params = req.params[0] as TransactionParams | undefined;
const txParams: TransactionParams = {
...params,
from: await validateAndNormalizeKeyholder(params?.from || '', req),
const params = req.params[0] as
| (TransactionParams & {
input?: string;
data?: string;
})
| undefined;

const { from, data, input, ...restParams } = params || {};
if (data && input && data !== input) {
throw rpcErrors.invalidInput();
}

const txParamsData = data || input;
const txParams: TransactionParams & { data?: string; input?: string } = {
...restParams,
...(txParamsData
? { data: txParamsData, input: txParamsData }
: undefined),
from: await validateAndNormalizeKeyholder(from || '', req),
};
res.result = await processTransaction(txParams, req);
}
Expand Down Expand Up @@ -458,7 +473,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 476 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 476 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 476 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 476 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 476 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 476 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 476 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 476 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 476 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