-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tx: consolidate generic tx capabilities
- Loading branch information
1 parent
35ec012
commit 414afdc
Showing
5 changed files
with
78 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { bigIntToUnpaddedBytes, ecrecover } from '@ethereumjs/util' | ||
import { keccak256 } from 'ethereum-cryptography/keccak.js' | ||
|
||
import { Capability, type TypedTransaction } from '../types.js' | ||
|
||
export function isSigned(this: TypedTransaction): boolean { | ||
const { v, r, s } = this | ||
if (v === undefined || r === undefined || s === undefined) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
export function errorMsg(this: TypedTransaction, msg: string) { | ||
return `${msg} (${this.errorStr()})` | ||
} | ||
|
||
export function hash(this: TypedTransaction): Uint8Array { | ||
if (!this.isSigned()) { | ||
const msg = errorMsg.bind(this)('Cannot call hash method if transaction is not signed') | ||
throw new Error(msg) | ||
} | ||
|
||
if (Object.isFrozen(this)) { | ||
if (!this['cache'].hash) { | ||
this['cache'].hash = keccak256(this.serialize()) | ||
} | ||
return this['cache'].hash | ||
} | ||
|
||
return keccak256(this.serialize()) | ||
} | ||
|
||
export function getSenderPublicKey(this: TypedTransaction): Uint8Array { | ||
if (this['cache'].senderPubKey !== undefined) { | ||
return this['cache'].senderPubKey | ||
} | ||
|
||
const msgHash = this.getMessageToVerifySignature() | ||
|
||
const { v, r, s } = this | ||
|
||
this['_validateHighS']() | ||
|
||
try { | ||
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 = errorMsg.bind(this)('Invalid Signature') | ||
throw new Error(msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters