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

Tx: Cache sender #2985

Merged
merged 3 commits into from
Aug 24, 2023
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: 3 additions & 1 deletion packages/tx/src/baseTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import type { Hardfork } from '@ethereumjs/common'
import type { BigIntLike } from '@ethereumjs/util'

interface TransactionCache {
hash: Uint8Array | undefined
hash?: Uint8Array
dataFee?: {
value: bigint
hardfork: string | Hardfork
}
senderPubKey?: Uint8Array
}

/**
Expand Down Expand Up @@ -62,6 +63,7 @@ export abstract class BaseTransaction<T extends TransactionType>
protected cache: TransactionCache = {
hash: undefined,
dataFee: undefined,
senderPubKey: undefined,
}

protected readonly txOptions: TxOptions
Expand Down
10 changes: 9 additions & 1 deletion packages/tx/src/eip1559Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<TransactionType
* Returns the public key of the sender
*/
public getSenderPublicKey(): Uint8Array {
if (this.cache.senderPubKey !== undefined) {
return this.cache.senderPubKey
}

if (!this.isSigned()) {
const msg = this._errorMsg('Cannot call this method if transaction is not signed')
throw new Error(msg)
Expand All @@ -347,12 +351,16 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<TransactionType
this._validateHighS()

try {
return ecrecover(
const sender = ecrecover(
msgHash,
v! + BigInt(27), // Recover the 27 which was stripped from ecsign
bigIntToUnpaddedBytes(r!),
bigIntToUnpaddedBytes(s!)
)
if (Object.isFrozen(this)) {
this.cache.senderPubKey = sender
}
return sender
} catch (e: any) {
const msg = this._errorMsg('Invalid Signature')
throw new Error(msg)
Expand Down
10 changes: 9 additions & 1 deletion packages/tx/src/eip2930Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ export class AccessListEIP2930Transaction extends BaseTransaction<TransactionTyp
* Returns the public key of the sender
*/
public getSenderPublicKey(): Uint8Array {
if (this.cache.senderPubKey !== undefined) {
return this.cache.senderPubKey
}

if (!this.isSigned()) {
const msg = this._errorMsg('Cannot call this method if transaction is not signed')
throw new Error(msg)
Expand All @@ -318,12 +322,16 @@ export class AccessListEIP2930Transaction extends BaseTransaction<TransactionTyp
this._validateHighS()

try {
return ecrecover(
const sender = ecrecover(
msgHash,
v! + BigInt(27), // Recover the 27 which was stripped from ecsign
bigIntToUnpaddedBytes(r!),
bigIntToUnpaddedBytes(s!)
)
if (Object.isFrozen(this)) {
this.cache.senderPubKey = sender
}
return sender
} catch (e: any) {
const msg = this._errorMsg('Invalid Signature')
throw new Error(msg)
Expand Down
10 changes: 9 additions & 1 deletion packages/tx/src/eip4844Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
* Returns the public key of the sender
*/
public getSenderPublicKey(): Uint8Array {
if (this.cache.senderPubKey !== undefined) {
return this.cache.senderPubKey
}

if (!this.isSigned()) {
const msg = this._errorMsg('Cannot call this method if transaction is not signed')
throw new Error(msg)
Expand All @@ -545,12 +549,16 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
this._validateHighS()

try {
return ecrecover(
const sender = ecrecover(
msgHash,
v! + BigInt(27), // Recover the 27 which was stripped from ecsign
bigIntToUnpaddedBytes(r!),
bigIntToUnpaddedBytes(s!)
)
if (Object.isFrozen(this)) {
this.cache.senderPubKey = sender
}
return sender
} catch (e: any) {
const msg = this._errorMsg('Invalid Signature')
throw new Error(msg)
Expand Down
10 changes: 9 additions & 1 deletion packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,28 @@ export class LegacyTransaction extends BaseTransaction<TransactionType.Legacy> {
* Returns the public key of the sender
*/
getSenderPublicKey(): Uint8Array {
if (this.cache.senderPubKey !== undefined) {
return this.cache.senderPubKey
}

const msgHash = this.getMessageToVerifySignature()

const { v, r, s } = this

this._validateHighS()

try {
return ecrecover(
const sender = ecrecover(
msgHash,
v!,
bigIntToUnpaddedBytes(r!),
bigIntToUnpaddedBytes(s!),
this.supports(Capability.EIP155ReplayProtection) ? this.common.chainId() : undefined
)
if (Object.isFrozen(this)) {
this.cache.senderPubKey = sender
}
return sender
} catch (e: any) {
const msg = this._errorMsg('Invalid Signature')
throw new Error(msg)
Expand Down