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

New Byte Type Aliases with Byte Length Indicators #3335

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/block/src/types.ts
Expand Up @@ -6,6 +6,9 @@ import type {
BigIntLike,
BytesLike,
JsonRpcWithdrawal,
PrefixedHex20,
PrefixedHex32,
PrefixedHex8,
PrefixedHexString,
WithdrawalBytes,
WithdrawalData,
Expand Down Expand Up @@ -253,10 +256,10 @@ export interface JsonRpcBlock {

// Note: all these strings are 0x-prefixed
export type WithdrawalV1 = {
index: PrefixedHexString // Quantity, 8 Bytes
validatorIndex: PrefixedHexString // Quantity, 8 bytes
address: PrefixedHexString // DATA, 20 bytes
amount: PrefixedHexString // Quantity, 32 bytes
index: PrefixedHex8 // Quantity
validatorIndex: PrefixedHex8 // Quantity
address: PrefixedHex20 // DATA
amount: PrefixedHex32 // Quantity
}

// Note: all these strings are 0x-prefixed
Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/baseTransaction.ts
Expand Up @@ -25,7 +25,7 @@ import type {
TxOptions,
TxValuesArray,
} from './types.js'
import type { BigIntLike } from '@ethereumjs/util'
import type { BigIntLike, Bytes32 } from '@ethereumjs/util'

/**
* This base class will likely be subject to further
Expand Down Expand Up @@ -247,7 +247,7 @@ export abstract class BaseTransaction<T extends TransactionType>
// Returns the hashed unsigned tx, which is used to sign the transaction.
abstract getHashedMessageToSign(): Uint8Array

abstract hash(): Uint8Array
abstract hash(): Bytes32

abstract getMessageToVerifySignature(): Uint8Array

Expand Down
3 changes: 2 additions & 1 deletion packages/tx/src/capabilities/legacy.ts
Expand Up @@ -5,6 +5,7 @@ import { BaseTransaction } from '../baseTransaction.js'
import { Capability } from '../types.js'

import type { LegacyTxInterface } from '../types.js'
import type { Bytes32 } from '@ethereumjs/util'

export function errorMsg(tx: LegacyTxInterface, msg: string) {
return `${msg} (${tx.errorStr()})`
Expand Down Expand Up @@ -39,7 +40,7 @@ export function getDataFee(tx: LegacyTxInterface, extraCost?: bigint): bigint {
return cost
}

export function hash(tx: LegacyTxInterface): Uint8Array {
export function hash(tx: LegacyTxInterface): Bytes32 {
if (!tx.isSigned()) {
const msg = errorMsg(tx, 'Cannot call hash method if transaction is not signed')
throw new Error(msg)
Expand Down
3 changes: 2 additions & 1 deletion packages/tx/src/eip1559Transaction.ts
Expand Up @@ -29,6 +29,7 @@ import type {
TxOptions,
} from './types.js'
import type { Common } from '@ethereumjs/common'
import type { Bytes32 } from '@ethereumjs/util'

type TxData = AllTypesTxData[TransactionType.FeeMarketEIP1559]
type TxValuesArray = AllTypesTxValuesArray[TransactionType.FeeMarketEIP1559]
Expand Down Expand Up @@ -298,7 +299,7 @@ export class FeeMarketEIP1559Transaction extends BaseTransaction<TransactionType
* This method can only be used for signed txs (it throws otherwise).
* Use {@link FeeMarketEIP1559Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
*/
public hash(): Uint8Array {
public hash(): Bytes32 {
return Legacy.hash(this)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/tx/src/eip2930Transaction.ts
Expand Up @@ -27,6 +27,7 @@ import type {
TxOptions,
} from './types.js'
import type { Common } from '@ethereumjs/common'
import type { Bytes32 } from '@ethereumjs/util'

type TxData = AllTypesTxData[TransactionType.AccessListEIP2930]
type TxValuesArray = AllTypesTxValuesArray[TransactionType.AccessListEIP2930]
Expand Down Expand Up @@ -266,7 +267,7 @@ export class AccessListEIP2930Transaction extends BaseTransaction<TransactionTyp
* This method can only be used for signed txs (it throws otherwise).
* Use {@link AccessListEIP2930Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
*/
public hash(): Uint8Array {
public hash(): Bytes32 {
return Legacy.hash(this)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/eip4844Transaction.ts
Expand Up @@ -36,7 +36,7 @@ import type {
TxOptions,
} from './types.js'
import type { Common } from '@ethereumjs/common'
import type { Kzg } from '@ethereumjs/util'
import type { Bytes32, Kzg } from '@ethereumjs/util'

type TxData = AllTypesTxData[TransactionType.BlobEIP4844]
type TxValuesArray = AllTypesTxValuesArray[TransactionType.BlobEIP4844]
Expand Down Expand Up @@ -540,7 +540,7 @@ export class BlobEIP4844Transaction extends BaseTransaction<TransactionType.Blob
* This method can only be used for signed txs (it throws otherwise).
* Use {@link BlobEIP4844Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
*/
public hash(): Uint8Array {
public hash(): Bytes32 {
return Legacy.hash(this)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/tx/src/legacyTransaction.ts
Expand Up @@ -23,6 +23,7 @@ import type {
TxOptions,
} from './types.js'
import type { Common } from '@ethereumjs/common'
import type { Bytes32 } from '@ethereumjs/util'

type TxData = AllTypesTxData[TransactionType.Legacy]
type TxValuesArray = AllTypesTxValuesArray[TransactionType.Legacy]
Expand Down Expand Up @@ -252,7 +253,7 @@ export class LegacyTransaction extends BaseTransaction<TransactionType.Legacy> {
* This method can only be used for signed txs (it throws otherwise).
* Use {@link Transaction.getMessageToSign} to get a tx hash for the purpose of signing.
*/
hash(): Uint8Array {
hash(): Bytes32 {
return Legacy.hash(this)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/tx/src/types.ts
Expand Up @@ -5,7 +5,7 @@ import type { AccessListEIP2930Transaction } from './eip2930Transaction.js'
import type { BlobEIP4844Transaction } from './eip4844Transaction.js'
import type { LegacyTransaction } from './legacyTransaction.js'
import type { AccessList, AccessListBytes, Common, Hardfork } from '@ethereumjs/common'
import type { Address, AddressLike, BigIntLike, BytesLike } from '@ethereumjs/util'
import type { Address, AddressLike, BigIntLike, Bytes32, BytesLike } from '@ethereumjs/util'
export type {
AccessList,
AccessListBytes,
Expand Down Expand Up @@ -158,7 +158,7 @@ export interface TransactionInterface<T extends TransactionType = TransactionTyp
serialize(): Uint8Array
getMessageToSign(): Uint8Array | Uint8Array[]
getHashedMessageToSign(): Uint8Array
hash(): Uint8Array
hash(): Bytes32
getMessageToVerifySignature(): Uint8Array
getValidationErrors(): string[]
isSigned(): boolean
Expand Down
20 changes: 20 additions & 0 deletions packages/util/src/types.ts
Expand Up @@ -20,11 +20,31 @@ export type BytesLike =
| TransformabletoBytes
| PrefixedHexString

/**
* Byte type aliases for Uint8Array with a Byte length indicator
*
* Every type added here should have a corresponding PrefixedHex[NUM] type
* (see below).
*/
export type Bytes8 = Uint8Array
export type Bytes20 = Uint8Array
export type Bytes32 = Uint8Array

/*
* A type that represents a `0x`-prefixed hex string.
*/
export type PrefixedHexString = string

/**
* Prefixed hex string type aliases for Uint8Array with a Byte length indicator
*
* Every type added here should have a corresponding Bytes[NUM] type
* (see above).
*/
export type PrefixedHex8 = string
export type PrefixedHex20 = string
export type PrefixedHex32 = string
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be updated to PrefixedHexString rather than string?

Copy link
Member Author

Choose a reason for hiding this comment

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

Do not see the advantage of such alias chaining TBH.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah you're right, but I was thinking in the context of that other PR (where PR was updated to 0x${string}).


/**
* A type that represents an input that can be converted to an Address.
*/
Expand Down