Skip to content

fix: remove leading zeroes in authorization list #5830

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

Merged
merged 6 commits into from
May 27, 2025
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
4 changes: 4 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `transactionBatches` array to state.
- Add `TransactionBatchMeta` type.

### Fixed

- Support leading zeroes in `authorizationList` properties ([#5830](https://github.com/MetaMask/core/pull/5830))

## [56.2.0]

### Added
Expand Down
12 changes: 1 addition & 11 deletions packages/transaction-controller/src/utils/eip7702.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe('EIP-7702 Utils', () => {
nonce: AUTHORIZATION_LIST_MOCK[0].nonce,
r: '0x82d5b4845dfc808802480749c30b0e02d6d7817061ba141d2d1dcd520f9b65c5',
s: '0x9d0b985134dc2958a9981ce3b5d1061176313536e6da35852cfae41404f53ef3',
yParity: '0x',
yParity: '0x0',
},
]);
});
Expand Down Expand Up @@ -217,16 +217,6 @@ describe('EIP-7702 Utils', () => {
expect(result?.[1]?.nonce).toBe('0x125');
expect(result?.[2]?.nonce).toBe('0x126');
});

it('normalizes nonce to 0x if zero', async () => {
const result = await signAuthorizationList({
authorizationList: [{ ...AUTHORIZATION_LIST_MOCK[0], nonce: '0x0' }],
messenger: controllerMessenger,
transactionMeta: TRANSACTION_META_MOCK,
});

expect(result?.[0]?.nonce).toBe('0x');
});
});

