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

Util: Allow v to be 0 or 1 for EIP1559 transactions #1905

Merged
merged 5 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ export default class Transaction extends BaseTransaction<Transaction> {
}
}

if (!this.supports(Capability.EIP155ReplayProtection)) {
// In case of a signed non-EIP155 tx, `ecrecover` does not get a `chainId` provided
// Internally, `ecrecover` will calculate the recovery bit by `v = v - 27`
// This recovery bit is either 0 or 1. Thus, any other value than `v = 27` or `v = 28` will
// throw upon calling `getSenderAddress` here. Thus throw early in the constructor
if (this.v !== undefined) {
if (!this.v.eqn(27) && !this.v.eqn(28)) {
throw new Error(`Non-EIP155 txs need either v = 27 or v = 28, got v = ${this.v}`)
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't tie this to "after the _validateTxV() analysis" but rather do before respectively directly in the method, think this will get more robust (less ocassion for the analysis to go wrong).

Will try this and update the tests.


if (this.common.isActivatedEIP(3860)) {
checkMaxInitCodeSize(this.common, this.data.length)
}
Expand Down Expand Up @@ -382,7 +394,7 @@ export default class Transaction extends BaseTransaction<Transaction> {
// No unsigned tx and EIP-155 activated and chain ID included
if (
v !== undefined &&
!v.eqn(0) &&
!v.eqn(0) && // TODO fixme -> why overwrite/return default common when `v` is set to 0?
jochem-brouwer marked this conversation as resolved.
Show resolved Hide resolved
(!common || common.gteHardfork('spuriousDragon')) &&
!v.eqn(27) &&
!v.eqn(28)
Comment on lines 397 to 398
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these becoming unreachable code?

Expand Down
28 changes: 21 additions & 7 deletions packages/tx/test/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,6 @@ tape('[Transaction]', function (t) {
'hex'
)

const fixtureTxSignedWithEIP155 = Transaction.fromTxData(txData).sign(privateKey)

const common = new Common({
chain: Chain.Mainnet,
hardfork: Hardfork.TangerineWhistle,
Expand All @@ -400,9 +398,7 @@ tape('[Transaction]', function (t) {
common,
}).sign(privateKey)

let signedWithEIP155 = Transaction.fromTxData(<any>fixtureTxSignedWithEIP155.toJSON()).sign(
privateKey
)
let signedWithEIP155 = Transaction.fromTxData(<any>txData).sign(privateKey)

st.true(signedWithEIP155.verifySignature())
st.notEqual(signedWithEIP155.v?.toString('hex'), '1c')
Expand All @@ -416,7 +412,7 @@ tape('[Transaction]', function (t) {
st.notEqual(signedWithEIP155.v?.toString('hex'), '1c')
st.notEqual(signedWithEIP155.v?.toString('hex'), '1b')

let signedWithoutEIP155 = Transaction.fromTxData(<any>fixtureTxSignedWithEIP155.toJSON(), {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these tests we should review if there was any reason why we used this toJSON logic for already-signed txs.

let signedWithoutEIP155 = Transaction.fromTxData(<any>txData, {
common,
}).sign(privateKey)

Expand All @@ -427,7 +423,7 @@ tape('[Transaction]', function (t) {
"v shouldn't be EIP155 encoded"
)

signedWithoutEIP155 = Transaction.fromTxData(<any>fixtureTxSignedWithoutEIP155.toJSON(), {
signedWithoutEIP155 = Transaction.fromTxData(<any>txData, {
common,
}).sign(privateKey)

Expand All @@ -442,6 +438,24 @@ tape('[Transaction]', function (t) {
}
)

t.test(
'constructor: throw on non-EIP155 transactions which have v != 27 and v != 28',
function (st) {
function getTxData(v: number) {
return {
v,
}
}
for (let n = 0; n < 27; n++) {
st.throws(() => Transaction.fromTxData(getTxData(n)))
}
st.throws(() => Transaction.fromTxData(getTxData(29)))
st.doesNotThrow(() => Transaction.fromTxData(getTxData(27)))
st.doesNotThrow(() => Transaction.fromTxData(getTxData(28)))
st.end()
}
)

t.test('sign(), verifySignature(): sign tx with chainId specified in params', function (st) {
const common = new Common({ chain: 42, hardfork: Hardfork.Petersburg })
let tx = Transaction.fromTxData({}, { common })
Expand Down