Skip to content

Commit

Permalink
Tx: Cache sender (#2985)
Browse files Browse the repository at this point in the history
* tx: cache sender

* tx: add legacy tx to sender cache
  • Loading branch information
jochem-brouwer authored Aug 24, 2023
1 parent 5f96687 commit 66e79cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
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

0 comments on commit 66e79cc

Please sign in to comment.