describe('doesChainSupportEIP7702', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/transaction-controller/src/utils/eip7702.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,14 @@ async function signAuthorization(
);

const r = signature.slice(0, 66) as Hex;
const s = `0x${signature.slice(66, 130)}` as Hex;
const s = add0x(signature.slice(66, 130));
const v = parseInt(signature.slice(130, 132), 16);
const yParity = v - 27 === 0 ? '0x' : '0x1';
const finalNonce = nonceDecimal === 0 ? '0x' : nonce;
const yParity = toHex(v - 27 === 0 ? 0 : 1);

const result: Required<Authorization> = {
address,
chainId,
nonce: finalNonce,
nonce,
r,
s,
yParity,
Expand Down
96 changes: 95 additions & 1 deletion packages/transaction-controller/src/utils/prepare.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { FeeMarketEIP1559Transaction, LegacyTransaction } from '@ethereumjs/tx';
import {
FeeMarketEIP1559Transaction,
LegacyTransaction,
EOACodeEIP7702Transaction,
} from '@ethereumjs/tx';

import { prepareTransaction, serializeTransaction } from './prepare';
import type { Authorization } from '../types';
import { TransactionEnvelopeType, type TransactionParams } from '../types';

const CHAIN_ID_MOCK = '0x123';
Expand All @@ -27,6 +32,22 @@ const TRANSACTION_PARAMS_FEE_MARKET_MOCK: TransactionParams = {
maxPriorityFeePerGas: '0x1234567',
};

const TRANSACTION_PARAMS_SET_CODE_MOCK: TransactionParams = {
...TRANSACTION_PARAMS_MOCK,
type: TransactionEnvelopeType.setCode,
authorizationList: [
{
address: '0x0034567890123456789012345678901234567890',
chainId: '0x123',
// @ts-expect-error Wrong nonce type in `ethereumjs/tx`.
nonce: ['0x1'],
r: '0x1234567890123456789012345678901234567890123456789012345678901234',
s: '0x1234567890123456789012345678901234567890123456789012345678901235',
yParity: '0x1',
},
],
};

describe('Prepare Utils', () => {
describe('prepareTransaction', () => {
it('returns legacy transaction object', () => {
Expand All @@ -41,6 +62,79 @@ describe('Prepare Utils', () => {
);
expect(result).toBeInstanceOf(FeeMarketEIP1559Transaction);
});

it('returns set code transaction object', () => {
const result = prepareTransaction(
CHAIN_ID_MOCK,
TRANSACTION_PARAMS_SET_CODE_MOCK,
);
expect(result).toBeInstanceOf(EOACodeEIP7702Transaction);
});

describe('removes leading zeroes', () => {
it.each(['r', 's'] as const)('from authorization %s', (propertyName) => {
const transaction = prepareTransaction(CHAIN_ID_MOCK, {
...TRANSACTION_PARAMS_SET_CODE_MOCK,
authorizationList: [
{
...TRANSACTION_PARAMS_SET_CODE_MOCK.authorizationList?.[0],
[propertyName]:
'0x0034567890123456789012345678901234567890123456789012345678901234',
} as Authorization,
],
}) as EOACodeEIP7702Transaction;

expect(transaction.AuthorizationListJSON[0][propertyName]).toBe(
'0x34567890123456789012345678901234567890123456789012345678901234',
);
});

it('from authorization yParity', () => {
const transaction = prepareTransaction(CHAIN_ID_MOCK, {
...TRANSACTION_PARAMS_SET_CODE_MOCK,
authorizationList: [
{
...TRANSACTION_PARAMS_SET_CODE_MOCK.authorizationList?.[0],
yParity: '0x0',
} as Authorization,
],
}) as EOACodeEIP7702Transaction;

expect(transaction.AuthorizationListJSON[0].yParity).toBe('0x');
});

it('including multiple pairs', () => {
const transaction = prepareTransaction(CHAIN_ID_MOCK, {
...TRANSACTION_PARAMS_SET_CODE_MOCK,
authorizationList: [
{
...TRANSACTION_PARAMS_SET_CODE_MOCK.authorizationList?.[0],
r: '0x0000007890123456789012345678901234567890123456789012345678901234',
} as Authorization,
],
}) as EOACodeEIP7702Transaction;

expect(transaction.AuthorizationListJSON[0].r).toBe(
'0x7890123456789012345678901234567890123456789012345678901234',
);
});

it('allows zero nibbles', () => {
const transaction = prepareTransaction(CHAIN_ID_MOCK, {
...TRANSACTION_PARAMS_SET_CODE_MOCK,
authorizationList: [
{
...TRANSACTION_PARAMS_SET_CODE_MOCK.authorizationList?.[0],
r: '0x0200567890123456789012345678901234567890123456789012345678901234',
} as Authorization,
],
}) as EOACodeEIP7702Transaction;

expect(transaction.AuthorizationListJSON[0].r).toBe(
'0x0200567890123456789012345678901234567890123456789012345678901234',
);
});
});
});

describe('serializeTransaction', () => {
Expand Down
55 changes: 53 additions & 2 deletions packages/transaction-controller/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type { TypedTransaction, TypedTxData } from '@ethereumjs/tx';
import { TransactionFactory } from '@ethereumjs/tx';
import { bytesToHex } from '@metamask/utils';
import type { Hex } from '@metamask/utils';
import { cloneDeep } from 'lodash';

import type { TransactionParams } from '../types';
import type { AuthorizationList, TransactionParams } from '../types';

export const HARDFORK = Hardfork.Prague;

Expand All @@ -20,8 +21,10 @@ export function prepareTransaction(
chainId: Hex,
txParams: TransactionParams,
): TypedTransaction {
const normalizedData = normalizeParams(txParams);

// Does not allow `gasPrice` on type 4 transactions.
const data = txParams as TypedTxData;
const data = normalizedData as TypedTxData;

return TransactionFactory.fromTxData(data, {
freeze: false,
Expand Down Expand Up @@ -55,3 +58,51 @@ function getCommonConfiguration(chainId: Hex): Common {
eips: [7702],
});
}

/**
* Normalize the transaction parameters for compatibility with `ethereumjs/tx`.
*
* @param params - The transaction parameters to normalize.
* @returns The normalized transaction parameters.
*/
function normalizeParams(params: TransactionParams): TransactionParams {
const newParams = cloneDeep(params);
normalizeAuthorizationList(newParams.authorizationList);
return newParams;
}

/**
* Normalize the authorization list for `ethereumjs/tx` compatibility.
*
* @param authorizationList - The list of authorizations to normalize.
*/
function normalizeAuthorizationList(authorizationList?: AuthorizationList) {
if (!authorizationList) {
return;
}

for (const authorization of authorizationList) {
authorization.nonce = removeLeadingZeroes(authorization.nonce);
authorization.r = removeLeadingZeroes(authorization.r);
authorization.s = removeLeadingZeroes(authorization.s);
authorization.yParity = removeLeadingZeroes(authorization.yParity);
}
}

/**
* Remove leading zeroes from a hexadecimal string.
*
* @param value - The hexadecimal string to process.
* @returns The processed hexadecimal string.
*/
function removeLeadingZeroes(value: Hex | undefined): Hex | undefined {
if (!value) {
return value;
}

if (value === '0x0') {
return '0x';
}

return (value.replace?.(/^0x(00)+/u, '0x') as Hex) ?? value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ describe('validation', () => {
},
);

it('throws if yParity is not 0x or 0x1', () => {
it('throws if yParity is not 0x0 or 0x1', () => {
expect(() =>
validateTxParams({
authorizationList: [
Expand All @@ -590,7 +590,7 @@ describe('validation', () => {
}),
).toThrow(
rpcErrors.invalidParams(
`Invalid transaction params: yParity must be '0x' or '0x1'. got: 0x2`,
`Invalid transaction params: yParity must be '0x0' or '0x1'. got: 0x2`,
),
);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/transaction-controller/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,9 @@ function validateAuthorization(authorization: Authorization) {

const { yParity } = authorization;

if (yParity && !['0x', '0x1'].includes(yParity)) {
if (yParity && !['0x0', '0x1'].includes(yParity)) {
throw rpcErrors.invalidParams(
`Invalid transaction params: yParity must be '0x' or '0x1'. got: ${yParity}`,
`Invalid transaction params: yParity must be '0x0' or '0x1'. got: ${yParity}`,
);
}
}
Expand Down
Loading