Skip to content

Commit

Permalink
wip: error types
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Feb 2, 2024
1 parent 471e456 commit 4b54154
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/errors/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import type { Hash } from '../types/misc.js'

import { BaseError } from './base.js'

export type BlobSizeTooLargeType = BlobSizeTooLarge & {
name: 'BlobSizeTooLarge'
export type BlobSizeTooLargeErrorType = BlobSizeTooLargeError & {
name: 'BlobSizeTooLargeError'
}
export class BlobSizeTooLarge extends BaseError {
override name = 'BlobSizeTooLarge'
export class BlobSizeTooLargeError extends BaseError {
override name = 'BlobSizeTooLargeError'
constructor({ maxSize, size }: { maxSize: number; size: number }) {
super('Blob size is too large.', {
metaMessages: [`Max: ${maxSize} bytes`, `Given: ${size} bytes`],
Expand Down
10 changes: 8 additions & 2 deletions src/utils/blob/blobsToCommitments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ErrorType } from '../../errors/utils.js'
import type { Kzg } from '../../types/kzg.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { hexToBytes } from '../encoding/toBytes.js'
import { bytesToHex } from '../encoding/toHex.js'
import { type HexToBytesErrorType, hexToBytes } from '../encoding/toBytes.js'
import { type BytesToHexErrorType, bytesToHex } from '../encoding/toHex.js'

type To = 'hex' | 'bytes'

Expand All @@ -21,6 +22,11 @@ export type BlobsToCommitmentsReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray[] : never)
| (to extends 'hex' ? Hex[] : never)

export type BlobsToCommitmentsErrorType =
| HexToBytesErrorType
| BytesToHexErrorType
| ErrorType

/**
* Compute commitments from a list of blobs.
*
Expand Down
10 changes: 8 additions & 2 deletions src/utils/blob/commitmentToVersionedHash.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ErrorType } from '../../errors/utils.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { bytesToHex } from '../encoding/toHex.js'
import { sha256 } from '../hash/sha256.js'
import { type BytesToHexErrorType, bytesToHex } from '../encoding/toHex.js'
import { type Sha256ErrorType, sha256 } from '../hash/sha256.js'

type To = 'hex' | 'bytes'

Expand All @@ -20,6 +21,11 @@ export type CommitmentToVersionedHashReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray : never)
| (to extends 'hex' ? Hex : never)

export type CommitmentToVersionedHashErrorType =
| Sha256ErrorType
| BytesToHexErrorType
| ErrorType

/**
* Transform a commitment to it's versioned hash.
*
Expand Down
10 changes: 9 additions & 1 deletion src/utils/blob/commitmentsToVersionedHashes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { ErrorType } from '../../errors/utils.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { commitmentToVersionedHash } from './commitmentToVersionedHash.js'
import {
type CommitmentToVersionedHashErrorType,
commitmentToVersionedHash,
} from './commitmentToVersionedHash.js'

type To = 'hex' | 'bytes'

Expand All @@ -19,6 +23,10 @@ export type CommitmentsToVersionedHashesReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray[] : never)
| (to extends 'hex' ? Hex[] : never)

export type CommitmentsToVersionedHashesErrorType =
| CommitmentToVersionedHashErrorType
| ErrorType

/**
* Transform a list of commitments to their versioned hashes.
*
Expand Down
10 changes: 9 additions & 1 deletion src/utils/blob/sidecarsToVersionedHashes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ErrorType } from '../../errors/utils.js'
import type { BlobSidecars } from '../../types/eip4844.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { commitmentToVersionedHash } from './commitmentToVersionedHash.js'
import {
type CommitmentToVersionedHashErrorType,
commitmentToVersionedHash,
} from './commitmentToVersionedHash.js'

type To = 'hex' | 'bytes'

Expand All @@ -20,6 +24,10 @@ export type SidecarsToVersionedHashesReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray[] : never)
| (to extends 'hex' ? Hex[] : never)

export type SidecarsToVersionedHashesErrorType =
| CommitmentToVersionedHashErrorType
| ErrorType

/**
* Transforms a list of sidecars to their versioned hashes.
*
Expand Down
10 changes: 8 additions & 2 deletions src/utils/blob/toBlobProofs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ErrorType } from '../../errors/utils.js'
import type { Kzg } from '../../types/kzg.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { hexToBytes } from '../encoding/toBytes.js'
import { bytesToHex } from '../encoding/toHex.js'
import { type HexToBytesErrorType, hexToBytes } from '../encoding/toBytes.js'
import { type BytesToHexErrorType, bytesToHex } from '../encoding/toHex.js'

type To = 'hex' | 'bytes'

Expand Down Expand Up @@ -33,6 +34,11 @@ export type toBlobProofsReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray[] : never)
| (to extends 'hex' ? Hex[] : never)

export type ToBlobProofsErrorType =
| BytesToHexErrorType
| HexToBytesErrorType
| ErrorType

/**
* Compute the proofs for a list of blobs and their commitments.
*
Expand Down
17 changes: 14 additions & 3 deletions src/utils/blob/toBlobSidecars.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { ErrorType } from '../../errors/utils.js'
import type { BlobSidecars } from '../../types/eip4844.js'
import type { Kzg } from '../../types/kzg.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import type { OneOf } from '../../types/utils.js'
import { blobsToCommitments } from './blobsToCommitments.js'
import { toBlobProofs } from './toBlobProofs.js'
import { toBlobs } from './toBlobs.js'
import {
type BlobsToCommitmentsErrorType,
blobsToCommitments,
} from './blobsToCommitments.js'
import { type ToBlobProofsErrorType, toBlobProofs } from './toBlobProofs.js'
import { type ToBlobsErrorType, toBlobs } from './toBlobs.js'

type To = 'hex' | 'bytes'

Expand Down Expand Up @@ -37,10 +41,17 @@ export type ToBlobSidecarsParameters<
proofs: _blobsType | Hex[] | ByteArray[]
}
>

export type ToBlobSidecarsReturnType<to extends To> =
| (to extends 'bytes' ? BlobSidecars<ByteArray> : never)
| (to extends 'hex' ? BlobSidecars<Hex> : never)

export type ToBlobSidecarsErrorType =
| BlobsToCommitmentsErrorType
| ToBlobsErrorType
| ToBlobProofsErrorType
| ErrorType

/**
* Transforms arbitrary data (or blobs, commitments, & proofs) into a sidecar array.
*
Expand Down
27 changes: 22 additions & 5 deletions src/utils/blob/toBlobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import {
fieldElementsPerBlob,
maxBytesPerTransaction,
} from '../../constants/blob.js'
import { BlobSizeTooLarge, EmptyBlobError } from '../../errors/blob.js'
import {
BlobSizeTooLargeError,
type BlobSizeTooLargeErrorType,
EmptyBlobError,
type EmptyBlobErrorType,
} from '../../errors/blob.js'
import type { ErrorType } from '../../errors/utils.js'
import type { ByteArray, Hex } from '../../types/misc.js'
import { size } from '../data/size.js'
import { hexToBytes } from '../encoding/toBytes.js'
import { bytesToHex } from '../encoding/toHex.js'
import { type SizeErrorType, size } from '../data/size.js'
import { type HexToBytesErrorType, hexToBytes } from '../encoding/toBytes.js'
import { type BytesToHexErrorType, bytesToHex } from '../encoding/toHex.js'

type To = 'hex' | 'bytes'

Expand All @@ -25,6 +31,14 @@ export type ToBlobsReturnType<to extends To> =
| (to extends 'bytes' ? ByteArray[] : never)
| (to extends 'hex' ? Hex[] : never)

export type ToBlobsErrorType =
| BlobSizeTooLargeErrorType
| BytesToHexErrorType
| EmptyBlobErrorType
| HexToBytesErrorType
| SizeErrorType
| ErrorType

/**
* Transforms arbitrary data to blobs.
*
Expand All @@ -51,7 +65,10 @@ export function toBlobs<
const size_ = size(data)
if (!size_) throw new EmptyBlobError()
if (size_ > maxBytesPerTransaction)
throw new BlobSizeTooLarge({ maxSize: maxBytesPerTransaction, size: size_ })
throw new BlobSizeTooLargeError({
maxSize: maxBytesPerTransaction,
size: size_,
})

const length = Math.ceil(size_ / bytesPerBlob)
const paddedData = pad(data as any, length)
Expand Down
24 changes: 20 additions & 4 deletions src/utils/transaction/serializeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,22 @@ import type {
TransactionType,
} from '../../types/transaction.js'
import type { OneOf } from '../../types/utils.js'
import { blobsToCommitments } from '../blob/blobsToCommitments.js'
import { commitmentsToVersionedHashes } from '../blob/commitmentsToVersionedHashes.js'
import { toBlobProofs } from '../blob/toBlobProofs.js'
import { toBlobSidecars } from '../blob/toBlobSidecars.js'
import {
type BlobsToCommitmentsErrorType,
blobsToCommitments,
} from '../blob/blobsToCommitments.js'
import {
type CommitmentsToVersionedHashesErrorType,
commitmentsToVersionedHashes,
} from '../blob/commitmentsToVersionedHashes.js'
import {
type ToBlobProofsErrorType,
toBlobProofs,
} from '../blob/toBlobProofs.js'
import {
type ToBlobSidecarsErrorType,
toBlobSidecars,
} from '../blob/toBlobSidecars.js'
import { type ConcatHexErrorType, concatHex } from '../data/concat.js'
import { trim } from '../data/trim.js'
import { type ToHexErrorType, toHex } from '../encoding/toHex.js'
Expand Down Expand Up @@ -107,6 +119,10 @@ export function serializeTransaction<

type SerializeTransactionEIP4844ErrorType =
| AssertTransactionEIP4844ErrorType
| BlobsToCommitmentsErrorType
| CommitmentsToVersionedHashesErrorType
| ToBlobProofsErrorType
| ToBlobSidecarsErrorType
| ConcatHexErrorType
| InvalidLegacyVErrorType
| ToHexErrorType
Expand Down

0 comments on commit 4b54154

Please sign in to comment.