# Ox Ethereum Standard Library # Installation Install Ox via your package manager, a ` ``` ## Using Unreleased Commits If you can't wait for a new release to test the latest features, you can either install from the `canary` tag (tracks the [`main`](https://github.com/wevm/viem/tree/main) branch). :::code-group ```bash [pnpm] pnpm add ox@canary ``` ```bash [npm] npm install ox@canary ``` ```bash [yarn] yarn add ox@canary ``` ```bash [bun] bun add ox@canary ``` ::: Or clone the [Ox repo](https://github.com/wevm/ox) to your local machine, build, and link it yourself. ```bash gh repo clone wevm/ox cd ox pnpm install pnpm build pnpm link --global ``` Then go to the project where you are using Ox and run `pnpm link --global ox` (or the package manager that you used to link Ox globally). ## Security Ethereum-related projects are often targeted in attacks to steal users' assets. Make sure you follow security best-practices for your project. Some quick things to get started. * Pin package versions, upgrade mindfully, and inspect lockfile changes to minimize the risk of [supply-chain attacks](https://nodejs.org/en/guides/security/#supply-chain-attacks). * Install the [Socket Security](https://socket.dev) [GitHub App](https://github.com/apps/socket-security) to help detect and block supply-chain attacks. * Add a [Content Security Policy](https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html) to defend against external scripts running in your app. # Imports & Bundle Size ## Imports There are two approaches to import Modules in Ox: * [Named Imports](#named-imports): Importing modules via the root `ox` namespace. * [Entrypoint Imports](#entrypoint-imports): Importing modules via an `ox/{Module}` namespace. ### Named Imports Modules can be imported via their respective module export in the root `ox` namespace: ```ts twoslash import { Hex, Rlp } from 'ox' const rlp = Rlp.fromHex([Hex.fromString('hello'), Hex.fromString('world')]) ``` This approach does not compromise on [tree-shakability](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking), as most modern bundlers support Deep Scope Analysis. As a result, this will not impact the bundle size of your application. Bundlers known to support Deep Scope Analysis include: [Vite](https://vitejs.dev/), [Rollup](https://rollupjs.org/), [Webpack 5+](https://webpack.js.org/), [esbuild](https://esbuild.github.io/), [swc](https://swc.rs/), and more. ### Entrypoint Imports If your bundler does not support Deep Scope Analysis, you are also able to import modules via their respective entrypoint: ```ts twoslash // @noErrors import * as Hex from 'ox/Hex' import * as Rlp from 'ox/Rlp' const rlp = Rlp.fromHex([Hex.fromString('hello'), Hex.fromString('world')]) ``` ## Tree Shakability & Bundle Size Each Module in Ox exports a number of functions (e.g. `Hex` exports `from`, `concat`, `padLeft`, etc). It is important to note that Modules **are not stateful instances with methods** (ie. you cannot instantiate a `Hex` class/object), they are merely a collection of pure stateless functions. This is because function exports are [tree-shakable](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking), whereas instance methods are not. When Modules are imported in Ox, only the functions that you use from that Module will be included in the final bundle of your application. Unused functions are automatically removed, resulting in a lower bundle size. Whereas, methods that are attached to instances cannot be tree-shaken by bundlers, which will lead to all methods of a given instance being included in the bundle, regardless of whether they are used or not. # Error Handling Every function namespace in Ox exports an accompanying error type (`ErrorType`) and parser (`parseError`) that you can use to strongly type your `catch` statements, or inject into a custom type-safe error handling library (e.g. [`neverthrow`](https://github.com/supermacro/neverthrow), [`Effect`](https://effect.website/), etc.). ## Usage with Vanilla TypeScript Unfortunately, [TypeScript doesn't have an abstraction for typed exceptions](https://github.com/microsoft/TypeScript/issues/13219), so the most pragmatic & vanilla approach would be to explicitly cast error types in the `catch` statement with the function's `.ErrorType` property. ```ts twoslash import { AbiParameters, Errors, Hex } from 'ox' try { AbiParameters.encode( AbiParameters.from('address'), ['0xc961145a54c96e3ae9baa048c4f4d6b04c13916b'] ) } catch (err) { const error = err as AbiParameters.encode.ErrorType error.name // ^? if (error.name === 'Address.InvalidAddressError') error.cause.name // ^? } ``` ## Usage with `neverthrow` You can utilize Ox's `.ErrorType` property into custom type-safe error handling libraries like [`neverthrow`](https://github.com/supermacro/neverthrow). ```ts twoslash // @noErrors import { AbiParameters } from 'ox' import { fromThrowable } from 'neverthrow'; const encode = fromThrowable( // [!code hl] AbiParameters.encode, // [!code hl] e => e as AbiParameters.encode.ErrorType // [!code hl] ) // [!code hl] const result = encode(AbiParameters.from('bytes'), ['0xdeadbeef']) if (result.isErr()) // [!code hl] result.error.name // [!code hl] // ^? ``` # Platform Compatibility \[Platforms compatible with Ox] **Ox supports all modern browsers (Chrome, Edge, Firefox, etc) & runtime environments (Node 18+, Deno, Bun, etc).** Ox uses modern EcmaScript features such as: * [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) * Error [`cause`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) * TextEncoder [`encode`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encode) You can check support for these features on [Can I use...](https://caniuse.com/) ## Polyfills If your platform does not support one of the required features, it is also possible to import a polyfill. ### Error `cause` * [core-js](https://github.com/zloirock/core-js) ### `TextEncoder` * [FastestSmallestTextEncoderDecoder](https://github.com/anonyco/FastestSmallestTextEncoderDecoder) # Migrating from v0 Ox v1 includes breaking changes to decoded addresses, cryptographic values, blob handling, and Tempo types. This guide covers every breaking change and the migration required for each one. First, upgrade Ox to v1. :::code-group ```bash [pnpm] pnpm add ox@^1 ``` ```bash [npm] npm install ox@^1 ``` ```bash [yarn] yarn add ox@^1 ``` ```bash [bun] bun add ox@^1 ``` ::: ## ABI addresses are checksummed ABI decode functions now checksum decoded addresses by default. If your application compares decoded addresses as case-sensitive strings, normalize both sides or pass `checksumAddress: false` to preserve the v0 behavior. ```ts const values = AbiParameters.decode(parameters, data) // [!code --] const values = AbiParameters.decode(parameters, data, { // [!code ++] checksumAddress: false, // [!code ++] }) // [!code ++] ``` The option is also available on the higher-level ABI decode functions that return addresses. ## Cryptographic coordinates use padded hex ECDSA and BLS coordinate fields now use padded `Hex.Hex` strings instead of `bigint`. This includes `r`, `s`, `x`, `y`, and BLS `Fp`/`Fp2` values on `Signature`, `PublicKey`, `BlsPoint`, `Transaction`, `Authorization`, `TxEnvelope`, and related Tempo and ERC types. ECDSA coordinates are 32 bytes. BLS12-381 coordinates are 48 bytes. The `bigintType` generic has been removed. ```ts const signature = Signature.from({ // [!code --] r: 0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bfn, // [!code --] s: 0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8n, // [!code --] yParity: 1, // [!code --] }) // [!code --] const signature = Signature.from({ // [!code ++] r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // [!code ++] s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // [!code ++] yParity: 1, // [!code ++] }) // [!code ++] ``` Use `Hex.fromNumber(value, { size: 32 })` for ECDSA values and `Hex.fromNumber(value, { size: 48 })` for BLS12-381 values when migrating stored bigints. ## Noble and Scure dependencies use v2 Ox now uses v2 of `@noble/ciphers`, `@noble/curves`, `@noble/hashes`, `@scure/bip32`, and `@scure/bip39`. ECDSA signatures now default to `lowS: true` for both `Secp256k1` and `P256`. In v0, `P256` signatures could have high-S values. If you use the `noble` re-exports on `Secp256k1`, `P256`, `Ed25519`, `X25519`, or `Bls`, migrate to the noble v2 API. Notable renames include: | v0 | v1 | | --- | --- | | `ProjectivePoint` or `ExtendedPoint` | `Point` | | `bls.sign` and `bls.verify` | `bls.longSignatures.sign` and `bls.longSignatures.verify` | Refer to the relevant noble and Scure v2 changelogs for the complete upstream API changes. ## PeerDAS replaces blob sidecars The EIP-4844 blob-sidecar API has been removed in favor of PeerDAS (EIP-7594). * `Kzg.Kzg` no longer includes `computeBlobKzgProof`. * `Blobs.toSidecars`, `Blobs.toProofs`, and `Blobs.sidecarsToVersionedHashes` have been removed. * `Blobs.BlobSidecar` and `Blobs.BlobSidecars` have been removed. * `TxEnvelopeEip4844.sidecars` and the legacy network-wrapper serialization have been removed. Use a PeerDAS-capable KZG backend. It must provide `blobToKzgCommitment`, `computeCells`, `computeCellsAndKzgProofs`, `recoverCellsAndKzgProofs`, and `verifyCellKzgProofBatch`. Use `BlobCells` to construct and verify cells and data columns. ```ts const sidecars = Blobs.toSidecars(blobs, { kzg }) // [!code --] const versionedHashes = Blobs.sidecarsToVersionedHashes(sidecars) // [!code --] const columns = BlobCells.toDataColumns(blobs, { kzg }) // [!code ++] const versionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) // [!code ++] ``` `Kzg.Kzg.blobToKzgCommitment` and `Blobs.toVersionedHashes` remain available for transaction versioned-hash derivation. ## Tempo addresses use plain hex The `TempoAddress` module and its `tempox`-prefixed address format have been removed. Tempo modules now accept plain `Address.Address` hex values. Remove calls to `TempoAddress.format`, `TempoAddress.parse`, and `TempoAddress.resolve` at application boundaries. The `addressType` generic has also been removed from `Call`, `TxEnvelopeTempo`, `KeyAuthorization`, `AuthorizationTempo`, and `TransactionRequest`. ```ts import { TempoAddress } from 'ox/tempo' // [!code --] const formatted = TempoAddress.format(address) // [!code --] const { address: resolved } = TempoAddress.parse(formatted) // [!code --] const resolved = address // [!code ++] ``` ## Tempo tokens use addresses The Tempo `TokenId` module has been removed. `feeToken`, `Channel.token`, and the token arguments passed to `PoolId.from` now accept `Address.Address` values instead of numeric token IDs. ```ts const envelope = TxEnvelopeTempo.from({ // ... feeToken: 1n, // [!code --] feeToken: '0x20c0000000000000000000000000000000000001', // [!code ++] }) ``` `Channel.Resolved` has also been removed. Use `Channel.Channel` with address-valued token fields. ## Tempo multisig no longer uses `config_id` TIP-1061 multisig account addresses now derive directly from the initial configuration. `MultisigConfig.toId` and the `genesisConfigId` fields have been removed. ```ts const id = MultisigConfig.toId(genesisConfig) // [!code --] const account = MultisigConfig.getAddress({ genesisConfigId: id }) // [!code --] const account = MultisigConfig.getAddress(genesisConfig) // [!code ++] const payload = MultisigConfig.getSignPayload({ // [!code --] payload: transactionPayload, // [!code --] account, // [!code --] genesisConfigId: id, // [!code --] }) // [!code --] const payload = MultisigConfig.getSignPayload({ // [!code ++] payload: transactionPayload, // [!code ++] account, // [!code ++] }) // [!code ++] const envelope = SignatureEnvelope.from({ // [!code --] account, // [!code --] genesisConfigId: id, // [!code --] signatures, // [!code --] }) // [!code --] const envelope = SignatureEnvelope.from({ account, signatures }) // [!code ++] ``` Owner approval digests now bind only the account. The signature wire format is `0x05 || rlp([account, signatures, init?])`, `MultisigConfig.maxOwners` is 255 with `u8` weights, and owner approvals may contain nested multisig signatures. ## RPC schema codecs return native quantities The `ox/zod` RPC schema codecs now decode scalar quantity results to native values. Balance, gas, fee, and block-number quantities decode to `bigint`; chain IDs, transaction counts, and block transaction counts decode to `number`. Use `RpcSchema.FromZod` when you need the raw JSON-RPC wire types. Use the codec return types when consuming decoded values. ## Transaction envelope type detection expects native types `TransactionEnvelope.getType` now rejects RPC-style type strings such as `'0x0'` through `'0x4'`. Convert RPC transaction requests before detecting their envelope type. ```ts const type = TransactionEnvelope.getType(rpcRequest) // [!code --] const request = TransactionRequest.fromRpc(rpcRequest) // [!code ++] const type = TransactionEnvelope.getType(request) // [!code ++] ``` ## Stricter validation Ox v1 rejects malformed values that v0 sometimes accepted or normalized. Audit code that relies on permissive parsing, particularly for: * undersized ABI data, invalid packed-array lengths, and anonymous event topics; * weak or invalid keystore KDF parameters; * malformed public keys, blooms, typed data, COSE keys, values, RLP, and JSON-RPC responses; * non-32-byte access-list storage keys; * EIP-4844 envelopes without `blobVersionedHashes`; and * invalid WebAuthn assertion flags or extension data. See the [v1.0.0 changelog](https://github.com/wevm/ox/blob/main/CHANGELOG.md#100) for the full list of corrected validation and serialization behavior. # Benchmarks These benchmarks compare high-level operations across Ox implementations and engine providers. Lower timings are better. The following results are mean single-call durations from an Apple M4 Max running macOS 26.5.2 and Node.js 25.9.0. The comparison uses Ox 1.2.0 and 0.14.33. The fastest result in each row is bold. | Task | Operation | `ox v0` | `ox` | `ox/node` | `ox/wasm` | Fastest vs. Ox v0 | | --- | --- | ---: | ---: | ---: | ---: | ---: | | Calculate Swap Input Amount | `getAmountIn` (bigint) | 484.0 ns | 526.4 ns | 480.0 ns | **479.6 ns** | 1.0× faster | | Calculate Swap Output Amount | `getAmountOut` (bigint) | 186.7 ns | **184.4 ns** | 191.3 ns | 184.7 ns | 1.0× faster | | Decode RLP Struct | `Rlp.toBytes` | **146.8 ns** | 149.9 ns | 150.7 ns | 161.0 ns | Baseline | | Decrypt JSON Keystore | `Keystore.decrypt` | 5.9 µs | 4.7 µs | 5.2 µs | **2.4 µs** | 2.5× faster | | Derive CREATE2 Contract Address | `ContractAddress.fromCreate2` | 14.7 µs | 11.4 µs | 11.6 µs | **6.8 µs** | 2.2× faster | | Derive Mnemonic Private Key | `Mnemonic.toPrivateKey` | 7.26 ms | 7.34 ms | **1.82 ms** | 3.04 ms | 4.0× faster | | Encode Event Topics | `AbiEvent.encode` | 4.5 µs | 3.3 µs | 3.3 µs | **1.0 µs** | 4.7× faster | | Encode RLP Struct | `Rlp.fromBytes` | 257.0 ns | 118.5 ns | 118.9 ns | **117.6 ns** | 2.2× faster | | Encode Seaport Fulfill Order | `AbiFunction.encodeData` (cached) | 13.4 µs | 7.4 µs | 7.3 µs | **7.2 µs** | 1.9× faster | | Encode Seaport Fulfill Order | `AbiFunction.encodeData` (dynamic) | 39.5 µs | 29.3 µs | 28.4 µs | **24.1 µs** | 1.6× faster | | Encode Uniswap V2 Swap | `AbiFunction.encodeData` (cached) | 1.72 µs | 1.08 µs | **1.07 µs** | 1.19 µs | 1.6× faster | | Encode Uniswap V2 Swap | `AbiFunction.encodeData` (dynamic) | 8.8 µs | 6.9 µs | 6.7 µs | **4.2 µs** | 2.1× faster | | Generate Random Private Key | `Secp256k1.randomPrivateKey` | 1.61 µs | **1.47 µs** | 1.51 µs | 1.54 µs | 1.1× faster | | Get Personal Message Sign Payload | `PersonalMessage.getSignPayload` | 4.9 µs | 3.4 µs | 3.4 µs | **1.0 µs** | 4.8× faster | | Get Transaction Sign Payload | `TransactionEnvelope.getSignPayload` | 5.6 µs | 3.9 µs | 4.0 µs | **1.5 µs** | 3.6× faster | | Hash 32-byte Payload | `Hash.keccak256` (32 B) | 3.55 µs | 2.59 µs | 2.60 µs | **343 ns** | 10.4× faster | | Hash Typed Data | `TypedData.getSignPayload` | 70.3 µs | 52.0 µs | 52.5 µs | **20.0 µs** | 3.5× faster | | Recover Secp256k1 Public Key | `Secp256k1.recoverPublicKey` (32 B) | 1.05 ms | 1.10 ms | 1.10 ms | **36.6 µs** | 28.8× faster | | Sign Secp256k1 Message | `Secp256k1.sign` (32 B message) | 178.6 µs | 172.6 µs | 171.9 µs | **24.2 µs** | 7.4× faster | | Verify Secp256k1 Signature | `Secp256k1.verify` (32 B message) | 1.83 ms | 963.1 µs | 965.0 µs | **31.3 µs** | 58.3× faster | WASM has the largest effect on operations that perform one or more Keccak-256 hashes. Node.js has the largest effect on mnemonic derivation because its engine supplies native PBKDF2. ABI encoding remains mostly JavaScript data-layout work. The cached case extracts and prepares Seaport's `fulfillOrder` function before measurement. The dynamic case searches the complete Seaport ABI and prepares the function inside every measured call. The Uniswap V2, bigint, and RLP cases use the inputs from the [Alloy benchmark suite](https://github.com/alloy-rs/examples/tree/main/benches). The dynamic Uniswap case parses its JSON ABI inside every measured call. Provider engines do not override bigint arithmetic or RLP encoding, so small differences among `ox`, `ox/node`, and `ox/wasm` in those rows are runtime noise. ## Method Run the comparison from the Ox repository: ```sh pnpm bench:comparison --run ``` The harness uses Vitest bench through Vite+. It runs each provider in an isolated file and runs the files sequentially to avoid CPU contention. Every provider receives the same inputs. Provider initialization, WASM compilation, fixture preparation, and keystore key derivation happen outside the timed functions. The `ox/wasm` variant combines the aggregate WASM engine with its opt-in Keystore and Secp256k1 providers. Ox 0.14.33 does not expose the generic `TransactionEnvelope.getSignPayload` function. That column uses `TxEnvelopeEip1559.getSignPayload`, the equivalent for the EIP-1559 fixture. Ox exposes event topic encoding as `AbiEvent.encode`. This benchmark measures the operation sometimes described as `AbiEvent.encodeTopics`. These results are a local snapshot, not a performance guarantee. CPU, JavaScript runtime, OpenSSL, WASM runtime, inputs, and background load can change both timings and rankings. For lower-level cryptographic measurements, see [WASM and engine benchmarks](/guides/runtime/engines#benchmarks). # Guides ## Overview Task-oriented guides for building with Ox. Each guide combines related modules into a complete workflow and links to the [API reference](/api) for deeper detail. New to Ox? Start with [Installation](/installation), then choose a guide below. # ABIs & Contracts ## Overview The [Application Binary Interface (ABI)](https://docs.soliditylang.org/en/latest/abi-spec.html) defines how data is encoded and decoded between your application and a contract's bytecode. Ox provides a module for each ABI item type — [`Abi`](/api/Abi), [`AbiFunction`](/api/AbiFunction), [`AbiEvent`](/api/AbiEvent), [`AbiError`](/api/AbiError), and [`AbiConstructor`](/api/AbiConstructor) — covering everything between your app and the contract: parsing ABIs, encoding calls, and decoding what comes back. Recipes end at the encoded payload; hand it to a JSON-RPC transport, or use a higher-level client like [Viem](https://viem.sh) for full contract workflows. ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', ]) const approve = AbiFunction.fromAbi(abi, 'approve') const data = AbiFunction.encodeData(approve, [ '0xcb98643b8786950f0461f3b0edf99d88f274574d', 100_000_000n, ]) // @log: '0x095ea7b3000000000000000000000000cb98643b8786950f0461f3b0edf99d88f274574d0000000000000000000000000000000000000000000000000000000005f5e100' ``` # Deploy Contracts & Compute Addresses ## Overview A contract deployment is a transaction whose calldata is the contract's bytecode with the ABI-encoded constructor arguments appended, and no `to` address. [`AbiConstructor`](/api/AbiConstructor) encodes that deploy data, and [`ContractAddress`](/api/ContractAddress) computes the address the deployment will land on — before anything is broadcast. ## Recipes ### Encode Constructor Arguments Define the constructor with [`AbiConstructor.from`](/api/AbiConstructor/from), then append the encoded arguments to the bytecode with [`AbiConstructor.encode`](/api/AbiConstructor/encode). The result is the deploy transaction's calldata. ```ts twoslash import { AbiConstructor, RpcTransport } from 'ox' const bytecode = '0x...' const constructor = AbiConstructor.from('constructor(address owner)') const data = AbiConstructor.encode(constructor, { bytecode, args: ['0x9f1fdab6458c5fc642fa0f4c5af7473c46837357'], }) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const hash = await transport.request({ method: 'eth_sendTransaction', params: [{ data }], }) ``` If you are working with a JSON ABI, extract the constructor with [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi). Note that deploy transactions omit the `to` address. ### Compute a CREATE Address Ordinary deployments use the `CREATE` opcode: the resulting address is derived from the deployer address and its account nonce at deployment time. [`ContractAddress.fromCreate`](/api/ContractAddress/fromCreate) computes it locally. ```ts twoslash import { ContractAddress } from 'ox' const address = ContractAddress.fromCreate({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', nonce: 0n, // [!code hl] }) // @log: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2' ``` `nonce` is the deployer's transaction count at the moment of deployment (`eth_getTransactionCount` for an EOA), not a value you choose. ### Compute a CREATE2 Address Factory deployments via the [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode derive the address from the factory address, a salt, and the init code — so the address is deterministic and independent of nonces. Use [`ContractAddress.fromCreate2`](/api/ContractAddress/fromCreate2). ```ts twoslash import { ContractAddress, Hex } from 'ox' const address = ContractAddress.fromCreate2({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', bytecode: '0x6394198df16000526103ff60206004601c335afa6040516060f3', salt: Hex.fromString('hello world'), // [!code hl] }) // @log: '0x59fbb593abe27cb193b6ee5c5dc7bbde312290ab' ``` `bytecode` is the full init code — the deployment bytecode plus encoded constructor arguments, i.e. the same `data` produced by `AbiConstructor.encode`. If you already have `keccak256` of the init code, pass it as `bytecodeHash` instead. ## Best Practices ### CREATE2 Commits to the Init Code Constructor arguments are part of the init code, so changing them changes the CREATE2 address. Compute the address from the exact `data` you will deploy, not from the bare bytecode. ### Pin the Deployer Nonce CREATE addresses shift with every transaction the deployer sends. If a predictable address matters, compute it from a fresh nonce and deploy before anything else, or use CREATE2. ## See More # Work with ABIs ## Overview [`Abi`](/api/Abi) parses human-readable and JSON ABIs into typed objects, and [`AbiItem`](/api/AbiItem) and [`AbiParameter`](/api/AbiParameter) operate on individual items and parameters — extraction, formatting, signatures, and selectors. The `Solidity` module supplies companion constants (e.g. `Solidity.maxUint256`) and regexes for validating types and values. ## Recipes ### Use Human-Readable ABIs [`Abi.from`](/api/Abi/from) parses Solidity-style signatures into a fully typed JSON ABI — types are inferred at the type level, so downstream encoding and decoding stay type-safe. ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', 'error InsufficientBalance(uint256 available, uint256 required)', ]) ``` Human-readable signatures support functions, events, errors, constructors, fallback/receive functions, structs, and bare parameters: ```ts 'function balanceOf(address owner) view returns (uint256)' 'event Transfer(address indexed from, address indexed to, uint256 amount)' 'error Unauthorized(address caller)' 'constructor(address owner) payable' 'fallback() external payable' 'receive() external payable' 'struct Account { address owner; uint256 balance; }' 'address spender, uint256 amount' ``` Some syntax rules are enforced for parity between runtime parsing and type-level inference: whitespace matters (`'function name() returns (string)'`, not `'function name()returns(string)'`), semicolons are omitted, parameters may be named or unnamed, inline tuples map to `tuple` parameters, and struct signatures must be declared before the signature that uses them (recursive structs are not supported). ### Parse a JSON ABI [`Abi.from`](/api/Abi/from) also accepts JSON ABIs (e.g. compiler output). Extract typed items with the per-type extractors, such as [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi). ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ { type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' }, ], outputs: [{ type: 'bool' }], }, ]) const approve = AbiFunction.fromAbi(abi, 'approve') ``` Each item type has its own extractor — [`AbiEvent.fromAbi`](/api/AbiEvent/fromAbi), [`AbiError.fromAbi`](/api/AbiError/fromAbi), and [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi). ### Format Back to Human-Readable [`Abi.format`](/api/Abi/format) converts a JSON ABI back into human-readable signatures — useful for display, logging, or storing ABIs compactly. ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', ]) const formatted = Abi.format(abi) // @log: [ // @log: 'function approve(address spender, uint256 amount) returns (bool)', // @log: 'event Transfer(address indexed from, address indexed to, uint256 amount)', // @log: ] ``` Single items and parameters format the same way with [`AbiItem.format`](/api/AbiItem/format) and [`AbiParameter.format`](/api/AbiParameter/format). ### Extract Items & Compute Selectors [`AbiItem.fromAbi`](/api/AbiItem/fromAbi) extracts an item by name — or by selector, which is handy for reverse-lookups from calldata or log topics. [`AbiItem.getSelector`](/api/AbiItem/getSelector) and [`AbiItem.getSignatureHash`](/api/AbiItem/getSignatureHash) compute the identifiers themselves. ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)', 'event Transfer(address indexed from, address indexed to, uint256 amount)', ]) const approve = AbiItem.fromAbi(abi, 'approve') const signature = AbiItem.getSignature(approve) // @log: 'approve(address,uint256)' const selector = AbiItem.getSelector(approve) // @log: '0x095ea7b3' const topic = AbiItem.getSignatureHash(abi, 'Transfer') // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' const bySelector = AbiItem.fromAbi(abi, '0x095ea7b3') ``` Functions and errors are identified by the first 4 bytes of the signature hash (the selector); events are identified by the full 32-byte signature hash in `topics[0]`. ## Best Practices ### Keep ABIs Statically Analyzable Type inference only works when the ABI is a literal in source (or generated as one). ABIs parsed from runtime strings or fetched JSON widen to generic types — encoding still works, but you lose compile-time checking of names and arguments. ### Prefer Human-Readable ABIs in Source They diff cleanly, carry parameter names, and parse to the same JSON ABI. Reserve raw JSON for ABIs consumed directly from compiler artifacts. ## See More # Work with Events & Logs ## Overview Contracts emit events as logs: indexed arguments become `topics` (filterable by the node), while non-indexed arguments are ABI-encoded into `data`. [`AbiEvent`](/api/AbiEvent) encodes an event's indexed arguments into topics for log queries (e.g. `eth_getLogs`), and decodes the resulting logs back into named, typed arguments. ## Recipes ### Build an Event Filter Encode an event and its indexed arguments into topics with [`AbiEvent.encode`](/api/AbiEvent/encode), then pass the topics to a log query. Below, we filter for ERC-20 `Transfer` events sent from a specific address. ```ts twoslash import { AbiEvent, RpcTransport } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const { topics } = AbiEvent.encode(transfer, { from: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', // [!code hl] }) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const logs = await transport.request({ method: 'eth_getLogs', params: [{ topics }], }) ``` If you are working with a JSON ABI, extract the event with [`AbiEvent.fromAbi`](/api/AbiEvent/fromAbi) instead of defining it inline. ### Decode a Log Against an Event Decode each returned log's `topics` and `data` into named arguments with [`AbiEvent.decode`](/api/AbiEvent/decode). ```ts twoslash import { AbiEvent, RpcTransport } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const { topics } = AbiEvent.encode(transfer, { from: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', }) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const logs = await transport.request({ method: 'eth_getLogs', params: [{ topics }], }) const decoded = logs.map((log) => AbiEvent.decode(transfer, log)) // @log: [ // @log: { // @log: from: '0x9F1fdAb6458c5fc642fa0F4C5af7473C46837357', // @log: to: '0xcb98643b8786950F0461f3B0edf99D88F274574D', // @log: value: 1n, // @log: }, // @log: ... // @log: ] ``` Decoding throws if the log's selector (`topics[0]`) does not match the event. To decode heterogeneous logs against a whole ABI, use [`AbiEvent.extractLogs`](/api/AbiEvent/extractLogs), which matches each log by selector and skips non-matching entries. ### Handle Indexed & Non-Indexed Arguments Only indexed arguments can be filtered on. Omitted indexed arguments produce a `null` topic, which matches any value — non-indexed arguments (like `value` below) never appear in `topics`. ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const { topics } = AbiEvent.encode(transfer, { to: '0xcb98643b8786950f0461f3b0edf99d88f274574d', // [!code hl] }) // @log: [ // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', // @log: null, // @log: '0x000000000000000000000000cb98643b8786950f0461f3b0edf99d88f274574d', // @log: ] ``` Dynamic indexed types (`string`, `bytes`, arrays, and tuples) are stored as keccak256 hashes in topics, so decoding returns the 32-byte hash — not the original value. Keep such arguments non-indexed when you need to read them back from logs. ## Best Practices ### Match the Deployed Signature Exactly The selector and topic layout are derived from the event signature, including which parameters are `indexed`. A signature that drifts from the deployed contract will fail to match logs or mis-assign arguments. ### Constrain Block Ranges Most providers cap `eth_getLogs` queries. Pass `fromBlock`/`toBlock` bounds and paginate over ranges instead of querying the entire chain in one request. ## See More # Work with Function Calls ## Overview [`AbiFunction`](/api/AbiFunction) encodes a function and its arguments into calldata, and decodes call results and incoming calldata back into typed values. Underneath sits [`AbiParameters`](/api/AbiParameters), the coder for standalone parameter lists. Recipes below use a raw JSON-RPC transport; any [EIP-1193 provider](/api/Provider) or client works the same way. ## Recipes ### Encode a Read Call & Decode the Result Read-only (`pure`/`view`) functions execute via `eth_call` — no transaction, no gas. Encode the call with [`AbiFunction.encodeData`](/api/AbiFunction/encodeData) and decode the return value with [`AbiFunction.decodeResult`](/api/AbiFunction/decodeResult). Below, we read an ERC-20 balance. ```ts twoslash import { AbiFunction, RpcTransport } from 'ox' const balanceOf = AbiFunction.from( 'function balanceOf(address) returns (uint256)', ) const data = AbiFunction.encodeData(balanceOf, [ '0xcb98643b8786950f0461f3b0edf99d88f274574d', ]) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const result = await transport.request({ method: 'eth_call', params: [ { data, to: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', }, ], }) const balance = AbiFunction.decodeResult(balanceOf, result) ``` If you are working with a JSON ABI, extract the function with [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi) instead of defining it inline. ### Encode a State-Modifying Call State-modifying (`nonpayable`/`payable`) functions require a transaction. The calldata is encoded exactly the same way — only the JSON-RPC method changes. Below, we approve an ERC-20 spend. ```ts twoslash import { AbiFunction, RpcTransport, Value } from 'ox' const approve = AbiFunction.from( 'function approve(address spender, uint256 amount) returns (bool)', ) const data = AbiFunction.encodeData(approve, [ '0xcb98643b8786950f0461f3b0edf99d88f274574d', Value.from('100', 6), // 100 USDC (6 decimals) // [!code hl] ]) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const hash = await transport.request({ method: 'eth_sendTransaction', params: [ { data, to: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', }, ], }) ``` Swap `eth_sendTransaction` for `eth_call` to simulate the call (and decode its return value with `AbiFunction.decodeResult`), or `eth_estimateGas` to estimate its gas. To sign with a local key instead of a node-managed account, place the calldata in a [transaction envelope](/guides/transactions/build-sign-send). ### Decode Incoming Calldata For routers, inspectors, or smart account implementations, reverse the flow: extract the function by the calldata's 4-byte selector with [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi), then decode the arguments with [`AbiFunction.decodeData`](/api/AbiFunction/decodeData). ```ts twoslash import { Abi, AbiFunction, Hex } from 'ox' // Calldata from a transaction `input` or a user operation. declare const data: Hex.Hex const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)', 'function transfer(address to, uint256 amount) returns (bool)', ]) const fn = AbiFunction.fromAbi(abi, data) // [!code hl] const args = AbiFunction.decodeData(fn, data) // @log: fn.name: 'approve' // @log: args: ['0xcb98643b8786950F0461f3B0edf99D88F274574D', 100000000n] ``` `fromAbi` throws if no item in the ABI matches the selector — branch on `fn.name` to route the call. ### Encode & Decode Standalone Parameters [`AbiParameters.encode`](/api/AbiParameters/encode) and [`AbiParameters.decode`](/api/AbiParameters/decode) code raw parameter lists without a function selector — the building blocks for structured data that is hashed, signed, or embedded in other payloads. ```ts twoslash import { AbiParameters } from 'ox' const encoded = AbiParameters.encode( AbiParameters.from(['address', 'uint32[]']), ['0xcb98643b8786950F0461f3B0edf99D88F274574D', [1, 2, 3]], ) const decoded = AbiParameters.decode( AbiParameters.from(['address', 'uint32[]']), encoded, ) // @log: ['0xcb98643b8786950F0461f3B0edf99D88F274574D', [1, 2, 3]] ``` Parameters can also be defined as JSON objects (e.g. `[{ type: 'address' }]`) or full human-readable signatures via [`AbiParameters.from`](/api/AbiParameters/from). ### Packed Encoding [`AbiParameters.encodePacked`](/api/AbiParameters/encodePacked) implements Solidity's [`abi.encodePacked`](https://docs.soliditylang.org/en/latest/abi-spec.html#non-standard-packed-mode): values are concatenated without padding or offsets. ```ts twoslash import { AbiParameters } from 'ox' const encoded = AbiParameters.encodePacked( ['address', 'string'], ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 'hello world'], ) // @log: '0xd8da6bf26964af9d7eed9e03e53415d37aa9604568656c6c6f20776f726c64' ``` Packed encoding is not self-describing and cannot be decoded — use it for hashing and signature payloads, not for data you need to read back. ## Best Practices ### Simulate Before You Send Run the same calldata through `eth_call` before broadcasting a transaction. Reverts surface as decodable [error data](/guides/abi/errors) instead of a wasted transaction fee. ### Prefer Standard Encoding Over Packed Packed encoding of multiple dynamic types is ambiguous (`("a", "bc")` and `("ab", "c")` collide). Reserve `encodePacked` for cases that require byte-exact parity with Solidity, and hash structured data with standard encoding otherwise. ## See More # Work with Reverts & Custom Errors ## Overview When a contract call reverts, the node returns ABI-encoded revert data: a 4-byte error selector followed by the encoded arguments. [`AbiError`](/api/AbiError) decodes custom errors, legacy `Error(string)` revert reasons, and compiler `Panic` codes back into typed values — and encodes them again for testing. ## Recipes ### Decode Revert Data from `eth_call` A reverted `eth_call` surfaces its revert data on the `data` field of the JSON-RPC error response. [`AbiError.extract`](/api/AbiError/extract) matches the data's selector against the ABI and decodes the arguments in one step. ```ts twoslash import { Abi, AbiError, Hex } from 'ox' // Revert data from an `eth_call` JSON-RPC error response. declare const data: Hex.Hex const abi = Abi.from([ 'function transfer(address to, uint256 amount) returns (bool)', 'error InsufficientBalance(uint256 available, uint256 required)', ]) const { error, args } = AbiError.extract(abi, data) // @log: { // @log: error: { name: 'InsufficientBalance', type: 'error', ... }, // @log: args: [100n, 320n], // @log: } ``` `Error(string)` reverts (from `require(condition, "reason")`) and `Panic` reverts (from failed `assert`s, overflows, etc.) are matched automatically, even when they are not present in the ABI. ### Match an Error by Selector To branch on a specific error before decoding, compare the first 4 bytes of the revert data against the error's selector from [`AbiError.getSelector`](/api/AbiError/getSelector). ```ts twoslash import { AbiError, Hex } from 'ox' declare const data: Hex.Hex const insufficientBalance = AbiError.from( 'error InsufficientBalance(uint256 available, uint256 required)', ) const selector = Hex.slice(data, 0, 4) // [!code hl] if (selector === AbiError.getSelector(insufficientBalance)) { const [available, required] = AbiError.decode(insufficientBalance, data) } ``` The built-in Solidity cases have exported selectors and definitions — `Panic` codes map to human-readable descriptions via [`AbiError.panicReasons`](/api/AbiError). ```ts twoslash import { AbiError, Hex } from 'ox' declare const data: Hex.Hex if (Hex.slice(data, 0, 4) === AbiError.solidityErrorSelector) { const reason = AbiError.decode(AbiError.solidityError, data) } // @log: 'Not enough Ether provided.' if (Hex.slice(data, 0, 4) === AbiError.solidityPanicSelector) { const code = AbiError.decode(AbiError.solidityPanic, data) // @log: 'Arithmetic operation resulted in underflow or overflow.' const reason = AbiError.panicReasons[Number(code)] } ``` ### Encode Errors for Testing [`AbiError.encode`](/api/AbiError/encode) produces the selector-prefixed revert data for an error and its arguments — useful for stubbing `eth_call` responses or asserting expected reverts in tests. ```ts twoslash import { AbiError } from 'ox' const error = AbiError.from( 'error InsufficientBalance(uint256 available, uint256 required)', ) const data = AbiError.encode(error, [100n, 320n]) // @log: '0xcf47918100000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000140' ``` ## Best Practices ### Keep Custom Errors in the ABI Revert data identifies an error only by its 4-byte selector. Include every custom error the contract (and the libraries it calls) can throw in the ABI you pass to `AbiError.extract`, or matching will fail with a "not found" error. ### Treat Revert Reasons as Untrusted Output Revert strings and error arguments come from contract code. Log them for diagnostics, but do not render them unescaped or branch security decisions on their contents. ## See More # Account Abstraction :::info These guides cover Ox's low-level primitives. If you are looking for a high-level bundler and smart account client, check out [Viem's Account Abstraction guides](https://viem.sh/account-abstraction). ::: ## Overview Account abstraction moves accounts from externally-owned key pairs to smart contracts, changing how transactions are built, executed, and attributed. Ox ships primitives for each layer of that stack: [`UserOperation`](/ercs/erc4337/UserOperation) constructs and signs ERC-4337 user operations, [`Calls`](/ercs/erc7821/Calls) and [`Execute`](/ercs/erc7821/Execute) encode ERC-7821 batched executions, and [`Attribution`](/ercs/erc8021/Attribution) appends ERC-8021 attribution codes to calldata. To upgrade an existing EOA into a smart account, see [Delegate with EIP-7702](/guides/transactions/eip-7702). ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' import { Execute } from 'ox/erc7821' // 1. Encode a batch of calls for an ERC-7821 account. const callData = Execute.encodeData([ { to: '0xcafebabecafebabecafebabecafebabecafebabe', value: Value.fromEther('1'), }, { data: '0xdeadbeef', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', }, ]) // 2. Wrap it in an ERC-4337 user operation. const userOperation = UserOperation.from({ callData, callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 0n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n, }) ``` ## Guides # Attribute Calldata with ERC-8021 ## Overview [ERC-8021](https://eip.tools/eip/8021) appends a self-describing suffix to transaction calldata so the entities that facilitated a transaction — apps, wallets, and services — can be attributed onchain. Because ABI decoding ignores trailing calldata bytes, the suffix travels with the transaction without changing its behavior. The [`Attribution`](/ercs/erc8021/Attribution) module encodes and extracts these suffixes. ## Recipes ### Append an Attribution Suffix Convert attribution codes to a data suffix with `Attribution.toDataSuffix` and concatenate it onto the encoded call. ```ts twoslash import { AbiFunction, Hex } from 'ox' import { Attribution } from 'ox/erc8021' const transfer = AbiFunction.from( 'function transfer(address recipient, uint256 amount)', ) const data = AbiFunction.encodeData(transfer, [ '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 100n, ]) const suffix = Attribution.toDataSuffix({ codes: ['baseapp', 'morpho'], // [!code hl] }) // @log: '0x626173656170702c6d6f7270686f0e0080218021802180218021802180218021' const calldata = Hex.concat(data, suffix) ``` The example above uses schema 0 (canonical registry). Pass a `codeRegistry` for schema 1 (custom registry), or fields like `appCode`, `walletCode`, and `metadata` for schema 2 (CBOR-encoded). ### Parse Attribution Codes Extract the attribution from transaction calldata with `Attribution.fromData`. It returns `undefined` when no valid suffix is present. ```ts twoslash import { Attribution } from 'ox/erc8021' const attribution = Attribution.fromData( '0xdddddddd62617365617070070080218021802180218021802180218021', ) // @log: { codes: ['baseapp'], id: 0 } if (attribution?.codes) { // Resolve `attribution.codes` against the code registry. } ``` The `id` field reports the schema (`0` canonical registry, `1` custom registry, `2` CBOR-encoded), so consumers can resolve codes against the right registry. ## Best Practices ### Append the Suffix Last The ERC-8021 marker must be the final bytes of calldata. Apply the suffix after all other calldata construction and wrapping, or parsers will not find it. ### Pick the Smallest Schema That Fits Schema 0 with registered codes is the cheapest. Reach for schema 1 only when you operate a custom registry, and schema 2 only when you need optional fields or arbitrary metadata. ### Parse Defensively Any transaction can carry trailing bytes that look like data. `Attribution.fromData` validates the marker and structure and returns `undefined` otherwise — always handle that case. ## See More # Batch Calls with ERC-7821 ## Overview [ERC-7821](https://eips.ethereum.org/EIPS/eip-7821) is a minimal interface — `execute(bytes32 mode, bytes executionData)` — that smart accounts expose to execute a batch of calls atomically. The [`Calls`](/ercs/erc7821/Calls) module encodes and decodes the `executionData` payload, and [`Execute`](/ercs/erc7821/Execute) produces complete `execute` function data with the correct mode. ## Recipes ### Encode a Batch of Calls Encode an array of `{ to, value, data }` calls into ERC-7821 `executionData` with `Calls.encode`. ```ts twoslash import { Calls } from 'ox/erc7821' const executionData = Calls.encode([ { data: '0xdeadbeef', to: '0xcafebabecafebabecafebabecafebabecafebabe', value: 1n, }, { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 2n, }, ]) ``` `value` and `data` are optional and default to `0n` and `0x`. This is only the `executionData` argument — see the next recipe for complete `execute` calldata. ### Encode an `execute` Payload Produce the full calldata for the account's `execute` function with `Execute.encodeData`. The mode is selected automatically based on whether `opData` is present. ```ts twoslash import { Execute } from 'ox/erc7821' const data = Execute.encodeData([ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n, }, ]) // With account-defined authorization data (`opData` mode). const dataWithOpData = Execute.encodeData( [ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n, }, ], { opData: '0xdeadbeef' }, ) ``` Use the resulting data as a transaction's `data` field, or as the `callData` of an [ERC-4337 user operation](/guides/account-abstraction/user-operations). ### Decode Incoming Executions Recover the calls (and optional `opData`) from `execute` calldata with `Execute.decodeData` — for example inside a wallet, simulator, or indexer inspecting a batch. ```ts twoslash import { Hex } from 'ox' import { Execute } from 'ox/erc7821' // Calldata for `execute(bytes32,bytes)` (eg. from a transaction request). declare const data: Hex.Hex const { calls, opData } = Execute.decodeData(data) // [!code hl] // @log: { // @log: calls: [ // @log: { // @log: data: '0xcafebabe', // @log: to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', // @log: value: 1n, // @log: }, // @log: ], // @log: } ``` For the "batch of batches" mode, use `Execute.decodeBatchOfBatchesData` (and `Execute.encodeBatchOfBatchesData` to produce it). ## Best Practices ### Batches Are Ordered and Atomic ERC-7821 accounts execute calls in order and revert the whole batch if any call fails. Sequence dependent calls (approve → swap) accordingly, and do not assume partial execution. ### Treat `opData` as Account-Defined The meaning of `opData` (signatures, paymaster data, nonces) is defined by the account implementation, not the standard. Encode exactly what the target account expects. ## See More # Build ERC-4337 User Operations :::info This guide is intended to be low-level. If you are looking for a high-level bundler and smart account client, check out [Viem's Account Abstraction guides](https://viem.sh/account-abstraction). ::: ## Overview [ERC-4337](https://eips.ethereum.org/EIPS/eip-4337) routes smart account transactions as "user operations" through bundlers and the EntryPoint contract. The [`UserOperation`](/ercs/erc4337/UserOperation) module constructs, hashes, signs, and packs user operations, [`EntryPoint`](/ercs/erc4337/EntryPoint) provides the ABIs and addresses for EntryPoint versions `0.6`–`0.9`, and [`UserOperationGas`](/ercs/erc4337/UserOperationGas) and [`UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt) convert bundler RPC responses. ## Recipes ### Construct a User Operation Instantiate a user operation with `UserOperation.from`. The `callData` is what the smart account executes — typically an encoded [ERC-7821 batch](/guides/account-abstraction/erc-7821) or another account-specific `execute` call. ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.from({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n, }) ``` For a counterfactual (not yet deployed) account on EntryPoint `0.7`+, also set `factory` and `factoryData` so the EntryPoint can deploy the account on first use. ### Compute the Hash & Sign Payload Compute the signing payload for a target chain and EntryPoint with `UserOperation.getSignPayload`, sign it, and attach the signature. ```ts twoslash import { Secp256k1, Value } from 'ox' import { EntryPoint, UserOperation } from 'ox/erc4337' const userOperation = UserOperation.from({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n, }) const payload = UserOperation.getSignPayload(userOperation, { chainId: 1, entryPointAddress: EntryPoint.addressV07, // [!code hl] entryPointVersion: '0.7', // [!code hl] }) const signature = Secp256k1.sign({ payload, privateKey: '0x...', }) const userOperation_signed = UserOperation.from(userOperation, { signature }) ``` The same value is the "user operation hash" bundlers and explorers use — compute it directly with `UserOperation.hash`. On EntryPoint `0.8`+ the payload is EIP-712 based; use `UserOperation.toTypedData` when signing with a wallet's `eth_signTypedData_v4`. ### Pack for the EntryPoint EntryPoint `0.7`+ consumes the packed representation onchain. Convert a signed user operation with `UserOperation.toPacked`, and unpack contract data with `UserOperation.fromPacked`. ```ts twoslash import { UserOperation } from 'ox/erc4337' declare const userOperation: UserOperation.UserOperation<'0.7', true> const packed = UserOperation.toPacked(userOperation) // [!code hl] // Recover the structured form from a packed user operation. const unpacked = UserOperation.fromPacked(packed) ``` `UserOperation.toInitCode` computes the packed `initCode` field on its own, including the special EIP-7702 `0x7702` factory convention. ### Convert to & from RPC Bundler JSON-RPC methods exchange hex-quantity payloads. Convert with `UserOperation.toRpc` and submit via a [`RpcTransport`](/api/RpcTransport) typed with the [`RpcSchema.Bundler`](/ercs/erc4337/RpcSchema) schema. ```ts twoslash import { RpcSchema, RpcTransport } from 'ox' import { EntryPoint, RpcSchema as RpcSchema_erc4337, UserOperation, UserOperationReceipt, } from 'ox/erc4337' declare const userOperation: UserOperation.UserOperation<'0.7', true> const transport = RpcTransport.fromHttp('https://bundler.example.com', { schema: RpcSchema.from>(), }) const userOpHash = await transport.request({ method: 'eth_sendUserOperation', params: [UserOperation.toRpc(userOperation), EntryPoint.addressV07], // [!code hl] }) const receipt = await transport.request({ method: 'eth_getUserOperationReceipt', params: [userOpHash], }) if (receipt) { const { success, actualGasUsed } = UserOperationReceipt.fromRpc(receipt) } ``` Convert incoming payloads the other way with `UserOperation.fromRpc`, and gas estimates from `eth_estimateUserOperationGas` with `UserOperationGas.fromRpc`. ## Best Practices ### Pin the EntryPoint Version The hash — and therefore the signature — commits to the EntryPoint address, version, and chain ID. Use the `EntryPoint.addressV06`–`addressV09` constants and keep the `entryPointVersion` consistent across hashing, signing, and submission. ### Sign After Gas Values Are Final All gas limits and fee fields are part of the signed hash. Estimate with `eth_estimateUserOperationGas` first; changing any field after signing invalidates the signature. ### Prefer Typed Data on EntryPoint 0.8+ `UserOperation.toTypedData` produces an EIP-712 definition, so wallet users see a structured, human-readable signing prompt instead of an opaque hash. ## See More # Accounts & Keys ## Overview An Ethereum account is a secp256k1 key pair and the address derived from it. Ox provides the primitives for the whole key lifecycle: generating [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonics, deriving [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) HD keys, computing and validating addresses, and encrypting private keys at rest as JSON keystores. ```ts twoslash import { Address, Mnemonic, Secp256k1 } from 'ox' // Generate a mnemonic and derive a private key (`m/44'/60'/0'/0/0`). const mnemonic = Mnemonic.random(Mnemonic.english) const privateKey = Mnemonic.toPrivateKey(mnemonic) // Derive the public key and address. const publicKey = Secp256k1.getPublicKey({ privateKey }) const address = Address.fromPublicKey(publicKey) ``` ## Guides # Derive & Validate Addresses ## Overview An Ethereum address is the last 20 bytes of the keccak256 hash of an uncompressed secp256k1 public key. The [`Address`](/api/Address) module derives, checksums, validates, and compares addresses; the [`PublicKey`](/api/PublicKey) module handles the public keys they are derived from. ## Recipes ### Derive an Address from a Private Key Extract the public key with [`Secp256k1.getPublicKey`](/api/Secp256k1/getPublicKey), then convert it to an address with [`Address.fromPublicKey`](/api/Address/fromPublicKey). ```ts twoslash import { Address, Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const publicKey = Secp256k1.getPublicKey({ privateKey }) const address = Address.fromPublicKey(publicKey) ``` ### Derive an Address from a Public Key When you already have a serialized public key — for example one recovered from a signature — instantiate it with [`PublicKey.from`](/api/PublicKey/from) before deriving the address. ```ts twoslash import { Address, PublicKey } from 'ox' const publicKey = PublicKey.from( '0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', ) const address = Address.fromPublicKey(publicKey) // @log: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' ``` Pass `{ checksum: true }` to return the [ERC-55](https://eips.ethereum.org/EIPS/eip-55) checksummed form instead of lowercase. ### Checksum & Validate an Address Compute the ERC-55 mixed-case form with [`Address.checksum`](/api/Address/checksum), and check untrusted input with [`Address.validate`](/api/Address/validate) (returns `false`) or [`Address.assert`](/api/Address/assert) (throws). ```ts twoslash import { Address } from 'ox' const checksummed = Address.checksum( '0xa0cf798816d4b9b9866b5330eea46a18382f251e', ) // @log: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' const valid = Address.validate('0xA0Cf798816D4b9b9866b5330EEa46a18382f251e') // @log: true const invalid = Address.validate('0xdeadbeef') // @log: false ``` By default, mixed-case input is also verified against its checksum (`strict: true`). Pass `{ strict: false }` to accept any correctly-shaped 20-byte hex string. Use [`Address.from`](/api/Address/from) to convert a validated string into a typed `Address`. ### Compare Addresses Use [`Address.isEqual`](/api/Address/isEqual) to compare addresses regardless of casing. ```ts twoslash import { Address } from 'ox' const equal = Address.isEqual( '0xa0cf798816d4b9b9866b5330eea46a18382f251e', '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', ) // @log: true ``` ## Best Practices ### Compare with Address.isEqual, Not === The same address can arrive lowercase, uppercase, or checksummed. Strict string equality produces false negatives; `Address.isEqual` does not. ### Validate at the Boundary Run `Address.validate` (or `Address.assert`) on user input, RPC responses, and decoded calldata before storing or acting on an address — checksum verification catches most transcription errors. ### Checksum for Display Store and compare addresses in lowercase, but render the `Address.checksum` form in UIs so users (and their wallets) can spot corruption. ## See More # Mnemonics & HD Wallets ## Overview A [BIP-39 mnemonic phrase](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) is a list of words that is a representation of a seed, which can be used to derive the keys of a [BIP-32 Hierarchical Deterministic (HD) Wallet](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki). We can combine a mnemonic phrase (or seed) with an Ethereum-specific derivation path (e.g. `m/44'/60'/0'/0/0`) to derive a private key and its associated Ethereum address. The [`Mnemonic`](/api/Mnemonic) module handles phrases; the [`HdKey`](/api/HdKey) module handles the derived key tree. ## Recipes ### Generate a Random Mnemonic We can generate a random mnemonic phrase using [`Mnemonic.random`](/api/Mnemonic/random). ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) // @log: 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' ``` Ox supports the following languages: | Language | Export | | ------------------- | ----------------------------- | | English | `Mnemonic.english` | | Czech | `Mnemonic.czech` | | French | `Mnemonic.french` | | Italian | `Mnemonic.italian` | | Japanese | `Mnemonic.japanese` | | Korean | `Mnemonic.korean` | | Portuguese | `Mnemonic.portuguese` | | Simplified Chinese | `Mnemonic.simplifiedChinese` | | Spanish | `Mnemonic.spanish` | | Traditional Chinese | `Mnemonic.traditionalChinese` | ### Derive a Private Key at a Path Derive a private key from a mnemonic phrase using [`Mnemonic.toPrivateKey`](/api/Mnemonic/toPrivateKey). This will use the default path of `m/44'/60'/0'/0/0`. ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const privateKey = Mnemonic.toPrivateKey(mnemonic) ``` We can also specify a custom path using the [`Mnemonic.path`](/api/Mnemonic/path) function. ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const path = Mnemonic.path({ account: 1, index: 2 }) // `m/44'/60'/1'/0/2` const privateKey = Mnemonic.toPrivateKey(mnemonic, { path }) // or, we can pass the path as a string const privateKey_2 = Mnemonic.toPrivateKey(mnemonic, { path: "m/44'/60'/1'/0/2", }) ``` Mnemonic private keys are derived on the secp256k1 curve, so an address follows via [`Secp256k1.getPublicKey`](/api/Secp256k1/getPublicKey) and [`Address.fromPublicKey`](/api/Address/fromPublicKey) — see [Derive & Validate Addresses](/guides/accounts/addresses). ### Derive Many Accounts (HD Paths) Convert the mnemonic to an HD key once with [`Mnemonic.toHdKey`](/api/Mnemonic/toHdKey), then derive one account per address index. ```ts twoslash import { Address, Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const hdKey = Mnemonic.toHdKey(mnemonic) const addresses = [] for (let index = 0; index < 3; index++) { const account = hdKey.derive(Mnemonic.path({ index })) addresses.push(Address.fromPublicKey(account.publicKey)) } ``` Each `account` also exposes `privateKey`, `publicKey`, and extended keys — see the [`HdKey`](/api/HdKey) reference. ### Restore an HD Key from a Seed or Extended Key An HD key does not have to come from a mnemonic. Restore one from a master seed with [`HdKey.fromSeed`](/api/HdKey/fromSeed), or from a serialized extended private key (`xpriv`) with [`HdKey.fromExtendedKey`](/api/HdKey/fromExtendedKey). ```ts twoslash import { HdKey, Mnemonic } from 'ox' declare const xpriv: string // From a master seed. const seed = Mnemonic.toSeed( 'test test test test test test test test test test test junk', ) const hdKey = HdKey.fromSeed(seed) // From an extended private key. const hdKey_2 = HdKey.fromExtendedKey(xpriv) const account = hdKey.derive(HdKey.path({ index: 0 })) ``` ## Best Practices ### Store the Phrase, Not the Keys Every account is re-derivable from the mnemonic, so the phrase is the single secret worth protecting. Never persist it in plaintext — encrypt derived keys at rest with a [JSON keystore](/guides/accounts/keystores). ### Validate Before Deriving User-entered phrases should be checked with [`Mnemonic.validate`](/api/Mnemonic/validate) against the expected wordlist before derivation — a typo otherwise silently derives a different (empty) wallet. ### Use Hardened Account Paths Stick to the BIP-44 layout that `Mnemonic.path`/`HdKey.path` produce (`m/44'/60'/account'/0/index`). Non-hardened variations of the same numbers derive entirely different keys and break interoperability with other wallets. ## See More # Work with Keystores ## Overview A [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) (Web3 Secret Storage, version 3) stores a private key encrypted under a password-derived key — the format used by Geth and most wallet export flows. The [`Keystore`](/api/Keystore) module derives keys with scrypt or PBKDF2 and encrypts/decrypts private keys against them. ## Recipes ### Encrypt a Private Key Derive an encryption key from a password with [`Keystore.pbkdf2`](/api/Keystore/pbkdf2) (or [`Keystore.scrypt`](/api/Keystore/scrypt)), then encrypt the private key with [`Keystore.encrypt`](/api/Keystore/encrypt). ```ts twoslash import { Keystore, Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() // Derive an encryption key from a password. const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' }) // Encrypt the private key. const keystore = Keystore.encrypt(privateKey, key, opts) // @log: { // @log: "crypto": { // @log: "cipher": "aes-128-ctr", // @log: "ciphertext": "...", // @log: "kdf": "pbkdf2", // @log: ... // @log: }, // @log: "id": "...", // @log: "version": 3, // @log: } ``` The resulting object is plain JSON — persist it with `JSON.stringify` and it will interoperate with Geth, ethers, and other keystore-aware tooling. ### Decrypt a Keystore Re-derive the decryption key from the keystore's stored KDF parameters with [`Keystore.toKey`](/api/Keystore/toKey), then recover the private key with [`Keystore.decrypt`](/api/Keystore/decrypt). ```ts twoslash import { Keystore } from 'ox' declare const keystore: Keystore.Keystore // Derive the decryption key from the keystore & password. const key = Keystore.toKey(keystore, { password: 'testpassword' }) // Decrypt the private key. const privateKey = Keystore.decrypt(keystore, key) // @log: '0x...' ``` `toKey` reads which KDF (and which parameters) the keystore was encrypted with, so the same code handles both scrypt and PBKDF2 keystores. ### Choose scrypt vs PBKDF2 scrypt is memory-hard, making large-scale GPU/ASIC cracking expensive — it is the default in Geth exports and the better choice for new keystores. PBKDF2 is cheaper to compute (for attackers too) but universally supported. Both have async variants that keep the main thread responsive. ```ts twoslash import { Keystore, Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const [key, opts] = await Keystore.scryptAsync({ password: 'testpassword' }) const keystore = Keystore.encrypt(privateKey, key, opts) ``` See [`Keystore.scryptAsync`](/api/Keystore/scryptAsync) and [`Keystore.pbkdf2Async`](/api/Keystore/pbkdf2Async) for tuning parameters (cost factor `n`, iteration count). ## Best Practices ### Prefer the Async KDF Variants Key derivation is deliberately slow. In browsers, servers, and anything interactive, use `scryptAsync`/`pbkdf2Async`/[`toKeyAsync`](/api/Keystore/toKeyAsync) so derivation does not block the event loop. ### The Keystore Is Only as Strong as the Password The KDF slows brute force; it does not fix a weak password. Keep the default work factors (218 scrypt cost / 262,144 PBKDF2 iterations) or raise them — never lower them for convenience. ### Treat Keystore JSON as Sensitive A keystore file is an offline-crackable target. Store it with the same care as any secret, and zero out decrypted private keys as soon as they have served their purpose. ## See More # Chain Data & State ## Overview Everything a node returns — blocks, receipts, logs, proofs — arrives as hex-encoded RPC objects. Ox's chain-data modules ([`Block`](/api/Block), [`TransactionReceipt`](/api/TransactionReceipt), [`Log`](/api/Log), [`Filter`](/api/Filter), [`AccountProof`](/api/AccountProof), and friends) convert between that wire format and typed JavaScript objects with a consistent `fromRpc`/`toRpc` pattern. Alongside them sit [`Ens`](/api/Ens) for name hashing, [`Bloom`](/api/Bloom) for log pre-checks, and [`StateOverrides`](/api/StateOverrides) / [`BlockOverrides`](/api/BlockOverrides) for simulation. ```ts twoslash import { Block, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const block = await transport .request({ method: 'eth_getBlockByNumber', params: ['latest', false] }) .then(Block.fromRpc) // [!code hl] // @log: { number: 19868020n, timestamp: 1662222222n, ... } ``` # Query Logs, Filters & Bloom ## Overview [`Filter`](/api/Filter) expresses `eth_getLogs` queries in typed form, [`Log`](/api/Log) converts the results, and [`Bloom`](/api/Bloom) checks a block's `logsBloom` before you pay for a log query. Pair them with [`AbiEvent`](/api/AbiEvent) to build topics from Solidity event signatures. ## Recipes ### Build Log Filters Encode an event into topics with [`AbiEvent.encode`](/api/AbiEvent/encode), then serialize the filter for the wire with [`Filter.toRpc`](/api/Filter/toRpc). ```ts twoslash import { AbiEvent, Filter, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)', ) const { topics } = AbiEvent.encode(transfer) const filter = Filter.toRpc({ address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: 19760235n, toBlock: 19760240n, topics, }) const logs = await transport.request({ method: 'eth_getLogs', params: [filter], }) ``` The same RPC filter object works with `eth_newFilter` + `eth_getFilterChanges` for polling-based subscriptions. ### Convert RPC Logs [`Log.fromRpc`](/api/Log/fromRpc) converts hex block numbers and indices into `bigint` and `number` values. ```ts twoslash import { Log, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const logs = await transport.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: '0x12d846b', toBlock: '0x12d8470', }, ], }) const converted = logs.map((log) => Log.fromRpc(log)) // @log: [{ // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: blockNumber: 19760236n, // @log: logIndex: 271, // @log: ... // @log: }] ``` To decode a log's `topics` and `data` into named event arguments, use [`AbiEvent.decode`](/api/AbiEvent/decode) — see [Work with Events & Logs](/guides/abi/events). ### Pre-Check Membership with Bloom Filters Every block header carries a 256-byte `logsBloom`. [`Bloom.contains`](/api/Bloom/contains) tests whether an address or topic *may* appear in the block's logs, letting you skip `eth_getLogs` calls for blocks that definitely contain nothing of interest. ```ts twoslash import { Block, Bloom, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const block = await transport .request({ method: 'eth_getBlockByNumber', params: ['latest', false] }) .then(Block.fromRpc) const mayContain = Bloom.contains( block!.logsBloom, '0xef2d6d194084c2de36e0dabfce45d046b37d1106', ) // @log: true ``` For repeated checks against the same bloom, use [`Bloom.prepare`](/api/Bloom/prepare) with [`Bloom.containsPrepared`](/api/Bloom/containsPrepared) to skip the per-call conversion. ## Best Practices ### Treat Bloom Hits as Maybes Bloom filters are probabilistic: a negative result is definitive, but a positive result may be a false positive. Always confirm a hit by fetching and decoding the actual logs. ### Scope Filters Tightly Providers cap `eth_getLogs` ranges and result sizes. Constrain `address`, `topics`, and the block range, and paginate long backfills in fixed-size windows. ## See More # Resolve ENS Names ## Overview [`Ens`](/api/Ens) provides the local primitives of ENS resolution: normalizing names, hashing them into registry keys, and computing multichain coin types. The onchain half — querying the registry and resolver contracts — needs a transport or client; for a batteries-included flow, use [Viem's ENS actions](https://viem.sh/docs/ens/actions/getEnsAddress). ## Recipes ### Normalize a Name User input must be normalized per [ENSIP-15](https://docs.ens.domains/ensip/15) before hashing or display. [`Ens.normalize`](/api/Ens/normalize) applies UTS-46 normalization and throws on disallowed characters. ```ts twoslash import { Ens } from 'ox' const name = Ens.normalize('WEVM.eth') // @log: 'wevm.eth' ``` ### Compute Namehash & Labelhash [`Ens.namehash`](/api/Ens/namehash) hashes a full name into the node used as the key of the ENS registry; [`Ens.labelhash`](/api/Ens/labelhash) hashes a single label (e.g. for registrar token IDs). ```ts twoslash import { Ens } from 'ox' const node = Ens.namehash('wevm.eth') // @log: '0x08c85f2f4059e930c45a6aeff9dcd3bd95dc3c5c1cddef6a0626b31152248560' const label = Ens.labelhash('wevm') // @log: '0xcca19c3b64f2cbc38b510a15f4c577cb455225ed774a1e100cd539af6d3f2eb7' ``` Pass the `namehash` output to registry and resolver calls such as `resolver(bytes32)` and `addr(bytes32)`. ### Coin Types for Multichain Addresses Multichain resolvers key addresses by [ENSIP-11](https://docs.ens.domains/ensip/11) coin type. [`Ens.toCoinType`](/api/Ens/toCoinType) converts an EVM chain ID into its coin type. ```ts twoslash import { Ens } from 'ox' const coinType = Ens.toCoinType(10n) // @log: 2147483658n const mainnet = Ens.toCoinType(1n) // @log: 60n ``` Mainnet maps to the SLIP-44 Ether coin type (`60`); other EVM chains set the most significant bit over their chain ID. ## Best Practices ### Normalize Before Hashing `namehash` and `labelhash` operate on raw strings — hashing an unnormalized name produces a different node than wallets and resolvers expect. Run every user-supplied name through `Ens.normalize` first. ### Never Trust a Name Without Forward Resolution When displaying a reverse-resolved name, resolve it forward again and check that it maps back to the original address before treating the name as verified. ## See More # Simulate with State Overrides ## Overview `eth_call` and `eth_simulateV1` accept override sets that ephemerally patch chain state for the duration of a call. [`StateOverrides`](/api/StateOverrides) types per-account overrides (balance, nonce, code, storage) and [`BlockOverrides`](/api/BlockOverrides) types the block context (number, timestamp, fees); both convert to the wire format with `toRpc`. ## Recipes ### Override Balances & Code for `eth_call` Give the caller a funded balance or swap a contract's bytecode with [`StateOverrides.toRpc`](/api/StateOverrides/toRpc), then pass the set as the third parameter of `eth_call`. ```ts twoslash import { RpcTransport, StateOverrides } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const stateOverrides = StateOverrides.toRpc({ // Fund the caller with 1 ETH. '0xd2135CfB216b74109775236E36d4b433F1DF507B': { balance: 1_000_000_000_000_000_000n, // [!code hl] }, // Replace the callee's bytecode with a mock. '0x0D44f617435088c947F00B31160f64b074e412B4': { code: '0x6042805f5260205ff3', // [!code hl] }, }) const result = await transport.request({ method: 'eth_call', params: [ { from: '0xd2135CfB216b74109775236E36d4b433F1DF507B', to: '0x0D44f617435088c947F00B31160f64b074e412B4', data: '0xdeadbeef', }, 'latest', stateOverrides, // [!code hl] ], }) ``` Storage can be overridden too: `state` replaces the account's entire storage, while `stateDiff` patches individual slots. ### Override Block Context Simulate a call as if it executed in a different block — a future timestamp, another block number, or a custom base fee — with [`BlockOverrides.toRpc`](/api/BlockOverrides/toRpc) as the fourth `eth_call` parameter. ```ts twoslash import { BlockOverrides, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const blockOverrides = BlockOverrides.toRpc({ number: 19868021n, // [!code hl] time: 1735689600n, // [!code hl] }) const result = await transport.request({ method: 'eth_call', params: [ { to: '0x0D44f617435088c947F00B31160f64b074e412B4', data: '0xdeadbeef', }, 'latest', {}, blockOverrides, // [!code hl] ], }) ``` `eth_simulateV1` accepts the same `stateOverrides` and `blockOverrides` objects per simulated block in its `blockStateCalls`. ## Best Practices ### Prefer `stateDiff` over `state` `state` wipes every storage slot the override does not mention, which silently breaks contracts that read untouched slots. Use `stateDiff` unless you intend to replace the whole storage layout. ### Build Overrides in Typed Form Keep balances and nonces as `bigint` throughout your code and convert once at the boundary with `toRpc` — hand-built hex quantities are a common source of off-by-encoding bugs. ## See More # Verify State & Account Proofs ## Overview [`AccountProof`](/api/AccountProof) types the Merkle proofs returned by `eth_getProof`, covering an account's balance, nonce, code hash, and storage slots. [`BinaryStateTree`](/api/BinaryStateTree) implements the [EIP-7864](https://eips.ethereum.org/EIPS/eip-7864) binary state tree, the proposed successor to Ethereum's hexary state trie. ## Recipes ### Fetch & Convert `eth_getProof` Results Request a proof for an account and a set of storage keys, then decode it with [`AccountProof.fromRpc`](/api/AccountProof/fromRpc). ```ts twoslash import { AccountProof, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const proof = await transport .request({ method: 'eth_getProof', params: [ '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', ['0x0000000000000000000000000000000000000000000000000000000000000000'], 'latest', ], }) .then(AccountProof.fromRpc) // @log: { // @log: address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', // @log: balance: 1n, // @log: nonce: 2, // @log: storageHash: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // @log: accountProof: [...], // @log: storageProof: [{ key: '0x00...00', proof: [...], value: 3n }], // @log: } ``` The `accountProof` and `storageProof` arrays contain the RLP-encoded trie nodes linking the account (and each storage slot) to the block's `stateRoot`. ### Work with Binary State Trees (EIP-7864) Build a binary state tree from key-value pairs and compute its Merkle root with [`BinaryStateTree.merkelize`](/api/BinaryStateTree/merkelize). ```ts twoslash import { BinaryStateTree, Bytes } from 'ox' const tree = BinaryStateTree.create() BinaryStateTree.insert( tree, Bytes.fromHex( '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54', ), Bytes.fromHex( '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1', ), ) const root = BinaryStateTree.merkelize(tree) ``` Keys are split into a 31-byte stem and a 1-byte sub-index, mirroring the EIP-7864 layout. ## Best Practices ### Verify Against a Trusted Root A proof is only as good as the root it is checked against. Compare proofs to the `stateRoot` of a block header you have independently verified (or received from a trusted light-client protocol), not to a value returned by the same untrusted node. ### Pin the Block Fetch proofs with an explicit block number or hash rather than `latest`, so the proof and the header you verify against cannot drift across a reorg between requests. ## See More # Work with Blocks & Receipts ## Overview [`Block`](/api/Block), [`Withdrawal`](/api/Withdrawal), and [`TransactionReceipt`](/api/TransactionReceipt) convert between the hex-quantity RPC representation and typed objects with `bigint` quantities. Each module offers `fromRpc` for decoding node responses and `toRpc` for serializing back to the wire format. ## Recipes ### Convert an RPC Block to a Typed Object Fetch a block with `eth_getBlockByNumber` and pass the result to [`Block.fromRpc`](/api/Block/fromRpc). ```ts twoslash import { Block, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const block = await transport .request({ method: 'eth_getBlockByNumber', params: ['latest', false] }) .then(Block.fromRpc) // @log: { // @log: hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', // @log: number: 19868020n, // @log: size: 520n, // @log: timestamp: 1662222222n, // @log: ... // @log: } ``` `Block.fromRpc` returns `null` when the block is not found, and converts embedded transactions when the block was fetched with full transaction objects. ### Read Consensus Withdrawals Post-Shanghai blocks embed validator withdrawals. `Block.fromRpc` converts the embedded `withdrawals` list automatically; use [`Withdrawal.fromRpc`](/api/Withdrawal/fromRpc) for standalone RPC values. ```ts twoslash import { Withdrawal } from 'ox' const withdrawal = Withdrawal.fromRpc({ address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', amount: '0x620323', index: '0x0', validatorIndex: '0x1', }) // @log: { // @log: address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', // @log: amount: 6423331n, // @log: index: 0, // @log: validatorIndex: 1 // @log: } ``` The `amount` is denominated in Gwei, as defined by the consensus specification. ### Convert Receipts Fetch a receipt with `eth_getTransactionReceipt` and decode it with [`TransactionReceipt.fromRpc`](/api/TransactionReceipt/fromRpc), which also converts the receipt's embedded logs and maps `status` and `type` to their named variants. ```ts twoslash import { RpcTransport, TransactionReceipt } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const receipt = await transport .request({ method: 'eth_getTransactionReceipt', params: [ '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', ], }) .then(TransactionReceipt.fromRpc) // @log: { // @log: blockNumber: 19868015n, // @log: gasUsed: 175034n, // @log: status: 'success', // @log: type: 'eip1559', // @log: ... // @log: } ``` ## Best Practices ### Handle the `null` Case `eth_getBlockByNumber` and `eth_getTransactionReceipt` return `null` for unknown hashes and not-yet-mined transactions — the `fromRpc` functions preserve that `null` in their return type, so narrow before use. ### Round-Trip with `toRpc` When persisting or forwarding chain data, serialize typed objects back to the wire format with `Block.toRpc`, `Withdrawal.toRpc`, and `TransactionReceipt.toRpc` instead of hand-rolling hex conversions. ## See More # Cryptography ## Overview The signing curves, hashes, and ciphers Ethereum (and its ecosystem) runs on — audited implementations, tree-shakable. Modules such as [`Secp256k1`](/api/Secp256k1), [`P256`](/api/P256), and [`Hash`](/api/Hash) expose stateless functions that accept and return `Hex` or `Bytes`. Heavy algorithms (BLS12-381, ML-DSA, some hashes) can be backed by pluggable [engines](/guides/runtime/engines) such as WASM or native Node.js crypto. ```ts twoslash import { Hash, Hex, Secp256k1 } from 'ox' declare const privateKey: Hex.Hex const payload = Hash.keccak256(Hex.fromString('agree to terms')) // @log: '0x6f7a…b21c' const signature = Secp256k1.sign({ payload, privateKey }) // @log: { r: '0x1c34…', s: '0x4f8d…', yParity: 0 } const signer = Secp256k1.recoverAddress({ payload, signature }) // @log: '0x71bE63f3384f5fb98995898A86B02Fb2426c5788' ``` # BLS Signatures & Aggregation ## Overview [`Bls`](/api/Bls) implements BLS12-381 signatures — the scheme Ethereum consensus validators use — with points represented as structured [`BlsPoint`](/api/BlsPoint) values. By default, public keys are short G1 points (48 bytes) and signatures are long G2 points (96 bytes); pass `size: 'long-key:short-sig'` to flip that trade-off. Pairing operations are heavy, so the default implementation can be swapped for a faster backend via [engines](/guides/runtime/engines). ## Recipes ### Sign and Verify a Payload [`Bls.createKeyPair`](/api/Bls/createKeyPair) generates a key pair, and [`Bls.sign`](/api/Bls/sign) / [`Bls.verify`](/api/Bls/verify) sign and check payloads. ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const { privateKey, publicKey } = Bls.createKeyPair() const signature = Bls.sign({ payload, privateKey }) // [!code hl] const verified = Bls.verify({ payload, publicKey, signature }) // @log: true ``` `verify` infers the point groups from the inputs, so it works unchanged with either key size. ### Aggregate Signatures and Public Keys [`Bls.aggregate`](/api/Bls/aggregate) combines many signatures over the same payload into one, and many public keys into one — a single pairing check then verifies the whole set. ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const privateKeys = Array.from({ length: 100 }, () => Bls.randomPrivateKey()) const publicKeys = privateKeys.map((privateKey) => Bls.getPublicKey({ privateKey }), ) const signatures = privateKeys.map((privateKey) => Bls.sign({ payload, privateKey }), ) const publicKey = Bls.aggregate(publicKeys) // [!code hl] const signature = Bls.aggregate(signatures) // [!code hl] const verified = Bls.verify({ payload, publicKey, signature }) // @log: true ``` All points in one call must come from the same group. When aggregating serialized (hex or bytes) points, pass `{ group: 'G1' }` or `{ group: 'G2' }` so they can be deserialized. ### Serialize BLS Points Structured points serialize to compressed hex or bytes with [`BlsPoint.toHex`](/api/BlsPoint/toHex) and [`BlsPoint.toBytes`](/api/BlsPoint/toBytes), and deserialize with [`BlsPoint.fromHex`](/api/BlsPoint/fromHex) and [`BlsPoint.fromBytes`](/api/BlsPoint/fromBytes). ```ts twoslash import { Bls, BlsPoint, Hex } from 'ox' const payload = Hex.random(32) const { privateKey, publicKey } = Bls.createKeyPair() const signature = Bls.sign({ payload, privateKey }) const publicKeyHex = BlsPoint.toHex(publicKey) // [!code hl] // @log: '0xacafff52…b32d9e66' (48 bytes) const signatureHex = BlsPoint.toHex(signature) // @log: '0xb4698f76…4ebe427c' (96 bytes) const publicKey2 = BlsPoint.fromHex(publicKeyHex, 'G1') const signature2 = BlsPoint.fromHex(signatureHex, 'G2') ``` Deserialization needs the group name because 48-byte values are G1 and 96-byte values are G2. ## Best Practices ### Aggregate Over a Single Payload A plain aggregate verification assumes every signer signed the same payload. Mixing payloads without a proof-of-possession scheme opens rogue-key attacks — stick to one payload per aggregate unless you know why you are deviating. ### Install a Faster Engine for Bulk Verification Pairing checks dominate BLS cost. For validator-scale workloads, install the WASM or Node [engine](/guides/runtime/engines) instead of the pure-JS default. ## See More # Convert Signature Formats ## Overview [`Signature`](/api/Signature) is the structured ECDSA form every Ox signer returns: an object with `r`, `s`, and an optional recovery bit `yParity`. The module converts between that shape and every serialized form you will meet in the wild — 64/65-byte hex, DER, compact bytes, legacy `v` values, JSON-RPC objects, and RLP tuples. ## Recipes ### Instantiate a Signature [`Signature.from`](/api/Signature/from) accepts a structured object — or any serialized form, which it parses automatically. ```ts twoslash import { Signature } from 'ox' const signature = Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 0, }) const parsed = Signature.from( '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81b', ) // @log: { r: '0x6e100a35…', s: '0x4a90a229…', yParity: 0 } ``` ### Convert Between Hex, Bytes, and Objects [`Signature.toHex`](/api/Signature/toHex) serializes a signature to its 65-byte form, and [`Signature.fromHex`](/api/Signature/fromHex) parses it back. ```ts twoslash import { Signature } from 'ox' const signature = Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 0, }) const hex = Signature.toHex(signature) // [!code hl] // @log: '0x6e100a35…6db81b' const restored = Signature.fromHex(hex) // @log: { r: '0x6e100a35…', s: '0x4a90a229…', yParity: 0 } ``` [`Signature.toBytes`](/api/Signature/toBytes) and [`Signature.fromBytes`](/api/Signature/fromBytes) do the same for `Uint8Array` values, and [`Signature.from`](/api/Signature/from) accepts any of these forms directly. ### Convert DER and Compact Forms Hardware modules and Web Crypto emit DER; some verifiers expect a bare 64-byte `r ++ s` encoding. Use [`Signature.toDerHex`](/api/Signature/toDerHex) and [`Signature.toCompactBytes`](/api/Signature/toCompactBytes) to produce them. ```ts twoslash import { Signature } from 'ox' const signature = Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', }) const der = Signature.toDerHex(signature) // [!code hl] // @log: '0x304402206e100a35…6db8' const compact = Signature.toCompactBytes(signature) // @log: Uint8Array [110, 16, 10, …] (64 bytes) const fromDer = Signature.fromDerHex(der) ``` [`Signature.fromDerBytes`](/api/Signature/fromDerBytes) and [`Signature.fromCompactBytes`](/api/Signature/fromCompactBytes) parse the byte variants. DER and compact forms carry no recovery bit, so the results have no `yParity`. ### Convert Legacy v and yParity Pre-EIP-155 tooling encodes the recovery bit as `v` (27/28). [`Signature.fromLegacy`](/api/Signature/fromLegacy) and [`Signature.toLegacy`](/api/Signature/toLegacy) translate whole signatures; [`Signature.vToYParity`](/api/Signature/vToYParity) and [`Signature.yParityToV`](/api/Signature/yParityToV) translate just the bit. ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromLegacy({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', v: 28, // [!code hl] }) // @log: { r: '0x6e100a35…', s: '0x4a90a229…', yParity: 1 } const legacy = Signature.toLegacy(signature) // @log: { r: '0x6e100a35…', s: '0x4a90a229…', v: 28 } const yParity = Signature.vToYParity(28) // @log: 1 const v = Signature.yParityToV(1) // @log: 28 ``` ### Convert RPC and Tuple Formats JSON-RPC responses hex-encode `yParity`; transaction envelopes and EIP-7702 authorization lists serialize signatures as RLP tuples. [`Signature.fromRpc`](/api/Signature/fromRpc), [`Signature.toRpc`](/api/Signature/toRpc), [`Signature.fromTuple`](/api/Signature/fromTuple), and [`Signature.toTuple`](/api/Signature/toTuple) cover both. ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromRpc({ r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0', }) const rpc = Signature.toRpc(signature) // [!code hl] // @log: { r: '0x635dc203…', s: '0x50c2667a…', yParity: '0x0' } const tuple = Signature.toTuple(signature) // @log: [yParity: '0x', r: '0x635dc203…', s: '0x50c2667a…'] const restored = Signature.fromTuple(tuple) ``` `fromRpc` also accepts responses that carry a legacy `v` field instead of `yParity`. ## Best Practices ### Preserve yParity Recovery-based verification (and `eth_sendRawTransaction`) needs the recovery bit. Only drop to DER or compact forms when the verifier holds the public key. ### Validate Untrusted Input Run externally supplied signatures through [`Signature.validate`](/api/Signature/validate) (boolean) or [`Signature.assert`](/api/Signature/assert) (throws) before use — both check the `r`, `s`, and `yParity` ranges. ## See More # Ed25519 & X25519 ## Overview [`Ed25519`](/api/Ed25519) provides EdDSA signatures and [`X25519`](/api/X25519) provides Diffie-Hellman key agreement, both on Curve25519. They are the workhorses of off-chain infrastructure — session keys, encrypted channels, p2p identities — and complement the ECDSA curves Ethereum uses onchain. Keys, signatures, and secrets are plain `Hex` or `Bytes` values. ## Recipes ### Sign and Verify with Ed25519 [`Ed25519.createKeyPair`](/api/Ed25519/createKeyPair) generates a key pair; [`Ed25519.sign`](/api/Ed25519/sign) and [`Ed25519.verify`](/api/Ed25519/verify) sign and check payloads. ```ts twoslash import { Ed25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() const signature = Ed25519.sign({ payload: '0xdeadbeef', privateKey }) // [!code hl] const verified = Ed25519.verify({ payload: '0xdeadbeef', publicKey, signature, }) // @log: true ``` Ed25519 hashes internally, so payloads of any length can be signed directly — no keccak256 step required. ### Derive a Shared Secret with X25519 [`X25519.getSharedSecret`](/api/X25519/getSharedSecret) computes the same 32-byte secret on both sides of an exchange from one party's private key and the other's public key. ```ts twoslash import { X25519 } from 'ox' const alice = X25519.createKeyPair() const bob = X25519.createKeyPair() const sharedSecretAlice = X25519.getSharedSecret({ privateKey: alice.privateKey, // [!code hl] publicKey: bob.publicKey, // [!code hl] }) const sharedSecretBob = X25519.getSharedSecret({ privateKey: bob.privateKey, publicKey: alice.publicKey, }) // @log: sharedSecretAlice === sharedSecretBob ``` Feed the secret into a KDF before using it as an encryption key — see [Work with AES-GCM](/guides/crypto/encryption). ### Reuse an Ed25519 Key Pair for X25519 An existing Ed25519 identity can also perform key agreement. [`Ed25519.toX25519PrivateKey`](/api/Ed25519/toX25519PrivateKey) and [`Ed25519.toX25519PublicKey`](/api/Ed25519/toX25519PublicKey) convert signing keys to their Montgomery-curve equivalents. ```ts twoslash import { Ed25519, X25519 } from 'ox' const signer = Ed25519.createKeyPair() const peer = Ed25519.createKeyPair() const sharedSecret = X25519.getSharedSecret({ privateKey: Ed25519.toX25519PrivateKey({ privateKey: signer.privateKey }), // [!code hl] publicKey: Ed25519.toX25519PublicKey({ publicKey: peer.publicKey }), // [!code hl] }) ``` Both parties convert their own private key and the peer's public key, then derive the secret as usual. ## Best Practices ### Run Shared Secrets Through a KDF Raw X25519 output is a curve point coordinate, not a uniformly random key. Derive the actual symmetric key with HKDF or [`Hash.hmac256`](/api/Hash/hmac256) before encrypting. ### Separate Long-Lived Identities from Session Keys Key conversion is convenient, but a compromised session secret should not burn a signing identity. Prefer fresh X25519 pairs per session and sign them with the Ed25519 identity. ## See More # Hash Data ## Overview [`Hash`](/api/Hash) computes the digests Ethereum and its ecosystem rely on: keccak256 for addresses, selectors, and sign payloads, plus SHA-256, RIPEMD-160, HMAC-SHA256, and BLAKE3. Every function accepts `Hex` or `Bytes` and returns the same format it was given. The default implementation is the audited [noble-hashes](https://github.com/paulmillr/noble-hashes) library; a faster backend can be installed via [engines](/guides/runtime/engines). ## Recipes ### Hash with Keccak256 [`Hash.keccak256`](/api/Hash/keccak256) is Ethereum's canonical hash — use it for sign payloads, event topics, and anything consumed onchain. ```ts twoslash import { Hash, Hex } from 'ox' const hash = Hash.keccak256(Hex.fromString('hello world')) // [!code hl] // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0' ``` Pass `{ as: 'Bytes' }` to get a `Uint8Array` regardless of the input format. ### Hash with SHA-256 and RIPEMD-160 [`Hash.sha256`](/api/Hash/sha256) and [`Hash.ripemd160`](/api/Hash/ripemd160) cover the precompile-backed hashes (addresses `0x02` and `0x03`) and Bitcoin-style key hashing. ```ts twoslash import { Hash } from 'ox' const sha = Hash.sha256('0xdeadbeef') // @log: '0x5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953' const ripemd = Hash.ripemd160('0xdeadbeef') // @log: '0x226821c2f5423e11fe9af68bd285c249db2e4b5a' ``` ### Compute HMAC and BLAKE3 [`Hash.hmac256`](/api/Hash/hmac256) produces keyed HMAC-SHA256 digests — the standard tool for deriving keys from shared secrets. [`Hash.blake3`](/api/Hash/blake3) is a fast modern hash for content addressing and checksums. ```ts twoslash import { Hash, Hex } from 'ox' const mac = Hash.hmac256(Hex.fromString('secret-key'), '0xdeadbeef') // [!code hl] const digest = Hash.blake3('0xdeadbeef') // @log: '0x53147f3ce49ed4f60dfa5b9654c36ba6103c11f5737df3dabd4cbd296c4161bd' ``` ### Hash Incrementally For streamed or chunked data, the `create*` variants ([`Hash.createKeccak256`](/api/Hash/createKeccak256), [`Hash.createSha256`](/api/Hash/createSha256), [`Hash.createRipemd160`](/api/Hash/createRipemd160), [`Hash.createHmac256`](/api/Hash/createHmac256), [`Hash.createBlake3`](/api/Hash/createBlake3)) return a stateful hasher that absorbs any number of chunks. ```ts twoslash import { Hash } from 'ox' const hasher = Hash.createKeccak256() hasher.update('0xdead') hasher.update('0xbeef') const hash = hasher.digest() // [!code hl] // @log: '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ``` `digest` consumes the state — call `clone()` first to branch several digests from the same prefix. ## Best Practices ### Use keccak256 for Anything Onchain Addresses, function selectors, event topics, and EIP-191/EIP-712 sign payloads are all keccak256. Reach for SHA-256 only when a spec (WebAuthn, P256, precompiles) demands it. ### Validate Digest Inputs When accepting a "hash" from external input, [`Hash.validate`](/api/Hash/validate) checks that the value is well-formed 32-byte hex before you use it as a sign payload. ## See More # Post-Quantum Signatures (ML-DSA) ## Overview [`MlDsa44`](/api/MlDsa44) implements ML-DSA-44 (FIPS 204, the standardized Dilithium2) — a lattice-based signature scheme that stays secure against quantum attackers. The private key is a compact 32-byte seed; the public key is 1,312 bytes and signatures are 2,420 bytes. The default implementation can be swapped for a faster backend via [engines](/guides/runtime/engines). ## Recipes ### Generate Keys, Sign, and Verify [`MlDsa44.createKeyPair`](/api/MlDsa44/createKeyPair) generates the seed and expands its public key; [`MlDsa44.sign`](/api/MlDsa44/sign) and [`MlDsa44.verify`](/api/MlDsa44/verify) mirror the ECDSA signer APIs. ```ts twoslash import { MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() const signature = MlDsa44.sign({ payload: '0xdeadbeef', privateKey }) // [!code hl] const verified = MlDsa44.verify({ payload: '0xdeadbeef', publicKey, signature, }) // @log: true ``` The 32-byte seed is the canonical interchange form of an ML-DSA private key — back up the seed, and [`MlDsa44.getPublicKey`](/api/MlDsa44/getPublicKey) deterministically re-derives the public key. ### Domain-Separate with a Context String FIPS 204 supports a context string (up to 255 bytes) so signatures from one protocol cannot be replayed in another. Pass the same `context` to both `sign` and `verify`. ```ts twoslash import { Hex, MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() const context = Hex.fromString('my-app:login:v1') const signature = MlDsa44.sign({ context, // [!code hl] payload: '0xdeadbeef', privateKey, }) const verified = MlDsa44.verify({ context, // [!code hl] payload: '0xdeadbeef', publicKey, signature, }) // @log: true ``` Verification with a different (or missing) context returns `false`. ## Best Practices ### Enable Hedged Signing on Hardware Signing is deterministic by default. Pass `extraEntropy: true` to `sign` for the hedged FIPS 204 variant, which protects against fault attacks and randomness-reuse pitfalls at the cost of reproducibility. ### Budget for Signature Size At 2,420 bytes per signature and 1,312 bytes per public key, ML-DSA payloads are two orders of magnitude larger than ECDSA. Plan storage and calldata costs accordingly before committing to onchain verification. ## See More # Work with AES-GCM ## Overview [`AesGcm`](/api/AesGcm) wraps the Web Crypto API's [AES-GCM](https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams) cipher, which combines high-performance encryption with built-in message integrity. Keys are non-extractable `CryptoKey`s derived from a password with [`AesGcm.getKey`](/api/AesGcm/getKey) (PBKDF2) or from a WebAuthn PRF output with [`AesGcm.fromPrf`](/api/AesGcm/fromPrf). All functions are async. ## Recipes ### Derive a Key from a Password [`AesGcm.getKey`](/api/AesGcm/getKey) stretches a password into an AES-256-GCM key using PBKDF2 (900,000 iterations by default). ```ts twoslash import { AesGcm } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) // [!code hl] // @log: CryptoKey {} ``` A random salt is generated per call — pass explicit `salt` (and optionally `iterations`) [options](/api/AesGcm/getKey) and persist the salt, or a key derived later from the same password will not decrypt earlier data. ### Encrypt Data [`AesGcm.encrypt`](/api/AesGcm/encrypt) encrypts `Hex` or `Bytes` data with the derived key. ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const data = Hex.fromString('i am top secret') const encrypted = await AesGcm.encrypt(data, key) // [!code hl] // @log: '0x5e257b25bcf53d5431e54e5a68ca0138306d31bb6154f35a97bb8ea18111e7d82bcf619d3c76c4650688bc5310eed80b8fc86d1e3e' ``` A fresh random initialization vector is generated on every call and prepended to the ciphertext, so the output is self-contained. ### Decrypt Data [`AesGcm.decrypt`](/api/AesGcm/decrypt) reads the prepended IV and returns the original data. GCM authentication means tampered ciphertext throws instead of decrypting to garbage. ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const data = Hex.fromString('i am top secret') const encrypted = await AesGcm.encrypt(data, key) const decrypted = await AesGcm.decrypt(encrypted, key) // [!code hl] // @log: '0x6920616d20746f7020736563726574' ``` Convert back to a string with [`Hex.toString`](/api/Hex/toString). ## Best Practices ### Persist the Salt The default random salt makes each derived key unique. Store it (it is not secret) next to the ciphertext and pass it back to `getKey` when decrypting. ### Prefer Hardware-Derived Keys Over Passwords Where a passkey is available, [`AesGcm.fromPrf`](/api/AesGcm/fromPrf) derives the key from the authenticator's PRF output — no password to phish or brute-force. See [Derive Secrets with PRF](/guides/webauthn/prf). ## See More # Work with P256 ## Overview [`P256`](/api/P256) implements ECDSA and ECDH on the NIST P256 (secp256r1) curve — the curve used by passkeys, secure enclaves, and much of Ethereum account abstraction. Signatures and public keys use the same structured [`Signature`](/api/Signature) and [`PublicKey`](/api/PublicKey) shapes as `Secp256k1`, so the two signers are interchangeable at the type level. ## Recipes ### Create a Key Pair [`P256.createKeyPair`](/api/P256/createKeyPair) generates a random private key and its corresponding public key in one call. ```ts twoslash import { P256 } from 'ox' const { privateKey, publicKey } = P256.createKeyPair() // [!code hl] // @log: { // @log: privateKey: '0x…', // @log: publicKey: { prefix: 4, x: '0x…', y: '0x…' }, // @log: } ``` [`P256.randomPrivateKey`](/api/P256/randomPrivateKey) and [`P256.getPublicKey`](/api/P256/getPublicKey) perform the two steps separately. ### Sign a Payload [`P256.sign`](/api/P256/sign) signs the payload as-is and returns a structured signature with a recovery bit. ```ts twoslash import { Hex, P256 } from 'ox' declare const privateKey: Hex.Hex const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) // [!code hl] // @log: { r: '0x…', s: '0x…', yParity: 0 } ``` Pass `hash: true` to SHA-256-hash the payload before signing — matching what WebAuthn authenticators and Web Crypto verifiers expect. ### Verify a Signature [`P256.verify`](/api/P256/verify) checks a signature against the payload and the signer's public key. ```ts twoslash import { P256 } from 'ox' const { privateKey, publicKey } = P256.createKeyPair() const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) const verified = P256.verify({ payload: '0xdeadbeef', publicKey, signature }) // [!code hl] // @log: true ``` If signing used `hash: true`, pass `hash: true` here as well. ### Recover a Public Key [`P256.recoverPublicKey`](/api/P256/recoverPublicKey) recovers the signing public key from the payload and a signature that carries `yParity`. ```ts twoslash import { P256 } from 'ox' const { privateKey } = P256.createKeyPair() const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) const publicKey = P256.recoverPublicKey({ payload: '0xdeadbeef', signature, // [!code hl] }) // @log: { prefix: 4, x: '0x…', y: '0x…' } ``` Signatures from `WebCryptoP256` or WebAuthn have no `yParity`, so they cannot be recovered — verify those against a stored public key instead. ### Derive a Shared Secret (ECDH) [`P256.getSharedSecret`](/api/P256/getSharedSecret) computes an Elliptic Curve Diffie-Hellman secret between one party's private key and the other's public key. ```ts twoslash import { P256 } from 'ox' const alice = P256.createKeyPair() const bob = P256.createKeyPair() const sharedSecret = P256.getSharedSecret({ privateKey: alice.privateKey, // [!code hl] publicKey: bob.publicKey, // [!code hl] }) ``` Both sides derive the same secret. Run it through a KDF (for example [`Hash.hmac256`](/api/Hash/hmac256)) before using it as a symmetric key. ## Best Practices ### Match Hashing on Both Sides `sign` and `verify` must agree on `hash`. Signing a pre-hashed digest but verifying with `hash: true` (or vice versa) fails silently with `false`. ### Prefer Hardware-Backed Keys in Browsers Raw hex private keys live in JavaScript memory. In browser contexts, prefer [`WebCryptoP256`](/guides/crypto/webcrypto-p256) (non-extractable `CryptoKey`s) or [passkeys](/guides/webauthn/signing). ## See More # Work with Secp256k1 ## Overview [`Secp256k1`](/api/Secp256k1) implements ECDSA on the curve behind Ethereum accounts and transactions. Signing returns a structured [`Signature`](/api/Signature) object (`r`, `s`, `yParity`), and public keys are structured [`PublicKey`](/api/PublicKey) objects, so results plug directly into transaction envelopes and message signing. ## Recipes ### Create a Key Pair [`Secp256k1.createKeyPair`](/api/Secp256k1/createKeyPair) generates a random private key with its corresponding public key. Derive the Ethereum address with [`Address.fromPublicKey`](/api/Address/fromPublicKey). ```ts twoslash import { Address, Secp256k1 } from 'ox' const { privateKey, publicKey } = Secp256k1.createKeyPair() const address = Address.fromPublicKey(publicKey) // [!code hl] // @log: '0x71bE63f3384f5fb98995898A86B02Fb2426c5788' ``` [`Secp256k1.randomPrivateKey`](/api/Secp256k1/randomPrivateKey) and [`Secp256k1.getPublicKey`](/api/Secp256k1/getPublicKey) perform the two steps separately. ### Sign a Payload Hash the message first — [`Secp256k1.sign`](/api/Secp256k1/sign) signs the 32-byte payload it is given. ```ts twoslash import { Hash, Hex, Secp256k1 } from 'ox' declare const privateKey: Hex.Hex const payload = Hash.keccak256(Hex.fromString('agree to terms')) const signature = Secp256k1.sign({ payload, privateKey }) // [!code hl] // @log: { r: '0x1c34…', s: '0x4f8d…', yParity: 0 } ``` Serialize the result with [`Signature.toHex`](/api/Signature/toHex) — see [Convert Signature Formats](/guides/crypto/signatures). ### Verify a Signature [`Secp256k1.verify`](/api/Secp256k1/verify) accepts either the signer's address or their public key. ```ts twoslash import { Address, Secp256k1 } from 'ox' const { privateKey, publicKey } = Secp256k1.createKeyPair() const address = Address.fromPublicKey(publicKey) const payload = '0xdeadbeef' const signature = Secp256k1.sign({ payload, privateKey }) const verified = Secp256k1.verify({ address, payload, signature }) // [!code hl] // @log: true ``` Pass `publicKey` instead of `address` to verify against the key directly — required when the signature has no `yParity` to recover from. ### Recover the Signer [`Secp256k1.recoverAddress`](/api/Secp256k1/recoverAddress) and [`Secp256k1.recoverPublicKey`](/api/Secp256k1/recoverPublicKey) recover the signer from the payload and a signature that carries its recovery bit (`yParity`). ```ts twoslash import { Hex, Secp256k1 } from 'ox' declare const privateKey: Hex.Hex const payload = '0xdeadbeef' const signature = Secp256k1.sign({ payload, privateKey }) const address = Secp256k1.recoverAddress({ payload, signature }) // [!code hl] // @log: '0x71bE63f3384f5fb98995898A86B02Fb2426c5788' const publicKey = Secp256k1.recoverPublicKey({ payload, signature }) // @log: { prefix: 4, x: '0xd6c2…', y: '0x9a47…' } ``` ## Best Practices ### Hash Before Signing Sign keccak256 digests, never raw application data. For user-facing messages, prefer the EIP-191 flow in [Sign Personal Messages](/guides/messages/personal-messages), which prefixes and hashes for you. ### Preserve the Recovery Bit `yParity` is what makes address recovery possible. Serialize signatures with [`Signature.toHex`](/api/Signature/toHex) (65 bytes) rather than dropping to `r ++ s` when the verifier needs to identify the signer. ## See More # Work with WebCryptoP256 ## Overview [`WebCryptoP256`](/api/WebCryptoP256) wraps the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) for P256: private keys are non-extractable [`CryptoKey`](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey)s that the runtime never exposes to JavaScript, while public keys and signatures use Ox's structured [`PublicKey`](/api/PublicKey) and [`Signature`](/api/Signature) shapes. All functions are async. ## Recipes ### Create a Non-Extractable Key Pair [`WebCryptoP256.createKeyPair`](/api/WebCryptoP256/createKeyPair) generates an ECDSA signing key. The private key cannot be exported or serialized by default. ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey, publicKey } = await WebCryptoP256.createKeyPair() // [!code hl] // @log: { // @log: privateKey: CryptoKey {}, // @log: publicKey: { prefix: 4, x: '0x…', y: '0x…' }, // @log: } ``` Persist the `CryptoKey` in IndexedDB if the key must survive a page reload — only pass `extractable: true` if you genuinely need to export it. ### Sign and Verify in the Browser [`WebCryptoP256.sign`](/api/WebCryptoP256/sign) SHA-256-hashes the payload and signs it with the `CryptoKey`; [`WebCryptoP256.verify`](/api/WebCryptoP256/verify) checks the result against the structured public key. ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey, publicKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ payload: '0xdeadbeef', privateKey, // [!code hl] }) const verified = await WebCryptoP256.verify({ payload: '0xdeadbeef', publicKey, signature, }) // @log: true ``` Web Crypto signatures carry no `yParity`, so the signer cannot be recovered — store the public key alongside whatever the signature protects. Ox normalizes signatures to low-S so they round-trip with [`P256.verify`](/api/P256/verify) and onchain verifiers. ### Derive a Shared Secret (ECDH) Key agreement needs a dedicated key pair from [`WebCryptoP256.createKeyPairECDH`](/api/WebCryptoP256/createKeyPairECDH); then [`WebCryptoP256.getSharedSecret`](/api/WebCryptoP256/getSharedSecret) computes the Diffie-Hellman secret. ```ts twoslash import { WebCryptoP256 } from 'ox' const alice = await WebCryptoP256.createKeyPairECDH() const bob = await WebCryptoP256.createKeyPairECDH() const sharedSecret = await WebCryptoP256.getSharedSecret({ privateKey: alice.privateKey, // [!code hl] publicKey: bob.publicKey, // [!code hl] }) ``` Passing an ECDSA `CryptoKey` throws — Web Crypto binds each key to a single algorithm, so signing and agreement always use separate pairs. ## Best Practices ### Keep Keys Non-Extractable The default `extractable: false` is the point of this module: even an XSS payload cannot read the private key, only ask the runtime to sign with it. ### Verify With a Stored Public Key Without a recovery bit there is no "recover the signer" flow. Treat the public key as part of the credential record and verify against it explicitly. ## See More # Data & Encoding ## Overview Everything on Ethereum bottoms out in byte data. Ox represents it with two primitive types — [`Hex`](/api/Hex) and [`Bytes`](/api/Bytes) — and ships the codecs that move values between them and the wire: RLP for protocol serialization, Base64/Base58/Bech32m/CBOR for ecosystem interchange, plus bigint-safe JSON and lossless ether/gwei unit conversion. ```ts twoslash import { Hex, Value } from 'ox' const amount = Value.fromEther('1.5') // @log: 1500000000000000000n const quantity = Hex.fromNumber(amount) // @log: '0x14d1120d7b160000' ``` # Base32 Coding ## Overview [`Base32`](/api/Base32) implements the raw BIP-173 character set — the alphabet Bech32 addresses build on — without a checksum or human-readable prefix. Reach for it when a protocol hands you bare base32 data; for checksummed addresses use [`Bech32m`](/api/Bech32m) instead. ## Recipes ### Encode & Decode Data Round-trip hex payloads with [`Base32.fromHex`](/api/Base32/fromHex) and [`Base32.toHex`](/api/Base32/toHex); [`Base32.fromBytes`](/api/Base32/fromBytes) and [`Base32.toBytes`](/api/Base32/toBytes) cover `Uint8Array` payloads. ```ts twoslash import { Base32 } from 'ox' const encoded = Base32.fromHex('0xdeadbeef') // @log: 'm6kmamc' const decoded = Base32.toHex(encoded) // @log: '0xdeadbeef' ``` ## Best Practices ### Prefer Bech32m for Addresses Base32 carries no integrity protection. When encoding anything a user might copy by hand — an address, an invoice — use [`Bech32m`](/guides/data/bech32m), which adds a prefix and a checksum that catches transcription errors. ## See More # Base58 Coding ## Overview [`Base58`](/api/Base58) is the alphabet behind Bitcoin addresses, IPFS CIDv0 hashes, and Solana public keys — Base64 without the visually ambiguous characters (`0`, `O`, `I`, `l`). Ox converts Base58 values to and from [`Hex`](/api/Hex), [`Bytes`](/api/Bytes), and plain strings. ## Recipes ### Encode & Decode Data Round-trip a value with [`Base58.fromString`](/api/Base58/fromString) and [`Base58.toString`](/api/Base58/toString). ```ts twoslash import { Base58 } from 'ox' const encoded = Base58.fromString('Hello World!') // @log: '2NEpo7TZRRrLZSi2U' const decoded = Base58.toString(encoded) // @log: 'Hello World!' ``` ### Bring Base58 Identifiers into Primitive Types Use [`Base58.toHex`](/api/Base58/toHex) or [`Base58.toBytes`](/api/Base58/toBytes) to convert external identifiers — a Solana public key, an IPFS CIDv0 hash — into the types the rest of Ox operates on. ```ts twoslash import { Base58 } from 'ox' const hex = Base58.toHex('2NEpo7TZRRrLZSi2U') // [!code hl] // @log: '0x48656c6c6f20576f726c6421' ``` Decoding preserves leading zero bytes (encoded as `1` characters), matching Bitcoin's address conventions. ## Best Practices ### Expect Typed Errors from Untrusted Input The decoder validates every character against the Base58 alphabet and throws `Base58.InvalidCharacterError` on corruption. Wrap decoding of user-supplied identifiers and surface the failure instead of propagating malformed bytes. ## See More # Base64 Coding ## Overview [`Base64`](/api/Base64) converts between Base64 strings and Ox's primitive types — the encoding behind data URLs, HTTP payloads, and WebAuthn's transport format. Encoders exist for [`Hex`](/api/Base64/fromHex), [`Bytes`](/api/Base64/fromBytes), and plain [strings](/api/Base64/fromString), each with a matching decoder. ## Recipes ### Encode & Decode Data Round-trip a value with [`Base64.fromString`](/api/Base64/fromString) and [`Base64.toString`](/api/Base64/toString). ```ts twoslash import { Base64 } from 'ox' const encoded = Base64.fromString('hello world') // @log: 'aGVsbG8gd29ybGQ=' const decoded = Base64.toString(encoded) // @log: 'hello world' ``` [`Base64.fromBytes`](/api/Base64/fromBytes) and [`Base64.toBytes`](/api/Base64/toBytes) do the same for `Uint8Array` payloads. ### Embed Calldata in URLs Use [`Base64.fromHex`](/api/Base64/fromHex) with URL-safe characters to embed calldata or other hex payloads in query strings, data URLs, and JSON APIs, then recover the hex with [`Base64.toHex`](/api/Base64/toHex). ```ts twoslash import { Base64 } from 'ox' const encoded = Base64.fromHex('0xa9059cbb', { url: true, pad: false }) // [!code hl] // @log: 'qQWcuw' const calldata = Base64.toHex(encoded) // @log: '0xa9059cbb' ``` ## Best Practices ### Use URL-Safe Base64 on the Wire The standard Base64 alphabet contains `+` and `/`, which break query strings and path segments. Pass `{ url: true }` whenever the encoded value travels inside a URL. ## See More # Bech32m Coding ## Overview [`Bech32m`](/api/Bech32m) (BIP-350) combines a human-readable prefix with a checksummed data part — the format behind Bitcoin Taproot addresses and other modern address schemes. The checksum catches transcription errors before they become lost funds. ## Recipes ### Encode Data with a Prefix [`Bech32m.encode`](/api/Bech32m/encode) takes a human-readable part (HRP) and the data bytes to protect. ```ts twoslash import { Bech32m } from 'ox' const address = Bech32m.encode('tempo', new Uint8Array(20)) // [!code hl] // @log: 'tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwa7xtm' ``` ### Decode & Verify a Checksum [`Bech32m.decode`](/api/Bech32m/decode) verifies the checksum and returns the prefix and data, throwing a typed error on corruption. ```ts twoslash import { Bech32m } from 'ox' declare const address: string const { hrp, data } = Bech32m.decode(address) // [!code hl] // @log: { hrp: 'tempo', data: Uint8Array(20) } ``` For raw BIP-173 base32 data without a checksum, use [`Base32`](/guides/data/base32). ## Best Practices ### Treat the Prefix as Part of the Address The HRP is covered by the checksum: `tempo1…` and `other1…` encoding the same bytes are different addresses. Validate that a decoded `hrp` matches the network you expect before using the data part. ## See More # CBOR Coding ## Overview [`Cbor`](/api/Cbor) round-trips JavaScript values through the Concise Binary Object Representation (RFC 8949) — the serialization behind WebAuthn attestation objects, COSE keys, and IPFS/IPLD blocks. ## Recipes ### Encode & Decode Values [`Cbor.encode`](/api/Cbor/encode) serializes objects, arrays, and primitives; [`Cbor.decode`](/api/Cbor/decode) recovers them. ```ts twoslash import { Cbor } from 'ox' const encoded = Cbor.encode({ foo: 'bar', baz: [1, 2, 3] }) // @log: '0xa263666f6f636261726362617a83010203' const decoded = Cbor.decode(encoded) // @log: { foo: 'bar', baz: [1, 2, 3] } ``` ### Choose the Output Type Pass `{ as: 'Bytes' }` to encode straight to a `Uint8Array` when the payload feeds an API that expects raw bytes rather than hex. ```ts twoslash import { Cbor } from 'ox' const bytes = Cbor.encode({ alg: -7 }, { as: 'Bytes' }) // [!code hl] // @log: Uint8Array [161, 99, 97, 108, 103, 38] ``` ## Best Practices ### Decode Before You Trust CBOR from an authenticator or a network peer is untrusted input. Decode it, then validate the shape of the result — decoding succeeding says nothing about the payload matching the structure your code expects. ## See More # CompactSize Coding ## Overview [`CompactSize`](/api/CompactSize) implements Bitcoin's variable-length integer encoding, used to length-prefix scripts and payloads on the wire. Values up to `0xfc` take one byte; larger values get a marker byte plus a little-endian integer. ## Recipes ### Encode a Length Prefix [`CompactSize.toHex`](/api/CompactSize/toHex) (or [`CompactSize.toBytes`](/api/CompactSize/toBytes)) encodes an integer with the smallest valid representation. ```ts twoslash import { CompactSize } from 'ox' const encoded = CompactSize.toHex(520) // [!code hl] // @log: '0xfd0802' ``` ### Decode a Varint & Advance a Cursor [`CompactSize.fromHex`](/api/CompactSize/fromHex) returns both the decoded value and the number of bytes consumed, so a parser knows how far to advance its cursor. ```ts twoslash import { CompactSize } from 'ox' const { value, size } = CompactSize.fromHex('0xfd0802') // [!code hl] // @log: { value: 520n, size: 3 } ``` Decoding enforces minimal encodings and throws `CompactSize.NonMinimalEncodingError` on padded values, matching Bitcoin consensus rules. ## Best Practices ### Track the Consumed Size A CompactSize prefix is one, three, five, or nine bytes long. Always advance parsing offsets by the returned `size` rather than assuming a fixed width. ## See More # Format Ether & Gwei Values ## Overview User interfaces speak decimal strings; Ethereum speaks integral wei. [`Value`](/api/Value) converts between the two with pure bigint arithmetic — no floating point, no precision loss. ## Recipes ### Parse User Input to Wei Use [`Value.fromEther`](/api/Value/fromEther) to turn a decimal string from a form input into a wei amount ready for a transaction's `value` field, and [`Value.fromGwei`](/api/Value/fromGwei) for user-supplied gas prices. ```ts twoslash import { Value } from 'ox' const value = Value.fromEther('0.05') // [!code hl] // @log: 50000000000000000n const maxFeePerGas = Value.fromGwei('20') // @log: 20000000000n ``` Malformed input such as `'1.2.3'` throws `Value.InvalidDecimalNumberError` — surface it as form validation feedback instead of letting garbage reach a transaction. ### Format Wei for Display [`Value.formatEther`](/api/Value/formatEther) and [`Value.formatGwei`](/api/Value/formatGwei) render wei balances and gas prices as human-readable decimal strings. ```ts twoslash import { Value } from 'ox' const balance = Value.formatEther(1_500_000_000_000_000_000n) // @log: '1.5' const gasPrice = Value.formatGwei(20_000_000_000n) // @log: '20' ``` Trailing zeros are trimmed, so `1_000_000_000_000_000_000n` formats as `'1'`, not `'1.000000'`. ### Handle Token Decimals ERC-20 tokens define their own precision — pass the token's `decimals` to [`Value.from`](/api/Value/from) and [`Value.format`](/api/Value/format) to parse and render token amounts (e.g. `6` for USDC). ```ts twoslash import { Value } from 'ox' const decimals = 6 // from the token contract's `decimals()` function const amount = Value.from('100.5', decimals) // [!code hl] // @log: 100500000n const display = Value.format(100_500_000n, decimals) // @log: '100.5' ``` Input with more fractional digits than `decimals` is rounded half-away-from-zero rather than silently truncated. ## Best Practices ### Keep Amounts as BigInt Convert only at the edges: parse once when input arrives, format once when a value is displayed. All arithmetic, comparison, and storage in between should stay in wei (`bigint`). ### Never Round-Trip Through Number `Number` loses integer precision above 2^53 — roughly 0.009 ether in wei. Avoid `parseFloat`, `Number(value)`, and arithmetic operators on stringified amounts; `Value` exists so you never need them. ## See More # Serialize JSON Safely ## Overview `JSON.stringify` throws on `bigint`, and integers above `Number.MAX_SAFE_INTEGER` silently lose precision in `JSON.parse` — routine hazards once wei amounts move through APIs, caches, and storage. [`Json`](/api/Json) provides drop-in replacements that round-trip bigints intact, plus [RFC 8785](https://www.rfc-editor.org/rfc/rfc8785) canonicalization for hashing and signing. ## Recipes ### Parse JSON Containing BigInts [`Json.parse`](/api/Json/parse) restores bigint values that were serialized with [`Json.stringify`](/api/Json/stringify), so wei balances survive the round-trip through your API or database without truncating to a lossy `number`. ```ts twoslash import { Json } from 'ox' const data = Json.parse( '{"balance":"69420694206942069420694206942069420694206942069420#__bigint"}', ) // @log: { balance: 69420694206942069420694206942069420694206942069420n } ``` Plain JSON without the bigint marker takes a fast path through native `JSON.parse`, so it is safe to use `Json.parse` for every payload. ### Stringify Without Precision Loss [`Json.stringify`](/api/Json/stringify) serializes bigint values with a marker suffix instead of throwing, letting wei-denominated state travel through JSON APIs unchanged. ```ts twoslash import { Json } from 'ox' const json = Json.stringify({ to: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', value: 1_000_000_000_000_000_000n, // [!code hl] }) // @log: '{"to":"0xd8da6bf26964af9d7eed9e03e53415d37aa96045","value":"1000000000000000000#__bigint"}' ``` It accepts the same `replacer` and `space` arguments as native `JSON.stringify`. ### Canonicalize for Hashing & Signing [`Json.canonicalize`](/api/Json/canonicalize) emits RFC 8785 canonical JSON — keys recursively sorted, no whitespace — so structurally equal objects always hash to the same digest. ```ts twoslash import { Hash, Hex, Json } from 'ox' const canonical = Json.canonicalize({ b: 2, a: 1 }) // [!code hl] // @log: '{"a":1,"b":2}' const digest = Hash.keccak256(Hex.fromString(canonical)) ``` Canonicalization rejects `bigint` and non-finite numbers by design — convert those to strings before hashing. ## Best Practices ### Pair Stringify with Parse The `#__bigint` marker is an Ox convention: only `Json.parse` revives it. When the consumer is a third party, convert bigints to explicit decimal strings (e.g. with [`Value.format`](/api/Value/format)) instead of leaking the marker into their schema. ### Canonicalize Before You Hash Plain `JSON.stringify` output depends on key insertion order, so equal objects can produce different digests. Any time a JSON document feeds a hash or a signature, build it with `Json.canonicalize`. ## See More # Work with Bytes & Hex ## Overview When working with Ethereum, byte data — addresses, hashes, signatures, serialized payloads — is commonly represented as either **hexadecimal strings** or **byte arrays**. Ox models the two with [`Hex`](/api/Hex) ([`Hex.Hex`](/api/Hex/types#hexhex), a `0x`-prefixed `string`) and [`Bytes`](/api/Bytes) ([`Bytes.Bytes`](/api/Bytes/types#bytesbytes), a `Uint8Array` instance). Every operation on one module has a mirror on the other, so recipes below show whichever side is most idiomatic. ## Recipes ### Instantiate from Primitives Construct values with [`Hex.from`](/api/Hex/from) & [`Bytes.from`](/api/Bytes/from), or lift booleans, numbers, and strings with the typed constructors: [`Hex.fromBoolean`](/api/Hex/fromBoolean) / [`Bytes.fromBoolean`](/api/Bytes/fromBoolean), [`Hex.fromNumber`](/api/Hex/fromNumber) / [`Bytes.fromNumber`](/api/Bytes/fromNumber), and [`Hex.fromString`](/api/Hex/fromString) / [`Bytes.fromString`](/api/Bytes/fromString). ```ts twoslash import { Bytes, Hex } from 'ox' const hex = Hex.from('0xdeadbeef') // @log: '0xdeadbeef' const bytes = Bytes.from([0xde, 0xad, 0xbe, 0xef]) // @log: Uint8Array [0xde, 0xad, 0xbe, 0xef] const bool = Hex.fromBoolean(true) // @log: '0x1' const bigint = Hex.fromNumber(420n) // @log: '0x1a4' const string = Hex.fromString('hello') // @log: '0x68656c6c6f' ``` `Hex` can also be instantiated from `Bytes`, and vice versa — `Hex.from(bytes)` and `Bytes.from(hex)` convert between the two representations. ### Convert Between Types Recover primitive JavaScript values with [`Hex.toBigInt`](/api/Hex/toBigInt) / [`Bytes.toBigInt`](/api/Bytes/toBigInt), [`Hex.toBoolean`](/api/Hex/toBoolean) / [`Bytes.toBoolean`](/api/Bytes/toBoolean), [`Hex.toNumber`](/api/Hex/toNumber) / [`Bytes.toNumber`](/api/Bytes/toNumber), and [`Hex.toString`](/api/Hex/toString) / [`Bytes.toString`](/api/Bytes/toString). ```ts twoslash import { Bytes, Hex } from 'ox' const bool = Bytes.toBoolean(Bytes.from([1])) // @log: true const bigint = Hex.toBigInt('0x01a4') // @log: 420n const number = Bytes.toNumber(Bytes.from([1, 164])) // @log: 420 const string = Hex.toString('0x68656c6c6f') // @log: 'hello' ``` Prefer `toBigInt` over `toNumber` for on-chain quantities — wei amounts routinely exceed `Number.MAX_SAFE_INTEGER`. ### Concatenate, Pad, Slice & Trim Byte-manipulation helpers exist on both modules — [`Hex.concat`](/api/Hex/concat) / [`Bytes.concat`](/api/Bytes/concat), [`Hex.padLeft`](/api/Hex/padLeft) / [`Hex.padRight`](/api/Hex/padRight), [`Hex.slice`](/api/Hex/slice) / [`Bytes.slice`](/api/Bytes/slice), [`Hex.trimLeft`](/api/Hex/trimLeft) / [`Hex.trimRight`](/api/Hex/trimRight), and [`Hex.size`](/api/Hex/size) / [`Bytes.size`](/api/Bytes/size). Sizes and offsets are measured in bytes, not characters. ```ts twoslash import { Bytes, Hex } from 'ox' const concatenated = Hex.concat('0xdead', '0xbeef') // @log: '0xdeadbeef' const padded = Hex.padLeft('0xdead', 4) // @log: '0x0000dead' const sliced = Hex.slice('0x0123456789', 1, 4) // @log: '0x234567' const trimmed = Bytes.trimLeft(Bytes.from([0x00, 0x00, 0xde, 0xad])) // @log: Uint8Array [0xde, 0xad] const size = Hex.size('0xdeadbeefdeadbeefdeadbeefdeadbeef') // @log: 16 ``` `padLeft` & `padRight` default to a size of `32` bytes — the width of an EVM word — so `Hex.padLeft('0xdead')` produces an ABI-ready word. ### Compare & Validate [`Hex.isEqual`](/api/Hex/isEqual) & [`Bytes.isEqual`](/api/Bytes/isEqual) compare by value; [`Hex.validate`](/api/Hex/validate) & [`Bytes.validate`](/api/Bytes/validate) return a boolean for untrusted input, while [`Hex.assert`](/api/Hex/assert) & [`Bytes.assert`](/api/Bytes/assert) throw a typed error instead. ```ts twoslash import { Bytes, Hex } from 'ox' const equal = Bytes.isEqual( Bytes.from([0xde, 0xad, 0xbe, 0xef]), Bytes.from([0xca, 0xfe, 0xba, 0xbe]), ) // @log: false const valid = Hex.validate('0xdeadbeefz') // @log: false Hex.assert('abc') // @error: Error: Hex.InvalidHexValueError ``` Reach for `assert` at trust boundaries where invalid data should halt processing, and `validate` where you branch on the result. ### Generate Random Values [`Hex.random`](/api/Hex/random) & [`Bytes.random`](/api/Bytes/random) produce cryptographically secure random bytes of a given length — ready for `CREATE2` salts, nonces, and session identifiers. ```ts twoslash import { Bytes, Hex } from 'ox' const salt = Hex.random(32) // @log: '0x86d8b4…' (32 random bytes) const nonce = Bytes.random(16) // @log: Uint8Array(16) [134, 216, …] ``` ## Best Practices ### Choose the Representation per Boundary Most Ox functions accept either type. Use `Hex` at serialization boundaries — JSON-RPC and signing payloads speak `0x`-prefixed strings — and keep `Bytes` for repeated binary work, where avoiding hex round-trips in hot paths saves allocations. ### Validate Before You Trust A `0x` prefix does not make a string valid hex. Run external input through `validate` or `assert` before slicing, padding, or converting it, so malformed data fails loudly at the edge. ## See More # Work with RLP ## Overview Recursive Length Prefix (RLP) is the Ethereum protocol's core serialization method — a space-efficient standard for packaging and transferring arbitrarily nested byte data. [`Rlp`](/api/Rlp) encodes and decodes it. RLP underpins [transaction envelope serialization](/guides/transactions/build-sign-send), [EIP-7702 authorization](/api/Authorization/getSignPayload) sign payloads, and [`CREATE` contract address derivation](/api/ContractAddress/fromCreate). ## Recipes ### Encode & Decode a Value Round-trip a single value with [`Rlp.fromHex`](/api/Rlp/fromHex) and [`Rlp.toHex`](/api/Rlp/toHex). ```ts twoslash import { Rlp } from 'ox' const rlp = Rlp.fromHex('0x68656c6c6f') // @log: '0x8568656c6c6f' const value = Rlp.toHex(rlp) // @log: '0x68656c6c6f' ``` ### Encode Nested Data [`Rlp.fromHex`](/api/Rlp/fromHex) serializes arbitrarily nested arrays of [`Hex`](/api/Hex) values into a single RLP payload. ```ts twoslash import { Hex, Rlp } from 'ox' const rlp = Rlp.fromHex([ Hex.fromString('hello'), Hex.fromNumber(1337), [Hex.fromString('foo'), Hex.fromString('bar')], // [!code hl] ]) // @log: '0xd28568656c6c6f820539c883666f6f83626172' ``` Working with `Uint8Array` data instead? [`Rlp.fromBytes`](/api/Rlp/fromBytes) accepts nested [`Bytes`](/api/Bytes) and returns `Bytes`. ### Decode to Hex or Bytes [`Rlp.toHex`](/api/Rlp/toHex) deserializes an RLP payload back into its original nested structure with `Hex` leaves; [`Rlp.toBytes`](/api/Rlp/toBytes) does the same with `Bytes` leaves. ```ts twoslash import { Rlp } from 'ox' const values = Rlp.toHex('0xd28568656c6c6f820539c883666f6f83626172') // @log: ['0x68656c6c6f', '0x0539', ['0x666f6f', '0x626172']] ``` Either function accepts `Hex` or `Bytes` input, so raw payloads from the wire decode without a manual conversion step. ## Best Practices ### Bring Your Own Schema RLP encodes structure, not types. Decoding returns nested `Hex` (or `Bytes`) leaves — it is up to you to reinterpret each position (`Hex.toString`, `Hex.toNumber`, …) according to the schema the data was encoded with, in the same order. ## See More # JSON-RPC & Providers ## Overview For an application to communicate with the Ethereum network, it needs to connect to an Ethereum Node and exchange messages in a standardized format. All Ethereum Nodes implement the [JSON-RPC specification](https://www.jsonrpc.org/specification), and most Wallets expose an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) Provider interface on top of it. Ox provides type-safe primitives for both sides of the wire: [`RpcRequest`](/api/RpcRequest), [`RpcResponse`](/api/RpcResponse), [`RpcTransport`](/api/RpcTransport), [`RpcSchema`](/api/RpcSchema), and [`Provider`](/api/Provider). ```ts twoslash import { RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') // [!code hl] const blockNumber = await transport.request({ method: 'eth_blockNumber' }) // @log: '0x1a2b3c' ``` # Send JSON-RPC Requests ## Overview [`RpcRequest`](/api/RpcRequest) builds [JSON-RPC 2.0](https://www.jsonrpc.org/specification) request objects, [`RpcResponse`](/api/RpcResponse) parses what the node returns, and [`RpcTransport`](/api/RpcTransport) bundles both behind an HTTP `request` function. All three are stateless and transport-agnostic — pair them with `fetch`, WebSockets, or any messaging layer. ## Recipes ### Build a Request Store [`RpcRequest.createStore`](/api/RpcRequest/createStore) returns a `prepare` function that builds strongly-typed JSON-RPC request objects with an auto-incrementing `id`. ```ts twoslash import { RpcRequest } from 'ox' const store = RpcRequest.createStore() const request_1 = store.prepare({ method: 'eth_blockNumber', }) // @log: { id: 0, jsonrpc: '2.0', method: 'eth_blockNumber' } const request_2 = store.prepare({ method: 'eth_getBlockByNumber', params: ['latest', false], }) // @log: { id: 1, jsonrpc: '2.0', method: 'eth_getBlockByNumber', params: ['latest', false] } ``` Use [`RpcRequest.from`](/api/RpcRequest/from) instead to build a single request and manage the `id` yourself. ### Send over HTTP Fetch A prepared request is a plain JSON-serializable object — POST it to any RPC endpoint. ```ts twoslash import { RpcRequest } from 'ox' const store = RpcRequest.createStore() const request = store.prepare({ method: 'eth_getBlockByNumber', params: ['latest', false], }) const response = await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), method: 'POST', headers: { 'Content-Type': 'application/json', }, }).then((res) => res.json()) ``` ### Send with an RPC Transport [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) wraps the prepare → fetch → parse loop into a single `request` function that manages `id`s and raises JSON-RPC errors for you. ```ts twoslash import { RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const blockNumber = await transport.request({ method: 'eth_blockNumber' }) // @log: '0x1a2b3c' ``` The transport is also EIP-1193 compatible — see [Use EIP-1193 Providers](/guides/rpc/providers). ### Parse Responses [`RpcResponse.parse`](/api/RpcResponse/parse) extracts the JSON-RPC `result`, throws a typed error when the response contains an `error`, and — given the originating `request` — strongly types the result. ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' const store = RpcRequest.createStore() const request = store.prepare({ method: 'eth_getBlockByNumber', params: ['latest', false], }) const block = await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), method: 'POST', headers: { 'Content-Type': 'application/json', }, }) .then((res) => res.json()) .then((res) => RpcResponse.parse(res, { request })) ``` Set `raw: true` to receive the whole `{ result, error }` response object instead of throwing on errors. ## Best Practices ### Reuse One Store per Connection A store increments its `id` on every `prepare` call. Create one store per connection or session so ids stay unique and responses can be correlated with their requests. ### Let a Transport Own the Plumbing Reach for `RpcTransport.fromHttp` unless you control the wire format yourself. For a full-featured client with retries, batching, and wallet actions, use [Viem](https://viem.sh). ## See More # Serve & Handle RPC Requests ## Overview On the serving side of JSON-RPC — an API route, a service worker, or an [EIP-1193 Provider `request` handler](/api/Provider/from) in a Wallet — [`RpcResponse.from`](/api/RpcResponse/from) builds spec-compliant response objects for incoming [`RpcRequest`](/api/RpcRequest)s. ## Recipes ### Handle Requests in a Server or Worker Match on `request.method`, answer what you can locally, and proxy the rest to an upstream node. Passing `{ request }` fills the response's `id` and `jsonrpc` properties from the request. ```ts twoslash import { RpcRequest, RpcResponse, RpcSchema } from 'ox' const accounts = [ '0xd2135CfB216b74109775236E36d4b433F1DF507B', '0x0D44f617435088c947F00B31160f64b074e412B4', ] as const async function handleRequest(request: RpcRequest.RpcRequest) { if (request.method === 'eth_accounts') { return RpcResponse.from({ result: accounts }, { request }) // [!code hl] } if (request.method === 'eth_chainId') { return RpcResponse.from({ result: '0x1' }, { request }) // [!code hl] } // Fall back to an upstream node for everything else. return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), method: 'POST', headers: { 'Content-Type': 'application/json', }, }) .then((res) => res.json()) .then((res) => RpcResponse.from(res)) } ``` The `RpcSchema.Eth` annotation narrows `request.method` and types each `result` against the method's return type. ### Return Typed Errors [`RpcResponse`](/api/RpcResponse) exports an error class for every JSON-RPC error code (see [RpcResponse errors](/api/RpcResponse/errors)). Throw them inside your handler and convert anything caught with [`RpcResponse.parseError`](/api/RpcResponse/parseError) before responding. ```ts twoslash import { RpcRequest, RpcResponse, RpcSchema } from 'ox' function handleRequest(request: RpcRequest.RpcRequest) { try { if (request.method !== 'eth_chainId') throw new RpcResponse.MethodNotSupportedError() // [!code hl] return RpcResponse.from({ result: '0x1' }, { request }) } catch (error) { const { code, message } = RpcResponse.parseError(error) // [!code hl] return RpcResponse.from({ error: { code, message } }, { request }) // [!code hl] } } ``` `RpcResponse.parseError` maps unknown exceptions to [`RpcResponse.InternalError`](/api/RpcResponse/errors) (code `-32603`), so callers always receive a structured JSON-RPC error object. ## Best Practices ### Always Echo the Request `id` JSON-RPC clients correlate responses by `id`. Pass `{ request }` to `RpcResponse.from` instead of filling `id` and `jsonrpc` by hand. ### Respond with JSON-RPC Errors, Not Exceptions A thrown exception terminates the transport; a JSON-RPC error keeps the session alive and tells the client what went wrong. Map failures to the typed error classes and their well-known codes. ## See More # Type-Safe RPC Schemas ## Overview [`RpcSchema`](/api/RpcSchema) statically types JSON-RPC method names, parameters, and return values. The default schema covers the `eth_` and `wallet_` namespaces; a schema built with [`RpcSchema.from`](/api/RpcSchema/from) plugs the same type information into [`RpcTransport`](/api/RpcTransport), [`Provider`](/api/Provider), and [`RpcRequest.createStore`](/api/RpcRequest/createStore). ## Recipes ### Type a Transport with a Schema `RpcSchema.from` is a runtime no-op — it exists purely to tag a transport with the union of methods it supports. ```ts twoslash import { RpcSchema, RpcTransport } from 'ox' const schema = RpcSchema.from< | RpcSchema.Default | { Request: { method: 'ox_getMagic' params: [id: number] } ReturnType: `0x${string}` } >() const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com', { schema, // [!code hl] }) const magic = await transport.request({ method: 'ox_getMagic', // [!code hl] params: [1], }) ``` Include `RpcSchema.Default` in the union to keep the standard `eth_` and `wallet_` methods available alongside your custom ones. ### Extend with Custom Methods The same schema types an [EIP-1193 Provider](/api/Provider)'s `request` function — useful when a Wallet exposes methods beyond the standard namespaces. ```ts twoslash import 'ox/window' import { Provider, RpcSchema } from 'ox' const schema = RpcSchema.from< | RpcSchema.Default | { Request: { method: 'wallet_getSecret' // [!code hl] params: [id: string] // [!code hl] } ReturnType: `0x${string}` // [!code hl] } >() const provider = Provider.from(window.ethereum, { schema }) const secret = await provider.request({ method: 'wallet_getSecret', params: ['1'], }) ``` `RpcRequest.createStore({ schema })` accepts the same option, so a Wallet can share one schema between the requests it sends and the requests it handles. ## Best Practices ### Define the Schema Once Declare the schema in a shared module and import it wherever a store, transport, or provider is created. Diverging copies defeat the purpose of end-to-end typing. ### Types Are Not Validation A schema types the compile-time surface only — nothing is checked at runtime. Validate untrusted requests and responses with runtime schemas — see [Validate with Zod](/guides/schemas/zod). ## See More # Use EIP-1193 Providers ## Overview [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) defines a JavaScript Ethereum Provider API for interacting with the Ethereum network from an arbitrary JavaScript environment (e.g. Web Application, Server, etc). You would typically use one when communicating with a Wallet — most Browser Extension Wallets inject a `window.ethereum` object that conforms to the EIP-1193 API. Ox's [`Provider`](/api/Provider) module instantiates typed providers, and the `ox/window` entrypoint augments `window.ethereum` with the provider types. ## Recipes ### Wrap an Injected Provider External EIP-1193 Providers can be instantiated with [`Provider.from`](/api/Provider/from). Importing `ox/window` types `window.ethereum`. ```ts twoslash import 'ox/window' import { Provider } from 'ox' const provider = Provider.from(window.ethereum) const blockNumber = await provider.request({ method: 'eth_blockNumber' }) ``` You can also plug in a Provider distributed by a library: | Library | Description | | ----------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | | [`mipd`](https://github.com/wevm/mipd) | Multi-injected browser wallet provider discovery | | [`@walletconnect/ethereum-provider`](https://www.npmjs.com/package/@walletconnect/ethereum-provider) | Connect and interact with WalletConnect-enabled wallets | | [`@metamask/sdk`](https://docs.metamask.io/wallet/connect/metamask-sdk/javascript/) | Connect and interact with MetaMask Wallet | | [`@coinbase/wallet-sdk`](https://github.com/coinbase/coinbase-wallet-sdk) | Connect and interact with Coinbase Wallet | | [`@safe-global/safe-apps-provider`](https://github.com/safe-global/safe-apps-sdk/tree/main/packages/safe-apps-provider) | Connect and interact with Safe Wallets | ### Create a Provider from a Transport Ox's [`RpcTransport`](/api/RpcTransport) is also EIP-1193 compliant, and can be used to instantiate an EIP-1193 Provider. This means you can use any HTTP RPC endpoint as an EIP-1193 Provider. ```ts twoslash import { Provider, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const provider = Provider.from(transport) const blockNumber = await provider.request({ method: 'eth_blockNumber' }) ``` ### Emit Provider Events Event emitters for EIP-1193 Providers can be created using [`Provider.createEmitter`](/api/Provider/createEmitter) — useful for Wallets that distribute a Provider (e.g. webpage injection via `window.ethereum`). ```ts twoslash import { Provider, RpcRequest, RpcResponse } from 'ox' // 1. Instantiate a Provider Emitter. const emitter = Provider.createEmitter() // [!code hl] const store = RpcRequest.createStore() const provider = Provider.from({ // 2. Pass the Emitter to the Provider. ...emitter, // [!code hl] async request(args) { return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(store.prepare(args)), method: 'POST', headers: { 'Content-Type': 'application/json', }, }) .then((res) => res.json()) .then(RpcResponse.parse) }, }) // 3. Emit Provider Events. emitter.emit('accountsChanged', ['0x...']) // [!code hl] ``` Consumers subscribe with `provider.on('accountsChanged', ...)` and the other [EIP-1193 events](https://eips.ethereum.org/EIPS/eip-1193#events). ### Handle Provider Errors Wallets reject requests with well-known EIP-1193 error codes. [`Provider.parseError`](/api/Provider/parseError) converts an unknown thrown value into a typed [Provider error](/api/Provider/errors), such as `Provider.UserRejectedRequestError`. ```ts twoslash // @noErrors import { Provider } from 'ox' const provider = Provider.from(window.ethereum) try { const accounts = await provider.request({ method: 'eth_requestAccounts', }) // @log: ['0x71bE63f3384f5fb98995898A86B02Fb2426c5788'] } catch (e) { const error = Provider.parseError(e) if (error instanceof Provider.UserRejectedRequestError) { // @log: code: 4001 — user rejected the request } } ``` ## Best Practices ### Feature-Detect the Injected Provider `window.ethereum` is `undefined` outside wallet-enabled browsers. Check for it (or discover providers with [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963) via `mipd`) before wrapping. ### Parse Errors at the Boundary Wrap `provider.request` calls in `try`/`catch` and normalize failures with `Provider.parseError` so application code can branch on typed error classes instead of numeric codes. ## See More # Messages & Authentication ## Overview Ethereum accounts prove ownership and intent off-chain by signing structured messages. Ox provides primitives for every common signing envelope: [ERC-191](https://eips.ethereum.org/EIPS/eip-191) personal messages, [EIP-712](https://eips.ethereum.org/EIPS/eip-712) typed data, [Sign-In with Ethereum](https://eips.ethereum.org/EIPS/eip-4361) authentication, and the [ERC-6492](https://eips.ethereum.org/EIPS/eip-6492)/[ERC-8010](https://eips.ethereum.org/EIPS/eip-8010) wrappers used by smart accounts. ```ts twoslash import { Hex, PersonalMessage, Secp256k1 } from 'ox' // Compute the EIP-191 payload for a message. const payload = PersonalMessage.getSignPayload(Hex.fromString('hello world')) // Sign the payload. const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) // Recover the signer. const signer = Secp256k1.recoverAddress({ payload, signature }) ``` ## Guides # Sign Personal Messages (EIP-191) ## Overview The [ERC-191 Signed Data](https://eips.ethereum.org/EIPS/eip-191) standard defines a specification for handling signed data in Ethereum contracts. Signable data is prefixed with a version byte (e.g. `0x45` for personal messages). This protects an end-user from signing obscured transaction data constructed by a malicious actor and consequently losing funds. [`PersonalMessage`](/api/PersonalMessage) handles the `0x45` (personal message) version and [`ValidatorData`](/api/ValidatorData) the `0x00` (intended validator) version. Ox supports the following ERC-191 versions: | Module | Name | Version | | ----------------------------------------- | ------------------------------------------------------------------------- | ------- | | [`PersonalMessage`](/api/PersonalMessage) | Personal Message (aka. `personal_sign`) | `0x45` | | [`TypedData`](/api/TypedData) | Typed Data — see [Sign Typed Data (EIP-712)](/guides/messages/typed-data) | `0x01` | | [`ValidatorData`](/api/ValidatorData) | Data with intended validator | `0x00` | ## Recipes ### Compute the Sign Payload & Sign Personal messages are typically used to sign arbitrary messages that will be displayed to the user, for example, a [Sign-In with Ethereum (SIWE) message](/guides/messages/siwe). Compute the signable payload with [`PersonalMessage.getSignPayload`](/api/PersonalMessage/getSignPayload), then sign it — here with [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Hex, PersonalMessage, Secp256k1 } from 'ox' const payload = PersonalMessage.getSignPayload(Hex.fromString('hello world')) const signature = Secp256k1.sign({ payload, privateKey: '0x...', }) ``` Any Ox signer works — see [Work with Secp256k1](/guides/crypto/secp256k1) for the full signer lifecycle. ### Verify & Recover the Signer Recompute the payload from the exact message that was signed, then recover the signing address with [`Secp256k1.recoverAddress`](/api/Secp256k1/recoverAddress), or check against a known address with [`Secp256k1.verify`](/api/Secp256k1/verify). ```ts twoslash import { Hex, PersonalMessage, Secp256k1, Signature } from 'ox' declare const signature: Signature.Signature const payload = PersonalMessage.getSignPayload(Hex.fromString('hello world')) const signer = Secp256k1.recoverAddress({ payload, signature }) const verified = Secp256k1.verify({ address: signer, payload, signature, }) // @log: true ``` ### Sign with a Wallet (personal\_sign) Most Wallets expose a [`personal_sign` RPC interface](https://docs.metamask.io/wallet/reference/json-rpc-methods/personal_sign/) that can be used to sign Personal Messages. This means you can use the `personal_sign` RPC method to sign a message without the ceremony of constructing and signing it yourself. ```ts twoslash import 'ox/window' import { Hex, Provider } from 'ox' const provider = Provider.from(window.ethereum) const [address] = await provider.request({ method: 'eth_requestAccounts' }) const signature = await provider.request({ method: 'personal_sign', params: [Hex.fromString('hello world'), address], }) ``` ### Sign Intended-Validator Data (ERC-191 0x00) The `0x00` version binds a signature to an intended validator (e.g. the contract that will consume it), so it cannot be replayed against another verifier. Compute the payload with [`ValidatorData.getSignPayload`](/api/ValidatorData/getSignPayload). ```ts twoslash import { Hex, Secp256k1, ValidatorData } from 'ox' const payload = ValidatorData.getSignPayload({ data: Hex.fromString('hello world'), validator: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', }) const signature = Secp256k1.sign({ payload, privateKey: '0x...', }) ``` If you need the raw `0x19 ‖ 0x00 ‖ validator ‖ data` envelope before hashing, use [`ValidatorData.encode`](/api/ValidatorData/encode). ## Best Practices ### Hash via the Module, Not by Hand Always derive payloads with `PersonalMessage.getSignPayload` (or `ValidatorData.getSignPayload`) so the ERC-191 version byte and length prefix are applied. The prefix is what prevents a signed message from doubling as a valid transaction or other signable payload. ### Recover Against the Exact Bytes Verification recomputes the payload from the message bytes. Any difference in encoding or whitespace between what the user signed and what the server hashes will recover a different address. ## See More # Sign Typed Data (EIP-712) ## Overview Typed Data is signed data that presents structured, human-readable data to the user to sign. This structure (and encoding format) is defined by the [EIP-712 standard](https://eips.ethereum.org/EIPS/eip-712). The [`TypedData`](/api/TypedData) module computes sign payloads, domain separators, and serialized representations of typed data. ## Recipes ### Define & Hash Typed Data A signable Typed Data payload can be computed using [`TypedData.getSignPayload`](/api/TypedData/getSignPayload): ```ts twoslash import { TypedData } from 'ox' const payload = TypedData.getSignPayload({ domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000', }, types: { Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' }, ], Mail: [ { name: 'from', type: 'Person' }, { name: 'to', type: 'Person' }, { name: 'contents', type: 'string' }, ], }, primaryType: 'Mail', message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', }, contents: 'Hello, Bob!', }, }) ``` The payload is `keccak256(0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message))` — the ERC-191 `0x01` version envelope. ### Sign & Verify Sign the payload using a signer — here [`Secp256k1.sign`](/api/Secp256k1/sign) — then recover the signer with [`Secp256k1.recoverAddress`](/api/Secp256k1/recoverAddress). ```ts twoslash import { Secp256k1, TypedData } from 'ox' const payload = TypedData.getSignPayload({ domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000', }, types: { Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' }, ], Mail: [ { name: 'from', type: 'Person' }, { name: 'to', type: 'Person' }, { name: 'contents', type: 'string' }, ], }, primaryType: 'Mail', message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', }, contents: 'Hello, Bob!', }, }) const signature = Secp256k1.sign({ payload, privateKey: '0x...', }) const signer = Secp256k1.recoverAddress({ payload, signature }) ``` Compare the recovered signer against the expected address with [`Address.isEqual`](/api/Address/isEqual). ### Sign with a Wallet (eth\_signTypedData\_v4) Most Wallets expose a [`eth_signTypedData_v4` RPC interface](https://docs.metamask.io/wallet/reference/json-rpc-methods/eth_signtypeddata_v4/) that can be used to sign Typed Data. This means you can use the `eth_signTypedData_v4` RPC method to sign a message without the ceremony of constructing and signing it yourself. ```ts twoslash // @noErrors import 'ox/window' import { Hex, Provider, Secp256k1, TypedData } from 'ox' const provider = Provider.from(window.ethereum) const [address] = await provider.request({ method: 'eth_requestAccounts' }) const payload = TypedData.serialize({ /* ... */ }) const signature = await provider.request({ method: 'eth_signTypedData_v4', params: [address, payload], }) ``` [`TypedData.serialize`](/api/TypedData/serialize) produces the JSON string that `eth_signTypedData_v4` expects as its second parameter. ### Extract the Domain Derive the EIP-712 domain schema with [`TypedData.extractEip712DomainTypes`](/api/TypedData/extractEip712DomainTypes), and its [`domainSeparator`](/api/TypedData/domainSeparator) — useful when interoperating with contracts that expose `eip712Domain()` (ERC-5267). ```ts twoslash import { TypedData } from 'ox' const domain = { name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', } as const const types = TypedData.extractEip712DomainTypes(domain) // @log: [ // @log: { 'name': 'name', 'type': 'string' }, // @log: { 'name': 'version', 'type': 'string' }, // @log: { 'name': 'chainId', 'type': 'uint256' }, // @log: { 'name': 'verifyingContract', 'type': 'address' }, // @log: ] const separator = TypedData.domainSeparator(domain) // @log: '0x9911ee4f58a7059a8f5385248040e6984d80e2c849500fe6a4d11c4fa98c2af3' ``` ## Best Practices ### Bind Signatures to a Domain Include `chainId` and `verifyingContract` in the domain so a signature for one contract on one chain cannot be replayed against another. ### Validate Untrusted Definitions When typed data arrives from an external source, check it with [`TypedData.validate`](/api/TypedData/validate) (returns `false`) or [`TypedData.assert`](/api/TypedData/assert) (throws) before hashing or signing. ## See More # Sign-In with Ethereum (SIWE) ## Overview **Sign-In with Ethereum (SIWE)** is a standard described by [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) for how Ethereum accounts authenticate with off-chain services by signing a standard message format. The [`Siwe`](/api/Siwe) module generates nonces, creates and parses messages, and validates their fields. This guide details how to perform SIWE with Ox in a client-server architecture. ## Recipes The recipes below form one flow: the server issues a nonce, the client creates and signs the message, and the server validates the result. ### Generate a Nonce Using [`Siwe.generateNonce`](/api/Siwe/generateNonce), you can generate a random nonce that can be used to prevent replay attacks. ```ts twoslash [Server] import { Siwe } from 'ox' function handler() { const nonce = Siwe.generateNonce() return nonce } ``` Your server should generate a new nonce for each SIWE process it performs. The nonce should be stored for later use (e.g. in the session or database) to validate the signature. ### Create the SIWE Message Before you can create a SIWE message, you need to source the following information: * `address`: The Ethereum address performing the signing. * `chainId`: The [EIP-155](https://eips.ethereum.org/EIPS/eip-155) Chain ID to which the session is bound, * `domain`: [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing. * `uri`: [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI referring to the resource that is the subject of the signing (as in the subject of a claim). * `version`: The current version of the SIWE Message. ```ts twoslash [Client] function onClick() { const data = { address: '', // e.g. Wagmi `useAccount()`/`getAccount()` chainId: 1, domain: window.location.host, nonce: '', // e.g. `await getNonceFromServer()` uri: window.location.origin, version: '1', } as const } ``` Once you have the information, you can create the SIWE message with [`Siwe.createMessage`](/api/Siwe/createMessage). ```ts twoslash [Client] import { Siwe } from 'ox' function onClick() { const data = { address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: '65ed4681d4efe0270b923ff5f4b097b1c95974dc33aeebecd5724c42fd86dfd25dc70b27ef836b2aa22e68f19ebcccc1', uri: 'https://example.com/path', version: '1', } as const satisfies Siwe.Message const message = Siwe.createMessage(data) } ``` In addition, to the required fields, there are some optional fields you can include: * `expirationTime`: Time when the signed authentication message is no longer valid. * `issuedAt`: Time when the message was generated, typically the current time. * `notBefore`: Time when the signed authentication message will become valid. * `requestId`: A system-specific identifier that may be used to uniquely refer to the sign-in request. * `resources`: A list of information or references to information the user wishes to have resolved as part of authentication by the relying party. * `scheme`: [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request. * `statement`: A human-readable ASCII assertion that the user will sign. ### Sign the Message Once the message is created, the next step is to sign it. In this guide, we will assume a browser wallet will sign over JSON-RPC, but you could also sign with [`Secp256k1.sign`](/api/Secp256k1/sign) if you have access to the private key directly. ```ts twoslash [Client] import 'ox/window' //---cut--- import { Hex, Provider, Siwe } from 'ox' async function onClick() { const data = { address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: '65ed4681d4efe0270b923ff5f4b097b1c95974dc33aeebecd5724c42fd86dfd25dc70b27ef836b2aa22e68f19ebcccc1', uri: 'https://example.com/path', version: '1', } as const satisfies Siwe.Message const message = Siwe.createMessage(data) const provider = Provider.from(window.ethereum) const signature = await provider.request({ method: 'personal_sign', params: [Hex.fromString(message), data.address], }) } ``` :::info To keep this example simple, we use `window.ethereum` to create our [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) Provider. Since `window.ethereum` is considered deprecated, you should use [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963) instead. We recommend either using [Wagmi](https://wagmi.sh) to manage the wallet connection and signing, but you can also use a lower-level library like [`mipd`](https://github.com/wevm/mipd) if all you want is the provider. ::: ### Validate the Signature Now that we have our message and signature, we can validate them to confirm the address did indeed sign the message. This validation must be performed in such a way that it cannot be tampered with. In our case, we will use the server. ```ts twoslash [Server] // @errors: 2322 import { Address, Hex, PersonalMessage, Secp256k1, Siwe } from 'ox' function handler() { const payload = { // ↑ Sent by client message: 'example.com wants you to sign in with your Ethereum account: ...', signature: '0x...', } as const // Parse message string into structured object const parsed = Siwe.parseMessage(payload.message) if (!parsed.address) return false // Validate message fields (e.g. `now >= expirationTime`), nonce, etc. const isValid = Siwe.validateMessage({ message: parsed, nonce: '', }) if (!isValid) return false const personalMessage = PersonalMessage.encode( Hex.fromString(payload.message), ) const verified = Address.isEqual( Address.from(parsed.address), Secp256k1.recoverAddress({ payload: personalMessage, signature: payload.signature, }), ) return verified } ``` ## Best Practices ### Issue a Fresh Nonce per Attempt Generate a new nonce for every sign-in attempt and invalidate it once used. Reusing nonces allows a captured signature to be replayed. ### Validate on the Server Both [`Siwe.validateMessage`](/api/Siwe/validateMessage) and signature recovery must run in an environment the client cannot tamper with. Client-side checks are cosmetic only. ### Account for Smart Accounts Counterfactual smart accounts cannot produce a plain ECDSA signature — they return an [ERC-6492 wrapped signature](/guides/messages/smart-account-signatures) instead. Detect the wrapper before attempting ECDSA recovery. ## See More # Smart Account Signatures (6492/8010) ## Overview Smart accounts verify signatures onchain via [ERC-1271](https://eips.ethereum.org/EIPS/eip-1271) `isValidSignature` — which fails when the account contract does not exist yet. [`SignatureErc6492`](/ercs/erc6492/SignatureErc6492) wraps a signature with the factory call that deploys the account, so verifiers can validate signatures from **counterfactual** (not-yet-deployed) accounts. [`SignatureErc8010`](/ercs/erc8010/SignatureErc8010) wraps a signature with a signed [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) authorization, so verifiers can validate against an EOA whose delegation is not yet live onchain. ## Recipes ### Wrap a Signature for a Counterfactual Account (ERC-6492) Wrap the account's signature together with the factory address (`to`) and the calldata that deploys the account (`data`) using [`SignatureErc6492.wrap`](/ercs/erc6492/SignatureErc6492/wrap). ```ts twoslash import { Address, Hex, PersonalMessage, Secp256k1, Signature } from 'ox' import { SignatureErc6492 } from 'ox/erc6492' declare const factory: Address.Address declare const factoryData: Hex.Hex const payload = PersonalMessage.getSignPayload(Hex.fromString('hello world')) const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) const wrapped = SignatureErc6492.wrap({ data: factoryData, signature: Signature.toHex(signature), to: factory, }) ``` The wrapped value ends in the ERC-6492 magic bytes (`SignatureErc6492.magicBytes`), which is how verifiers distinguish it from a plain signature. ### Unwrap & Verify (ERC-6492) Detect the wrapper with [`SignatureErc6492.validate`](/ercs/erc6492/SignatureErc6492/validate), then recover the constituent parts with [`SignatureErc6492.unwrap`](/ercs/erc6492/SignatureErc6492/unwrap). ```ts twoslash import { SignatureErc6492 } from 'ox/erc6492' declare const wrapped: SignatureErc6492.Wrapped if (!SignatureErc6492.validate(wrapped)) throw new Error('not an ERC-6492 wrapped signature') const { data, signature, to } = SignatureErc6492.unwrap(wrapped) // [!code hl] ``` For full counterfactual verification, perform an `eth_call` that deploys [`SignatureErc6492.universalSignatureValidatorBytecode`](/ercs/erc6492/SignatureErc6492) with the signer, payload hash, and wrapped signature as constructor arguments — it validates deployed accounts (ERC-1271), undeployed accounts, and plain ECDSA signatures alike. ### Delegate Verification with ERC-8010 Wrap a signature together with a signed EIP-7702 authorization using [`SignatureErc8010.wrap`](/ercs/erc8010/SignatureErc8010/wrap), so verifiers can apply the delegation before checking the signature. ```ts twoslash import { Authorization, PersonalMessage, Secp256k1, Signature } from 'ox' import { SignatureErc8010 } from 'ox/erc8010' // 1. Sign the EIP-7702 authorization for the delegation. const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n, }) const authorizationSignature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...', }) const authorizationSigned = Authorization.from(authorization, { signature: authorizationSignature, }) // 2. Sign the payload. const signature = Secp256k1.sign({ payload: PersonalMessage.getSignPayload('0xdeadbeef'), privateKey: '0x...', }) // 3. Wrap the signature with the authorization. const wrapped = SignatureErc8010.wrap({ authorization: authorizationSigned, signature: Signature.toHex(signature), }) ``` On the verifying side, [`SignatureErc8010.unwrap`](/ercs/erc8010/SignatureErc8010/unwrap) recovers the authorization and the inner signature: ```ts twoslash import { SignatureErc8010 } from 'ox/erc8010' declare const wrapped: SignatureErc8010.Wrapped const { authorization, signature } = SignatureErc8010.unwrap(wrapped) ``` ## Best Practices ### Check the Magic Bytes First Both formats terminate in distinctive magic bytes. Use `validate` before unwrapping, and fall back to plain ECDSA verification when it returns `false` — EOAs and deployed accounts still produce unwrapped signatures. ### Serialize the Inner Signature `wrap` expects the inner signature as serialized hex. Convert structured signatures with [`Signature.toHex`](/api/Signature/toHex) before wrapping. ### Verify Against Live Chain State Wrappers only carry the *instructions* to make verification possible. Trustworthy verification still requires executing them against current chain state (e.g. the universal validator via `eth_call`), since the account may have been deployed, upgraded, or re-delegated since signing. ## See More # Runtime & Performance ## Overview Ox runs on portable JavaScript cryptography by default and lets performance-sensitive applications opt into faster backends. Install a WASM or Node.js [`Engine`](/api/Engine) to accelerate cryptography without changing calling code, create an independently owned WASM KZG instance for blobs and PeerDAS, and manage Ox's global caches and bundle footprint. ```ts twoslash import { Engine, Hash } from 'ox/wasm' await Engine.install() const digest = Hash.keccak256('0xdeadbeef') ``` # Caching & Bundle Size ## Overview Ox keeps a small set of module-instance-global caches to avoid recomputing derived values. The `Caches` module exposes them: [`Address.checksum`](/api/Address/checksum) results are memoized in a bounded map holding up to 32,768 entries with first-in-first-out eviction. Long-running processes and test suites can clear or tune these caches directly. ## Recipes ### Clear Global Caches Reset every global cache in one call — for example between test cases, or in a long-running process after a burst of one-off work. ```ts twoslash import { Address, Caches } from 'ox' // Checksumming populates the global checksum cache. Address.checksum('0xa0cf798816d4b9b9866b5330eea46a18382f251e') Caches.clear() ``` Installing, setting, or resetting an [engine](/guides/runtime/engines) also clears Ox's derived cryptographic caches automatically. ### Clear a Single Cache Each cache is exported as a bounded `Map`, so the standard `Map` API applies when only one cache should be inspected or reset. ```ts twoslash import { Address, Caches } from 'ox' Address.checksum('0xa0cf798816d4b9b9866b5330eea46a18382f251e') Caches.checksum.size // @log: 1 Caches.checksum.clear() ``` ### Tune Cache Capacity Lower `maxSize` to trade hit rate for memory in constrained environments. Once the bound is reached, writes evict the oldest entry first. ```ts twoslash import { Caches } from 'ox' Caches.checksum.maxSize = 1_024 ``` The default bound is 32,768 entries per cache. ## Bundle Size Ox modules are collections of pure, stateless functions — not stateful instances — so bundlers can tree-shake unused functions out of the final bundle. Both named imports (`import { Hex } from 'ox'`) and entrypoint imports (`import * as Hex from 'ox/Hex'`) stay tree-shakable; the [Imports & Bundle Size](/imports) page covers both approaches and which bundlers support them. Optional entrypoints such as `ox/wasm` and `ox/node` live outside the default entrypoint, so their runtime-specific artifacts are only bundled when an application imports them. ## See More # WASM & Engines ## Overview Ox uses portable JavaScript cryptography by default. An [`Engine`](/api/Engine) replaces individual implementations without changing the `Hash`, `Secp256k1`, `Keystore`, or other public APIs that call them. | Engine | Runtime | Benefits | Trade-offs | | --------- | ------------------------- | ----------------------------------- | ------------------------------------ | | `ox` | Browser, Node, and edge | Audited, isomorphic, no setup | Slower for some primitives | | `ox/wasm` | Runtimes with WebAssembly | Fast, isomorphic, portable | Async startup and memory marshalling | | `ox/node` | Node.js 22 and newer | Native cryptography, no compilation | Node-only and intentionally partial | | Custom | Implementation-dependent | Any supported primitive or provider | You own correctness and key handling | Engines are partial. Anything they omit leaves the currently installed implementation unchanged, falling back to Ox's default when no override was installed earlier. [`Engine.install`](/api/Engine/install) resolves selected provider modules in parallel and installs them only after every module is ready. Calls merge, including within the same slot, and Ox selects an implementation when a cryptographic function is called. Install an engine once during application startup, before making cryptographic calls. The registry belongs to one loaded Ox module instance, so separate copies of Ox have separate registries. ## Recipes ### Use the Default Engine No setup is required: ```ts twoslash import { Hash } from 'ox' const digest = Hash.keccak256('0xdeadbeef') ``` Ox's defaults use the [`@noble`](https://github.com/paulmillr/noble-hashes) and [`@scure`](https://github.com/paulmillr/scure-bip32) JavaScript libraries. [`Engine.get()`](/api/Engine/get) returns `{}` until an override is installed because defaults are fallbacks, not registered overrides. **Benefits** * Works across browsers, Node.js, and edge runtimes. * Covers Ox's complete cryptography surface. * Requires no initialization or runtime-specific entrypoint. * Keeps runtime-specific dependencies out of the default entrypoint. **Trade-offs** * Pure JavaScript can be slower than WASM or platform-native implementations. * It cannot use Node's OpenSSL and hardware-accelerated SHA paths. * It may not satisfy a policy requiring a particular native, hardware, or validated provider. ### Install the WASM Engine The WASM engine compiles and installs asynchronously: ```ts twoslash import { Engine, Hash } from 'ox/wasm' await Engine.install() const digest = Hash.keccak256('0xdeadbeef') ``` It supplies the following partial slots: * `Hash`: BLAKE3, HMAC-SHA256, Keccak256, RIPEMD-160, and SHA-256. * `Keystore`: synchronous PBKDF2-HMAC-SHA256. * `MlDsa44`: public-key derivation, signing, and verification. * `Mnemonic`: BIP-39 seed derivation. * `Secp256k1`: public-key derivation, shared-secret calculation, recovery, signing, and verification. * `Ed25519`: public-key derivation, signing, verification, and private-key conversion to X25519. * `X25519`: public-key derivation and shared-secret calculation. Other operations keep their current implementation, or use Ox's default when no earlier override exists. In particular, asynchronous KDFs remain on the default because wrapping synchronous WASM in a promise would not make it yield. Random key generation for `MlDsa44` and `Secp256k1` remains host-backed. The dedicated `ox/wasm/Keystore` provider also supplies synchronous scrypt. Install that provider explicitly. The aggregate engine excludes scrypt because its relative performance depends on `N`, `r`, `p`, and the runtime. KZG is intentionally outside the global registry. Use the explicit, independently owned factory described in the [WASM KZG guide](/guides/runtime/kzg). **Benefits** * Runs in browsers, Node.js, and other runtimes with WebAssembly. * Accelerates hashes, key derivation, Ed25519, ML-DSA-44, Secp256k1, and X25519 on common runtimes. * Keeps cryptographic calls synchronous after one asynchronous startup step. * Embeds the compiled module, with no separate WASM asset to host. * Clears copied secret inputs, staged outputs, and explicit workspaces after each sensitive call, including after recoverable traps. **Trade-offs** * Requires asynchronous initialization before cryptographic calls. * Copies inputs and outputs across WASM linear memory. * Gains vary by primitive, input size, runtime, and processor. * Adds the WASM implementation without removing JavaScript fallbacks from the bundle. * Uses pinned portable C implementations that Ox must keep reviewed and updated. ### Install the Node Engine The Node engine uses the built-in `node:crypto` implementation. Its setup is asynchronous to match `ox/wasm`, although Node requires no compilation: ```ts twoslash import { Engine, Hash } from 'ox/node' await Engine.install() const digest = Hash.sha256('0xdeadbeef') ``` It supplies the following partial slots: * `Hash`: HMAC-SHA256, RIPEMD-160, and SHA-256. * `Keystore`: AES-CTR and synchronous/asynchronous PBKDF2-HMAC-SHA256. * `Mnemonic`: BIP-39 seed derivation. * `Ed25519`: public-key derivation, signing, and private-key conversion to X25519. * `P256`: public-key derivation. * `X25519`: public-key derivation and shared-secret calculation. Other operations keep their current implementation, or use Ox's default when no earlier override exists. Node's `sha3-256` is not Ethereum Keccak256 and must not be substituted for it. Ed25519 verification stays on the default because Node/OpenSSL does not implement Ox's ZIP-215 semantics. Scrypt also stays on the default because OpenSSL rejects parameter combinations accepted by Ox's public API. **Benefits** * Uses Node's native OpenSSL-backed cryptography and libuv threadpool. * Can use processor acceleration for hashes, AES, and supported curves. * Requires no WASM compilation or memory marshalling. * Lives in a separate entrypoint, so browser bundles do not resolve `node:crypto`. **Trade-offs** * Supports Node.js only and must not be imported into browser bundles. * Does not provide Keccak256, BLAKE3, scrypt, or ZIP-215 verification. * Native call overhead can affect short-input rankings. * Algorithm availability and validation modes depend on the Node and OpenSSL build. FIPS mode may reject RIPEMD-160. * Leaves Ox's default fallback code available. ### Install Selected Modules Provider modules expose the regular Ox API alongside an `engine` factory. Use this to install only the modules an application needs: ```ts twoslash import { Engine } from 'ox' import { Hash } from 'ox/wasm' await Engine.install({ Hash: Hash.engine(), }) const digest = Hash.sha256('0xdeadbeef') ``` `Engine.install` awaits all supplied slot promises together, then installs the resolved modules atomically. `Hash.engine()` returns the raw `Hash` slot; `Hash.sha256()` remains the normal formatted public API. Install one Node module through the same core API: ```ts twoslash import { Engine } from 'ox' import { Hash } from 'ox/node' await Engine.install({ Hash: Hash.engine(), }) const digest = Hash.sha256('0xdeadbeef') ``` ### Combine Node & WASM Modules Select modules from different providers in one atomic installation: ```ts twoslash import { Engine, Hash } from 'ox' import { Hash as NodeHash } from 'ox/node' import { Ed25519 as WasmEd25519 } from 'ox/wasm' await Engine.install({ Ed25519: WasmEd25519.engine(), Hash: NodeHash.engine(), }) const digest = Hash.sha256('0xdeadbeef') ``` The factories begin initializing while the argument is evaluated, then `Engine.install` awaits them together. If any factory rejects, none of the resolved modules are installed. Repeated installations merge primitives. Installing Node's `Hash` after WASM keeps WASM-only BLAKE3 and Keccak256 installed while replacing the three overlapping hashes. Reset the slot first when replacing it completely: ```ts twoslash import { Engine } from 'ox' import { Hash } from 'ox/node' Engine.reset('Hash') await Engine.install({ Hash: Hash.engine() }) ``` ### Build a Custom Engine Install any subset of Ox's engine contract with [`Engine.set`](/api/Engine/set): ```ts twoslash import { Engine } from 'ox' Engine.set({ Hash: { keccak256: (input) => myKeccak256(input), }, }) // ---cut-after--- declare function myKeccak256(input: Uint8Array): Uint8Array ``` Slots and primitives are optional, and repeated calls merge: ```ts twoslash import { Engine } from 'ox' // ---cut--- Engine.set({ Hash: { keccak256: myKeccak256 } }) Engine.set({ Secp256k1: mySecp256k1 }) // ---cut-after--- declare const myKeccak256: (input: Uint8Array) => Uint8Array declare const mySecp256k1: NonNullable ``` Use `Engine.install` when one or more custom modules are asynchronous. Values may be slots or promises of slots: ```ts twoslash import { Engine } from 'ox' await Engine.install({ Hash: Promise.resolve({ keccak256: myKeccak256 }), }) // ---cut-after--- declare const myKeccak256: (input: Uint8Array) => Uint8Array ``` Binary values cross engine boundaries as raw `Uint8Array` values. Ox performs `Hex` and `Bytes` conversion outside the engine boundary. Install explicitly rather than relying on a side-effect import: Ox declares `sideEffects: false`, so a bundler may drop an import that appears unused. **Benefits** * Supports synchronous native libraries and policy-required providers. * Replaces one primitive without requiring a complete slot. * Preserves Ox's public APIs and input/output conversion. * Composes with the built-in Node and WASM engines. **Trade-offs** * The engine author owns algorithm semantics, output lengths, key formats, and error behavior. * Most engine functions are synchronous. Only explicitly asynchronous contracts such as `scryptAsync` may return promises. * Overrides are module-instance-global, so initialization order and concurrent use matter. * Installing an engine changes dispatch but does not remove defaults from the bundle. ## Benchmarks Run the engine comparison with: ```sh pnpm bench:engines ``` The harness covers every engine slot and all 38 primitives in Ox's default engine. The columns count the primitives each provider or reference supplies: | Slot | Primitives | `ox` | `ox/node` | `ox/wasm` | C | | ----------- | ---------- | ------ | --------- | --------- | ------ | | `Bls` | 5 | 5 | n/a | n/a | n/a | | `Ed25519` | 6 | 6 | 3 | 4 | 4 | | `Hash` | 5 | 5 | 3 | 5 | 5 | | `Keystore` | 6 | 6 | 4 | 2 | 2 | | `MlDsa44` | 4 | 4 | n/a | 3 | 3 | | `Mnemonic` | 1 | 1 | 1 | 1 | 1 | | `P256` | 6 | 6 | 1 | n/a | n/a | | `Secp256k1` | 6 | 6 | n/a | 5 | 5 | | `X25519` | 3 | 3 | 2 | 2 | 2 | | **Total** | **42** | **42** | **14** | **22** | **22** | `n/a` means the provider does not implement a primitive in that slot. The harness never times Ox's fallback under another engine's name. The C reference compiles the same primitive wrappers, vendored sources, and target configuration as `ox/wasm` for the host. C covers every primitive supplied by WASM, but is not an Ox engine. The `ox/wasm` count includes the opt-in Keystore scrypt provider. Local runs on an Apple M4 Max with Node.js 25.9.0 and Apple Clang 21.0.0 used 50 ms warmups, 200 ms measurement budgets, and three repeats. The following table shows the best-observed timings (lower is better). Speedup compares the fastest Ox engine in each row with `ox`; C remains a reference only. The full command prints every primitive and input size: | Primitive and case | `ox` | `ox/node` | `ox/wasm` | C | Fastest Ox engine vs `ox` | | -------------------------------------- | --------- | --------- | --------- | --------- | ------------------------- | | `Bls.sign`, 32 B message | 15.70 ms | n/a | n/a | n/a | n/a | | `Ed25519.getPublicKey`, 32 B key | 95.51 µs | 41.87 µs | 21.82 µs | 18.80 µs | 4.38× (wasm) | | `Ed25519.sign`, 32 B message | 202.12 µs | 43.13 µs | 45.18 µs | 38.61 µs | 4.69× (node) | | `Ed25519.verify`, 32 B message | 960.96 µs | n/a | 59.31 µs | 49.81 µs | 16.20× (wasm) | | `Hash.blake3`, 32 B | 1.23 µs | n/a | 428 ns | 563 ns | 2.88× (wasm) | | `Hash.blake3`, 1024 KiB | 11.30 ms | n/a | 1.08 ms | 875.06 µs | 10.50× (wasm) | | `Hash.keccak256`, 32 B | 2.62 µs | n/a | 278 ns | 187 ns | 9.44× (wasm) | | `Hash.keccak256`, 1024 KiB | 20.10 ms | n/a | 2.32 ms | 1.39 ms | 8.67× (wasm) | | `Hash.sha256`, 1024 KiB | 3.95 ms | 368.29 µs | 3.25 ms | 2.48 ms | 10.72× (node) | | `Keystore.aesCtrEncrypt`, 4 KiB | 32.02 µs | 2.92 µs | n/a | n/a | 10.95× (node) | | `Keystore.pbkdf2Sha256`, 262,144 runs | 241.53 ms | 22.94 ms | 105.01 ms | 86.59 ms | 10.53× (node) | | `Keystore.scrypt`, N=1,024, r=1, p=1 | 207.76 µs | n/a | 161.16 µs | 141.87 µs | 1.29× (wasm) | | `Keystore.scrypt`, N=16,384, r=8, p=1 | 24.14 ms | n/a | 21.06 ms | 18.38 ms | 1.15× (wasm) | | `Keystore.scrypt`, N=262,144, r=1, p=8 | 513.45 ms | n/a | 443.69 ms | 445.30 ms | 1.16× (wasm) | | `Mnemonic.toSeed`, 12 words | 6.52 ms | 470.29 µs | 1.93 ms | 1.83 ms | 13.86× (node) | | `P256.getPublicKey`, 32 B key | 142.41 µs | 11.03 µs | n/a | n/a | 12.91× (node) | | `Secp256k1.getPublicKey`, 32 B key | 147.40 µs | n/a | 19.14 µs | 15.42 µs | 7.70× (wasm) | | `Secp256k1.getSharedSecret`, 65 B key | 1.77 ms | n/a | 34.20 µs | 27.51 µs | 51.64× (wasm) | | `Secp256k1.recoverPublicKey`, 32 B | 1.15 ms | n/a | 36.62 µs | 32.46 µs | 31.40× (wasm) | | `Secp256k1.sign`, 32 B message | 183.76 µs | n/a | 25.03 µs | 19.86 µs | 7.34× (wasm) | | `Secp256k1.verify`, 32 B message | 1.03 ms | n/a | 31.50 µs | 27.31 µs | 32.80× (wasm) | | `X25519.getSharedSecret`, 32 B key | 627.61 µs | 48.74 µs | 41.32 µs | 33.69 µs | 15.19× (wasm) | The benchmark initializes engines outside the timed loops and sends each Ox call through the same engine resolver. It uses identical inputs, warmups, budgets, and repeats, and reports the best-observed repeat as a peak-throughput microbenchmark, not a latency distribution. It measures raw single-call byte-array implementations, not Ox formatting or whole applications. Treat the results as runtime-specific: Node/OpenSSL, CPU acceleration, the WASM runtime, the C compiler, JIT state, and input size can all change the ranking. ## Testing Reset the installed engine between tests: ```ts twoslash import { Engine } from 'ox' import { beforeEach } from 'vitest' beforeEach(() => { Engine.reset() }) ``` For differential tests, obtain an implementation with `engine`, then install it for one synchronous call with [`Engine.with`](/api/Engine/with): ```ts twoslash import { Engine, Hash } from 'ox' import { Engine as WasmEngine } from 'ox/wasm' const wasm = await WasmEngine.engine() const digest = Engine.with(wasm, () => Hash.sha256('0xdeadbeef')) ``` `Engine.with` rejects asynchronous functions because concurrent work could observe the module-instance-global override. `Engine.install`, `Engine.set`, and `Engine.reset` clear Ox's derived cryptographic caches automatically. Test custom implementations against published vectors, boundary-sized and empty inputs, and an independent implementation. Verify exact digest, signature, and key lengths rather than checking only that a call succeeds. Ox's built-in engines add independent conformance coverage for NIST AES-CTR, RFC 7914 PBKDF2, RFC 8032 Ed25519, all 196 ZIP-215 verification cases, RFC 7748 X25519, libsodium's low-order X25519 corpus, official BLAKE3 vectors, and English and Japanese BIP-39 vectors. Differential fuzz tests also cover subviews, input immutability, output ownership, interleaved providers, memory growth, and boundary-sized inputs. WASM conformance runs in Chromium, Firefox, and WebKit as well as Node.js. ## Security Treat an engine as trusted code. Depending on the slot, it can receive HMAC keys, private keys, passwords, mnemonic phrases, and plaintext keystore material. `Engine.install` and `Engine.set` validate slot and primitive names, but they cannot validate correctness, constant-time behavior, or key handling. Ox's default engine uses audited cryptographic implementations. The WASM engine does not promise protection from timing or cache side channels. WebAssembly has [no constant-time execution guarantee](https://webassembly.org/docs/security/). The WASM providers clear copied secret inputs, staged outputs, and explicit workspaces from linear memory in `finally` blocks, including after recoverable WebAssembly traps. They cannot clear caller-owned buffers, JavaScript strings, runtime-managed state, or every compiler-created stack temporary. This does not make the surrounding runtime side-channel resistant. The WASM curve and mnemonic provider pins [Monocypher 4.0.3](https://github.com/LoupVaillant/Monocypher/tree/4.0.3). Its published Cure53 audit covered version 3.1.1, not the 4.x series, and 4.0.3 includes a fix recorded in Monocypher's [security disclosures](https://monocypher.org/quality-assurance/disclosures). The BLAKE3 provider pins the official portable C implementation and disables architecture-specific SIMD and atomics. The Node engine inherits the properties of the active Node and OpenSSL build. Using `node:crypto` does not itself mean FIPS mode is enabled or that every configured provider exposes RIPEMD-160. For threats involving precise timing measurement or hostile same-process code, use an OS keystore, hardware-backed signer, or isolated signing service. ## See More # WASM KZG ## Overview Ox provides an opt-in KZG implementation backed by [`c-kzg-4844`](https://github.com/ethereum/c-kzg-4844). It implements the existing [`Kzg.Kzg`](/api/Kzg) interface without changing the global [`Engine`](/api/Engine) registry. ## Recipes ### Create an Instance Supply the Ethereum trusted setup when creating an instance: ```ts twoslash import { Blobs } from 'ox' import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet }) try { const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) } finally { kzg.dispose() } ``` `Setups.mainnet` contains Ox's packed copy of the canonical Ethereum setup. You can reuse the same value across factory calls because each call copies the setup before asynchronous initialization. Ox sources the setup from an explicit tagged `c-kzg-4844` release and records both source and generated-data SHA-256 values. Release updates require an explicit review and regeneration; they never follow a moving branch. The factory also accepts custom setups with the standard `g1_lagrange`, `g1_monomial`, and `g2_monomial` fields. Each field may contain hex points or one packed byte array. The instance implements all EIP-4844 and EIP-7594 operations in `Kzg.Kzg`: * `blobToKzgCommitment` * `computeCells` * `computeCellsAndKzgProofs` * `recoverCellsAndKzgProofs` * `verifyCellKzgProofBatch` Import `ox/wasm/crypto/Kzg` directly when only KZG is needed. The dedicated artifact does not load through `ox`, another WASM module, or an `Engine` slot. Import `ox/trusted-setups/Setups` directly when only setup data is needed. The packed setup is not loaded by `ox`, `ox/wasm`, or `ox/trusted-setups/Paths`. ### Own & Dispose Instances Each `create` call owns separate WASM memory and trusted-setup state, so release an instance explicitly once its work is done. ```ts twoslash import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet }) kzg.dispose() kzg.dispose() // idempotent ``` Concurrent factory calls share only the compiled module, not mutable memory or setup state. Operations are synchronous and run to completion. Create one instance inside each JavaScript worker that performs KZG operations. `dispose` is idempotent. It frees c-kzg setup allocations, clears the module reference, and makes later operations throw `Kzg.DisposedError`. The JavaScript runtime controls when the underlying `WebAssembly.Memory` object is collected. Drop other references to the instance so the runtime can reclaim that memory. Inputs are copied into linear memory. Scratch inputs and outputs are zeroed and freed after each operation, including failed operations. ### Tune Memory & Precomputation `precompute` selects c-kzg's fixed-base MSM window. The default `0` uses the least memory. Larger windows trade memory for proof-generation performance. ```ts twoslash import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet, precompute: 4, // [!code hl] }) ``` The KZG artifact is 166.1 KiB raw, 221.4 KiB as embedded base64, and 51.4 KiB gzip. The trusted setup contains 390.1 KiB of packed points, uses 520.1 KiB of embedded base64, and produces a 395.6 KiB gzip standalone bundle. Each KZG instance starts with 8 MiB and may grow to 128 MiB. | `precompute` | Linear memory after initialization | | ------------ | ---------------------------------- | | 0 | 8 MiB | | 1 | 8 MiB | | 2 | 8 MiB | | 3 | 8 MiB | | 4 | 10.5 MiB | | 5 | 16.5 MiB | | 6 | 28.5 MiB | | 7 | 52.5 MiB | | 8 | 100.5 MiB | These values measure WASM linear memory immediately after trusted-setup initialization. Operation inputs and outputs may grow memory further, within the 128 MiB limit. Use the smallest window that meets the target workload. Run `pnpm bench src/wasm/crypto/Kzg.bench.ts --project core` on the deployment hardware to compare initialization and every supported operation. ## See More # Schemas & Validation ## Overview Ox ships [Zod](https://zod.dev) schemas for Ethereum data structures via the [`ox/zod`](/zod) entrypoint. Most schemas are codecs that convert between the JSON-RPC wire format (hex-string quantities) and the ergonomic decoded format used in application code (`bigint`/`number`), and every schema doubles as a validator for untrusted input. ```ts twoslash import { z } from 'ox/zod' const value = z.decode(z.Uint256, '0x2a') // @log: 42n ``` # Validate with Zod ## Overview Ox provides a set of [Zod](https://zod.dev) schemas for Ethereum data structures via the [`ox/zod`](/zod) entrypoint. These schemas are useful for **validating**, **decoding**, and **encoding** values that cross the JSON-RPC boundary (for example, parsing the result of an RPC call, or building a strongly-typed request). Most schemas are [Zod codecs](https://zod.dev/codecs) that convert between two representations: * **Input (RPC / wire format)** – the format used over JSON-RPC, where quantities are hex strings (e.g. `'0x1'`). * **Output (decoded format)** – the ergonomic format used in application code, where quantities are `bigint`/`number` (e.g. `1n`). `zod` ships as a dependency of Ox, so there is nothing extra to install. ## Recipes ### Import Schemas Everything is available under the `z` namespace, which re-exports all of [`zod/mini`](https://zod.dev/packages/mini) alongside Ox's schema namespaces: ```ts twoslash import { z } from 'ox/zod' ``` This gives you: * All standard Zod utilities (`z.decode`, `z.encode`, `z.parse`, `z.safeParse`, `z.object`, the `z.input`/`z.output` type helpers, etc.). * Module-scoped Ethereum schemas (`z.Address`, `z.Block`, `z.Transaction`, `z.Log`, …). * Direct integer quantity schemas (`z.Uint256`, `z.Int256`, `z.Number`, `z.BigInt`, …). * JSON-RPC method schemas (`z.RpcSchema`). * Tempo schemas (`z.tempo`). ### Decode & Encode RPC Data Use `z.decode` to convert an RPC value into its decoded form, and `z.encode` to convert it back. ```ts twoslash import { z } from 'ox/zod' // Decode an RPC hex quantity into a `bigint`. const value = z.decode(z.Uint256, '0x2a') // @log: 42n // Encode it back into an RPC hex quantity. const hex = z.encode(z.Uint256, value) // @log: '0x2a' ``` Schemas compose, so you can decode an entire structure in one pass: ```ts twoslash import { z } from 'ox/zod' const signature = z.decode(z.Signature.Signature, { r: '0x0000000000000000000000000000000000000000000000000000000000000001', s: '0x0000000000000000000000000000000000000000000000000000000000000002', yParity: '0x1', }) // @log: { r: 1n, s: 2n, yParity: 1 } ``` ### Validate Untrusted Input Since these are Zod schemas, you can validate values with the usual `z.parse` and `z.safeParse` helpers: ```ts twoslash import { z } from 'ox/zod' const result = z.safeParse(z.Address.Address, '0xdeadbeef') // @log: { success: false, error: [ZodError] } ``` ### Decode Integer Quantities Hex quantities can be decoded directly into `bigint` or `number` using sized integer schemas. Unsigned (`Uint`) and signed (`Int`) variants are available in 8-bit increments from `8` to `256`, in addition to the unsized `Uint`/`Int` and the `Number`/`BigInt` helpers. ```ts twoslash import { z } from 'ox/zod' z.decode(z.Uint8, '0xff') // 255 (number) z.decode(z.Uint256, '0x2a') // 42n (bigint) z.decode(z.Number, '0x1b4') // 436 (number) z.decode(z.BigInt, '0x1b4') // 436n (bigint) ``` Schemas with a result that fits within 48 bits (e.g. `Uint8` … `Uint48`) decode to a `number`; larger schemas decode to a `bigint`. ### Use JSON-RPC Method Schemas `z.RpcSchema` exposes per-method schemas for the `eth_` and `wallet_` namespaces, plus helpers to validate and decode requests, params, and results. The namespaces are: * `z.RpcSchema.Eth` – `eth_` methods. * `z.RpcSchema.Wallet` – `wallet_` methods. * `z.RpcSchema.Default` – the union of both. Decode the `params` or `result` for a single method: ```ts twoslash import { z } from 'ox/zod' // Decode the `params` for a method. const params = z.RpcSchema.decodeParams( z.RpcSchema.Eth, 'eth_getBlockByNumber', ['0x1', true], ) // Decode the `result` for a method. const result = z.RpcSchema.decodeReturns( z.RpcSchema.Eth, 'eth_blockNumber', '0x1b4', ) // @log: 436n ``` `z.RpcSchema.decodeRequest` (aliased as `z.RpcSchema.parse`) validates and decodes a full `{ method, params }` request, dispatching on `method`: ```ts twoslash import { z } from 'ox/zod' const request = z.RpcSchema.decodeRequest(z.RpcSchema.Eth, { method: 'eth_getBlockByNumber', params: ['0x1', true], }) ``` Use `z.RpcSchema.parseItem` to retrieve the schema for a single method (its `method`, `params`, `returns`, and `request` schemas): ```ts twoslash import { z } from 'ox/zod' const item = z.RpcSchema.parseItem(z.RpcSchema.Eth, 'eth_blockNumber') ``` If a method does not exist on the namespace, the helpers throw a `z.RpcSchema.MethodNotFoundError`. ### Define Custom Method Schemas Use `z.RpcSchema.from` to define your own method schema: ```ts twoslash import { z } from 'ox/zod' const eth_blockNumber = z.RpcSchema.from({ method: 'eth_blockNumber', params: z.tuple([]), returns: z.Uint256, }) ``` ## Tempo Schemas Schemas for [Tempo](/tempo) data structures are available under `z.tempo` (e.g. `z.tempo.Transaction`, `z.tempo.TransactionReceipt`, `z.tempo.TxEnvelopeTempo`, `z.tempo.SignatureEnvelope`, `z.tempo.KeyAuthorization`, `z.tempo.RpcSchemaTempo`). They work the same way as the core schemas: ```ts twoslash import { z } from 'ox/zod' type TransactionRpc = z.input type Transaction = z.output ``` ## Types Use Zod's `z.input` and `z.output` type helpers to extract the RPC and decoded types from any schema: ```ts twoslash import { z } from 'ox/zod' type TransactionRpc = z.input type Transaction = z.output ``` ## Available Schemas The `ox/zod` entrypoint includes schemas for: * **Primitives**: `Address`, `Bytes`, `Hash`, `Hex`, `BigInt`, `Number`, `Int`, `Uint` * **Accounts & State**: `AccountProof`, `StateOverrides`, `BlockOverrides` * **Blocks & Logs**: `Block`, `Log`, `Filter`, `Withdrawal` * **Fees & Access**: `Fee`, `AccessList`, `Authorization` * **Transactions**: `Transaction`, `TransactionReceipt`, `TransactionRequest`, `TransactionEnvelope` (and per-type `TxEnvelopeLegacy`, `TxEnvelopeEip1559`, `TxEnvelopeEip2930`, `TxEnvelopeEip4844`, `TxEnvelopeEip7702`) * **Signatures**: `Signature` * **JSON-RPC**: `RpcResponse`, `RpcSchema` * **Tempo**: `tempo.*` ## See More # Transactions ## Overview Ox provides protocol-level primitives for every Ethereum transaction envelope type — from legacy transactions through [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) blobs and [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) delegations. The [`TxEnvelope*`](/api/TransactionEnvelope) modules construct, sign, serialize, and deserialize envelopes locally, with no client required. Recipes end at the network boundary — hand the serialized payload to a JSON-RPC transport, or use a higher-level client like [Viem](https://viem.sh) when an application also needs to fill and track transactions. ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' // 1. Construct the envelope. const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) // 2. Sign over the envelope. const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...', }) // 3. Serialize it, ready for `eth_sendRawTransaction`. const serialized = TxEnvelopeEip1559.serialize(envelope, { signature }) ``` # Build, Sign & Send ## Overview A transaction envelope is a structure that defines the properties of a transaction, and is generally used to construct transactions to be broadcast to a network. This guide walks an [`TxEnvelopeEip1559`](/api/TxEnvelopeEip1559) — the most commonly used envelope type — through its full lifecycle with [`Secp256k1`](/api/Secp256k1) for signing and [`RpcTransport`](/api/RpcTransport) for broadcasting. Every envelope module shares the same lifecycle functions (`from`, `getSignPayload`, `serialize`, `toRpc`), so these recipes apply to all five envelope types. See [Choose an Envelope Type](/guides/transactions/envelope-types) for the others. ## Recipes ### Construct an EIP-1559 Envelope Use [`TxEnvelopeEip1559.from`](/api/TxEnvelopeEip1559/from) to instantiate an envelope. Ox is stateless — it does not query a node — so supply `nonce`, `gas`, and fee values yourself, or [hand off to a wallet](#sign-remotely-wallets--signing-servers) that fills them. ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) ``` See [Estimate Fees & Access Lists](/guides/transactions/fees-access-lists) for deriving `maxFeePerGas` from fee history. ### Compute the Sign Payload & Sign Pass the result of [`TxEnvelopeEip1559.getSignPayload`](/api/TxEnvelopeEip1559/getSignPayload) — the keccak256 hash of the presign serialization — to a signer such as [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), // [!code hl] privateKey: '0x...', }) // @log: { r: '0x...', s: '0x...', yParity: 0 } ``` ### Attach the Signature & Serialize Attach the signature with `TxEnvelopeEip1559.from`, then serialize into RLP-encoded form with [`TxEnvelopeEip1559.serialize`](/api/TxEnvelopeEip1559/serialize). `deserialize` reverses the transformation. ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...', }) // Attach the signature to the envelope. // @log: { ..., r: '0x...', s: '0x...', yParity: 0 } const signed = TxEnvelopeEip1559.from(envelope, { signature }) // [!code hl] // Serialize the signed envelope. // @log: '0x02f8730145843b9aca00...' const serialized = TxEnvelopeEip1559.serialize(signed) // [!code hl] // Deserialize it back into a typed envelope. const deserialized = TxEnvelopeEip1559.deserialize(serialized) ``` ### Broadcast via `eth_sendRawTransaction` Serialize the envelope with its signature, then broadcast it over JSON-RPC. The example below uses [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to send a `eth_sendRawTransaction` request over HTTP. ```ts twoslash import { RpcTransport, Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...', }) // Serialize the envelope with the signature. const serialized = TxEnvelopeEip1559.serialize(envelope, { signature }) // Broadcast the envelope with `eth_sendRawTransaction`. const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const hash = await transport.request({ method: 'eth_sendRawTransaction', // [!code hl] params: [serialized], // [!code hl] }) ``` If an application also needs to fill fields, wait for receipts, or retry, a client like [Viem](https://viem.sh/docs/actions/wallet/sendRawTransaction) handles that on top of the same primitives. ### Sign Remotely (Wallets & Signing Servers) The recipes above manually fill and sign the transaction. When a wallet — or more generally an entity responsible for filling and signing transactions — manages the account, skip that ceremony with the `eth_sendTransaction` RPC method. The example below uses an [EIP-1193 Provider](/api/Provider) to interact with a browser extension wallet; a `RpcTransport.fromHttp` works the same way if a backend supports `eth_sendTransaction`. ```ts twoslash import 'ox/window' import { Provider, TxEnvelopeEip1559, Value } from 'ox' // Construct the envelope. The wallet fills nonce, gas & fees. const envelope = TxEnvelopeEip1559.from({ chainId: 1, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) // Convert the envelope to an RPC-compatible format. const envelope_rpc = TxEnvelopeEip1559.toRpc(envelope) // [!code hl] // Broadcast the envelope with `eth_sendTransaction`. const provider = Provider.from(window.ethereum) const hash = await provider.request({ method: 'eth_sendTransaction', params: [envelope_rpc], }) ``` ## Best Practices ### Fill Every Field Before Signing Ox never reads chain state. A transaction signed with a stale `nonce` or an underpriced `maxFeePerGas` serializes fine but will be rejected or stall at broadcast. Fetch `nonce` (`eth_getTransactionCount`), estimate `gas` (`eth_estimateGas`), and derive fees before signing. ### Always Sign the Sign Payload `getSignPayload` hashes the type-prefixed presign serialization. Signing anything else — the raw serialized bytes, or a hash computed by hand — produces a signature the network will attribute to a different transaction (or no valid sender at all). ### Prefer `eth_sendTransaction` for Wallet-Managed Accounts When the key lives in a wallet or signing server, send an RPC-formatted envelope with `eth_sendTransaction` instead of exporting key material or replicating the wallet's fee-filling logic. ## See More # Choose an Envelope Type ## Overview Ox supports the five core Ethereum transaction envelope types, each with a dedicated module. They share the same lifecycle functions (`from`, `getSignPayload`, `serialize`, `toRpc`), so switching types means changing which fields you supply. The generic [`TransactionEnvelope`](/api/TransactionEnvelope) module dispatches to the right type automatically, inferring it from the envelope's shape when `type` is omitted. | Module | EIP | Type | Fee fields | Adds | | --------------------------------------------- | --------------------------------------------------- | ------ | -------------------------------------- | --------------------------------- | | [`TxEnvelopeLegacy`](/api/TxEnvelopeLegacy) | — | `0x00` | `gasPrice` | Original, pre-typed format | | [`TxEnvelopeEip2930`](/api/TxEnvelopeEip2930) | [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) | `0x01` | `gasPrice` | `accessList` | | [`TxEnvelopeEip1559`](/api/TxEnvelopeEip1559) | [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) | `0x02` | `maxFeePerGas`, `maxPriorityFeePerGas` | Dynamic fee market | | [`TxEnvelopeEip4844`](/api/TxEnvelopeEip4844) | [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) | `0x03` | EIP-1559 fields + `maxFeePerBlobGas` | `blobVersionedHashes`, `sidecars` | | [`TxEnvelopeEip7702`](/api/TxEnvelopeEip7702) | [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) | `0x04` | EIP-1559 fields | `authorizationList` | ## Recipes ### Send a Legacy Transaction Use [`TxEnvelopeLegacy`](/api/TxEnvelopeLegacy) for chains or tooling that predate typed transactions. Legacy envelopes price gas with a single `gasPrice` field. ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), // [!code hl] nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1'), }) ``` Omitting `chainId` produces a pre-EIP-155 transaction that is replayable across chains — only do that deliberately. ### Warm Storage with an EIP-2930 Access List Use [`TxEnvelopeEip2930`](/api/TxEnvelopeEip2930) to declare the addresses and storage slots a transaction will touch, pre-warming them at a discount. ```ts twoslash import { TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ // [!code hl:start] accessList: [ { address: '0x1e0049783f008a0085193e00003d00cd54003c71', storageKeys: [ '0x0000000000000000000000000000000000000000000000000000000000000001', ], }, ], // [!code hl:end] chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1'), }) ``` EIP-1559, EIP-4844, and EIP-7702 envelopes also accept `accessList` — see [Estimate Fees & Access Lists](/guides/transactions/fees-access-lists) for building them. ### Pay Dynamic Fees with EIP-1559 Use [`TxEnvelopeEip1559`](/api/TxEnvelopeEip1559) — the default choice on post-London networks. The sender caps total spend with `maxFeePerGas` and tips the block producer with `maxPriorityFeePerGas`. ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), // [!code hl] maxPriorityFeePerGas: Value.fromGwei('1'), // [!code hl] nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) ``` See [Build, Sign & Send](/guides/transactions/build-sign-send) for the full lifecycle of this envelope. ### Carry Blob Data with EIP-4844 Use [`TxEnvelopeEip4844`](/api/TxEnvelopeEip4844) to post data blobs (typically rollup batches). The envelope commits to its blobs through `blobVersionedHashes` and prices blob space with a separate `maxFeePerBlobGas` market. ```ts twoslash import { TxEnvelopeEip4844, Value } from 'ox' const envelope = TxEnvelopeEip4844.from({ // [!code hl:start] blobVersionedHashes: [ '0x01a24709d3997e8b217fe5460aef10ee515513ceba0362bf2d02a3ba73d7cb09', ], maxFeePerBlobGas: Value.fromGwei('3'), // [!code hl:end] chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) ``` Computing versioned hashes and sidecars requires a KZG implementation — see [Send Blob Transactions (EIP-4844)](/guides/transactions/blobs) for the complete flow. ### Delegate Code with EIP-7702 Use [`TxEnvelopeEip7702`](/api/TxEnvelopeEip7702) to set contract code on Externally Owned Accounts via a signed `authorizationList`. ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' // Sign an authorization over the delegate contract. const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 40n, }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...', }) const envelope = TxEnvelopeEip7702.from({ authorizationList: [Authorization.from(authorization, { signature })], // [!code hl] chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1'), }) ``` See [Delegate with EIP-7702](/guides/transactions/eip-7702) for authorization semantics and RPC conversion. ## Best Practices ### Default to EIP-1559 Unless a transaction needs blobs, delegations, or targets a chain without the London fee market, use `TxEnvelopeEip1559`. Legacy and EIP-2930 envelopes overpay whenever `gasPrice` is set above the block's base fee plus a competitive tip. ### Let the Envelope Shape Pick the Type When handling heterogeneous input, use the generic [`TransactionEnvelope.from`](/api/TransactionEnvelope/from): it infers `eip1559` from `maxFeePerGas`, `eip4844` from blob fields, `eip7702` from `authorizationList`, and so on — no manual `type` bookkeeping. ## See More # Delegate with EIP-7702 ## Overview [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) lets an Externally Owned Account set contract code for itself by signing an authorization over a delegate contract's address. The [`Authorization`](/api/Authorization) module creates, signs, and converts authorization tuples; [`TxEnvelopeEip7702`](/api/TxEnvelopeEip7702) carries a list of them in a type-4 transaction. The account that signs an authorization (the authority) and the account that sends the transaction (the sponsor) can be different — that is what enables gas sponsorship. ## Recipes ### Sign an Authorization Construct the authorization with [`Authorization.from`](/api/Authorization/from), sign its [`getSignPayload`](/api/Authorization/getSignPayload) — `keccak256('0x05' || rlp([chain_id, address, nonce]))` — with the **authority's** private key, then attach the signature. ```ts twoslash import { Authorization, Secp256k1 } from 'ox' // `address` is the delegate contract; `nonce` is the authority's // nonce at execution time. const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code hl] chainId: 1, nonce: 40n, }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...', // the authority's private key }) const authorization_signed = Authorization.from(authorization, { signature }) // @log: { address: '0x7099...79c8', chainId: 1, nonce: 40n, r: '0x...', s: '0x...', yParity: 1 } ``` ### Build a 7702 Envelope Attach one or more signed authorizations to a [`TxEnvelopeEip7702`](/api/TxEnvelopeEip7702), then sign and serialize the envelope with the **sender's** key like any other transaction. ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 40n, }) const authorization_signed = Authorization.from(authorization, { signature: Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...', // authority }), }) const envelope = TxEnvelopeEip7702.from({ authorizationList: [authorization_signed], // [!code hl] chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 0n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) // Sign & serialize with the sender's key, ready for `eth_sendRawTransaction`. const signature = Secp256k1.sign({ payload: TxEnvelopeEip7702.getSignPayload(envelope), privateKey: '0x...', // sender (sponsor) }) const serialized = TxEnvelopeEip7702.serialize(envelope, { signature }) ``` See [Build, Sign & Send](/guides/transactions/build-sign-send) for broadcasting the serialized envelope. ### Convert Authorization Lists for RPC Wallet-facing flows exchange authorizations in hex-quantity RPC format. Use [`Authorization.toRpcList`](/api/Authorization/toRpcList) before placing a list on an `eth_sendTransaction` request, and [`Authorization.fromRpcList`](/api/Authorization/fromRpcList) to restore typed values from RPC transactions. ```ts twoslash import { Authorization } from 'ox' declare const authorizationList: Authorization.ListSigned // Typed -> RPC (e.g. for `eth_sendTransaction` params). // @log: [{ address: '0x...', chainId: '0x1', nonce: '0x28', r: '0x...', s: '0x...', yParity: '0x1' }] const authorizationList_rpc = Authorization.toRpcList(authorizationList) // RPC -> typed (e.g. from `eth_getTransactionByHash` results). const authorizationList_typed = Authorization.fromRpcList(authorizationList_rpc) // @log: [{ address: '0x...', chainId: 1, nonce: 40n, r: '0x...', s: '0x...', yParity: 1 }] ``` ## Best Practices ### Mind the Authority's Nonce An authorization is only valid while its `nonce` equals the authority's account nonce at execution. When the authority sends its own delegation transaction, the transaction nonce is consumed first — sign the authorization with the account nonce **plus one**. ### Scope Delegations by Chain A `chainId` of `0` makes the authorization valid on every chain. Prefer an explicit chain ID unless a cross-chain delegation is exactly what you intend. ### Revoke by Delegating to the Zero Address Delegations persist until replaced. To clear an account's code, sign a new authorization whose `address` is `0x0000000000000000000000000000000000000000`. ## See More # Estimate Fees & Access Lists ## Overview The [`Fee`](/api/Fee) module converts `eth_feeHistory` payloads and provides the fee math for EIP-1559 transactions; [`AccessList`](/api/AccessList) converts access lists between their object and serialized tuple forms. Both operate on plain data — pair them with an [`RpcTransport`](/api/RpcTransport) when values come from a node. ## Recipes ### Compute the Effective Gas Price Use [`Fee.effectiveGasPrice`](/api/Fee/effectiveGasPrice) to determine what an EIP-1559 transaction actually pays per gas: `min(maxFeePerGas, baseFeePerGas + maxPriorityFeePerGas)`. ```ts twoslash import { Fee, Value } from 'ox' const effectiveGasPrice = Fee.effectiveGasPrice({ baseFeePerGas: Value.fromGwei('20'), maxFeePerGas: Value.fromGwei('30'), maxPriorityFeePerGas: Value.fromGwei('2'), }) // @log: 22000000000n (22 gwei = base fee + tip, under the 30 gwei cap) ``` `maxFeePerGas` is a cap, not a price — the difference between the cap and the effective price is refunded to the sender. ### Estimate `maxFeePerGas` from Fee History Fetch recent blocks with `eth_feeHistory`, convert the payload with [`Fee.fromHistoryRpc`](/api/Fee/fromHistoryRpc), then derive a cap with [`Fee.estimateMaxFeePerGas`](/api/Fee/estimateMaxFeePerGas). The default multiplier doubles the base fee for headroom against base-fee bumps. ```ts twoslash import { Fee, RpcTransport, Value } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') // Fetch the last 5 blocks with the 20th-percentile priority fee. const history = Fee.fromHistoryRpc( await transport.request({ method: 'eth_feeHistory', // [!code hl] params: ['0x5', 'latest', [20]], }), ) // `baseFeePerGas` includes the *next* block's base fee as its last element. const baseFeePerGas = history.baseFeePerGas[history.baseFeePerGas.length - 1]! // Tip at the sampled percentile of the most recent block. const rewards = history.reward ?? [] const maxPriorityFeePerGas = rewards[rewards.length - 1]?.[0] ?? Value.fromGwei('1') const maxFeePerGas = Fee.estimateMaxFeePerGas({ baseFeePerGas, maxPriorityFeePerGas, }) // @log: baseFeePerGas * 2n + maxPriorityFeePerGas ``` Tune the headroom with `multiplierNumerator`/`multiplierDenominator` (e.g. `3n`/`2n` for 1.5x). ### Build & Serialize Access Lists Attach an access list of addresses and storage keys directly to an envelope — `serialize` converts it to tuple form automatically. Use [`AccessList.toTupleList`](/api/AccessList/toTupleList) and [`AccessList.fromTupleList`](/api/AccessList/fromTupleList) when working at the RLP boundary yourself. ```ts twoslash import { AccessList, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ // [!code hl:start] accessList: [ { address: '0x1e0049783f008a0085193e00003d00cd54003c71', storageKeys: [ '0x0000000000000000000000000000000000000000000000000000000000000001', ], }, ], // [!code hl:end] chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) // Object form <-> serialized tuple form. // @log: [['0x1e0049783f008a0085193e00003d00cd54003c71', ['0x0000...0001']]] const tuples = AccessList.toTupleList(envelope.accessList) const accessList = AccessList.fromTupleList(tuples) ``` Both directions validate address shape and enforce 32-byte storage keys, throwing `AccessList.InvalidStorageKeySizeError` on malformed input. ## Best Practices ### Give the Base Fee Headroom The base fee can rise 12.5% per block, and a transaction may not be included immediately. The 2x default multiplier in `Fee.estimateMaxFeePerGas` absorbs roughly six consecutive full blocks; lower it only when inclusion time is not critical. ### Access Lists Only Pay Off When Used Each listed address costs 2,400 gas and each storage key 1,900 gas up front. The discount only nets out if the transaction actually touches the listed slots — measure with real workloads before shipping EIP-2930-style lists. ## See More # Parse & Inspect Transactions ## Overview The generic [`TransactionEnvelope`](/api/TransactionEnvelope) module deserializes any raw transaction, inferring the envelope type from its serialized prefix. [`Transaction`](/api/Transaction), [`TransactionReceipt`](/api/TransactionReceipt), and [`TransactionRequest`](/api/TransactionRequest) convert the hex-quantity payloads that JSON-RPC returns into typed objects with `bigint` numerics — and back. ## Recipes ### Deserialize a Raw Transaction Use [`TransactionEnvelope.deserialize`](/api/TransactionEnvelope/deserialize) on any raw transaction — from a mempool feed, `eth_getRawTransactionByHash`, or a signing service — and get back a typed envelope. ```ts twoslash import { TransactionEnvelope } from 'ox' const envelope = TransactionEnvelope.deserialize( '0x02f8730145843b9aca008502540be4008252089470997970c51812dc3a010c7d01b50e0d17dc79c88814d1120d7b16000080c080a098ebfabbfbe83dfcdc039bd68cce49cb55bb3d32e58f5caef200d3cb6e10351ca07b39f98ad5f89675a2e5014331283c79f3744e857681a03f445a802023297de9', ) // @log: { // @log: chainId: 1, // @log: gas: 21000n, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: nonce: 69n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: type: 'eip1559', // @log: value: 1500000000000000000n, // @log: r: '0x...', s: '0x...', yParity: 0, // @log: } ``` Use [`TransactionEnvelope.validate`](/api/TransactionEnvelope/validate) first when the input is untrusted and a `boolean` is preferable to a thrown error. ### Recover the Sender Address Extract the signature from a signed envelope, then recover the signer of the envelope's sign payload with [`Secp256k1.recoverAddress`](/api/Secp256k1/recoverAddress). ```ts twoslash import { Secp256k1, Signature, TransactionEnvelope } from 'ox' const envelope = TransactionEnvelope.deserialize( '0x02f8730145843b9aca008502540be4008252089470997970c51812dc3a010c7d01b50e0d17dc79c88814d1120d7b16000080c080a098ebfabbfbe83dfcdc039bd68cce49cb55bb3d32e58f5caef200d3cb6e10351ca07b39f98ad5f89675a2e5014331283c79f3744e857681a03f445a802023297de9', ) const signature = Signature.extract(envelope) if (!signature) throw new Error('transaction is unsigned') const sender = Secp256k1.recoverAddress({ payload: TransactionEnvelope.getSignPayload(envelope), // [!code hl] signature, }) // @log: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8' ``` ### Convert RPC Transactions & Receipts Pipe `eth_getTransactionByHash` and `eth_getTransactionReceipt` results through [`Transaction.fromRpc`](/api/Transaction/fromRpc) and [`TransactionReceipt.fromRpc`](/api/TransactionReceipt/fromRpc) to get typed objects — hex quantities become `bigint`/`number`, `type` becomes `'eip1559'`-style strings, and `status` becomes `'success' | 'reverted'`. ```ts twoslash import { RpcTransport, Transaction, TransactionReceipt } from 'ox' const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const hash = '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0' const transaction = await transport .request({ method: 'eth_getTransactionByHash', params: [hash] }) // @log: { ..., gas: 278365n, type: 'eip1559', value: 700000000000000000n } .then(Transaction.fromRpc) // [!code hl] const receipt = await transport .request({ method: 'eth_getTransactionReceipt', params: [hash] }) .then(TransactionReceipt.fromRpc) // [!code hl] // @log: { ..., gasUsed: 175034n, status: 'success', type: 'eip1559' } ``` Both converters pass `null` through, so missing transactions stay `null` rather than throwing. ### Prepare a Transaction Request Flatten an envelope into a [`TransactionRequest`](/api/TransactionRequest) with [`TransactionEnvelope.toTransactionRequest`](/api/TransactionEnvelope/toTransactionRequest), then serialize it to RPC form for methods like `eth_estimateGas`, `eth_call`, or `eth_sendTransaction`. ```ts twoslash import { RpcTransport, TransactionEnvelope, TransactionRequest, Value, } from 'ox' const envelope = TransactionEnvelope.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5'), }) const request = TransactionRequest.toRpc( TransactionEnvelope.toTransactionRequest(envelope), // [!code hl] ) const transport = RpcTransport.fromHttp('https://1.rpc.thirdweb.com') const gas = await transport.request({ method: 'eth_estimateGas', params: [request], }) // @log: '0x5208' ``` The reverse direction works too: normalize wallet input with [`TransactionRequest.fromRpc`](/api/TransactionRequest/fromRpc), then promote it to a signable envelope with [`TransactionRequest.toEnvelope`](/api/TransactionRequest/toEnvelope). ## Best Practices ### Validate Untrusted Payloads Before Use Raw transactions from user input or third-party feeds may be truncated or carry an unknown type prefix. Gate on `TransactionEnvelope.validate` (or catch `TransactionEnvelope.InvalidSerializedError`) before acting on the decoded fields. ### Convert at the RPC Boundary Once JSON-RPC encodes every quantity as hex strings. Run `fromRpc` converters immediately after each request — and `toRpc` immediately before — so the rest of the application only ever handles typed `bigint` values. ## See More # Send Blob Transactions (EIP-4844) ## Overview [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) transactions carry data in blobs that are committed to with KZG proofs and pruned after the availability window. The [`Blobs`](/api/Blobs) module packs data into blobs and computes their commitments and proofs; [`BlobCells`](/api/BlobCells) handles PeerDAS ([EIP-7594](https://eips.ethereum.org/EIPS/eip-7594)) cells; [`TxEnvelopeEip4844`](/api/TxEnvelopeEip4844) builds the type-3 envelope. KZG operations use Ox's opt-in WASM implementation ([`Kzg`](/wasm/crypto/Kzg)) — see the [WASM KZG guide](/guides/runtime/kzg) for setup, memory, and disposal details. ## Recipes ### Turn Data into Blobs Use [`Blobs.from`](/api/Blobs/from) to pack arbitrary data into 131,072-byte blobs, and [`Blobs.toHex`](/api/Blobs/toHex) to recover the original data. No KZG implementation is needed for this step. ```ts twoslash import { Blobs } from 'ox' const blobs = Blobs.from('0xdeadbeef') // [!code hl] // @log: ['0x00de00adbe...'] (one 131,072-byte blob) const data = Blobs.toHex(blobs) // @log: '0xdeadbeef' ``` A single transaction can carry up to six blobs (761,855 bytes of payload after field-element encoding); `Blobs.from` throws `Blobs.BlobSizeTooLargeError` beyond that. ### Compute Commitments & Proofs Create a KZG instance from the Ethereum trusted setup, then derive one commitment per blob with [`Blobs.toCommitments`](/api/Blobs/toCommitments), 128 cell proofs per blob with [`Blobs.toCellProofs`](/api/Blobs/toCellProofs), and the versioned hashes that anchor the envelope with [`Blobs.commitmentsToVersionedHashes`](/api/Blobs/commitmentsToVersionedHashes). ```ts twoslash import { Blobs } from 'ox' import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet }) const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) // [!code hl] // @log: 128 cell proofs per blob (EIP-7594) const cellProofs = Blobs.toCellProofs(blobs, { kzg }) // [!code hl] const blobVersionedHashes = Blobs.commitmentsToVersionedHashes(commitments) // @log: ['0x01a24709d3997e8b217fe5460aef10ee515513ceba0362bf2d02a3ba73d7cb09'] kzg.dispose() ``` Only the versioned hashes go into the signed envelope; blobs, commitments, and cell proofs travel alongside it as sidecars. ### Build a 4844 Envelope with Sidecars Construct the envelope from the versioned hashes, sign it, then serialize with `sidecars` to produce the 5-element `PooledTransactions` network wrapper that `eth_sendRawTransaction` expects. ```ts twoslash import { Blobs, Secp256k1, TxEnvelopeEip4844, Value } from 'ox' import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet }) const blobs = Blobs.from('0xdeadbeef') const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes: Blobs.toVersionedHashes(blobs, { kzg }), // [!code hl] chainId: 1, gas: 21_000n, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 0n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip4844.getSignPayload(envelope), privateKey: '0x...', }) // Serialize into the PeerDAS network wrapper, ready for `eth_sendRawTransaction`. const serialized = TxEnvelopeEip4844.serialize(envelope, { signature, sidecars: { // [!code hl:start] blobs, commitments: Blobs.toCommitments(blobs, { kzg }), cellProofs: Blobs.toCellProofs(blobs, { kzg }), // [!code hl:end] }, }) kzg.dispose() ``` [`TxEnvelopeEip4844.hash`](/api/TxEnvelopeEip4844/hash) always operates on the bare envelope — the transaction hash never includes the sidecar wrapper. See [Build, Sign & Send](/guides/transactions/build-sign-send) for broadcasting. ### Verify Blob Cells (PeerDAS) Split a blob into its 128 extended cells with [`BlobCells.fromBlob`](/api/BlobCells/fromBlob), then check any subset of cells against the blob's commitment with [`BlobCells.verify`](/api/BlobCells/verify). ```ts twoslash import { BlobCells, Blobs } from 'ox' import { Setups } from 'ox/trusted-setups' import { Kzg } from 'ox/wasm' const kzg = await Kzg.create({ trustedSetup: Setups.mainnet }) const [blob] = Blobs.from('0xdeadbeef') const [commitment] = Blobs.toCommitments([blob], { kzg }) const { cells, proofs } = BlobCells.fromBlob(blob, { kzg }) // [!code hl] // @log: 128 cells (2,048 bytes each), 128 proofs const valid = BlobCells.verify({ cells, cellIndices: cells.map((_, i) => i), commitments: cells.map(() => commitment), proofs, kzg, }) // @log: true kzg.dispose() ``` Any 64 of the 128 cells are enough to reconstruct the rest with [`BlobCells.recover`](/api/BlobCells/recover). ## Best Practices ### Reuse and Dispose KZG Instances Each `Kzg.create` call owns separate WASM memory (8 MiB and up). Create one instance per worker, reuse it across operations, and call `dispose` when done — see the [WASM KZG guide](/guides/runtime/kzg) for ownership details. ### Blobs Are Not Permanent Storage Blob data is pruned after the availability window and is never readable from the EVM — only the versioned hashes remain on-chain. Persist the underlying data elsewhere if it must stay retrievable. ### Budget Blob Gas Separately `maxFeePerBlobGas` prices a fee market independent of execution gas. A transaction can be priced competitively on one market and stall on the other, so derive both caps before signing. ## See More # WebAuthn & Passkeys ## Overview The [Web Authentication API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API) exposes P256 credentials held by authenticators such as [Passkeys](https://passkeys.dev) and [YubiKeys](https://en.wikipedia.org/wiki/YubiKey), so users can sign transactions and arbitrary payloads without handling a raw private key. Combined with Account Abstraction, Smart Contract Accounts can verify these signatures onchain via mechanisms such as [ERC-1271](https://eips.ethereum.org/EIPS/eip-1271). The [`WebAuthn`](/api/WebAuthn) module covers the credential lifecycle end-to-end, while the [`ox/webauthn` entrypoint](/webauthn) adds granular registration, authentication, and server-side verification utilities. ```ts twoslash import { WebAuthn } from 'ox' // 1. Register a credential (ie. passkey). const credential = await WebAuthn.createCredential({ name: 'Example' }) // 2. Sign a challenge with the credential. const { metadata, signature } = await WebAuthn.sign({ challenge: '0xdeadbeef', credentialId: credential.id, }) // 3. Verify the signature. const verified = WebAuthn.verify({ challenge: '0xdeadbeef', metadata, publicKey: credential.publicKey, signature, }) // @log: true ``` ## Guides # Derive Secrets with PRF ## Overview The WebAuthn [PRF extension](https://w3c.github.io/webauthn/#prf-extension) evaluates a pseudo-random function bound to a credential, returning a 32-byte output for a given input. The same credential and input always reproduce the same output, which makes it usable as deterministic key material. The [`Prf`](/api/Prf) module builds PRF inputs ("tags"), [`WebAuthn`](/api/WebAuthn) requests outputs from a credential, and [`Secp256k1`](/api/Secp256k1), [`Ed25519`](/api/Ed25519), [`MlDsa44`](/api/MlDsa44), and [`AesGcm`](/api/AesGcm) each expose a `fromPrf` function that derives an independent key from the same output using a fixed derivation domain. :::warning Treat PRF outputs and derived keys as secrets. Do not serialize or send the raw WebAuthn credential or response. PRF-derived keys are software keys. Code on any origin allowed to use the same RP ID can request the same output after user verification. ::: ## Recipes ### Configure a PRF Tag & Request Outputs Encode an application-owned PRF input with [`Prf.tag`](/api/Prf/tag) and pass it when creating or requesting a credential. Using the same tag with the same credential reproduces the output. ```ts twoslash import { Prf, WebAuthn } from 'ox' // Register a credential with PRF evaluation enabled. const credential = await WebAuthn.createCredential({ name: 'Example', prf: Prf.tag('account.1'), // [!code hl] }) // Later — reproduce the same 32-byte output from the stored credential. const { prf } = await WebAuthn.getCredential({ credentialId: credential.id, prf: Prf.tag('account.1'), // [!code hl] }) ``` Passing `prf: true` instead of a tag uses the stable default input `ox.webauthn.prf.v1`. Tags are public identifiers — store them alongside the credential metadata, not as secrets. ### Sign Ethereum Transactions with a Passkey-Derived Key Derive a secp256k1 private key with [`Secp256k1.fromPrf`](/api/Secp256k1/fromPrf) and use it like any other Ethereum key — here, to sign an EIP-1559 transaction envelope. ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value, WebAuthn } from 'ox' const { prf } = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: true, }) const privateKey = Secp256k1.fromPrf(prf) // [!code hl] const envelope = TxEnvelopeEip1559.from({ chainId: 1, gas: 21_000n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 0n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('0.1'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey, }) const serialized = TxEnvelopeEip1559.serialize(envelope, { signature }) ``` Broadcast `serialized` with `eth_sendRawTransaction` — see [Build, Sign & Send](/guides/transactions/build-sign-send). ### Derive an Ed25519 Key Derive an Ed25519 private key with [`Ed25519.fromPrf`](/api/Ed25519/fromPrf) for off-chain signing (session tokens, sync payloads, non-EVM chains). ```ts twoslash import { Ed25519, WebAuthn } from 'ox' const { prf } = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: true, }) const privateKey = Ed25519.fromPrf(prf) // [!code hl] const publicKey = Ed25519.getPublicKey({ privateKey }) const signature = Ed25519.sign({ payload: '0xdeadbeef', privateKey, }) ``` ### Derive a Post-Quantum Key Derive an ML-DSA-44 (FIPS 204) private key with [`MlDsa44.fromPrf`](/api/MlDsa44/fromPrf) to sign with a post-quantum scheme. ```ts twoslash import { MlDsa44, WebAuthn } from 'ox' const { prf } = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: true, }) const privateKey = MlDsa44.fromPrf(prf) // [!code hl] const publicKey = MlDsa44.getPublicKey({ privateKey }) const signature = MlDsa44.sign({ payload: '0xdeadbeef', privateKey, }) const verified = MlDsa44.verify({ payload: '0xdeadbeef', publicKey, signature, }) // @log: true ``` ### Encrypt User Data Derive an AES-256-GCM key with [`AesGcm.fromPrf`](/api/AesGcm/fromPrf) to encrypt data that only the passkey holder can decrypt. Unlike the signing variants, `AesGcm.fromPrf` is **async** — it returns a non-extractable Web Crypto `CryptoKey`. ```ts twoslash import { AesGcm, Hex, WebAuthn } from 'ox' const { prf } = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: true, }) const key = await AesGcm.fromPrf(prf) // [!code hl] const secret = Hex.fromString('i am a secret message') const encrypted = await AesGcm.encrypt(secret, key) const decrypted = await AesGcm.decrypt(encrypted, key) // @log: Hex.fromString('i am a secret message') ``` ## Best Practices ### Namespace Tags per Purpose Use one tag per account or feature (e.g. `account.1`, `backup.v2`) so keys stay independent and rotatable. Each `fromPrf` module already applies its own derivation domain, so the same output safely feeds different algorithms. ### Re-Derive Instead of Persisting PRF outputs are reproducible on demand after user verification. Re-derive keys when needed rather than storing private keys, and keep derived material out of logs, storage, and analytics. ### Plan for Credential Loss A PRF output is bound to a single credential — losing the passkey loses every key derived from it. Register a backup credential or escrow encrypted recovery material before deriving long-lived keys. ## See More # Register & Authenticate Credentials ## Overview A WebAuthn credential is created once (registration) and asserted many times (authentication). The [`ox/webauthn` entrypoint](/webauthn) mirrors this split: [`Registration`](/webauthn/webauthn/Registration) creates and verifies credentials, [`Authentication`](/webauthn/webauthn/Authentication) requests and verifies assertions, and [`Credential`](/webauthn/webauthn/Credential) serializes credentials for transport and storage. [`Authenticator`](/webauthn/webauthn/Authenticator) constructs raw authenticator payloads (mostly for testing), and [`CoseKey`](/api/CoseKey) converts the COSE-encoded public keys found in attestation objects. ## Recipes ### Register a Credential Create a credential with a server-issued challenge, then serialize it so it can be sent to the server for verification and storage. ```ts twoslash import { Credential, Registration } from 'ox/webauthn' // Challenge generated and stored by the server for this registration. const challenge = '0x69abb4b5a0de4bc62a2a201f8d25bae9' const credential = await Registration.create({ challenge, name: 'Example', // [!code hl] }) // Serialize for transmission — `Credential.serialize` converts binary // fields to base64url/hex strings. const serialized = Credential.serialize(credential) const body = JSON.stringify(serialized) ``` The returned `credential.id` and `credential.publicKey` identify the credential in later ceremonies — persist them client-side if you plan to verify locally. ### Authenticate an Existing Credential Request an assertion for a known credential ID with a fresh server-issued challenge, then check it against the public key stored at registration. ```ts twoslash import { PublicKey } from 'ox' import { Authentication } from 'ox/webauthn' // Public key stored by the server at registration. declare const publicKey: PublicKey.PublicKey // Challenge issued by the server for this login attempt. const challenge = '0x1e0c4b8b5f14ec863da1e7f0d3a58b8a' const response = await Authentication.sign({ challenge, credentialId: 'oZ48...', // [!code hl] }) const valid = Authentication.verify({ challenge, metadata: response.metadata, origin: 'https://example.com', publicKey, rpId: 'example.com', signature: response.signature, }) // @log: true ``` To verify on a server, send the assertion with [`Authentication.serializeResponse`](/webauthn/webauthn/Authentication) and rehydrate it with `Authentication.deserializeResponse` before calling `verify`. ### Verify Registration on the Server Deserialize the credential received from the client and validate the whole registration ceremony — challenge, origin, RP ID, authenticator flags, and attestation — with [`Registration.verify`](/webauthn/webauthn/Registration). ```ts twoslash import { Credential, Registration } from 'ox/webauthn' // Request body received from the client. declare const body: string const credential = Credential.deserialize(JSON.parse(body)) const result = Registration.verify({ credential, challenge: '0x69abb4b5a0de4bc62a2a201f8d25bae9', // [!code hl] origin: 'https://example.com', rpId: 'example.com', }) // @log: { // @log: credential: { // @log: id: 'oZ48...', // @log: publicKey: { prefix: 4, x: 51421...5123n, y: 12345...6789n }, // @log: }, // @log: counter: 0, // @log: userVerified: true, // @log: } ``` Store `result.credential.id`, `result.credential.publicKey`, and `result.counter` — they are the inputs for verifying future authentication ceremonies. ## Best Practices ### Issue Challenges Server-Side Generate a random, single-use challenge on the server for every ceremony and reject responses whose challenge does not match. A client-chosen challenge defeats replay protection. ### Pin Origin and RP ID Always pass `origin` and `rpId` when verifying on a server. They bind the ceremony to your site and stop credentials phished on look-alike origins from validating. ### Never Ship the Raw Credential Serialize with `Credential.serialize` rather than the native `toJSON()` — native output can include client extension results (such as PRF outputs), which are secrets. ## See More # Sign & Verify with Passkeys ## Overview Once a credential is registered, it can sign arbitrary challenges — transaction hashes, message digests, or login nonces. The [`WebAuthn`](/api/WebAuthn) module signs with [`WebAuthn.sign`](/api/WebAuthn/sign) and verifies with [`WebAuthn.verify`](/api/WebAuthn/verify), while [`PublicKey`](/api/PublicKey) serializes the credential's P256 public key for storage. ## Recipes ### Sign a Payload Hash the payload into a 32-byte challenge and sign it with a stored credential. The response contains the `signature` and the authenticator `metadata` required to verify it. ```ts twoslash import { Hash, Hex, WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const challenge = Hash.keccak256(Hex.fromString('hello world')) const { metadata, signature } = await WebAuthn.sign({ challenge, // [!code hl] credentialId: credential.id, }) ``` Omitting `credentialId` prompts the user to pick any credential previously registered for the origin. ### Extract the Public Key Serialize the credential's public key with [`PublicKey.toHex`](/api/PublicKey/toHex) so it can be persisted (or registered with an onchain verifier), and rehydrate it when verifying. ```ts twoslash import { PublicKey, WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const publicKey = PublicKey.toHex(credential.publicKey) // [!code hl] // @log: '0x04ab891400140fc4f8e941ce0ff90e419de9470acaca613bbd717a4775435031a7...' // Rehydrate the stored key when verifying. const restored = PublicKey.fromHex(publicKey) ``` ### Verify a Signature Verify the signature against the challenge, the credential's public key, and the authenticator metadata returned by `WebAuthn.sign`. ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const { metadata, signature } = await WebAuthn.sign({ challenge: '0xdeadbeef', credentialId: credential.id, }) const verified = WebAuthn.verify({ challenge: '0xdeadbeef', metadata, publicKey: credential.publicKey, signature, }) // @log: true ``` When verifying on a server, also pass `origin` and `rpId` so the client data and relying party binding are validated — see [Register & Authenticate Credentials](/guides/webauthn/credentials). ## Best Practices ### Bind Challenges to Content Derive the challenge from what is actually being authorized — e.g. `Hash.keccak256` of the serialized transaction or message — so a signature cannot be replayed for a different action. ### Keep the Metadata with the Signature `metadata` (authenticator data + client data JSON) is part of the signed material. Store and transmit it alongside the signature; verification is impossible without it. ### Verify Onchain via ERC-1271 Smart Contract Accounts can validate WebAuthn P256 signatures with [ERC-1271](https://eips.ethereum.org/EIPS/eip-1271)-style verifiers. Use [`WebAuthn.getSignPayload`](/webauthn/webauthn/Authentication/getSignPayload) when a contract expects the raw P256 digest instead of the WebAuthn envelope. ## See More # API Reference | Module | Description | | --- | --- | | **ABI** | | | [Abi](/api/Abi) | Utilities & types for working with [Application Binary Interfaces (ABIs)](https://docs.soliditylang.org/en/latest/abi-spec.html) | | [AbiConstructor](/api/AbiConstructor) | Utilities & types for working with [Constructors](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. | | [AbiError](/api/AbiError) | Utilities & types for working with [Errors](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. | | [AbiEvent](/api/AbiEvent) | Utilities & types for working with [Events](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. | | [AbiFunction](/api/AbiFunction) | Utilities & types for working with [Functions](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. | | [AbiItem](/api/AbiItem) | Utilities & types for working with [ABI Items](https://docs.soliditylang.org/en/latest/abi-spec.html#json) | | [AbiParameter](/api/AbiParameter) | Utilities & types for working with a single [ABI Parameter](https://docs.soliditylang.org/en/latest/abi-spec.html#types). | | [AbiParameters](/api/AbiParameters) | Utilities & types for encoding, decoding, and working with [ABI Parameters](https://docs.soliditylang.org/en/latest/abi-spec.html#types) | | **Addresses** | | | [Address](/api/Address) | Utility functions for working with Ethereum addresses. | | [ContractAddress](/api/ContractAddress) | Utility functions for computing Contract Addresses. | | **Authorization (EIP-7702)** | | | [Authorization](/api/Authorization) | Utility functions for working with [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization lists & tuples. | | **Binary State Tree (EIP-7864)** | | | [BinaryStateTree](/api/BinaryStateTree) | Utility functions for working with [EIP-7864](https://eips.ethereum.org/EIPS/eip-7864) Binary State Trees. | | **Blobs (EIP-4844)** | | | [Blobs](/api/Blobs) | Utility functions for working with [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Blobs. | | [Kzg](/api/Kzg) | Utility functions for working with KZG Commitments. | | **Blobs (EIP-7594)** | | | [BlobCells](/api/BlobCells) | Cell-level helpers for [PeerDAS (EIP-7594)](https://eips.ethereum.org/EIPS/eip-7594): deriving the 128 cells and cell KZG proofs of an extended blob, and verifying cell proofs against blob commitments. | | **Crypto** | | | [AesGcm](/api/AesGcm) | Utilities & types for working with AES-GCM encryption. Internally uses the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). | | [Bls](/api/Bls) | Utility functions for [BLS12-381](https://hackmd.io/@benjaminion/bls12-381) cryptography. | | [BlsPoint](/api/BlsPoint) | Utility functions for working with BLS12-381 points. | | [CoseKey](/api/CoseKey) | Utility functions for converting between COSE\_Key and P256 public keys. | | [Ed25519](/api/Ed25519) | Utilities for working with Ed25519 signatures and key pairs. | | [Engine](/api/Engine) | Functions for delegating ox's cryptography to a different implementation. | | [Hash](/api/Hash) | Utility functions for hashing (keccak256, sha256, etc). | | [HdKey](/api/HdKey) | Utility functions for generating and working with [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) HD Wallets. | | [Keystore](/api/Keystore) | Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage). | | [Mnemonic](/api/Mnemonic) | Utility functions for generating and working with [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonics. | | [P256](/api/P256) | Utility functions for [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) ECDSA cryptography. | | [Prf](/api/Prf) | Utilities for constructing credential-bound PRF configurations. | | [PublicKey](/api/PublicKey) | Utility functions for working with ECDSA public keys. | | [Secp256k1](/api/Secp256k1) | Utility functions for [secp256k1](https://www.secg.org/sec2-v2.pdf) ECDSA cryptography. | | [Signature](/api/Signature) | Utility functions for working with ECDSA signatures. | | [WebAuthn](/api/WebAuthn) | Utilities for WebAuthn credentials, credential-bound PRFs, and [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) signatures. | | [WebCryptoP256](/api/WebCryptoP256) | Utility functions for [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) ECDSA cryptography using the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) | | [X25519](/api/X25519) | Utilities for working with X25519 elliptic curve Diffie-Hellman key agreement. | | **Data** | | | [Base32](/api/Base32) | Utility functions for working with Base32 values using the [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) bech32 alphabet. | | [Base58](/api/Base58) | Utility functions for working with [Base58](https://digitalbazaar.github.io/base58-spec/) values. | | [Base64](/api/Base64) | Utility functions for working with [RFC-4648](https://datatracker.ietf.org/doc/html/rfc4648) Base64. | | [Bech32m](/api/Bech32m) | Utility functions for [BIP-350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki) bech32m encoding and decoding. | | [Bytes](/api/Bytes) | A set of Ethereum-related utility functions for working with [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) instances. | | [Cbor](/api/Cbor) | Functions for encoding and decoding CBOR (Concise Binary Object Representation) data. | | [CompactSize](/api/CompactSize) | Utility functions for [Bitcoin's CompactSize](https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) variable-length integer encoding. | | [Hex](/api/Hex) | A set of Ethereum-related utility functions for working with hexadecimal string values (e.g. `"0xdeadbeef"`). | | [Rlp](/api/Rlp) | Utility functions for encoding and decoding [Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/) structures. | | [Value](/api/Value) | Utility functions for displaying and parsing Ethereum Values as defined under **2.1. Value** in the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) | | **ENS** | | | [Ens](/api/Ens) | Utility functions for working with ENS names. | | **Execution Spec** | | | [AccessList](/api/AccessList) | Utilities & types for working with Access Lists as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4140e528360fea53c34a766d86a000c6c039100e/src/schemas/transaction.yaml#L73) | | [AccountProof](/api/AccountProof) | Utilities & types for working with Account Proofs as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml) | | [Block](/api/Block) | Utilities & types for working with Blocks as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml) | | [BlockOverrides](/api/BlockOverrides) | Utilities & types for working with **Block Overrides**. | | [Bloom](/api/Bloom) | Utility functions for working with Bloom Filters as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml) | | [Fee](/api/Fee) | Utility types for working with Ethereum transaction fees and fee history. | | [Filter](/api/Filter) | Utilities & types for working with Filters as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.yaml) | | [Log](/api/Log) | Utilities & types for working with Logs as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml) | | [StateOverrides](/api/StateOverrides) | Utilities & types for working with **State Overrides**. | | [Transaction](/api/Transaction) | Utilities & types for working with **Transactions** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml) | | [TransactionReceipt](/api/TransactionReceipt) | Utilities & types for working with **Transaction Receipts** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml) | | [TransactionRequest](/api/TransactionRequest) | Utilities & types for working with **Transaction Requests** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4aca1d7a3e5aab24c8f6437131289ad386944eaa/src/schemas/transaction.yaml#L358-L423) | | [Withdrawal](/api/Withdrawal) | Utilities & types for working with Withdrawals as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml) | | **JSON** | | | [Json](/api/Json) | Utility functions for working with JSON (with support for `bigint`). | | **JSON-RPC** | | | [RpcRequest](/api/RpcRequest) | Utility types & functions for working with [JSON-RPC 2.0 Requests](https://www.jsonrpc.org/specification#request_object) and Ethereum JSON-RPC methods as defined on the [Ethereum API specification](https://github.com/ethereum/execution-apis) | | [RpcResponse](/api/RpcResponse) | Utility types & functions for working with [JSON-RPC 2.0 Responses](https://www.jsonrpc.org/specification#response_object) | | [RpcSchema](/api/RpcSchema) | Utility types for working with Ethereum JSON-RPC namespaces & schemas. | | [RpcTransport](/api/RpcTransport) | Utility functions for working with JSON-RPC Transports. | | **Post Quantum** | | | [MlDsa44](/api/MlDsa44) | Utilities for working with ML-DSA-44 signatures and key pairs, as defined in [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final). | | **Providers (EIP-1193)** | | | [Provider](/api/Provider) | Utilities & types for working with [EIP-1193 Providers](https://eips.ethereum.org/EIPS/eip-1193) | | **Sign-In with Ethereum (EIP-4361)** | | | [Siwe](/api/Siwe) | Utility functions for working with [EIP-4361: Sign-In with Ethereum](https://eips.ethereum.org/EIPS/eip-4361) | | **Signed & Typed Data** | | | [PersonalMessage](/api/PersonalMessage) | Utilities & types for working with [EIP-191 Personal Messages](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e) | | [TypedData](/api/TypedData) | Utility functions for working with [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) | | [ValidatorData](/api/ValidatorData) | Utilities & types for working with [EIP-191 Validator Data](https://eips.ethereum.org/EIPS/eip-191#0x00) | | **Transaction Envelopes** | | | [TransactionEnvelope](/api/TransactionEnvelope) | Errors & Types for working with Transaction Envelopes. | | [TxEnvelopeEip1559](/api/TxEnvelopeEip1559) | Utility functions for working with [EIP-1559 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-1559) | | [TxEnvelopeEip2930](/api/TxEnvelopeEip2930) | Utility functions for working with [EIP-2930 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-2930) | | [TxEnvelopeEip4844](/api/TxEnvelopeEip4844) | Utility functions for working with [EIP-4844 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-4844) | | [TxEnvelopeEip7702](/api/TxEnvelopeEip7702) | Utility functions for working with [EIP-7702 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-7702) | | [TxEnvelopeLegacy](/api/TxEnvelopeLegacy) | Utility functions for working with **Legacy Transaction Envelopes**. | # API Reference | Module | Description | | --- | --- | | **WebAuthn** | | | [Authentication](/webauthn/webauthn/Authentication) | Utility functions and types for WebAuthn authentication ceremonies (signing and verification). | | [Authenticator](/webauthn/webauthn/Authenticator) | Utility functions for constructing and parsing authenticator data and client data JSON. | | [Credential](/webauthn/webauthn/Credential) | Utility functions and types for WebAuthn P256 credentials. | | [Registration](/webauthn/webauthn/Registration) | Utility functions and types for WebAuthn registration ceremonies (credential creation and verification). | # API Reference | Module | Description | | --- | --- | | **ERC-4337** | | | [EntryPoint](/ercs/erc4337/EntryPoint) | Utility functions and types for working with [ERC-4337 EntryPoints](https://eips.ethereum.org/EIPS/eip-4337). | | [RpcSchema](/ercs/erc4337/RpcSchema) | Utility types for working with ERC-4337 JSON-RPC schemas. | | [UserOperation](/ercs/erc4337/UserOperation) | Utility functions and types for working with [ERC-4337 User Operations](https://eips.ethereum.org/EIPS/eip-4337). | | [UserOperationGas](/ercs/erc4337/UserOperationGas) | Utility functions and types for working with [ERC-4337 User Operation Gas](https://eips.ethereum.org/EIPS/eip-4337). | | [UserOperationReceipt](/ercs/erc4337/UserOperationReceipt) | Utility functions and types for working with [ERC-4337 User Operation Receipts](https://eips.ethereum.org/EIPS/eip-4337). | | **ERC-6492** | | | [SignatureErc6492](/ercs/erc6492/SignatureErc6492) | Utility functions for working with [ERC-6492 wrapped signatures](https://eips.ethereum.org/EIPS/eip-6492#specification). | | **ERC-7821** | | | [Calls](/ercs/erc7821/Calls) | Utility functions for encoding and decoding [ERC-7821](https://eips.ethereum.org/EIPS/eip-7821) calls. | | [Execute](/ercs/erc7821/Execute) | Utility functions for encoding and decoding [ERC-7821](https://eips.ethereum.org/EIPS/eip-7821) `execute` function data. | | **ERC-8010** | | | [SignatureErc8010](/ercs/erc8010/SignatureErc8010) | Utility functions for working with [ERC-8010 wrapped signatures](https://eips.ethereum.org/EIPS/eip-8010#specification). | | **ERC-8021** | | | [Attribution](/ercs/erc8021/Attribution) | Utility functions for working with [ERC-8021 Transaction Attribution](https://eip.tools/eip/8021). | # Overview Ox provides low-level, type-safe primitives for constructing, signing, serializing, and inspecting Tempo protocol data. Explore the guides for common workflows, or browse the API reference for individual modules. ## Guides ## API Reference | Module | Description | | --- | --- | | **Reference** | | | [AuthorizationTempo](/tempo/reference/AuthorizationTempo) | Utilities for Tempo-flavoured EIP-7702 authorizations. | | [Channel](/tempo/reference/Channel) | TIP-20 channel reserve descriptor, constants, and deterministic hashing utilities. | | [EarnShares](/tempo/reference/EarnShares) | Tempo Earn `VaultAdapter` share math: raw vault-share and venue-share conversions at the anchor rate, the dilution-correct fee-share formula, and the `minimumOutput` slippage floor. | | [KeyAuthorization](/tempo/reference/KeyAuthorization) | Tempo key authorization utilities for provisioning and signing access keys. | | [MultisigConfig](/tempo/reference/MultisigConfig) | Native multisig account utilities (TIP-1061). | | [Period](/tempo/reference/Period) | Utilities for constructing period durations (in seconds) for recurring spending limits. | | [PoolId](/tempo/reference/PoolId) | Pool ID utilities for computing pool identifiers from token pairs. | | [ReceivePolicyReceipt](/tempo/reference/ReceivePolicyReceipt) | TIP-1028 receive-policy claim receipt utilities. | | [RpcSchemaTempo](/tempo/reference/RpcSchemaTempo) | Union of all JSON-RPC Methods for the `tempo_` namespace. | | [SignatureEnvelope](/tempo/reference/SignatureEnvelope) | Signature envelope utilities for secp256k1, P256, WebAuthn, and keychain signatures. | | [Tick](/tempo/reference/Tick) | Tick-based pricing utilities for DEX price conversions. | | [TokenRole](/tempo/reference/TokenRole) | Token role utilities for serializing role identifiers to keccak256 hashes. | | [Transaction](/tempo/reference/Transaction) | Utilities for converting between RPC and structured transaction formats. | | [TransactionReceipt](/tempo/reference/TransactionReceipt) | Utilities for working with Tempo transaction receipts. | | [TransactionRequest](/tempo/reference/TransactionRequest) | Utilities for preparing RPC-formatted transaction requests. | | [TxEnvelopeTempo](/tempo/reference/TxEnvelopeTempo) | Utilities for instantiating, serializing, and hashing Tempo transaction envelopes. | | [VirtualAddress](/tempo/reference/VirtualAddress) | TIP-1022 virtual address encoding and parsing utilities. | | [VirtualMaster](/tempo/reference/VirtualMaster) | TIP-1022 master registration utilities. | | [WithdrawalSenderTag](/tempo/reference/WithdrawalSenderTag) | Utilities for deriving the sender tag that correlates a Zone withdrawal with its indexed parent-chain `WithdrawalProcessed` event. | | [ZoneId](/tempo/reference/ZoneId) | Zone ID utilities for converting between zone IDs and zone chain IDs. | | [ZoneRpcAuthentication](/tempo/reference/ZoneRpcAuthentication) | Zone RPC authentication token utilities for private zone RPC access. | # API Reference | Module | Description | | --- | --- | | **JSON-RPC** | | | [z.RpcSchema](/zod/jsonrpc/RpcSchema) | z.RpcSchema schemas. | | [z.RpcSchema.from](/zod/jsonrpc/from) | z.RpcSchema.from schemas. | | **Schemas** | | | [Integers](/zod/schemas/Integers) | Integer quantity schemas exported directly from `z`. | | [z.Abi](/zod/schemas/Abi) | z.Abi schemas. | | [z.AbiConstructor](/zod/schemas/AbiConstructor) | z.AbiConstructor schemas. | | [z.AbiError](/zod/schemas/AbiError) | z.AbiError schemas. | | [z.AbiEvent](/zod/schemas/AbiEvent) | z.AbiEvent schemas. | | [z.AbiFallback](/zod/schemas/AbiFallback) | z.AbiFallback schemas. | | [z.AbiFunction](/zod/schemas/AbiFunction) | z.AbiFunction schemas. | | [z.AbiItem](/zod/schemas/AbiItem) | z.AbiItem schemas. | | [z.AbiParameter](/zod/schemas/AbiParameter) | z.AbiParameter schemas. | | [z.AbiParameters](/zod/schemas/AbiParameters) | z.AbiParameters schemas. | | [z.AbiReceive](/zod/schemas/AbiReceive) | z.AbiReceive schemas. | | [z.AccessList](/zod/schemas/AccessList) | z.AccessList schemas. | | [z.AccountProof](/zod/schemas/AccountProof) | z.AccountProof schemas. | | [z.Address](/zod/schemas/Address) | z.Address schemas. | | [z.Authorization](/zod/schemas/Authorization) | z.Authorization schemas. | | [z.Block](/zod/schemas/Block) | z.Block schemas. | | [z.BlockOverrides](/zod/schemas/BlockOverrides) | z.BlockOverrides schemas. | | [z.Bytes](/zod/schemas/Bytes) | z.Bytes schemas. | | [z.Fee](/zod/schemas/Fee) | z.Fee schemas. | | [z.Filter](/zod/schemas/Filter) | z.Filter schemas. | | [z.Hash](/zod/schemas/Hash) | z.Hash schemas. | | [z.Hex](/zod/schemas/Hex) | z.Hex schemas. | | [z.Log](/zod/schemas/Log) | z.Log schemas. | | [z.RpcResponse](/zod/schemas/RpcResponse) | z.RpcResponse schemas. | | [z.Signature](/zod/schemas/Signature) | z.Signature schemas. | | [z.Solidity](/zod/schemas/Solidity) | z.Solidity schemas. | | [z.StateOverrides](/zod/schemas/StateOverrides) | z.StateOverrides schemas. | | [z.Transaction](/zod/schemas/Transaction) | z.Transaction schemas. | | [z.TransactionEnvelope](/zod/schemas/TransactionEnvelope) | z.TransactionEnvelope schemas. | | [z.TransactionReceipt](/zod/schemas/TransactionReceipt) | z.TransactionReceipt schemas. | | [z.TransactionRequest](/zod/schemas/TransactionRequest) | z.TransactionRequest schemas. | | [z.TxEnvelopeEip1559](/zod/schemas/TxEnvelopeEip1559) | z.TxEnvelopeEip1559 schemas. | | [z.TxEnvelopeEip2930](/zod/schemas/TxEnvelopeEip2930) | z.TxEnvelopeEip2930 schemas. | | [z.TxEnvelopeEip4844](/zod/schemas/TxEnvelopeEip4844) | z.TxEnvelopeEip4844 schemas. | | [z.TxEnvelopeEip7702](/zod/schemas/TxEnvelopeEip7702) | z.TxEnvelopeEip7702 schemas. | | [z.TxEnvelopeLegacy](/zod/schemas/TxEnvelopeLegacy) | z.TxEnvelopeLegacy schemas. | | [z.TypedData](/zod/schemas/TypedData) | z.TypedData schemas. | | [z.Withdrawal](/zod/schemas/Withdrawal) | z.Withdrawal schemas. | | **Tempo** | | | [z.tempo.AuthorizationTempo](/zod/tempo/AuthorizationTempo) | z.tempo.AuthorizationTempo schemas. | | [z.tempo.KeyAuthorization](/zod/tempo/KeyAuthorization) | z.tempo.KeyAuthorization schemas. | | [z.tempo.MultisigConfig](/zod/tempo/MultisigConfig) | z.tempo.MultisigConfig schemas. | | [z.tempo.PoolId](/zod/tempo/PoolId) | z.tempo.PoolId schemas. | | [z.tempo.RpcSchemaTempo](/zod/tempo/RpcSchemaTempo) | z.tempo.RpcSchemaTempo schemas. | | [z.tempo.SignatureEnvelope](/zod/tempo/SignatureEnvelope) | z.tempo.SignatureEnvelope schemas. | | [z.tempo.Tick](/zod/tempo/Tick) | z.tempo.Tick schemas. | | [z.tempo.TokenRole](/zod/tempo/TokenRole) | z.tempo.TokenRole schemas. | | [z.tempo.Transaction](/zod/tempo/Transaction) | z.tempo.Transaction schemas. | | [z.tempo.TransactionReceipt](/zod/tempo/TransactionReceipt) | z.tempo.TransactionReceipt schemas. | | [z.tempo.TransactionRequest](/zod/tempo/TransactionRequest) | z.tempo.TransactionRequest schemas. | | [z.tempo.TxEnvelopeTempo](/zod/tempo/TxEnvelopeTempo) | z.tempo.TxEnvelopeTempo schemas. | | [z.tempo.VirtualAddress](/zod/tempo/VirtualAddress) | z.tempo.VirtualAddress schemas. | | [z.tempo.ZoneId](/zod/tempo/ZoneId) | z.tempo.ZoneId schemas. | | [z.tempo.ZoneRpcAuthentication](/zod/tempo/ZoneRpcAuthentication) | z.tempo.ZoneRpcAuthentication schemas. | # Abi Utilities & types for working with [Application Binary Interfaces (ABIs)](https://docs.soliditylang.org/en/latest/abi-spec.html) :::note If you are looking for ABI parameter **encoding** & **decoding** functions, see [`AbiParameters.encode`](/api/AbiParameters/encode) & [`AbiParameters.decode`](/api/AbiParameters/decode). ::: ## Examples Below are some examples demonstrating common usages of the `Abi` module: * [Instantiating JSON ABIs](#instantiating-json-abis) * [Instantiating Human Readable ABIs](#instantiating-human-readable-abis) * [Formatting ABIs](#formatting-abis) ### Instantiating JSON ABIs An [`Abi.Abi`](/api/Abi/types#abi) can be instantiated from a JSON ABI by using [`Abi.from`](/api/Abi/from): ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ { type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] } ]) abi //^? ``` ### Instantiating Human Readable ABIs An [`Abi.Abi`](/api/Abi/types#abi) can be instantiated from a human-readable ABI by using [`Abi.from`](/api/Abi/from): ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)' ]) abi //^? ``` ### Formatting ABIs An [`Abi.Abi`](/api/Abi/types#abi) can be formatted into a human-readable ABI by using [`Abi.format`](/api/Abi/format): ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ { type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] } ]) //---cut--- const formatted = Abi.format(abi) formatted // ^? ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Abi.format`](/api/Abi/format) | Formats an [`Abi.Abi`](/api/Abi/types#abi) into a **Human Readable ABI**. | | [`Abi.from`](/api/Abi/from) | Parses an arbitrary **JSON ABI** or **Human Readable ABI** into a typed [`Abi.Abi`](/api/Abi/types#abi). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Abi.CircularReferenceError`](/api/Abi/errors#abicircularreferenceerror) | | | [`Abi.InvalidSignatureError`](/api/Abi/errors#abiinvalidsignatureerror) | | | [`Abi.InvalidStructSignatureError`](/api/Abi/errors#abiinvalidstructsignatureerror) | | | [`Abi.UnknownSignatureError`](/api/Abi/errors#abiunknownsignatureerror) | | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Abi.Abi`](/api/Abi/types#abiabi) | Root type for an ABI. | # Abi.format Formats an [`Abi.Abi`](/api/Abi/types#abi) into a **Human Readable ABI**. ## Imports :::code-group ```ts [Named] import { Abi } from 'ox' ``` ```ts [Entrypoint] import * as Abi from 'ox/Abi' ``` ::: ## Examples ```ts twoslash import { Abi } from 'ox' const formatted = Abi.format([ { type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] } ]) formatted // ^? ``` ## Definition ```ts function format( abi: Abi | readonly unknown[], ): readonly string[] ``` **Source:** [src/core/Abi.ts](https://github.com/wevm/ox/blob/main/src/core/Abi.ts#L19) ## Parameters ### abi * **Type:** `Abi | readonly unknown[]` The ABI to format. ## Return Type The formatted ABI. `readonly string[]` # Abi.from Parses an arbitrary **JSON ABI** or **Human Readable ABI** into a typed [`Abi.Abi`](/api/Abi/types#abi). ## Imports :::code-group ```ts [Named] import { Abi } from 'ox' ``` ```ts [Entrypoint] import * as Abi from 'ox/Abi' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ { type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] } ]) abi //^? ``` ### Human Readable ABIs ```ts twoslash import { Abi } from 'ox' const abi = Abi.from([ 'function approve(address spender, uint256 amount) returns (bool)' ]) abi //^? ``` ## Definition ```ts function from( abi: Abi | readonly string[], ): Abi ``` **Source:** [src/core/Abi.ts](https://github.com/wevm/ox/blob/main/src/core/Abi.ts#L67) ## Parameters ### abi * **Type:** `Abi | readonly string[]` The ABI to parse. ## Return Type The typed ABI. `Abi.Abi` # Abi Errors ## `Abi.CircularReferenceError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L197) ## `Abi.InvalidSignatureError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L145) ## `Abi.InvalidStructSignatureError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L171) ## `Abi.UnknownSignatureError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L161) # Abi Types ## `Abi.Abi` Root type for an ABI. **Source:** [src/core/Abi.ts](https://github.com/wevm/ox/blob/main/src/core/Abi.ts#L9) # AbiConstructor Utilities & types for working with [Constructors](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. `AbiConstructor` is a sub-type of [`AbiItem`](/api/AbiItem). ## Examples Below are some examples demonstrating common usages of the `AbiConstructor` module: * [Instantiating via JSON ABI](#instantiating-via-json-abi) * [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item) * [Encoding to Deploy Data](#encoding-to-deploy-data) ### Instantiating via JSON ABI An `AbiConstructor` can be instantiated from a JSON ABI by using [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi): ```ts twoslash import { Abi, AbiConstructor } from 'ox' const abi = Abi.from([ 'constructor(address owner)', 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiConstructor.fromAbi(abi) // [!code focus] // ^? ``` ### Instantiating via Human-Readable ABI Item An `AbiConstructor` can be instantiated from a human-readable ABI by using [`AbiConstructor.from`](/api/AbiConstructor/from): ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from( 'constructor(address owner)' ) constructor //^? ``` ### Encoding to Deploy Data Constructor arguments can be ABI-encoded using [`AbiConstructor.encode`](/api/AbiConstructor/encode) (with bytecode) into deploy data. This data can then be passed to a transaction to deploy a contract. ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from( 'constructor(address, uint256)' ) const data = AbiConstructor.encode(constructor, { // [!code focus] bytecode: '0x...', // [!code focus] args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n] // [!code focus] }) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiConstructor.decode`](/api/AbiConstructor/decode) | ABI-decodes the provided constructor input (`inputs`). | | [`AbiConstructor.encode`](/api/AbiConstructor/encode) | ABI-encodes the provided constructor input (`inputs`). | | [`AbiConstructor.format`](/api/AbiConstructor/format) | Formats an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) into a **Human Readable ABI Function**. | | [`AbiConstructor.from`](/api/AbiConstructor/from) | Parses an arbitrary **JSON ABI Constructor** or **Human Readable ABI Constructor** into a typed [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor). | | [`AbiConstructor.fromAbi`](/api/AbiConstructor/fromAbi) | Extracts an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AbiConstructor.BytecodeMismatchError`](/api/AbiConstructor/errors#abiconstructorbytecodemismatcherror) | Throws when the provided `data` does not begin with the provided `bytecode`. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructorabiconstructor) | Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with a `constructor` type. | # AbiConstructor.decode ABI-decodes the provided constructor input (`inputs`). ## Imports :::code-group ```ts [Named] import { AbiConstructor } from 'ox' ``` ```ts [Entrypoint] import * as AbiConstructor from 'ox/AbiConstructor' ``` ::: ## Examples ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from( 'constructor(address, uint256)' ) const bytecode = '0x...' const data = AbiConstructor.encode(constructor, { bytecode, args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n] }) const decoded = AbiConstructor.decode(constructor, { // [!code focus] bytecode, // [!code focus] data // [!code focus] }) // [!code focus] ``` ### ABI-shorthand You can also specify an entire ABI object as a parameter to `AbiConstructor.decode`. ```ts twoslash // @noErrors import { Abi, AbiConstructor } from 'ox' const abi = Abi.from([...]) const data = AbiConstructor.encode(abi, { bytecode: '0x...', args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n], }) const decoded = AbiConstructor.decode(abi, { // [!code focus] bytecode: '0x...', // [!code focus] data, // [!code focus] }) // [!code focus] ``` ## Definition ```ts function decode( abi: abi | Abi.Abi | readonly unknown[], options: decode.Options, ): decode.ReturnType ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L81) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### options * **Type:** `decode.Options` Decoding options. ## Return Type The decoded constructor inputs. `decode.ReturnType` # AbiConstructor.encode ABI-encodes the provided constructor input (`inputs`). ## Imports :::code-group ```ts [Named] import { AbiConstructor } from 'ox' ``` ```ts [Entrypoint] import * as AbiConstructor from 'ox/AbiConstructor' ``` ::: ## Examples ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from( 'constructor(address, uint256)' ) const data = AbiConstructor.encode(constructor, { bytecode: '0x...', args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n] }) ``` ### ABI-shorthand You can also specify an entire ABI object as a parameter to `AbiConstructor.encode`. ```ts twoslash // @noErrors import { Abi, AbiConstructor } from 'ox' const abi = Abi.from([...]) const data = AbiConstructor.encode(abi, { // [!code focus] bytecode: '0x...', // [!code focus] args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n], // [!code focus] }) // [!code focus] ``` ### End-to-end Below is an end-to-end example of using `AbiConstructor.encode` to encode the constructor of a contract and deploy it. ```ts twoslash import 'ox/window' import { AbiConstructor, Hex } from 'ox' // 1. Instantiate the ABI Constructor. const constructor = AbiConstructor.from( 'constructor(address owner, uint256 amount)' ) // 2. Encode the ABI Constructor. const data = AbiConstructor.encode(constructor, { bytecode: '0x...', args: ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 123n] }) // 3. Deploy the contract. const hash = await window.ethereum!.request({ method: 'eth_sendTransaction', params: [{ data }] }) ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function encode( abi: abi | Abi.Abi | readonly unknown[], options: encode.Options, ): encode.ReturnType ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L227) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### options * **Type:** `encode.Options` Encoding options. #### options.args * **Type:** `args` The constructor arguments to encode. #### options.bytecode * **Type:** `0x${string}` The bytecode of the contract. ## Return Type The encoded constructor. `encode.ReturnType` # AbiConstructor.format Formats an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) into a **Human Readable ABI Function**. ## Imports :::code-group ```ts [Named] import { AbiConstructor } from 'ox' ``` ```ts [Entrypoint] import * as AbiConstructor from 'ox/AbiConstructor' ``` ::: ## Examples ```ts twoslash import { AbiConstructor } from 'ox' const formatted = AbiConstructor.format({ inputs: [{ name: 'owner', type: 'address' }], payable: false, stateMutability: 'nonpayable', type: 'constructor' }) formatted // ^? ``` ## Definition ```ts function format( abiConstructor: AbiConstructor, ): string ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L293) ## Parameters ### abiConstructor * **Type:** `AbiConstructor` The ABI Constructor to format. ## Return Type The formatted ABI Constructor. `string` # AbiConstructor.from Parses an arbitrary **JSON ABI Constructor** or **Human Readable ABI Constructor** into a typed [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor). ## Imports :::code-group ```ts [Named] import { AbiConstructor } from 'ox' ``` ```ts [Entrypoint] import * as AbiConstructor from 'ox/AbiConstructor' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from({ inputs: [{ name: 'owner', type: 'address' }], payable: false, stateMutability: 'nonpayable', type: 'constructor' }) constructor //^? ``` ### Human Readable ABIs A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from( 'constructor(address owner)' // [!code hl] ) constructor //^? ``` It is possible to specify `struct`s along with your definitions: ```ts twoslash import { AbiConstructor } from 'ox' const constructor = AbiConstructor.from([ 'struct Foo { address owner; uint256 amount; }', // [!code hl] 'constructor(Foo foo)' ]) constructor //^? ``` ## Definition ```ts function from( abiConstructor: AbiConstructor | string | readonly string[], ): AbiConstructor ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L331) ## Parameters ### abiConstructor * **Type:** `AbiConstructor | string | readonly string[]` The ABI Constructor to parse. ## Return Type Typed ABI Constructor. `AbiConstructor` # AbiConstructor.fromAbi Extracts an [`AbiConstructor.AbiConstructor`](/api/AbiConstructor/types#abiconstructor) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. ## Imports :::code-group ```ts [Named] import { AbiConstructor } from 'ox' ``` ```ts [Entrypoint] import * as AbiConstructor from 'ox/AbiConstructor' ``` ::: ## Examples ### Extracting by Name ABI Events can be extracted by their name using the `name` option: ```ts twoslash import { Abi, AbiConstructor } from 'ox' const abi = Abi.from([ 'constructor(address owner)', 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiConstructor.fromAbi(abi) // [!code focus] // ^? ``` ## Definition ```ts function fromAbi( abi: Abi.Abi | readonly unknown[], ): AbiConstructor ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L421) ## Parameters ### abi * **Type:** `Abi.Abi | readonly unknown[]` ## Return Type The ABI constructor. `AbiConstructor` # AbiConstructor Errors ## `AbiConstructor.BytecodeMismatchError` Throws when the provided `data` does not begin with the provided `bytecode`. ### Examples ```ts twoslash import { AbiConstructor } from 'ox' AbiConstructor.decode( AbiConstructor.from('constructor(address)'), { bytecode: '0x6080...', data: '0xdeadbeef' } ) // @error: AbiConstructor.BytecodeMismatchError: Provided `data` does not start with the provided `bytecode`. ``` **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L479) # AbiConstructor Types ## `AbiConstructor.AbiConstructor` Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with a `constructor` type. **Source:** [src/core/AbiConstructor.ts](https://github.com/wevm/ox/blob/main/src/core/AbiConstructor.ts#L12) # AbiError Utilities & types for working with [Errors](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. `AbiError` is a sub-type of [`AbiItem`](/api/AbiItem). ## Examples Below are some examples demonstrating common usages of the `AbiError` module: * [Instantiating via JSON ABI](#instantiating-via-json-abi) * [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item) * [Decoding Error Data](#decoding-error-data) ### Instantiating via JSON ABI An `AbiError` can be instantiated from a JSON ABI by using [`AbiError.fromAbi`](/api/AbiError/fromAbi): ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'function foo()', 'error BadSignatureV(uint8 v)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiError.fromAbi(abi, 'BadSignatureV') // [!code focus] // ^? ``` ### Instantiating via Human-Readable ABI Item An `AbiError` can be instantiated from a human-readable ABI by using [`AbiError.from`](/api/AbiError/from): ```ts twoslash import { AbiError } from 'ox' const error = AbiError.from('error BadSignatureV(uint8 v)') error //^? ``` ### Decoding Error Data Error data can be ABI-decoded using the [`AbiError.decode`](/api/AbiError/decode) function. ```ts twoslash // @noErrors import { Abi, AbiError } from 'ox' const abi = Abi.from([...]) const error = AbiError.fromAbi(abi, 'InvalidSignature') const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001') // [!code focus] // @log: [420n, 69n, 1] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiError.decode`](/api/AbiError/decode) | ABI-decodes the provided error input (`inputs`). | | [`AbiError.encode`](/api/AbiError/encode) | ABI-encodes the provided error input (`inputs`), prefixed with the 4 byte error selector. | | [`AbiError.extract`](/api/AbiError/extract) | Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) from an [`Abi.Abi`](/api/Abi/types#abi) and decodes its arguments from error data. | | [`AbiError.format`](/api/AbiError/format) | Formats an [`AbiError.AbiError`](/api/AbiError/types#abierror) into a **Human Readable ABI Error**. | | [`AbiError.from`](/api/AbiError/from) | Parses an arbitrary **JSON ABI Error** or **Human Readable ABI Error** into a typed [`AbiError.AbiError`](/api/AbiError/types#abierror). | | [`AbiError.fromAbi`](/api/AbiError/fromAbi) | Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. | | [`AbiError.getSelector`](/api/AbiError/getSelector) | Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiError.AbiError`](/api/AbiError/types#abierror). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiError.AbiError`](/api/AbiError/types#abierrorabierror) | Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with an `error` type. | | [`AbiError.ExtractNames`](/api/AbiError/types#abierrorextractnames) | | | [`AbiError.FromAbi`](/api/AbiError/types#abierrorfromabi) | Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. | | [`AbiError.Name`](/api/AbiError/types#abierrorname) | Extracts the names of all [`AbiError.AbiError`](/api/AbiError/types#abierror) items in an [`Abi.Abi`](/api/Abi/types#abi). | # AbiError.decode ABI-decodes the provided error input (`inputs`). :::tip This function is typically used to decode contract function reverts (e.g. a JSON-RPC error response). See the [End-to-end Example](#end-to-end). ::: ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ```ts twoslash import { AbiError } from 'ox' const error = AbiError.from( 'error InvalidSignature(uint r, uint s, uint8 yParity)' ) const value = AbiError.decode( error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001' ) // @log: [420n, 69n, 1] ``` You can extract an ABI Error from a JSON ABI with [`AbiError.fromAbi`](/api/AbiError/fromAbi): ```ts twoslash // @noErrors import { Abi, AbiError } from 'ox' const abi = Abi.from([...]) // [!code hl] const error = AbiError.fromAbi(abi, 'InvalidSignature') // [!code hl] const value = AbiError.decode(error, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001') // @log: [420n, 69n, 1] ``` You can pass the error `data` to the `name` property of [`AbiError.fromAbi`](/api/AbiError/fromAbi) to extract and infer the error by its 4-byte selector: ```ts twoslash // @noErrors import { Abi, AbiError } from 'ox' const data = '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001' const abi = Abi.from([...]) const error = AbiError.fromAbi(abi, data) // [!code hl] const value = AbiError.decode(error, data) // @log: [420n, 69n, 1] ``` ### ABI-shorthand You can also specify an entire ABI object as a parameter to [`AbiError.decode`](/api/AbiError/decode): ```ts twoslash // @noErrors import { Abi, AbiError } from 'ox' const abi = Abi.from([...]) const value = AbiError.decode( abi, // [!code hl] 'InvalidSignature', // [!code hl] '0x...' ) // @log: [420n, 69n, 1] ``` ### End-to-end Below is an end-to-end example of using `AbiError.decode` to decode the revert error of an `approve` contract call on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2). ```ts twoslash // @noErrors import 'ox/window' import { Abi, AbiError, AbiFunction } from 'ox' // 1. Extract the Function from the Contract's ABI. const abi = Abi.from([ // ... { inputs: [ { name: 'to', type: 'address' }, { name: 'tokenId', type: 'uint256' } ], name: 'approve', outputs: [], stateMutability: 'nonpayable', type: 'function' } // ... ]) const approve = AbiFunction.fromAbi(abi, 'approve') // 2. Encode the Function Input. const data = AbiFunction.encodeData(approve, [ '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n ]) try { // 3. Attempt to perform the the Contract Call. await window.ethereum!.request({ method: 'eth_call', params: [ { data, to: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2' } ] }) } catch (e) { // [!code focus] // 4. Extract and decode the Error. // [!code focus] const error = AbiError.fromAbi(abi, e.data) // [!code focus] const value = AbiError.decode(error, e.data) // [!code focus] console.error(`${error.name}(${value})`) // [!code focus] // @error: Error(ERC721: approve caller is not owner nor approved for all) } // [!code focus] ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function decode( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), data: Hex.Hex, options?: decode.Options, ): decode.ReturnType ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L19) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### data * **Type:** `Hex.Hex` The error data. ### options * **Type:** `decode.Options` * **Optional** Decoding options. #### options.as * **Type:** `as | "Array" | "Object"` * **Optional** Whether the decoded values should be returned as an `Object` or `Array`. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. ## Return Type The decoded error. `decode.ReturnType` # AbiError.encode ABI-encodes the provided error input (`inputs`), prefixed with the 4 byte error selector. ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ```ts twoslash import { AbiError } from 'ox' const error = AbiError.from( 'error InvalidSignature(uint r, uint s, uint8 yParity)' ) const data = AbiError.encode( // [!code focus] error, // [!code focus] [1n, 2n, 0] // [!code focus] ) // [!code focus] // @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c' ``` ### ABI-shorthand You can also specify an entire ABI object and an error name as parameters to `AbiError.encode`. ```ts twoslash // @noErrors import { Abi, AbiError } from 'ox' const abi = Abi.from([...]) const data = AbiError.encode( abi, // [!code hl] 'InvalidSignature', // [!code hl] [1n, 2n, 0] ) ``` ## Definition ```ts function encode( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), args: encode.Args, ): encode.ReturnType ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L410) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### args * **Type:** `encode.Args` Error arguments ## Return Type ABI-encoded error name and arguments `encode.ReturnType` # AbiError.extract Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) from an [`Abi.Abi`](/api/Abi/types#abi) and decodes its arguments from error data. ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'error InvalidSignature(uint r, uint s, uint8 yParity)' ]) const { error, args } = AbiError.extract( abi, '0xecde634900000000000000000000000000000000000000000000000000000000000001a400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001' ) // @log: { // @log: error: { name: 'InvalidSignature', type: 'error', ... }, // @log: args: [420n, 69n, 1], // @log: } ``` ## Definition ```ts function extract( abi: abi | Abi.Abi | readonly unknown[], data: Hex.Hex, options?: extract.Options, ): extract.ReturnType, as> ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L306) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract from. ### data * **Type:** `Hex.Hex` The error data. ### options * **Type:** `extract.Options` * **Optional** Extraction options. ## Return Type The extracted ABI Error and decoded arguments. `extract.ReturnType, as>` # AbiError.format Formats an [`AbiError.AbiError`](/api/AbiError/types#abierror) into a **Human Readable ABI Error**. ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ```ts twoslash import { AbiError } from 'ox' const formatted = AbiError.format({ type: 'error', name: 'Example', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ] }) formatted // ^? ``` ## Definition ```ts function format( abiError: abiError | AbiError, ): format.ReturnType ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L512) ## Parameters ### abiError * **Type:** `abiError | AbiError` The ABI Error to format. ## Return Type The formatted ABI Error. `format.ReturnType` # AbiError.from Parses an arbitrary **JSON ABI Error** or **Human Readable ABI Error** into a typed [`AbiError.AbiError`](/api/AbiError/types#abierror). ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { AbiError } from 'ox' const badSignatureVError = AbiError.from({ inputs: [{ name: 'v', type: 'uint8' }], name: 'BadSignatureV', type: 'error' }) badSignatureVError //^? ``` ### Human Readable ABIs A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiError } from 'ox' const badSignatureVError = AbiError.from( 'error BadSignatureV(uint8 v)' // [!code hl] ) badSignatureVError //^? ``` It is possible to specify `struct`s along with your definitions: ```ts twoslash import { AbiError } from 'ox' const badSignatureVError = AbiError.from([ 'struct Signature { uint8 v; }', // [!code hl] 'error BadSignatureV(Signature signature)' ]) badSignatureVError //^? ``` ## Definition ```ts function from( abiError: (abiError | AbiError | string | readonly string[]) & ((abiError extends string ? internal.Signature : never) | (abiError extends readonly string[] ? internal.Signatures : never) | AbiError), options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L580) ## Parameters ### abiError * **Type:** `(abiError | AbiError | string | readonly string[]) & ((abiError extends string ? internal.Signature : never) | (abiError extends readonly string[] ? internal.Signatures : never) | AbiError)` The ABI Error to parse. ### options * **Type:** `from.Options` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted function (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type Typed ABI Error. `from.ReturnType` # AbiError.fromAbi Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ### Extracting by Name ABI Errors can be extracted by their name using the `name` option: ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'function foo()', 'error BadSignatureV(uint8 v)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiError.fromAbi(abi, 'BadSignatureV') // [!code focus] // ^? ``` ### Extracting by Selector ABI Errors can be extract by their selector when [`Hex.Hex`](/api/Hex/types#hex) is provided to `name`. ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'function foo()', 'error BadSignatureV(uint8 v)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiError.fromAbi(abi, '0x095ea7b3') // [!code focus] // ^? ``` :::note Extracting via a hex selector is useful when extracting an ABI Error from JSON-RPC error data. ::: ## Definition ```ts function fromAbi( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), options?: AbiItem.fromAbi.Options>, ): fromAbi.ReturnType ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L662) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract from. ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` The name (or selector) of the ABI item to extract. ### options * **Type:** `AbiItem.fromAbi.Options>` * **Optional** Extraction options. #### options.args * **Type:** `allArgs | (Widen & (args extends allArgs ? unknown : never))` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted item (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type The ABI item. `fromAbi.ReturnType` # AbiError.getSelector Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiError.AbiError`](/api/AbiError/types#abierror). ## Imports :::code-group ```ts [Named] import { AbiError } from 'ox' ``` ```ts [Entrypoint] import * as AbiError from 'ox/AbiError' ``` ::: ## Examples ```ts twoslash import { AbiError } from 'ox' const selector = AbiError.getSelector( 'error BadSignatureV(uint8 v)' ) // @log: '0x6352211e' ``` ```ts twoslash import { AbiError } from 'ox' const selector = AbiError.getSelector({ inputs: [{ name: 'v', type: 'uint8' }], name: 'BadSignatureV', type: 'error' }) // @log: '0x6352211e' ``` ## Definition ```ts function getSelector( abiItem: string | AbiError, ): Hex.Hex ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L746) ## Parameters ### abiItem * **Type:** `string | AbiError` The ABI item to compute the selector for. #### abiItem.hash * **Type:** `0x${string}` * **Optional** #### abiItem.overloads * **Type:** `readonly any[]` * **Optional** ## Return Type The first 4 bytes of the [`Hash.keccak256`](/api/Hash/keccak256) hash of the error signature. `Hex.Hex` # AbiError Types ## `AbiError.AbiError` Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with an `error` type. **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L13) ## `AbiError.ExtractNames` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L833) ## `AbiError.FromAbi` Extracts an [`AbiError.AbiError`](/api/AbiError/types#abierror) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. ### Examples ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'error Foo(string)', 'error Bar(uint256)' ]) type Foo = AbiError.FromAbi // ^? ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L809) ## `AbiError.Name` Extracts the names of all [`AbiError.AbiError`](/api/AbiError/types#abierror) items in an [`Abi.Abi`](/api/Abi/types#abi). ### Examples ```ts twoslash import { Abi, AbiError } from 'ox' const abi = Abi.from([ 'error Foo(string)', 'error Bar(uint256)' ]) type names = AbiError.Name // ^? ``` **Source:** [src/core/AbiError.ts](https://github.com/wevm/ox/blob/main/src/core/AbiError.ts#L830) # AbiEvent Utilities & types for working with [Events](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. `AbiEvent` is a sub-type of [`AbiItem`](/api/AbiItem). ## Examples Below are some examples demonstrating common usages of the `AbiEvent` module: * [Instantiating via JSON ABI](#instantiating-via-json-abi) * [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item) * [Encoding to Event Topics](#encoding-to-event-topics) * [Decoding Event Topics and Data](#decoding-event-topics-and-data) ### Instantiating via JSON ABI An `AbiEvent` can be instantiated from a JSON ABI by using [`AbiEvent.fromAbi`](/api/AbiEvent/fromAbi): ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiEvent.fromAbi(abi, 'Transfer') // [!code focus] // ^? ``` ### Instantiating via Human-Readable ABI Item An `AbiEvent` can be instantiated from a human-readable ABI by using [`AbiEvent.from`](/api/AbiEvent/from): ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' // [!code hl] ) transfer //^? ``` ### Encoding to Event Topics Encode an `AbiEvent` into topics using [`AbiEvent.encode`](/api/AbiEvent/encode): ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const { topics } = AbiEvent.encode(transfer, { from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code hl] to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8' // [!code hl] }) // @log: [ // @log: '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', // @log: '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', // @log: '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8' // @log: ] ``` ### Decoding Event Topics and Data Event topics and data can be decoded using [`AbiEvent.decode`](/api/AbiEvent/decode): ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const log = { // ... data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] } as const const decoded = AbiEvent.decode(transfer, log) // @log: { // @log: from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', // @log: to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', // @log: value: 1n // @log: } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiEvent.assertArgs`](/api/AbiEvent/assertArgs) | Asserts that the provided arguments match the decoded log arguments. | | [`AbiEvent.decode`](/api/AbiEvent/decode) | ABI-Decodes the provided [Log Topics and Data](https://info.etherscan.com/what-is-event-logs/) according to the ABI Event's parameter types (`input`). | | [`AbiEvent.decodeLog`](/api/AbiEvent/decodeLog) | Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) from an [`Abi.Abi`](/api/Abi/types#abi) and decodes its arguments from a Log. | | [`AbiEvent.encode`](/api/AbiEvent/encode) | ABI-encodes the provided event input (`inputs`) into an array of [Event Topics](https://info.etherscan.com/what-is-event-logs/). | | [`AbiEvent.extractLogs`](/api/AbiEvent/extractLogs) | Extracts and decodes Logs that match an ABI Event in an ABI. | | [`AbiEvent.format`](/api/AbiEvent/format) | Formats an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) into a **Human Readable ABI Error**. | | [`AbiEvent.from`](/api/AbiEvent/from) | Parses an arbitrary **JSON ABI Event** or **Human Readable ABI Event** into a typed [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent). | | [`AbiEvent.fromAbi`](/api/AbiEvent/fromAbi) | Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. | | [`AbiEvent.getSelector`](/api/AbiEvent/getSelector) | Computes the event selector (hash of event signature) for an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AbiEvent.ArgsMismatchError`](/api/AbiEvent/errors#abieventargsmismatcherror) | Thrown when the provided arguments do not match the expected arguments. | | [`AbiEvent.DataMismatchError`](/api/AbiEvent/errors#abieventdatamismatcherror) | Thrown when the provided data size does not match the expected size from the non-indexed parameters. | | [`AbiEvent.FilterTypeNotSupportedError`](/api/AbiEvent/errors#abieventfiltertypenotsupportederror) | Thrown when the provided filter type is not supported. | | [`AbiEvent.InputNotFoundError`](/api/AbiEvent/errors#abieventinputnotfounderror) | Thrown when no argument was found on the event signature. | | [`AbiEvent.SelectorTopicMismatchError`](/api/AbiEvent/errors#abieventselectortopicmismatcherror) | Thrown when the provided selector does not match the expected selector. | | [`AbiEvent.SelectorTopicNotFoundError`](/api/AbiEvent/errors#abieventselectortopicnotfounderror) | Thrown when the selector topic is not found. | | [`AbiEvent.TopicsMismatchError`](/api/AbiEvent/errors#abieventtopicsmismatcherror) | Thrown when the provided topics do not match the expected number of topics. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abieventabievent) | Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with an `event` type. | | [`AbiEvent.ExtractNames`](/api/AbiEvent/types#abieventextractnames) | | | [`AbiEvent.FromAbi`](/api/AbiEvent/types#abieventfromabi) | Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. | | [`AbiEvent.Name`](/api/AbiEvent/types#abieventname) | Extracts the names of all [`AbiError.AbiError`](/api/AbiError/types#abierror) items in an [`Abi.Abi`](/api/Abi/types#abi). | # AbiEvent.assertArgs Asserts that the provided arguments match the decoded log arguments. ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] }) AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n }) // @error: AbiEvent.ArgsMismatchError: Given arguments to not match the arguments decoded from the log. // @error: Event: event Transfer(address indexed from, address indexed to, uint256 value) // @error: Expected Arguments: // @error: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac // @error: to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad // @error: value: 1 // @error: Given Arguments: // @error: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad // @error: to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac // @error: value: 1 ``` ## Definition ```ts function assertArgs( abiEvent: abiEvent | AbiEvent, args: unknown, matchArgs: IsNarrowable extends true ? abiEvent['inputs'] extends readonly [] ? never : internal.ParametersToPrimitiveTypes : unknown, ): void ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L200) ## Parameters ### abiEvent * **Type:** `abiEvent | AbiEvent` ABI Event to check. ### args * **Type:** `unknown` Decoded arguments. ### matchArgs * **Type:** `IsNarrowable extends true ? abiEvent['inputs'] extends readonly [] ? never : internal.ParametersToPrimitiveTypes : unknown` The arguments to check. ## Return Type `void` # AbiEvent.decode ABI-Decodes the provided [Log Topics and Data](https://info.etherscan.com/what-is-event-logs/) according to the ABI Event's parameter types (`input`). :::tip This function is typically used to decode an [Event Log](https://info.etherscan.com/what-is-event-logs/) that may be returned from a Log Query (e.g. `eth_getLogs`) or Transaction Receipt. See the [End-to-end Example](#end-to-end). ::: ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const log = { // ... data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] } as const const decoded = AbiEvent.decode(transfer, log) // @log: { // @log: from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: value: 1n // @log: } ``` ### ABI-shorthand You can also specify an entire ABI object and an event name as parameters to [`AbiEvent.decode`](/api/AbiEvent/decode): ```ts twoslash // @noErrors import { Abi, AbiEvent } from 'ox' const abi = Abi.from([...]) const log = { // ... data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', ], } as const const decoded = AbiEvent.decode( abi, // [!code focus] 'Transfer', // [!code focus] log ) // @log: { // @log: from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: value: 1n // @log: } ``` ### End-to-end Below is an end-to-end example of using `AbiEvent.decode` to decode the topics of a `Transfer` event on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2). ```ts twoslash import 'ox/window' import { AbiEvent, Hex } from 'ox' // 1. Instantiate the `Transfer` ABI Event. const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) // 2. Encode the ABI Event into Event Topics. const { topics } = AbiEvent.encode(transfer) // 3. Query for events matching the encoded Topics. const logs = await window.ethereum!.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: Hex.fromNumber(19760235n), toBlock: Hex.fromNumber(19760240n), topics } ] }) // 4. Decode the Log. // [!code focus] const decoded = AbiEvent.decode(transfer, logs[0]!) // [!code focus] // @log: { // @log: from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: value: 603n // @log: } ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function decode( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), log: decode.Log, options?: decode.Options, ): decode.ReturnType ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L417) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### log * **Type:** `decode.Log` `topics` & `data` to decode. #### log.data * **Type:** `0x${string}` * **Optional** #### log.topics * **Type:** `readonly 0x${string}[]` ### options * **Type:** `decode.Options` * **Optional** Decoding options. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. ## Return Type The decoded event. `decode.ReturnType` # AbiEvent.decodeLog Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) from an [`Abi.Abi`](/api/Abi/types#abi) and decodes its arguments from a Log. ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)' ]) const decoded = AbiEvent.decodeLog(abi, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] }) // @log: { // @log: event: { name: 'Transfer', type: 'event', ... }, // @log: args: { // @log: from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: value: 1n, // @log: }, // @log: } ``` ## Definition ```ts function decodeLog( abi: abi | Abi.Abi | readonly unknown[], log: decodeLog.Log, options?: decodeLog.Options, ): decodeLog.ReturnType> ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L650) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract an event from. ### log * **Type:** `decodeLog.Log` `topics` & `data` to decode. ### options * **Type:** `decodeLog.Options` * **Optional** Decoding options. ## Return Type The decoded event and arguments. `decodeLog.ReturnType>` # AbiEvent.encode ABI-encodes the provided event input (`inputs`) into an array of [Event Topics](https://info.etherscan.com/what-is-event-logs/). :::tip This function is typically used to encode event arguments into [Event Topics](https://info.etherscan.com/what-is-event-logs/). See the [End-to-end Example](#end-to-end). ::: ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const { topics } = AbiEvent.encode(transfer) // @log: ['0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0'] ``` ### Passing Arguments You can pass `indexed` parameter values to `AbiEvent.encode`. TypeScript types will be inferred from the ABI Event, to guard you from inserting the wrong values. For example, the `Transfer` event below accepts an `address` type for the `from` and `to` attributes. ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const { topics } = AbiEvent.encode(transfer, { from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code hl] to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8' // [!code hl] }) // @log: [ // @log: '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', // @log: '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', // @log: '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8' // @log: ] ``` ### ABI-shorthand You can also specify an entire ABI object and an event name as parameters to [`AbiEvent.encode`](/api/AbiEvent/encode): ```ts twoslash // @noErrors import { Abi, AbiEvent } from 'ox' const abi = Abi.from([...]) const { topics } = AbiEvent.encode( abi, // [!code focus] 'Transfer', // [!code focus] { from: '0xf39fd6e51aad88f6f4ce6ab882779cfffb92266', // [!code focus] to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', } ) // @log: [ // @log: '0x406dade31f7ae4b5dbc276258c28dde5ae6d5c2773c5745802c493a2360e55e0', // @log: '0x00000000000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266', // @log: '0x0000000000000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8' // @log: ] ``` ### End-to-end Below is an end-to-end example of using `AbiEvent.encode` to encode the topics of a `Transfer` event and query for events matching the encoded topics on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2). ```ts twoslash import 'ox/window' import { AbiEvent, Hex } from 'ox' // 1. Instantiate the `Transfer` ABI Event. const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) // 2. Encode the ABI Event into Event Topics. const { topics } = AbiEvent.encode(transfer) // 3. Query for events matching the encoded Topics. const logs = await window.ethereum!.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: Hex.fromNumber(19760235n), toBlock: Hex.fromNumber(19760240n), topics } ] }) // @log: [ // @log: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // @log: "0x0000000000000000000000000000000000000000000000000000000000000000", // @log: "0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1", // @log: "0x000000000000000000000000000000000000000000000000000000000000025b", // @log: ] ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function encode( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), [args]: encode.Args, ): encode.ReturnType ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1180) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### \[args] * **Type:** `encode.Args` ## Return Type The encoded event topics. `encode.ReturnType` # AbiEvent.extractLogs Extracts and decodes Logs that match an ABI Event in an ABI. ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'event Transfer(address indexed from, address indexed to, uint256 value)' ]) const logs = AbiEvent.extractLogs(abi, [ { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] } ]) // @log: [{ // @log: eventName: 'Transfer', // @log: args: { // @log: from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', // @log: value: 1n, // @log: }, // @log: topics: [...], // @log: data: '0x...', // @log: }] ``` ## Definition ```ts function extractLogs( abi: abi | Abi.Abi | readonly unknown[], logs: logs | readonly extractLogs.Log[], options?: extractLogs.Options, ): extractLogs.ReturnType, logs[number], strict>[] ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L722) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract events from. ### logs * **Type:** `logs | readonly extractLogs.Log[]` Logs to extract. ### options * **Type:** `extractLogs.Options` * **Optional** Extraction options. #### options.args * **Type:** `abiEvent["inputs"] extends readonly [] ? never : ParametersToPrimitiveTypes` * **Optional** Arguments to match against decoded event arguments. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. #### options.eventName * **Type:** `eventName | Name[]` * **Optional** Event name, or event names, to extract. #### options.strict * **Type:** `boolean | strict` * **Optional** Whether to strictly decode log topics and data. ## Return Type The extracted logs. `extractLogs.ReturnType, logs[number], strict>[]` # AbiEvent.format Formats an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) into a **Human Readable ABI Error**. ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { AbiEvent } from 'ox' const formatted = AbiEvent.format({ type: 'event', name: 'Transfer', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' } ] }) formatted // ^? ``` ## Definition ```ts function format( abiEvent: abiEvent | AbiEvent, ): format.ReturnType ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1336) ## Parameters ### abiEvent * **Type:** `abiEvent | AbiEvent` The ABI Event to format. ## Return Type The formatted ABI Event. `format.ReturnType` # AbiEvent.from Parses an arbitrary **JSON ABI Event** or **Human Readable ABI Event** into a typed [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent). ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from({ name: 'Transfer', type: 'event', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' } ] }) transfer //^? ``` ### Human Readable ABIs A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' // [!code hl] ) transfer //^? ``` ## Definition ```ts function from( abiEvent: (abiEvent | AbiEvent | string | readonly string[]) & ((abiEvent extends string ? internal.Signature : never) | (abiEvent extends readonly string[] ? internal.Signatures : never) | AbiEvent), options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1391) ## Parameters ### abiEvent * **Type:** `(abiEvent | AbiEvent | string | readonly string[]) & ((abiEvent extends string ? internal.Signature : never) | (abiEvent extends readonly string[] ? internal.Signatures : never) | AbiEvent)` The ABI Event to parse. ### options * **Type:** `from.Options` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted event (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type Typed ABI Event. `from.ReturnType` # AbiEvent.fromAbi Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ### Extracting by Name ABI Events can be extracted by their name using the `name` option: ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiEvent.fromAbi(abi, 'Transfer') // [!code focus] // ^? ``` ### Extracting by Selector ABI Events can be extract by their selector when [`Hex.Hex`](/api/Hex/types#hex) is provided to `name`. ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const selector = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' const item = AbiEvent.fromAbi(abi, selector) // [!code focus] // ^? ``` :::note Extracting via a hex selector is useful when extracting an ABI Event from the first topic of a Log. ::: ## Definition ```ts function fromAbi( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), options?: AbiItem.fromAbi.Options>, ): AbiItem.fromAbi.ReturnType ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1475) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract from. ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` The name (or selector) of the ABI item to extract. ### options * **Type:** `AbiItem.fromAbi.Options>` * **Optional** Extraction options. #### options.args * **Type:** `allArgs | (Widen & (args extends allArgs ? unknown : never))` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted item (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type The ABI item. `AbiItem.fromAbi.ReturnType` # AbiEvent.getSelector Computes the event selector (hash of event signature) for an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent). ## Imports :::code-group ```ts [Named] import { AbiEvent } from 'ox' ``` ```ts [Entrypoint] import * as AbiEvent from 'ox/AbiEvent' ``` ::: ## Examples ```ts twoslash import { AbiEvent } from 'ox' const selector = AbiEvent.getSelector( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f556a2' ``` ```ts twoslash import { AbiEvent } from 'ox' const selector = AbiEvent.getSelector({ name: 'Transfer', type: 'event', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'value', type: 'uint256' } ] }) // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f556a2' ``` ## Definition ```ts function getSelector( abiItem: string | AbiEvent, ): Hex.Hex ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1534) ## Parameters ### abiItem * **Type:** `string | AbiEvent` The ABI event to compute the selector for. #### abiItem.hash * **Type:** `0x${string}` * **Optional** #### abiItem.overloads * **Type:** `readonly any[]` * **Optional** ## Return Type The [`Hash.keccak256`](/api/Hash/keccak256) hash of the event signature. `Hex.Hex` # AbiEvent Errors ## `AbiEvent.ArgsMismatchError` Thrown when the provided arguments do not match the expected arguments. ### Examples ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad' ] }) AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', value: 1n }) // @error: AbiEvent.ArgsMismatchError: Given arguments do not match the expected arguments. // @error: Event: event Transfer(address indexed from, address indexed to, uint256 value) // @error: Expected Arguments: // @error: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac // @error: to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad // @error: value: 1 // @error: Given Arguments: // @error: from: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad // @error: to: 0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac // @error: value: 1 ``` ### Solution The provided arguments need to match the expected arguments. ```ts twoslash // @noErrors import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad' ] }) AbiEvent.assertArgs(abiEvent, args, { from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', // [!code --] from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', // [!code ++] to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', // [!code --] to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', // [!code ++] value: 1n }) ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1609) ## `AbiEvent.DataMismatchError` Thrown when the provided data size does not match the expected size from the non-indexed parameters. ### Examples ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address to, uint256 value)' // ↑ 32 bytes + ↑ 32 bytes = 64 bytes ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000023c34600', // ↑ 32 bytes ❌ topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266' ] }) // @error: AbiEvent.DataMismatchError: Data size of 32 bytes is too small for non-indexed event parameters. // @error: Non-indexed Parameters: (address to, uint256 value) // @error: Data: 0x0000000000000000000000000000000000000000000000000000000023c34600 (32 bytes) ``` ### Solution Ensure that the data size matches the expected size. ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address to, uint256 value)' // ↑ 32 bytes + ↑ 32 bytes = 64 bytes ) const args = AbiEvent.decode(abiEvent, { data: '0x0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb922660000000000000000000000000000000000000000000000000000000023c34600', // ↑ 64 bytes ✅ topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266' ] }) ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1747) ## `AbiEvent.FilterTypeNotSupportedError` Thrown when the provided filter type is not supported. ### Examples ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer((string) indexed a, string b)' ) AbiEvent.encode(transfer, { a: ['hello'] }) // @error: AbiEvent.FilterTypeNotSupportedError: Filter type "tuple" is not supported. ``` ### Solution Provide a valid event input type. ```ts twoslash // @noErrors import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer((string) indexed a, string b)' ) // [!code --] const transfer = AbiEvent.from( 'event Transfer(string indexed a, string b)' ) // [!code ++] ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1975) ## `AbiEvent.InputNotFoundError` Thrown when no argument was found on the event signature. ### Examples ```ts twoslash // @noErrors import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad' ] }) AbiEvent.assertArgs(abiEvent, args, { a: 'b', from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', value: 1n }) // @error: AbiEvent.InputNotFoundError: Parameter "a" not found on `event Transfer(address indexed from, address indexed to, uint256 value)`. ``` ### Solution Ensure the arguments match the event signature. ```ts twoslash // @noErrors import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ad' ] }) AbiEvent.assertArgs(abiEvent, args, { a: 'b', // [!code --] from: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac', to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ad', value: 1n }) ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1692) ## `AbiEvent.SelectorTopicMismatchError` Thrown when the provided selector does not match the expected selector. ### Examples ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, bool sender)' ) AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266' ] }) // @error: AbiEvent.SelectorTopicMismatchError: topics[0]="0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" does not match the expected topics[0]="0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f". // @error: Event: event Transfer(address indexed from, address indexed to, bool sender) // @error: Selector: 0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f ``` ### Solution Ensure that the provided selector matches the selector of the event signature. ```ts twoslash import { AbiEvent } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, bool sender)' ) AbiEvent.decode(transfer, { topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', // [!code --] '0x3da3cd3cf420c78f8981e7afeefa0eab1f0de0eb56e78ad9ba918ed01c0b402f', // [!code ++] '0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266' ] }) ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1896) ## `AbiEvent.SelectorTopicNotFoundError` Thrown when the selector topic is not found. ### Examples ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'event Transfer(address indexed from)' ]) AbiEvent.decodeLog(abi, { topics: [], data: '0x' }) // @error: AbiEvent.SelectorTopicNotFoundError: Selector topic not found. ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1932) ## `AbiEvent.TopicsMismatchError` Thrown when the provided topics do not match the expected number of topics. ### Examples ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac' ] }) // @error: AbiEvent.TopicsMismatchError: Expected a topic for indexed event parameter "to" for "event Transfer(address indexed from, address indexed to, uint256 value)". ``` ### Solution Ensure that the topics match the expected number of topics. ```ts twoslash import { AbiEvent } from 'ox' const abiEvent = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 value)' ) const args = AbiEvent.decode(abiEvent, { data: '0x0000000000000000000000000000000000000000000000000000000000000001', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266' // [!code ++] ] }) ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L1828) # AbiEvent Types ## `AbiEvent.AbiEvent` Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with an `event` type. **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L18) ## `AbiEvent.ExtractNames` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L155) ## `AbiEvent.FromAbi` Extracts an [`AbiEvent.AbiEvent`](/api/AbiEvent/types#abievent) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. ### Examples ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'event Foo(string)', 'event Bar(uint256)' ]) type Foo = AbiEvent.FromAbi // ^? ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L131) ## `AbiEvent.Name` Extracts the names of all [`AbiError.AbiError`](/api/AbiError/types#abierror) items in an [`Abi.Abi`](/api/Abi/types#abi). ### Examples ```ts twoslash import { Abi, AbiEvent } from 'ox' const abi = Abi.from([ 'event Foo(string)', 'event Bar(uint256)' ]) type names = AbiEvent.Name // ^? ``` **Source:** [src/core/AbiEvent.ts](https://github.com/wevm/ox/blob/main/src/core/AbiEvent.ts#L152) # AbiFunction Utilities & types for working with [Functions](https://docs.soliditylang.org/en/latest/abi-spec.html#json) on ABIs. `AbiFunction` is a sub-type of [`AbiItem`](/api/AbiItem). ## Examples Below are some examples demonstrating common usages of the `AbiFunction` module: * [Instantiating via JSON ABI](#instantiating-via-json-abi) * [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item) * [Encoding to Function Data](#encoding-to-function-data) * [Decoding a Function's Result](#decoding-a-function's-result) ### Instantiating via JSON ABI An `AbiFunction` can be instantiated from a JSON ABI by using [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi): ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiFunction.fromAbi(abi, 'bar') // [!code focus] // ^? ``` ### Instantiating via Human-Readable ABI Item An `AbiFunction` can be instantiated from a human-readable ABI by using [`AbiFunction.from`](/api/AbiFunction/from): ```ts twoslash import { AbiFunction } from 'ox' const bar = AbiFunction.from( 'function bar(string a) returns (uint256 x)' ) bar //^? ``` ### Encoding to Function Data A Function and its arguments can be ABI-encoded into data using the [`AbiFunction.encodeData`](/api/AbiFunction/encodeData) function. The output of this function can then be passed to `eth_sendTransaction` or `eth_call` as the `data` parameter. ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from( 'function approve(address, uint256)' ) const data = AbiFunction.encodeData( // [!code focus] approve, // [!code focus] ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n] // [!code focus] ) // [!code focus] // @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c' ``` ### Decoding a Function's Result A Function's result can be ABI-decoded using the [`AbiFunction.decodeResult`](/api/AbiFunction/decodeResult) function. ```ts twoslash import { AbiFunction } from 'ox' const data = '0x000000000000000000000000000000000000000000000000000000000000002a' // ↑ Example data that could be returned from a contract call via `eth_call`. const totalSupply = AbiFunction.from( 'function totalSupply() returns (uint256)' ) const output = AbiFunction.decodeResult(totalSupply, data) // [!code focus] // @log: 42n ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiFunction.decodeData`](/api/AbiFunction/decodeData) | ABI-decodes function arguments according to the ABI Item's input types (`inputs`). | | [`AbiFunction.decodeResult`](/api/AbiFunction/decodeResult) | ABI-decodes a function's result according to the ABI Item's output types (`outputs`). | | [`AbiFunction.encodeData`](/api/AbiFunction/encodeData) | ABI-encodes function arguments (`inputs`), prefixed with the 4 byte function selector. | | [`AbiFunction.encodeResult`](/api/AbiFunction/encodeResult) | ABI-encodes a function's result (`outputs`). | | [`AbiFunction.format`](/api/AbiFunction/format) | Formats an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) into a **Human Readable ABI Function**. | | [`AbiFunction.from`](/api/AbiFunction/from) | Parses an arbitrary **JSON ABI Function** or **Human Readable ABI Function** into a typed [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction). | | [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi) | Extracts an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. | | [`AbiFunction.getSelector`](/api/AbiFunction/getSelector) | Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunctionabifunction) | Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with a `function` type. | | [`AbiFunction.ExtractNames`](/api/AbiFunction/types#abifunctionextractnames) | | | [`AbiFunction.FromAbi`](/api/AbiFunction/types#abifunctionfromabi) | Extracts an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. | | [`AbiFunction.Name`](/api/AbiFunction/types#abifunctionname) | Extracts the names of all [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) items in an [`Abi.Abi`](/api/Abi/types#abi). | # AbiFunction.decodeData ABI-decodes function arguments according to the ABI Item's input types (`inputs`). ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from( 'function approve(address, uint256)' ) const data = AbiFunction.encodeData(approve, [ '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n ]) // '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c' const input = AbiFunction.decodeData(approve, data) // [!code focus] // @log: ['0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 69420n] ``` ### ABI-shorthand You can also specify an entire ABI object and a function name as parameters to [`AbiFunction.decodeData`](/api/AbiFunction/decodeData): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const abi = Abi.from([...]) const data = '0x... const input = AbiFunction.decodeData( abi, // [!code focus] 'approve', // [!code focus] data ) // @log: ['0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 69420n] ``` ### ABI selector shorthand You can also specify an entire ABI object and calldata. The ABI Function is extracted from the 4-byte selector: ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const abi = Abi.from([...]) const data = '0x095ea7b3... const input = AbiFunction.decodeData( abi, // [!code focus] data // [!code focus] ) // @log: ['0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', 69420n] ``` ## Definition ```ts function decodeData( abi: abi | Abi.Abi | readonly unknown[], data: Hex.Hex, options?: decodeData.Options, ): decodeData.ReturnType ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L129) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### data * **Type:** `Hex.Hex` The data to decode. ### options * **Type:** `decodeData.Options` * **Optional** Decoding options. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. ## Return Type `decodeData.ReturnType>` # AbiFunction.decodeResult ABI-decodes a function's result according to the ABI Item's output types (`outputs`). :::tip This function is typically used to decode contract function return values (e.g. the response of an `eth_call` or the `input` property of a Transaction). See the [End-to-end Example](#end-to-end). ::: ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const data = '0x000000000000000000000000000000000000000000000000000000000000002a' const totalSupply = AbiFunction.from( 'function totalSupply() returns (uint256)' ) const output = AbiFunction.decodeResult(totalSupply, data) // @log: 42n ``` You can extract an ABI Function from a JSON ABI with [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const data = '0x000000000000000000000000000000000000000000000000000000000000002a' const erc20Abi = Abi.from([...]) // [!code hl] const totalSupply = AbiFunction.fromAbi(erc20Abi, 'totalSupply') // [!code hl] const output = AbiFunction.decodeResult(totalSupply, data) // @log: 42n ``` ### ABI-shorthand You can also specify an entire ABI object and a function name as parameters to [`AbiFunction.decodeResult`](/api/AbiFunction/decodeResult): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const data = '0x000000000000000000000000000000000000000000000000000000000000002a' const erc20Abi = Abi.from([...]) const output = AbiFunction.decodeResult( erc20Abi, // [!code focus] 'totalSupply', // [!code focus] data ) // @log: 42n ``` ### End-to-end Below is an end-to-end example of using `AbiFunction.decodeResult` to decode the result of a `balanceOf` contract call on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2). ```ts twoslash import 'ox/window' import { Abi, AbiFunction } from 'ox' // 1. Extract the Function from the Contract's ABI. const abi = Abi.from([ // ... { name: 'balanceOf', type: 'function', inputs: [{ name: 'account', type: 'address' }], outputs: [{ name: 'balance', type: 'uint256' }], stateMutability: 'view' } // ... ]) const balanceOf = AbiFunction.fromAbi(abi, 'balanceOf') // 2. Encode the Function Input. const data = AbiFunction.encodeData(balanceOf, [ '0xd2135CfB216b74109775236E36d4b433F1DF507B' ]) // 3. Perform the Contract Call. const response = await window.ethereum!.request({ method: 'eth_call', params: [ { data, to: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2' } ] }) // 4. Decode the Function Output. // [!code focus] const balance = AbiFunction.decodeResult( balanceOf, response ) // [!code focus] // @log: 42n ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function decodeResult( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), data: Hex.Hex, options?: decodeResult.Options, ): decodeResult.ReturnType ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L383) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### data * **Type:** `Hex.Hex` ABI-encoded function output ### options * **Type:** `decodeResult.Options` * **Optional** Decoding options #### options.as * **Type:** `"Array" | "Object" | as` * **Optional** Whether the decoded values should be returned as an `Object` or `Array`. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. ## Return Type Decoded function output `decodeResult.ReturnType` # AbiFunction.encodeData ABI-encodes function arguments (`inputs`), prefixed with the 4 byte function selector. :::tip This function is typically used to encode a contract function and its arguments for contract calls (e.g. `data` parameter of an `eth_call` or `eth_sendTransaction`). See the [End-to-end Example](#end-to-end). ::: ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from( 'function approve(address, uint256)' ) const data = AbiFunction.encodeData( // [!code focus] approve, // [!code focus] ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n] // [!code focus] ) // [!code focus] // @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c' ``` You can extract an ABI Function from a JSON ABI with [`AbiFunction.fromAbi`](/api/AbiFunction/fromAbi): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const erc20Abi = Abi.from([...]) // [!code hl] const approve = AbiFunction.fromAbi(erc20Abi, 'approve') // [!code hl] const data = AbiFunction.encodeData( approve, ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n] ) // @log: '0x095ea7b3000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000000000000000000000000000000000000000010f2c' ``` ### ABI-shorthand You can specify an entire ABI object and a function name as parameters to [`AbiFunction.encodeData`](/api/AbiFunction/encodeData): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const erc20Abi = Abi.from([...]) const data = AbiFunction.encodeData( erc20Abi, // [!code focus] 'approve', // [!code focus] ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 69420n] ) ``` ### End-to-end Below is an end-to-end example of using `AbiFunction.encodeData` to encode the input of a `balanceOf` contract call on the [Wagmi Mint Example contract](https://etherscan.io/address/0xfba3912ca04dd458c843e2ee08967fc04f3579c2). ```ts twoslash import 'ox/window' import { Abi, AbiFunction } from 'ox' // 1. Extract the Function from the Contract's ABI. const abi = Abi.from([ // ... { name: 'balanceOf', type: 'function', inputs: [{ name: 'account', type: 'address' }], outputs: [{ name: 'balance', type: 'uint256' }], stateMutability: 'view' } // ... ]) const balanceOf = AbiFunction.fromAbi(abi, 'balanceOf') // 2. Encode the Function Input. // [!code focus] const data = AbiFunction.encodeData( // [!code focus] balanceOf, // [!code focus] ['0xd2135CfB216b74109775236E36d4b433F1DF507B'] // [!code focus] ) // [!code focus] // 3. Perform the Contract Call. const response = await window.ethereum!.request({ method: 'eth_call', params: [ { data, to: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2' } ] }) // 4. Decode the Function Output. const balance = AbiFunction.decodeResult( balanceOf, response ) ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function encodeData( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), args: encodeData.Args, ): Hex.Hex ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L615) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### args * **Type:** `encodeData.Args` Function arguments ## Return Type ABI-encoded function name and arguments `Hex.Hex` # AbiFunction.encodeResult ABI-encodes a function's result (`outputs`). ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const totalSupply = AbiFunction.from( 'function totalSupply() returns (uint256)' ) const output = AbiFunction.decodeResult( totalSupply, '0x000000000000000000000000000000000000000000000000000000000000002a' ) // 42n const data = AbiFunction.encodeResult(totalSupply, 42n) // [!code focus] // @log: '0x000000000000000000000000000000000000000000000000000000000000002a' ``` ### ABI-shorthand You can also specify an entire ABI object and a function name as parameters to [`AbiFunction.encodeResult`](/api/AbiFunction/encodeResult): ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const abi = Abi.from([...]) const data = AbiFunction.encodeResult( abi, // [!code focus] 'totalSupply', // [!code focus] 42n ) // @log: '0x000000000000000000000000000000000000000000000000000000000000002a' ``` ## Definition ```ts function encodeResult( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), output: encodeResult.Output, options?: encodeResult.Options, ): Hex.Hex ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L744) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` ### output * **Type:** `encodeResult.Output` The function output to encode. ### options * **Type:** `encodeResult.Options` * **Optional** Encoding options. #### options.as * **Type:** `"Array" | "Object" | as` * **Optional** ## Return Type The encoded function output. `Hex.Hex` # AbiFunction.format Formats an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) into a **Human Readable ABI Function**. ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const formatted = AbiFunction.format({ type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] }) formatted // ^? ``` ## Definition ```ts function format( abiFunction: abiFunction | AbiFunction, ): format.ReturnType ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L866) ## Parameters ### abiFunction * **Type:** `abiFunction | AbiFunction` The ABI Function to format. ## Return Type The formatted ABI Function. `format.ReturnType` # AbiFunction.from Parses an arbitrary **JSON ABI Function** or **Human Readable ABI Function** into a typed [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction). ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from({ type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] }) approve //^? ``` ### Human Readable ABIs A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from( 'function approve(address spender, uint256 amount) returns (bool)' // [!code hl] ) approve //^? ``` It is possible to specify `struct`s along with your definitions: ```ts twoslash import { AbiFunction } from 'ox' const approve = AbiFunction.from([ 'struct Foo { address spender; uint256 amount; }', // [!code hl] 'function approve(Foo foo) returns (bool)' ]) approve //^? ``` ## Definition ```ts function from( abiFunction: (abiFunction | AbiFunction | string | readonly string[]) & ((abiFunction extends string ? internal.Signature : never) | (abiFunction extends readonly string[] ? internal.Signatures : never) | AbiFunction), options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L945) ## Parameters ### abiFunction * **Type:** `(abiFunction | AbiFunction | string | readonly string[]) & ((abiFunction extends string ? internal.Signature : never) | (abiFunction extends readonly string[] ? internal.Signatures : never) | AbiFunction)` The ABI Function to parse. ### options * **Type:** `from.Options` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted function (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type Typed ABI Function. `from.ReturnType` # AbiFunction.fromAbi Extracts an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ### Extracting by Name ABI Functions can be extracted by their name using the `name` option: ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiFunction.fromAbi(abi, 'foo') // [!code focus] // ^? ``` ### Extracting by Selector ABI Functions can be extract by their selector when [`Hex.Hex`](/api/Hex/types#hex) is provided to `name`. ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiFunction.fromAbi(abi, '0x095ea7b3') // [!code focus] // ^? ``` :::note Extracting via a hex selector is useful when extracting an ABI Function from an `eth_call` RPC response or from a Transaction `input`. ::: ## Definition ```ts function fromAbi( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), options?: AbiItem.fromAbi.Options>, ): AbiItem.fromAbi.ReturnType ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L1029) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract from. ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` The name (or selector) of the ABI item to extract. ### options * **Type:** `AbiItem.fromAbi.Options>` * **Optional** Extraction options. #### options.args * **Type:** `allArgs | (Widen & (args extends allArgs ? unknown : never))` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted item (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type The ABI item. `AbiItem.fromAbi.ReturnType` # AbiFunction.getSelector Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction). Useful for computing function selectors for calldata. ## Imports :::code-group ```ts [Named] import { AbiFunction } from 'ox' ``` ```ts [Entrypoint] import * as AbiFunction from 'ox/AbiFunction' ``` ::: ## Examples ```ts twoslash import { AbiFunction } from 'ox' const selector = AbiFunction.getSelector( 'function ownerOf(uint256 tokenId)' ) // @log: '0x6352211e' ``` ```ts twoslash import { AbiFunction } from 'ox' const selector = AbiFunction.getSelector({ inputs: [{ type: 'uint256' }], name: 'ownerOf', outputs: [], stateMutability: 'view', type: 'function' }) // @log: '0x6352211e' ``` ## Definition ```ts function getSelector( abiItem: string | AbiFunction, ): Hex.Hex ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L1088) ## Parameters ### abiItem * **Type:** `string | AbiFunction` The ABI item to compute the selector for. #### abiItem.hash * **Type:** `0x${string}` * **Optional** #### abiItem.overloads * **Type:** `readonly any[]` * **Optional** ## Return Type The first 4 bytes of the [`Hash.keccak256`](/api/Hash/keccak256) hash of the function signature. `Hex.Hex` # AbiFunction Types ## `AbiFunction.AbiFunction` Root type for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) with a `function` type. **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L14) ## `AbiFunction.ExtractNames` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L59) ## `AbiFunction.FromAbi` Extracts an [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. ### Examples ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function foo(string)', 'function bar(uint256)' ]) type Foo = AbiFunction.FromAbi // ^? ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L35) ## `AbiFunction.Name` Extracts the names of all [`AbiFunction.AbiFunction`](/api/AbiFunction/types#abifunction) items in an [`Abi.Abi`](/api/Abi/types#abi). ### Examples ```ts twoslash import { Abi, AbiFunction } from 'ox' const abi = Abi.from([ 'function foo(string)', 'function bar(uint256)' ]) type names = AbiFunction.Name // ^? ``` **Source:** [src/core/AbiFunction.ts](https://github.com/wevm/ox/blob/main/src/core/AbiFunction.ts#L56) # AbiItem Utilities & types for working with [ABI Items](https://docs.soliditylang.org/en/latest/abi-spec.html#json) The `AbiItem` type is a super-type of: * [`AbiConstructor`](/api/AbiConstructor) * [`AbiFunction`](/api/AbiFunction) * [`AbiEvent`](/api/AbiEvent) * [`AbiError`](/api/AbiError) ## Examples Below are some examples demonstrating common usages of the `AbiItem` module: * [Instantiating via JSON ABI](#instantiating-via-json-abi) * [Instantiating via Human-Readable ABI Item](#instantiating-via-human-readable-abi-item) * [Formatting ABI Items](#formatting-abi-items) ### Instantiating via JSON ABI An `AbiItem` can be instantiated from a JSON ABI by using [`AbiItem.fromAbi`](/api/AbiItem/fromAbi): ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiItem.fromAbi(abi, 'Transfer') // [!code focus] // ^? ``` ### Instantiating via Human-Readable ABI Item A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiItem } from 'ox' const abiItem = AbiItem.from( 'function approve(address spender, uint256 amount) returns (bool)' ) abiItem //^? ``` ### Formatting ABI Items An `AbiItem` can be formatted into a human-readable ABI Item by using [`AbiItem.format`](/api/AbiItem/format): ```ts twoslash import { AbiItem } from 'ox' const abiItem = AbiItem.from( 'function approve(address spender, uint256 amount) returns (bool)' ) const formatted = AbiItem.format(abiItem) // @log: 'function approve(address spender, uint256 amount) returns (bool)' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiItem.format`](/api/AbiItem/format) | Formats an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) into a **Human Readable ABI Item**. | | [`AbiItem.from`](/api/AbiItem/from) | Parses an arbitrary **JSON ABI Item** or **Human Readable ABI Item** into a typed [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). | | [`AbiItem.fromAbi`](/api/AbiItem/fromAbi) | Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. | | [`AbiItem.getSelector`](/api/AbiItem/getSelector) | Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). | | [`AbiItem.getSignature`](/api/AbiItem/getSignature) | Computes the stringified signature for a given [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). | | [`AbiItem.getSignatureHash`](/api/AbiItem/getSignatureHash) | Computes the signature hash for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AbiItem.AmbiguityError`](/api/AbiItem/errors#abiitemambiguityerror) | Throws when ambiguous types are found on overloaded ABI items. | | [`AbiItem.InvalidAbiItemError`](/api/AbiItem/errors#abiiteminvalidabiitemerror) | | | [`AbiItem.InvalidSelectorSizeError`](/api/AbiItem/errors#abiiteminvalidselectorsizeerror) | Throws when the selector size is invalid. | | [`AbiItem.NotFoundError`](/api/AbiItem/errors#abiitemnotfounderror) | Throws when an ABI item is not found in the ABI. | | [`AbiItem.UnknownSolidityTypeError`](/api/AbiItem/errors#abiitemunknownsoliditytypeerror) | | | [`AbiItem.UnknownTypeError`](/api/AbiItem/errors#abiitemunknowntypeerror) | | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiItem.AbiItem`](/api/AbiItem/types#abiitemabiitem) | Root type for an item on an [`Abi.Abi`](/api/Abi/types#abi). | | [`AbiItem.ExtractNames`](/api/AbiItem/types#abiitemextractnames) | | | [`AbiItem.FromAbi`](/api/AbiItem/types#abiitemfromabi) | Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. | | [`AbiItem.Name`](/api/AbiItem/types#abiitemname) | Extracts the names of all [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) items in an [`Abi.Abi`](/api/Abi/types#abi). | # AbiItem.format Formats an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) into a **Human Readable ABI Item**. ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ```ts twoslash import { AbiItem } from 'ox' const formatted = AbiItem.format({ type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] }) formatted // ^? ``` ## Definition ```ts function format( abiItem: abiItem | AbiItem, ): format.ReturnType ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L97) ## Parameters ### abiItem * **Type:** `abiItem | AbiItem` The ABI Item to format. ## Return Type The formatted ABI Item . `format.ReturnType` # AbiItem.from Parses an arbitrary **JSON ABI Item** or **Human Readable ABI Item** into a typed [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ### JSON ABIs ```ts twoslash import { AbiItem } from 'ox' const abiItem = AbiItem.from({ type: 'function', name: 'approve', stateMutability: 'nonpayable', inputs: [ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ], outputs: [{ type: 'bool' }] }) abiItem //^? ``` ### Human Readable ABIs A Human Readable ABI can be parsed into a typed ABI object: ```ts twoslash import { AbiItem } from 'ox' const abiItem = AbiItem.from( 'function approve(address spender, uint256 amount) returns (bool)' // [!code hl] ) abiItem //^? ``` It is possible to specify `struct`s along with your definitions: ```ts twoslash import { AbiItem } from 'ox' const abiItem = AbiItem.from([ 'struct Foo { address spender; uint256 amount; }', // [!code hl] 'function approve(Foo foo) returns (bool)' ]) abiItem //^? ``` ## Definition ```ts function from( abiItem: (abiItem | AbiItem | string | readonly string[]) & ((abiItem extends string ? internal.Signature : never) | (abiItem extends readonly string[] ? internal.Signatures : never) | AbiItem), options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L176) ## Parameters ### abiItem * **Type:** `(abiItem | AbiItem | string | readonly string[]) & ((abiItem extends string ? internal.Signature : never) | (abiItem extends readonly string[] ? internal.Signatures : never) | AbiItem)` The ABI Item to parse. ### options * **Type:** `from.Options` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted item (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type The typed ABI Item. `from.ReturnType` # AbiItem.fromAbi Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) from an [`Abi.Abi`](/api/Abi/types#abi) given a name and optional arguments. ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ABI Items can be extracted by their name using the `name` option: ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiItem.fromAbi(abi, 'Transfer') // [!code focus] // ^? ``` ### Extracting by Selector ABI Items can be extract by their selector when [`Hex.Hex`](/api/Hex/types#hex) is provided to `name`. ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'function foo()', 'event Transfer(address owner, address to, uint256 tokenId)', 'function bar(string a) returns (uint256 x)' ]) const item = AbiItem.fromAbi(abi, '0x095ea7b3') // [!code focus] // ^? ``` :::note Extracting via a hex selector is useful when extracting an ABI Item from an `eth_call` RPC response, a Transaction `input`, or from Event Log `topics`. ::: ## Definition ```ts function fromAbi( abi: abi | Abi.Abi | readonly unknown[], name: Hex.Hex | (name extends allNames ? name : never), options?: fromAbi.Options, ): fromAbi.ReturnType ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L271) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` The ABI to extract from. ### name * **Type:** `Hex.Hex | (name extends allNames ? name : never)` The name (or selector) of the ABI item to extract. ### options * **Type:** `fromAbi.Options` * **Optional** Extraction options. #### options.args * **Type:** `allArgs | (Widen & (args extends allArgs ? unknown : never))` * **Optional** #### options.prepare * **Type:** `boolean` * **Optional** Whether or not to prepare the extracted item (optimization for encoding performance). When `true`, the `hash` property is computed and included in the returned value. ## Return Type The ABI item. `fromAbi.ReturnType` # AbiItem.getSelector Computes the [4-byte selector](https://solidity-by-example.org/function-selector/) for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). Useful for computing function selectors for calldata. ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ```ts twoslash import { AbiItem } from 'ox' const selector = AbiItem.getSelector( 'function ownerOf(uint256 tokenId)' ) // @log: '0x6352211e' ``` ```ts twoslash // @noErrors import { Abi, AbiItem } from 'ox' const erc20Abi = Abi.from([...]) const selector = AbiItem.getSelector(erc20Abi, 'ownerOf') // @log: '0x6352211e' ``` ```ts twoslash import { AbiItem } from 'ox' const selector = AbiItem.getSelector({ inputs: [{ type: 'uint256' }], name: 'ownerOf', outputs: [], stateMutability: 'view', type: 'function' }) // @log: '0x6352211e' ``` ## Definition ```ts function getSelector( abi: abi | Abi.Abi | readonly unknown[], name: name, ): Hex.Hex ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L478) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `name` ## Return Type The first 4 bytes of the [`Hash.keccak256`](/api/Hash/keccak256) hash of the function signature. `Hex.Hex` # AbiItem.getSignature Computes the stringified signature for a given [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ```ts twoslash import { AbiItem } from 'ox' const signature = AbiItem.getSignature( 'function ownerOf(uint256 tokenId)' ) // @log: 'ownerOf(uint256)' ``` ```ts twoslash // @noErrors import { Abi, AbiItem } from 'ox' const erc20Abi = Abi.from([...]) const signature = AbiItem.getSignature(erc20Abi, 'ownerOf') // @log: 'ownerOf(uint256)' ``` ```ts twoslash import { AbiItem } from 'ox' const signature = AbiItem.getSignature({ name: 'ownerOf', type: 'function', inputs: [{ name: 'tokenId', type: 'uint256' }], outputs: [], stateMutability: 'view' }) // @log: 'ownerOf(uint256)' ``` ## Definition ```ts function getSignature( abi: abi | Abi.Abi | readonly unknown[], name: name, ): string ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L547) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `name` ## Return Type The stringified signature of the ABI Item. `string` # AbiItem.getSignatureHash Computes the signature hash for an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem). Useful for computing Event Topic values. ## Imports :::code-group ```ts [Named] import { AbiItem } from 'ox' ``` ```ts [Entrypoint] import * as AbiItem from 'ox/AbiItem' ``` ::: ## Examples ```ts twoslash import { AbiItem } from 'ox' const hash = AbiItem.getSignatureHash( 'event Transfer(address indexed from, address indexed to, uint256 amount)' ) // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' ``` ```ts twoslash // @noErrors import { Abi, AbiItem } from 'ox' const erc20Abi = Abi.from([...]) const hash = AbiItem.getSignatureHash(erc20Abi, 'Transfer') // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' ``` ```ts twoslash import { AbiItem } from 'ox' const hash = AbiItem.getSignatureHash({ name: 'Transfer', type: 'event', inputs: [ { name: 'from', type: 'address', indexed: true }, { name: 'to', type: 'address', indexed: true }, { name: 'amount', type: 'uint256', indexed: false } ] }) // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' ``` ## Definition ```ts function getSignatureHash( abi: abi | Abi.Abi | readonly unknown[], name: name, ): Hex.Hex ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L623) ## Parameters ### abi * **Type:** `abi | Abi.Abi | readonly unknown[]` ### name * **Type:** `name` ## Return Type The [`Hash.keccak256`](/api/Hash/keccak256) hash of the ABI item's signature. `Hex.Hex` # AbiItem Errors ## `AbiItem.AmbiguityError` Throws when ambiguous types are found on overloaded ABI items. ### Examples ```ts twoslash import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function foo(bytes20)' ]) AbiFunction.fromAbi(foo, 'foo', { args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'] }) // @error: AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items. // @error: `bytes20` in `foo(bytes20)`, and // @error: `address` in `foo(address)` // @error: These types encode differently and cannot be distinguished at runtime. // @error: Remove one of the ambiguous items in the ABI. ``` ### Solution Remove one of the ambiguous types from the ABI. ```ts twoslash import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function foo(bytes20)' // [!code --] ]) AbiFunction.fromAbi(foo, 'foo', { args: ['0xA0Cf798816D4b9b9866b5330EEa46a18382f251e'] }) // @error: AbiItem.AmbiguityError: Found ambiguous types in overloaded ABI Items. // @error: `bytes20` in `foo(bytes20)`, and // @error: `address` in `foo(address)` // @error: These types encode differently and cannot be distinguished at runtime. // @error: Remove one of the ambiguous items in the ABI. ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L696) ## `AbiItem.InvalidAbiItemError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L5) ## `AbiItem.InvalidSelectorSizeError` Throws when the selector size is invalid. ### Examples ```ts twoslash import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function bar(uint)' ]) AbiFunction.fromAbi(foo, '0xaaa') // @error: AbiItem.InvalidSelectorSizeError: Selector size is invalid. Expected 4 bytes. Received 2 bytes ("0xaaa"). ``` ### Solution Ensure the selector size is 4 bytes. ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function bar(uint)' ]) AbiFunction.fromAbi(foo, '0x7af82b1a') ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L797) ## `AbiItem.NotFoundError` Throws when an ABI item is not found in the ABI. ### Examples ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function bar(uint)' ]) AbiFunction.fromAbi(foo, 'baz') // @error: AbiItem.NotFoundError: ABI function with name "baz" not found. ``` ### Solution Ensure the ABI item exists on the ABI. ```ts twoslash // @noErrors import { Abi, AbiFunction } from 'ox' const foo = Abi.from([ 'function foo(address)', 'function bar(uint)', 'function baz(bool)' // [!code ++] ]) AbiFunction.fromAbi(foo, 'baz') ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L747) ## `AbiItem.UnknownSolidityTypeError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L28) ## `AbiItem.UnknownTypeError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L16) # AbiItem Types ## `AbiItem.AbiItem` Root type for an item on an [`Abi.Abi`](/api/Abi/types#abi). **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L11) ## `AbiItem.ExtractNames` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L61) ## `AbiItem.FromAbi` Extracts an [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) item from an [`Abi.Abi`](/api/Abi/types#abi), given a name. ### Examples ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'error Foo(string)', 'function foo(string)', 'event Bar(uint256)' ]) type Foo = AbiItem.FromAbi // ^? ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L36) ## `AbiItem.Name` Extracts the names of all [`AbiItem.AbiItem`](/api/AbiItem/types#abiitem) items in an [`Abi.Abi`](/api/Abi/types#abi). ### Examples ```ts twoslash import { Abi, AbiItem } from 'ox' const abi = Abi.from([ 'error Foo(string)', 'function foo(string)', 'event Bar(uint256)' ]) type names = AbiItem.Name // ^? ``` **Source:** [src/core/AbiItem.ts](https://github.com/wevm/ox/blob/main/src/core/AbiItem.ts#L58) # AbiParameter Utilities & types for working with a single [ABI Parameter](https://docs.soliditylang.org/en/latest/abi-spec.html#types). ## Examples Below are some examples demonstrating common usages of the `AbiParameter` module: * [Instantiating Human Readable ABI Parameters](#instantiating-human-readable-abi-parameters) * [Formatting ABI Parameters](#formatting-abi-parameters) ### Instantiating Human Readable ABI Parameters A Human Readable ABI Parameter can be instantiated by using [`AbiParameter.from`](/api/AbiParameter/from): ```ts twoslash import { AbiParameter } from 'ox' const parameter = AbiParameter.from('address spender') parameter //^? ``` ### Formatting ABI Parameters An ABI Parameter can be formatted into a human-readable ABI Parameter by using [`AbiParameter.format`](/api/AbiParameter/format): ```ts twoslash import { AbiParameter } from 'ox' const formatted = AbiParameter.format({ name: 'spender', type: 'address' }) formatted // ^? ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameter.format`](/api/AbiParameter/format) | Formats an [`AbiParameter.AbiParameter`](/api/AbiParameter/types#abiparameter) into a **Human Readable ABI Parameter**. | | [`AbiParameter.from`](/api/AbiParameter/from) | Parses an arbitrary **JSON ABI Parameter** or **Human Readable ABI Parameter** into a typed [`AbiParameter.AbiParameter`](/api/AbiParameter/types#abiparameter). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameter.InvalidAbiParameterError`](/api/AbiParameter/errors#abiparameterinvalidabiparametererror) | | | [`AbiParameter.InvalidAbiTypeParameterError`](/api/AbiParameter/errors#abiparameterinvalidabitypeparametererror) | | | [`AbiParameter.InvalidFunctionModifierError`](/api/AbiParameter/errors#abiparameterinvalidfunctionmodifiererror) | | | [`AbiParameter.InvalidModifierError`](/api/AbiParameter/errors#abiparameterinvalidmodifiererror) | | | [`AbiParameter.InvalidParameterError`](/api/AbiParameter/errors#abiparameterinvalidparametererror) | | | [`AbiParameter.InvalidParenthesisError`](/api/AbiParameter/errors#abiparameterinvalidparenthesiserror) | | | [`AbiParameter.SolidityProtectedKeywordError`](/api/AbiParameter/errors#abiparametersolidityprotectedkeyworderror) | | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameter.AbiEventParameter`](/api/AbiParameter/types#abiparameterabieventparameter) | A parameter on an ABI event. | | [`AbiParameter.AbiParameter`](/api/AbiParameter/types#abiparameterabiparameter) | Root type for an ABI parameter. | # AbiParameter.format Formats an [`AbiParameter.AbiParameter`](/api/AbiParameter/types#abiparameter) into a **Human Readable ABI Parameter**. ## Imports :::code-group ```ts [Named] import { AbiParameter } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameter from 'ox/AbiParameter' ``` ::: ## Examples ```ts twoslash import { AbiParameter } from 'ox' const formatted = AbiParameter.format({ name: 'spender', type: 'address' }) formatted // ^? ``` ## Definition ```ts function format( parameter: parameter | AbiParameter | AbiEventParameter, ): format.ReturnType ``` **Source:** [src/core/AbiParameter.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameter.ts#L41) ## Parameters ### parameter * **Type:** `parameter | AbiParameter | AbiEventParameter` The ABI Parameter to format. ## Return Type The formatted ABI Parameter. `format.ReturnType` # AbiParameter.from Parses an arbitrary **JSON ABI Parameter** or **Human Readable ABI Parameter** into a typed [`AbiParameter.AbiParameter`](/api/AbiParameter/types#abiparameter). ## Imports :::code-group ```ts [Named] import { AbiParameter } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameter from 'ox/AbiParameter' ``` ::: ## Examples ### JSON Parameters ```ts twoslash import { AbiParameter } from 'ox' const parameter = AbiParameter.from({ name: 'spender', type: 'address' }) parameter //^? ``` ### Human Readable Parameters ```ts twoslash import { AbiParameter } from 'ox' const parameter = AbiParameter.from('address spender') parameter //^? ``` It is possible to specify `struct`s along with your definition: ```ts twoslash import { AbiParameter } from 'ox' const parameter = AbiParameter.from([ 'struct Foo { address spender; uint256 amount; }', 'Foo foo' ]) parameter //^? ``` ## Definition ```ts function from( parameter: parameter | AbiParameter | string | readonly string[], ): from.ReturnType ``` **Source:** [src/core/AbiParameter.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameter.ts#L105) ## Parameters ### parameter * **Type:** `parameter | AbiParameter | string | readonly string[]` The ABI Parameter to parse. ## Return Type The typed ABI Parameter. `from.ReturnType` # AbiParameter Errors ## `AbiParameter.InvalidAbiParameterError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L38) ## `AbiParameter.InvalidAbiTypeParameterError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L130) ## `AbiParameter.InvalidFunctionModifierError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L106) ## `AbiParameter.InvalidModifierError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L83) ## `AbiParameter.InvalidParameterError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L60) ## `AbiParameter.InvalidParenthesisError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L182) ## `AbiParameter.SolidityProtectedKeywordError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L70) # AbiParameter Types ## `AbiParameter.AbiEventParameter` A parameter on an ABI event. **Source:** [src/core/AbiParameter.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameter.ts#L10) ## `AbiParameter.AbiParameter` Root type for an ABI parameter. **Source:** [src/core/AbiParameter.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameter.ts#L7) # AbiParameters Utilities & types for encoding, decoding, and working with [ABI Parameters](https://docs.soliditylang.org/en/latest/abi-spec.html#types) ## Examples Below are some examples demonstrating common usages of the `AbiParameters` module: * [Encoding ABI Parameters](#encoding-abi-parameters) * [Decoding ABI Parameters](#decoding-abi-parameters) * [JSON-ABI Parameters](#json-abi-parameters) * [Human Readable ABI Parameters](#human-readable-abi-parameters) ### Encoding ABI Parameters ABI Parameters can be ABI-encoded as per the [Application Binary Interface (ABI) Specification](https://docs.soliditylang.org/en/latest/abi-spec) using [`AbiParameters.encode`](/api/AbiParameters/encode): ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.encode( AbiParameters.from('string, uint, bool'), ['wagmi', 420n, true] ) ``` :::tip The example above uses [`AbiParameters.from`](/api/AbiParameters/from) to specify human-readable ABI Parameters. However, you can also pass JSON-ABI Parameters: ```ts import { AbiParameters } from 'ox' const data = AbiParameters.encode( [{ type: 'string' }, { type: 'uint' }, { type: 'bool' }], ['wagmi', 420n, true] ) ``` ::: ### Decoding ABI Parameters ABI-encoded data can be decoded using [`AbiParameters.decode`](/api/AbiParameters/decode): ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.decode( AbiParameters.from('string, uint, bool'), '0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000057761676d69000000000000000000000000000000000000000000000000000000' ) // @log: ['wagmi', 420n, true] ``` ### JSON-ABI Parameters JSON-ABI Parameters can be instantiated using [`AbiParameters.from`](/api/AbiParameters/from): ```ts twoslash import { AbiParameters } from 'ox' const parameters = AbiParameters.from([ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ]) parameters //^? ``` ### Human Readable ABI Parameters Human Readable ABI Parameters can be instantiated using [`AbiParameters.from`](/api/AbiParameters/from): ```ts twoslash import { AbiParameters } from 'ox' const parameters = AbiParameters.from( 'address spender, uint256 amount' ) parameters //^? ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameters.decode`](/api/AbiParameters/decode) | Decodes ABI-encoded data into its respective primitive values based on ABI Parameters. | | [`AbiParameters.encode`](/api/AbiParameters/encode) | Encodes primitive values into ABI encoded data as per the [Application Binary Interface (ABI) Specification](https://docs.soliditylang.org/en/latest/abi-spec). | | [`AbiParameters.encodePacked`](/api/AbiParameters/encodePacked) | Encodes an array of primitive values to a [packed ABI encoding](https://docs.soliditylang.org/en/latest/abi-spec.html#non-standard-packed-mode). | | [`AbiParameters.format`](/api/AbiParameters/format) | Formats [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters) into **Human Readable ABI Parameters**. | | [`AbiParameters.from`](/api/AbiParameters/from) | Parses arbitrary **JSON ABI Parameters** or **Human Readable ABI Parameters** into typed [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameters.ArrayLengthMismatchError`](/api/AbiParameters/errors#abiparametersarraylengthmismatcherror) | The length of the array value does not match the length specified in the corresponding ABI parameter. | | [`AbiParameters.BytesSizeMismatchError`](/api/AbiParameters/errors#abiparametersbytessizemismatcherror) | The size of the bytes value does not match the size specified in the corresponding ABI parameter. | | [`AbiParameters.DataSizeTooSmallError`](/api/AbiParameters/errors#abiparametersdatasizetoosmallerror) | Throws when the data size is too small for the given parameters. | | [`AbiParameters.InvalidAbiParametersError`](/api/AbiParameters/errors#abiparametersinvalidabiparameterserror) | | | [`AbiParameters.InvalidAbiTypeParameterError`](/api/AbiParameters/errors#abiparametersinvalidabitypeparametererror) | | | [`AbiParameters.InvalidArrayError`](/api/AbiParameters/errors#abiparametersinvalidarrayerror) | The value provided is not a valid array as specified in the corresponding ABI parameter. | | [`AbiParameters.InvalidFunctionModifierError`](/api/AbiParameters/errors#abiparametersinvalidfunctionmodifiererror) | | | [`AbiParameters.InvalidModifierError`](/api/AbiParameters/errors#abiparametersinvalidmodifiererror) | | | [`AbiParameters.InvalidParameterError`](/api/AbiParameters/errors#abiparametersinvalidparametererror) | | | [`AbiParameters.InvalidParenthesisError`](/api/AbiParameters/errors#abiparametersinvalidparenthesiserror) | | | [`AbiParameters.InvalidTypeError`](/api/AbiParameters/errors#abiparametersinvalidtypeerror) | Throws when the ABI parameter type is invalid. | | [`AbiParameters.LengthMismatchError`](/api/AbiParameters/errors#abiparameterslengthmismatcherror) | The length of the values to encode does not match the length of the ABI parameters. | | [`AbiParameters.SolidityProtectedKeywordError`](/api/AbiParameters/errors#abiparameterssolidityprotectedkeyworderror) | | | [`AbiParameters.ZeroDataError`](/api/AbiParameters/errors#abiparameterszerodataerror) | Throws when zero data is provided, but data is expected. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparametersabiparameters) | Root type for ABI parameters. | | [`AbiParameters.PackedAbiType`](/api/AbiParameters/types#abiparameterspackedabitype) | A packed ABI type. | | [`AbiParameters.Parameter`](/api/AbiParameters/types#abiparametersparameter) | A parameter on an [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters). | # AbiParameters.decode Decodes ABI-encoded data into its respective primitive values based on ABI Parameters. ## Imports :::code-group ```ts [Named] import { AbiParameters } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameters from 'ox/AbiParameters' ``` ::: ## Examples ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.decode( AbiParameters.from(['string', 'uint', 'bool']), '0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000057761676d69000000000000000000000000000000000000000000000000000000' ) // @log: ['wagmi', 420n, true] ``` ### JSON Parameters You can pass **JSON ABI** Parameters: ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.decode( [ { name: 'x', type: 'string' }, { name: 'y', type: 'uint' }, { name: 'z', type: 'bool' } ], '0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001a4000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000057761676d69000000000000000000000000000000000000000000000000000000' ) // @log: ['wagmi', 420n, true] ``` ## Definition ```ts function decode( parameters: parameters, data: Bytes.Bytes | Hex.Hex, options?: decode.Options, ): decode.ReturnType ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L75) ## Parameters ### parameters * **Type:** `parameters` The set of ABI parameters to decode, in the shape of the `inputs` or `outputs` attribute of an ABI Item. These parameters must include valid [ABI types](https://docs.soliditylang.org/en/latest/types.html). ### data * **Type:** `Bytes.Bytes | Hex.Hex` ABI encoded data. ### options * **Type:** `decode.Options` * **Optional** Decoding options. #### options.as * **Type:** `"Array" | "Object" | as` * **Optional** Whether the decoded values should be returned as an `Object` or `Array`. #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether decoded addresses should be checksummed. ## Return Type Array of decoded values. `decode.ReturnType` # AbiParameters.encode Encodes primitive values into ABI encoded data as per the [Application Binary Interface (ABI) Specification](https://docs.soliditylang.org/en/latest/abi-spec). ## Imports :::code-group ```ts [Named] import { AbiParameters } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameters from 'ox/AbiParameters' ``` ::: ## Examples ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.encode( AbiParameters.from(['string', 'uint', 'bool']), ['wagmi', 420n, true] ) ``` ### JSON Parameters Specify **JSON ABI** Parameters as schema: ```ts twoslash import { AbiParameters } from 'ox' const data = AbiParameters.encode( [ { type: 'string', name: 'name' }, { type: 'uint', name: 'age' }, { type: 'bool', name: 'isOwner' } ], ['wagmi', 420n, true] ) ``` ## Definition ```ts function encode( parameters: parameters, values: parameters extends AbiParameters ? internal.ToPrimitiveTypes : never, options?: encode.Options, ): Hex.Hex ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L205) ## Parameters ### parameters * **Type:** `parameters` The set of ABI parameters to encode, in the shape of the `inputs` or `outputs` attribute of an ABI Item. These parameters must include valid [ABI types](https://docs.soliditylang.org/en/latest/types.html). ### values * **Type:** `parameters extends AbiParameters ? internal.ToPrimitiveTypes : never` The set of primitive values that correspond to the ABI types defined in `parameters`. ### options * **Type:** `encode.Options` * **Optional** #### options.checksumAddress * **Type:** `boolean` * **Optional** Whether addresses should be checked against their checksum. ## Return Type ABI encoded data. `Hex.Hex` # AbiParameters.encodePacked Encodes an array of primitive values to a [packed ABI encoding](https://docs.soliditylang.org/en/latest/abi-spec.html#non-standard-packed-mode). ## Imports :::code-group ```ts [Named] import { AbiParameters } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameters from 'ox/AbiParameters' ``` ::: ## Examples ```ts twoslash import { AbiParameters } from 'ox' const encoded = AbiParameters.encodePacked( ['address', 'string'], [ '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 'hello world' ] ) // @log: '0xd8da6bf26964af9d7eed9e03e53415d37aa9604568656c6c6f20776f726c64' ``` ## Definition ```ts function encodePacked( types: packedAbiTypes, values: encodePacked.Values, ): Hex.Hex ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L258) ## Parameters ### types * **Type:** `packedAbiTypes` Set of ABI types to pack encode. ### values * **Type:** `encodePacked.Values` The set of primitive values that correspond to the ABI types defined in `types`. #### values.type * **Type:** `packedAbiTypes[key]` ## Return Type The encoded packed data. `Hex.Hex` # AbiParameters.format Formats [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters) into **Human Readable ABI Parameters**. ## Imports :::code-group ```ts [Named] import { AbiParameters } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameters from 'ox/AbiParameters' ``` ::: ## Examples ```ts twoslash import { AbiParameters } from 'ox' const formatted = AbiParameters.format([ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ]) formatted // ^? ``` ## Definition ```ts function format( parameters: parameters | readonly [ Parameter | abitype.AbiEventParameter, ...(readonly (Parameter | abitype.AbiEventParameter)[]) ], ): format.ReturnType ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L379) ## Parameters ### parameters * **Type:** `parameters | readonly [ Parameter | abitype.AbiEventParameter, ...(readonly (Parameter | abitype.AbiEventParameter)[]) ]` The ABI Parameters to format. ## Return Type The formatted ABI Parameters . `format.ReturnType` # AbiParameters.from Parses arbitrary **JSON ABI Parameters** or **Human Readable ABI Parameters** into typed [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters). ## Imports :::code-group ```ts [Named] import { AbiParameters } from 'ox' ``` ```ts [Entrypoint] import * as AbiParameters from 'ox/AbiParameters' ``` ::: ## Examples ### JSON Parameters ```ts twoslash import { AbiParameters } from 'ox' const parameters = AbiParameters.from([ { name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' } ]) parameters //^? // // // // // // // // // // // // // // // // ``` ### Human Readable Parameters Human Readable ABI Parameters can be parsed into a typed [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters): ```ts twoslash import { AbiParameters } from 'ox' const parameters = AbiParameters.from( 'address spender, uint256 amount' ) parameters //^? // // // // // // // // // // // // // // // // ``` It is possible to specify `struct`s along with your definitions: ```ts twoslash import { AbiParameters } from 'ox' const parameters = AbiParameters.from([ 'struct Foo { address spender; uint256 amount; }', // [!code hl] 'Foo foo, address bar' ]) parameters //^? // // // // // // // // // // // // // // // // ``` ## Definition ```ts function from( parameters: parameters | AbiParameters | string | readonly string[], ): from.ReturnType ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L514) ## Parameters ### parameters * **Type:** `parameters | AbiParameters | string | readonly string[]` The ABI Parameters to parse. ## Return Type The typed ABI Parameters. `from.ReturnType` # AbiParameters Errors ## `AbiParameters.ArrayLengthMismatchError` The length of the array value does not match the length specified in the corresponding ABI parameter. ### Example ```ts twoslash // @noErrors import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode(AbiParameters.from('uint256[3]'), [ [69n, 420n] ]) // ↑ expected: 3 ↑ ❌ length: 2 // @error: AbiParameters.ArrayLengthMismatchError: ABI encoding array length mismatch // @error: for type `uint256[3]`. Expected: `3`. Given: `2`. ``` ### Solution Pass an array of the correct length. ```ts twoslash import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode(AbiParameters.from(['uint256[3]']), [ [69n, 420n, 69n] ]) // ↑ ✅ length: 3 ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L651) ## `AbiParameters.BytesSizeMismatchError` The size of the bytes value does not match the size specified in the corresponding ABI parameter. ### Example ```ts twoslash // @noErrors import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode(AbiParameters.from('bytes8'), [ ['0xdeadbeefdeadbeefdeadbeef'] ]) // ↑ expected: 8 bytes ↑ ❌ size: 12 bytes // @error: BytesSizeMismatchError: Size of bytes "0xdeadbeefdeadbeefdeadbeef" // @error: (bytes12) does not match expected size (bytes8). ``` ### Solution Pass a bytes value of the correct size. ```ts twoslash import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode(AbiParameters.from(['bytes8']), [ '0xdeadbeefdeadbeef' ]) // ↑ ✅ size: 8 bytes ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L698) ## `AbiParameters.DataSizeTooSmallError` Throws when the data size is too small for the given parameters. ### Examples ```ts twoslash import { AbiParameters } from 'ox' AbiParameters.decode([{ type: 'uint256' }], '0x010f') // ↑ ❌ 2 bytes // @error: AbiParameters.DataSizeTooSmallError: Data size of 2 bytes is too small for given parameters. // @error: Params: (uint256) // @error: Data: 0x010f (2 bytes) ``` ### Solution Pass a valid data size. ```ts twoslash import { AbiParameters } from 'ox' AbiParameters.decode( [{ type: 'uint256' }], '0x00000000000000000000000000000000000000000000000000000000000010f' ) // ↑ ✅ 32 bytes ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L566) ## `AbiParameters.InvalidAbiParametersError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L49) ## `AbiParameters.InvalidAbiTypeParameterError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L130) ## `AbiParameters.InvalidArrayError` The value provided is not a valid array as specified in the corresponding ABI parameter. ### Example ```ts twoslash // @noErrors import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode( AbiParameters.from(['uint256[3]']), [69] ) ``` ### Solution Pass an array value. **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L779) ## `AbiParameters.InvalidFunctionModifierError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L106) ## `AbiParameters.InvalidModifierError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L83) ## `AbiParameters.InvalidParameterError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L60) ## `AbiParameters.InvalidParenthesisError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L182) ## `AbiParameters.InvalidTypeError` Throws when the ABI parameter type is invalid. ### Examples ```ts twoslash import { AbiParameters } from 'ox' AbiParameters.decode( [{ type: 'lol' }], '0x00000000000000000000000000000000000000000000000000000000000010f' ) // ↑ ❌ invalid type // @error: AbiParameters.InvalidTypeError: Type `lol` is not a valid ABI Type. ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L801) ## `AbiParameters.LengthMismatchError` The length of the values to encode does not match the length of the ABI parameters. ### Example ```ts twoslash // @noErrors import { AbiParameters } from 'ox' // ---cut--- AbiParameters.encode( AbiParameters.from(['string', 'uint256']), ['hello'] ) // @error: LengthMismatchError: ABI encoding params/values length mismatch. // @error: Expected length (params): 2 // @error: Given length (values): 1 ``` ### Solution Pass the correct number of values to encode. ### Solution Pass a [valid ABI type](https://docs.soliditylang.org/en/develop/abi-spec.html#types). **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L741) ## `AbiParameters.SolidityProtectedKeywordError` **Source:** [src/core/internal/human-readable/errors.ts](https://github.com/wevm/ox/blob/main/src/core/internal/human-readable/errors.ts#L70) ## `AbiParameters.ZeroDataError` Throws when zero data is provided, but data is expected. ### Examples ```ts twoslash import { AbiParameters } from 'ox' AbiParameters.decode([{ type: 'uint256' }], '0x') // ↑ ❌ zero data // @error: AbiParameters.DataSizeTooSmallError: Data size of 2 bytes is too small for given parameters. // @error: Params: (uint256) // @error: Data: 0x010f (2 bytes) ``` ### Solution Pass valid data. ```ts twoslash import { AbiParameters } from 'ox' AbiParameters.decode( [{ type: 'uint256' }], '0x00000000000000000000000000000000000000000000000000000000000010f' ) // ↑ ✅ 32 bytes ``` **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L614) # AbiParameters Types ## `AbiParameters.AbiParameters` Root type for ABI parameters. **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L13) ## `AbiParameters.PackedAbiType` A packed ABI type. **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L29) ## `AbiParameters.Parameter` A parameter on an [`AbiParameters.AbiParameters`](/api/AbiParameters/types#abiparameters). **Source:** [src/core/AbiParameters.ts](https://github.com/wevm/ox/blob/main/src/core/AbiParameters.ts#L16) # Address Utility functions for working with Ethereum addresses. ## Examples Below are some examples demonstrating common usages of the `Address` module: * [Instantiating Addresses](#instantiating-addresses) * [Validating Addresses](#validating-addresses) * [Addresses from ECDSA Public Keys](#addresses-from-ecdsa-public-keys) ### Instantiating Addresses An [`Address.Address`](/api/Address/types#address) can be instantiated from a hex string using [`Address.from`](/api/Address/from): ```ts twoslash import { Address } from 'ox' const address = Address.from( '0xa0cf798816d4b9b9866b5330eea46a18382f251e' ) // @log: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ``` ### Validating Addresses The [`Address.validate`](/api/Address/validate) function will return `true` if the address is valid, and `false` otherwise: ```ts twoslash import { Address } from 'ox' const valid = Address.validate( '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ) // @log: true ``` The [`Address.assert`](/api/Address/assert) function will throw an error if the address is invalid: ```ts twoslash import { Address } from 'ox' Address.assert('0xdeadbeef') // @error: InvalidAddressError: Address "0xdeadbeef" is invalid. ``` ### Addresses from ECDSA Public Keys An [`Address.Address`](/api/Address/types#address) can be computed from an ECDSA public key using [`Address.fromPublicKey`](/api/Address/fromPublicKey): ```ts twoslash import { Address, Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const publicKey = Secp256k1.getPublicKey({ privateKey }) const address = Address.fromPublicKey(publicKey) // @log: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Address.assert`](/api/Address/assert) | Asserts that the given value is a valid [`Address.Address`](/api/Address/types#address). | | [`Address.checksum`](/api/Address/checksum) | Computes the checksum address for the given [`Address.Address`](/api/Address/types#address). | | [`Address.from`](/api/Address/from) | Converts a stringified address to a typed (optionally checksummed) [`Address.Address`](/api/Address/types#address). | | [`Address.fromPublicKey`](/api/Address/fromPublicKey) | Converts an ECDSA public key to an [`Address.Address`](/api/Address/types#address). | | [`Address.isEqual`](/api/Address/isEqual) | Checks if two [`Address.Address`](/api/Address/types#address) are equal. | | [`Address.validate`](/api/Address/validate) | Checks if the given address is a valid [`Address.Address`](/api/Address/types#address). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Address.InvalidAddressError`](/api/Address/errors#addressinvalidaddresserror) | Thrown when an address is invalid. | | [`Address.InvalidChecksumError`](/api/Address/errors#addressinvalidchecksumerror) | Thrown when an address does not match its checksum counterpart. | | [`Address.InvalidInputError`](/api/Address/errors#addressinvalidinputerror) | Thrown when an address is not a 20 byte (40 hexadecimal character) value. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Address.Address`](/api/Address/types#addressaddress) | Root type for Address. | # Address.assert Asserts that the given value is a valid [`Address.Address`](/api/Address/types#address). ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address } from 'ox' Address.assert('0xA0Cf798816D4b9b9866b5330EEa46a18382f251e') ``` ```ts twoslash import { Address } from 'ox' Address.assert('0xdeadbeef') // @error: InvalidAddressError: Address "0xdeadbeef" is invalid. ``` ## Definition ```ts function assert( value: string, options?: assert.Options, ): asserts value is Address ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L33) ## Parameters ### value * **Type:** `string` Value to assert if it is a valid address. ### options * **Type:** `assert.Options` * **Optional** Assertion options. #### options.strict * **Type:** `boolean` * **Optional** Enables strict mode. Whether or not to compare the address against its checksum. # Address.checksum Computes the checksum address for the given [`Address.Address`](/api/Address/types#address). ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address } from 'ox' Address.checksum( '0xa0cf798816d4b9b9866b5330eea46a18382f251e' ) // @log: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ``` ## Definition ```ts function checksum( address: string, ): Address ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L83) ## Parameters ### address * **Type:** `string` The address to compute the checksum for. ## Return Type The checksummed address. `Address` # Address.from Converts a stringified address to a typed (optionally checksummed) [`Address.Address`](/api/Address/types#address). ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address } from 'ox' Address.from('0xa0cf798816d4b9b9866b5330eea46a18382f251e') // @log: '0xa0cf798816d4b9b9866b5330eea46a18382f251e' ``` ```ts twoslash import { Address } from 'ox' Address.from('0xa0cf798816d4b9b9866b5330eea46a18382f251e', { checksum: true }) // @log: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ``` ```ts twoslash import { Address } from 'ox' Address.from('hello') // @error: InvalidAddressError: Address "0xa" is invalid. ``` ## Definition ```ts function from( address: string, options?: from.Options, ): Address ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L170) ## Parameters ### address * **Type:** `string` An address string to convert to a typed Address. ### options * **Type:** `from.Options` * **Optional** Conversion options. #### options.checksum * **Type:** `boolean` * **Optional** Whether to checksum the address. ## Return Type The typed Address. `Address` # Address.fromPublicKey Converts an ECDSA public key to an [`Address.Address`](/api/Address/types#address). ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address, PublicKey } from 'ox' const publicKey = PublicKey.from( '0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ) const address = Address.fromPublicKey(publicKey) // @log: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' ``` ## Definition ```ts function fromPublicKey( publicKey: PublicKey.PublicKey, options?: fromPublicKey.Options, ): Address ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L211) ## Parameters ### publicKey * **Type:** `PublicKey.PublicKey` The ECDSA public key to convert to an [`Address.Address`](/api/Address/types#address). #### publicKey.prefix * **Type:** `numberType` #### publicKey.x * **Type:** `0x${string}` #### publicKey.y * **Type:** `0x${string}` ### options * **Type:** `fromPublicKey.Options` * **Optional** Conversion options. #### options.checksum * **Type:** `boolean` * **Optional** Whether to checksum the address. ## Return Type The [`Address.Address`](/api/Address/types#address) corresponding to the public key. `Address` # Address.isEqual Checks if two [`Address.Address`](/api/Address/types#address) are equal. ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address } from 'ox' Address.isEqual( '0xa0cf798816d4b9b9866b5330eea46a18382f251e', '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ) // @log: true ``` ```ts twoslash import { Address } from 'ox' Address.isEqual( '0xa0cf798816d4b9b9866b5330eea46a18382f251e', '0xA0Cf798816D4b9b9866b5330EEa46a18382f251f' ) // @log: false ``` ## Definition ```ts function isEqual( addressA: Address, addressB: Address, ): boolean ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L266) ## Parameters ### addressA * **Type:** `Address` The first address to compare. ### addressB * **Type:** `Address` The second address to compare. ## Return Type Whether the addresses are equal. `boolean` # Address.validate Checks if the given address is a valid [`Address.Address`](/api/Address/types#address). ## Imports :::code-group ```ts [Named] import { Address } from 'ox' ``` ```ts [Entrypoint] import * as Address from 'ox/Address' ``` ::: ## Examples ```ts twoslash import { Address } from 'ox' Address.validate( '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e' ) // @log: true ``` ```ts twoslash import { Address } from 'ox' Address.validate('0xdeadbeef') // @log: false ``` ## Definition ```ts function validate( address: string, options?: validate.Options, ): address is Address ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L310) ## Parameters ### address * **Type:** `string` Value to check if it is a valid address. ### options * **Type:** `validate.Options` * **Optional** Check options. #### options.strict * **Type:** `boolean` * **Optional** Enables strict mode. Whether or not to compare the address against its checksum. ## Return Type Whether the address is a valid address. `address is Address` # Address Errors ## `Address.InvalidAddressError` Thrown when an address is invalid. ### Examples ```ts twoslash import { Address } from 'ox' Address.from('0x123') // @error: Address.InvalidAddressError: Address `0x123` is invalid. ``` **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L343) ## `Address.InvalidChecksumError` Thrown when an address does not match its checksum counterpart. **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L367) ## `Address.InvalidInputError` Thrown when an address is not a 20 byte (40 hexadecimal character) value. **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L358) # Address Types ## `Address.Address` Root type for Address. **Source:** [src/core/Address.ts](https://github.com/wevm/ox/blob/main/src/core/Address.ts#L10) # ContractAddress Utility functions for computing Contract Addresses. ## Examples Below are some examples demonstrating common usages of the `ContractAddress` module: * [Computing Contract Addresses (CREATE)](#computing-contract-addresses-\(create\)) * [Computing Contract Addresses (CREATE2)](#computing-contract-addresses-\(create2\)) ### Computing Contract Addresses (CREATE) A Contract Address that was instantiated using the `CREATE` opcode can be computed using [`ContractAddress.fromCreate`](/api/ContractAddress/fromCreate): ```ts twoslash import { ContractAddress } from 'ox' ContractAddress.fromCreate({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', nonce: 0n }) // @log: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ``` ### Computing Contract Addresses (CREATE2) A Contract Address that was instantiated using the `CREATE2` opcode can be computed using [`ContractAddress.fromCreate2`](/api/ContractAddress/fromCreate2): ```ts twoslash import { Bytes, ContractAddress, Hex } from 'ox' ContractAddress.fromCreate2({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', bytecode: Bytes.from( '0x6394198df16000526103ff60206004601c335afa6040516060f3' ), salt: Hex.fromString('hello world') }) // @log: '0x59fbB593ABe27Cb193b6ee5C5DC7bbde312290aB' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`ContractAddress.from`](/api/ContractAddress/from) | Computes Contract Address generated by the [CREATE](https://ethereum.stackexchange.com/questions/68943/create-opcode-what-does-it-really-do/68945#68945) or [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode. | | [`ContractAddress.fromCreate`](/api/ContractAddress/fromCreate) | Computes contract address via [CREATE](https://ethereum.stackexchange.com/questions/68943/create-opcode-what-does-it-really-do/68945#68945) opcode. | | [`ContractAddress.fromCreate2`](/api/ContractAddress/fromCreate2) | Computes contract address via [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode. | # ContractAddress.from Computes Contract Address generated by the [CREATE](https://ethereum.stackexchange.com/questions/68943/create-opcode-what-does-it-really-do/68945#68945) or [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode. ## Imports :::code-group ```ts [Named] import { ContractAddress } from 'ox' ``` ```ts [Entrypoint] import * as ContractAddress from 'ox/ContractAddress' ``` ::: ## Examples ### CREATE Computes via the [CREATE](https://ethereum.stackexchange.com/questions/68943/create-opcode-what-does-it-really-do/68945#68945) opcode. Shorthand for [`ContractAddress.fromCreate`](/api/ContractAddress/fromCreate). ```ts twoslash import { ContractAddress } from 'ox' ContractAddress.from({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', nonce: 0n }) // @log: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ``` ### CREATE2 Computes via the [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode. Shorthand for [`ContractAddress.fromCreate2`](/api/ContractAddress/fromCreate2). ```ts twoslash import { ContractAddress, Hex } from 'ox' ContractAddress.from({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', bytecode: '0x6394198df16000526103ff60206004601c335afa6040516060f3', salt: Hex.fromString('hello world') }) // @log: '0x59fbB593ABe27Cb193b6ee5C5DC7bbde312290aB' ``` ## Definition ```ts function from( options: from.Options, ): Address.Address ``` **Source:** [src/core/ContractAddress.ts](https://github.com/wevm/ox/blob/main/src/core/ContractAddress.ts#L45) ## Parameters ### options * **Type:** `from.Options` Options. #### options.bytecode * **Type:** `0x${string} | Uint8Array` #### options.bytecodeHash * **Type:** `0x${string} | Uint8Array` #### options.from * **Type:** `abitype_Address` #### options.nonce * **Type:** `bigint` The nonce of the transaction which deployed the contract. #### options.salt * **Type:** `0x${string} | Uint8Array` ## Return Type Contract Address. `Address.Address` # ContractAddress.fromCreate Computes contract address via [CREATE](https://ethereum.stackexchange.com/questions/68943/create-opcode-what-does-it-really-do/68945#68945) opcode. ## Imports :::code-group ```ts [Named] import { ContractAddress } from 'ox' ``` ```ts [Entrypoint] import * as ContractAddress from 'ox/ContractAddress' ``` ::: ## Examples ```ts twoslash import { ContractAddress } from 'ox' ContractAddress.fromCreate({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', nonce: 0n }) // @log: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2' ``` ## Definition ```ts function fromCreate( options: fromCreate.Options, ): Address.Address ``` **Source:** [src/core/ContractAddress.ts](https://github.com/wevm/ox/blob/main/src/core/ContractAddress.ts#L76) ## Parameters ### options * **Type:** `fromCreate.Options` Options for retrieving address. #### options.from * **Type:** `abitype_Address` The address the contract was deployed from. #### options.nonce * **Type:** `bigint` The nonce of the transaction which deployed the contract. ## Return Type Contract Address. `Address.Address` # ContractAddress.fromCreate2 Computes contract address via [CREATE2](https://eips.ethereum.org/EIPS/eip-1014) opcode. ## Imports :::code-group ```ts [Named] import { ContractAddress } from 'ox' ``` ```ts [Entrypoint] import * as ContractAddress from 'ox/ContractAddress' ``` ::: ## Examples ```ts twoslash import { ContractAddress, Hex } from 'ox' ContractAddress.fromCreate2({ from: '0x1a1e021a302c237453d3d45c7b82b19ceeb7e2e6', bytecode: '0x6394198df16000526103ff60206004601c335afa6040516060f3', salt: Hex.fromString('hello world') }) // @log: '0x59fbB593ABe27Cb193b6ee5C5DC7bbde312290aB' ``` ## Definition ```ts function fromCreate2( options: fromCreate2.Options, ): Address.Address ``` **Source:** [src/core/ContractAddress.ts](https://github.com/wevm/ox/blob/main/src/core/ContractAddress.ts#L123) ## Parameters ### options * **Type:** `fromCreate2.Options` Options for retrieving address. #### options.bytecode * **Type:** `0x${string} | Uint8Array` #### options.bytecodeHash * **Type:** `0x${string} | Uint8Array` #### options.from * **Type:** `abitype_Address` #### options.salt * **Type:** `0x${string} | Uint8Array` ## Return Type Contract Address. `Address.Address` # Authorization Utility functions for working with [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization lists & tuples. ## Examples Below are some examples demonstrating common usages of the `Authorization` module: * [Instantiating Authorizations](#instantiating-authorizations) * [Computing Sign Payload](#computing-sign-payload) * [Attaching Signatures to Authorizations](#attaching-signatures-to-authorizations) ### Instantiating Authorizations An Authorization can be instantiated using [`Authorization.from`](/api/Authorization/from): ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) ``` ### Computing Sign Payload A signing payload can be computed using [`Authorization.getSignPayload`](/api/Authorization/getSignPayload). The result can then be passed to signing functions like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Authorization, Secp256k1 } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const payload = Authorization.getSignPayload(authorization) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ### Attaching Signatures to Authorizations A signature can be attached to an Authorization using [`Authorization.from`](/api/Authorization/from): ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', chainId: 1, nonce: 40n }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorization_signed = Authorization.from( authorization, { signature } ) // [!code focus] const envelope = TxEnvelopeEip7702.from({ authorizationList: [authorization_signed], chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Authorization.from`](/api/Authorization/from) | Converts an [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization object into a typed [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.fromRpc`](/api/Authorization/fromRpc) | Converts an [`Authorization.Rpc`](/api/Authorization/types#rpc) to an [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.fromRpcList`](/api/Authorization/fromRpcList) | Converts an [`Authorization.ListRpc`](/api/Authorization/types#listrpc) to an [`Authorization.List`](/api/Authorization/types#list). | | [`Authorization.fromTuple`](/api/Authorization/fromTuple) | Converts an [`Authorization.Tuple`](/api/Authorization/types#tuple) to an [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.fromTupleList`](/api/Authorization/fromTupleList) | Converts an [`Authorization.TupleList`](/api/Authorization/types#tuplelist) to an [`Authorization.List`](/api/Authorization/types#list). | | [`Authorization.getSignPayload`](/api/Authorization/getSignPayload) | Computes the sign payload for an [`Authorization.Authorization`](/api/Authorization/types#authorization) in [EIP-7702 format](https://eips.ethereum.org/EIPS/eip-7702): `keccak256('0x05' || rlp([chain_id, address, nonce]))`. | | [`Authorization.hash`](/api/Authorization/hash) | Computes the hash for an [`Authorization.Authorization`](/api/Authorization/types#authorization) in [EIP-7702 format](https://eips.ethereum.org/EIPS/eip-7702): `keccak256('0x05' || rlp([chain_id, address, nonce]))`. | | [`Authorization.toRpc`](/api/Authorization/toRpc) | Converts an [`Authorization.Authorization`](/api/Authorization/types#authorization) to an [`Authorization.Rpc`](/api/Authorization/types#rpc). | | [`Authorization.toRpcList`](/api/Authorization/toRpcList) | Converts an [`Authorization.List`](/api/Authorization/types#list) to an [`Authorization.ListRpc`](/api/Authorization/types#listrpc). | | [`Authorization.toTuple`](/api/Authorization/toTuple) | Converts an [`Authorization.Authorization`](/api/Authorization/types#authorization) to an [`Authorization.Tuple`](/api/Authorization/types#tuple). | | [`Authorization.toTupleList`](/api/Authorization/toTupleList) | Converts an [`Authorization.List`](/api/Authorization/types#list) to an [`Authorization.TupleList`](/api/Authorization/types#tuplelist). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Authorization.Authorization`](/api/Authorization/types#authorizationauthorization) | Root type for an EIP-7702 Authorization. | | [`Authorization.List`](/api/Authorization/types#authorizationlist) | List of [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.ListRpc`](/api/Authorization/types#authorizationlistrpc) | RPC representation of an [`Authorization.List`](/api/Authorization/types#list). | | [`Authorization.ListSigned`](/api/Authorization/types#authorizationlistsigned) | Signed representation of a list of [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.Rpc`](/api/Authorization/types#authorizationrpc) | RPC representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.Signed`](/api/Authorization/types#authorizationsigned) | Signed representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.Tuple`](/api/Authorization/types#authorizationtuple) | Tuple representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.TupleList`](/api/Authorization/types#authorizationtuplelist) | Tuple representation of a list of [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.TupleListSigned`](/api/Authorization/types#authorizationtuplelistsigned) | Tuple representation of a list of signed [`Authorization.Authorization`](/api/Authorization/types#authorization). | | [`Authorization.TupleSigned`](/api/Authorization/types#authorizationtuplesigned) | Tuple representation of a signed [`Authorization.Authorization`](/api/Authorization/types#authorization). | # Authorization.from Converts an [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization object into a typed [`Authorization.Authorization`](/api/Authorization/types#authorization). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples An Authorization can be instantiated from an [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple in object format. ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) ``` ### Attaching Signatures A [`Signature.Signature`](/api/Signature/types#signature) can be attached with the `signature` option. The example below demonstrates signing an Authorization with [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Authorization, Secp256k1 } from 'ox' const authorization = Authorization.from({ address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', chainId: 1, nonce: 40n }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorization_signed = Authorization.from( authorization, { signature } ) // [!code focus] ``` ## Definition ```ts function from( authorization: authorization | Authorization, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L123) ## Parameters ### authorization * **Type:** `authorization | Authorization` An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple in object format. ### options * **Type:** `from.Options` * **Optional** Authorization options. #### options.signature * **Type:** `signature | { r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** The [`Signature.Signature`](/api/Signature/types#signature) to attach to the Authorization. ## Return Type The [`Authorization.Authorization`](/api/Authorization/types#authorization). `from.ReturnType` # Authorization.fromRpc Converts an [`Authorization.Rpc`](/api/Authorization/types#rpc) to an [`Authorization.Authorization`](/api/Authorization/types#authorization). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.fromRpc({ address: '0x0000000000000000000000000000000000000000', chainId: '0x1', nonce: '0x1', r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0' }) ``` ## Definition ```ts function fromRpc( authorization: Rpc, ): Signed ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L180) ## Parameters ### authorization * **Type:** `Rpc` The RPC-formatted Authorization. ## Return Type A signed [`Authorization.Authorization`](/api/Authorization/types#authorization). [`Signed`](/api/Authorization/types#authorizationsigned) # Authorization.fromRpcList Converts an [`Authorization.ListRpc`](/api/Authorization/types#listrpc) to an [`Authorization.List`](/api/Authorization/types#list). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorizationList = Authorization.fromRpcList([ { address: '0x0000000000000000000000000000000000000000', chainId: '0x1', nonce: '0x1', r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0' } ]) ``` ## Definition ```ts function fromRpcList( authorizationList: ListRpc, ): ListSigned ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L218) ## Parameters ### authorizationList * **Type:** [`ListRpc`](/api/Authorization/types#authorizationlistrpc) The RPC-formatted Authorization list. ## Return Type A signed [`Authorization.List`](/api/Authorization/types#list). [`ListSigned`](/api/Authorization/types#authorizationlistsigned) # Authorization.fromTuple Converts an [`Authorization.Tuple`](/api/Authorization/types#tuple) to an [`Authorization.Authorization`](/api/Authorization/types#authorization). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.fromTuple([ '0x1', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x3' ]) // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 1, // @log: nonce: 3n // @log: } ``` It is also possible to append a Signature tuple to the end of an Authorization tuple. ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.fromTuple([ '0x1', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x3', '0x1', '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064' ]) // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 1, // @log: nonce: 3n // @log: r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', // @log: s: '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064', // @log: yParity: 0, // @log: } ``` ## Definition ```ts function fromTuple( tuple: tuple, ): fromTuple.ReturnType ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L272) ## Parameters ### tuple * **Type:** `tuple` The [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple. ## Return Type The [`Authorization.Authorization`](/api/Authorization/types#authorization). `fromTuple.ReturnType` # Authorization.fromTupleList Converts an [`Authorization.TupleList`](/api/Authorization/types#tuplelist) to an [`Authorization.List`](/api/Authorization/types#list). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorizationList = Authorization.fromTupleList([ [ '0x1', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x3' ], [ '0x3', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x14' ] ]) // @log: [ // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 1, // @log: nonce: 3n, // @log: }, // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 3, // @log: nonce: 20n, // @log: }, // @log: ] ``` It is also possible to append a Signature tuple to the end of an Authorization tuple. ```ts twoslash import { Authorization } from 'ox' const authorizationList = Authorization.fromTupleList([ [ '0x1', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x3', '0x1', '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064' ], [ '0x3', '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', '0x14', '0x1', '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064' ] ]) // @log: [ // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 1, // @log: nonce: 3n, // @log: r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', // @log: s: '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064', // @log: yParity: 0, // @log: }, // @log: { // @log: address: '0xbe95c3f554e9fc85ec51be69a3d807a0d55bcf2c', // @log: chainId: 3, // @log: nonce: 20n, // @log: r: '0x68a020a209d3d56c46f38cc50a33f704f4a9a10a59377f8dd762ac66910e9b90', // @log: s: '0x7e865ad05c4035ab5792787d4a0297a43617ae897930a6fe4d822b8faea52064', // @log: yParity: 0, // @log: }, // @log: ] ``` ## Definition ```ts function fromTupleList( tupleList: tupleList, ): fromTupleList.ReturnType ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L374) ## Parameters ### tupleList * **Type:** `tupleList` The [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple list. ## Return Type An [`Authorization.List`](/api/Authorization/types#list). `fromTupleList.ReturnType` # Authorization.getSignPayload Computes the sign payload for an [`Authorization.Authorization`](/api/Authorization/types#authorization) in [EIP-7702 format](https://eips.ethereum.org/EIPS/eip-7702): `keccak256('0x05' || rlp([chain_id, address, nonce]))`. ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples The example below demonstrates computing the sign payload for an [`Authorization.Authorization`](/api/Authorization/types#authorization). This payload can then be passed to signing functions like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Authorization, Secp256k1 } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const payload = Authorization.getSignPayload(authorization) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( authorization: Authorization, ): Hex.Hex ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L417) ## Parameters ### authorization * **Type:** `Authorization` The [`Authorization.Authorization`](/api/Authorization/types#authorization). #### authorization.address * **Type:** `abitype_Address` Address of the contract to set as code for the Authority. #### authorization.chainId * **Type:** `numberType` Chain ID to authorize. #### authorization.nonce * **Type:** `bigintType` Nonce of the Authority to authorize. ## Return Type The sign payload. `Hex.Hex` # Authorization.hash Computes the hash for an [`Authorization.Authorization`](/api/Authorization/types#authorization) in [EIP-7702 format](https://eips.ethereum.org/EIPS/eip-7702): `keccak256('0x05' || rlp([chain_id, address, nonce]))`. ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const hash = Authorization.hash(authorization) // [!code focus] ``` ## Definition ```ts function hash( authorization: Authorization, options?: hash.Options, ): Hex.Hex ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L444) ## Parameters ### authorization * **Type:** `Authorization` The [`Authorization.Authorization`](/api/Authorization/types#authorization). #### authorization.address * **Type:** `abitype_Address` Address of the contract to set as code for the Authority. #### authorization.chainId * **Type:** `numberType` Chain ID to authorize. #### authorization.nonce * **Type:** `bigintType` Nonce of the Authority to authorize. ### options * **Type:** `hash.Options` * **Optional** #### options.presign * **Type:** `boolean` * **Optional** Whether to hash this authorization for signing. ## Return Type The hash. `Hex.Hex` # Authorization.toRpc Converts an [`Authorization.Authorization`](/api/Authorization/types#authorization) to an [`Authorization.Rpc`](/api/Authorization/types#rpc). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.toRpc({ address: '0x0000000000000000000000000000000000000000', chainId: 1, nonce: 1n, r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: 0 }) ``` ## Definition ```ts function toRpc( authorization: toRpc.Input, ): Rpc ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L501) ## Parameters ### authorization * **Type:** `toRpc.Input` An Authorization. ## Return Type An RPC-formatted Authorization. `Rpc` # Authorization.toRpcList Converts an [`Authorization.List`](/api/Authorization/types#list) to an [`Authorization.ListRpc`](/api/Authorization/types#listrpc). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.toRpcList([ { address: '0x0000000000000000000000000000000000000000', chainId: 1, nonce: 1n, r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: 0 } ]) ``` ## Definition ```ts function toRpcList( authorizationList: toRpcList.Input, ): ListRpc ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L541) ## Parameters ### authorizationList * **Type:** `toRpcList.Input` An Authorization List. ## Return Type An RPC-formatted Authorization List. [`ListRpc`](/api/Authorization/types#authorizationlistrpc) # Authorization.toTuple Converts an [`Authorization.Authorization`](/api/Authorization/types#authorization) to an [`Authorization.Tuple`](/api/Authorization/types#tuple). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const tuple = Authorization.toTuple(authorization) // [!code focus] // @log: [ // @log: address: '0x1234567890abcdef1234567890abcdef12345678', // @log: chainId: 1, // @log: nonce: 69n, // @log: ] ``` ## Definition ```ts function toTuple( authorization: authorization, ): toTuple.ReturnType ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L576) ## Parameters ### authorization * **Type:** `authorization` The [`Authorization.Authorization`](/api/Authorization/types#authorization). ## Return Type An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple. `toTuple.ReturnType` # Authorization.toTupleList Converts an [`Authorization.List`](/api/Authorization/types#list) to an [`Authorization.TupleList`](/api/Authorization/types#tuplelist). ## Imports :::code-group ```ts [Named] import { Authorization } from 'ox' ``` ```ts [Entrypoint] import * as Authorization from 'ox/Authorization' ``` ::: ## Examples ```ts twoslash import { Authorization } from 'ox' const authorization_1 = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const authorization_2 = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 3, nonce: 20n }) const tuple = Authorization.toTupleList([ authorization_1, authorization_2 ]) // [!code focus] // @log: [ // @log: [ // @log: address: '0x1234567890abcdef1234567890abcdef12345678', // @log: chainId: 1, // @log: nonce: 69n, // @log: ], // @log: [ // @log: address: '0x1234567890abcdef1234567890abcdef12345678', // @log: chainId: 3, // @log: nonce: 20n, // @log: ], // @log: ] ``` ## Definition ```ts function toTupleList( list?: list, ): toTupleList.ReturnType ``` **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L635) ## Parameters ### list * **Type:** `list` * **Optional** An [`Authorization.List`](/api/Authorization/types#list). ## Return Type An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Authorization tuple list. `toTupleList.ReturnType` # Authorization Types ## `Authorization.Authorization` Root type for an EIP-7702 Authorization. **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L11) ## `Authorization.List` List of [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L32) ## `Authorization.ListRpc` RPC representation of an [`Authorization.List`](/api/Authorization/types#list). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L39) ## `Authorization.ListSigned` Signed representation of a list of [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L42) ## `Authorization.Rpc` RPC representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L29) ## `Authorization.Signed` Signed representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L49) ## `Authorization.Tuple` Tuple representation of an [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L56) ## `Authorization.TupleList` Tuple representation of a list of [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L71) ## `Authorization.TupleListSigned` Tuple representation of a list of signed [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L75) ## `Authorization.TupleSigned` Tuple representation of a signed [`Authorization.Authorization`](/api/Authorization/types#authorization). **Source:** [src/core/Authorization.ts](https://github.com/wevm/ox/blob/main/src/core/Authorization.ts#L68) # BinaryStateTree Utility functions for working with [EIP-7864](https://eips.ethereum.org/EIPS/eip-7864) Binary State Trees. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`BinaryStateTree.create`](/api/BinaryStateTree/create) | Creates a new Binary State Tree instance. | | [`BinaryStateTree.insert`](/api/BinaryStateTree/insert) | Inserts a key-value pair into the Binary State Tree. | | [`BinaryStateTree.merkelize`](/api/BinaryStateTree/merkelize) | Merkelizes a Binary State Tree. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`BinaryStateTree.BinaryStateTree`](/api/BinaryStateTree/types#binarystatetreebinarystatetree) | Type that defines a Binary State Tree instance. | | [`BinaryStateTree.Node`](/api/BinaryStateTree/types#binarystatetreenode) | Type defining a node of the BST. | # BinaryStateTree.create Creates a new Binary State Tree instance. ## Imports :::code-group ```ts [Named] import { BinaryStateTree } from 'ox' ``` ```ts [Entrypoint] import * as BinaryStateTree from 'ox/BinaryStateTree' ``` ::: ## Examples ```ts twoslash import { BinaryStateTree } from 'ox' const tree = BinaryStateTree.create() ``` ## Definition ```ts function create(): BinaryStateTree ``` **Source:** [src/core/BinaryStateTree.ts](https://github.com/wevm/ox/blob/main/src/core/BinaryStateTree.ts#L25) ## Return Type A Binary State Tree. [`BinaryStateTree.BinaryStateTree`](/api/BinaryStateTree/types#binarystatetreebinarystatetree) # BinaryStateTree.insert Inserts a key-value pair into the Binary State Tree. ## Imports :::code-group ```ts [Named] import { BinaryStateTree } from 'ox' ``` ```ts [Entrypoint] import * as BinaryStateTree from 'ox/BinaryStateTree' ``` ::: ## Examples ```ts twoslash import { BinaryStateTree, Bytes } from 'ox' const tree = BinaryStateTree.create() BinaryStateTree.insert( // [!code focus] tree, // [!code focus] Bytes.fromHex( '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54' ), // [!code focus] Bytes.fromHex( '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ) // [!code focus] ) // [!code focus] ``` ## Definition ```ts function insert( tree: BinaryStateTree.BinaryStateTree, key: Bytes.Bytes, value: Bytes.Bytes, ): void ``` **Source:** [src/core/BinaryStateTree.ts](https://github.com/wevm/ox/blob/main/src/core/BinaryStateTree.ts#L56) ## Parameters ### tree * **Type:** [`BinaryStateTree.BinaryStateTree`](/api/BinaryStateTree/types#binarystatetreebinarystatetree) Binary State Tree instance. #### tree.root * **Type:** `Node` ### key * **Type:** `Bytes.Bytes` Key to insert. ### value * **Type:** `Bytes.Bytes` Value to insert. ## Return Type `void` # BinaryStateTree.merkelize Merkelizes a Binary State Tree. ## Imports :::code-group ```ts [Named] import { BinaryStateTree } from 'ox' ``` ```ts [Entrypoint] import * as BinaryStateTree from 'ox/BinaryStateTree' ``` ::: ## Examples ```ts twoslash import { BinaryStateTree, Bytes } from 'ox' const tree = BinaryStateTree.create() BinaryStateTree.insert( tree, Bytes.fromHex( '0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54' ), Bytes.fromHex( '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ) ) const hash = BinaryStateTree.merkelize(tree) // [!code focus] ``` ## Definition ```ts function merkelize( tree: BinaryStateTree.BinaryStateTree, ): Bytes.Bytes ``` **Source:** [src/core/BinaryStateTree.ts](https://github.com/wevm/ox/blob/main/src/core/BinaryStateTree.ts#L135) ## Parameters ### tree * **Type:** [`BinaryStateTree.BinaryStateTree`](/api/BinaryStateTree/types#binarystatetreebinarystatetree) Binary State Tree instance. #### tree.root * **Type:** `Node` ## Return Type Merkle hash. `Bytes.Bytes` # BinaryStateTree Types ## `BinaryStateTree.BinaryStateTree` Type that defines a Binary State Tree instance. **Source:** [src/core/BinaryStateTree.ts](https://github.com/wevm/ox/blob/main/src/core/BinaryStateTree.ts#L6) ## `BinaryStateTree.Node` Type defining a node of the BST. **Source:** [src/core/BinaryStateTree.ts](https://github.com/wevm/ox/blob/main/src/core/BinaryStateTree.ts#L11) # Blobs Utility functions for working with [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Blobs. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Blobs.commitmentsToVersionedHashes`](/api/Blobs/commitmentsToVersionedHashes) | Transform a list of Commitments to Blob Versioned Hashes. | | [`Blobs.commitmentToVersionedHash`](/api/Blobs/commitmentToVersionedHash) | Transform a Commitment to its Blob Versioned Hash. | | [`Blobs.from`](/api/Blobs/from) | Transforms arbitrary data to [`Blobs.Blobs`](/api/Blobs/types#blobs). | | [`Blobs.to`](/api/Blobs/to) | Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. | | [`Blobs.toBytes`](/api/Blobs/toBytes) | Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. | | [`Blobs.toCellProofs`](/api/Blobs/toCellProofs) | Compute the flat list of PeerDAS (EIP-7594) cell KZG proofs for a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). Returns `128 * blobs.length` proofs, where `proofs[i * 128 + j]` is the proof for cell `j` of `blobs[i]`'s extended form. | | [`Blobs.toCommitments`](/api/Blobs/toCommitments) | Compute commitments from a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). | | [`Blobs.toHex`](/api/Blobs/toHex) | Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. | | [`Blobs.toVersionedHashes`](/api/Blobs/toVersionedHashes) | Compute Blob Versioned Hashes from a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Blobs.BlobSizeTooLargeError`](/api/Blobs/errors#blobsblobsizetoolargeerror) | Thrown when the blob size is too large. | | [`Blobs.EmptyBlobError`](/api/Blobs/errors#blobsemptybloberror) | Thrown when the blob is empty. | | [`Blobs.EmptyBlobVersionedHashesError`](/api/Blobs/errors#blobsemptyblobversionedhasheserror) | Thrown when the blob versioned hashes are empty. | | [`Blobs.InvalidVersionedHashSizeError`](/api/Blobs/errors#blobsinvalidversionedhashsizeerror) | Thrown when the blob versioned hash size is invalid. | | [`Blobs.InvalidVersionedHashVersionError`](/api/Blobs/errors#blobsinvalidversionedhashversionerror) | Thrown when the blob versioned hash version is invalid. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Blobs.Blob`](/api/Blobs/types#blobsblob) | Root type for a Blob. | | [`Blobs.Blobs`](/api/Blobs/types#blobsblobs) | A list of [`Blobs.Blob`](/api/Blobs/types#blob). | # Blobs.commitmentsToVersionedHashes Transform a list of Commitments to Blob Versioned Hashes. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) const versionedHashes = Blobs.commitmentsToVersionedHashes(commitments) // [!code focus] // @log: ['0x...', '0x...'] ``` ### Configuring Return Type It is possible to configure the return type for the Versioned Hashes with the `as` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) const versionedHashes = Blobs.commitmentsToVersionedHashes( commitments, { as: 'Bytes' // [!code focus] } ) // @log: [Uint8Array [ ... ], Uint8Array [ ... ]] ``` ### Versioning Hashes It is possible to configure the version for the Versioned Hashes with the `version` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) const versionedHashes = Blobs.commitmentsToVersionedHashes( commitments, { version: 2 // [!code focus] } ) ``` ## Definition ```ts function commitmentsToVersionedHashes( commitments: commitments | readonly Bytes.Bytes[] | readonly Hex.Hex[], options?: commitmentsToVersionedHashes.Options, ): commitmentsToVersionedHashes.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L99) ## Parameters ### commitments * **Type:** `commitments | readonly Bytes.Bytes[] | readonly Hex.Hex[]` A list of commitments. ### options * **Type:** `commitmentsToVersionedHashes.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.version * **Type:** `number` * **Optional** Version to tag onto the hashes. ## Return Type A list of Blob Versioned Hashes. `commitmentsToVersionedHashes.ReturnType` # Blobs.commitmentToVersionedHash Transform a Commitment to its Blob Versioned Hash. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const [commitment] = Blobs.toCommitments(blobs, { kzg }) const versionedHash = Blobs.commitmentToVersionedHash(commitment) // [!code focus] ``` ### Configuring Return Type It is possible to configure the return type for the Versioned Hash with the `as` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const [commitment] = Blobs.toCommitments(blobs, { kzg }) const versionedHashes = Blobs.commitmentToVersionedHash( commitment, { as: 'Bytes' // [!code focus] } ) // @log: [Uint8Array [ ... ], Uint8Array [ ... ]] ``` ### Versioning Hashes It is possible to configure the version for the Versioned Hash with the `version` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const [commitment] = Blobs.toCommitments(blobs, { kzg }) const versionedHashes = Blobs.commitmentToVersionedHash( commitment, { version: 2 // [!code focus] } ) ``` ## Definition ```ts function commitmentToVersionedHash( commitment: commitment | Hex.Hex | Bytes.Bytes, options?: commitmentToVersionedHash.Options, ): commitmentToVersionedHash.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L200) ## Parameters ### commitment * **Type:** `commitment | Hex.Hex | Bytes.Bytes` The commitment. ### options * **Type:** `commitmentToVersionedHash.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.version * **Type:** `number` * **Optional** Version to tag onto the hash. ## Return Type The Blob Versioned Hash. `commitmentToVersionedHash.ReturnType` # Blobs.from Transforms arbitrary data to [`Blobs.Blobs`](/api/Blobs/types#blobs). ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash import { Blobs } from 'ox' const blobs = Blobs.from('0xdeadbeef') ``` ### Creating Blobs from a String An example of creating Blobs from a string using [`Hex.from`](/api/Hex/from): ```ts twoslash import { Blobs, Hex } from 'ox' const blobs = Blobs.from(Hex.fromString('Hello world!')) ``` ### Configuring Return Type It is possible to configure the return type for the Blobs with the `as` option. ```ts twoslash import { Blobs } from 'ox' const blobs = Blobs.from('0xdeadbeef', { as: 'Bytes' }) // ^? ``` ## Definition ```ts function from( data: data | Hex.Hex | Bytes.Bytes, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L271) ## Parameters ### data * **Type:** `data | Hex.Hex | Bytes.Bytes` The data to convert to [`Blobs.Blobs`](/api/Blobs/types#blobs). ### options * **Type:** `from.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. ## Return Type The [`Blobs.Blobs`](/api/Blobs/types#blobs). `from.ReturnType` # Blobs.to Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash import { Blobs, Hex } from 'ox' const blobs = Blobs.from('0xdeadbeef') const data = Blobs.to(blobs) // [!code focus] // @log: '0xdeadbeef' ``` ### Configuring Return Type It is possible to configure the return type with second argument. ```ts twoslash import { Blobs } from 'ox' const blobs = Blobs.from('0xdeadbeef') const data = Blobs.to(blobs, 'Bytes') // @log: Uint8Array [ 13, 174, 190, 239 ] ``` ## Definition ```ts function to( blobs: blobs | Blobs | Blobs, to?: to | 'Hex' | 'Bytes', ): to.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L381) ## Parameters ### blobs * **Type:** `blobs | Blobs | Blobs` The [`Blobs.Blobs`](/api/Blobs/types#blobs) to transform. ### to * **Type:** `to | 'Hex' | 'Bytes'` * **Optional** The type to transform to. ## Return Type The originating data. `to.ReturnType` # Blobs.toBytes Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts import { Blobs, Hex } from 'ox' const blobs = Blobs.from('0xdeadbeef') const data = Blobs.toBytes(blobs) // [!code focus] // @log: Uint8Array [ 13, 174, 190, 239 ] ``` ## Definition ```ts function toBytes( blobs: Blobs | Blobs, ): toBytes.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L475) ## Parameters ### blobs * **Type:** `Blobs | Blobs` ## Return Type `toBytes.ReturnType` # Blobs.toCellProofs Compute the flat list of PeerDAS (EIP-7594) cell KZG proofs for a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). Returns `128 * blobs.length` proofs, where `proofs[i * 128 + j]` is the proof for cell `j` of `blobs[i]`'s extended form. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const cellProofs = Blobs.toCellProofs(blobs, { kzg }) // [!code focus] ``` ### Configuring Return Type It is possible to configure the return type with the `as` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const cellProofs = Blobs.toCellProofs(blobs, { as: 'Bytes', // [!code focus] kzg }) // @log: [Uint8Array [ ... ], Uint8Array [ ... ], ...] ``` ## Definition ```ts function toCellProofs( blobs: blobs | Blobs | Blobs, options: toCellProofs.Options, ): toCellProofs.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L605) ## Parameters ### blobs * **Type:** `blobs | Blobs | Blobs` The [`Blobs.Blobs`](/api/Blobs/types#blobs) to transform to cell proofs. ### options * **Type:** `toCellProofs.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type The flat list of cell KZG proofs. `toCellProofs.ReturnType` # Blobs.toCommitments Compute commitments from a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { kzg }) // [!code focus] ``` ### Configuring Return Type It is possible to configure the return type with the `as` option. ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const commitments = Blobs.toCommitments(blobs, { as: 'Bytes', // [!code focus] kzg }) // @log: [Uint8Array [ ... ], Uint8Array [ ... ]] ``` ## Definition ```ts function toCommitments( blobs: blobs | Blobs | Blobs, options: toCommitments.Options, ): toCommitments.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L516) ## Parameters ### blobs * **Type:** `blobs | Blobs | Blobs` The [`Blobs.Blobs`](/api/Blobs/types#blobs) to transform to commitments. ### options * **Type:** `toCommitments.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type The commitments. `toCommitments.ReturnType` # Blobs.toHex Transforms Ox-shaped [`Blobs.Blobs`](/api/Blobs/types#blobs) into the originating data. ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash import { Blobs, Hex } from 'ox' const blobs = Blobs.from('0xdeadbeef') const data = Blobs.toHex(blobs) // [!code focus] // @log: '0xdeadbeef' ``` ## Definition ```ts function toHex( blobs: Blobs | Blobs, ): toHex.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L452) ## Parameters ### blobs * **Type:** `Blobs | Blobs` ## Return Type `toHex.ReturnType` # Blobs.toVersionedHashes Compute Blob Versioned Hashes from a list of [`Blobs.Blobs`](/api/Blobs/types#blobs). ## Imports :::code-group ```ts [Named] import { Blobs } from 'ox' ``` ```ts [Entrypoint] import * as Blobs from 'ox/Blobs' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const versionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) // [!code focus] ``` ## Definition ```ts function toVersionedHashes( blobs: blobs | Blobs | Blobs, options: toVersionedHashes.Options, ): toVersionedHashes.ReturnType ``` **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L675) ## Parameters ### blobs * **Type:** `blobs | Blobs | Blobs` The [`Blobs.Blobs`](/api/Blobs/types#blobs) to transform into Blob Versioned Hashes. ### options * **Type:** `toVersionedHashes.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type The Blob Versioned Hashes. `toVersionedHashes.ReturnType` # Blobs Errors ## `Blobs.BlobSizeTooLargeError` Thrown when the blob size is too large. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L708) ## `Blobs.EmptyBlobError` Thrown when the blob is empty. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L718) ## `Blobs.EmptyBlobVersionedHashesError` Thrown when the blob versioned hashes are empty. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L726) ## `Blobs.InvalidVersionedHashSizeError` Thrown when the blob versioned hash size is invalid. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L734) ## `Blobs.InvalidVersionedHashVersionError` Thrown when the blob versioned hash version is invalid. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L744) # Blobs Types ## `Blobs.Blob` Root type for a Blob. **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L31) ## `Blobs.Blobs` A list of [`Blobs.Blob`](/api/Blobs/types#blob). **Source:** [src/core/Blobs.ts](https://github.com/wevm/ox/blob/main/src/core/Blobs.ts#L35) # Kzg Utility functions for working with KZG Commitments. Mainly for [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Blobs. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Kzg.from`](/api/Kzg/from) | Defines a KZG interface. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Kzg.Kzg`](/api/Kzg/types#kzgkzg) | Root type for a KZG interface. | # Kzg.from Defines a KZG interface. ## Imports :::code-group ```ts [Named] import { Kzg } from 'ox' ``` ```ts [Entrypoint] import * as Kzg from 'ox/Kzg' ``` ::: ## Examples ```ts twoslash // @noErrors import * as cKzg from 'c-kzg' import { Kzg } from 'ox' import { Paths } from 'ox/trusted-setups' cKzg.loadTrustedSetup(Paths.mainnet) const kzg = Kzg.from(cKzg) ``` ## Definition ```ts function from( value: Kzg, ): Kzg ``` **Source:** [src/core/Kzg.ts](https://github.com/wevm/ox/blob/main/src/core/Kzg.ts#L66) ## Parameters ### value * **Type:** `Kzg` The KZG object to convert. #### value.cells * **Type:** `readonly Uint8Array[]` #### value.proofs * **Type:** `readonly Uint8Array[]` ## Return Type The KZG interface object. `Kzg` # Kzg Types ## `Kzg.Kzg` Root type for a KZG interface. **Source:** [src/core/Kzg.ts](https://github.com/wevm/ox/blob/main/src/core/Kzg.ts#L8) # BlobCells Cell-level helpers for [PeerDAS (EIP-7594)](https://eips.ethereum.org/EIPS/eip-7594): deriving the 128 cells and cell KZG proofs of an extended blob, and verifying cell proofs against blob commitments. ## Examples ```ts twoslash // @noErrors import { BlobCells, Blobs } from 'ox' import { kzg } from './kzg' const [blob] = Blobs.from('0xdeadbeef') const { cells, proofs } = BlobCells.fromBlob(blob, { kzg }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`BlobCells.fromBlob`](/api/BlobCells/fromBlob) | Compute the cells and KZG proofs for a single blob (PeerDAS, EIP-7594). | | [`BlobCells.recover`](/api/BlobCells/recover) | Reconstruct all 128 cells (and their KZG proofs) of an extended blob from at least 64 known cells (PeerDAS, EIP-7594). | | [`BlobCells.toDataColumns`](/api/BlobCells/toDataColumns) | Build the 128 PeerDAS data columns from a list of blobs (EIP-7594). | | [`BlobCells.verify`](/api/BlobCells/verify) | Verify a batch of cell KZG proofs against their commitments (PeerDAS, EIP-7594). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`BlobCells.InsufficientCellsError`](/api/BlobCells/errors#blobcellsinsufficientcellserror) | Thrown when fewer than [`BlobCells.cellsPerExtBlob`](/api/BlobCells)/2 cells are passed to [`BlobCells.recover`](/api/BlobCells/recover). | | [`BlobCells.MismatchedLengthsError`](/api/BlobCells/errors#blobcellsmismatchedlengthserror) | Thrown when two parallel input arrays have different lengths. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`BlobCells.Cell`](/api/BlobCells/types#blobcellscell) | Root type for a Cell. | | [`BlobCells.CellIndex`](/api/BlobCells/types#blobcellscellindex) | The 0-based index of a cell within an extended blob (0…127). | | [`BlobCells.CellProof`](/api/BlobCells/types#blobcellscellproof) | Root type for a cell KZG proof. | | [`BlobCells.ColumnIndex`](/api/BlobCells/types#blobcellscolumnindex) | The 0-based index of a column across all blobs in a block (0…127). | | [`BlobCells.DataColumn`](/api/BlobCells/types#blobcellsdatacolumn) | A PeerDAS data column for a single block: one cell + cell proof per blob, plus the blob-level commitments needed to verify them. | # BlobCells.fromBlob Compute the cells and KZG proofs for a single blob (PeerDAS, EIP-7594). Returns 128 cells (each 2048 bytes) and 128 cell-level KZG proofs (each 48 bytes). ## Imports :::code-group ```ts [Named] import { BlobCells } from 'ox' ``` ```ts [Entrypoint] import * as BlobCells from 'ox/BlobCells' ``` ::: ## Examples ```ts twoslash // @noErrors import { BlobCells, Blobs } from 'ox' import { kzg } from './kzg' const [blob] = Blobs.from('0xdeadbeef') const { cells, proofs } = BlobCells.fromBlob(blob, { kzg }) ``` ## Definition ```ts function fromBlob( blob: blob | Hex.Hex | Bytes.Bytes, options: fromBlob.Options, ): fromBlob.ReturnType ``` **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L68) ## Parameters ### blob * **Type:** `blob | Hex.Hex | Bytes.Bytes` The blob to convert. ### options * **Type:** `fromBlob.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type The cells and proofs. `fromBlob.ReturnType` # BlobCells.recover Reconstruct all 128 cells (and their KZG proofs) of an extended blob from at least 64 known cells (PeerDAS, EIP-7594). ## Imports :::code-group ```ts [Named] import { BlobCells } from 'ox' ``` ```ts [Entrypoint] import * as BlobCells from 'ox/BlobCells' ``` ::: ## Examples ```ts twoslash // @noErrors import { BlobCells } from 'ox' import { kzg } from './kzg' // Reconstruct from 64 of 128 cells. const { cells, proofs } = BlobCells.recover( knownIndices, // e.g. [0, 2, 4, …] knownCells, { kzg } ) ``` ## Definition ```ts function recover( cellIndices: readonly CellIndex[], cells: cells | readonly (Hex.Hex | Bytes.Bytes)[], options: recover.Options, ): recover.ReturnType ``` **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L206) ## Parameters ### cellIndices * **Type:** `readonly CellIndex[]` The indices of the known cells (must contain ≥ 64 distinct values). ### cells * **Type:** `cells | readonly (Hex.Hex | Bytes.Bytes)[]` The known cells, parallel to `cellIndices`. ### options * **Type:** `recover.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type The full set of 128 cells and 128 proofs. `recover.ReturnType` # BlobCells.toDataColumns Build the 128 PeerDAS data columns from a list of blobs (EIP-7594). For each column index `i ∈ [0, 128)`, produces a [`BlobCells.DataColumn`](/api/BlobCells/types#datacolumn) containing one cell + one cell proof per blob (at column `i` of each blob's extended form), alongside the blob-level commitments needed to verify them. ## Imports :::code-group ```ts [Named] import { BlobCells } from 'ox' ``` ```ts [Entrypoint] import * as BlobCells from 'ox/BlobCells' ``` ::: ## Examples ```ts twoslash // @noErrors import { BlobCells, Blobs } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const columns = BlobCells.toDataColumns(blobs, { kzg }) // 128 columns ``` ## Definition ```ts function toDataColumns( blobs: blobs | Blobs.Blobs | Blobs.Blobs, options: toDataColumns.Options, ): toDataColumns.ReturnType ``` **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L301) ## Parameters ### blobs * **Type:** `blobs | Blobs.Blobs | Blobs.Blobs` The blobs to convert. ### options * **Type:** `toDataColumns.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Return type. #### options.kzg * **Type:** `Pick` KZG implementation. ## Return Type 128 data columns. `toDataColumns.ReturnType` # BlobCells.verify Verify a batch of cell KZG proofs against their commitments (PeerDAS, EIP-7594). Each cell at index `cellIndices[i]` is checked against the commitment `commitments[i]` using the proof `proofs[i]`. ## Imports :::code-group ```ts [Named] import { BlobCells } from 'ox' ``` ```ts [Entrypoint] import * as BlobCells from 'ox/BlobCells' ``` ::: ## Examples ```ts twoslash // @noErrors import { BlobCells, Blobs } from 'ox' import { kzg } from './kzg' const [blob] = Blobs.from('0xdeadbeef') const [commitment] = Blobs.toCommitments([blob], { kzg }) const { cells, proofs } = BlobCells.fromBlob(blob, { kzg }) const valid = BlobCells.verify({ cells, cellIndices: cells.map((_, i) => i), commitments: cells.map(() => commitment), proofs, kzg }) ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L154) ## Parameters ### options * **Type:** `verify.Options` Verification options. #### options.cellIndices * **Type:** `readonly number[]` Cell indices within their respective extended blobs. #### options.cells * **Type:** `readonly (0x${string} | Uint8Array)[]` Cells to verify. #### options.commitments * **Type:** `readonly (0x${string} | Uint8Array)[]` Commitments, one per cell (parallel to `cells`). #### options.kzg * **Type:** `Pick` KZG implementation. #### options.proofs * **Type:** `readonly (0x${string} | Uint8Array)[]` Proofs, one per cell (parallel to `cells`). ## Return Type Whether all (commitment, cell, proof) tuples verify. `boolean` # BlobCells Errors ## `BlobCells.InsufficientCellsError` Thrown when fewer than [`BlobCells.cellsPerExtBlob`](/api/BlobCells)/2 cells are passed to [`BlobCells.recover`](/api/BlobCells/recover). **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L362) ## `BlobCells.MismatchedLengthsError` Thrown when two parallel input arrays have different lengths. **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L373) # BlobCells Types ## `BlobCells.Cell` Root type for a Cell. **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L18) ## `BlobCells.CellIndex` The 0-based index of a cell within an extended blob (0…127). **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L27) ## `BlobCells.CellProof` Root type for a cell KZG proof. **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L22) ## `BlobCells.ColumnIndex` The 0-based index of a column across all blobs in a block (0…127). **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L30) ## `BlobCells.DataColumn` A PeerDAS data column for a single block: one cell + cell proof per blob, plus the blob-level commitments needed to verify them. **Source:** [src/core/BlobCells.ts](https://github.com/wevm/ox/blob/main/src/core/BlobCells.ts#L36) # AesGcm Utilities & types for working with AES-GCM encryption. Internally uses the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API). ## Examples Below are some examples demonstrating common usages of the `AesGcm` module: * [Encrypting Data](#encrypting-data) * [Decrypting Data](#decrypting-data) ### Encrypting Data Data can be encrypted using [`AesGcm.encrypt`](/api/AesGcm/encrypt): ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const secret = Hex.fromString('i am a secret message') const encrypted = await AesGcm.encrypt(secret, key) // [!code focus] // @log: '0x5e257b25bcf53d5431e54e5a68ca0138306d31bb6154f35a97bb8ea18111e7d82bcf619d3c76c4650688bc5310eed80b8fc86d1e3e' ``` ### Decrypting Data Data can be decrypted using [`AesGcm.decrypt`](/api/AesGcm/decrypt): ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const encrypted = await AesGcm.encrypt( Hex.fromString('i am a secret message'), key ) const decrypted = await AesGcm.decrypt(encrypted, key) // [!code focus] // @log: Hex.fromString('i am a secret message') ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AesGcm.decrypt`](/api/AesGcm/decrypt) | Decrypts encrypted data using AES-GCM. | | [`AesGcm.encrypt`](/api/AesGcm/encrypt) | Encrypts data using AES-GCM. | | [`AesGcm.fromPrf`](/api/AesGcm/fromPrf) | Derives an AES-256-GCM key from a 32-byte WebAuthn PRF output. | | [`AesGcm.getKey`](/api/AesGcm/getKey) | Derives an AES-GCM key from a password using PBKDF2. | | [`AesGcm.randomSalt`](/api/AesGcm/randomSalt) | Generates a random salt of the specified size. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AesGcm.InvalidPrfSizeError`](/api/AesGcm/errors#aesgcminvalidprfsizeerror) | Thrown when a WebAuthn PRF output is not 32 bytes. | # AesGcm.decrypt Decrypts encrypted data using AES-GCM. ## Imports :::code-group ```ts [Named] import { AesGcm } from 'ox' ``` ```ts [Entrypoint] import * as AesGcm from 'ox/AesGcm' ``` ::: ## Examples ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const secret = Hex.fromString('i am a secret message') const encrypted = await AesGcm.encrypt(secret, key) const decrypted = await AesGcm.decrypt(encrypted, key) // [!code focus] // @log: Hex.fromString('i am a secret message') ``` ## Definition ```ts function decrypt( value: value | Bytes.Bytes | Hex.Hex, key: CryptoKey, options?: decrypt.Options, ): Promise> ``` **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L28) ## Parameters ### value * **Type:** `value | Bytes.Bytes | Hex.Hex` The data to encrypt. ### key * **Type:** `CryptoKey` The `CryptoKey` to use for encryption. ### options * **Type:** `decrypt.Options` * **Optional** Decryption options. #### options.as * **Type:** `as | "Bytes" | "Hex"` * **Optional** The output format. ## Return Type The decrypted data. `Promise>` # AesGcm.encrypt Encrypts data using AES-GCM. ## Imports :::code-group ```ts [Named] import { AesGcm } from 'ox' ``` ```ts [Entrypoint] import * as AesGcm from 'ox/AesGcm' ``` ::: ## Examples ```ts twoslash import { AesGcm, Hex } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) const secret = Hex.fromString('i am a secret message') const encrypted = await AesGcm.encrypt(secret, key) // [!code focus] // @log: '0x5e257b25bcf53d5431e54e5a68ca0138306d31bb6154f35a97bb8ea18111e7d82bcf619d3c76c4650688bc5310eed80b8fc86d1e3e' ``` ## Definition ```ts function encrypt( value: value | Bytes.Bytes | Hex.Hex, key: CryptoKey, options?: encrypt.Options, ): Promise> ``` **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L90) ## Parameters ### value * **Type:** `value | Bytes.Bytes | Hex.Hex` The data to encrypt. ### key * **Type:** `CryptoKey` The `CryptoKey` to use for encryption. ### options * **Type:** `encrypt.Options` * **Optional** Encryption options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The output format. ## Return Type The encrypted data. `Promise>` # AesGcm.fromPrf Derives an AES-256-GCM key from a 32-byte WebAuthn PRF output. The permanent derivation contract uses the PRF output as the HMAC-SHA256 key. Its message is the UTF-8 bytes of `ox.aesGcm.fromPrf.v1` followed by a 32-bit big-endian counter set to zero. ## Imports :::code-group ```ts [Named] import { AesGcm } from 'ox' ``` ```ts [Entrypoint] import * as AesGcm from 'ox/AesGcm' ``` ::: ## Examples ```ts twoslash import { AesGcm } from 'ox' const key = await AesGcm.fromPrf( '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' ) ``` ## Definition ```ts function fromPrf( value: Hex.Hex | Bytes.Bytes, ): Promise ``` **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L156) ## Parameters ### value * **Type:** `Hex.Hex | Bytes.Bytes` A 32-byte WebAuthn PRF output. ## Return Type A nonextractable AES-256-GCM key for encryption and decryption. `Promise` # AesGcm.getKey Derives an AES-GCM key from a password using PBKDF2. ## Imports :::code-group ```ts [Named] import { AesGcm } from 'ox' ``` ```ts [Entrypoint] import * as AesGcm from 'ox/AesGcm' ``` ::: ## Examples ```ts twoslash import { AesGcm } from 'ox' const key = await AesGcm.getKey({ password: 'qwerty' }) // @log: CryptoKey {} ``` ## Definition ```ts function getKey( options: getKey.Options, ): Promise ``` **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L215) ## Parameters ### options * **Type:** `getKey.Options` Options for key derivation. #### options.iterations * **Type:** `number` * **Optional** The number of iterations to use. #### options.password * **Type:** `string` Password to derive key from. #### options.salt * **Type:** `Uint8Array` * **Optional** Salt to use for key derivation. ## Return Type The derived key. `Promise` # AesGcm.randomSalt Generates a random salt of the specified size. ## Imports :::code-group ```ts [Named] import { AesGcm } from 'ox' ``` ```ts [Entrypoint] import * as AesGcm from 'ox/AesGcm' ``` ::: ## Examples ```ts twoslash import { AesGcm } from 'ox' const salt = AesGcm.randomSalt() // @log: Uint8Array [123, 79, 183, 167, 163, 136, 136, 16, 168, 126, 13, 165, 170, 166, 136, 136, 16, 168, 126, 13, 165, 170, 166, 136, 136, 16, 168, 126, 13, 165, 170, 166] ``` ## Definition ```ts function randomSalt( size?: number, ): Bytes.Bytes ``` **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L266) ## Parameters ### size * **Type:** `number` * **Optional** The size of the salt to generate. Defaults to `32`. ## Return Type A random salt of the specified size. `Bytes.Bytes` # AesGcm Errors ## `AesGcm.InvalidPrfSizeError` Thrown when a WebAuthn PRF output is not 32 bytes. **Source:** [src/core/AesGcm.ts](https://github.com/wevm/ox/blob/main/src/core/AesGcm.ts#L275) # Bls Utility functions for [BLS12-381](https://hackmd.io/@benjaminion/bls12-381) cryptography. :::info The `Bls` module is a friendly wrapper over [`@noble/curves/bls12-381`](https://github.com/paulmillr/noble-curves), an **audited** implementation of BLS12-381. ::: ## Examples Below are some examples demonstrating common usages of the `Bls` module: * [Computing a Random Private Key](#computing-a-random-private-key) * [Getting a Public Key](#getting-a-public-key) * [Signing a Payload](#signing-a-payload) * [Verifying a Signature](#verifying-a-signature) * [Aggregating Public Keys & Signatures](#aggregating-public-keys-&-signatures) * [Verify Aggregated Signatures](#verify-aggregated-signatures) ### Computing a Random Private Key A random private key can be computed using [`Bls.randomPrivateKey`](/api/Bls/randomPrivateKey): ```ts twoslash import { Bls } from 'ox' const privateKey = Bls.randomPrivateKey() // @log: '0x...' ``` ### Getting a Public Key A public key can be derived from a private key using [`Bls.getPublicKey`](/api/Bls/getPublicKey): ```ts twoslash import { Bls } from 'ox' const privateKey = Bls.randomPrivateKey() const publicKey = Bls.getPublicKey({ privateKey }) // @log: { x: 3251...5152n, y: 1251...5152n, z: 1n } ``` ### Signing a Payload A payload can be signed using [`Bls.sign`](/api/Bls/sign): ```ts twoslash import { Bls } from 'ox' const privateKey = Bls.randomPrivateKey() const signature = Bls.sign({ payload: '0xdeadbeef', privateKey }) // @log: { x: 1251...5152n, y: 1251...5152n, z: 1n } ``` ### Verifying a Signature A signature can be verified using [`Secp256k1.verify`](/api/Secp256k1/verify): ```ts twoslash import { Bls } from 'ox' const privateKey = Bls.randomPrivateKey() const publicKey = Bls.getPublicKey({ privateKey }) const signature = Bls.sign({ payload: '0xdeadbeef', privateKey }) const isValid = Bls.verify({ // [!code focus] payload: '0xdeadbeef', // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ### Aggregating Public Keys & Signatures Public keys and signatures can be aggregated using [`Bls.aggregate`](/api/Bls/aggregate): ```ts twoslash import { Bls } from 'ox' const publicKeys = [ Bls.getPublicKey({ privateKey: '0x...' }), Bls.getPublicKey({ privateKey: '0x...' }) ] const publicKey = Bls.aggregate(publicKeys) const signatures = [ Bls.sign({ payload: '0x...', privateKey: '0x...' }), Bls.sign({ payload: '0x...', privateKey: '0x...' }) ] const signature = Bls.aggregate(signatures) ``` ### Verify Aggregated Signatures We can also pass a public key and signature that was aggregated with [`Bls.aggregate`](/api/Bls/aggregate) to `Bls.verify`. ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const privateKeys = Array.from({ length: 100 }, () => Bls.randomPrivateKey() ) const publicKeys = privateKeys.map((privateKey) => Bls.getPublicKey({ privateKey }) ) const signatures = privateKeys.map((privateKey) => Bls.sign({ payload, privateKey }) ) const publicKey = Bls.aggregate(publicKeys) // [!code focus] const signature = Bls.aggregate(signatures) // [!code focus] const valid = Bls.verify({ payload, publicKey, signature }) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Bls.aggregate`](/api/Bls/aggregate) | Aggregates a set of BLS points that are either on the G1 or G2 curves (ie. public keys or signatures). | | [`Bls.createKeyPair`](/api/Bls/createKeyPair) | Creates a new BLS12-381 key pair consisting of a private key and its corresponding public key. | | [`Bls.getPublicKey`](/api/Bls/getPublicKey) | Computes the BLS12-381 public key from a provided private key. | | [`Bls.randomPrivateKey`](/api/Bls/randomPrivateKey) | Generates a random BLS12-381 private key. | | [`Bls.sign`](/api/Bls/sign) | Signs the payload with the provided private key. | | [`Bls.verify`](/api/Bls/verify) | Verifies a payload was signed by the provided public key(s). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Bls.Size`](/api/Bls/types#blssize) | | # Bls.aggregate Aggregates a set of BLS points that are either on the G1 or G2 curves (ie. public keys or signatures). ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ### Aggregating Signatures ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const signatures = [ Bls.sign({ payload, privateKey: '0x...' }), Bls.sign({ payload, privateKey: '0x...' }) ] const signature = Bls.aggregate(signatures) ``` ### Aggregating Public Keys ```ts twoslash import { Bls } from 'ox' const publicKeys = [ Bls.getPublicKey({ privateKey: '0x...' }), Bls.getPublicKey({ privateKey: '0x...' }) ] const publicKey = Bls.aggregate(publicKeys) ``` ## Definition ```ts function aggregate( points: points, ): BlsPoint.BlsPoint ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L90) ## Parameters ### points * **Type:** `points` The points to aggregate. ## Return Type The aggregated point. `points extends readonly BlsPoint.G1[] ? BlsPoint.G1 : BlsPoint.G2` # Bls.createKeyPair Creates a new BLS12-381 key pair consisting of a private key and its corresponding public key. * G1 Point (Default): - short (48 bytes) - computes longer G2 Signatures (96 bytes) - G2 Point: - long (96 bytes) - computes short G1 Signatures (48 bytes) ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ### Short G1 Public Keys (Default) ```ts twoslash import { Bls } from 'ox' const { publicKey } = Bls.createKeyPair() // ^? ``` ### Long G2 Public Keys A G2 Public Key can be derived as a G2 point (96 bytes) using `size: 'long-key:short-sig'`. This will allow you to compute G1 Signatures (48 bytes) with [`Bls.sign`](/api/Bls/sign). ```ts twoslash import { Bls } from 'ox' const { publicKey } = Bls.createKeyPair({ size: 'long-key:short-sig' }) publicKey // ^? ``` ### Serializing Public Keys can be serialized to hex or bytes using [`BlsPoint.toHex`](/api/BlsPoint/toHex) or [`BlsPoint.toBytes`](/api/BlsPoint/toBytes): ```ts twoslash import { Bls, BlsPoint } from 'ox' const { publicKey } = Bls.createKeyPair() const publicKeyHex = BlsPoint.toHex(publicKey) // ^? const publicKeyBytes = BlsPoint.toBytes(publicKey) // ^? ``` They can also be deserialized from hex or bytes using [`BlsPoint.fromHex`](/api/BlsPoint/fromHex) or [`BlsPoint.fromBytes`](/api/BlsPoint/fromBytes): ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKeyHex = '0x...' const publicKey = BlsPoint.fromHex(publicKeyHex, 'G1') // ^? ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L223) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. #### options.size * **Type:** `size | Size` * **Optional** Size of the public key to compute. * `'short-key:long-sig'`: 48 bytes; computes long signatures (96 bytes) * `'long-key:short-sig'`: 96 bytes; computes short signatures (48 bytes) ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # Bls.getPublicKey Computes the BLS12-381 public key from a provided private key. Public Keys can be derived as a point on one of the BLS12-381 groups: * G1 Point (Default): - short (48 bytes) - computes longer G2 Signatures (96 bytes) - G2 Point: - long (96 bytes) - computes short G1 Signatures (48 bytes) ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ### Short G1 Public Keys (Default) ```ts twoslash import { Bls } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...' }) // ^? ``` ### Long G2 Public Keys A G2 Public Key can be derived as a G2 point (96 bytes) using `size: 'long-key:short-sig'`. This will allow you to compute G1 Signatures (48 bytes) with [`Bls.sign`](/api/Bls/sign). ```ts twoslash import { Bls } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...', size: 'long-key:short-sig' }) publicKey // ^? ``` ### Serializing Public Keys can be serialized to hex or bytes using [`BlsPoint.toHex`](/api/BlsPoint/toHex) or [`BlsPoint.toBytes`](/api/BlsPoint/toBytes): ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...' }) const publicKeyHex = BlsPoint.toHex(publicKey) // ^? const publicKeyBytes = BlsPoint.toBytes(publicKey) // ^? ``` They can also be deserialized from hex or bytes using [`BlsPoint.fromHex`](/api/BlsPoint/fromHex) or [`BlsPoint.fromBytes`](/api/BlsPoint/fromBytes): ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKeyHex = '0x...' const publicKey = BlsPoint.fromHex(publicKeyHex, 'G1') // ^? ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L344) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to compute the public key from. #### options.size * **Type:** `Size | size` * **Optional** Size of the public key to compute. * `'short-key:long-sig'`: 48 bytes; computes long signatures (96 bytes) * `'long-key:short-sig'`: 96 bytes; computes short signatures (48 bytes) ## Return Type The computed public key. `getPublicKey.ReturnType` # Bls.randomPrivateKey Generates a random BLS12-381 private key. ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ```ts twoslash import { Bls } from 'ox' const privateKey = Bls.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L415) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # Bls.sign Signs the payload with the provided private key. ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ```ts twoslash import { Bls, Hex } from 'ox' const signature = Bls.sign({ // [!code focus] payload: Hex.random(32), // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] ``` ### Serializing Signatures can be serialized to hex or bytes using [`BlsPoint.toHex`](/api/BlsPoint/toHex) or [`BlsPoint.toBytes`](/api/BlsPoint/toBytes): ```ts twoslash import { Bls, BlsPoint, Hex } from 'ox' const signature = Bls.sign({ payload: Hex.random(32), privateKey: '0x...' }) const signatureHex = BlsPoint.toHex(signature) // ^? const signatureBytes = BlsPoint.toBytes(signature) // ^? ``` They can also be deserialized from hex or bytes using [`BlsPoint.fromHex`](/api/BlsPoint/fromHex) or [`BlsPoint.fromBytes`](/api/BlsPoint/fromBytes): ```ts twoslash import { Bls, BlsPoint } from 'ox' const signatureHex = '0x...' const signature = BlsPoint.fromHex(signatureHex, 'G2') // ^? ``` ## Definition ```ts function sign( options: sign.Options, ): sign.ReturnType ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L488) ## Parameters ### options * **Type:** `sign.Options` The signing options. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `0x${string} | Uint8Array` BLS private key. #### options.size * **Type:** `Size | size` * **Optional** Size of the signature to compute. * `'long-key:short-sig'`: 48 bytes * `'short-key:long-sig'`: 96 bytes #### options.suite * **Type:** `string` * **Optional** Ciphersuite to use for signing. Defaults to "Basic". ## Return Type BLS Point. `sign.ReturnType` # Bls.verify Verifies a payload was signed by the provided public key(s). ## Imports :::code-group ```ts [Named] import { Bls } from 'ox' ``` ```ts [Entrypoint] import * as Bls from 'ox/Bls' ``` ::: ## Examples ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const privateKey = Bls.randomPrivateKey() const publicKey = Bls.getPublicKey({ privateKey }) const signature = Bls.sign({ payload, privateKey }) const verified = Bls.verify({ // [!code focus] payload, // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] ``` ### Verify Aggregated Signatures We can also pass a public key and signature that was aggregated with [`Bls.aggregate`](/api/Bls/aggregate) to `Bls.verify`. ```ts twoslash import { Bls, Hex } from 'ox' const payload = Hex.random(32) const privateKeys = Array.from({ length: 100 }, () => Bls.randomPrivateKey() ) const publicKeys = privateKeys.map((privateKey) => Bls.getPublicKey({ privateKey }) ) const signatures = privateKeys.map((privateKey) => Bls.sign({ payload, privateKey }) ) const publicKey = Bls.aggregate(publicKeys) // [!code focus] const signature = Bls.aggregate(signatures) // [!code focus] const valid = Bls.verify({ payload, publicKey, signature }) // [!code focus] ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L622) ## Parameters ### options * **Type:** `verify.Options` Verification options. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { x: { c0: 0x${string}; c1: 0x${string}; }; y: { c0: 0x${string}; c1: 0x${string}; }; z: { c0: 0x${string}; c1: 0x${string}; }; }` Public key (G2). Accepts a structured [`BlsPoint.G2`](/api/BlsPoint/types#g2), a hex string, or a `Uint8Array`. #### options.signature * **Type:** `0x${string} | Uint8Array | { x: 0x${string}; y: 0x${string}; z: 0x${string}; }` Signature (G1). Accepts a structured [`BlsPoint.G1`](/api/BlsPoint/types#g1), a hex string, or a `Uint8Array`. #### options.suite * **Type:** `string` * **Optional** Ciphersuite to use for verification. Defaults to "Basic". ## Return Type Whether the payload was signed by the provided public key. `boolean` # Bls Types ## `Bls.Size` **Source:** [src/core/Bls.ts](https://github.com/wevm/ox/blob/main/src/core/Bls.ts#L51) # BlsPoint Utility functions for working with BLS12-381 points. :::info The `BlsPoint` module is a friendly wrapper over [`@noble/curves/bls12-381`](https://github.com/paulmillr/noble-curves), an **audited** implementation of BLS12-381. ::: ## Examples Below are some examples demonstrating common usages of the `BlsPoint` module: * [Public Keys or Signatures to Hex](#public-keys-or-signatures-to-hex) * [Hex to Public Keys or Signatures](#hex-to-public-keys-or-signatures) ### Public Keys or Signatures to Hex BLS points can be converted to hex using [`BlsPoint.toHex`](/api/BlsPoint/toHex): ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...' }) const publicKeyHex = BlsPoint.toHex(publicKey) // @log: '0xacafff52270773ad1728df2807c0f1b0b271fa6b37dfb8b2f75448573c76c81bcd6790328a60e40ef5a13343b32d9e66' const signature = Bls.sign({ payload: '0xdeadbeef', privateKey: '0x...' }) const signatureHex = BlsPoint.toHex(signature) // @log: '0xb4698f7611999fba87033b9cf72312c76c683bbc48175e2d4cb275907d6a267ab9840a66e3051e5ed36fd13aa712f9a9024f9fa9b67f716dfb74ae4efb7d9f1b7b43b4679abed6644cf476c12e79f309351ea8452487cd93f66e29e04ebe427c' ``` ### Hex to Public Keys or Signatures BLS points can be converted from hex using [`BlsPoint.fromHex`](/api/BlsPoint/fromHex): ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKey = BlsPoint.fromHex( '0xacafff52270773ad1728df2807c0f1b0b271fa6b37dfb8b2f75448573c76c81bcd6790328a60e40ef5a13343b32d9e66', 'G1' ) // @log: { x: 172...514n, y: 175...235n, z: 1n } const signature = BlsPoint.fromHex( '0xb4698f7611999fba87033b9cf72312c76c683bbc48175e2d4cb275907d6a267ab9840a66e3051e5ed36fd13aa712f9a9024f9fa9b67f716dfb74ae4efb7d9f1b7b43b4679abed6644cf476c12e79f309351ea8452487cd93f66e29e04ebe427c', 'G2' ) // @log: { x: 1251...5152n, y: 1251...5152n, z: 1n } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`BlsPoint.fromBytes`](/api/BlsPoint/fromBytes) | Converts [`Bytes.Bytes`](/api/Bytes/types#bytes) to a BLS point. | | [`BlsPoint.fromHex`](/api/BlsPoint/fromHex) | Converts [`Hex.Hex`](/api/Hex/types#hex) to a BLS point. | | [`BlsPoint.toBytes`](/api/BlsPoint/toBytes) | Converts a BLS point to [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`BlsPoint.toHex`](/api/BlsPoint/toHex) | Converts a BLS point to [`Hex.Hex`](/api/Hex/types#hex). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`BlsPoint.BlsPoint`](/api/BlsPoint/types#blspointblspoint) | Root type for a BLS point on the G1 or G2 curve. | | [`BlsPoint.Fp`](/api/BlsPoint/types#blspointfp) | Type for a field element in the base field of the BLS12-381 curve. | | [`BlsPoint.Fp2`](/api/BlsPoint/types#blspointfp2) | Type for a field element in the extension field of the BLS12-381 curve. | | [`BlsPoint.G1`](/api/BlsPoint/types#blspointg1) | Type for a BLS point on the G1 curve. | | [`BlsPoint.G1Bytes`](/api/BlsPoint/types#blspointg1bytes) | Branded type for a bytes representation of a G1 point. | | [`BlsPoint.G1Hex`](/api/BlsPoint/types#blspointg1hex) | Branded type for a hex representation of a G1 point. | | [`BlsPoint.G2`](/api/BlsPoint/types#blspointg2) | Type for a BLS point on the G2 curve. | | [`BlsPoint.G2Bytes`](/api/BlsPoint/types#blspointg2bytes) | Branded type for a bytes representation of a G2 point. | | [`BlsPoint.G2Hex`](/api/BlsPoint/types#blspointg2hex) | Branded type for a hex representation of a G2 point. | # BlsPoint.fromBytes Converts [`Bytes.Bytes`](/api/Bytes/types#bytes) to a BLS point. ## Imports :::code-group ```ts [Named] import { BlsPoint } from 'ox' ``` ```ts [Entrypoint] import * as BlsPoint from 'ox/BlsPoint' ``` ::: ## Examples ### Bytes to Public Key ```ts twoslash // @noErrors import { BlsPoint } from 'ox' const publicKey = BlsPoint.fromBytes(Bytes.from([172, 175, 255, ...]), 'G1') // @log: { // @log: x: '0x00...ac', // @log: y: '0x00...af', // @log: z: '0x00...01', // @log: } ``` ### Bytes to Signature ```ts twoslash // @noErrors import { BlsPoint } from 'ox' const signature = BlsPoint.fromBytes(Bytes.from([172, 175, 255, ...]), 'G2') // @log: { // @log: x: { c0: '0x00...11', c1: '0x00...22' }, // @log: y: { c0: '0x00...33', c1: '0x00...44' }, // @log: z: { c0: '0x00...01', c1: '0x00...00' }, // @log: } ``` ## Definition ```ts function fromBytes( bytes: Bytes.Bytes, group: group, ): group extends 'G1' ? G1 : G2 ``` **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L234) ## Parameters ### bytes * **Type:** `Bytes.Bytes` The bytes to convert. ### group * **Type:** `group` ## Return Type The BLS point. `group extends 'G1' ? G1 : G2` # BlsPoint.fromHex Converts [`Hex.Hex`](/api/Hex/types#hex) to a BLS point. ## Imports :::code-group ```ts [Named] import { BlsPoint } from 'ox' ``` ```ts [Entrypoint] import * as BlsPoint from 'ox/BlsPoint' ``` ::: ## Examples ### Hex to Public Key ```ts twoslash // @noErrors import { BlsPoint } from 'ox' const publicKey = BlsPoint.fromHex( '0xacafff52270773ad1728df2807c0f1b0b271fa6b37dfb8b2f75448573c76c81bcd6790328a60e40ef5a13343b32d9e66', 'G1' ) // @log: { // @log: x: '0x00...ac', // @log: y: '0x00...af', // @log: z: '0x00...01', // @log: } ``` ### Hex to Signature ```ts twoslash // @noErrors import { BlsPoint } from 'ox' const signature = BlsPoint.fromHex( '0xb4698f7611999fba87033b9cf72312c76c683bbc48175e2d4cb275907d6a267ab9840a66e3051e5ed36fd13aa712f9a9024f9fa9b67f716dfb74ae4efb7d9f1b7b43b4679abed6644cf476c12e79f309351ea8452487cd93f66e29e04ebe427c', 'G2' ) // @log: { // @log: x: { c0: '0x00...11', c1: '0x00...22' }, // @log: y: { c0: '0x00...33', c1: '0x00...44' }, // @log: z: { c0: '0x00...01', c1: '0x00...00' }, // @log: } ``` ## Definition ```ts function fromHex( hex: Hex.Hex, group: group, ): group extends 'G1' ? G1 : G2 ``` **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L299) ## Parameters ### hex * **Type:** `Hex.Hex` ### group * **Type:** `group` ## Return Type The BLS point. `group extends 'G1' ? G1 : G2` # BlsPoint.toBytes Converts a BLS point to [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { BlsPoint } from 'ox' ``` ```ts [Entrypoint] import * as BlsPoint from 'ox/BlsPoint' ``` ::: ## Examples ### Public Key to Bytes ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...' }) const publicKeyBytes = BlsPoint.toBytes(publicKey) // @log: Uint8Array [172, 175, 255, ...] ``` ### Signature to Bytes ```ts twoslash import { Bls, BlsPoint } from 'ox' const signature = Bls.sign({ payload: '0x...', privateKey: '0x...' }) const signatureBytes = BlsPoint.toBytes(signature) // @log: Uint8Array [172, 175, 255, ...] ``` ## Definition ```ts function toBytes( point: point, ): point extends G1 ? G1Bytes : G2Bytes ``` **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L143) ## Parameters ### point * **Type:** `point` The BLS point to convert. ## Return Type The bytes representation of the BLS point. `point extends G1 ? G1Bytes : G2Bytes` # BlsPoint.toHex Converts a BLS point to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { BlsPoint } from 'ox' ``` ```ts [Entrypoint] import * as BlsPoint from 'ox/BlsPoint' ``` ::: ## Examples ### Public Key to Hex ```ts twoslash import { Bls, BlsPoint } from 'ox' const publicKey = Bls.getPublicKey({ privateKey: '0x...' }) const publicKeyHex = BlsPoint.toHex(publicKey) // @log: '0xacafff52270773ad1728df2807c0f1b0b271fa6b37dfb8b2f75448573c76c81bcd6790328a60e40ef5a13343b32d9e66' ``` ### Signature to Hex ```ts twoslash import { Bls, BlsPoint } from 'ox' const signature = Bls.sign({ payload: '0x...', privateKey: '0x...' }) const signatureHex = BlsPoint.toHex(signature) // @log: '0xb4698f7611999fba87033b9cf72312c76c683bbc48175e2d4cb275907d6a267ab9840a66e3051e5ed36fd13aa712f9a9024f9fa9b67f716dfb74ae4efb7d9f1b7b43b4679abed6644cf476c12e79f309351ea8452487cd93f66e29e04ebe427c' ``` ## Definition ```ts function toHex( point: point, ): point extends G1 ? G1Hex : G2Hex ``` **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L186) ## Parameters ### point * **Type:** `point` The BLS point to convert. ## Return Type The hex representation of the BLS point. `point extends G1 ? G1Hex : G2Hex` # BlsPoint Types ## `BlsPoint.BlsPoint` Root type for a BLS point on the G1 or G2 curve. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L14) ## `BlsPoint.Fp` Type for a field element in the base field of the BLS12-381 curve. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L9) ## `BlsPoint.Fp2` Type for a field element in the extension field of the BLS12-381 curve. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L11) ## `BlsPoint.G1` Type for a BLS point on the G1 curve. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L21) ## `BlsPoint.G1Bytes` Branded type for a bytes representation of a G1 point. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L23) ## `BlsPoint.G1Hex` Branded type for a hex representation of a G1 point. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L25) ## `BlsPoint.G2` Type for a BLS point on the G2 curve. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L28) ## `BlsPoint.G2Bytes` Branded type for a bytes representation of a G2 point. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L30) ## `BlsPoint.G2Hex` Branded type for a hex representation of a G2 point. **Source:** [src/core/BlsPoint.ts](https://github.com/wevm/ox/blob/main/src/core/BlsPoint.ts#L32) # CoseKey Utility functions for converting between COSE\_Key and P256 public keys. COSE\_Key is the key format used in WebAuthn attestation objects, as defined in [RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053). ## Examples Below are some examples demonstrating common usages of the `CoseKey` module: * [Encoding a Public Key to COSE\_Key](#encoding-a-public-key-to-cose_key) * [Decoding a COSE\_Key to Public Key](#decoding-a-cose_key-to-public-key) ### Encoding a Public Key to COSE\_Key ```ts twoslash import { CoseKey, P256 } from 'ox' const { publicKey } = P256.createKeyPair() const coseKey = CoseKey.fromPublicKey(publicKey) ``` ### Decoding a COSE\_Key to Public Key ```ts twoslash import { CoseKey, P256 } from 'ox' const { publicKey } = P256.createKeyPair() const coseKey = CoseKey.fromPublicKey(publicKey) const publicKey2 = CoseKey.toPublicKey(coseKey) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`CoseKey.fromPublicKey`](/api/CoseKey/fromPublicKey) | Converts a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to a CBOR-encoded COSE\_Key. | | [`CoseKey.toPublicKey`](/api/CoseKey/toPublicKey) | Converts a CBOR-encoded COSE\_Key to a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`CoseKey.InvalidCoseKeyError`](/api/CoseKey/errors#cosekeyinvalidcosekeyerror) | Thrown when a COSE\_Key does not contain valid P256 public key coordinates. | # CoseKey.fromPublicKey Converts a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to a CBOR-encoded COSE\_Key. The COSE\_Key uses integer map keys per [RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053): - `1` (kty): `2` (EC2) - `3` (alg): `-7` (ES256) - `-1` (crv): `1` (P-256) - `-2` (x): x coordinate bytes - `-3` (y): y coordinate bytes ## Imports :::code-group ```ts [Named] import { CoseKey } from 'ox' ``` ```ts [Entrypoint] import * as CoseKey from 'ox/CoseKey' ``` ::: ## Examples ```ts twoslash import { CoseKey, P256 } from 'ox' const { publicKey } = P256.createKeyPair() const coseKey = CoseKey.fromPublicKey(publicKey) ``` ## Definition ```ts function fromPublicKey( publicKey: PublicKey.PublicKey, ): Hex.Hex ``` **Source:** [src/core/CoseKey.ts](https://github.com/wevm/ox/blob/main/src/core/CoseKey.ts#L30) ## Parameters ### publicKey * **Type:** `PublicKey.PublicKey` The P256 public key to convert. #### publicKey.prefix * **Type:** `numberType` #### publicKey.x * **Type:** `0x${string}` #### publicKey.y * **Type:** `0x${string}` ## Return Type The CBOR-encoded COSE\_Key as a Hex string. `Hex.Hex` # CoseKey.toPublicKey Converts a CBOR-encoded COSE\_Key to a P256 [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). Accepts the COSE key as either a hex string or raw bytes. When the `returnByteLength` or `returnDecoded` option is set, the function returns an object containing the public key plus the consumed byte length and/or the decoded CBOR map. This is useful for parsing CBOR streams (such as WebAuthn `authenticatorData`) where the COSE key is followed by trailing data. ## Imports :::code-group ```ts [Named] import { CoseKey } from 'ox' ``` ```ts [Entrypoint] import * as CoseKey from 'ox/CoseKey' ``` ::: ## Examples ```ts twoslash import { CoseKey, P256 } from 'ox' const { publicKey } = P256.createKeyPair() const coseKey = CoseKey.fromPublicKey(publicKey) const publicKey2 = CoseKey.toPublicKey(coseKey) ``` ### With Byte Length ```ts twoslash import { CoseKey, P256 } from 'ox' const { publicKey } = P256.createKeyPair() const coseKey = CoseKey.fromPublicKey(publicKey) const { publicKey: pk, byteLength } = CoseKey.toPublicKey( coseKey, { returnByteLength: true } ) ``` ## Definition ```ts function toPublicKey( coseKey: Hex.Hex | Uint8Array, options?: options | toPublicKey.Options, ): toPublicKey.ReturnType ``` **Source:** [src/core/CoseKey.ts](https://github.com/wevm/ox/blob/main/src/core/CoseKey.ts#L92) ## Parameters ### coseKey * **Type:** `Hex.Hex | Uint8Array` The CBOR-encoded COSE\_Key as hex or bytes. ### options * **Type:** `options | toPublicKey.Options` * **Optional** Decoding options. ## Return Type The P256 public key, optionally with byte length and decoded CBOR. `toPublicKey.ReturnType` # CoseKey Errors ## `CoseKey.InvalidCoseKeyError` Thrown when a COSE\_Key does not contain valid P256 public key coordinates. **Source:** [src/core/CoseKey.ts](https://github.com/wevm/ox/blob/main/src/core/CoseKey.ts#L215) # Ed25519 Utilities for working with Ed25519 signatures and key pairs. Ed25519 is a modern elliptic curve signature scheme that provides strong security guarantees and high performance. It is widely used in various cryptographic applications. ## Examples Below are some examples demonstrating common usages of the `Ed25519` module: * [Creating Key Pairs](#creating-key-pairs) * [Signing & Verifying](#signing-&-verifying) ### Creating Key Pairs ```ts twoslash import { Ed25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() ``` ### Signing & Verifying ```ts twoslash import { Ed25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() const payload = '0xdeadbeef' const signature = Ed25519.sign({ payload, privateKey }) const isValid = Ed25519.verify({ payload, publicKey, signature }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Ed25519.createKeyPair`](/api/Ed25519/createKeyPair) | Creates a new Ed25519 key pair consisting of a private key and its corresponding public key. | | [`Ed25519.fromPrf`](/api/Ed25519/fromPrf) | Derives an Ed25519 private key from a 32-byte WebAuthn PRF output. | | [`Ed25519.getPublicKey`](/api/Ed25519/getPublicKey) | Computes the Ed25519 public key from a provided private key. | | [`Ed25519.randomPrivateKey`](/api/Ed25519/randomPrivateKey) | Generates a random Ed25519 private key. | | [`Ed25519.sign`](/api/Ed25519/sign) | Signs the payload with the provided private key and returns an Ed25519 signature. | | [`Ed25519.toX25519PrivateKey`](/api/Ed25519/toX25519PrivateKey) | Converts an Ed25519 private key to an X25519 private key. | | [`Ed25519.toX25519PublicKey`](/api/Ed25519/toX25519PublicKey) | Converts an Ed25519 public key to an X25519 public key. | | [`Ed25519.verify`](/api/Ed25519/verify) | Verifies a payload was signed by the provided public key. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Ed25519.InvalidPrfSizeError`](/api/Ed25519/errors#ed25519invalidprfsizeerror) | Thrown when a WebAuthn PRF output is not 32 bytes. | # Ed25519.createKeyPair Creates a new Ed25519 key pair consisting of a private key and its corresponding public key. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L24) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private and public keys. ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # Ed25519.fromPrf Derives an Ed25519 private key from a 32-byte WebAuthn PRF output. The permanent derivation contract uses the PRF output as the HMAC-SHA256 key. Its message is the UTF-8 bytes of `ox.ed25519.fromPrf.v1` followed by a 32-bit big-endian counter set to zero. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const privateKey = Ed25519.fromPrf( '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' ) ``` ## Definition ```ts function fromPrf( value: Hex.Hex | Bytes.Bytes, options?: fromPrf.Options, ): fromPrf.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L82) ## Parameters ### value * **Type:** `Hex.Hex | Bytes.Bytes` A 32-byte WebAuthn PRF output. ### options * **Type:** `fromPrf.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type An Ed25519 private key. `fromPrf.ReturnType` # Ed25519.getPublicKey Computes the Ed25519 public key from a provided private key. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const publicKey = Ed25519.getPublicKey({ privateKey: '0x...' }) ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L141) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to compute the public key from. ## Return Type The computed public key. `getPublicKey.ReturnType` # Ed25519.randomPrivateKey Generates a random Ed25519 private key. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const privateKey = Ed25519.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L187) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # Ed25519.sign Signs the payload with the provided private key and returns an Ed25519 signature. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const signature = Ed25519.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function sign( options: sign.Options, ): sign.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L229) ## Parameters ### options * **Type:** `sign.Options` The signing options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Ed25519 private key. ## Return Type The Ed25519 signature. `sign.ReturnType` # Ed25519.toX25519PrivateKey Converts an Ed25519 private key to an X25519 private key. This is useful for performing X25519 Diffie-Hellman key exchange using an Ed25519 signing key pair. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519, X25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() const x25519PrivateKey = Ed25519.toX25519PrivateKey({ privateKey }) ``` ## Definition ```ts function toX25519PrivateKey( options: toX25519PrivateKey.Options, ): toX25519PrivateKey.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L383) ## Parameters ### options * **Type:** `toX25519PrivateKey.Options` The options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Ed25519 private key to convert. ## Return Type The X25519 private key. `toX25519PrivateKey.ReturnType` # Ed25519.toX25519PublicKey Converts an Ed25519 public key to an X25519 public key. This is useful for performing X25519 Diffie-Hellman key exchange using an Ed25519 signing key pair. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519, X25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() const x25519PublicKey = Ed25519.toX25519PublicKey({ publicKey }) ``` ## Definition ```ts function toX25519PublicKey( options: toX25519PublicKey.Options, ): toX25519PublicKey.ReturnType ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L332) ## Parameters ### options * **Type:** `toX25519PublicKey.Options` The options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.publicKey * **Type:** `0x${string} | Uint8Array` Ed25519 public key to convert. ## Return Type The X25519 public key. `toX25519PublicKey.ReturnType` # Ed25519.verify Verifies a payload was signed by the provided public key. ## Imports :::code-group ```ts [Named] import { Ed25519 } from 'ox' ``` ```ts [Entrypoint] import * as Ed25519 from 'ox/Ed25519' ``` ::: ## Examples ```ts twoslash import { Ed25519 } from 'ox' const { privateKey, publicKey } = Ed25519.createKeyPair() const signature = Ed25519.sign({ payload: '0xdeadbeef', privateKey }) const verified = Ed25519.verify({ // [!code focus] publicKey, // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L291) ## Parameters ### options * **Type:** `verify.Options` The verification options. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array` Public key that signed the payload. #### options.signature * **Type:** `0x${string} | Uint8Array` Signature of the payload. ## Return Type Whether the payload was signed by the provided public key. `boolean` # Ed25519 Errors ## `Ed25519.InvalidPrfSizeError` Thrown when a WebAuthn PRF output is not 32 bytes. **Source:** [src/core/Ed25519.ts](https://github.com/wevm/ox/blob/main/src/core/Ed25519.ts#L415) # Engine Functions for delegating ox's cryptography to a different implementation. Ox uses [`@noble`](https://github.com/paulmillr/noble-hashes) and [`@scure`](https://github.com/paulmillr/scure-bip32) by default. An engine replaces those implementations with your own — for example a WASM build of an audited C library. ## Examples Below are some examples demonstrating common usages of the `Engine` module: * [Installing an Engine](#installing-an-engine) * [Overriding a Single Primitive](#overriding-a-single-primitive) ### Installing an Engine Engine slots are named after ox modules, and everything is optional: any slot or function you leave out keeps using ox's default implementation. ```ts twoslash // @noErrors import { Engine } from 'ox' import { Hash } from 'ox/wasm' await Engine.install({ Hash: Hash.engine() }) ``` ### Overriding a Single Primitive ```ts twoslash import { Engine } from 'ox' Engine.set({ Hash: { keccak256: () => new Uint8Array(32) } }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Engine.get`](/api/Engine/get) | Returns the installed engine. | | [`Engine.install`](/api/Engine/install) | Resolves and installs crypto implementations. | | [`Engine.reset`](/api/Engine/reset) | Restores ox's default implementations. | | [`Engine.set`](/api/Engine/set) | Installs crypto implementations, replacing the `@noble/*` implementations ox uses by default. | | [`Engine.with`](/api/Engine/with) | Runs a function with an engine installed, then restores the previous engine. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Engine.AsyncScopeError`](/api/Engine/errors#engineasyncscopeerror) | Thrown when [`Engine.with`](/api/Engine/with) is given an asynchronous function. | | [`Engine.InvalidSlotValueError`](/api/Engine/errors#engineinvalidslotvalueerror) | Thrown when an engine slot is not an object or `undefined`. | | [`Engine.UnknownPrimitiveError`](/api/Engine/errors#engineunknownprimitiveerror) | Thrown when a slot is given an unrecognized primitive name. | | [`Engine.UnknownSlotError`](/api/Engine/errors#engineunknownsloterror) | Thrown when an unrecognized engine slot is supplied. | # Engine.get Returns the installed engine. Only overrides are returned -- slots left on ox's defaults are absent, so an empty object means ox is entirely on its default implementations. ## Imports :::code-group ```ts [Named] import { Engine } from 'ox' ``` ```ts [Entrypoint] import * as Engine from 'ox/Engine' ``` ::: ## Examples ```ts twoslash import { Engine } from 'ox' Engine.get() // @log: {} ``` ## Definition ```ts function get(): engine.Engine ``` **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L155) ## Return Type The installed engine. `engine.Engine` # Engine.install Resolves and installs crypto implementations. Slots resolve in parallel, then use [`Engine.set`](/api/Engine/set) merge semantics: omitted slots and primitives preserve existing overrides. If a slot rejects or validation fails, the installed engine is unchanged. ## Imports :::code-group ```ts [Named] import { Engine } from 'ox' ``` ```ts [Entrypoint] import * as Engine from 'ox/Engine' ``` ::: ## Examples ```ts twoslash import { Engine } from 'ox' import { Hash } from 'ox/wasm' await Engine.install({ Hash: Hash.engine() }) ``` ## Definition ```ts function install( value: value & install.Exact, ): Promise> ``` **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L38) ## Parameters ### value * **Type:** `value & install.Exact` Engine slots or promises for engine slots. ## Return Type The resolved engine that was installed. `Promise>` # Engine.reset Restores ox's default implementations. ## Imports :::code-group ```ts [Named] import { Engine } from 'ox' ``` ```ts [Entrypoint] import * as Engine from 'ox/Engine' ``` ::: ## Examples ```ts twoslash import { Engine } from 'ox' Engine.reset('Hash') Engine.reset() ``` ## Definition ```ts function reset( slot?: keyof engine.Engine, ): void ``` **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L183) ## Parameters ### slot * **Type:** `keyof engine.Engine` * **Optional** Slot to reset. Resets every slot when omitted. #### slot.Bls * **Type:** `Bls` * **Optional** Implementation for [`Bls`](/api/Bls). #### slot.Ed25519 * **Type:** `Eddsa` * **Optional** Implementation for [`Ed25519`](/api/Ed25519). #### slot.Hash * **Type:** `Hash` * **Optional** Implementation for [`Hash`](/api/Hash). #### slot.Keystore * **Type:** `Keystore` * **Optional** Implementation for [`Keystore`](/api/Keystore). #### slot.MlDsa44 * **Type:** `MlDsa` * **Optional** Implementation for [`MlDsa44`](/api/MlDsa44). #### slot.Mnemonic * **Type:** `Mnemonic` * **Optional** Implementation for [`Mnemonic`](/api/Mnemonic). #### slot.P256 * **Type:** `Ecdsa` * **Optional** Implementation for [`P256`](/api/P256). #### slot.Secp256k1 * **Type:** `Ecdsa` * **Optional** Implementation for [`Secp256k1`](/api/Secp256k1). #### slot.X25519 * **Type:** `Ecdh` * **Optional** Implementation for [`X25519`](/api/X25519). ## Return Type `void` # Engine.set Installs crypto implementations, replacing the `@noble/*` implementations ox uses by default. Slots and their functions are optional. Omissions preserve earlier overrides, or use Ox's default when none exists. Calls merge, so a later engine can override one primitive. Call this once, during application startup, before any crypto call. ox resolves the engine at call time, so values computed beforehand used whatever implementation was installed then. ## Imports :::code-group ```ts [Named] import { Engine } from 'ox' ``` ```ts [Entrypoint] import * as Engine from 'ox/Engine' ``` ::: ## Examples ```ts twoslash import { Engine, Hash } from 'ox' Engine.set({ Hash: { keccak256: () => new Uint8Array(32) } }) Hash.keccak256('0xdeadbeef') // @log: '0x0000000000000000000000000000000000000000000000000000000000000000' ``` ## Definition ```ts function set( value: engine.Engine, ): void ``` **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L110) ## Parameters ### value * **Type:** `engine.Engine` Engine to install. #### value.Bls * **Type:** `Bls` * **Optional** Implementation for [`Bls`](/api/Bls). #### value.Ed25519 * **Type:** `Eddsa` * **Optional** Implementation for [`Ed25519`](/api/Ed25519). #### value.Hash * **Type:** `Hash` * **Optional** Implementation for [`Hash`](/api/Hash). #### value.Keystore * **Type:** `Keystore` * **Optional** Implementation for [`Keystore`](/api/Keystore). #### value.MlDsa44 * **Type:** `MlDsa` * **Optional** Implementation for [`MlDsa44`](/api/MlDsa44). #### value.Mnemonic * **Type:** `Mnemonic` * **Optional** Implementation for [`Mnemonic`](/api/Mnemonic). #### value.P256 * **Type:** `Ecdsa` * **Optional** Implementation for [`P256`](/api/P256). #### value.Secp256k1 * **Type:** `Ecdsa` * **Optional** Implementation for [`Secp256k1`](/api/Secp256k1). #### value.X25519 * **Type:** `Ecdh` * **Optional** Implementation for [`X25519`](/api/X25519). ## Return Type `void` # Engine.with Runs a function with an engine installed, then restores the previous engine. Only safe for synchronous functions: the engine is process-wide for the duration of the call, so concurrent asynchronous work would observe it too. Passing a function that returns a promise throws [`Engine.AsyncScopeError`](/api/Engine/errors#asyncscopeerror). ## Imports :::code-group ```ts [Named] import { Engine } from 'ox' ``` ```ts [Entrypoint] import * as Engine from 'ox/Engine' ``` ::: ## Examples ```ts twoslash import { Engine, Hash } from 'ox' const hash = Engine.with( { Hash: { keccak256: () => new Uint8Array(32) } }, () => Hash.keccak256('0xdeadbeef') ) // @log: '0x0000000000000000000000000000000000000000000000000000000000000000' ``` ## Definition ```ts function with( value: engine.Engine, fn: () => returnType, ): returnType ``` **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L223) ## Parameters ### value * **Type:** `engine.Engine` Engine to install for the duration of the call. #### value.Bls * **Type:** `Bls` * **Optional** Implementation for [`Bls`](/api/Bls). #### value.Ed25519 * **Type:** `Eddsa` * **Optional** Implementation for [`Ed25519`](/api/Ed25519). #### value.Hash * **Type:** `Hash` * **Optional** Implementation for [`Hash`](/api/Hash). #### value.Keystore * **Type:** `Keystore` * **Optional** Implementation for [`Keystore`](/api/Keystore). #### value.MlDsa44 * **Type:** `MlDsa` * **Optional** Implementation for [`MlDsa44`](/api/MlDsa44). #### value.Mnemonic * **Type:** `Mnemonic` * **Optional** Implementation for [`Mnemonic`](/api/Mnemonic). #### value.P256 * **Type:** `Ecdsa` * **Optional** Implementation for [`P256`](/api/P256). #### value.Secp256k1 * **Type:** `Ecdsa` * **Optional** Implementation for [`Secp256k1`](/api/Secp256k1). #### value.X25519 * **Type:** `Ecdh` * **Optional** Implementation for [`X25519`](/api/X25519). ### fn * **Type:** `() => returnType` Synchronous function to run. ## Return Type The return value of `fn`. `returnType` # Engine Errors ## `Engine.AsyncScopeError` Thrown when [`Engine.with`](/api/Engine/with) is given an asynchronous function. **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L299) ## `Engine.InvalidSlotValueError` Thrown when an engine slot is not an object or `undefined`. **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L263) ## `Engine.UnknownPrimitiveError` Thrown when a slot is given an unrecognized primitive name. **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L283) ## `Engine.UnknownSlotError` Thrown when an unrecognized engine slot is supplied. **Source:** [src/core/Engine.ts](https://github.com/wevm/ox/blob/main/src/core/Engine.ts#L272) # Hash Utility functions for hashing (keccak256, sha256, etc). ## Examples ```ts twoslash import { Hash } from 'ox' const value = Hash.keccak256('0xdeadbeef') // '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Hash.blake3`](/api/Hash/blake3) | Calculates the [BLAKE3](https://github.com/BLAKE3-team/BLAKE3) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hash.createBlake3`](/api/Hash/createBlake3) | Creates an incremental BLAKE3 hasher. | | [`Hash.createHmac256`](/api/Hash/createHmac256) | Creates an incremental HMAC-SHA256 hasher. | | [`Hash.createKeccak256`](/api/Hash/createKeccak256) | Creates an incremental Keccak256 hasher. | | [`Hash.createRipemd160`](/api/Hash/createRipemd160) | Creates an incremental RIPEMD-160 hasher. | | [`Hash.createSha256`](/api/Hash/createSha256) | Creates an incremental SHA-256 hasher. | | [`Hash.hmac256`](/api/Hash/hmac256) | Calculates the [HMAC-SHA256](https://en.wikipedia.org/wiki/HMAC) of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hash.keccak256`](/api/Hash/keccak256) | Calculates the [Keccak256](https://en.wikipedia.org/wiki/SHA-3) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hash.ripemd160`](/api/Hash/ripemd160) | Calculates the [Ripemd160](https://en.wikipedia.org/wiki/RIPEMD) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hash.sha256`](/api/Hash/sha256) | Calculates the [Sha256](https://en.wikipedia.org/wiki/SHA-256) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hash.validate`](/api/Hash/validate) | Checks if a string is a valid hash value. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Hash.HasherDestroyedError`](/api/Hash/errors#hashhasherdestroyederror) | Thrown when an incremental hash state has been consumed or destroyed. | | [`Hash.InvalidDigestSizeError`](/api/Hash/errors#hashinvaliddigestsizeerror) | Thrown when a digest output buffer is too small. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Hash.Hasher`](/api/Hash/types#hashhasher) | Incremental hash state. | # Hash.blake3 Calculates the [BLAKE3](https://github.com/BLAKE3-team/BLAKE3) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. Backed by `blake3` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library, unless another implementation is installed with [`Engine.set`](/api/Engine/set). ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' Hash.blake3('0xdeadbeef') // @log: '0x53147f3ce49ed4f60dfa5b9654c36ba6103c11f5737df3dabd4cbd296c4161bd' ``` ### Configure Return Type ```ts twoslash import { Hash } from 'ox' Hash.blake3('0xdeadbeef', { as: 'Bytes' }) // @log: Uint8Array [...] ``` ## Definition ```ts function blake3( value: value | Hex.Hex | Bytes.Bytes, options?: blake3.Options, ): blake3.ReturnType ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L212) ## Parameters ### value * **Type:** `value | Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. ### options * **Type:** `blake3.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The return type. Defaults to the input format. ## Return Type BLAKE3 hash. `blake3.ReturnType` # Hash.createBlake3 Creates an incremental BLAKE3 hasher. The installed Engine provider is captured when this function is called. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' const hash = Hash.createBlake3() hash.update('0xdead') hash.update('0xbeef') hash.digest() // @log: '0x53147f3ce49ed4f60dfa5b9654c36ba6103c11f5737df3dabd4cbd296c4161bd' ``` ## Definition ```ts function createBlake3(): Hasher ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L73) ## Return Type An incremental BLAKE3 hasher. [`Hasher`](/api/Hash/types#hashhasher) # Hash.createHmac256 Creates an incremental HMAC-SHA256 hasher. The installed Engine provider is captured when this function is called. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash, Hex } from 'ox' const hash = Hash.createHmac256(Hex.fromString('key')) hash.update('0xdead') hash.update('0xbeef') hash.digest() ``` ## Definition ```ts function createHmac256( key: Hex.Hex | Bytes.Bytes, ): Hasher ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L99) ## Parameters ### key * **Type:** `Hex.Hex | Bytes.Bytes` HMAC key. ## Return Type An incremental HMAC-SHA256 hasher. [`Hasher`](/api/Hash/types#hashhasher) # Hash.createKeccak256 Creates an incremental Keccak256 hasher. The installed Engine provider is captured when this function is called. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' const hash = Hash.createKeccak256() hash.update('0xdead') hash.update('0xbeef') hash.digest() // @log: '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ``` ## Definition ```ts function createKeccak256(): Hasher ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L125) ## Return Type An incremental Keccak256 hasher. [`Hasher`](/api/Hash/types#hashhasher) # Hash.createRipemd160 Creates an incremental RIPEMD-160 hasher. The installed Engine provider is captured when this function is called. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' const hash = Hash.createRipemd160() hash.update('0xdead') hash.update('0xbeef') hash.digest() // @log: '0x226821c2f5423e11fe9af68bd285c249db2e4b5a' ``` ## Definition ```ts function createRipemd160(): Hasher ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L151) ## Return Type An incremental RIPEMD-160 hasher. [`Hasher`](/api/Hash/types#hashhasher) # Hash.createSha256 Creates an incremental SHA-256 hasher. The installed Engine provider is captured when this function is called. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' const hash = Hash.createSha256() hash.update('0xdead') hash.update('0xbeef') hash.digest() // @log: '0x5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953' ``` ## Definition ```ts function createSha256(): Hasher ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L177) ## Return Type An incremental SHA-256 hasher. [`Hasher`](/api/Hash/types#hashhasher) # Hash.hmac256 Calculates the [HMAC-SHA256](https://en.wikipedia.org/wiki/HMAC) of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. Backed by `hmac` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library, unless another implementation is installed with [`Engine.set`](/api/Engine/set). ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash, Hex } from 'ox' Hash.hmac256(Hex.fromString('key'), '0xdeadbeef') // @log: '0x...' ``` ### Configure Return Type ```ts twoslash import { Hash, Hex } from 'ox' Hash.hmac256(Hex.fromString('key'), '0xdeadbeef', { as: 'Bytes' }) // @log: Uint8Array [...] ``` ## Definition ```ts function hmac256( key: Hex.Hex | Bytes.Bytes, value: value | Hex.Hex | Bytes.Bytes, options?: hmac256.Options, ): hmac256.ReturnType ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L343) ## Parameters ### key * **Type:** `Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) key. ### value * **Type:** `value | Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. ### options * **Type:** `hmac256.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The return type. ## Return Type HMAC-SHA256 hash. `hmac256.ReturnType` # Hash.keccak256 Calculates the [Keccak256](https://en.wikipedia.org/wiki/SHA-3) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. Backed by `keccak_256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library, unless another implementation is installed with [`Engine.set`](/api/Engine/set). ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' Hash.keccak256('0xdeadbeef') // @log: '0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1' ``` ### Calculate Hash of a String ```ts twoslash import { Hash, Hex } from 'ox' Hash.keccak256(Hex.fromString('hello world')) // @log: '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0' ``` ### Configure Return Type ```ts twoslash import { Hash } from 'ox' Hash.keccak256('0xdeadbeef', { as: 'Bytes' }) // @log: Uint8Array [...] ``` ## Definition ```ts function keccak256( value: value | Hex.Hex | Bytes.Bytes, options?: keccak256.Options, ): keccak256.ReturnType ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L281) ## Parameters ### value * **Type:** `value | Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. ### options * **Type:** `keccak256.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The return type. ## Return Type Keccak256 hash. `keccak256.ReturnType` # Hash.ripemd160 Calculates the [Ripemd160](https://en.wikipedia.org/wiki/RIPEMD) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. Backed by `ripemd160` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library, unless another implementation is installed with [`Engine.set`](/api/Engine/set). ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' Hash.ripemd160('0xdeadbeef') // '0x226821c2f5423e11fe9af68bd285c249db2e4b5a' ``` ## Definition ```ts function ripemd160( value: value | Hex.Hex | Bytes.Bytes, options?: ripemd160.Options, ): ripemd160.ReturnType ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L395) ## Parameters ### value * **Type:** `value | Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. ### options * **Type:** `ripemd160.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The return type. ## Return Type Ripemd160 hash. `ripemd160.ReturnType` # Hash.sha256 Calculates the [Sha256](https://en.wikipedia.org/wiki/SHA-256) hash of a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. Backed by `sha256` from [`@noble/hashes`](https://github.com/paulmillr/noble-hashes), an audited & minimal JS hashing library, unless another implementation is installed with [`Engine.set`](/api/Engine/set). ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' Hash.sha256('0xdeadbeef') // '0x5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953' ``` ## Definition ```ts function sha256( value: value | Hex.Hex | Bytes.Bytes, options?: sha256.Options, ): sha256.ReturnType ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L444) ## Parameters ### value * **Type:** `value | Hex.Hex | Bytes.Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value. ### options * **Type:** `sha256.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The return type. ## Return Type Sha256 hash. `sha256.ReturnType` # Hash.validate Checks if a string is a valid hash value. ## Imports :::code-group ```ts [Named] import { Hash } from 'ox' ``` ```ts [Entrypoint] import * as Hash from 'ox/Hash' ``` ::: ## Examples ```ts twoslash import { Hash } from 'ox' Hash.validate('0x') // @log: false Hash.validate( '0x3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0' ) // @log: true ``` ## Definition ```ts function validate( value: string, ): value is Hex.Hex ``` **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L495) ## Parameters ### value * **Type:** `string` Value to check. ## Return Type Whether the value is a valid hash. `value is Hex.Hex` # Hash Errors ## `Hash.HasherDestroyedError` Thrown when an incremental hash state has been consumed or destroyed. **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L507) ## `Hash.InvalidDigestSizeError` Thrown when a digest output buffer is too small. **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L516) # Hash Types ## `Hash.Hasher` Incremental hash state. A hasher accepts any number of chunks. Calling `digest` or `digestInto` consumes the state. Call `clone` before finalizing to branch from the same prefix. **Source:** [src/core/Hash.ts](https://github.com/wevm/ox/blob/main/src/core/Hash.ts#L14) # HdKey Utility functions for generating and working with [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) HD Wallets. :::info The `HdKey` module is a friendly wrapper over [`@scure/bip32`](https://github.com/paulmillr/scure-bip32), an **audited** implementation of [BIP-32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) HD Wallets. ::: ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`HdKey.fromExtendedKey`](/api/HdKey/fromExtendedKey) | Creates a HD Key from an extended private key. | | [`HdKey.fromJson`](/api/HdKey/fromJson) | Creates a HD Key from a JSON object containing an extended private key (`xpriv`). | | [`HdKey.fromSeed`](/api/HdKey/fromSeed) | Creates a HD Key from a master seed. | | [`HdKey.path`](/api/HdKey/path) | Creates an Ethereum-based BIP-44 HD path. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`HdKey.HdKey`](/api/HdKey/types#hdkeyhdkey) | Root type for a Hierarchical Deterministic (HD) Key. | # HdKey.fromExtendedKey Creates a HD Key from an extended private key. ## Imports :::code-group ```ts [Named] import { HdKey } from 'ox' ``` ```ts [Entrypoint] import * as HdKey from 'ox/HdKey' ``` ::: ## Examples ```ts twoslash import { HdKey } from 'ox' const hdKey = HdKey.fromExtendedKey('...') console.log(hdKey.privateKey) // @log: '0x...' ``` ## Definition ```ts function fromExtendedKey( extendedKey: string, ): HdKey ``` **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L37) ## Parameters ### extendedKey * **Type:** `string` The extended private key. ## Return Type The HD Key. `HdKey.HdKey` # HdKey.fromJson Creates a HD Key from a JSON object containing an extended private key (`xpriv`). ## Imports :::code-group ```ts [Named] import { HdKey } from 'ox' ``` ```ts [Entrypoint] import * as HdKey from 'ox/HdKey' ``` ::: ## Examples ```ts twoslash import { HdKey } from 'ox' const hdKey = HdKey.fromJson({ xpriv: '...' }) console.log(hdKey.privateKey) // @log: '0x...' ``` ## Definition ```ts function fromJson( json: { xpriv: string; }, ): HdKey ``` **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L62) ## Parameters ### json * **Type:** `{ xpriv: string; }` The JSON object containing an extended private key (`xpriv`). ## Return Type The HD Key. `HdKey.HdKey` # HdKey.fromSeed Creates a HD Key from a master seed. ## Imports :::code-group ```ts [Named] import { HdKey } from 'ox' ``` ```ts [Entrypoint] import * as HdKey from 'ox/HdKey' ``` ::: ## Examples ```ts twoslash import { HdKey, Mnemonic } from 'ox' const seed = Mnemonic.toSeed( 'test test test test test test test test test test test junk' ) const hdKey = HdKey.fromSeed(seed) ``` ### Path Derivation You can derive a HD Key at a specific path using `derive`. ```ts twoslash import { HdKey, Mnemonic } from 'ox' const mnemonic = Mnemonic.toSeed( 'test test test test test test test test test test test junk' ) const hdKey = HdKey.fromSeed(mnemonic).derive(HdKey.path()) console.log(hdKey.privateKey) // @log: '0x...' ``` ## Definition ```ts function fromSeed( seed: Hex.Hex | Bytes.Bytes, options?: fromSeed.Options, ): HdKey ``` **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L104) ## Parameters ### seed * **Type:** `Hex.Hex | Bytes.Bytes` The master seed to create the HD Key from. ### options * **Type:** `fromSeed.Options` * **Optional** Creation options. #### options.versions * **Type:** `Versions | undefined` * **Optional** The versions to use for the HD Key. ## Return Type The HD Key. `HdKey.HdKey` # HdKey.path Creates an Ethereum-based BIP-44 HD path. ## Imports :::code-group ```ts [Named] import { HdKey } from 'ox' ``` ```ts [Entrypoint] import * as HdKey from 'ox/HdKey' ``` ::: ## Examples ```ts twoslash import { HdKey } from 'ox' const path = HdKey.path({ account: 1, index: 2 }) // @log: "m/44'/60'/1'/0/2" ``` ## Definition ```ts function path( options?: path.Options, ): string ``` **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L139) ## Parameters ### options * **Type:** `path.Options` * **Optional** Path options. #### options.account * **Type:** `number` * **Optional** The account. #### options.change * **Type:** `number` * **Optional** The change. #### options.index * **Type:** `number` * **Optional** The address index. ## Return Type The path. `string` # HdKey Types ## `HdKey.HdKey` Root type for a Hierarchical Deterministic (HD) Key. **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L9) # Keystore Utilities & types for working with [Keystores](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage). ## Examples Below are some examples demonstrating common usages of the `Keystore` module: * [Encrypting Private Keys](#encrypting-private-keys) * [Decrypting Private Keys](#decrypting-private-keys) ### Encrypting Private Keys Private keys can be encrypted into a JSON keystore using [`Keystore.encrypt`](/api/Keystore/encrypt): ```ts twoslash import { Keystore, Secp256k1 } from 'ox' // Generate a random private key. const privateKey = Secp256k1.randomPrivateKey() // Derive a key from a password. const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' }) // Encrypt the private key. const keystore = Keystore.encrypt(privateKey, key, opts) // @log: { // @log: "crypto": { // @log: "cipher": "aes-128-ctr", // @log: "ciphertext": "...", // @log: "cipherparams": { // @log: "iv": "...", // @log: }, // @log: "kdf": "pbkdf2", // @log: "kdfparams": { // @log: "salt": "...", // @log: "dklen": 32, // @log: "prf": "hmac-sha256", // @log: "c": 262144, // @log: }, // @log: "mac": "...", // @log: }, // @log: "id": "...", // @log: "version": 3, // @log: } ``` ### Decrypting Private Keys Private keys can be decrypted from a JSON keystore using [`Keystore.decrypt`](/api/Keystore/decrypt): ```ts twoslash // @noErrors import { Keystore, Secp256k1 } from 'ox' const keystore = { crypto: { ... }, id: '...', version: 3 } // Derive the key. const key = Keystore.toKey(keystore, { password: 'testpassword' }) // Decrypt the private key. const decrypted = Keystore.decrypt(keystore, key) // @log: "0x..." ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Keystore.decrypt`](/api/Keystore/decrypt) | Decrypts a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) into a private key. | | [`Keystore.encrypt`](/api/Keystore/encrypt) | Encrypts a private key as a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) using a derived key. | | [`Keystore.pbkdf2`](/api/Keystore/pbkdf2) | Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). | | [`Keystore.pbkdf2Async`](/api/Keystore/pbkdf2Async) | Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). | | [`Keystore.scrypt`](/api/Keystore/scrypt) | Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). | | [`Keystore.scryptAsync`](/api/Keystore/scryptAsync) | Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). | | [`Keystore.toKey`](/api/Keystore/toKey) | Extracts a Key from a JSON Keystore to use for decryption. | | [`Keystore.toKeyAsync`](/api/Keystore/toKeyAsync) | Extracts a Key asynchronously from a JSON Keystore to use for decryption. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Keystore.DeriveOpts`](/api/Keystore/types#keystorederiveopts) | Derivation Options. | | [`Keystore.Key`](/api/Keystore/types#keystorekey) | Key. | | [`Keystore.Keystore`](/api/Keystore/types#keystorekeystore) | Keystore. | | [`Keystore.Pbkdf2DeriveOpts`](/api/Keystore/types#keystorepbkdf2deriveopts) | PBKDF2 Derivation Options. | | [`Keystore.ScryptDeriveOpts`](/api/Keystore/types#keystorescryptderiveopts) | Scrypt Derivation Options. | # Keystore.decrypt Decrypts a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) into a private key. Supports the following key derivation functions (KDFs): - [`Keystore.pbkdf2`](/api/Keystore/pbkdf2) - [`Keystore.scrypt`](/api/Keystore/scrypt) ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash // @noErrors import { Keystore, Secp256k1 } from 'ox' // JSON keystore. const keystore = { crypto: { ... }, id: '...', version: 3 } // Derive the key using your password. const key = Keystore.toKey(keystore, { password: 'hunter2' }) // Decrypt the private key. const privateKey = Keystore.decrypt(keystore, key) // @log: "0x..." ``` ## Definition ```ts function decrypt( keystore: Keystore, key: Key, options?: decrypt.Options, ): decrypt.ReturnType ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L90) ## Parameters ### keystore * **Type:** `Keystore` JSON keystore. #### keystore.cipher * **Type:** `"aes-128-ctr"` #### keystore.cipherparams * **Type:** `{ iv: string; }` #### keystore.ciphertext * **Type:** `string` #### keystore.crypto * **Type:** `{ cipher: "aes-128-ctr"; ciphertext: string; cipherparams: { iv: string; }; mac: string; } & Pick` #### keystore.id * **Type:** `string` #### keystore.iv * **Type:** `string` #### keystore.mac * **Type:** `string` #### keystore.version * **Type:** `3` ### key * **Type:** [`Key`](/api/Keystore/types#keystorekey) Key to use for decryption. ### options * **Type:** `decrypt.Options` * **Optional** Decryption options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Output format. ## Return Type Decrypted private key. `decrypt.ReturnType` # Keystore.encrypt Encrypts a private key as a [JSON keystore](https://ethereum.org/en/developers/docs/data-structures-and-encoding/web3-secret-storage/) using a derived key. Supports the following key derivation functions (KDFs): - [`Keystore.pbkdf2`](/api/Keystore/pbkdf2) - [`Keystore.scrypt`](/api/Keystore/scrypt) ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash import { Keystore, Secp256k1 } from 'ox' // Generate a random private key. const privateKey = Secp256k1.randomPrivateKey() // Derive key from password. const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' }) // Encrypt the private key. const encrypted = Keystore.encrypt(privateKey, key, opts) // @log: { // @log: "crypto": { // @log: "cipher": "aes-128-ctr", // @log: "ciphertext": "...", // @log: "cipherparams": { // @log: "iv": "...", // @log: }, // @log: "kdf": "pbkdf2", // @log: "kdfparams": { // @log: "salt": "...", // @log: "dklen": 32, // @log: "prf": "hmac-sha256", // @log: "c": 262144, // @log: }, // @log: "mac": "...", // @log: }, // @log: "id": "...", // @log: "version": 3, // @log: } ``` ## Definition ```ts function encrypt( privateKey: Bytes.Bytes | Hex.Hex, key: Key, options: encrypt.Options, ): Keystore ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L176) ## Parameters ### privateKey * **Type:** `Bytes.Bytes | Hex.Hex` Private key to encrypt. ### key * **Type:** [`Key`](/api/Keystore/types#keystorekey) Key to use for encryption. ### options * **Type:** `encrypt.Options` Encryption options. #### options.c * **Type:** `number` #### options.dklen * **Type:** `number` #### options.id * **Type:** `string` * **Optional** UUID. #### options.n * **Type:** `number` #### options.p * **Type:** `number` #### options.prf * **Type:** `"hmac-sha256"` #### options.r * **Type:** `number` #### options.salt * **Type:** `string` ## Return Type Encrypted keystore. `Keystore` # Keystore.pbkdf2 Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash import { Keystore } from 'ox' const [key, opts] = Keystore.pbkdf2({ password: 'testpassword' }) ``` ## Definition ```ts function pbkdf2( options: pbkdf2.Options, ): [() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly c: number; readonly dklen: 32; readonly prf: "hmac-sha256"; readonly salt: string; }; readonly kdf: "pbkdf2"; } & { iv: Bytes.Bytes; }] ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L228) ## Parameters ### options * **Type:** `pbkdf2.Options` PBKDF2 options. #### options.iterations * **Type:** `number` * **Optional** The number of iterations to use. #### options.iv * **Type:** `0x${string} | Uint8Array` * **Optional** The counter to use for the AES-CTR encryption. #### options.password * **Type:** `string` Password to derive key from. #### options.salt * **Type:** `0x${string} | Uint8Array` * **Optional** Salt to use for key derivation. ## Return Type PBKDF2 key. `[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly c: number; readonly dklen: 32; readonly prf: "hmac-sha256"; readonly salt: string; }; readonly kdf: "pbkdf2"; } & { iv: Bytes.Bytes; }]` # Keystore.pbkdf2Async Derives a key from a password using [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2). ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash import { Keystore } from 'ox' const [key, opts] = await Keystore.pbkdf2Async({ password: 'testpassword' }) ``` ## Definition ```ts function pbkdf2Async( options: pbkdf2.Options, ): Promise<[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly c: number; readonly dklen: 32; readonly prf: "hmac-sha256"; readonly salt: string; }; readonly kdf: "pbkdf2"; } & { iv: Bytes.Bytes; }]> ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L280) ## Parameters ### options * **Type:** `pbkdf2.Options` PBKDF2 options. #### options.iterations * **Type:** `number` * **Optional** The number of iterations to use. #### options.iv * **Type:** `0x${string} | Uint8Array` * **Optional** The counter to use for the AES-CTR encryption. #### options.password * **Type:** `string` Password to derive key from. #### options.salt * **Type:** `0x${string} | Uint8Array` * **Optional** Salt to use for key derivation. ## Return Type PBKDF2 key. `Promise<[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly c: number; readonly dklen: 32; readonly prf: "hmac-sha256"; readonly salt: string; }; readonly kdf: "pbkdf2"; } & { iv: Bytes.Bytes; }]>` # Keystore.scrypt Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash import { Keystore } from 'ox' const [key, opts] = Keystore.scrypt({ password: 'testpassword' }) ``` ## Definition ```ts function scrypt( options: scrypt.Options, ): [() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly dklen: 32; readonly n: number; readonly p: number; readonly r: number; readonly salt: string; }; readonly kdf: "scrypt"; } & { iv: Bytes.Bytes; }] ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L323) ## Parameters ### options * **Type:** `scrypt.Options` Scrypt options. #### options.iv * **Type:** `0x${string} | Uint8Array` * **Optional** The counter to use for the AES-CTR encryption. #### options.n * **Type:** `number` * **Optional** Cost factor. #### options.p * **Type:** `number` * **Optional** Parallelization factor. #### options.password * **Type:** `string` Password to derive key from. #### options.r * **Type:** `number` * **Optional** Block size. #### options.salt * **Type:** `0x${string} | Uint8Array` * **Optional** Salt to use for key derivation. ## Return Type Scrypt key. `[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly dklen: 32; readonly n: number; readonly p: number; readonly r: number; readonly salt: string; }; readonly kdf: "scrypt"; } & { iv: Bytes.Bytes; }]` # Keystore.scryptAsync Derives a key from a password using [scrypt](https://en.wikipedia.org/wiki/Scrypt). ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash import { Keystore } from 'ox' const [key, opts] = await Keystore.scryptAsync({ password: 'testpassword' }) ``` ## Definition ```ts function scryptAsync( options: scrypt.Options, ): Promise<[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly dklen: 32; readonly n: number; readonly p: number; readonly r: number; readonly salt: string; }; readonly kdf: "scrypt"; } & { iv: Bytes.Bytes; }]> ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L377) ## Parameters ### options * **Type:** `scrypt.Options` Scrypt options. #### options.iv * **Type:** `0x${string} | Uint8Array` * **Optional** The counter to use for the AES-CTR encryption. #### options.n * **Type:** `number` * **Optional** Cost factor. #### options.p * **Type:** `number` * **Optional** Parallelization factor. #### options.password * **Type:** `string` Password to derive key from. #### options.r * **Type:** `number` * **Optional** Block size. #### options.salt * **Type:** `0x${string} | Uint8Array` * **Optional** Salt to use for key derivation. ## Return Type Scrypt key. `Promise<[() => `0x${string}`, { readonly iv: `0x${string}` | Uint8Array | undefined; readonly kdfparams: { readonly dklen: 32; readonly n: number; readonly p: number; readonly r: number; readonly salt: string; }; readonly kdf: "scrypt"; } & { iv: Bytes.Bytes; }]>` # Keystore.toKey Extracts a Key from a JSON Keystore to use for decryption. ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash // @noErrors import { Keystore } from 'ox' // JSON keystore. const keystore = { crypto: { ... }, id: '...', version: 3 } const key = Keystore.toKey(keystore, { password: 'hunter2' }) // [!code focus] const decrypted = Keystore.decrypt(keystore, key) ``` ## Definition ```ts function toKey( keystore: Keystore, options: toKey.Options, ): Key ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L428) ## Parameters ### keystore * **Type:** `Keystore` JSON Keystore #### keystore.cipher * **Type:** `"aes-128-ctr"` #### keystore.cipherparams * **Type:** `{ iv: string; }` #### keystore.ciphertext * **Type:** `string` #### keystore.crypto * **Type:** `{ cipher: "aes-128-ctr"; ciphertext: string; cipherparams: { iv: string; }; mac: string; } & Pick` #### keystore.id * **Type:** `string` #### keystore.iv * **Type:** `string` #### keystore.mac * **Type:** `string` #### keystore.version * **Type:** `3` ### options * **Type:** `toKey.Options` Options #### options.password * **Type:** `string` Password to derive key from. ## Return Type Key [`Key`](/api/Keystore/types#keystorekey) # Keystore.toKeyAsync Extracts a Key asynchronously from a JSON Keystore to use for decryption. ## Imports :::code-group ```ts [Named] import { Keystore } from 'ox' ``` ```ts [Entrypoint] import * as Keystore from 'ox/Keystore' ``` ::: ## Examples ```ts twoslash // @noErrors import { Keystore } from 'ox' // JSON keystore. const keystore = { crypto: { ... }, id: '...', version: 3 } const key = await Keystore.toKeyAsync(keystore, { password: 'hunter2' }) // [!code focus] const decrypted = Keystore.decrypt(keystore, key) ``` ## Definition ```ts function toKeyAsync( keystore: Keystore, options: toKeyAsync.Options, ): Promise ``` **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L490) ## Parameters ### keystore * **Type:** `Keystore` JSON Keystore #### keystore.cipher * **Type:** `"aes-128-ctr"` #### keystore.cipherparams * **Type:** `{ iv: string; }` #### keystore.ciphertext * **Type:** `string` #### keystore.crypto * **Type:** `{ cipher: "aes-128-ctr"; ciphertext: string; cipherparams: { iv: string; }; mac: string; } & Pick` #### keystore.id * **Type:** `string` #### keystore.iv * **Type:** `string` #### keystore.mac * **Type:** `string` #### keystore.version * **Type:** `3` ### options * **Type:** `toKeyAsync.Options` Options #### options.password * **Type:** `string` Password to derive key from. ## Return Type Key [`Promise`](/api/Keystore/types#keystorekey) # Keystore Types ## `Keystore.DeriveOpts` Derivation Options. **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L36) ## `Keystore.Key` Key. **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L33) ## `Keystore.Keystore` Keystore. **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L19) ## `Keystore.Pbkdf2DeriveOpts` PBKDF2 Derivation Options. **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L39) ## `Keystore.ScryptDeriveOpts` Scrypt Derivation Options. **Source:** [src/core/Keystore.ts](https://github.com/wevm/ox/blob/main/src/core/Keystore.ts#L50) # Mnemonic Utility functions for generating and working with [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonics. :::info The `Mnemonic` module is a friendly wrapper over [`@scure/bip39`](https://github.com/paulmillr/scure-bip39), an **audited** implementation of [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) ::: ## Examples Below are some examples demonstrating common usages of the `Mnemonic` module: * [Generating a Random Mnemonic](#generating-a-random-mnemonic) * [Converting to Private Key](#converting-to-private-key) * [Converting to HD Key](#converting-to-hd-key) * [Converting to Seed](#converting-to-seed) ### Generating a Random Mnemonic Random mnemonics can be generated using [`Mnemonic.random`](/api/Mnemonic/random): ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) // @log: 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' ``` ### Converting to Private Key Mnemonics can be converted to a private key using [`Mnemonic.toPrivateKey`](/api/Mnemonic/toPrivateKey): ```ts twoslash import { Mnemonic } from 'ox' const privateKey = Mnemonic.toPrivateKey( 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' ) // @log: '0x...' ``` ### Converting to HD Key Mnemonics can be converted to a HD Key using [`Mnemonic.toHdKey`](/api/Mnemonic/toHdKey): ```ts twoslash import { Mnemonic } from 'ox' const hdKey = Mnemonic.toHdKey( 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' ) ``` ### Converting to Seed Mnemonics can be converted to a master seed using [`Mnemonic.toSeed`](/api/Mnemonic/toSeed): ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' const seed = Mnemonic.toSeed(mnemonic) // @log: Uint8Array [...64 bytes] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Mnemonic.path`](/api/Mnemonic/path) | Creates an Ethereum-based BIP-44 HD path. | | [`Mnemonic.random`](/api/Mnemonic/random) | Generates a random mnemonic. | | [`Mnemonic.toHdKey`](/api/Mnemonic/toHdKey) | Converts a mnemonic to a HD Key. | | [`Mnemonic.toPrivateKey`](/api/Mnemonic/toPrivateKey) | Converts a mnemonic to a private key. | | [`Mnemonic.toSeed`](/api/Mnemonic/toSeed) | Converts a mnemonic to a master seed. | | [`Mnemonic.validate`](/api/Mnemonic/validate) | Checks if a mnemonic is valid, given a wordlist. | # Mnemonic.path Creates an Ethereum-based BIP-44 HD path. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { HdKey } from 'ox' const path = HdKey.path({ account: 1, index: 2 }) // @log: "m/44'/60'/1'/0/2" ``` ## Definition ```ts function path( options?: path.Options, ): string ``` **Source:** [src/core/HdKey.ts](https://github.com/wevm/ox/blob/main/src/core/HdKey.ts#L139) ## Parameters ### options * **Type:** `path.Options` * **Optional** Path options. #### options.account * **Type:** `number` * **Optional** The account. #### options.change * **Type:** `number` * **Optional** The change. #### options.index * **Type:** `number` * **Optional** The address index. ## Return Type The path. `string` # Mnemonic.random Generates a random mnemonic. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) // @log: 'buyer zoo end danger ice capable shrug naive twist relief mass bonus' ``` ## Definition ```ts function random( wordlist: string[], options?: random.Options, ): string ``` **Source:** [src/core/Mnemonic.ts](https://github.com/wevm/ox/blob/main/src/core/Mnemonic.ts#L38) ## Parameters ### wordlist * **Type:** `string[]` The wordlist to use. ### options * **Type:** `random.Options` * **Optional** Generation options. #### options.strength * **Type:** `number` * **Optional** The strength of the mnemonic to generate, in bits. ## Return Type The mnemonic. `string` # Mnemonic.toHdKey Converts a mnemonic to a HD Key. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const hdKey = Mnemonic.toHdKey(mnemonic) ``` ### Path Derivation You can derive a HD Key at a specific path using `derive`: ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const hdKey = Mnemonic.toHdKey(mnemonic).derive( Mnemonic.path({ index: 1 }) ) ``` ## Definition ```ts function toHdKey( mnemonic: string, options?: toHdKey.Options, ): HdKey.HdKey ``` **Source:** [src/core/Mnemonic.ts](https://github.com/wevm/ox/blob/main/src/core/Mnemonic.ts#L87) ## Parameters ### mnemonic * **Type:** `string` The mnemonic to convert. ### options * **Type:** `toHdKey.Options` * **Optional** Conversion options. #### options.passphrase * **Type:** `string` * **Optional** An optional passphrase for additional protection to the seed. ## Return Type The HD Key. `HdKey.HdKey` # Mnemonic.toPrivateKey Converts a mnemonic to a private key. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const privateKey = Mnemonic.toPrivateKey(mnemonic) // @log: '0x...' ``` ### Paths You can derive a private key at a specific path using the `path` option. ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const privateKey = Mnemonic.toPrivateKey(mnemonic, { path: Mnemonic.path({ index: 1 }) // 'm/44'/60'/0'/0/1' // [!code focus] }) // @log: '0x...' ``` ## Definition ```ts function toPrivateKey( mnemonic: string, options?: toPrivateKey.Options, ): toPrivateKey.ReturnType ``` **Source:** [src/core/Mnemonic.ts](https://github.com/wevm/ox/blob/main/src/core/Mnemonic.ts#L136) ## Parameters ### mnemonic * **Type:** `string` The mnemonic to convert. ### options * **Type:** `toPrivateKey.Options` * **Optional** Conversion options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The output format. #### options.passphrase * **Type:** `string` * **Optional** An optional passphrase for additional protection to the seed. #### options.path * **Type:** `string` * **Optional** An optional path to derive the private key from. ## Return Type The private key. `toPrivateKey.ReturnType` # Mnemonic.toSeed Converts a mnemonic to a master seed. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.random(Mnemonic.english) const seed = Mnemonic.toSeed(mnemonic) // @log: Uint8Array [...64 bytes] ``` ## Definition ```ts function toSeed( mnemonic: string, options?: toSeed.Options, ): toSeed.ReturnType ``` **Source:** [src/core/Mnemonic.ts](https://github.com/wevm/ox/blob/main/src/core/Mnemonic.ts#L179) ## Parameters ### mnemonic * **Type:** `string` The mnemonic to convert. ### options * **Type:** `toSeed.Options` * **Optional** Conversion options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The output format. #### options.passphrase * **Type:** `string` * **Optional** An optional passphrase for additional protection to the seed. ## Return Type The master seed. `toSeed.ReturnType` # Mnemonic.validate Checks if a mnemonic is valid, given a wordlist. ## Imports :::code-group ```ts [Named] import { Mnemonic } from 'ox' ``` ```ts [Entrypoint] import * as Mnemonic from 'ox/Mnemonic' ``` ::: ## Examples ```ts twoslash import { Mnemonic } from 'ox' const mnemonic = Mnemonic.validate( 'buyer zoo end danger ice capable shrug naive twist relief mass bonus', Mnemonic.english ) // @log: true ``` ## Definition ```ts function validate( mnemonic: string, wordlist: string[], ): boolean ``` **Source:** [src/core/Mnemonic.ts](https://github.com/wevm/ox/blob/main/src/core/Mnemonic.ts#L222) ## Parameters ### mnemonic * **Type:** `string` The mnemonic to validate. ### wordlist * **Type:** `string[]` The wordlist to use. ## Return Type Whether the mnemonic is valid. `boolean` # P256 Utility functions for [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) ECDSA cryptography. :::info The `P256` module is a friendly wrapper over [`@noble/curves/p256`](https://github.com/paulmillr/noble-curves), an **audited** implementation of [P256](https://www.secg.org/sec2-v2.pdf) ::: ## Examples Below are some examples demonstrating common usages of the `P256` module: * [Computing a Random Private Key](#computing-a-random-private-key) * [Getting a Public Key](#getting-a-public-key) * [Signing a Payload](#signing-a-payload) * [Verifying a Signature](#verifying-a-signature) ### Computing a Random Private Key A random private key can be computed using [`P256.randomPrivateKey`](/api/P256/randomPrivateKey): ```ts twoslash import { P256 } from 'ox' const privateKey = P256.randomPrivateKey() // @log: '0x...' ``` ### Getting a Public Key A public key can be derived from a private key using [`P256.getPublicKey`](/api/P256/getPublicKey): ```ts twoslash import { P256 } from 'ox' const privateKey = P256.randomPrivateKey() const publicKey = P256.getPublicKey({ privateKey }) // [!code focus] // @log: { x: 3251...5152n, y: 1251...5152n } ``` ### Signing a Payload A payload can be signed using [`P256.sign`](/api/P256/sign): ```ts twoslash import { P256 } from 'ox' const privateKey = P256.randomPrivateKey() const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) // [!code focus] // @log: { r: 1251...5152n, s: 1251...5152n, yParity: 1 } ``` ### Verifying a Signature A signature can be verified using [`P256.verify`](/api/P256/verify): ```ts twoslash import { P256 } from 'ox' const privateKey = P256.randomPrivateKey() const publicKey = P256.getPublicKey({ privateKey }) const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) const isValid = P256.verify({ // [!code focus] payload: '0xdeadbeef', // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`P256.createKeyPair`](/api/P256/createKeyPair) | Creates a new P256 ECDSA key pair consisting of a private key and its corresponding public key. | | [`P256.getPublicKey`](/api/P256/getPublicKey) | Computes the P256 ECDSA public key from a provided private key. | | [`P256.getSharedSecret`](/api/P256/getSharedSecret) | Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key. | | [`P256.randomPrivateKey`](/api/P256/randomPrivateKey) | Generates a random P256 ECDSA private key. | | [`P256.recoverPublicKey`](/api/P256/recoverPublicKey) | Recovers the signing public key from the signed payload and signature. | | [`P256.sign`](/api/P256/sign) | Signs the payload with the provided private key and returns a P256 signature. | | [`P256.verify`](/api/P256/verify) | Verifies a payload was signed by the provided public key. | # P256.createKeyPair Creates a new P256 ECDSA key pair consisting of a private key and its corresponding public key. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const { privateKey, publicKey } = P256.createKeyPair() ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L37) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # P256.getPublicKey Computes the P256 ECDSA public key from a provided private key. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const publicKey = P256.getPublicKey({ privateKey: '0x...' }) ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L85) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to compute the public key from. ## Return Type The computed public key. `getPublicKey.ReturnType` # P256.getSharedSecret Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const { privateKey: privateKeyA } = P256.createKeyPair() const { publicKey: publicKeyB } = P256.createKeyPair() const sharedSecret = P256.getSharedSecret({ privateKey: privateKeyA, publicKey: publicKeyB }) ``` ## Definition ```ts function getSharedSecret( options: getSharedSecret.Options, ): getSharedSecret.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L134) ## Parameters ### options * **Type:** `getSharedSecret.Options` The options to compute the shared secret. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned shared secret. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to use for the shared secret computation. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key to use for the shared secret computation. Accepts a structured [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), a serialized hex string, or a `Uint8Array` (SEC1 encoding). ## Return Type The computed shared secret. `getSharedSecret.ReturnType` # P256.randomPrivateKey Generates a random P256 ECDSA private key. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const privateKey = P256.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L189) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # P256.recoverPublicKey Recovers the signing public key from the signed payload and signature. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const signature = P256.sign({ payload: '0xdeadbeef', privateKey: '0x...' }) const publicKey = P256.recoverPublicKey({ // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function recoverPublicKey( options: recoverPublicKey.Options, ): recoverPublicKey.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L236) ## Parameters ### options * **Type:** `recoverPublicKey.Options` The recovery options. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array` (65-byte recovered). ## Return Type The recovered public key. `recoverPublicKey.ReturnType` # P256.sign Signs the payload with the provided private key and returns a P256 signature. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const signature = P256.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function sign( options: sign.Options, ): sign.ReturnType ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L292) ## Parameters ### options * **Type:** `sign.Options` The signing options. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.extraEntropy * **Type:** `boolean | 0x${string} | Uint8Array` * **Optional** Extra entropy to add to the signing process. Setting to `true` enables hedged (RFC 6979 + extra randomness) signing. #### options.hash * **Type:** `boolean` * **Optional** If set to `true`, the payload will be hashed (sha256) before being signed. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `0x${string} | Uint8Array` ECDSA private key. ## Return Type The ECDSA [`Signature.Signature`](/api/Signature/types#signature). `sign.ReturnType` # P256.verify Verifies a payload was signed by the provided public key. ## Imports :::code-group ```ts [Named] import { P256 } from 'ox' ``` ```ts [Entrypoint] import * as P256 from 'ox/P256' ``` ::: ## Examples ```ts twoslash import { P256 } from 'ox' const { privateKey, publicKey } = P256.createKeyPair() const signature = P256.sign({ payload: '0xdeadbeef', privateKey }) const verified = P256.verify({ // [!code focus] publicKey, // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/P256.ts](https://github.com/wevm/ox/blob/main/src/core/P256.ts#L373) ## Parameters ### options * **Type:** `verify.Options` The verification options. #### options.hash * **Type:** `boolean` * **Optional** If set to `true`, the payload will be hashed (sha256) before being verified. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key that signed the payload. Accepts a structured [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), a serialized hex string, or a `Uint8Array` (SEC1 encoding). #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity?: number; } | { r: 0x${string}; s: 0x${string}; yParity: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array`. ## Return Type Whether the payload was signed by the provided public key. `boolean` # Prf Utilities for constructing credential-bound PRF configurations. ## Examples ```ts twoslash import { Prf, WebAuthn } from 'ox' const credential = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: Prf.tag('account.1') }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Prf.tag`](/api/Prf/tag) | Creates a credential-bound PRF configuration from a UTF-8 tag. | # Prf.tag Creates a credential-bound PRF configuration from a UTF-8 tag. Tags are public, stable identifiers. Use the same tag with the same credential to reproduce a PRF output. ## Imports :::code-group ```ts [Named] import { Prf } from 'ox' ``` ```ts [Entrypoint] import * as Prf from 'ox/Prf' ``` ::: ## Examples ```ts twoslash import { Prf, WebAuthn } from 'ox' const credential = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: Prf.tag('account.1') }) ``` ## Definition ```ts function tag( value: string, ): tag.ReturnType ``` **Source:** [src/core/Prf.ts](https://github.com/wevm/ox/blob/main/src/core/Prf.ts#L23) ## Parameters ### value * **Type:** `string` Tag to encode. ## Return Type A credential-bound PRF configuration. `tag.ReturnType` # PublicKey Utility functions for working with ECDSA public keys. ## Examples Below are some examples demonstrating common usages of the `PublicKey` module: * [Serializing Public Keys](#serializing-public-keys) * [Deserializing Public Keys](#deserializing-public-keys) ### Serializing Public Keys Public Keys can be serialized to Hex or Bytes using [`PublicKey.toHex`](/api/PublicKey/toHex): ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from({ prefix: 4, x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' }) const serialized = PublicKey.toHex(publicKey) // [!code focus] // @log: '0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ``` ### Deserializing Public Keys Public Keys can be deserialized from Hex or Bytes using [`PublicKey.fromHex`](/api/PublicKey/fromHex): ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.fromHex( '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ) // @log: { // @log: prefix: 4, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`PublicKey.assert`](/api/PublicKey/assert) | Asserts that a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) is valid. | | [`PublicKey.compress`](/api/PublicKey/compress) | Compresses a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). | | [`PublicKey.from`](/api/PublicKey/from) | Instantiates a typed [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) object from a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), [`Bytes.Bytes`](/api/Bytes/types#bytes), or [`Hex.Hex`](/api/Hex/types#hex). | | [`PublicKey.fromBytes`](/api/PublicKey/fromBytes) | Deserializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`PublicKey.fromHex`](/api/PublicKey/fromHex) | Deserializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) from a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`PublicKey.toBytes`](/api/PublicKey/toBytes) | Serializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`PublicKey.toHex`](/api/PublicKey/toHex) | Serializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to [`Hex.Hex`](/api/Hex/types#hex). | | [`PublicKey.validate`](/api/PublicKey/validate) | Validates a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). Returns `true` if valid, `false` otherwise. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`PublicKey.InvalidCompressedPrefixError`](/api/PublicKey/errors#publickeyinvalidcompressedprefixerror) | Thrown when the public key has an invalid prefix for a compressed public key. | | [`PublicKey.InvalidError`](/api/PublicKey/errors#publickeyinvaliderror) | Thrown when a public key is invalid. | | [`PublicKey.InvalidPrefixError`](/api/PublicKey/errors#publickeyinvalidprefixerror) | Thrown when a public key has an invalid prefix. | | [`PublicKey.InvalidSerializedSizeError`](/api/PublicKey/errors#publickeyinvalidserializedsizeerror) | Thrown when the public key has an invalid serialized size. | | [`PublicKey.InvalidUncompressedPrefixError`](/api/PublicKey/errors#publickeyinvaliduncompressedprefixerror) | Thrown when the public key has an invalid prefix for an uncompressed public key. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`PublicKey.PublicKey`](/api/PublicKey/types#publickeypublickey) | Root type for an ECDSA Public Key. | # PublicKey.assert Asserts that a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) is valid. ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' PublicKey.assert({ prefix: 4, y: '0x6e1c1f59ee1cf25b75a8d57b3c89e7e6b3b1da823df8b3b89497f30c1f000000' }) // @error: PublicKey.InvalidError: Value \`{"y":"0x..."}\` is not a valid public key. // @error: Public key must contain: // @error: - an `x` and `prefix` value (compressed) // @error: - an `x`, `y`, and `prefix` value (uncompressed) ``` ## Definition ```ts function assert( publicKey: ExactPartial, options?: assert.Options, ): asserts publicKey is PublicKey ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L44) ## Parameters ### publicKey * **Type:** `ExactPartial` The public key object to assert. ### options * **Type:** `assert.Options` * **Optional** #### options.compressed * **Type:** `boolean` * **Optional** Whether or not the public key should be compressed. # PublicKey.compress Compresses a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from({ prefix: 4, x: '0x83185...', y: '0x35477...' }) const compressed = PublicKey.compress(publicKey) // [!code focus] // @log: { // @log: prefix: 3, // @log: x: '0x83185...', // @log: } ``` ## Definition ```ts function compress( publicKey: PublicKey, ): PublicKey ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L133) ## Parameters ### publicKey * **Type:** `PublicKey` The public key to compress. #### publicKey.prefix * **Type:** `numberType` #### publicKey.x * **Type:** `0x${string}` #### publicKey.y * **Type:** `0x${string}` ## Return Type The compressed public key. `PublicKey` # PublicKey.from Instantiates a typed [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) object from a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), [`Bytes.Bytes`](/api/Bytes/types#bytes), or [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from({ prefix: 4, x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' }) // @log: { // @log: prefix: 4, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: } ``` ### From Serialized ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from( '0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ) // @log: { // @log: prefix: 4, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: } ``` ## Definition ```ts function from( value: from.Value, ): from.ReturnType ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L184) ## Parameters ### value * **Type:** `from.Value` The public key value to instantiate. #### value.prefix * **Type:** `number` * **Optional** ## Return Type The instantiated [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). `from.ReturnType` # PublicKey.fromBytes Deserializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash // @noErrors import { PublicKey } from 'ox' const publicKey = PublicKey.fromBytes(new Uint8Array([128, 3, 131, ...])) // @log: { // @log: prefix: 4, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: } ``` ## Definition ```ts function fromBytes( publicKey: Bytes.Bytes, ): PublicKey ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L262) ## Parameters ### publicKey * **Type:** `Bytes.Bytes` The serialized public key. ## Return Type The deserialized public key. `PublicKey` # PublicKey.fromHex Deserializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) from a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.fromHex( '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ) // @log: { // @log: prefix: 4, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: } ``` ### Deserializing a Compressed Public Key ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.fromHex( '0x038318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75' ) // @log: { // @log: prefix: 3, // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: } ``` ## Definition ```ts function fromHex( publicKey: Hex.Hex, ): PublicKey ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L308) ## Parameters ### publicKey * **Type:** `Hex.Hex` The serialized public key. ## Return Type The deserialized public key. `PublicKey` # PublicKey.toBytes Serializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from({ prefix: 4, x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' }) const bytes = PublicKey.toBytes(publicKey) // [!code focus] // @log: Uint8Array [128, 3, 131, ...] ``` ## Definition ```ts function toBytes( publicKey: PublicKey, options?: toBytes.Options, ): Bytes.Bytes ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L378) ## Parameters ### publicKey * **Type:** `PublicKey` The public key to serialize. #### publicKey.prefix * **Type:** `numberType` #### publicKey.x * **Type:** `0x${string}` #### publicKey.y * **Type:** `0x${string}` ### options * **Type:** `toBytes.Options` * **Optional** #### options.includePrefix * **Type:** `boolean` * **Optional** Whether to include the prefix in the serialized public key. ## Return Type The serialized public key. `Bytes.Bytes` # PublicKey.toHex Serializes a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const publicKey = PublicKey.from({ prefix: 4, x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' }) const hex = PublicKey.toHex(publicKey) // [!code focus] // @log: '0x048318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed753547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5' ``` ## Definition ```ts function toHex( publicKey: PublicKey, options?: toHex.Options, ): Hex.Hex ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L421) ## Parameters ### publicKey * **Type:** `PublicKey` The public key to serialize. #### publicKey.prefix * **Type:** `numberType` #### publicKey.x * **Type:** `0x${string}` #### publicKey.y * **Type:** `0x${string}` ### options * **Type:** `toHex.Options` * **Optional** #### options.includePrefix * **Type:** `boolean` * **Optional** Whether to include the prefix in the serialized public key. ## Return Type The serialized public key. `Hex.Hex` # PublicKey.validate Validates a [`PublicKey.PublicKey`](/api/PublicKey/types#publickey). Returns `true` if valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { PublicKey } from 'ox' ``` ```ts [Entrypoint] import * as PublicKey from 'ox/PublicKey' ``` ::: ## Examples ```ts twoslash import { PublicKey } from 'ox' const valid = PublicKey.validate({ prefix: 4, y: '0x6e1c1f59ee1cf25b75a8d57b3c89e7e6b3b1da823df8b3b89497f30c1f000000' }) // @log: false ``` ## Definition ```ts function validate( publicKey: ExactPartial, options?: validate.Options, ): boolean ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L470) ## Parameters ### publicKey * **Type:** `ExactPartial` The public key object to assert. ### options * **Type:** `validate.Options` * **Optional** #### options.compressed * **Type:** `boolean` * **Optional** Whether or not the public key should be compressed. ## Return Type `boolean` # PublicKey Errors ## `PublicKey.InvalidCompressedPrefixError` Thrown when the public key has an invalid prefix for a compressed public key. **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L535) ## `PublicKey.InvalidError` Thrown when a public key is invalid. ### Examples ```ts twoslash import { PublicKey } from 'ox' PublicKey.assert({ y: '0x01' }) // @error: PublicKey.InvalidError: Value `{"y":"0x01"}` is not a valid public key. // @error: Public key must contain: // @error: - an `x` and `prefix` value (compressed) // @error: - an `x`, `y`, and `prefix` value (uncompressed) ``` **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L505) ## `PublicKey.InvalidPrefixError` Thrown when a public key has an invalid prefix. **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L520) ## `PublicKey.InvalidSerializedSizeError` Thrown when the public key has an invalid serialized size. **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L556) ## `PublicKey.InvalidUncompressedPrefixError` Thrown when the public key has an invalid prefix for an uncompressed public key. **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L547) # PublicKey Types ## `PublicKey.PublicKey` Root type for an ECDSA Public Key. **Source:** [src/core/PublicKey.ts](https://github.com/wevm/ox/blob/main/src/core/PublicKey.ts#L8) # Secp256k1 Utility functions for [secp256k1](https://www.secg.org/sec2-v2.pdf) ECDSA cryptography. :::info The `Secp256k1` module is a friendly wrapper over [`@noble/curves/secp256k1`](https://github.com/paulmillr/noble-curves), an **audited** implementation of [secp256k1](https://www.secg.org/sec2-v2.pdf) ::: ## Examples Below are some examples demonstrating common usages of the `Secp256k1` module: * [Computing a Random Private Key](#computing-a-random-private-key) * [Getting a Public Key](#getting-a-public-key) * [Signing a Payload](#signing-a-payload) * [Verifying a Signature](#verifying-a-signature) ### Computing a Random Private Key A random private key can be computed using [`Secp256k1.randomPrivateKey`](/api/Secp256k1/randomPrivateKey): ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() // @log: '0x...' ``` ### Getting a Public Key A public key can be derived from a private key using [`Secp256k1.getPublicKey`](/api/Secp256k1/getPublicKey): ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const publicKey = Secp256k1.getPublicKey({ privateKey }) // [!code focus] // @log: { x: 3251...5152n, y: 1251...5152n } ``` ### Signing a Payload A payload can be signed using [`Secp256k1.sign`](/api/Secp256k1/sign): ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey }) // [!code focus] // @log: { r: 1251...5152n, s: 1251...5152n, yParity: 1 } ``` ### Verifying a Signature A signature can be verified using [`Secp256k1.verify`](/api/Secp256k1/verify): ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() const publicKey = Secp256k1.getPublicKey({ privateKey }) const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey }) const isValid = Secp256k1.verify({ // [!code focus] payload: '0xdeadbeef', // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Secp256k1.createKeyPair`](/api/Secp256k1/createKeyPair) | Creates a new secp256k1 ECDSA key pair consisting of a private key and its corresponding public key. | | [`Secp256k1.fromPrf`](/api/Secp256k1/fromPrf) | Derives a valid secp256k1 private key from a 32-byte WebAuthn PRF output. | | [`Secp256k1.getPublicKey`](/api/Secp256k1/getPublicKey) | Computes the secp256k1 ECDSA public key from a provided private key. | | [`Secp256k1.getSharedSecret`](/api/Secp256k1/getSharedSecret) | Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key. | | [`Secp256k1.randomPrivateKey`](/api/Secp256k1/randomPrivateKey) | Generates a random ECDSA private key on the secp256k1 curve. | | [`Secp256k1.recoverAddress`](/api/Secp256k1/recoverAddress) | Recovers the signing address from the signed payload and signature. | | [`Secp256k1.recoverPublicKey`](/api/Secp256k1/recoverPublicKey) | Recovers the signing public key from the signed payload and signature. | | [`Secp256k1.sign`](/api/Secp256k1/sign) | Signs the payload with the provided private key. | | [`Secp256k1.verify`](/api/Secp256k1/verify) | Verifies a payload was signed by the provided address. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Secp256k1.InvalidPrfSizeError`](/api/Secp256k1/errors#secp256k1invalidprfsizeerror) | Thrown when a WebAuthn PRF output is not 32 bytes. | # Secp256k1.createKeyPair Creates a new secp256k1 ECDSA key pair consisting of a private key and its corresponding public key. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const { privateKey, publicKey } = Secp256k1.createKeyPair() ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L40) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # Secp256k1.fromPrf Derives a valid secp256k1 private key from a 32-byte WebAuthn PRF output. The permanent derivation contract uses the PRF output as the HMAC-SHA256 key. Its message is the UTF-8 bytes of `ox.secp256k1.fromPrf.v1` followed by a 32-bit big-endian counter starting at zero. Invalid scalars are skipped. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.fromPrf( '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' ) ``` ## Definition ```ts function fromPrf( value: Hex.Hex | Bytes.Bytes, options?: fromPrf.Options, ): fromPrf.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L95) ## Parameters ### value * **Type:** `Hex.Hex | Bytes.Bytes` A 32-byte WebAuthn PRF output. ### options * **Type:** `fromPrf.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type A valid secp256k1 private key. `fromPrf.ReturnType` # Secp256k1.getPublicKey Computes the secp256k1 ECDSA public key from a provided private key. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const publicKey = Secp256k1.getPublicKey({ privateKey: '0x...' }) ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L159) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to compute the public key from. ## Return Type The computed public key. `getPublicKey.ReturnType` # Secp256k1.getSharedSecret Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const { privateKey: privateKeyA } = Secp256k1.createKeyPair() const { publicKey: publicKeyB } = Secp256k1.createKeyPair() const sharedSecret = Secp256k1.getSharedSecret({ privateKey: privateKeyA, publicKey: publicKeyB }) ``` ## Definition ```ts function getSharedSecret( options: getSharedSecret.Options, ): getSharedSecret.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L212) ## Parameters ### options * **Type:** `getSharedSecret.Options` The options to compute the shared secret. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned shared secret. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to use for the shared secret computation. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key to use for the shared secret computation. Accepts a structured [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), a serialized hex string, or a `Uint8Array` (SEC1 encoding). ## Return Type The computed shared secret. `getSharedSecret.ReturnType` # Secp256k1.randomPrivateKey Generates a random ECDSA private key on the secp256k1 curve. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = Secp256k1.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L268) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # Secp256k1.recoverAddress Recovers the signing address from the signed payload and signature. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey: '0x...' }) const address = Secp256k1.recoverAddress({ // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function recoverAddress( options: recoverAddress.Options, ): recoverAddress.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L315) ## Parameters ### options * **Type:** `recoverAddress.Options` The recovery options. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array` (65-byte recovered). ## Return Type The recovered address. `recoverAddress.ReturnType` # Secp256k1.recoverPublicKey Recovers the signing public key from the signed payload and signature. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey: '0x...' }) const publicKey = Secp256k1.recoverPublicKey({ // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function recoverPublicKey( options: recoverPublicKey.Options, ): recoverPublicKey.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L364) ## Parameters ### options * **Type:** `recoverPublicKey.Options` The recovery options. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array` (65-byte recovered). ## Return Type The recovered public key. `recoverPublicKey.ReturnType` # Secp256k1.sign Signs the payload with the provided private key. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ```ts twoslash import { Secp256k1 } from 'ox' const signature = Secp256k1.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function sign( options: sign.Options, ): sign.ReturnType ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L420) ## Parameters ### options * **Type:** `sign.Options` The signing options. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.extraEntropy * **Type:** `boolean | 0x${string} | Uint8Array` * **Optional** Extra entropy to add to the signing process. Setting to `true` enables hedged (RFC 6979 + extra randomness) signing. #### options.hash * **Type:** `boolean` * **Optional** If set to `true`, the payload will be hashed (sha256) before being signed. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `0x${string} | Uint8Array` ECDSA private key. ## Return Type The ECDSA [`Signature.Signature`](/api/Signature/types#signature). `sign.ReturnType` # Secp256k1.verify Verifies a payload was signed by the provided address. ## Imports :::code-group ```ts [Named] import { Secp256k1 } from 'ox' ``` ```ts [Entrypoint] import * as Secp256k1 from 'ox/Secp256k1' ``` ::: ## Examples ### Verify with Ethereum Address ```ts twoslash import { Secp256k1 } from 'ox' const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey: '0x...' }) const verified = Secp256k1.verify({ // [!code focus] address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ### Verify with Public Key ```ts twoslash import { Secp256k1 } from 'ox' const privateKey = '0x...' const publicKey = Secp256k1.getPublicKey({ privateKey }) const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey }) const verified = Secp256k1.verify({ // [!code focus] publicKey, // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L522) ## Parameters ### options * **Type:** `verify.Options` The verification options. #### options.address * **Type:** `abitype_Address` Address that signed the payload. #### options.hash * **Type:** `boolean` * **Optional** If set to `true`, the payload will be hashed (sha256) before being verified. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key that signed the payload. Accepts a structured [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), a serialized hex string, or a `Uint8Array` (SEC1 encoding). #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity?: number; } | { r: 0x${string}; s: 0x${string}; yParity: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array`. ## Return Type Whether the payload was signed by the provided address. `boolean` # Secp256k1 Errors ## `Secp256k1.InvalidPrfSizeError` Thrown when a WebAuthn PRF output is not 32 bytes. **Source:** [src/core/Secp256k1.ts](https://github.com/wevm/ox/blob/main/src/core/Secp256k1.ts#L578) # Signature Utility functions for working with ECDSA signatures. ## Examples Below are some examples demonstrating common usages of the `Signature` module: * [Serializing a Signature](#serializing-a-signature) * [Deserializing a Signature](#deserializing-a-signature) ### Serializing a Signature Signatures can be serialized to Hex or Bytes using [`Signature.toHex`](/api/Signature/toHex): ```ts twoslash import { Signature } from 'ox' const signature = Signature.toHex({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c' ``` ### Deserializing a Signature Signatures can be deserialized from Hex or Bytes using [`Signature.fromHex`](/api/Signature/fromHex): ```ts twoslash import { Signature } from 'ox' Signature.fromHex( '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c' ) // @log: { r: 5231...n, s: 3522...n, yParity: 0 } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Signature.assert`](/api/Signature/assert) | Asserts that a Signature is valid. | | [`Signature.extract`](/api/Signature/extract) | Extracts a [`Signature.Signature`](/api/Signature/types#signature) from an arbitrary object that may include signature properties. | | [`Signature.from`](/api/Signature/from) | Instantiates a typed [`Signature.Signature`](/api/Signature/types#signature) object from a [`Signature.Signature`](/api/Signature/types#signature), [`Signature.Legacy`](/api/Signature/types#legacy), [`Bytes.Bytes`](/api/Bytes/types#bytes), or [`Hex.Hex`](/api/Hex/types#hex). | | [`Signature.fromBytes`](/api/Signature/fromBytes) | Deserializes a [`Bytes.Bytes`](/api/Bytes/types#bytes) signature into a structured [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromCompactBytes`](/api/Signature/fromCompactBytes) | Decodes a 64-byte compact byte representation (`r ++ s`, big-endian) into a [`Signature.Signature`](/api/Signature/types#signature) (without recovery). | | [`Signature.fromDerBytes`](/api/Signature/fromDerBytes) | Converts a DER-encoded signature to a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromDerHex`](/api/Signature/fromDerHex) | Converts a DER-encoded signature to a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromHex`](/api/Signature/fromHex) | Deserializes a [`Hex.Hex`](/api/Hex/types#hex) signature into a structured [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromLegacy`](/api/Signature/fromLegacy) | Converts a [`Signature.Legacy`](/api/Signature/types#legacy) into a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromRecoveredBytes`](/api/Signature/fromRecoveredBytes) | Decodes a 65-byte recovered byte representation (`yParity ++ r ++ s`, big-endian) into a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromRpc`](/api/Signature/fromRpc) | Converts a [`Signature.Rpc`](/api/Signature/types#rpc) into a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.fromTuple`](/api/Signature/fromTuple) | Converts a [`Signature.Tuple`](/api/Signature/types#tuple) to a [`Signature.Signature`](/api/Signature/types#signature). | | [`Signature.toBytes`](/api/Signature/toBytes) | Serializes a [`Signature.Signature`](/api/Signature/types#signature) to [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Signature.toCompactBytes`](/api/Signature/toCompactBytes) | Encodes a [`Signature.Signature`](/api/Signature/types#signature) as a 64-byte compact byte representation (`r ++ s`, big-endian, 32 bytes each). | | [`Signature.toDerBytes`](/api/Signature/toDerBytes) | Converts a [`Signature.Signature`](/api/Signature/types#signature) to DER-encoded format. | | [`Signature.toDerHex`](/api/Signature/toDerHex) | Converts a [`Signature.Signature`](/api/Signature/types#signature) to DER-encoded format. | | [`Signature.toHex`](/api/Signature/toHex) | Serializes a [`Signature.Signature`](/api/Signature/types#signature) to [`Hex.Hex`](/api/Hex/types#hex). | | [`Signature.toLegacy`](/api/Signature/toLegacy) | Converts a [`Signature.Signature`](/api/Signature/types#signature) into a [`Signature.Legacy`](/api/Signature/types#legacy). | | [`Signature.toRecoveredBytes`](/api/Signature/toRecoveredBytes) | Encodes a [`Signature.Signature`](/api/Signature/types#signature) as a 65-byte recovered byte representation (`yParity ++ r ++ s`, big-endian). | | [`Signature.toRpc`](/api/Signature/toRpc) | Converts a [`Signature.Signature`](/api/Signature/types#signature) into a [`Signature.Rpc`](/api/Signature/types#rpc). | | [`Signature.toTuple`](/api/Signature/toTuple) | Converts a [`Signature.Signature`](/api/Signature/types#signature) to a serialized [`Signature.Tuple`](/api/Signature/types#tuple) to be used for signatures in Transaction Envelopes, EIP-7702 Authorization Lists, etc. | | [`Signature.validate`](/api/Signature/validate) | Validates a Signature. Returns `true` if the signature is valid, `false` otherwise. | | [`Signature.vToYParity`](/api/Signature/vToYParity) | Converts a ECDSA `v` value to a `yParity` value. | | [`Signature.yParityToV`](/api/Signature/yParityToV) | Converts a ECDSA `v` value to a `yParity` value. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Signature.InvalidRError`](/api/Signature/errors#signatureinvalidrerror) | Thrown when the signature has an invalid `r` value. | | [`Signature.InvalidSerializedSizeError`](/api/Signature/errors#signatureinvalidserializedsizeerror) | Thrown when the serialized signature is of an invalid size. | | [`Signature.InvalidSError`](/api/Signature/errors#signatureinvalidserror) | Thrown when the signature has an invalid `s` value. | | [`Signature.InvalidVError`](/api/Signature/errors#signatureinvalidverror) | Thrown when the signature has an invalid `v` value. | | [`Signature.InvalidYParityError`](/api/Signature/errors#signatureinvalidyparityerror) | Thrown when the signature has an invalid `yParity` value. | | [`Signature.MissingPropertiesError`](/api/Signature/errors#signaturemissingpropertieserror) | Thrown when the signature is missing either an `r`, `s`, or `yParity` property. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Signature.Legacy`](/api/Signature/types#signaturelegacy) | (Legacy) ECDSA signature. | | [`Signature.LegacyRpc`](/api/Signature/types#signaturelegacyrpc) | RPC-formatted (Legacy) ECDSA signature. | | [`Signature.Rpc`](/api/Signature/types#signaturerpc) | RPC-formatted ECDSA signature. `yParity` is hex-encoded. | | [`Signature.Signature`](/api/Signature/types#signaturesignature) | Root type for an ECDSA signature. | | [`Signature.Tuple`](/api/Signature/types#signaturetuple) | | # Signature.assert Asserts that a Signature is valid. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash // @errors: 2322 import { Signature } from 'ox' Signature.assert({ r: '-0x6e1c1f59ee1cf25b75a8d57b3c89e7e6b3b1da823df8b3b89497f30c1f000000', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @error: InvalidSignatureRError: // @error: Value `-0x...` is an invalid r value. // @error: r must be a positive integer less than 2^256. ``` ## Definition ```ts function assert( signature: ExactPartial, options?: assert.Options, ): asserts signature is Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L66) ## Parameters ### signature * **Type:** `ExactPartial` The signature object to assert. ### options * **Type:** `assert.Options` * **Optional** #### options.recovered * **Type:** `boolean` * **Optional** Whether or not the signature should be recovered (contain `yParity`). # Signature.extract Extracts a [`Signature.Signature`](/api/Signature/types#signature) from an arbitrary object that may include signature properties. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash // @noErrors import { Signature } from 'ox' Signature.extract({ baz: 'barry', foo: 'bar', r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1, zebra: 'stripes' }) // @log: { // @log: r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // @log: s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // @log: yParity: 1 // @log: } ``` ## Definition ```ts function extract( value: extract.Value, ): Signature | undefined ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L214) ## Parameters ### value * **Type:** `extract.Value` The arbitrary object to extract the signature from. #### value.r * **Type:** `0x${string}` * **Optional** #### value.s * **Type:** `0x${string}` * **Optional** #### value.v * **Type:** `number | 0x${string}` * **Optional** #### value.yParity * **Type:** `number | 0x${string}` * **Optional** ## Return Type The extracted [`Signature.Signature`](/api/Signature/types#signature). `Signature | undefined` # Signature.from Instantiates a typed [`Signature.Signature`](/api/Signature/types#signature) object from a [`Signature.Signature`](/api/Signature/types#signature), [`Signature.Legacy`](/api/Signature/types#legacy), [`Bytes.Bytes`](/api/Bytes/types#bytes), or [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: { // @log: r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // @log: s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // @log: yParity: 1 // @log: } ``` ### From Serialized ```ts twoslash import { Signature } from 'ox' Signature.from( '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db801' ) // @log: { // @log: r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // @log: s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // @log: yParity: 1, // @log: } ``` ### From Legacy ```ts twoslash import { Signature } from 'ox' Signature.from({ r: '0x68a020a21f5d20c5cf6c9b4d2dccdcdd14a9f7b9c2eb19d3d3acdb2a1a9c1f50', s: '0x7e8d44a4a8a3e3a4c6a3c1a3c3a3c1a3c1a3c1a3c1a3c1a3c1a3c1a3c1a3c1a4', v: 27 }) // @log: { // @log: r: '0x68a020a21f5d20c5cf6c9b4d2dccdcdd14a9f7b9c2eb19d3d3acdb2a1a9c1f50', // @log: s: '0x7e8d44a4a8a3e3a4c6a3c1a3c3a3c1a3c1a3c1a3c1a3c1a3c1a3c1a3c1a3c1a4', // @log: yParity: 0 // @log: } ``` ## Definition ```ts function from( signature: signature | OneOf | Rpc | Legacy | LegacyRpc> | Hex.Hex | Bytes.Bytes, ): from.ReturnType ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L286) ## Parameters ### signature * **Type:** `signature | OneOf | Rpc | Legacy | LegacyRpc> | Hex.Hex | Bytes.Bytes` The signature value to instantiate. ## Return Type The instantiated [`Signature.Signature`](/api/Signature/types#signature). `from.ReturnType` # Signature.fromBytes Deserializes a [`Bytes.Bytes`](/api/Bytes/types#bytes) signature into a structured [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash // @noErrors import { Signature } from 'ox' Signature.fromBytes(new Uint8Array([128, 3, 131, ...])) // @log: { r: '0x6e10...', s: '0x4a90...', yParity: 0 } ``` ## Definition ```ts function fromBytes( signature: Bytes.Bytes, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L128) ## Parameters ### signature * **Type:** `Bytes.Bytes` The serialized signature. ## Return Type The deserialized [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromCompactBytes Decodes a 64-byte compact byte representation (`r ++ s`, big-endian) into a [`Signature.Signature`](/api/Signature/types#signature) (without recovery). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromCompactBytes( new Uint8Array(64) ) // @log: { r: '0x00...0000', s: '0x00...0000' } ``` ## Definition ```ts function fromCompactBytes( bytes: Bytes.Bytes, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L593) ## Parameters ### bytes * **Type:** `Bytes.Bytes` The 64-byte compact representation. ## Return Type The decoded [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromDerBytes Converts a DER-encoded signature to a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash // @noErrors import { Signature } from 'ox' const signature = Signature.fromDerBytes(new Uint8Array([132, 51, 23, ...])) // @log: { // @log: r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // @log: s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // @log: } ``` ## Definition ```ts function fromDerBytes( signature: Bytes.Bytes, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L357) ## Parameters ### signature * **Type:** `Bytes.Bytes` The DER-encoded signature to convert. ## Return Type The [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromDerHex Converts a DER-encoded signature to a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromDerHex( '0x304402206e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf02204a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8' ) // @log: { // @log: r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', // @log: s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', // @log: } ``` ## Definition ```ts function fromDerHex( signature: Hex.Hex, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L384) ## Parameters ### signature * **Type:** `Hex.Hex` The DER-encoded signature to convert. ## Return Type The [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromHex Deserializes a [`Hex.Hex`](/api/Hex/types#hex) signature into a structured [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' Signature.fromHex( '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c' ) // @log: { r: '0x6e10...', s: '0x4a90...', yParity: 0 } ``` ## Definition ```ts function fromHex( signature: Hex.Hex, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L152) ## Parameters ### signature * **Type:** `Hex.Hex` ## Return Type The deserialized [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromLegacy Converts a [`Signature.Legacy`](/api/Signature/types#legacy) into a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const legacy = Signature.fromLegacy({ r: '0x01', s: '0x02', v: 28 }) // @log: { r: '0x01', s: '0x02', yParity: 1 } ``` ## Definition ```ts function fromLegacy( signature: Legacy | LegacyRpc, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L417) ## Parameters ### signature * **Type:** `Legacy | LegacyRpc` The [`Signature.Legacy`](/api/Signature/types#legacy) to convert. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.v * **Type:** `numberType` ## Return Type The converted [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromRecoveredBytes Decodes a 65-byte recovered byte representation (`yParity ++ r ++ s`, big-endian) into a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromRecoveredBytes( new Uint8Array(65) ) // @log: { r: '0x00...0000', s: '0x00...0000', yParity: 0 } ``` ## Definition ```ts function fromRecoveredBytes( bytes: Bytes.Bytes, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L652) ## Parameters ### bytes * **Type:** `Bytes.Bytes` The 65-byte recovered representation. ## Return Type The decoded [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromRpc Converts a [`Signature.Rpc`](/api/Signature/types#rpc) into a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromRpc({ r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0' }) ``` ## Definition ```ts function fromRpc( signature: { r: Hex.Hex; s: Hex.Hex; yParity?: Hex.Hex | undefined; v?: Hex.Hex | undefined; }, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L448) ## Parameters ### signature * **Type:** `{ r: Hex.Hex; s: Hex.Hex; yParity?: Hex.Hex | undefined; v?: Hex.Hex | undefined; }` The [`Signature.Rpc`](/api/Signature/types#rpc) to convert. ## Return Type The converted [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.fromTuple Converts a [`Signature.Tuple`](/api/Signature/types#tuple) to a [`Signature.Signature`](/api/Signature/types#signature). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.fromTuple([ '0x01', '0x7b', '0x1c8' ]) // @log: { // @log: r: '0x000000000000000000000000000000000000000000000000000000000000007b', // @log: s: '0x00000000000000000000000000000000000000000000000000000000000001c8', // @log: yParity: 1, // @log: } ``` ## Definition ```ts function fromTuple( tuple: Tuple, ): Signature ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L497) ## Parameters ### tuple * **Type:** `Tuple` The [`Signature.Tuple`](/api/Signature/types#tuple) to convert. ## Return Type The [`Signature.Signature`](/api/Signature/types#signature). `Signature` # Signature.toBytes Serializes a [`Signature.Signature`](/api/Signature/types#signature) to [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.toBytes({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: Uint8Array [102, 16, 10, ...] ``` ## Definition ```ts function toBytes( signature: Signature, ): Bytes.Bytes ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L533) ## Parameters ### signature * **Type:** `Signature` The signature to serialize. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The serialized signature. `Bytes.Bytes` # Signature.toCompactBytes Encodes a [`Signature.Signature`](/api/Signature/types#signature) as a 64-byte compact byte representation (`r ++ s`, big-endian, 32 bytes each). Used for signature inputs that omit the recovery byte (e.g. ECDSA verify). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const bytes = Signature.toCompactBytes({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: Uint8Array [110, 16, 10, ...] // 64 bytes ``` ## Definition ```ts function toCompactBytes( signature: Signature, ): Bytes.Bytes ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L565) ## Parameters ### signature * **Type:** `Signature` The signature to encode. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The 64-byte compact representation. `Bytes.Bytes` # Signature.toDerBytes Converts a [`Signature.Signature`](/api/Signature/types#signature) to DER-encoded format. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8' }) const signature_der = Signature.toDerBytes(signature) // @log: Uint8Array [132, 51, 23, ...] ``` ## Definition ```ts function toDerBytes( signature: Signature, ): Bytes.Bytes ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L726) ## Parameters ### signature * **Type:** `Signature` The signature to convert. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The DER-encoded signature. `Bytes.Bytes` # Signature.toDerHex Converts a [`Signature.Signature`](/api/Signature/types#signature) to DER-encoded format. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.from({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8' }) const signature_der = Signature.toDerHex(signature) // @log: '0x304402206e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf02204a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8' ``` ## Definition ```ts function toDerHex( signature: Signature, ): Hex.Hex ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L754) ## Parameters ### signature * **Type:** `Signature` The signature to convert. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The DER-encoded signature. `Hex.Hex` # Signature.toHex Serializes a [`Signature.Signature`](/api/Signature/types#signature) to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.toHex({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c' ``` ## Definition ```ts function toHex( signature: Signature, ): Hex.Hex ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L682) ## Parameters ### signature * **Type:** `Signature` The signature to serialize. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The serialized signature. `Hex.Hex` # Signature.toLegacy Converts a [`Signature.Signature`](/api/Signature/types#signature) into a [`Signature.Legacy`](/api/Signature/types#legacy). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const legacy = Signature.toLegacy({ r: '0x01', s: '0x02', yParity: 1 }) // @log: { r: '0x01', s: '0x02', v: 28 } ``` ## Definition ```ts function toLegacy( signature: Signature, ): Legacy ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L781) ## Parameters ### signature * **Type:** `Signature` The [`Signature.Signature`](/api/Signature/types#signature) to convert. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The converted [`Signature.Legacy`](/api/Signature/types#legacy). [`Signature.Legacy`](/api/Signature/types#signaturelegacy) # Signature.toRecoveredBytes Encodes a [`Signature.Signature`](/api/Signature/types#signature) as a 65-byte recovered byte representation (`yParity ++ r ++ s`, big-endian). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const bytes = Signature.toRecoveredBytes({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: Uint8Array [1, 110, 16, ...] // 65 bytes ``` ## Definition ```ts function toRecoveredBytes( signature: Signature, ): Bytes.Bytes ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L623) ## Parameters ### signature * **Type:** `Signature` The signature to encode. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The 65-byte recovered representation. `Bytes.Bytes` # Signature.toRpc Converts a [`Signature.Signature`](/api/Signature/types#signature) into a [`Signature.Rpc`](/api/Signature/types#rpc). ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signature = Signature.toRpc({ r: '0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) ``` ## Definition ```ts function toRpc( signature: toRpc.Input, ): Rpc ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L810) ## Parameters ### signature * **Type:** `toRpc.Input` The [`Signature.Signature`](/api/Signature/types#signature) to convert. ## Return Type The converted [`Signature.Rpc`](/api/Signature/types#rpc). `Rpc` # Signature.toTuple Converts a [`Signature.Signature`](/api/Signature/types#signature) to a serialized [`Signature.Tuple`](/api/Signature/types#tuple) to be used for signatures in Transaction Envelopes, EIP-7702 Authorization Lists, etc. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const signatureTuple = Signature.toTuple({ r: '0x000000000000000000000000000000000000000000000000000000000000007b', s: '0x00000000000000000000000000000000000000000000000000000000000001c8', yParity: 1 }) // @log: [yParity: '0x01', r: '0x7b', s: '0x1c8'] ``` ## Definition ```ts function toTuple( signature: Signature, ): Tuple ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L844) ## Parameters ### signature * **Type:** `Signature` The [`Signature.Signature`](/api/Signature/types#signature) to convert. #### signature.r * **Type:** `0x${string}` #### signature.s * **Type:** `0x${string}` #### signature.yParity * **Type:** `numberType` * **Optional** ## Return Type The [`Signature.Tuple`](/api/Signature/types#tuple). `Tuple` # Signature.validate Validates a Signature. Returns `true` if the signature is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash // @errors: 2322 import { Signature } from 'ox' const valid = Signature.validate({ r: '-0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf', s: '0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8', yParity: 1 }) // @log: false ``` ## Definition ```ts function validate( signature: ExactPartial, options?: validate.Options, ): boolean ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L874) ## Parameters ### signature * **Type:** `ExactPartial` The signature object to assert. ### options * **Type:** `validate.Options` * **Optional** #### options.recovered * **Type:** `boolean` * **Optional** Whether or not the signature should be recovered (contain `yParity`). ## Return Type `boolean` # Signature.vToYParity Converts a ECDSA `v` value to a `yParity` value. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const yParity = Signature.vToYParity(28) // @log: 1 ``` ## Definition ```ts function vToYParity( v: number, ): Signature['yParity'] ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L909) ## Parameters ### v * **Type:** `number` The ECDSA `v` value to convert. ## Return Type The `yParity` value. `Signature['yParity']` # Signature.yParityToV Converts a ECDSA `v` value to a `yParity` value. ## Imports :::code-group ```ts [Named] import { Signature } from 'ox' ``` ```ts [Entrypoint] import * as Signature from 'ox/Signature' ``` ::: ## Examples ```ts twoslash import { Signature } from 'ox' const v = Signature.yParityToV(1) // @log: 28 ``` ## Definition ```ts function yParityToV( yParity: number, ): number ``` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L934) ## Parameters ### yParity * **Type:** `number` The ECDSA `yParity` value to convert. ## Return Type The `v` value. `number` # Signature Errors ## `Signature.InvalidRError` Thrown when the signature has an invalid `r` value. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L970) ## `Signature.InvalidSerializedSizeError` Thrown when the serialized signature is of an invalid size. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L945) ## `Signature.InvalidSError` Thrown when the signature has an invalid `s` value. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L981) ## `Signature.InvalidVError` Thrown when the signature has an invalid `v` value. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L1003) ## `Signature.InvalidYParityError` Thrown when the signature has an invalid `yParity` value. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L992) ## `Signature.MissingPropertiesError` Thrown when the signature is missing either an `r`, `s`, or `yParity` property. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L959) # Signature Types ## `Signature.Legacy` (Legacy) ECDSA signature. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L35) ## `Signature.LegacyRpc` RPC-formatted (Legacy) ECDSA signature. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L42) ## `Signature.Rpc` RPC-formatted ECDSA signature. `yParity` is hex-encoded. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L29) ## `Signature.Signature` Root type for an ECDSA signature. **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L11) ## `Signature.Tuple` **Source:** [src/core/Signature.ts](https://github.com/wevm/ox/blob/main/src/core/Signature.ts#L44) # WebAuthn Utilities for WebAuthn credentials, credential-bound PRFs, and [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) signatures. ## Examples Below are some examples demonstrating common usages of the `WebAuthn` module: * [Creating Credentials](#creating-credentials) * [Signing Payloads](#signing-payloads) * [Verifying Signatures](#verifying-signatures) ### Creating Credentials Credentials can be created using [`WebAuthn.createCredential`](/api/WebAuthn/createCredential): ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) // [!code focus] // @log: { // @log: id: 'oZ48...', // @log: publicKey: { x: 51421...5123n, y: 12345...6789n }, // @log: raw: PublicKeyCredential {}, // @log: } const { metadata, signature } = await WebAuthn.sign({ credentialId: credential.id, challenge: '0xdeadbeef' }) ``` ### Signing Payloads Payloads can be signed using [`WebAuthn.sign`](/api/WebAuthn/sign): ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const { metadata, signature } = await WebAuthn.sign({ // [!code focus] credentialId: credential.id, // [!code focus] challenge: '0xdeadbeef' // [!code focus] }) // [!code focus] // @log: { // @log: metadata: { // @log: authenticatorData: '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000', // @log: clientDataJSON: '{"type":"webauthn.get","challenge":"9jEFijuhEWrM4SOW-tChJbUEHEP44VcjcJ-Bqo1fTM8","origin":"http://localhost:5173","crossOrigin":false}', // @log: challengeIndex: 23, // @log: typeIndex: 1, // @log: userVerificationRequired: true, // @log: }, // @log: signature: { r: 51231...4215n, s: 12345...6789n }, // @log: } ``` ### Verifying Signatures Signatures can be verified using [`WebAuthn.verify`](/api/WebAuthn/verify): ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const { metadata, signature } = await WebAuthn.sign({ credentialId: credential.id, challenge: '0xdeadbeef' }) const result = await WebAuthn.verify({ // [!code focus] metadata, // [!code focus] challenge: '0xdeadbeef', // [!code focus] publicKey: credential.publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`WebAuthn.createCredential`](/api/WebAuthn/createCredential) | Creates a WebAuthn credential and optionally returns its credential-bound PRF output. | | [`WebAuthn.getCredential`](/api/WebAuthn/getCredential) | Requests a WebAuthn credential and returns its credential-bound PRF output. | | [`WebAuthn.sign`](/api/WebAuthn/sign) | Signs a challenge using a stored WebAuthn P256 Credential. If no Credential is provided, a prompt will be displayed for the user to select an existing Credential that was previously registered. | | [`WebAuthn.verify`](/api/WebAuthn/verify) | Verifies a signature using the Credential's public key and the challenge which was signed. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`WebAuthn.GetCredentialFailedError`](/api/WebAuthn/errors#webauthngetcredentialfailederror) | Thrown when a WebAuthn credential request fails. | | [`WebAuthn.InvalidExtensionError`](/api/WebAuthn/errors#webauthninvalidextensionerror) | Thrown when a caller supplies the managed `prf` extension. | | [`WebAuthn.InvalidOptionsError`](/api/WebAuthn/errors#webauthninvalidoptionserror) | Thrown when raw credential options are combined with managed PRF evaluation. | | [`WebAuthn.InvalidPrfOutputError`](/api/WebAuthn/errors#webauthninvalidprfoutputerror) | Thrown when a WebAuthn PRF result is not a valid 32-byte output. | | [`WebAuthn.PrfEvaluationFailedError`](/api/WebAuthn/errors#webauthnprfevaluationfailederror) | Thrown when WebAuthn PRF evaluation fails after a credential ceremony. | | [`WebAuthn.PrfNotSupportedError`](/api/WebAuthn/errors#webauthnprfnotsupportederror) | Thrown when a created credential does not support PRF evaluation. | | [`WebAuthn.PrfUnavailableError`](/api/WebAuthn/errors#webauthnprfunavailableerror) | Thrown when a credential assertion does not return a PRF output. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`WebAuthn.AttestationConveyancePreference`](/api/WebAuthn/types#webauthnattestationconveyancepreference) | | | [`WebAuthn.AuthenticatorAttachment`](/api/WebAuthn/types#webauthnauthenticatorattachment) | | | [`WebAuthn.AuthenticatorTransport`](/api/WebAuthn/types#webauthnauthenticatortransport) | | | [`WebAuthn.BufferSource`](/api/WebAuthn/types#webauthnbuffersource) | | | [`WebAuthn.COSEAlgorithmIdentifier`](/api/WebAuthn/types#webauthncosealgorithmidentifier) | | | [`WebAuthn.CredentialMediationRequirement`](/api/WebAuthn/types#webauthncredentialmediationrequirement) | | | [`WebAuthn.LargeBlobSupport`](/api/WebAuthn/types#webauthnlargeblobsupport) | | | [`WebAuthn.P256Credential`](/api/WebAuthn/types#webauthnp256credential) | A WebAuthn-flavored P256 credential. | | [`WebAuthn.Prf`](/api/WebAuthn/types#webauthnprf) | Configuration for evaluating a WebAuthn credential-bound PRF. | | [`WebAuthn.PrfExtension`](/api/WebAuthn/types#webauthnprfextension) | Inputs for the WebAuthn PRF extension. | | [`WebAuthn.PublicKeyCredential`](/api/WebAuthn/types#webauthnpublickeycredential) | | | [`WebAuthn.PublicKeyCredentialType`](/api/WebAuthn/types#webauthnpublickeycredentialtype) | | | [`WebAuthn.ResidentKeyRequirement`](/api/WebAuthn/types#webauthnresidentkeyrequirement) | | | [`WebAuthn.SignMetadata`](/api/WebAuthn/types#webauthnsignmetadata) | Metadata for a WebAuthn P256 signature. | | [`WebAuthn.UserVerificationRequirement`](/api/WebAuthn/types#webauthnuserverificationrequirement) | | # WebAuthn.createCredential Creates a WebAuthn credential and optionally returns its credential-bound PRF output. ## Imports :::code-group ```ts [Named] import { WebAuthn } from 'ox' ``` ```ts [Entrypoint] import * as WebAuthn from 'ox/WebAuthn' ``` ::: ## Examples ```ts twoslash import { Secp256k1, WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example', prf: true }) const privateKey = Secp256k1.fromPrf(credential.prf) ``` `prf: true` uses the stable input `ox.webauthn.prf.v1`. Pass `{ input }` to use an application-owned input instead. When credential creation enables PRF but does not return an output, this function performs a follow-up assertion. The user may be prompted twice. If PRF evaluation fails after registration, the thrown error retains the created credential on its `credential` property. :::warning PRF output is secret application-held key material. Native `credential.raw.toJSON()` output includes extension results, so do not serialize or send the raw credential. PRF-derived keys are software keys. Code on any origin allowed to use the same RP ID can request the same output after user verification. ::: ## Definition ```ts function createCredential( options: options, ): Promise> ``` **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L66) ## Parameters ### options * **Type:** `options` Credential creation and PRF options. ## Return Type The credential, with a copied PRF output when requested. `Promise>` # WebAuthn.getCredential Requests a WebAuthn credential and returns its credential-bound PRF output. ## Imports :::code-group ```ts [Named] import { WebAuthn } from 'ox' ``` ```ts [Entrypoint] import * as WebAuthn from 'ox/WebAuthn' ``` ::: ## Examples ```ts twoslash import { Secp256k1, WebAuthn } from 'ox' const { prf } = await WebAuthn.getCredential({ credentialId: 'oZ48...', prf: true }) const privateKey = Secp256k1.fromPrf(prf) ``` `prf: true` uses the stable input `ox.webauthn.prf.v1`. Pass `{ input }` to use an application-owned input instead. When more than one credential can be selected, check `result.id` before using the PRF output. :::warning PRF output is secret application-held key material. Native `response.raw.toJSON()` output includes extension results, so do not serialize or send the raw response. ::: ## Definition ```ts function getCredential( options: getCredential.Options, ): Promise ``` **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L262) ## Parameters ### options * **Type:** `getCredential.Options` Credential request and PRF options. #### options.challenge * **Type:** `0x${string}` * **Optional** Challenge to sign. Defaults to a random 32-byte value. Supply and verify a server-generated challenge when the assertion is also used to authenticate a user to a server. #### options.extensions * **Type:** `Omit` * **Optional** Additional WebAuthn extensions. The `prf` extension is managed by this function. #### options.getFn * **Type:** `(options?: CredentialRequestOptions` * **Optional** Function that requests the WebAuthn credential. #### options.prf * **Type:** `Prf` Credential-bound PRF configuration. #### options.publicKey * **Type:** `never` * **Optional** Raw credential options cannot be combined with managed PRF evaluation. ## Return Type The requested credential and a copied 32-byte PRF output. `Promise` # WebAuthn.sign Signs a challenge using a stored WebAuthn P256 Credential. If no Credential is provided, a prompt will be displayed for the user to select an existing Credential that was previously registered. ## Imports :::code-group ```ts [Named] import { WebAuthn } from 'ox' ``` ```ts [Entrypoint] import * as WebAuthn from 'ox/WebAuthn' ``` ::: ## Examples ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const { metadata, signature } = await WebAuthn.sign({ // [!code focus] credentialId: credential.id, // [!code focus] challenge: '0xdeadbeef' // [!code focus] }) // [!code focus] // @log: { // @log: metadata: { // @log: authenticatorData: '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000', // @log: clientDataJSON: '{"type":"webauthn.get","challenge":"9jEFijuhEWrM4SOW-tChJbUEHEP44VcjcJ-Bqo1fTM8","origin":"http://localhost:5173","crossOrigin":false}', // @log: challengeIndex: 23, // @log: typeIndex: 1, // @log: userVerificationRequired: true, // @log: }, // @log: signature: { r: 51231...4215n, s: 12345...6789n }, // @log: } ``` ## Definition ```ts function sign( options: sign.Options, ): Promise ``` **Source:** [src/core/WebAuthnP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthnP256.ts#L317) ## Parameters ### options * **Type:** `sign.Options` Options. ## Return Type The signature. `Promise` # WebAuthn.verify Verifies a signature using the Credential's public key and the challenge which was signed. ## Imports :::code-group ```ts [Named] import { WebAuthn } from 'ox' ``` ```ts [Entrypoint] import * as WebAuthn from 'ox/WebAuthn' ``` ::: ## Examples ```ts twoslash import { WebAuthn } from 'ox' const credential = await WebAuthn.createCredential({ name: 'Example' }) const { metadata, signature } = await WebAuthn.sign({ credentialId: credential.id, challenge: '0xdeadbeef' }) const result = await WebAuthn.verify({ // [!code focus] metadata, // [!code focus] challenge: '0xdeadbeef', // [!code focus] publicKey: credential.publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/WebAuthnP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthnP256.ts#L358) ## Parameters ### options * **Type:** `verify.Options` Options. ## Return Type Whether the signature is valid. `boolean` # WebAuthn Errors ## `WebAuthn.GetCredentialFailedError` Thrown when a WebAuthn credential request fails. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L571) ## `WebAuthn.InvalidExtensionError` Thrown when a caller supplies the managed `prf` extension. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L466) ## `WebAuthn.InvalidOptionsError` Thrown when raw credential options are combined with managed PRF evaluation. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L475) ## `WebAuthn.InvalidPrfOutputError` Thrown when a WebAuthn PRF result is not a valid 32-byte output. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L486) ## `WebAuthn.PrfEvaluationFailedError` Thrown when WebAuthn PRF evaluation fails after a credential ceremony. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L539) ## `WebAuthn.PrfNotSupportedError` Thrown when a created credential does not support PRF evaluation. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L513) ## `WebAuthn.PrfUnavailableError` Thrown when a credential assertion does not return a PRF output. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L526) # WebAuthn Types ## `WebAuthn.AttestationConveyancePreference` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L3) ## `WebAuthn.AuthenticatorAttachment` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L9) ## `WebAuthn.AuthenticatorTransport` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L11) ## `WebAuthn.BufferSource` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L39) ## `WebAuthn.COSEAlgorithmIdentifier` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L18) ## `WebAuthn.CredentialMediationRequirement` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L20) ## `WebAuthn.LargeBlobSupport` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L35) ## `WebAuthn.P256Credential` A WebAuthn-flavored P256 credential. **Source:** [src/core/WebAuthnP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthnP256.ts#L7) ## `WebAuthn.Prf` Configuration for evaluating a WebAuthn credential-bound PRF. **Source:** [src/core/WebAuthn.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthn.ts#L16) ## `WebAuthn.PrfExtension` Inputs for the WebAuthn PRF extension. **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L50) ## `WebAuthn.PublicKeyCredential` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L126) ## `WebAuthn.PublicKeyCredentialType` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L26) ## `WebAuthn.ResidentKeyRequirement` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L28) ## `WebAuthn.SignMetadata` Metadata for a WebAuthn P256 signature. **Source:** [src/core/WebAuthnP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebAuthnP256.ts#L10) ## `WebAuthn.UserVerificationRequirement` **Source:** [src/webauthn/Types.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Types.ts#L30) # WebCryptoP256 Utility functions for [NIST P256](https://csrc.nist.gov/csrc/media/events/workshop-on-elliptic-curve-cryptography-standards/documents/papers/session6-adalier-mehmet.pdf) ECDSA cryptography using the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) ## Examples Below are some examples demonstrating common usages of the `WebCryptoP256` module: * [Creating Key Pairs](#creating-key-pairs) * [Signing Payloads](#signing-payloads) * [Verifying Signatures](#verifying-signatures) ### Creating Key Pairs Key pairs can be created using [`WebCryptoP256.createKeyPair`](/api/WebCryptoP256/createKeyPair): ```ts twoslash import { WebCryptoP256 } from 'ox' const { publicKey, privateKey } = await WebCryptoP256.createKeyPair() // @log: { // @log: privateKey: CryptoKey {}, // @log: publicKey: { // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: prefix: 4, // @log: }, // @log: } ``` ### Signing Payloads Payloads can be signed using [`WebCryptoP256.sign`](/api/WebCryptoP256/sign): ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey // [!code focus] }) // [!code focus] // @log: { // @log: r: 151231...4423n, // @log: s: 516123...5512n, // @log: } ``` ### Verifying Signatures Signatures can be verified using [`WebCryptoP256.verify`](/api/WebCryptoP256/verify): ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey, publicKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ payload: '0xdeadbeef', privateKey }) const verified = await WebCryptoP256.verify({ // [!code focus] payload: '0xdeadbeef', // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`WebCryptoP256.createKeyPair`](/api/WebCryptoP256/createKeyPair) | Generates an ECDSA P256 key pair that includes: | | [`WebCryptoP256.createKeyPairECDH`](/api/WebCryptoP256/createKeyPairECDH) | Generates an ECDH P256 key pair for key agreement that includes: | | [`WebCryptoP256.getSharedSecret`](/api/WebCryptoP256/getSharedSecret) | Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key using Web Crypto APIs. | | [`WebCryptoP256.sign`](/api/WebCryptoP256/sign) | Signs a payload with the provided `CryptoKey` private key and returns a P256 signature. | | [`WebCryptoP256.verify`](/api/WebCryptoP256/verify) | Verifies a payload was signed by the provided public key. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`WebCryptoP256.InvalidPrivateKeyAlgorithmError`](/api/WebCryptoP256/errors#webcryptop256invalidprivatekeyalgorithmerror) | Thrown when an ECDSA private key is supplied to [`WebCryptoP256.getSharedSecret`](/api/WebCryptoP256/getSharedSecret). Only ECDH private keys are valid for shared secret derivation. | # WebCryptoP256.createKeyPair Generates an ECDSA P256 key pair that includes: * a `privateKey` of type [`CryptoKey`](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey) * a `publicKey` of type [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) ## Imports :::code-group ```ts [Named] import { WebCryptoP256 } from 'ox' ``` ```ts [Entrypoint] import * as WebCryptoP256 from 'ox/WebCryptoP256' ``` ::: ## Examples ```ts twoslash import { WebCryptoP256 } from 'ox' const { publicKey, privateKey } = await WebCryptoP256.createKeyPair() // @log: { // @log: privateKey: CryptoKey {}, // @log: publicKey: { // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: prefix: 4, // @log: }, // @log: } ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): Promise ``` **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L43) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** Options for creating the key pair. #### options.extractable * **Type:** `boolean` * **Optional** A boolean value indicating whether it will be possible to export the private key using `globalThis.crypto.subtle.exportKey()`. ## Return Type The key pair. `Promise` # WebCryptoP256.createKeyPairECDH Generates an ECDH P256 key pair for key agreement that includes: * a `privateKey` of type [`CryptoKey`](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKey) - a `publicKey` of type [`PublicKey.PublicKey`](/api/PublicKey/types#publickey) ## Imports :::code-group ```ts [Named] import { WebCryptoP256 } from 'ox' ``` ```ts [Entrypoint] import * as WebCryptoP256 from 'ox/WebCryptoP256' ``` ::: ## Examples ```ts twoslash import { WebCryptoP256 } from 'ox' const { publicKey, privateKey } = await WebCryptoP256.createKeyPairECDH() // @log: { // @log: privateKey: CryptoKey {}, // @log: publicKey: { // @log: x: '0x8318535b54105d4a7aae60c08fc45f9687181b4fdfc625bd1a753fa7397fed75', // @log: y: '0x3547f11ca8696646f2f3acb08e31016afac23e630c5d11f59f61fef57b0d2aa5', // @log: prefix: 4, // @log: }, // @log: } ``` ## Definition ```ts function createKeyPairECDH( options?: createKeyPairECDH.Options, ): Promise ``` **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L105) ## Parameters ### options * **Type:** `createKeyPairECDH.Options` * **Optional** Options for creating the key pair. #### options.extractable * **Type:** `boolean` * **Optional** A boolean value indicating whether it will be possible to export the private key using `globalThis.crypto.subtle.exportKey()`. ## Return Type The key pair. `Promise` # WebCryptoP256.getSharedSecret Computes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key using Web Crypto APIs. ## Imports :::code-group ```ts [Named] import { WebCryptoP256 } from 'ox' ``` ```ts [Entrypoint] import * as WebCryptoP256 from 'ox/WebCryptoP256' ``` ::: ## Examples ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey: privateKeyA } = await WebCryptoP256.createKeyPairECDH() const { publicKey: publicKeyB } = await WebCryptoP256.createKeyPairECDH() const sharedSecret = await WebCryptoP256.getSharedSecret({ privateKey: privateKeyA, publicKey: publicKeyB }) ``` ## Definition ```ts function getSharedSecret( options: getSharedSecret.Options, ): Promise> ``` **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L163) ## Parameters ### options * **Type:** `getSharedSecret.Options` The options to compute the shared secret. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned shared secret. #### options.privateKey * **Type:** `CryptoKey` Private key to use for the shared secret computation (must be a CryptoKey for ECDH). #### options.publicKey * **Type:** `{ prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key to use for the shared secret computation. ## Return Type The computed shared secret. `Promise>` # WebCryptoP256.sign Signs a payload with the provided `CryptoKey` private key and returns a P256 signature. ## Imports :::code-group ```ts [Named] import { WebCryptoP256 } from 'ox' ``` ```ts [Entrypoint] import * as WebCryptoP256 from 'ox/WebCryptoP256' ``` ::: ## Examples ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey // [!code focus] }) // [!code focus] // @log: { // @log: r: 151231...4423n, // @log: s: 516123...5512n, // @log: } ``` ## Definition ```ts function sign( options: sign.Options, ): Promise> ``` **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L249) ## Parameters ### options * **Type:** `sign.Options` Options for signing the payload. #### options.as * **Type:** `"Object" | "Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `CryptoKey` ECDSA private key. ## Return Type The P256 ECDSA [`Signature.Signature`](/api/Signature/types#signature) (always low-S normalized). `Promise>` # WebCryptoP256.verify Verifies a payload was signed by the provided public key. ## Imports :::code-group ```ts [Named] import { WebCryptoP256 } from 'ox' ``` ```ts [Entrypoint] import * as WebCryptoP256 from 'ox/WebCryptoP256' ``` ::: ## Examples ```ts twoslash import { WebCryptoP256 } from 'ox' const { privateKey, publicKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ payload: '0xdeadbeef', privateKey }) const verified = await WebCryptoP256.verify({ // [!code focus] payload: '0xdeadbeef', // [!code focus] publicKey, // [!code focus] signature // [!code focus] }) // [!code focus] // @log: true ``` ## Definition ```ts function verify( options: verify.Options, ): Promise ``` **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L322) ## Parameters ### options * **Type:** `verify.Options` The verification options. #### options.lowS * **Type:** `boolean` * **Optional** If set to `true`, only low-S signatures will be accepted. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array | { prefix: number; x: 0x${string}; y: 0x${string}; } | { prefix: number; x: 0x${string}; y?: undefined; }` Public key that signed the payload. Accepts a structured [`PublicKey.PublicKey`](/api/PublicKey/types#publickey), a serialized hex string, or a `Uint8Array` (SEC1 encoding). #### options.signature * **Type:** `0x${string} | Uint8Array | { r: 0x${string}; s: 0x${string}; yParity?: number; }` Signature of the payload. Accepts a structured [`Signature.Signature`](/api/Signature/types#signature), a serialized hex string, or a `Uint8Array`. ## Return Type Whether the payload was signed by the provided public key. `Promise` # WebCryptoP256 Errors ## `WebCryptoP256.InvalidPrivateKeyAlgorithmError` Thrown when an ECDSA private key is supplied to [`WebCryptoP256.getSharedSecret`](/api/WebCryptoP256/getSharedSecret). Only ECDH private keys are valid for shared secret derivation. **Source:** [src/core/WebCryptoP256.ts](https://github.com/wevm/ox/blob/main/src/core/WebCryptoP256.ts#L380) # X25519 Utilities for working with X25519 elliptic curve Diffie-Hellman key agreement. X25519 is a high-performance elliptic curve that can be used to perform Diffie-Hellman key agreement to derive shared secrets between parties. It is designed for use with the elliptic curve Diffie-Hellman (ECDH) key agreement scheme. ## Examples Below are some examples demonstrating common usages of the `X25519` module: * [Creating Key Pairs](#creating-key-pairs) * [Deriving Shared Secrets](#deriving-shared-secrets) ### Creating Key Pairs ```ts twoslash import { X25519 } from 'ox' const { privateKey, publicKey } = X25519.createKeyPair() ``` ### Deriving Shared Secrets ```ts twoslash import { X25519 } from 'ox' const { privateKey: privateKeyA } = X25519.createKeyPair() const { publicKey: publicKeyB } = X25519.createKeyPair() const sharedSecret = X25519.getSharedSecret({ privateKey: privateKeyA, publicKey: publicKeyB }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`X25519.createKeyPair`](/api/X25519/createKeyPair) | Creates a new X25519 key pair consisting of a private key and its corresponding public key. | | [`X25519.getPublicKey`](/api/X25519/getPublicKey) | Computes the X25519 public key from a provided private key. | | [`X25519.getSharedSecret`](/api/X25519/getSharedSecret) | Computes a shared secret using X25519 elliptic curve Diffie-Hellman between a private key and a public key. | | [`X25519.randomPrivateKey`](/api/X25519/randomPrivateKey) | Generates a random X25519 private key. | # X25519.createKeyPair Creates a new X25519 key pair consisting of a private key and its corresponding public key. ## Imports :::code-group ```ts [Named] import { X25519 } from 'ox' ``` ```ts [Entrypoint] import * as X25519 from 'ox/X25519' ``` ::: ## Examples ```ts twoslash import { X25519 } from 'ox' const { privateKey, publicKey } = X25519.createKeyPair() ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/X25519.ts](https://github.com/wevm/ox/blob/main/src/core/X25519.ts#L23) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private and public keys. ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # X25519.getPublicKey Computes the X25519 public key from a provided private key. ## Imports :::code-group ```ts [Named] import { X25519 } from 'ox' ``` ```ts [Entrypoint] import * as X25519 from 'ox/X25519' ``` ::: ## Examples ```ts twoslash import { X25519 } from 'ox' const publicKey = X25519.getPublicKey({ privateKey: '0x...' }) ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/X25519.ts](https://github.com/wevm/ox/blob/main/src/core/X25519.ts#L76) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to compute the public key from. ## Return Type The computed public key. `getPublicKey.ReturnType` # X25519.getSharedSecret Computes a shared secret using X25519 elliptic curve Diffie-Hellman between a private key and a public key. ## Imports :::code-group ```ts [Named] import { X25519 } from 'ox' ``` ```ts [Entrypoint] import * as X25519 from 'ox/X25519' ``` ::: ## Examples ```ts twoslash import { X25519 } from 'ox' const { privateKey: privateKeyA } = X25519.createKeyPair() const { publicKey: publicKeyB } = X25519.createKeyPair() const sharedSecret = X25519.getSharedSecret({ privateKey: privateKeyA, publicKey: publicKeyB }) ``` ## Definition ```ts function getSharedSecret( options: getSharedSecret.Options, ): getSharedSecret.ReturnType ``` **Source:** [src/core/X25519.ts](https://github.com/wevm/ox/blob/main/src/core/X25519.ts#L128) ## Parameters ### options * **Type:** `getSharedSecret.Options` The options to compute the shared secret. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned shared secret. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key to use for the shared secret computation. #### options.publicKey * **Type:** `0x${string} | Uint8Array` Public key to use for the shared secret computation. ## Return Type The computed shared secret. `getSharedSecret.ReturnType` # X25519.randomPrivateKey Generates a random X25519 private key. ## Imports :::code-group ```ts [Named] import { X25519 } from 'ox' ``` ```ts [Entrypoint] import * as X25519 from 'ox/X25519' ``` ::: ## Examples ```ts twoslash import { X25519 } from 'ox' const privateKey = X25519.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/X25519.ts](https://github.com/wevm/ox/blob/main/src/core/X25519.ts#L182) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # Base32 Utility functions for working with Base32 values using the [BIP-173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) bech32 alphabet. ## Examples Below are some examples demonstrating common usages of the `Base32` module: * [Encoding to Base32](#encoding-to-base32) * [Decoding Base32](#decoding-base32) ### Encoding to Base32 ```ts twoslash import { Base32 } from 'ox' const value = Base32.fromHex('0x00ff00') ``` ### Decoding Base32 ```ts twoslash import { Base32 } from 'ox' const value = Base32.toBytes('qrlsq') ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Base32.fromBytes`](/api/Base32/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to a Base32-encoded string (using the BIP-173 bech32 alphabet). | | [`Base32.fromHex`](/api/Base32/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) value to a Base32-encoded string (using the BIP-173 bech32 alphabet). | | [`Base32.toBytes`](/api/Base32/toBytes) | Decodes a Base32-encoded string (using the BIP-173 bech32 alphabet) to [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Base32.toHex`](/api/Base32/toHex) | Decodes a Base32-encoded string (using the BIP-173 bech32 alphabet) to [`Hex.Hex`](/api/Hex/types#hex). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Base32.InvalidCharacterError`](/api/Base32/errors#base32invalidcharactererror) | Thrown when a Base32 string contains an invalid character. | | [`Base32.InvalidPaddingError`](/api/Base32/errors#base32invalidpaddingerror) | Thrown when a Base32 string contains non-canonical (non-zero) trailing bits. | # Base32.fromBytes Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to a Base32-encoded string (using the BIP-173 bech32 alphabet). ## Imports :::code-group ```ts [Named] import { Base32 } from 'ox' ``` ```ts [Entrypoint] import * as Base32 from 'ox/Base32' ``` ::: ## Examples ```ts twoslash import { Base32, Bytes } from 'ox' const value = Base32.fromBytes( new Uint8Array([0x00, 0xff, 0x00]) ) ``` ## Definition ```ts function fromBytes( value: Bytes.Bytes, ): string ``` **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L25) ## Parameters ### value * **Type:** `Bytes.Bytes` The byte array to encode. ## Return Type The Base32 encoded string. `string` # Base32.fromHex Encodes a [`Hex.Hex`](/api/Hex/types#hex) value to a Base32-encoded string (using the BIP-173 bech32 alphabet). ## Imports :::code-group ```ts [Named] import { Base32 } from 'ox' ``` ```ts [Entrypoint] import * as Base32 from 'ox/Base32' ``` ::: ## Examples ```ts twoslash import { Base32 } from 'ox' const value = Base32.fromHex('0x00ff00') ``` ## Definition ```ts function fromHex( value: Hex.Hex, ): string ``` **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L50) ## Parameters ### value * **Type:** `Hex.Hex` The hex value to encode. ## Return Type The Base32 encoded string. `string` # Base32.toBytes Decodes a Base32-encoded string (using the BIP-173 bech32 alphabet) to [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Base32 } from 'ox' ``` ```ts [Entrypoint] import * as Base32 from 'ox/Base32' ``` ::: ## Examples ```ts twoslash import { Base32 } from 'ox' const value = Base32.toBytes('qqsa0') ``` ## Definition ```ts function toBytes( value: string, ): Bytes.Bytes ``` **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L71) ## Parameters ### value * **Type:** `string` The Base32 encoded string. ## Return Type The decoded byte array. `Bytes.Bytes` # Base32.toHex Decodes a Base32-encoded string (using the BIP-173 bech32 alphabet) to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Base32 } from 'ox' ``` ```ts [Entrypoint] import * as Base32 from 'ox/Base32' ``` ::: ## Examples ```ts twoslash import { Base32 } from 'ox' const value = Base32.toHex('qqsa0') ``` ## Definition ```ts function toHex( value: string, ): Hex.Hex ``` **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L114) ## Parameters ### value * **Type:** `string` The Base32 encoded string. ## Return Type The decoded hex string. `Hex.Hex` # Base32 Errors ## `Base32.InvalidCharacterError` Thrown when a Base32 string contains an invalid character. **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L123) ## `Base32.InvalidPaddingError` Thrown when a Base32 string contains non-canonical (non-zero) trailing bits. **Source:** [src/core/Base32.ts](https://github.com/wevm/ox/blob/main/src/core/Base32.ts#L132) # Base58 Utility functions for working with [Base58](https://digitalbazaar.github.io/base58-spec/) values. ## Examples Below are some examples demonstrating common usages of the `Base58` module: * [Encoding to Base58](#encoding-to-base58) * [Decoding Base58](#decoding-base58) ### Encoding to Base58 Values can be encoded to Base58 with: * [`Base58.fromString`](/api/Base58/fromString), or * [`Base58.fromBytes`](/api/Base58/fromBytes), or * [`Base58.fromHex`](/api/Base58/fromHex) ```ts twoslash import { Base58 } from 'ox' const value_string = Base58.fromString('Hello World!') // @log: '2NEpo7TZRRrLZSi2U' const value_bytes = Base58.fromBytes( new Uint8Array([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: '2NEpo7TZRRrLZSi2U' const value_hex = Base58.fromHex( '0x48656c6c6f20576f726c6421' ) // @log: '2NEpo7TZRRrLZSi2U' ``` ### Decoding Base58 Values can be decoded from Base58 with: * [`Base58.toString`](/api/Base58/toString), or * [`Base58.toBytes`](/api/Base58/toBytes), or * [`Base58.toHex`](/api/Base58/toHex) ```ts twoslash import { Base58 } from 'ox' const value_string = Base58.toString('2NEpo7TZRRrLZSi2U') // @log: 'Hello World!' const value_bytes = Base58.toBytes('2NEpo7TZRRrLZSi2U') // @log: Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33] const value_hex = Base58.toHex('2NEpo7TZRRrLZSi2U') // @log: '0x48656c6c6f20576f726c6421' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Base58.fromBytes`](/api/Base58/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) to a Base58-encoded string. | | [`Base58.fromHex`](/api/Base58/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) to a Base58-encoded string. | | [`Base58.fromString`](/api/Base58/fromString) | Encodes a string to a Base58-encoded string. | | [`Base58.toBytes`](/api/Base58/toBytes) | Decodes a Base58-encoded string to a [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Base58.toHex`](/api/Base58/toHex) | Decodes a Base58-encoded string to [`Hex.Hex`](/api/Hex/types#hex). | | [`Base58.toString`](/api/Base58/toString) | Decodes a Base58-encoded string to a string. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Base58.InvalidCharacterError`](/api/Base58/errors#base58invalidcharactererror) | Thrown when a Base58 string contains an invalid character. | # Base58.fromBytes Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) to a Base58-encoded string. ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58, Bytes } from 'ox' const value = Base58.fromBytes( Bytes.fromString('Hello World!') ) // @log: '2NEpo7TZRRrLZSi2U' ``` ## Definition ```ts function fromBytes( value: Bytes.Bytes, ): string ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L22) ## Parameters ### value * **Type:** `Bytes.Bytes` The byte array to encode. ## Return Type The Base58 encoded string. `string` # Base58.fromHex Encodes a [`Hex.Hex`](/api/Hex/types#hex) to a Base58-encoded string. ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58, Hex } from 'ox' const value = Base58.fromHex(Hex.fromString('Hello World!')) // @log: '2NEpo7TZRRrLZSi2U' ``` ## Definition ```ts function fromHex( value: Hex.Hex, ): string ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L44) ## Parameters ### value * **Type:** `Hex.Hex` The byte array to encode. ## Return Type The Base58 encoded string. `string` # Base58.fromString Encodes a string to a Base58-encoded string. ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58 } from 'ox' const value = Base58.fromString('Hello World!') // @log: '2NEpo7TZRRrLZSi2U' ``` ## Definition ```ts function fromString( value: string, ): string ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L66) ## Parameters ### value * **Type:** `string` The string to encode. ## Return Type The Base58 encoded string. `string` # Base58.toBytes Decodes a Base58-encoded string to a [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58 } from 'ox' const value = Base58.toBytes('2NEpo7TZRRrLZSi2U') // @log: Uint8Array [ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ] ``` ## Definition ```ts function toBytes( value: string, ): Bytes.Bytes ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L88) ## Parameters ### value * **Type:** `string` The Base58 encoded string. ## Return Type The decoded byte array. `Bytes.Bytes` # Base58.toHex Decodes a Base58-encoded string to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58 } from 'ox' const value = Base58.toHex('2NEpo7TZRRrLZSi2U') // @log: '0x48656c6c6f20576f726c6421' ``` ## Definition ```ts function toHex( value: string, ): Hex.Hex ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L110) ## Parameters ### value * **Type:** `string` The Base58 encoded string. ## Return Type The decoded hex string. `Hex.Hex` # Base58.toString Decodes a Base58-encoded string to a string. ## Imports :::code-group ```ts [Named] import { Base58 } from 'ox' ``` ```ts [Entrypoint] import * as Base58 from 'ox/Base58' ``` ::: ## Examples ```ts twoslash import { Base58 } from 'ox' const value = Base58.toString('2NEpo7TZRRrLZSi2U') // @log: 'Hello World!' ``` ## Definition ```ts function toString( value: string, ): string ``` **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L155) ## Parameters ### value * **Type:** `string` The Base58 encoded string. ## Return Type The decoded string. `string` # Base58 Errors ## `Base58.InvalidCharacterError` Thrown when a Base58 string contains an invalid character. **Source:** [src/core/Base58.ts](https://github.com/wevm/ox/blob/main/src/core/Base58.ts#L164) # Base64 Utility functions for working with [RFC-4648](https://datatracker.ietf.org/doc/html/rfc4648) Base64. ## Examples ### Encoding to Base64 Values can be encoded to Base64 with: * [`Base64.fromString`](/api/Base64/fromString), or * [`Base64.fromBytes`](/api/Base64/fromBytes), or * [`Base64.fromHex`](/api/Base64/fromHex) ```ts twoslash import { Base64 } from 'ox' const value_string = Base64.fromString('Hello World!') // @log: 'SGVsbG8gV29ybGQh==' const value_bytes = Base64.fromBytes( new Uint8Array([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: 'SGVsbG8gV29ybGQh==' const value_hex = Base64.fromHex( '0x48656c6c6f20576f726c6421' ) // @log: 'SGVsbG8gV29ybGQh==' ``` ### Decoding Base64 Values can be decoded from Base64 with: * [`Base64.toString`](/api/Base64/toString), or * [`Base64.toBytes`](/api/Base64/toBytes), or * [`Base64.toHex`](/api/Base64/toHex) ```ts twoslash import { Base64 } from 'ox' const value_string = Base64.toString('SGVsbG8gV29ybGQh==') // @log: 'Hello World!' const value_bytes = Base64.toBytes('SGVsbG8gV29ybGQh==') // @log: Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33] const value_hex = Base64.toHex('SGVsbG8gV29ybGQh==') // @log: '0x48656c6c6f20576f726c6421' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Base64.fromBytes`](/api/Base64/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) to a Base64-encoded string (with optional padding and/or URL-safe characters). | | [`Base64.fromHex`](/api/Base64/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) to a Base64-encoded string (with optional padding and/or URL-safe characters). | | [`Base64.fromString`](/api/Base64/fromString) | Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters). | | [`Base64.toBytes`](/api/Base64/toBytes) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Base64.toHex`](/api/Base64/toHex) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Hex.Hex`](/api/Hex/types#hex). | | [`Base64.toString`](/api/Base64/toString) | Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Base64.InvalidCharacterError`](/api/Base64/types#base64invalidcharactererror) | | | [`Base64.InvalidLengthError`](/api/Base64/types#base64invalidlengtherror) | | | [`Base64.InvalidPaddingError`](/api/Base64/types#base64invalidpaddingerror) | | # Base64.fromBytes Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) to a Base64-encoded string (with optional padding and/or URL-safe characters). ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64, Bytes } from 'ox' const value = Base64.fromBytes( Bytes.fromString('hello world') ) // @log: 'aGVsbG8gd29ybGQ=' ``` ### No Padding Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: ```ts twoslash import { Base64, Bytes } from 'ox' const value = Base64.fromBytes( Bytes.fromString('hello world'), { pad: false } ) // @log: 'aGVsbG8gd29ybGQ' ``` ### URL-safe Encoding Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: ```ts twoslash import { Base64, Bytes } from 'ox' const value = Base64.fromBytes( Bytes.fromString('hello wod'), { url: true } ) // @log: 'aGVsbG8gd29_77-9ZA==' ``` ## Definition ```ts function fromBytes( value: Bytes.Bytes, options?: fromBytes.Options, ): string ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L52) ## Parameters ### value * **Type:** `Bytes.Bytes` The byte array to encode. ### options * **Type:** `fromBytes.Options` * **Optional** Encoding options. #### options.pad * **Type:** `boolean` * **Optional** Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. #### options.url * **Type:** `boolean` * **Optional** Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). ## Return Type The Base64 encoded string. `string` # Base64.fromHex Encodes a [`Hex.Hex`](/api/Hex/types#hex) to a Base64-encoded string (with optional padding and/or URL-safe characters). ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64, Hex } from 'ox' const value = Base64.fromHex(Hex.fromString('hello world')) // @log: 'aGVsbG8gd29ybGQ=' ``` ### No Padding Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: ```ts twoslash import { Base64, Hex } from 'ox' const value = Base64.fromHex( Hex.fromString('hello world'), { pad: false } ) // @log: 'aGVsbG8gd29ybGQ' ``` ### URL-safe Encoding Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: ```ts twoslash import { Base64, Hex } from 'ox' const value = Base64.fromHex(Hex.fromString('hello wod'), { url: true }) // @log: 'aGVsbG8gd29_77-9ZA==' ``` ## Definition ```ts function fromHex( value: Hex.Hex, options?: fromHex.Options, ): string ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L118) ## Parameters ### value * **Type:** `Hex.Hex` The hex value to encode. ### options * **Type:** `fromHex.Options` * **Optional** Encoding options. #### options.pad * **Type:** `boolean` * **Optional** Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. #### options.url * **Type:** `boolean` * **Optional** Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). ## Return Type The Base64 encoded string. `string` # Base64.fromString Encodes a string to a Base64-encoded string (with optional padding and/or URL-safe characters). ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64 } from 'ox' const value = Base64.fromString('hello world') // @log: 'aGVsbG8gd29ybGQ=' ``` ### No Padding Turn off [padding of encoded data](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) with the `pad` option: ```ts twoslash import { Base64 } from 'ox' const value = Base64.fromString('hello world', { pad: false }) // @log: 'aGVsbG8gd29ybGQ' ``` ### URL-safe Encoding Turn on [URL-safe encoding](https://datatracker.ietf.org/doc/html/rfc4648#section-5) (Base64 URL) with the `url` option: ```ts twoslash import { Base64 } from 'ox' const value = Base64.fromString('hello wod', { url: true }) // @log: 'aGVsbG8gd29_77-9ZA==' ``` ## Definition ```ts function fromString( value: string, options?: fromString.Options, ): string ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L181) ## Parameters ### value * **Type:** `string` The string to encode. ### options * **Type:** `fromString.Options` * **Optional** Encoding options. #### options.pad * **Type:** `boolean` * **Optional** Whether to [pad](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) the Base64 encoded string. #### options.url * **Type:** `boolean` * **Optional** Whether to Base64 encode with [URL safe characters](https://datatracker.ietf.org/doc/html/rfc4648#section-5). ## Return Type The Base64 encoded string. `string` # Base64.toBytes Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64, Bytes } from 'ox' const value = Base64.toBytes('aGVsbG8gd29ybGQ=') // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) ``` ## Definition ```ts function toBytes( value: string, ): Bytes.Bytes ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L218) ## Parameters ### value * **Type:** `string` The string, hex value, or byte array to encode. ## Return Type The Base64 decoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Bytes.Bytes` # Base64.toHex Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64, Hex } from 'ox' const value = Base64.toHex('aGVsbG8gd29ybGQ=') // @log: 0x68656c6c6f20776f726c64 ``` ## Definition ```ts function toHex( value: string, ): Hex.Hex ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L244) ## Parameters ### value * **Type:** `string` The string, hex value, or byte array to encode. ## Return Type The Base64 decoded [`Hex.Hex`](/api/Hex/types#hex). `Hex.Hex` # Base64.toString Decodes a Base64-encoded string (with optional padding and/or URL-safe characters) to a string. ## Imports :::code-group ```ts [Named] import { Base64 } from 'ox' ``` ```ts [Entrypoint] import * as Base64 from 'ox/Base64' ``` ::: ## Examples ```ts twoslash import { Base64 } from 'ox' const value = Base64.toString('aGVsbG8gd29ybGQ=') // @log: 'hello world' ``` ## Definition ```ts function toString( value: string, ): string ``` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L266) ## Parameters ### value * **Type:** `string` The string, hex value, or byte array to encode. ## Return Type The Base64 decoded string. `string` # Base64 Types ## `Base64.InvalidCharacterError` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L276) ## `Base64.InvalidLengthError` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L282) ## `Base64.InvalidPaddingError` **Source:** [src/core/Base64.ts](https://github.com/wevm/ox/blob/main/src/core/Base64.ts#L288) # Bech32m Utility functions for [BIP-350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki) bech32m encoding and decoding. ## Examples Below are some examples demonstrating common usages of the `Bech32m` module: * [Encoding](#encoding) * [Decoding](#decoding) ### Encoding ```ts twoslash import { Bech32m } from 'ox' const encoded = Bech32m.encode('tempo', new Uint8Array(20)) ``` ### Decoding ```ts twoslash import { Bech32m } from 'ox' const { hrp, data } = Bech32m.decode( 'tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq7w9gdx' ) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Bech32m.decode`](/api/Bech32m/decode) | Decodes a bech32m string (BIP-350) into a human-readable part and data bytes. | | [`Bech32m.encode`](/api/Bech32m/encode) | Encodes data bytes with a human-readable part (HRP) into a bech32m string (BIP-350). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Bech32m.ExceedsLengthError`](/api/Bech32m/errors#bech32mexceedslengtherror) | Thrown when the encoded string exceeds the length limit. | | [`Bech32m.InvalidCharacterError`](/api/Bech32m/errors#bech32minvalidcharactererror) | Thrown when a bech32m string contains an invalid character. | | [`Bech32m.InvalidChecksumError`](/api/Bech32m/errors#bech32minvalidchecksumerror) | Thrown when a bech32m string has an invalid checksum. | | [`Bech32m.InvalidHrpError`](/api/Bech32m/errors#bech32minvalidhrperror) | Thrown when the HRP is invalid (empty or contains non-ASCII characters). | | [`Bech32m.MixedCaseError`](/api/Bech32m/errors#bech32mmixedcaseerror) | Thrown when a bech32m string contains mixed case. | | [`Bech32m.NoSeparatorError`](/api/Bech32m/errors#bech32mnoseparatorerror) | Thrown when a bech32m string has no separator. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Bech32m.InvalidPaddingError`](/api/Bech32m/types#bech32minvalidpaddingerror) | | # Bech32m.decode Decodes a bech32m string (BIP-350) into a human-readable part and data bytes. ## Imports :::code-group ```ts [Named] import { Bech32m } from 'ox' ``` ```ts [Entrypoint] import * as Bech32m from 'ox/Bech32m' ``` ::: ## Examples ```ts twoslash import { Bech32m } from 'ox' const { hrp, data } = Bech32m.decode( 'tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwa7xtm' ) // @log: { hrp: 'tempo', data: Uint8Array(20) } ``` ## Definition ```ts function decode( str: string, options?: decode.Options, ): decode.ReturnType ``` **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L76) ## Parameters ### str * **Type:** `string` The bech32m-encoded string to decode. ### options * **Type:** `decode.Options` * **Optional** #### options.limit * **Type:** `number` * **Optional** Maximum length of the encoded string. ## Return Type The decoded HRP and data bytes. `decode.ReturnType` # Bech32m.encode Encodes data bytes with a human-readable part (HRP) into a bech32m string (BIP-350). ## Imports :::code-group ```ts [Named] import { Bech32m } from 'ox' ``` ```ts [Entrypoint] import * as Bech32m from 'ox/Bech32m' ``` ::: ## Examples ```ts twoslash import { Bech32m } from 'ox' const encoded = Bech32m.encode('tempo', new Uint8Array(20)) // @log: 'tempo1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwa7xtm' ``` ## Definition ```ts function encode( hrp: string, data: Uint8Array, options?: encode.Options, ): string ``` **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L25) ## Parameters ### hrp * **Type:** `string` The human-readable part (e.g. `"tempo"`, `"tempoz"`). ### data * **Type:** `Uint8Array` The data bytes to encode. ### options * **Type:** `encode.Options` * **Optional** #### options.limit * **Type:** `number` * **Optional** Maximum length of the encoded string. ## Return Type The bech32m-encoded string. `string` # Bech32m Errors ## `Bech32m.ExceedsLengthError` Thrown when the encoded string exceeds the length limit. **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L190) ## `Bech32m.InvalidCharacterError` Thrown when a bech32m string contains an invalid character. **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L155) ## `Bech32m.InvalidChecksumError` Thrown when a bech32m string has an invalid checksum. **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L147) ## `Bech32m.InvalidHrpError` Thrown when the HRP is invalid (empty or contains non-ASCII characters). **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L180) ## `Bech32m.MixedCaseError` Thrown when a bech32m string contains mixed case. **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L172) ## `Bech32m.NoSeparatorError` Thrown when a bech32m string has no separator. **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L139) # Bech32m Types ## `Bech32m.InvalidPaddingError` **Source:** [src/core/Bech32m.ts](https://github.com/wevm/ox/blob/main/src/core/Bech32m.ts#L169) # Bytes A set of Ethereum-related utility functions for working with [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) instances. ## Examples Below are some examples demonstrating common usages of the `Bytes` module: * [Instantiating Bytes](#instantiating-bytes) * [Converting from Bytes](#converting-from-bytes) * [Concatenating Bytes](#concatenating-bytes) * [Slicing Bytes](#slicing-bytes) * [Padding Bytes](#padding-bytes) * [Trimming Bytes](#trimming-bytes) ### Instantiating Bytes Values can be instantiated as [`Bytes.Bytes`](/api/Bytes/types#bytes) using: * [`Bytes.fromArray`](/api/Bytes/fromArray) * [`Bytes.fromBoolean`](/api/Bytes/fromBoolean) * [`Bytes.fromHex`](/api/Bytes/fromHex) * [`Bytes.fromNumber`](/api/Bytes/fromNumber) * [`Bytes.fromString`](/api/Bytes/fromString) ```ts twoslash import { Bytes } from 'ox' const value_array = Bytes.from([1, 2, 3, 4, 5]) // @log: Uint8Array [1, 2, 3, 4, 5] const value_boolean = Bytes.fromBoolean(true) // @log: Uint8Array [1] const value_hex = Bytes.fromHex('0x1234567890abcdef') // @log: Uint8Array [18, 52, 86, 120, 144, 175, 207, 15] const value_number = Bytes.fromNumber(1234567890) // @log: Uint8Array [4, 160, 216] const value_string = Bytes.fromString('Hello World!') // @log: Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33] ``` ### Converting from Bytes Values can be converted from [`Bytes.Bytes`](/api/Bytes/types#bytes) using: * [`Bytes.toBigInt`](/api/Bytes/toBigInt) * [`Bytes.toBoolean`](/api/Bytes/toBoolean) * [`Bytes.toHex`](/api/Bytes/toHex) * [`Bytes.toNumber`](/api/Bytes/toNumber) * [`Bytes.toString`](/api/Bytes/toString) ```ts twoslash import { Bytes } from 'ox' const value_bigint = Bytes.toBigInt( Bytes.from([4, 160, 216]) ) // @log: 1234567890n const value_boolean = Bytes.toBoolean(Bytes.from([1])) // @log: true const value_hex = Bytes.toHex( Bytes.from([222, 173, 190, 239]) ) // @log: '0xdeadbeef' const value_number = Bytes.toNumber( Bytes.from([4, 160, 216]) ) // @log: 1234567890 const value_string = Bytes.toString( Bytes.from([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: 'Hello World!' ``` ### Concatenating Bytes Values can be concatenated using [`Bytes.concat`](/api/Bytes/concat): ```ts twoslash import { Bytes } from 'ox' const a = Bytes.from([1, 2, 3]) const b = Bytes.from([4, 5, 6]) const c = Bytes.concat(a, b) // @log: Uint8Array [1, 2, 3, 4, 5, 6] ``` ### Slicing Bytes Values can be sliced using [`Bytes.slice`](/api/Bytes/slice): ```ts twoslash import { Bytes } from 'ox' const value = Bytes.slice( Bytes.from([1, 2, 3, 4, 5, 6]), 2, 4 ) // @log: Uint8Array [3, 4] ``` ### Padding Bytes Values can be padded with zeroes using [`Bytes.padLeft`](/api/Bytes/padLeft) or [`Bytes.padRight`](/api/Bytes/padRight): ```ts twoslash import { Bytes } from 'ox' const value_1 = Bytes.padLeft(Bytes.from([1, 2, 3]), 5) // @log: Uint8Array [0, 0, 1, 2, 3] const value_2 = Bytes.padRight(Bytes.from([1, 2, 3]), 5) // @log: Uint8Array [1, 2, 3, 0, 0] ``` ### Trimming Bytes Zeroes in values can be trimmed using [`Bytes.trimLeft`](/api/Bytes/trimLeft) or [`Bytes.trimRight`](/api/Bytes/trimRight): ```ts twoslash import { Bytes } from 'ox' const value = Bytes.trimLeft(Bytes.from([0, 0, 1, 2, 3])) // @log: Uint8Array [1, 2, 3] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Bytes.assert`](/api/Bytes/assert) | Asserts if the given value is [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.concat`](/api/Bytes/concat) | Concatenates two or more [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.from`](/api/Bytes/from) | Instantiates a [`Bytes.Bytes`](/api/Bytes/types#bytes) value from a `Uint8Array`, a hex string, or an array of unsigned 8-bit integers. | | [`Bytes.fromArray`](/api/Bytes/fromArray) | Converts an array of unsigned 8-bit integers into [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.fromBoolean`](/api/Bytes/fromBoolean) | Encodes a boolean value into [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.fromHex`](/api/Bytes/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) value into [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.fromNumber`](/api/Bytes/fromNumber) | Encodes a number value into [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.fromString`](/api/Bytes/fromString) | Encodes a string into [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Bytes.isEqual`](/api/Bytes/isEqual) | Checks if two [`Bytes.Bytes`](/api/Bytes/types#bytes) values are equal. | | [`Bytes.padLeft`](/api/Bytes/padLeft) | Pads a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). | | [`Bytes.padRight`](/api/Bytes/padRight) | Pads a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). | | [`Bytes.random`](/api/Bytes/random) | Generates random [`Bytes.Bytes`](/api/Bytes/types#bytes) of the specified length. | | [`Bytes.size`](/api/Bytes/size) | Retrieves the size of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`Bytes.slice`](/api/Bytes/slice) | Returns a section of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value given a start/end bytes offset. | | [`Bytes.toBigInt`](/api/Bytes/toBigInt) | Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a bigint. | | [`Bytes.toBoolean`](/api/Bytes/toBoolean) | Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a boolean. | | [`Bytes.toHex`](/api/Bytes/toHex) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Bytes.toNumber`](/api/Bytes/toNumber) | Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a number. | | [`Bytes.toString`](/api/Bytes/toString) | Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a string. | | [`Bytes.trimLeft`](/api/Bytes/trimLeft) | Trims leading zeros from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`Bytes.trimRight`](/api/Bytes/trimRight) | Trims trailing zeros from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`Bytes.validate`](/api/Bytes/validate) | Checks if the given value is [`Bytes.Bytes`](/api/Bytes/types#bytes). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Bytes.InvalidBytesBooleanError`](/api/Bytes/errors#bytesinvalidbytesbooleanerror) | Thrown when the bytes value cannot be represented as a boolean. | | [`Bytes.InvalidBytesTypeError`](/api/Bytes/errors#bytesinvalidbytestypeerror) | Thrown when a value cannot be converted to bytes. | | [`Bytes.SizeExceedsPaddingSizeError`](/api/Bytes/errors#bytessizeexceedspaddingsizeerror) | Thrown when a the padding size exceeds the maximum allowed size. | | [`Bytes.SizeOverflowError`](/api/Bytes/errors#bytessizeoverflowerror) | Thrown when a size exceeds the maximum allowed size. | | [`Bytes.SliceOffsetOutOfBoundsError`](/api/Bytes/errors#bytessliceoffsetoutofboundserror) | Thrown when a slice offset is out-of-bounds. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Bytes.Bytes`](/api/Bytes/types#bytesbytes) | Root type for a Bytes array. | # Bytes.assert Asserts if the given value is [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.assert('abc') // @error: Bytes.InvalidBytesTypeError: // @error: Value `"abc"` of type `string` is an invalid Bytes value. // @error: Bytes values must be of type `Uint8Array`. ``` ## Definition ```ts function assert( value: unknown, ): asserts value is Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L38) ## Parameters ### value * **Type:** `unknown` Value to assert. # Bytes.concat Concatenates two or more [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const bytes = Bytes.concat( Bytes.from([1]), Bytes.from([69]), Bytes.from([420, 69]) ) // @log: Uint8Array [ 1, 69, 420, 69 ] ``` ## Definition ```ts function concat( values: readonly Bytes[], ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L69) ## Parameters ### values * **Type:** `readonly Bytes[]` Values to concatenate. ## Return Type Concatenated [`Bytes.Bytes`](/api/Bytes/types#bytes). `Bytes` # Bytes.from Instantiates a [`Bytes.Bytes`](/api/Bytes/types#bytes) value from a `Uint8Array`, a hex string, or an array of unsigned 8-bit integers. :::tip To instantiate from a **Boolean**, **String**, or **Number**, use one of the following: * `Bytes.fromBoolean` * `Bytes.fromString` * `Bytes.fromNumber` ::: ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash // @noErrors import { Bytes } from 'ox' const data = Bytes.from([255, 124, 5, 4]) // @log: Uint8Array([255, 124, 5, 4]) const data = Bytes.from('0xdeadbeef') // @log: Uint8Array([222, 173, 190, 239]) ``` ## Definition ```ts function from( value: Hex.Hex | Bytes | readonly number[], ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L117) ## Parameters ### value * **Type:** `Hex.Hex | Bytes | readonly number[]` Value to convert. ## Return Type A [`Bytes.Bytes`](/api/Bytes/types#bytes) instance. `Bytes` # Bytes.fromArray Converts an array of unsigned 8-bit integers into [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromArray([255, 124, 5, 4]) // @log: Uint8Array([255, 124, 5, 4]) ``` ## Definition ```ts function fromArray( value: readonly number[] | Uint8Array, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L144) ## Parameters ### value * **Type:** `readonly number[] | Uint8Array` Value to convert. ## Return Type A [`Bytes.Bytes`](/api/Bytes/types#bytes) instance. `Bytes` # Bytes.fromBoolean Encodes a boolean value into [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromBoolean(true) // @log: Uint8Array([1]) ``` ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromBoolean(true, { size: 32 }) // @log: Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) ``` ## Definition ```ts function fromBoolean( value: boolean, options?: fromBoolean.Options, ): Uint8Array ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L175) ## Parameters ### value * **Type:** `boolean` Boolean value to encode. ### options * **Type:** `fromBoolean.Options` * **Optional** Encoding options. #### options.size * **Type:** `number` * **Optional** Size of the output bytes. ## Return Type Encoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Uint8Array` # Bytes.fromHex Encodes a [`Hex.Hex`](/api/Hex/types#hex) value into [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromHex('0x48656c6c6f20776f726c6421') // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) ``` ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromHex('0x48656c6c6f20776f726c6421', { size: 32 }) // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ``` ## Definition ```ts function fromHex( value: Hex.Hex, options?: fromHex.Options, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L223) ## Parameters ### value * **Type:** `Hex.Hex` [`Hex.Hex`](/api/Hex/types#hex) value to encode. ### options * **Type:** `fromHex.Options` * **Optional** Encoding options. #### options.size * **Type:** `number` * **Optional** Size of the output bytes. ## Return Type Encoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Bytes` # Bytes.fromNumber Encodes a number value into [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromNumber(420) // @log: Uint8Array([1, 164]) ``` ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromNumber(420, { size: 4 }) // @log: Uint8Array([0, 0, 1, 164]) ``` ## Definition ```ts function fromNumber( value: bigint | number, options?: fromNumber.Options, ): Uint8Array ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L271) ## Parameters ### value * **Type:** `bigint | number` Number value to encode. ### options * **Type:** `fromNumber.Options` * **Optional** Encoding options. ## Return Type Encoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Uint8Array` # Bytes.fromString Encodes a string into [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromString('Hello world!') // @log: Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]) ``` ```ts twoslash import { Bytes } from 'ox' const data = Bytes.fromString('Hello world!', { size: 32 }) // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) ``` ## Definition ```ts function fromString( value: string, options?: fromString.Options, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L307) ## Parameters ### value * **Type:** `string` String to encode. ### options * **Type:** `fromString.Options` * **Optional** Encoding options. #### options.size * **Type:** `number` * **Optional** Size of the output bytes. ## Return Type Encoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Bytes` # Bytes.isEqual Checks if two [`Bytes.Bytes`](/api/Bytes/types#bytes) values are equal. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.isEqual(Bytes.from([1]), Bytes.from([1])) // @log: true Bytes.isEqual(Bytes.from([1]), Bytes.from([2])) // @log: false ``` ## Definition ```ts function isEqual( bytesA: Bytes, bytesB: Bytes, ): boolean ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L351) ## Parameters ### bytesA * **Type:** `Bytes` First [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ### bytesB * **Type:** `Bytes` Second [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Return Type `true` if the two values are equal, otherwise `false`. `boolean` # Bytes.padLeft Pads a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.padLeft(Bytes.from([1]), 4) // @log: Uint8Array([0, 0, 0, 1]) ``` ## Definition ```ts function padLeft( value: Bytes, size?: number, ): padLeft.ReturnType ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L374) ## Parameters ### value * **Type:** `Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) value to pad. ### size * **Type:** `number` * **Optional** Size to pad the [`Bytes.Bytes`](/api/Bytes/types#bytes) value to. ## Return Type Padded [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `padLeft.ReturnType` # Bytes.padRight Pads a [`Bytes.Bytes`](/api/Bytes/types#bytes) value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.padRight(Bytes.from([1]), 4) // @log: Uint8Array([1, 0, 0, 0]) ``` ## Definition ```ts function padRight( value: Bytes, size?: number, ): padRight.ReturnType ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L398) ## Parameters ### value * **Type:** `Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) value to pad. ### size * **Type:** `number` * **Optional** Size to pad the [`Bytes.Bytes`](/api/Bytes/types#bytes) value to. ## Return Type Padded [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `padRight.ReturnType` # Bytes.random Generates random [`Bytes.Bytes`](/api/Bytes/types#bytes) of the specified length. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const bytes = Bytes.random(32) // @log: Uint8Array([... x32]) ``` ## Definition ```ts function random( length: number, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L421) ## Parameters ### length * **Type:** `number` Length of the random [`Bytes.Bytes`](/api/Bytes/types#bytes) to generate. ## Return Type Random [`Bytes.Bytes`](/api/Bytes/types#bytes) of the specified length. `Bytes` # Bytes.size Retrieves the size of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.size(Bytes.from([1, 2, 3, 4])) // @log: 4 ``` ## Definition ```ts function size( value: Bytes, ): number ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L443) ## Parameters ### value * **Type:** `Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Return Type Size of the [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `number` # Bytes.slice Returns a section of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value given a start/end bytes offset. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.slice(Bytes.from([1, 2, 3, 4, 5, 6, 7, 8, 9]), 1, 4) // @log: Uint8Array([2, 3, 4]) ``` ## Definition ```ts function slice( value: Bytes, start?: number, end?: number, options?: slice.Options, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L468) ## Parameters ### value * **Type:** `Bytes` The [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ### start * **Type:** `number` * **Optional** Start offset. ### end * **Type:** `number` * **Optional** End offset. ### options * **Type:** `slice.Options` * **Optional** Slice options. #### options.strict * **Type:** `boolean` * **Optional** Asserts that the sliced value is the same size as the given start/end offsets. ## Return Type Sliced [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `Bytes` # Bytes.toBigInt Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a bigint. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts import { Bytes } from 'ox' Bytes.toBigInt(Bytes.from([1, 164])) // @log: 420n ``` ## Definition ```ts function toBigInt( bytes: Bytes, options?: toBigInt.Options, ): bigint ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L508) ## Parameters ### bytes * **Type:** `Bytes` The [`Bytes.Bytes`](/api/Bytes/types#bytes) to decode. ### options * **Type:** `toBigInt.Options` * **Optional** Decoding options. #### options.signed * **Type:** `boolean` * **Optional** Whether or not the number of a signed representation. #### options.size * **Type:** `number` * **Optional** Size of the bytes. ## Return Type Decoded bigint. `bigint` # Bytes.toBoolean Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a boolean. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts import { Bytes } from 'ox' Bytes.toBoolean(Bytes.from([1])) // @log: true ``` ## Definition ```ts function toBoolean( bytes: Bytes, options?: toBoolean.Options, ): boolean ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L543) ## Parameters ### bytes * **Type:** `Bytes` The [`Bytes.Bytes`](/api/Bytes/types#bytes) to decode. ### options * **Type:** `toBoolean.Options` * **Optional** Decoding options. #### options.size * **Type:** `number` * **Optional** Size of the bytes. ## Return Type Decoded boolean. `boolean` # Bytes.toHex Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.toHex( Bytes.from([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // '0x48656c6c6f20576f726c6421' ``` ## Definition ```ts function toHex( value: Bytes, options?: toHex.Options, ): Hex.Hex ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L589) ## Parameters ### value * **Type:** `Bytes` The [`Bytes.Bytes`](/api/Bytes/types#bytes) to decode. ### options * **Type:** `toHex.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** Size of the bytes. ## Return Type Decoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex.Hex` # Bytes.toNumber Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a number. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.toNumber(Bytes.from([1, 164])) // @log: 420 ``` ## Definition ```ts function toNumber( bytes: Bytes, options?: toNumber.Options, ): number ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L623) ## Parameters ### bytes * **Type:** `Bytes` ### options * **Type:** `toNumber.Options` * **Optional** #### options.signed * **Type:** `boolean` * **Optional** Whether or not the number of a signed representation. #### options.size * **Type:** `number` * **Optional** Size of the bytes. ## Return Type `number` # Bytes.toString Decodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) into a string. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' const data = Bytes.toString( Bytes.from([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: 'Hello world' ``` ## Definition ```ts function toString( bytes: Bytes, options?: toString.Options, ): string ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L662) ## Parameters ### bytes * **Type:** `Bytes` The [`Bytes.Bytes`](/api/Bytes/types#bytes) to decode. ### options * **Type:** `toString.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** Size of the bytes. ## Return Type Decoded string. `string` # Bytes.trimLeft Trims leading zeros from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.trimLeft(Bytes.from([0, 0, 0, 0, 1, 2, 3])) // @log: Uint8Array([1, 2, 3]) ``` ## Definition ```ts function trimLeft( value: Bytes, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L699) ## Parameters ### value * **Type:** `Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Return Type Trimmed [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `Bytes` # Bytes.trimRight Trims trailing zeros from a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.trimRight(Bytes.from([1, 2, 3, 0, 0, 0, 0])) // @log: Uint8Array([1, 2, 3]) ``` ## Definition ```ts function trimRight( value: Bytes, ): Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L721) ## Parameters ### value * **Type:** `Bytes` [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Return Type Trimmed [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `Bytes` # Bytes.validate Checks if the given value is [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Bytes } from 'ox' ``` ```ts [Entrypoint] import * as Bytes from 'ox/Bytes' ``` ::: ## Examples ```ts twoslash import { Bytes } from 'ox' Bytes.validate('0x') // @log: false Bytes.validate(Bytes.from([1, 2, 3])) // @log: true ``` ## Definition ```ts function validate( value: unknown, ): value is Bytes ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L746) ## Parameters ### value * **Type:** `unknown` Value to check. ## Return Type `true` if the value is [`Bytes.Bytes`](/api/Bytes/types#bytes), otherwise `false`. `value is Bytes` # Bytes Errors ## `Bytes.InvalidBytesBooleanError` Thrown when the bytes value cannot be represented as a boolean. ### Examples ```ts twoslash import { Bytes } from 'ox' Bytes.toBoolean(Bytes.from([5])) // @error: Bytes.InvalidBytesBooleanError: Bytes value `[5]` is not a valid boolean. // @error: The bytes array must contain a single byte of either a `0` or `1` value. ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L771) ## `Bytes.InvalidBytesTypeError` Thrown when a value cannot be converted to bytes. ### Examples ```ts twoslash // @noErrors import { Bytes } from 'ox' Bytes.from('foo') // @error: Bytes.InvalidBytesTypeError: Value `foo` of type `string` is an invalid Bytes value. ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L795) ## `Bytes.SizeExceedsPaddingSizeError` Thrown when a the padding size exceeds the maximum allowed size. ### Examples ```ts twoslash import { Bytes } from 'ox' Bytes.padLeft(Bytes.fromString('Hello World!'), 8) // @error: [Bytes.SizeExceedsPaddingSizeError: Bytes size (`12`) exceeds padding size (`8`). ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L871) ## `Bytes.SizeOverflowError` Thrown when a size exceeds the maximum allowed size. ### Examples ```ts twoslash import { Bytes } from 'ox' Bytes.fromString('Hello World!', { size: 8 }) // @error: Bytes.SizeOverflowError: Size cannot exceed `8` bytes. Given size: `12` bytes. ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L819) ## `Bytes.SliceOffsetOutOfBoundsError` Thrown when a slice offset is out-of-bounds. ### Examples ```ts twoslash import { Bytes } from 'ox' Bytes.slice(Bytes.from([1, 2, 3]), 4) // @error: Bytes.SliceOffsetOutOfBoundsError: Slice starting at offset `4` is out-of-bounds (size: `3`). ``` **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L840) # Bytes Types ## `Bytes.Bytes` Root type for a Bytes array. **Source:** [src/core/Bytes.ts](https://github.com/wevm/ox/blob/main/src/core/Bytes.ts#L21) # Cbor Functions for encoding and decoding CBOR (Concise Binary Object Representation) data. CBOR is a binary data format designed for compact data representation and efficient parsing. It supports all JSON data types plus additional types like byte strings, tags, and simple values. ## Examples ### Encoding Values to CBOR Values can be encoded to CBOR using [`Cbor.encode`](/api/Cbor/encode): ```ts twoslash import { Cbor } from 'ox' Cbor.encode({ foo: 'bar', baz: [1, 2, 3] }) // @log: '0xa263666f6f636261726362617a83010203' ``` ### Decoding CBOR to Values Values can be decoded from CBOR using [`Cbor.decode`](/api/Cbor/decode): ```ts twoslash import { Cbor } from 'ox' Cbor.decode('0xa263666f6f636261726362617a83010203') // @log: { foo: 'bar', baz: [1, 2, 3] } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Cbor.decode`](/api/Cbor/decode) | Decodes CBOR (Concise Binary Object Representation) data into a JavaScript value. | | [`Cbor.encode`](/api/Cbor/encode) | Encodes a value into CBOR (Concise Binary Object Representation) format. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Cbor.ArrayTooLargeError`](/api/Cbor/errors#cborarraytoolargeerror) | | | [`Cbor.ByteStringTooLargeError`](/api/Cbor/errors#cborbytestringtoolargeerror) | | | [`Cbor.InvalidAdditionalInfoError`](/api/Cbor/errors#cborinvalidadditionalinfoerror) | | | [`Cbor.InvalidIndefiniteLengthChunkError`](/api/Cbor/errors#cborinvalidindefinitelengthchunkerror) | | | [`Cbor.InvalidMajorTypeError`](/api/Cbor/errors#cborinvalidmajortypeerror) | | | [`Cbor.InvalidSimpleValueError`](/api/Cbor/errors#cborinvalidsimplevalueerror) | | | [`Cbor.NumberTooLargeError`](/api/Cbor/errors#cbornumbertoolargeerror) | | | [`Cbor.ObjectTooLargeError`](/api/Cbor/errors#cborobjecttoolargeerror) | | | [`Cbor.StringTooLargeError`](/api/Cbor/errors#cborstringtoolargeerror) | | | [`Cbor.UnexpectedTokenError`](/api/Cbor/errors#cborunexpectedtokenerror) | | | [`Cbor.Unsupported64BitIntegerError`](/api/Cbor/errors#cborunsupported64bitintegererror) | | | [`Cbor.UnsupportedBigIntError`](/api/Cbor/errors#cborunsupportedbiginterror) | | | [`Cbor.UnsupportedTagError`](/api/Cbor/errors#cborunsupportedtagerror) | | # Cbor.decode Decodes CBOR (Concise Binary Object Representation) data into a JavaScript value. ## Imports :::code-group ```ts [Named] import { Cbor } from 'ox' ``` ```ts [Entrypoint] import * as Cbor from 'ox/Cbor' ``` ::: ## Examples ```ts twoslash import { Cbor } from 'ox' Cbor.decode('0x83010203') // @log: [1, 2, 3] Cbor.decode('0xa263666f6f636261726362617a83010203') // @log: { foo: 'bar', baz: [1, 2, 3] } Cbor.decode(new Uint8Array([101, 104, 101, 108, 108, 111])) // @log: 'hello' ``` ## Definition ```ts function decode( data: Hex.Hex | Bytes.Bytes, ): type ``` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L85) ## Parameters ### data * **Type:** `Hex.Hex | Bytes.Bytes` The CBOR-encoded data to decode. ## Return Type The decoded value. `type` # Cbor.encode Encodes a value into CBOR (Concise Binary Object Representation) format. ## Imports :::code-group ```ts [Named] import { Cbor } from 'ox' ``` ```ts [Entrypoint] import * as Cbor from 'ox/Cbor' ``` ::: ## Examples ```ts twoslash import { Cbor } from 'ox' Cbor.encode([1, 2, 3]) // @log: '0x83010203' Cbor.encode({ foo: 'bar', baz: [1, 2, 3] }) // @log: '0xa263666f6f636261726362617a83010203' Cbor.encode('hello', { as: 'Bytes' }) // @log: Uint8Array(6) [ 101, 104, 101, 108, 108, 111 ] ``` ## Definition ```ts function encode( data: unknown, options?: encode.Options, ): encode.ReturnType ``` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L27) ## Parameters ### data * **Type:** `unknown` The value to encode. ### options * **Type:** `encode.Options` * **Optional** Encoding options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** The format to return the encoded value in. ## Return Type The CBOR-encoded value. `encode.ReturnType` # Cbor Errors ## `Cbor.ArrayTooLargeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L184) ## `Cbor.ByteStringTooLargeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L200) ## `Cbor.InvalidAdditionalInfoError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L110) ## `Cbor.InvalidIndefiniteLengthChunkError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L134) ## `Cbor.InvalidMajorTypeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L102) ## `Cbor.InvalidSimpleValueError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L142) ## `Cbor.NumberTooLargeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L166) ## `Cbor.ObjectTooLargeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L192) ## `Cbor.StringTooLargeError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L176) ## `Cbor.UnexpectedTokenError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L158) ## `Cbor.Unsupported64BitIntegerError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L118) ## `Cbor.UnsupportedBigIntError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L150) ## `Cbor.UnsupportedTagError` **Source:** [src/core/Cbor.ts](https://github.com/wevm/ox/blob/main/src/core/Cbor.ts#L126) # CompactSize Utility functions for [Bitcoin's CompactSize](https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) variable-length integer encoding. ## Examples Below are some examples demonstrating common usages of the `CompactSize` module: * [Encoding](#encoding) * [Decoding](#decoding) ### Encoding ```ts twoslash import { CompactSize } from 'ox' const bytes = CompactSize.toBytes(65535) ``` ### Decoding ```ts twoslash import { CompactSize } from 'ox' const { value, size } = CompactSize.fromBytes( new Uint8Array([0xfd, 0xff, 0xff]) ) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`CompactSize.fromBytes`](/api/CompactSize/fromBytes) | Decodes a CompactSize-encoded value from [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`CompactSize.fromHex`](/api/CompactSize/fromHex) | Decodes a CompactSize-encoded value from [`Hex.Hex`](/api/Hex/types#hex). | | [`CompactSize.toBytes`](/api/CompactSize/toBytes) | Encodes an integer using Bitcoin's CompactSize variable-length encoding. | | [`CompactSize.toHex`](/api/CompactSize/toHex) | Encodes an integer using Bitcoin's CompactSize variable-length encoding and returns it as [`Hex.Hex`](/api/Hex/types#hex). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`CompactSize.InsufficientBytesError`](/api/CompactSize/errors#compactsizeinsufficientbyteserror) | Thrown when there are insufficient bytes to decode a CompactSize value. | | [`CompactSize.InvalidValueError`](/api/CompactSize/errors#compactsizeinvalidvalueerror) | Thrown when a CompactSize input is not a safe integer. | | [`CompactSize.NegativeValueError`](/api/CompactSize/errors#compactsizenegativevalueerror) | Thrown when a CompactSize value is negative. | | [`CompactSize.NonMinimalEncodingError`](/api/CompactSize/errors#compactsizenonminimalencodingerror) | Thrown when a CompactSize value is encoded non-minimally. | # CompactSize.fromBytes Decodes a CompactSize-encoded value from [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { CompactSize } from 'ox' ``` ```ts [Entrypoint] import * as CompactSize from 'ox/CompactSize' ``` ::: ## Examples ```ts twoslash import { CompactSize } from 'ox' const result = CompactSize.fromBytes( new Uint8Array([0xfd, 0x00, 0x01]) ) // { value: 256, size: 3 } ``` ## Definition ```ts function fromBytes( data: Bytes.Bytes, ): fromBytes.ReturnType ``` **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L101) ## Parameters ### data * **Type:** `Bytes.Bytes` The bytes to decode from. ## Return Type The decoded value and number of bytes consumed. `fromBytes.ReturnType` # CompactSize.fromHex Decodes a CompactSize-encoded value from [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { CompactSize } from 'ox' ``` ```ts [Entrypoint] import * as CompactSize from 'ox/CompactSize' ``` ::: ## Examples ```ts twoslash import { CompactSize } from 'ox' const result = CompactSize.fromHex('0xfd0001') // { value: 256, size: 3 } ``` ## Definition ```ts function fromHex( data: Hex.Hex, ): fromBytes.ReturnType ``` **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L159) ## Parameters ### data * **Type:** `Hex.Hex` The hex string to decode from. ## Return Type The decoded value and number of bytes consumed. `fromBytes.ReturnType` # CompactSize.toBytes Encodes an integer using Bitcoin's CompactSize variable-length encoding. | Range | Encoding | Bytes | |---|---|---| | 0–252 | Direct value | 1 | | 253–65,535 | `0xFD` + 2 bytes LE | 3 | | 65,536–4,294,967,295 | `0xFE` + 4 bytes LE | 5 | | > 4,294,967,295 | `0xFF` + 8 bytes LE | 9 | ## Imports :::code-group ```ts [Named] import { CompactSize } from 'ox' ``` ```ts [Entrypoint] import * as CompactSize from 'ox/CompactSize' ``` ::: ## Examples ```ts twoslash import { CompactSize } from 'ox' const bytes = CompactSize.toBytes(252) // Uint8Array [252] const bytes2 = CompactSize.toBytes(253) // Uint8Array [253, 253, 0] ``` ## Definition ```ts function toBytes( value: bigint | number, ): Bytes.Bytes ``` **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L29) ## Parameters ### value * **Type:** `bigint | number` The integer to encode. ## Return Type The CompactSize-encoded bytes. `Bytes.Bytes` # CompactSize.toHex Encodes an integer using Bitcoin's CompactSize variable-length encoding and returns it as [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { CompactSize } from 'ox' ``` ```ts [Entrypoint] import * as CompactSize from 'ox/CompactSize' ``` ::: ## Examples ```ts twoslash import { CompactSize } from 'ox' const hex = CompactSize.toHex(252) // '0xfc' ``` ## Definition ```ts function toHex( value: bigint | number, ): Hex.Hex ``` **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L77) ## Parameters ### value * **Type:** `bigint | number` The integer to encode. ## Return Type The CompactSize-encoded hex string. `Hex.Hex` # CompactSize Errors ## `CompactSize.InsufficientBytesError` Thrown when there are insufficient bytes to decode a CompactSize value. **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L177) ## `CompactSize.InvalidValueError` Thrown when a CompactSize input is not a safe integer. **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L188) ## `CompactSize.NegativeValueError` Thrown when a CompactSize value is negative. **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L168) ## `CompactSize.NonMinimalEncodingError` Thrown when a CompactSize value is encoded non-minimally. **Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L197) # Hex A set of Ethereum-related utility functions for working with hexadecimal string values (e.g. `"0xdeadbeef"`). ## Examples Below are some examples demonstrating common usages of the `Hex` module: * [Instantiating Hex](#instantiating-hex) * [Converting from Hex](#converting-from-hex) * [Concatenating Hex](#concatenating-hex) * [Slicing Hex](#slicing-hex) * [Padding Hex](#padding-hex) * [Trimming Hex](#trimming-hex) ### Instantiating Hex Values can be instantiated as [`Hex.Hex`](/api/Hex/types#hex) using: * [`Hex.fromBoolean`](/api/Hex/fromBoolean) * [`Hex.fromBytes`](/api/Hex/fromBytes) * [`Hex.fromNumber`](/api/Hex/fromNumber) * [`Hex.fromString`](/api/Hex/fromString) ```ts twoslash import { Bytes, Hex } from 'ox' const value_boolean = Hex.fromBoolean(true) // @log: '0x1' const value_bytes = Hex.fromBytes(Bytes.from([1, 2, 3])) // @log: '0x010203' const value_number = Hex.fromNumber(1234567890) // @log: '0x499602d2' const value_string = Hex.fromString('Hello World!') // @log: '0x48656c6c6f20576f726c6421' ``` ### Converting from Hex Values can be converted from [`Hex.Hex`](/api/Hex/types#hex) using: * [`Hex.toBoolean`](/api/Hex/toBoolean) * [`Hex.toBytes`](/api/Hex/toBytes) * [`Hex.toNumber`](/api/Hex/toNumber) * [`Hex.toString`](/api/Hex/toString) ```ts twoslash import { Hex } from 'ox' const value_boolean = Hex.toBoolean('0x1') // @log: true const value_bytes = Hex.toBytes('0x010203') // @log: Uint8Array [1, 2, 3] const value_number = Hex.toNumber('0x499602d2') // @log: 1234567890 const value_string = Hex.toString( '0x48656c6c6f20576f726c6421' ) // @log: 'Hello World!' ``` ### Concatenating Hex Hex values can be concatenated using [`Hex.concat`](/api/Hex/concat): ```ts twoslash import { Hex } from 'ox' const a = Hex.fromString('0x1234567890abcdef') const b = Hex.fromString('0xdeadbeef') const c = Hex.concat(a, b) // @log: '0x1234567890abcdefdeadbeef' ``` ### Slicing Hex Hex values can be sliced using [`Hex.slice`](/api/Hex/slice): ```ts twoslash import { Hex } from 'ox' const value = Hex.slice('0x1234567890abcdefdeadbeef', 2, 8) // @log: '0x34567890' ``` ### Padding Hex Hex values can be padded with zeroes using [`Hex.padLeft`](/api/Hex/padLeft) or [`Hex.padRight`](/api/Hex/padRight): ```ts twoslash import { Hex } from 'ox' const value = Hex.padLeft('0x1234567890abcdef', 16) // @log: '0x00000000000000001234567890abcdef' ``` ### Trimming Hex Hex values can be trimmed of zeroes using [`Hex.trimLeft`](/api/Hex/trimLeft) or [`Hex.trimRight`](/api/Hex/trimRight): ```ts twoslash import { Hex } from 'ox' const value = Hex.trimLeft( '0x00000000000000001234567890abcdef' ) // @log: '0x1234567890abcdef' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Hex.assert`](/api/Hex/assert) | Asserts if the given value is [`Hex.Hex`](/api/Hex/types#hex). | | [`Hex.concat`](/api/Hex/concat) | Concatenates two or more [`Hex.Hex`](/api/Hex/types#hex). | | [`Hex.from`](/api/Hex/from) | Instantiates a [`Hex.Hex`](/api/Hex/types#hex) value from a hex string or [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`Hex.fromBoolean`](/api/Hex/fromBoolean) | Encodes a boolean into a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.fromBytes`](/api/Hex/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.fromNumber`](/api/Hex/fromNumber) | Encodes a number or bigint into a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.fromString`](/api/Hex/fromString) | Encodes a string into a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.isEqual`](/api/Hex/isEqual) | Checks if two [`Hex.Hex`](/api/Hex/types#hex) values are equal. | | [`Hex.padLeft`](/api/Hex/padLeft) | Pads a [`Hex.Hex`](/api/Hex/types#hex) value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). | | [`Hex.padRight`](/api/Hex/padRight) | Pads a [`Hex.Hex`](/api/Hex/types#hex) value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). | | [`Hex.random`](/api/Hex/random) | Generates a random [`Hex.Hex`](/api/Hex/types#hex) value of the specified length. | | [`Hex.size`](/api/Hex/size) | Retrieves the size of a [`Hex.Hex`](/api/Hex/types#hex) value (in bytes). | | [`Hex.slice`](/api/Hex/slice) | Returns a section of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value given a start/end bytes offset. | | [`Hex.toBigInt`](/api/Hex/toBigInt) | Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a BigInt. | | [`Hex.toBoolean`](/api/Hex/toBoolean) | Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a boolean. | | [`Hex.toBytes`](/api/Hex/toBytes) | Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a [`Bytes.Bytes`](/api/Bytes/types#bytes). | | [`Hex.toNumber`](/api/Hex/toNumber) | Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a number. | | [`Hex.toString`](/api/Hex/toString) | Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a string. | | [`Hex.trimLeft`](/api/Hex/trimLeft) | Trims leading zeros from a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.trimRight`](/api/Hex/trimRight) | Trims trailing zeros from a [`Hex.Hex`](/api/Hex/types#hex) value. | | [`Hex.validate`](/api/Hex/validate) | Checks if the given value is [`Hex.Hex`](/api/Hex/types#hex). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Hex.InvalidHexBooleanError`](/api/Hex/errors#hexinvalidhexbooleanerror) | Thrown when the provided hex value cannot be represented as a boolean. | | [`Hex.InvalidHexTypeError`](/api/Hex/errors#hexinvalidhextypeerror) | Thrown when the provided value is not a valid hex type. | | [`Hex.SizeExceedsPaddingSizeError`](/api/Hex/errors#hexsizeexceedspaddingsizeerror) | Thrown when the size of the value exceeds the pad size. | | [`Hex.SizeOverflowError`](/api/Hex/errors#hexsizeoverflowerror) | Thrown when the size of the value exceeds the expected max size. | | [`Hex.SliceOffsetOutOfBoundsError`](/api/Hex/errors#hexsliceoffsetoutofboundserror) | Thrown when the slice offset exceeds the bounds of the value. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Hex.Hex`](/api/Hex/types#hexhex) | Root type for a Hex string. | | [`Hex.IntegerOutOfRangeError`](/api/Hex/types#hexintegeroutofrangeerror) | Re-exported from `internal/codec/int.ts`. | | [`Hex.InvalidHexValueError`](/api/Hex/types#hexinvalidhexvalueerror) | Re-exported from `internal/codec/hex.ts`. | | [`Hex.InvalidLengthError`](/api/Hex/types#hexinvalidlengtherror) | Re-exported from `internal/codec/hex.ts`. | # Hex.assert Asserts if the given value is [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.assert('abc') // @error: InvalidHexValueTypeError: // @error: Value `"abc"` of type `string` is an invalid hex type. // @error: Hex types must be represented as `"0x\${string}"`. ``` ## Definition ```ts function assert( value: unknown, options?: assert.Options, ): asserts value is Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L34) ## Parameters ### value * **Type:** `unknown` The value to assert. ### options * **Type:** `assert.Options` * **Optional** Options. #### options.strict * **Type:** `boolean` * **Optional** Checks if the [`Hex.Hex`](/api/Hex/types#hex) value contains invalid hexadecimal characters. # Hex.concat Concatenates two or more [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.concat('0x123', '0x456') // @log: '0x123456' ``` ## Definition ```ts function concat( values: readonly Hex[], ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L72) ## Parameters ### values * **Type:** `readonly Hex[]` The [`Hex.Hex`](/api/Hex/types#hex) values to concatenate. ## Return Type The concatenated [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.from Instantiates a [`Hex.Hex`](/api/Hex/types#hex) value from a hex string or [`Bytes.Bytes`](/api/Bytes/types#bytes) value. :::tip To instantiate from a **Boolean**, **String**, or **Number**, use one of the following: * `Hex.fromBoolean` * `Hex.fromString` * `Hex.fromNumber` ::: ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Bytes, Hex } from 'ox' Hex.from('0x48656c6c6f20576f726c6421') // @log: '0x48656c6c6f20576f726c6421' Hex.from( Bytes.from([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: '0x48656c6c6f20576f726c6421' ``` ## Definition ```ts function from( value: Hex | Uint8Array | readonly number[], ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L117) ## Parameters ### value * **Type:** `Hex | Uint8Array | readonly number[]` The [`Bytes.Bytes`](/api/Bytes/types#bytes) value to encode. ## Return Type The encoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.fromBoolean Encodes a boolean into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.fromBoolean(true) // @log: '0x1' Hex.fromBoolean(false) // @log: '0x0' Hex.fromBoolean(true, { size: 32 }) // @log: '0x0000000000000000000000000000000000000000000000000000000000000001' ``` ## Definition ```ts function fromBoolean( value: boolean, options?: fromBoolean.Options, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L154) ## Parameters ### value * **Type:** `boolean` The boolean value to encode. ### options * **Type:** `fromBoolean.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The encoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.fromBytes Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Bytes, Hex } from 'ox' Hex.fromBytes( Bytes.from([ 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33 ]) ) // @log: '0x48656c6c6f20576f726c6421' ``` ## Definition ```ts function fromBytes( value: Uint8Array, options?: fromBytes.Options, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L197) ## Parameters ### value * **Type:** `Uint8Array` The [`Bytes.Bytes`](/api/Bytes/types#bytes) value to encode. ### options * **Type:** `fromBytes.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The encoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.fromNumber Encodes a number or bigint into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.fromNumber(420) // @log: '0x1a4' Hex.fromNumber(420, { size: 32 }) // @log: '0x00000000000000000000000000000000000000000000000000000000000001a4' ``` ## Definition ```ts function fromNumber( value: number | bigint, options?: fromNumber.Options, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L240) ## Parameters ### value * **Type:** `number | bigint` The number or bigint value to encode. ### options * **Type:** `fromNumber.Options` * **Optional** Options. #### options.signed * **Type:** `boolean` * **Optional** Whether or not the number of a signed representation. #### options.size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The encoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.fromString Encodes a string into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.fromString('Hello World!') // '0x48656c6c6f20576f726c6421' Hex.fromString('Hello World!', { size: 32 }) // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000' ``` ## Definition ```ts function fromString( value: string, options?: fromString.Options, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L338) ## Parameters ### value * **Type:** `string` The string value to encode. ### options * **Type:** `fromString.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The encoded [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.isEqual Checks if two [`Hex.Hex`](/api/Hex/types#hex) values are equal. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.isEqual('0xdeadbeef', '0xdeadbeef') // @log: true Hex.isEqual('0xda', '0xba') // @log: false ``` ## Definition ```ts function isEqual( hexA: Hex, hexB: Hex, ): boolean ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L372) ## Parameters ### hexA * **Type:** `Hex` The first [`Hex.Hex`](/api/Hex/types#hex) value. ### hexB * **Type:** `Hex` The second [`Hex.Hex`](/api/Hex/types#hex) value. ## Return Type `true` if the two [`Hex.Hex`](/api/Hex/types#hex) values are equal, `false` otherwise. `boolean` # Hex.padLeft Pads a [`Hex.Hex`](/api/Hex/types#hex) value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.padLeft('0x1234', 4) // @log: '0x00001234' ``` ## Definition ```ts function padLeft( value: Hex, size?: number, ): padLeft.ReturnType ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L395) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to pad. ### size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The padded [`Hex.Hex`](/api/Hex/types#hex) value. `padLeft.ReturnType` # Hex.padRight Pads a [`Hex.Hex`](/api/Hex/types#hex) value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts import { Hex } from 'ox' Hex.padRight('0x1234', 4) // @log: '0x12340000' ``` ## Definition ```ts function padRight( value: Hex, size?: number, ): padRight.ReturnType ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L419) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to pad. ### size * **Type:** `number` * **Optional** The size (in bytes) of the output hex value. ## Return Type The padded [`Hex.Hex`](/api/Hex/types#hex) value. `padRight.ReturnType` # Hex.random Generates a random [`Hex.Hex`](/api/Hex/types#hex) value of the specified length. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' const hex = Hex.random(32) // @log: '0x...' ``` ## Definition ```ts function random( length: number, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L441) ## Parameters ### length * **Type:** `number` ## Return Type Random [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.size Retrieves the size of a [`Hex.Hex`](/api/Hex/types#hex) value (in bytes). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.size('0xdeadbeef') // @log: 4 ``` ## Definition ```ts function size( value: Hex, ): number ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L519) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to get the size of. ## Return Type The size of the [`Hex.Hex`](/api/Hex/types#hex) value (in bytes). `number` # Hex.slice Returns a section of a [`Bytes.Bytes`](/api/Bytes/types#bytes) value given a start/end bytes offset. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.slice('0x0123456789', 1, 4) // @log: '0x234567' ``` ## Definition ```ts function slice( value: Hex, start?: number, end?: number, options?: slice.Options, ): Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L466) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to slice. ### start * **Type:** `number` * **Optional** The start offset (in bytes). ### end * **Type:** `number` * **Optional** The end offset (in bytes). ### options * **Type:** `slice.Options` * **Optional** Options. #### options.strict * **Type:** `boolean` * **Optional** Asserts that the sliced value is the same size as the given start/end offsets. ## Return Type The sliced [`Hex.Hex`](/api/Hex/types#hex) value. `Hex` # Hex.toBigInt Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a BigInt. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.toBigInt('0x1a4') // @log: 420n Hex.toBigInt( '0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 } ) // @log: 420n ``` ## Definition ```ts function toBigInt( hex: Hex, options?: toBigInt.Options, ): bigint ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L596) ## Parameters ### hex * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to decode. ### options * **Type:** `toBigInt.Options` * **Optional** Options. #### options.signed * **Type:** `boolean` * **Optional** Whether or not the number of a signed representation. #### options.size * **Type:** `number` * **Optional** Size (in bytes) of the hex value. ## Return Type The decoded BigInt. `bigint` # Hex.toBoolean Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a boolean. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.toBoolean('0x01') // @log: true Hex.toBoolean( '0x0000000000000000000000000000000000000000000000000000000000000001', { size: 32 } ) // @log: true ``` ## Definition ```ts function toBoolean( hex: Hex, options?: toBoolean.Options, ): boolean ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L645) ## Parameters ### hex * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to decode. ### options * **Type:** `toBoolean.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** Size (in bytes) of the hex value. ## Return Type The decoded boolean. `boolean` # Hex.toBytes Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a [`Bytes.Bytes`](/api/Bytes/types#bytes). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' const data = Hex.toBytes('0x48656c6c6f20776f726c6421') // @log: Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]) ``` ## Definition ```ts function toBytes( hex: Hex, options?: toBytes.Options, ): Uint8Array ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L681) ## Parameters ### hex * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to decode. ### options * **Type:** `toBytes.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** Size (in bytes) of the hex value. ## Return Type The decoded [`Bytes.Bytes`](/api/Bytes/types#bytes). `Uint8Array` # Hex.toNumber Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a number. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.toNumber('0x1a4') // @log: 420 Hex.toNumber( '0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 } ) // @log: 420 ``` ## Definition ```ts function toNumber( hex: Hex, options?: toNumber.Options, ): number ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L724) ## Parameters ### hex * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to decode. ### options * **Type:** `toNumber.Options` * **Optional** Options. ## Return Type The decoded number. `number` # Hex.toString Decodes a [`Hex.Hex`](/api/Hex/types#hex) value into a string. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.toString('0x48656c6c6f20576f726c6421') // @log: 'Hello world!' Hex.toString( '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', { size: 32 } ) // @log: 'Hello world' ``` ## Definition ```ts function toString( hex: Hex, options?: toString.Options, ): string ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L767) ## Parameters ### hex * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to decode. ### options * **Type:** `toString.Options` * **Optional** Options. #### options.size * **Type:** `number` * **Optional** Size (in bytes) of the hex value. ## Return Type The decoded string. `string` # Hex.trimLeft Trims leading zeros from a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.trimLeft('0x00000000deadbeef') // @log: '0xdeadbeef' ``` ## Definition ```ts function trimLeft( value: Hex, ): trimLeft.ReturnType ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L541) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to trim. ## Return Type The trimmed [`Hex.Hex`](/api/Hex/types#hex) value. `trimLeft.ReturnType` # Hex.trimRight Trims trailing zeros from a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Hex } from 'ox' Hex.trimRight('0xdeadbeef00000000') // @log: '0xdeadbeef' ``` ## Definition ```ts function trimRight( value: Hex, ): trimRight.ReturnType ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L565) ## Parameters ### value * **Type:** `Hex` The [`Hex.Hex`](/api/Hex/types#hex) value to trim. ## Return Type The trimmed [`Hex.Hex`](/api/Hex/types#hex) value. `trimRight.ReturnType` # Hex.validate Checks if the given value is [`Hex.Hex`](/api/Hex/types#hex). ## Imports :::code-group ```ts [Named] import { Hex } from 'ox' ``` ```ts [Entrypoint] import * as Hex from 'ox/Hex' ``` ::: ## Examples ```ts twoslash import { Bytes, Hex } from 'ox' Hex.validate('0xdeadbeef') // @log: true Hex.validate(Bytes.from([1, 2, 3])) // @log: false ``` ## Definition ```ts function validate( value: unknown, options?: validate.Options, ): value is Hex ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L814) ## Parameters ### value * **Type:** `unknown` The value to check. ### options * **Type:** `validate.Options` * **Optional** Options. #### options.strict * **Type:** `boolean` * **Optional** Checks if the [`Hex.Hex`](/api/Hex/types#hex) value contains invalid hexadecimal characters. ## Return Type `true` if the value is a [`Hex.Hex`](/api/Hex/types#hex), `false` otherwise. `value is Hex` # Hex Errors ## `Hex.InvalidHexBooleanError` Thrown when the provided hex value cannot be represented as a boolean. ### Examples ```ts twoslash import { Hex } from 'ox' Hex.toBoolean('0xa') // @error: Hex.InvalidHexBooleanError: Hex value `"0xa"` is not a valid boolean. // @error: The hex value must be `"0x0"` (false) or `"0x1"` (true). ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L859) ## `Hex.InvalidHexTypeError` Thrown when the provided value is not a valid hex type. ### Examples ```ts twoslash import { Hex } from 'ox' Hex.assert(1) // @error: Hex.InvalidHexTypeError: Value `1` of type `number` is an invalid hex type. ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L882) ## `Hex.SizeExceedsPaddingSizeError` Thrown when the size of the value exceeds the pad size. ### Examples ```ts twoslash import { Hex } from 'ox' Hex.padLeft( '0x1a4e12a45a21323123aaa87a897a897a898a6567a578a867a98778a667a85a875a87a6a787a65a675a6a9', 32 ) // @error: Hex.SizeExceedsPaddingSizeError: Hex size (`43`) exceeds padding size (`32`). ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L981) ## `Hex.SizeOverflowError` Thrown when the size of the value exceeds the expected max size. ### Examples ```ts twoslash import { Hex } from 'ox' Hex.fromString('Hello World!', { size: 8 }) // @error: Hex.SizeOverflowError: Size cannot exceed `8` bytes. Given size: `12` bytes. ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L926) ## `Hex.SliceOffsetOutOfBoundsError` Thrown when the slice offset exceeds the bounds of the value. ### Examples ```ts twoslash import { Hex } from 'ox' Hex.slice('0x0123456789', 6) // @error: Hex.SliceOffsetOutOfBoundsError: Slice starting at offset `6` is out-of-bounds (size: `5`). ``` **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L947) # Hex Types ## `Hex.Hex` Root type for a Hex string. **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L16) ## `Hex.IntegerOutOfRangeError` Re-exported from `internal/codec/int.ts`. **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L843) ## `Hex.InvalidHexValueError` Re-exported from `internal/codec/hex.ts`. **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L902) ## `Hex.InvalidLengthError` Re-exported from `internal/codec/hex.ts`. **Source:** [src/core/Hex.ts](https://github.com/wevm/ox/blob/main/src/core/Hex.ts#L913) # Rlp Utility functions for encoding and decoding [Recursive Length Prefix](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/) structures. ## Examples ```ts twoslash import { Hex, Rlp } from 'ox' const data = Rlp.fromHex([ Hex.fromString('hello'), Hex.fromString('world') ]) // @log: '0xcc8568656c6c6f85776f726c64' const values = Rlp.toHex(data) // @log: [Hex.fromString('hello'), Hex.fromString('world')] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Rlp.encodeTo`](/api/Rlp/encodeTo) | Encodes a value as Recursive-Length Prefix (RLP) and writes the encoded bytes through a synchronous callback without allocating the complete encoding. | | [`Rlp.from`](/api/Rlp/from) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value into a Recursive-Length Prefix (RLP) value. | | [`Rlp.fromBytes`](/api/Rlp/fromBytes) | Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a Recursive-Length Prefix (RLP) value. | | [`Rlp.fromHex`](/api/Rlp/fromHex) | Encodes a [`Hex.Hex`](/api/Hex/types#hex) value into a Recursive-Length Prefix (RLP) value. | | [`Rlp.toBytes`](/api/Rlp/toBytes) | Decodes a Recursive-Length Prefix (RLP) value into a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. | | [`Rlp.toHex`](/api/Rlp/toHex) | Decodes a Recursive-Length Prefix (RLP) value into a [`Hex.Hex`](/api/Hex/types#hex) value. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Rlp.DepthLimitExceededError`](/api/Rlp/errors#rlpdepthlimitexceedederror) | Thrown when an RLP value nests deeper than the decode depth limit. | | [`Rlp.ListBoundaryExceededError`](/api/Rlp/errors#rlplistboundaryexceedederror) | Thrown when RLP list items overrun the list's declared length. | | [`Rlp.TrailingBytesError`](/api/Rlp/errors#rlptrailingbyteserror) | Thrown when an RLP payload contains bytes after the decoded item. | # Rlp.encodeTo Encodes a value as Recursive-Length Prefix (RLP) and writes the encoded bytes through a synchronous callback without allocating the complete encoding. Ox validates the complete input before the first write. If the callback throws, the error propagates and earlier callback side effects remain. Ox does not mutate a chunk after the callback returns. A chunk may alias a byte-array leaf from the encoded value. The callback must not mutate the chunk before `encodeTo` returns. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Bytes, Rlp } from 'ox' const chunks: Uint8Array[] = [] Rlp.encodeTo(['0x01', '0x0203'], (chunk) => { chunks.push(chunk) }) const encoded = Bytes.concat(...chunks) // @log: Uint8Array([196, 1, 130, 2, 3]) ``` ## Definition ```ts function encodeTo( value: RecursiveArray | RecursiveArray, write: (chunk: Bytes.Bytes) => undefined, ): void ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L223) ## Parameters ### value * **Type:** `RecursiveArray | RecursiveArray` The bytes or Hex value, or nested list of values, to encode. ### write * **Type:** `(chunk: Bytes.Bytes) => undefined` The synchronous callback for encoded chunks. ## Return Type Nothing. `void` # Rlp.from Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value into a Recursive-Length Prefix (RLP) value. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Bytes, Rlp } from 'ox' Rlp.from('0x68656c6c6f20776f726c64', { as: 'Hex' }) // @log: 0x8b68656c6c6f20776f726c64 Rlp.from( Bytes.from([ 139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]), { as: 'Bytes' } ) // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) ``` ## Definition ```ts function from( value: RecursiveArray | RecursiveArray, options: from.Options, ): from.ReturnType ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L261) ## Parameters ### value * **Type:** `RecursiveArray | RecursiveArray` The [`Bytes.Bytes`](/api/Bytes/types#bytes) or [`Hex.Hex`](/api/Hex/types#hex) value to encode. ### options * **Type:** `from.Options` Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` The type to convert the RLP value to. ## Return Type The RLP value. `from.ReturnType` # Rlp.fromBytes Encodes a [`Bytes.Bytes`](/api/Bytes/types#bytes) value into a Recursive-Length Prefix (RLP) value. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Bytes, Rlp } from 'ox' Rlp.fromBytes( Bytes.from([ 139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 ]) ) // @log: Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) ``` ## Definition ```ts function fromBytes( bytes: RecursiveArray, options?: fromBytes.Options, ): fromBytes.ReturnType ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L335) ## Parameters ### bytes * **Type:** `RecursiveArray` The [`Bytes.Bytes`](/api/Bytes/types#bytes) value to encode. ### options * **Type:** `fromBytes.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` The type to convert the RLP value to. ## Return Type The RLP value. `fromBytes.ReturnType` # Rlp.fromHex Encodes a [`Hex.Hex`](/api/Hex/types#hex) value into a Recursive-Length Prefix (RLP) value. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Rlp } from 'ox' Rlp.fromHex('0x68656c6c6f20776f726c64') // @log: 0x8b68656c6c6f20776f726c64 ``` ## Definition ```ts function fromHex( hex: RecursiveArray, options?: fromHex.Options, ): fromHex.ReturnType ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L368) ## Parameters ### hex * **Type:** `RecursiveArray` The [`Hex.Hex`](/api/Hex/types#hex) value to encode. ### options * **Type:** `fromHex.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` The type to convert the RLP value to. ## Return Type The RLP value. `fromHex.ReturnType` # Rlp.toBytes Decodes a Recursive-Length Prefix (RLP) value into a [`Bytes.Bytes`](/api/Bytes/types#bytes) value. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Rlp } from 'ox' Rlp.toBytes('0x8b68656c6c6f20776f726c64') // Uint8Array([139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]) ``` ## Definition ```ts function toBytes( value: Bytes.Bytes | Hex.Hex, ): RecursiveArray ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L24) ## Parameters ### value * **Type:** `Bytes.Bytes | Hex.Hex` The value to decode. ## Return Type The decoded [`Bytes.Bytes`](/api/Bytes/types#bytes) value. `RecursiveArray` # Rlp.toHex Decodes a Recursive-Length Prefix (RLP) value into a [`Hex.Hex`](/api/Hex/types#hex) value. ## Imports :::code-group ```ts [Named] import { Rlp } from 'ox' ``` ```ts [Entrypoint] import * as Rlp from 'ox/Rlp' ``` ::: ## Examples ```ts twoslash import { Rlp } from 'ox' Rlp.toHex('0x8b68656c6c6f20776f726c64') // 0x68656c6c6f20776f726c64 ``` ## Definition ```ts function toHex( value: Bytes.Bytes | Hex.Hex, ): RecursiveArray ``` **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L47) ## Parameters ### value * **Type:** `Bytes.Bytes | Hex.Hex` The value to decode. ## Return Type The decoded [`Hex.Hex`](/api/Hex/types#hex) value. `RecursiveArray` # Rlp Errors ## `Rlp.DepthLimitExceededError` Thrown when an RLP value nests deeper than the decode depth limit. **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L845) ## `Rlp.ListBoundaryExceededError` Thrown when RLP list items overrun the list's declared length. **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L854) ## `Rlp.TrailingBytesError` Thrown when an RLP payload contains bytes after the decoded item. **Source:** [src/core/Rlp.ts](https://github.com/wevm/ox/blob/main/src/core/Rlp.ts#L865) # Value Utility functions for displaying and parsing Ethereum Values as defined under **2.1. Value** in the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf) ## Examples ```ts twoslash // @noErrors import { Value } from 'ox' const value = Value.fromEther('1') // @log: 1_000_000_000_000_000_000n const formattedValue = Value.formatEther(value) // @log: '1' const value = Value.fromEther('1', 'szabo') // @log: 1_000_000n ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Value.format`](/api/Value/format) | Formats a `bigint` Value to its string representation (divided by the given exponent). | | [`Value.formatEther`](/api/Value/formatEther) | Formats a `bigint` Value (default: wei) to a string representation of Ether. | | [`Value.formatGwei`](/api/Value/formatGwei) | Formats a `bigint` Value (default: wei) to a string representation of Gwei. | | [`Value.from`](/api/Value/from) | Parses a `string` representation of a Value to `bigint` (multiplied by the given exponent). | | [`Value.fromEther`](/api/Value/fromEther) | Parses a string representation of Ether to a `bigint` Value (default: wei). | | [`Value.fromGwei`](/api/Value/fromGwei) | Parses a string representation of Gwei to a `bigint` Value (default: wei). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Value.InvalidDecimalNumberError`](/api/Value/errors#valueinvaliddecimalnumbererror) | Thrown when a value is not a valid decimal number. | | [`Value.InvalidDecimalsError`](/api/Value/errors#valueinvaliddecimalserror) | Thrown when the `decimals` argument is not a non-negative integer. | # Value.format Formats a `bigint` Value to its string representation (divided by the given exponent). ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.format(420_000_000_000n, 9) // @log: '420' ``` ## Definition ```ts function format( value: bigint, decimals?: number, ): string ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L27) ## Parameters ### value * **Type:** `bigint` The `bigint` Value to format. ### decimals * **Type:** `number` * **Optional** The exponent to divide the `bigint` Value by. ## Return Type The string representation of the Value. `string` # Value.formatEther Formats a `bigint` Value (default: wei) to a string representation of Ether. ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.formatEther(1_000_000_000_000_000_000n) // @log: '1' ``` ## Definition ```ts function formatEther( wei: bigint, unit?: 'wei' | 'gwei' | 'szabo' | 'finney', ): string ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L67) ## Parameters ### wei * **Type:** `bigint` The Value to format. ### unit * **Type:** `'wei' | 'gwei' | 'szabo' | 'finney'` * **Optional** The unit to format the Value in. ## Return Type The Ether string representation of the Value. `string` # Value.formatGwei Formats a `bigint` Value (default: wei) to a string representation of Gwei. ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.formatGwei(1_000_000_000n) // @log: '1' ``` ## Definition ```ts function formatGwei( wei: bigint, unit?: 'wei', ): string ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L93) ## Parameters ### wei * **Type:** `bigint` The Value to format. ### unit * **Type:** `'wei'` * **Optional** The unit to format the Value in. ## Return Type The Gwei string representation of the Value. `string` # Value.from Parses a `string` representation of a Value to `bigint` (multiplied by the given exponent). ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.from('420', 9) // @log: 420000000000n ``` ## Definition ```ts function from( value: string, decimals?: number, ): bigint ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L116) ## Parameters ### value * **Type:** `string` The string representation of the Value. ### decimals * **Type:** `number` * **Optional** The exponent to multiply the Value by. ## Return Type The `bigint` representation of the Value. `bigint` # Value.fromEther Parses a string representation of Ether to a `bigint` Value (default: wei). ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.fromEther('420') // @log: 420000000000000000000n ``` ## Definition ```ts function fromEther( ether: string, unit?: 'wei' | 'gwei' | 'szabo' | 'finney', ): bigint ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L210) ## Parameters ### ether * **Type:** `string` String representation of Ether. ### unit * **Type:** `'wei' | 'gwei' | 'szabo' | 'finney'` * **Optional** The unit to parse to. ## Return Type A `bigint` Value. `bigint` # Value.fromGwei Parses a string representation of Gwei to a `bigint` Value (default: wei). ## Imports :::code-group ```ts [Named] import { Value } from 'ox' ``` ```ts [Entrypoint] import * as Value from 'ox/Value' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' Value.fromGwei('420') // @log: 420000000000n ``` ## Definition ```ts function fromGwei( gwei: string, unit?: 'wei', ): bigint ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L236) ## Parameters ### gwei * **Type:** `string` String representation of Gwei. ### unit * **Type:** `'wei'` * **Optional** The unit to parse to. ## Return Type A `bigint` Value. `bigint` # Value Errors ## `Value.InvalidDecimalNumberError` Thrown when a value is not a valid decimal number. ### Examples ```ts twoslash import { Value } from 'ox' Value.fromEther('123.456.789') // @error: Value.InvalidDecimalNumberError: Value `123.456.789` is not a valid decimal number. ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L255) ## `Value.InvalidDecimalsError` Thrown when the `decimals` argument is not a non-negative integer. ### Examples ```ts twoslash import { Value } from 'ox' Value.from('1', -1) // @error: Value.InvalidDecimalsError: `decimals` must be a non-negative integer. Got `-1`. ``` **Source:** [src/core/Value.ts](https://github.com/wevm/ox/blob/main/src/core/Value.ts#L273) # Ens Utility functions for working with ENS names. ## Examples Below are some examples demonstrating common usages of the `Ens` module: * [Normalizing ENS Names](#normalizing-ens-names) * [Namehashing ENS Names](#namehashing-ens-names) ### Normalizing ENS Names ENS names can be normalized using [`Ens.normalize`](/api/Ens/normalize): ```ts twoslash import { Ens } from 'ox' const name = Ens.normalize('w𝝣vm.eth') // @log: 'wξvm.eth' ``` ### Namehashing ENS Names ENS names can be namehashed using [`Ens.namehash`](/api/Ens/namehash): ```ts twoslash import { Ens } from 'ox' const name = Ens.namehash('alice.eth') // @log: '0x787192fc5378cc32aa956ddfdedbf26b24e8d78e40109add0eea2c1a012c3dec' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Ens.labelhash`](/api/Ens/labelhash) | Hashes ENS label. | | [`Ens.namehash`](/api/Ens/namehash) | Hashes ENS name. | | [`Ens.normalize`](/api/Ens/normalize) | Normalizes ENS name according to [ENSIP-15](https://github.com/ensdomains/docs/blob/9edf9443de4333a0ea7ec658a870672d5d180d53/ens-improvement-proposals/ensip-15-normalization-standard.md). | | [`Ens.toCoinType`](/api/Ens/toCoinType) | Converts a chain ID to an ENSIP-9 compliant coin type. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Ens.InvalidChainIdError`](/api/Ens/errors#ensinvalidchainiderror) | Thrown when an ENS chain ID is invalid. | # Ens.labelhash Hashes ENS label. Since ENS labels prohibit certain forbidden characters (e.g. underscore) and have other validation rules, you likely want to [normalize ENS labels](https://docs.ens.domains/contract-api-reference/name-processing#normalising-names) with [UTS-46 normalization](https://unicode.org/reports/tr46) before passing them to `labelhash`. You can use the built-in [`Ens.normalize`](/api/Ens/normalize) function for this. ## Imports :::code-group ```ts [Named] import { Ens } from 'ox' ``` ```ts [Entrypoint] import * as Ens from 'ox/Ens' ``` ::: ## Examples ```ts twoslash import { Ens } from 'ox' Ens.labelhash('eth') ;('0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0') ``` ## Definition ```ts function labelhash( label: string, ): `0x${string}` ``` **Source:** [src/core/Ens.ts](https://github.com/wevm/ox/blob/main/src/core/Ens.ts#L25) ## Parameters ### label * **Type:** `string` ENS label. ## Return Type ENS labelhash. `0x${string}` # Ens.namehash Hashes ENS name. Since ENS names prohibit certain forbidden characters (e.g. underscore) and have other validation rules, you likely want to [normalize ENS names](https://docs.ens.domains/contract-api-reference/name-processing#normalising-names) with [UTS-46 normalization](https://unicode.org/reports/tr46) before passing them to `namehash`. You can use the built-in [`Ens.normalize`](/api/Ens/normalize) function for this. ## Imports :::code-group ```ts [Named] import { Ens } from 'ox' ``` ```ts [Entrypoint] import * as Ens from 'ox/Ens' ``` ::: ## Examples ```ts twoslash import { Ens } from 'ox' Ens.namehash('wevm.eth') // @log: '0xf246651c1b9a6b141d19c2604e9a58f567973833990f830d882534a747801359' ``` ## Definition ```ts function namehash( name: string, ): `0x${string}` ``` **Source:** [src/core/Ens.ts](https://github.com/wevm/ox/blob/main/src/core/Ens.ts#L57) ## Parameters ### name * **Type:** `string` ENS name. ## Return Type ENS namehash. `0x${string}` # Ens.normalize Normalizes ENS name according to [ENSIP-15](https://github.com/ensdomains/docs/blob/9edf9443de4333a0ea7ec658a870672d5d180d53/ens-improvement-proposals/ensip-15-normalization-standard.md). For more info see [ENS documentation](https://docs.ens.domains/contract-api-reference/name-processing#normalising-names) on name processing. ## Imports :::code-group ```ts [Named] import { Ens } from 'ox' ``` ```ts [Entrypoint] import * as Ens from 'ox/Ens' ``` ::: ## Examples ```ts twoslash import { Ens } from 'ox' Ens.normalize('wevm.eth') // @log: 'wevm.eth' ``` ## Definition ```ts function normalize( name: string, ): string ``` **Source:** [src/core/Ens.ts](https://github.com/wevm/ox/blob/main/src/core/Ens.ts#L100) ## Parameters ### name * **Type:** `string` ENS name. ## Return Type Normalized ENS name. `string` # Ens.toCoinType Converts a chain ID to an ENSIP-9 compliant coin type. ## Imports :::code-group ```ts [Named] import { Ens } from 'ox' ``` ```ts [Entrypoint] import * as Ens from 'ox/Ens' ``` ::: ## Examples ```ts twoslash import { Ens } from 'ox' Ens.toCoinType(10n) // @log: 2147483658n ``` ## Definition ```ts function toCoinType( chainId: bigint, ): bigint ``` **Source:** [src/core/Ens.ts](https://github.com/wevm/ox/blob/main/src/core/Ens.ts#L122) ## Parameters ### chainId * **Type:** `bigint` Chain ID to convert. ## Return Type ENS coin type. `bigint` # Ens Errors ## `Ens.InvalidChainIdError` Thrown when an ENS chain ID is invalid. **Source:** [src/core/Ens.ts](https://github.com/wevm/ox/blob/main/src/core/Ens.ts#L134) # AccessList Utilities & types for working with Access Lists as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4140e528360fea53c34a766d86a000c6c039100e/src/schemas/transaction.yaml#L73) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AccessList.fromTupleList`](/api/AccessList/fromTupleList) | Converts a list of Access List tuples into a object-formatted list. | | [`AccessList.toTupleList`](/api/AccessList/toTupleList) | Converts a structured Access List into a list of tuples. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`AccessList.InvalidStorageKeySizeError`](/api/AccessList/errors#accesslistinvalidstoragekeysizeerror) | Thrown when the size of a storage key is invalid. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AccessList.AccessList`](/api/AccessList/types#accesslistaccesslist) | | | [`AccessList.Item`](/api/AccessList/types#accesslistitem) | | | [`AccessList.ItemTuple`](/api/AccessList/types#accesslistitemtuple) | | | [`AccessList.Tuple`](/api/AccessList/types#accesslisttuple) | | # AccessList.fromTupleList Converts a list of Access List tuples into a object-formatted list. ## Imports :::code-group ```ts [Named] import { AccessList } from 'ox' ``` ```ts [Entrypoint] import * as AccessList from 'ox/AccessList' ``` ::: ## Examples ```ts twoslash import { AccessList } from 'ox' const accessList = AccessList.fromTupleList([ [ '0x0000000000000000000000000000000000000000', [ '0x0000000000000000000000000000000000000000000000000000000000000001', '0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe' ] ] ]) // @log: [ // @log: { // @log: address: '0x0000000000000000000000000000000000000000', // @log: storageKeys: [ // @log: '0x0000000000000000000000000000000000000000000000000000000000000001', // @log: '0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe', // @log: ], // @log: }, // @log: ] ``` ## Definition ```ts function fromTupleList( accessList: AccessList.Tuple, ): AccessList ``` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L49) ## Parameters ### accessList * **Type:** [`AccessList.Tuple`](/api/AccessList/types#accesslisttuple) List of tuples. ## Return Type Access list. `AccessList` # AccessList.toTupleList Converts a structured Access List into a list of tuples. ## Imports :::code-group ```ts [Named] import { AccessList } from 'ox' ``` ```ts [Entrypoint] import * as AccessList from 'ox/AccessList' ``` ::: ## Examples ```ts twoslash import { AccessList } from 'ox' const accessList = AccessList.toTupleList([ { address: '0x0000000000000000000000000000000000000000', storageKeys: [ '0x0000000000000000000000000000000000000000000000000000000000000001', '0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe' ] } ]) // @log: [ // @log: [ // @log: '0x0000000000000000000000000000000000000000', // @log: [ // @log: '0x0000000000000000000000000000000000000000000000000000000000000001', // @log: '0x60fdd29ff912ce880cd3edaf9f932dc61d3dae823ea77e0323f94adb9f6a72fe', // @log: ], // @log: ], // @log: ] ``` ## Definition ```ts function toTupleList( accessList?: AccessList, ): Compute ``` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L100) ## Parameters ### accessList * **Type:** `AccessList` * **Optional** Access list. #### accessList.address * **Type:** `abitype_Address` #### accessList.storageKeys * **Type:** `readonly 0x${string}[]` ## Return Type List of tuples. `Compute` # AccessList Errors ## `AccessList.InvalidStorageKeySizeError` Thrown when the size of a storage key is invalid. **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L119) # AccessList Types ## `AccessList.AccessList` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L6) ## `AccessList.Item` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L8) ## `AccessList.ItemTuple` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L13) ## `AccessList.Tuple` **Source:** [src/core/AccessList.ts](https://github.com/wevm/ox/blob/main/src/core/AccessList.ts#L17) # AccountProof Utilities & types for working with Account Proofs as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`AccountProof.fromRpc`](/api/AccountProof/fromRpc) | Converts an [`AccountProof.Rpc`](/api/AccountProof/types#rpc) to an [`AccountProof.AccountProof`](/api/AccountProof/types#accountproof). | | [`AccountProof.toRpc`](/api/AccountProof/toRpc) | Converts an [`AccountProof.AccountProof`](/api/AccountProof/types#accountproof) to an [`AccountProof.Rpc`](/api/AccountProof/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`AccountProof.AccountProof`](/api/AccountProof/types#accountproofaccountproof) | An Account Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). | | [`AccountProof.Rpc`](/api/AccountProof/types#accountproofrpc) | An RPC Account Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). | | [`AccountProof.StorageProof`](/api/AccountProof/types#accountproofstorageproof) | A Storage Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). | | [`AccountProof.StorageProofRpc`](/api/AccountProof/types#accountproofstorageproofrpc) | An RPC Storage Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). | # AccountProof.fromRpc Converts an [`AccountProof.Rpc`](/api/AccountProof/types#rpc) to an [`AccountProof.AccountProof`](/api/AccountProof/types#accountproof). ## Imports :::code-group ```ts [Named] import { AccountProof } from 'ox' ``` ```ts [Entrypoint] import * as AccountProof from 'ox/AccountProof' ``` ::: ## Examples ```ts twoslash import { AccountProof } from 'ox' const proof = AccountProof.fromRpc({ address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', balance: '0x1', codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', nonce: '0x2', storageHash: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', accountProof: [], storageProof: [ { key: '0x0000000000000000000000000000000000000000000000000000000000000000', proof: [], value: '0x3' } ] }) // @log: { // @log: address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', // @log: balance: 1n, // @log: codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', // @log: nonce: 2, // @log: storageHash: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', // @log: accountProof: [], // @log: storageProof: [ // @log: { // @log: key: '0x0000000000000000000000000000000000000000000000000000000000000000', // @log: proof: [], // @log: value: 3n, // @log: }, // @log: ], // @log: } ``` ## Definition ```ts function fromRpc( proof: Rpc, ): AccountProof ``` **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L84) ## Parameters ### proof * **Type:** `Rpc` The RPC account proof. ## Return Type An instantiated [`AccountProof.AccountProof`](/api/AccountProof/types#accountproof). `AccountProof.AccountProof` # AccountProof.toRpc Converts an [`AccountProof.AccountProof`](/api/AccountProof/types#accountproof) to an [`AccountProof.Rpc`](/api/AccountProof/types#rpc). ## Imports :::code-group ```ts [Named] import { AccountProof } from 'ox' ``` ```ts [Entrypoint] import * as AccountProof from 'ox/AccountProof' ``` ::: ## Examples ```ts twoslash import { AccountProof } from 'ox' const proof = AccountProof.toRpc({ address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', balance: 1n, codeHash: '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', nonce: 2, storageHash: '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', accountProof: [], storageProof: [ { key: '0x0000000000000000000000000000000000000000000000000000000000000000', proof: [], value: 3n } ] }) ``` ## Definition ```ts function toRpc( proof: toRpc.Input, ): Rpc ``` **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L133) ## Parameters ### proof * **Type:** `toRpc.Input` The account proof to convert. ## Return Type An RPC account proof. `Rpc` # AccountProof Types ## `AccountProof.AccountProof` An Account Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L7) ## `AccountProof.Rpc` An RPC Account Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L25) ## `AccountProof.StorageProof` A Storage Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L28) ## `AccountProof.StorageProofRpc` An RPC Storage Proof as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/state.yaml). **Source:** [src/core/AccountProof.ts](https://github.com/wevm/ox/blob/main/src/core/AccountProof.ts#L38) # Block Utilities & types for working with Blocks as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml) ## Examples ### Converting from RPC Format Blocks can be converted from RPC format to internal format using [`Block.fromRpc`](/api/Block/fromRpc): ```ts twoslash import 'ox/window' import { Block } from 'ox' const block = await window .ethereum!.request({ method: 'eth_getBlockByNumber', params: ['latest', false] }) .then(Block.fromRpc) // [!code hl] // @log: { // @log: // ... // @log: hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', // @log: number: 19868020n, // @log: size: 520n, // @log: timestamp: 1662222222n, // @log: // ... // @log: } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Block.fromRpc`](/api/Block/fromRpc) | Converts a [`Block.Rpc`](/api/Block/types#rpc) to an [`Block.Block`](/api/Block/types#block). | | [`Block.toRpc`](/api/Block/toRpc) | Converts a [`Block.Block`](/api/Block/types#block) to an [`Block.Rpc`](/api/Block/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Block.Block`](/api/Block/types#blockblock) | A Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). | | [`Block.Hash`](/api/Block/types#blockhash) | A Block hash. | | [`Block.Identifier`](/api/Block/types#blockidentifier) | A Block identifier. | | [`Block.Number`](/api/Block/types#blocknumber) | A Block number. | | [`Block.Rpc`](/api/Block/types#blockrpc) | An RPC Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). | | [`Block.Tag`](/api/Block/types#blocktag) | A Block Tag as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). | # Block.fromRpc Converts a [`Block.Rpc`](/api/Block/types#rpc) to an [`Block.Block`](/api/Block/types#block). ## Imports :::code-group ```ts [Named] import { Block } from 'ox' ``` ```ts [Entrypoint] import * as Block from 'ox/Block' ``` ::: ## Examples ```ts twoslash // @noErrors import { Block } from 'ox' const block = Block.fromRpc({ // ... hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', number: '0xec6fc6', size: '0x208', timestamp: '0x63198f6f' // ... }) // @log: { // @log: // ... // @log: hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', // @log: number: 19868020n, // @log: size: 520n, // @log: timestamp: 1662222222n, // @log: // ... // @log: } ``` ### End-to-end Below is an end-to-end example of using `Block.fromRpc` to fetch a block from the network and convert it to an [`Block.Block`](/api/Block/types#block). ```ts twoslash import 'ox/window' import { Block } from 'ox' const block = await window .ethereum!.request({ method: 'eth_getBlockByNumber', params: ['latest', false] }) .then(Block.fromRpc) // [!code hl] // @log: { // @log: // ... // @log: hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', // @log: number: 19868020n, // @log: size: 520n, // @log: timestamp: 1662222222n, // @log: // ... // @log: } ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function fromRpc( block: block | Rpc | null, _options?: fromRpc.Options, ): block extends Rpc ? Block : null ``` **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L275) ## Parameters ### block * **Type:** `block | Rpc | null` The RPC block to convert. ## Return Type An instantiated [`Block.Block`](/api/Block/types#block). `block extends Rpc ? Block : null` # Block.toRpc Converts a [`Block.Block`](/api/Block/types#block) to an [`Block.Rpc`](/api/Block/types#rpc). ## Imports :::code-group ```ts [Named] import { Block } from 'ox' ``` ```ts [Entrypoint] import * as Block from 'ox/Block' ``` ::: ## Examples ```ts twoslash // @noErrors import { Block } from 'ox' const block = Block.toRpc({ // ... hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', number: 19868020n, size: 520n timestamp: 1662222222n, // ... }) // @log: { // @log: // ... // @log: hash: '0xebc3644804e4040c0a74c5a5bbbc6b46a71a5d4010fe0c92ebb2fdf4a43ea5dd', // @log: number: '0xec6fc6', // @log: size: '0x208', // @log: timestamp: '0x63198f6f', // @log: // ... // @log: } ``` ## Definition ```ts function toRpc( block: toRpc.Input, _options?: toRpc.Options, ): Rpc ``` **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L147) ## Parameters ### block * **Type:** `toRpc.Input` The Block to convert. ## Return Type An RPC Block. `Rpc` # Block Types ## `Block.Block` A Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L10) ## `Block.Hash` A Block hash. **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L80) ## `Block.Identifier` A Block identifier. **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L83) ## `Block.Number` A Block number. **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L98) ## `Block.Rpc` An RPC Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L101) ## `Block.Tag` A Block Tag as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). * `earliest`: The lowest numbered block the client has available; - `finalized`: The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination; - `safe`: The most recent block that is safe from re-orgs under honest majority and certain synchronicity assumptions; - `latest`: The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions; - `pending`: A sample next block built by the client on top of `latest` and containing the set of transactions usually taken from local mempool. **Source:** [src/core/Block.ts](https://github.com/wevm/ox/blob/main/src/core/Block.ts#L116) # BlockOverrides Utilities & types for working with **Block Overrides**. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`BlockOverrides.fromRpc`](/api/BlockOverrides/fromRpc) | Converts an [`BlockOverrides.Rpc`](/api/BlockOverrides/types#rpc) to an [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverrides). | | [`BlockOverrides.toRpc`](/api/BlockOverrides/toRpc) | Converts an [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverrides) to an [`BlockOverrides.Rpc`](/api/BlockOverrides/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverridesblockoverrides) | Block overrides. | | [`BlockOverrides.Rpc`](/api/BlockOverrides/types#blockoverridesrpc) | RPC block overrides. | # BlockOverrides.fromRpc Converts an [`BlockOverrides.Rpc`](/api/BlockOverrides/types#rpc) to an [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverrides). ## Imports :::code-group ```ts [Named] import { BlockOverrides } from 'ox' ``` ```ts [Entrypoint] import * as BlockOverrides from 'ox/BlockOverrides' ``` ::: ## Examples ```ts twoslash import { BlockOverrides } from 'ox' const blockOverrides = BlockOverrides.fromRpc({ baseFeePerGas: '0x1', blobBaseFee: '0x2', feeRecipient: '0x0000000000000000000000000000000000000000', gasLimit: '0x4', number: '0x5', prevRandao: '0x6', time: '0x1234567890', withdrawals: [ { address: '0x0000000000000000000000000000000000000000', amount: '0x1', index: '0x0', validatorIndex: '0x1' } ] }) ``` ## Definition ```ts function fromRpc( rpcBlockOverrides: Rpc, ): BlockOverrides ``` **Source:** [src/core/BlockOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/BlockOverrides.ts#L63) ## Parameters ### rpcBlockOverrides * **Type:** `Rpc` The RPC block overrides to convert. ## Return Type An instantiated [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverrides). `BlockOverrides.BlockOverrides` # BlockOverrides.toRpc Converts an [`BlockOverrides.BlockOverrides`](/api/BlockOverrides/types#blockoverrides) to an [`BlockOverrides.Rpc`](/api/BlockOverrides/types#rpc). ## Imports :::code-group ```ts [Named] import { BlockOverrides } from 'ox' ``` ```ts [Entrypoint] import * as BlockOverrides from 'ox/BlockOverrides' ``` ::: ## Examples ```ts twoslash import { BlockOverrides } from 'ox' const blockOverrides = BlockOverrides.toRpc({ baseFeePerGas: 1n, blobBaseFee: 2n, feeRecipient: '0x0000000000000000000000000000000000000000', gasLimit: 4n, number: 5n, prevRandao: 6n, time: 78187493520n, withdrawals: [ { address: '0x0000000000000000000000000000000000000000', amount: 1n, index: 0, validatorIndex: 1 } ] }) ``` ## Definition ```ts function toRpc( blockOverrides: toRpc.Input, ): Rpc ``` **Source:** [src/core/BlockOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/BlockOverrides.ts#L116) ## Parameters ### blockOverrides * **Type:** `toRpc.Input` The block overrides to convert. ## Return Type An instantiated [`BlockOverrides.Rpc`](/api/BlockOverrides/types#rpc). `Rpc` # BlockOverrides Types ## `BlockOverrides.BlockOverrides` Block overrides. **Source:** [src/core/BlockOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/BlockOverrides.ts#L9) ## `BlockOverrides.Rpc` RPC block overrides. **Source:** [src/core/BlockOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/BlockOverrides.ts#L31) # Bloom Utility functions for working with Bloom Filters as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Bloom.contains`](/api/Bloom/contains) | Checks if an input is matched in the bloom filter. | | [`Bloom.containsHash`](/api/Bloom/containsHash) | Checks if a precomputed `keccak256` hash is matched in a [`Bloom.Prepared`](/api/Bloom/types#prepared) bloom filter. Use when the caller already has the hash and wants to skip the keccak inside `containsPrepared`. | | [`Bloom.containsPrepared`](/api/Bloom/containsPrepared) | Checks if an input is matched in a [`Bloom.Prepared`](/api/Bloom/types#prepared) bloom filter. | | [`Bloom.prepare`](/api/Bloom/prepare) | Prepares a bloom filter for repeated membership checks against the same filter. | | [`Bloom.validate`](/api/Bloom/validate) | Checks if a string is a valid bloom filter value. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Bloom.InvalidBloomError`](/api/Bloom/errors#bloominvalidbloomerror) | Thrown when a value is not a valid bloom filter. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Bloom.Prepared`](/api/Bloom/types#bloomprepared) | Prepared bloom filter for use with [`Bloom.containsPrepared`](/api/Bloom/containsPrepared)/[`Bloom.containsHash`](/api/Bloom/containsHash). | # Bloom.contains Checks if an input is matched in the bloom filter. ## Imports :::code-group ```ts [Named] import { Bloom } from 'ox' ``` ```ts [Entrypoint] import * as Bloom from 'ox/Bloom' ``` ::: ## Examples ```ts twoslash import { Bloom } from 'ox' Bloom.contains( '0x00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', '0xef2d6d194084c2de36e0dabfce45d046b37d1106' ) // @log: true ``` ## Definition ```ts function contains( bloom: Hex.Hex, input: Hex.Hex | Bytes.Bytes, ): boolean ``` **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L24) ## Parameters ### bloom * **Type:** `Hex.Hex` Bloom filter value. ### input * **Type:** `Hex.Hex | Bytes.Bytes` Input to check. ## Return Type Whether the input is matched in the bloom filter. `boolean` # Bloom.containsHash Checks if a precomputed `keccak256` hash is matched in a [`Bloom.Prepared`](/api/Bloom/types#prepared) bloom filter. Use when the caller already has the hash and wants to skip the keccak inside `containsPrepared`. ## Imports :::code-group ```ts [Named] import { Bloom } from 'ox' ``` ```ts [Entrypoint] import * as Bloom from 'ox/Bloom' ``` ::: ## Examples ```ts twoslash import { Bloom, Hash } from 'ox' const prepared = Bloom.prepare( '0x00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ) const hash = Hash.keccak256( '0xef2d6d194084c2de36e0dabfce45d046b37d1106', { as: 'Bytes' } ) Bloom.containsHash(prepared, hash) // @log: true ``` ## Definition ```ts function containsHash( prepared: Prepared, hash: Bytes.Bytes, ): boolean ``` **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L135) ## Parameters ### prepared * **Type:** [`Prepared`](/api/Bloom/types#bloomprepared) Prepared bloom filter. #### prepared.filter * **Type:** `Uint8Array` ### hash * **Type:** `Bytes.Bytes` Precomputed `keccak256` hash of the input, as `Bytes`. ## Return Type Whether the input is matched in the bloom filter. `boolean` # Bloom.containsPrepared Checks if an input is matched in a [`Bloom.Prepared`](/api/Bloom/types#prepared) bloom filter. ## Imports :::code-group ```ts [Named] import { Bloom } from 'ox' ``` ```ts [Entrypoint] import * as Bloom from 'ox/Bloom' ``` ::: ## Examples ```ts twoslash import { Bloom } from 'ox' const prepared = Bloom.prepare( '0x00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ) Bloom.containsPrepared( prepared, '0xef2d6d194084c2de36e0dabfce45d046b37d1106' ) // @log: true ``` ## Definition ```ts function containsPrepared( prepared: Prepared, input: Hex.Hex | Bytes.Bytes, ): boolean ``` **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L100) ## Parameters ### prepared * **Type:** [`Prepared`](/api/Bloom/types#bloomprepared) Prepared bloom filter. #### prepared.filter * **Type:** `Uint8Array` ### input * **Type:** `Hex.Hex | Bytes.Bytes` Input to check. ## Return Type Whether the input is matched in the bloom filter. `boolean` # Bloom.prepare Prepares a bloom filter for repeated membership checks against the same filter. Pairs with [`Bloom.containsPrepared`](/api/Bloom/containsPrepared) (or [`Bloom.containsHash`](/api/Bloom/containsHash)) to avoid the per-call hex-to-bytes conversion that [`Bloom.contains`](/api/Bloom/contains) pays. ## Imports :::code-group ```ts [Named] import { Bloom } from 'ox' ``` ```ts [Entrypoint] import * as Bloom from 'ox/Bloom' ``` ::: ## Examples ```ts twoslash import { Bloom } from 'ox' const prepared = Bloom.prepare( '0x00000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000008000000001000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ) Bloom.containsPrepared( prepared, '0xef2d6d194084c2de36e0dabfce45d046b37d1106' ) // @log: true ``` ## Definition ```ts function prepare( bloom: Hex.Hex, ): Prepared ``` **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L67) ## Parameters ### bloom * **Type:** `Hex.Hex` Bloom filter value. ## Return Type A prepared bloom filter. [`Prepared`](/api/Bloom/types#bloomprepared) # Bloom.validate Checks if a string is a valid bloom filter value. ## Imports :::code-group ```ts [Named] import { Bloom } from 'ox' ``` ```ts [Entrypoint] import * as Bloom from 'ox/Bloom' ``` ::: ## Examples ```ts twoslash import { Bloom } from 'ox' Bloom.validate('0x') // @log: false Bloom.validate( '0x00000000000000000000008000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000044000200000000000000000002000000000000000000000040000000000000000000000000000020000000000000000000800000000000800000000000800000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000808002000000000400000000000000000000000060000000000000000000000000000000000000000000000100000000000002000000' ) // @log: true ``` ## Definition ```ts function validate( value: string, ): value is Hex.Hex ``` **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L168) ## Parameters ### value * **Type:** `string` Value to check. ## Return Type Whether the value is a valid bloom filter. `value is Hex.Hex` # Bloom Errors ## `Bloom.InvalidBloomError` Thrown when a value is not a valid bloom filter. **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L180) # Bloom Types ## `Bloom.Prepared` Prepared bloom filter for use with [`Bloom.containsPrepared`](/api/Bloom/containsPrepared)/[`Bloom.containsHash`](/api/Bloom/containsHash). **Source:** [src/core/Bloom.ts](https://github.com/wevm/ox/blob/main/src/core/Bloom.ts#L40) # Fee Utility types for working with Ethereum transaction fees and fee history. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Fee.effectiveGasPrice`](/api/Fee/effectiveGasPrice) | Computes the effective gas price an EIP-1559 transaction will pay: | | [`Fee.estimateMaxFeePerGas`](/api/Fee/estimateMaxFeePerGas) | Estimates a `maxFeePerGas` from a base fee, a priority tip, and a multiplier applied to the base fee: | | [`Fee.fromHistoryRpc`](/api/Fee/fromHistoryRpc) | Converts a [`Fee.FeeHistoryRpc`](/api/Fee/types#feehistoryrpc) to a [`Fee.FeeHistory`](/api/Fee/types#feehistory). | | [`Fee.toHistoryRpc`](/api/Fee/toHistoryRpc) | Converts a [`Fee.FeeHistory`](/api/Fee/types#feehistory) to a [`Fee.FeeHistoryRpc`](/api/Fee/types#feehistoryrpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Fee.FeeHistory`](/api/Fee/types#feefeehistory) | | | [`Fee.FeeHistoryRpc`](/api/Fee/types#feefeehistoryrpc) | | | [`Fee.FeeValues`](/api/Fee/types#feefeevalues) | | | [`Fee.FeeValuesEip1559`](/api/Fee/types#feefeevalueseip1559) | | | [`Fee.FeeValuesEip1559Rpc`](/api/Fee/types#feefeevalueseip1559rpc) | | | [`Fee.FeeValuesEip4844`](/api/Fee/types#feefeevalueseip4844) | | | [`Fee.FeeValuesEip4844Rpc`](/api/Fee/types#feefeevalueseip4844rpc) | | | [`Fee.FeeValuesLegacy`](/api/Fee/types#feefeevalueslegacy) | | | [`Fee.FeeValuesLegacyRpc`](/api/Fee/types#feefeevalueslegacyrpc) | | | [`Fee.FeeValuesRpc`](/api/Fee/types#feefeevaluesrpc) | | | [`Fee.FeeValuesType`](/api/Fee/types#feefeevaluestype) | | # Fee.effectiveGasPrice Computes the effective gas price an EIP-1559 transaction will pay: ``` effective = min(maxFeePerGas, baseFeePerGas + maxPriorityFeePerGas) ``` ## Imports :::code-group ```ts [Named] import { Fee } from 'ox' ``` ```ts [Entrypoint] import * as Fee from 'ox/Fee' ``` ::: ## Examples ```ts twoslash import { Fee } from 'ox' Fee.effectiveGasPrice({ baseFeePerGas: 100n, maxFeePerGas: 200n, maxPriorityFeePerGas: 50n }) // @log: 150n (= 100n + 50n) Fee.effectiveGasPrice({ baseFeePerGas: 100n, maxFeePerGas: 120n, maxPriorityFeePerGas: 50n }) // @log: 120n (capped at maxFeePerGas) ``` ## Definition ```ts function effectiveGasPrice( args: { baseFeePerGas: bigint; maxFeePerGas: bigint; maxPriorityFeePerGas: bigint; }, ): bigint ``` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L225) ## Parameters ### args * **Type:** `{ baseFeePerGas: bigint; maxFeePerGas: bigint; maxPriorityFeePerGas: bigint; }` ## Return Type Effective gas price (in wei). `bigint` # Fee.estimateMaxFeePerGas Estimates a `maxFeePerGas` from a base fee, a priority tip, and a multiplier applied to the base fee: ``` maxFeePerGas = baseFeePerGas * multiplier + maxPriorityFeePerGas ``` The multiplier is supplied as `multiplierNumerator / multiplierDenominator` to keep the math in `bigint`. The default is `2 / 1` (i.e. 2x), matching the common wallet/relay heuristic for headroom against base-fee bumps. ## Imports :::code-group ```ts [Named] import { Fee } from 'ox' ``` ```ts [Entrypoint] import * as Fee from 'ox/Fee' ``` ::: ## Examples ```ts twoslash import { Fee } from 'ox' Fee.estimateMaxFeePerGas({ baseFeePerGas: 100n, maxPriorityFeePerGas: 5n }) // @log: 205n Fee.estimateMaxFeePerGas({ baseFeePerGas: 100n, maxPriorityFeePerGas: 5n, multiplierNumerator: 3n, multiplierDenominator: 2n }) // @log: 155n (= 100n * 3n / 2n + 5n) ``` ## Definition ```ts function estimateMaxFeePerGas( args: { baseFeePerGas: bigint; maxPriorityFeePerGas: bigint; multiplierNumerator?: bigint | undefined; multiplierDenominator?: bigint | undefined; }, ): bigint ``` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L172) ## Parameters ### args * **Type:** `{ baseFeePerGas: bigint; maxPriorityFeePerGas: bigint; multiplierNumerator?: bigint | undefined; multiplierDenominator?: bigint | undefined; }` ## Return Type Suggested `maxFeePerGas`. `bigint` # Fee.fromHistoryRpc Converts a [`Fee.FeeHistoryRpc`](/api/Fee/types#feehistoryrpc) to a [`Fee.FeeHistory`](/api/Fee/types#feehistory). ## Imports :::code-group ```ts [Named] import { Fee } from 'ox' ``` ```ts [Entrypoint] import * as Fee from 'ox/Fee' ``` ::: ## Examples ```ts twoslash import { Fee } from 'ox' const history = Fee.fromHistoryRpc({ baseFeePerGas: ['0x01', '0x02'], gasUsedRatio: [0.5, 0.6], oldestBlock: '0x10', reward: [['0x01']] }) // @log: { baseFeePerGas: [1n, 2n], gasUsedRatio: [0.5, 0.6], oldestBlock: 16n, reward: [[1n]] } ``` ## Definition ```ts function fromHistoryRpc( history: FeeHistoryRpc, ): FeeHistory ``` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L79) ## Parameters ### history * **Type:** `FeeHistoryRpc` The RPC fee history to convert. ## Return Type An instantiated [`Fee.FeeHistory`](/api/Fee/types#feehistory). `FeeHistory` # Fee.toHistoryRpc Converts a [`Fee.FeeHistory`](/api/Fee/types#feehistory) to a [`Fee.FeeHistoryRpc`](/api/Fee/types#feehistoryrpc). ## Imports :::code-group ```ts [Named] import { Fee } from 'ox' ``` ```ts [Entrypoint] import * as Fee from 'ox/Fee' ``` ::: ## Examples ```ts twoslash import { Fee } from 'ox' const rpc = Fee.toHistoryRpc({ baseFeePerGas: [1n, 2n], gasUsedRatio: [0.5, 0.6], oldestBlock: 16n, reward: [[1n]] }) ``` ## Definition ```ts function toHistoryRpc( history: FeeHistory, ): FeeHistoryRpc ``` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L116) ## Parameters ### history * **Type:** `FeeHistory` The fee history to convert. #### history.baseFeePerGas * **Type:** `bigintType[]` An array of block base fees per gas (in wei). This includes the next block after the newest of the returned range, because this value can be derived from the newest block. Zeroes are returned for pre-EIP-1559 blocks. #### history.gasUsedRatio * **Type:** `number[]` An array of block gas used ratios. These are calculated as the ratio of gasUsed and gasLimit. #### history.oldestBlock * **Type:** `bigintType` Lowest number block of the returned range. #### history.reward * **Type:** `bigintType[][]` * **Optional** An array of effective priority fees (in wei) per gas data points from a single block. All zeroes are returned if the block is empty. ## Return Type An RPC fee history. `FeeHistoryRpc` # Fee Types ## `Fee.FeeHistory` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L5) ## `Fee.FeeHistoryRpc` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L19) ## `Fee.FeeValues` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L48) ## `Fee.FeeValuesEip1559` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L28) ## `Fee.FeeValuesEip1559Rpc` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L35) ## `Fee.FeeValuesEip4844` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L37) ## `Fee.FeeValuesEip4844Rpc` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L46) ## `Fee.FeeValuesLegacy` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L21) ## `Fee.FeeValuesLegacyRpc` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L26) ## `Fee.FeeValuesRpc` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L54) ## `Fee.FeeValuesType` **Source:** [src/core/Fee.ts](https://github.com/wevm/ox/blob/main/src/core/Fee.ts#L58) # Filter Utilities & types for working with Filters as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.yaml) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Filter.fromRpc`](/api/Filter/fromRpc) | Converts a [`Filter.Rpc`](/api/Filter/types#rpc) to an [`Filter.Filter`](/api/Filter/types#filter). | | [`Filter.toRpc`](/api/Filter/toRpc) | Converts a [`Filter.Filter`](/api/Filter/types#filter) to a [`Filter.Rpc`](/api/Filter/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Filter.Filter`](/api/Filter/types#filterfilter) | A Filter as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.yaml). | | [`Filter.Rpc`](/api/Filter/types#filterrpc) | RPC representation of a [`Filter.Filter`](/api/Filter/types#filter). | | [`Filter.Topic`](/api/Filter/types#filtertopic) | A filter topic. | | [`Filter.Topics`](/api/Filter/types#filtertopics) | Set of Filter topics. | # Filter.fromRpc Converts a [`Filter.Rpc`](/api/Filter/types#rpc) to an [`Filter.Filter`](/api/Filter/types#filter). ## Imports :::code-group ```ts [Named] import { Filter } from 'ox' ``` ```ts [Entrypoint] import * as Filter from 'ox/Filter' ``` ::: ## Examples ```ts twoslash import { Filter } from 'ox' const filter = Filter.fromRpc({ address: '0xd3cda913deb6f67967b99d671a681250403edf27', fromBlock: 'latest', toBlock: '0x010f2c', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', null, '0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1' ] }) // @log: { // @log: address: '0xd3cda913deb6f67967b99d671a681250403edf27', // @log: fromBlock: 'latest', // @log: toBlock: 69420n, // @log: topics: [ // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', // @log: null, // @log: '0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1', // @log: ], // @log: } ``` ## Definition ```ts function fromRpc( filter: Rpc, ): Filter ``` **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L78) ## Parameters ### filter * **Type:** [`Rpc`](/api/Filter/types#filterrpc) The RPC filter to convert. ## Return Type An instantiated [`Filter.Filter`](/api/Filter/types#filter). `Filter` # Filter.toRpc Converts a [`Filter.Filter`](/api/Filter/types#filter) to a [`Filter.Rpc`](/api/Filter/types#rpc). ## Imports :::code-group ```ts [Named] import { Filter } from 'ox' ``` ```ts [Entrypoint] import * as Filter from 'ox/Filter' ``` ::: ## Examples ```ts twoslash import { AbiEvent, Filter } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed, address indexed, uint256)' ) const { topics } = AbiEvent.encode(transfer) const filter = Filter.toRpc({ address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', topics }) // @log: { // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: topics: [ // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', // @log: ], // @log: } ``` ## Definition ```ts function toRpc( filter: toRpc.Input, ): Rpc ``` **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L126) ## Parameters ### filter * **Type:** `toRpc.Input` The filter to convert. ## Return Type An RPC filter. [`Rpc`](/api/Filter/types#filterrpc) # Filter Types ## `Filter.Filter` A Filter as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.yaml). **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L8) ## `Filter.Rpc` RPC representation of a [`Filter.Filter`](/api/Filter/types#filter). **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L32) ## `Filter.Topic` A filter topic. * `null`: Matches any topic. - `Hex`: Matches if the topic is equal. - `Hex[]`: Matches if the topic is in the array. **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L44) ## `Filter.Topics` Set of Filter topics. **Source:** [src/core/Filter.ts](https://github.com/wevm/ox/blob/main/src/core/Filter.ts#L35) # Log Utilities & types for working with Logs as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml) :::tip Utilities for Log encoding & decoding can be found on the [`AbiEvent`](/api/) module. ::: ## Examples ### Converting from RPC Format Logs can be converted from their RPC format using [`Log.fromRpc`](/api/Log/fromRpc): ```ts twoslash import 'ox/window' import { AbiEvent, Hex, Log } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 indexed value)' ) const { topics } = AbiEvent.encode(transfer) const logs = await window.ethereum!.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: Hex.fromNumber(19760235n), toBlock: Hex.fromNumber(19760240n), topics } ] }) const log = Log.fromRpc(logs[0]) // [!code focus] // @log: { // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // @log: blockNumber: 19760236n, // @log: data: '0x', // @log: logIndex: 271, // @log: removed: false, // @log: topics: [ // @log: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // @log: "0x0000000000000000000000000000000000000000000000000000000000000000", // @log: "0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1", // @log: "0x000000000000000000000000000000000000000000000000000000000000025b", // @log: transactionHash: // @log: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', // @log: transactionIndex: 145, // @log: } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Log.fromRpc`](/api/Log/fromRpc) | Converts a [`Log.Rpc`](/api/Log/types#rpc) to an [`Log.Log`](/api/Log/types#log). | | [`Log.toRpc`](/api/Log/toRpc) | Converts a [`Log.Log`](/api/Log/types#log) to a [`Log.Rpc`](/api/Log/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Log.Log`](/api/Log/types#loglog) | A Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). | | [`Log.Rpc`](/api/Log/types#logrpc) | An RPC Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). | # Log.fromRpc Converts a [`Log.Rpc`](/api/Log/types#rpc) to an [`Log.Log`](/api/Log/types#log). ## Imports :::code-group ```ts [Named] import { Log } from 'ox' ``` ```ts [Entrypoint] import * as Log from 'ox/Log' ``` ::: ## Examples ```ts twoslash import { Log } from 'ox' const log = Log.fromRpc({ address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x0000000000000000000000000000000000000000000000000000000000000000', '0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1', '0x000000000000000000000000000000000000000000000000000000000000025b' ], data: '0x', blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', blockNumber: '0x12d846c', transactionHash: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', transactionIndex: '0x91', logIndex: '0x10f', removed: false }) // @log: { // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // @log: blockNumber: 19760236n, // @log: data: '0x', // @log: logIndex: 271, // @log: removed: false, // @log: topics: [ // @log: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // @log: "0x0000000000000000000000000000000000000000000000000000000000000000", // @log: "0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1", // @log: "0x000000000000000000000000000000000000000000000000000000000000025b", // @log: transactionHash: // @log: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', // @log: transactionIndex: 145, // @log: } ``` ### End-to-end Below is an example of how to use `Log.fromRpc` to instantiate a [`Log.Log`](/api/Log/types#log) from an RPC log. ```ts twoslash import 'ox/window' import { AbiEvent, Hex, Log } from 'ox' const transfer = AbiEvent.from( 'event Transfer(address indexed from, address indexed to, uint256 indexed value)' ) const { topics } = AbiEvent.encode(transfer) const logs = await window.ethereum!.request({ method: 'eth_getLogs', params: [ { address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', fromBlock: Hex.fromNumber(19760235n), toBlock: Hex.fromNumber(19760240n), topics } ] }) const log = Log.fromRpc(logs[0]) // [!code focus] // @log: { // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // @log: blockNumber: 19760236n, // @log: data: '0x', // @log: logIndex: 271, // @log: removed: false, // @log: topics: [ // @log: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", // @log: "0x0000000000000000000000000000000000000000000000000000000000000000", // @log: "0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1", // @log: "0x000000000000000000000000000000000000000000000000000000000000025b", // @log: transactionHash: // @log: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', // @log: transactionIndex: 145, // @log: } ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function fromRpc( log: log | Rpc, _options?: fromRpc.Options, ): Log ``` **Source:** [src/core/Log.ts](https://github.com/wevm/ox/blob/main/src/core/Log.ts#L141) ## Parameters ### log * **Type:** `log | Rpc` The RPC log to convert. ## Return Type An instantiated [`Log.Log`](/api/Log/types#log). `Log` # Log.toRpc Converts a [`Log.Log`](/api/Log/types#log) to a [`Log.Rpc`](/api/Log/types#rpc). ## Imports :::code-group ```ts [Named] import { Log } from 'ox' ``` ```ts [Entrypoint] import * as Log from 'ox/Log' ``` ::: ## Examples ```ts twoslash import { Log } from 'ox' const log = Log.toRpc({ address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', blockNumber: 19760236n, data: '0x', logIndex: 271, removed: false, topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x0000000000000000000000000000000000000000000000000000000000000000', '0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1', '0x000000000000000000000000000000000000000000000000000000000000025b' ], transactionHash: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', transactionIndex: 145 }) // @log: { // @log: address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2', // @log: blockHash: '0xabe69134e80a12f6a93d0aa18215b5b86c2fb338bae911790ca374a8716e01a4', // @log: blockNumber: '0x012d846c', // @log: data: '0x', // @log: logIndex: '0x010f', // @log: removed: false, // @log: topics: [ // @log: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', // @log: '0x0000000000000000000000000000000000000000000000000000000000000000', // @log: '0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1', // @log: '0x000000000000000000000000000000000000000000000000000000000000025b', // @log: ], // @log: transactionHash: // @log: '0xcfa52db0bc2cb5bdcb2c5bd8816df7a2f018a0e3964ab1ef4d794cf327966e93', // @log: transactionIndex: '0x91', // @log: } ``` ## Definition ```ts function toRpc( log: log, _options?: toRpc.Options, ): Rpc ``` **Source:** [src/core/Log.ts](https://github.com/wevm/ox/blob/main/src/core/Log.ts#L219) ## Parameters ### log * **Type:** `log` The log to convert. ## Return Type An RPC log. `Rpc` # Log Types ## `Log.Log` A Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). **Source:** [src/core/Log.ts](https://github.com/wevm/ox/blob/main/src/core/Log.ts#L8) ## `Log.Rpc` An RPC Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). **Source:** [src/core/Log.ts](https://github.com/wevm/ox/blob/main/src/core/Log.ts#L36) # StateOverrides Utilities & types for working with **State Overrides**. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`StateOverrides.fromRpc`](/api/StateOverrides/fromRpc) | Converts an [`StateOverrides.Rpc`](/api/StateOverrides/types#rpc) to an [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverrides). | | [`StateOverrides.toRpc`](/api/StateOverrides/toRpc) | Converts an [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverrides) to an [`StateOverrides.Rpc`](/api/StateOverrides/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`StateOverrides.AccountOverrides`](/api/StateOverrides/types#stateoverridesaccountoverrides) | Details of an account to be overridden. | | [`StateOverrides.AccountStorage`](/api/StateOverrides/types#stateoverridesaccountstorage) | Key-value mapping to override all slots in the account storage before executing the call. | | [`StateOverrides.Rpc`](/api/StateOverrides/types#stateoverridesrpc) | RPC state overrides. | | [`StateOverrides.RpcAccountOverrides`](/api/StateOverrides/types#stateoverridesrpcaccountoverrides) | RPC account overrides. | | [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverridesstateoverrides) | State override set to specify state to be ephemerally overridden prior to executing a call. | # StateOverrides.fromRpc Converts an [`StateOverrides.Rpc`](/api/StateOverrides/types#rpc) to an [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverrides). ## Imports :::code-group ```ts [Named] import { StateOverrides } from 'ox' ``` ```ts [Entrypoint] import * as StateOverrides from 'ox/StateOverrides' ``` ::: ## Examples ```ts twoslash import { StateOverrides } from 'ox' const stateOverrides = StateOverrides.fromRpc({ '0x0000000000000000000000000000000000000000': { balance: '0x1' } }) ``` ## Definition ```ts function fromRpc( rpcStateOverrides: Rpc, ): StateOverrides ``` **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L72) ## Parameters ### rpcStateOverrides * **Type:** `Rpc` The RPC state overrides to convert. ## Return Type An instantiated [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverrides). `StateOverrides` # StateOverrides.toRpc Converts an [`StateOverrides.StateOverrides`](/api/StateOverrides/types#stateoverrides) to an [`StateOverrides.Rpc`](/api/StateOverrides/types#rpc). ## Imports :::code-group ```ts [Named] import { StateOverrides } from 'ox' ``` ```ts [Entrypoint] import * as StateOverrides from 'ox/StateOverrides' ``` ::: ## Examples ```ts twoslash import { StateOverrides } from 'ox' const stateOverrides = StateOverrides.toRpc({ '0x0000000000000000000000000000000000000000': { balance: 1n } }) ``` ## Definition ```ts function toRpc( stateOverrides: toRpc.Input, ): Rpc ``` **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L113) ## Parameters ### stateOverrides * **Type:** `toRpc.Input` The state overrides to convert. ## Return Type An instantiated [`StateOverrides.Rpc`](/api/StateOverrides/types#rpc). `Rpc` # StateOverrides Types ## `StateOverrides.AccountOverrides` Details of an account to be overridden. **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L21) ## `StateOverrides.AccountStorage` Key-value mapping to override all slots in the account storage before executing the call. **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L51) ## `StateOverrides.Rpc` RPC state overrides. **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L16) ## `StateOverrides.RpcAccountOverrides` RPC account overrides. **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L46) ## `StateOverrides.StateOverrides` State override set to specify state to be ephemerally overridden prior to executing a call. **Source:** [src/core/StateOverrides.ts](https://github.com/wevm/ox/blob/main/src/core/StateOverrides.ts#L9) # Transaction Utilities & types for working with **Transactions** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml) ## Examples ### Converting from RPC Format Transactions can be converted from RPC format using [`Transaction.fromRpc`](/api/Transaction/fromRpc): ```ts twoslash import { Transaction } from 'ox' const transaction = Transaction.fromRpc({ hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', nonce: '0x357', blockHash: '0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b', blockNumber: '0x12f296f', transactionIndex: '0x2', from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6', to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', value: '0x9b6e64a8ec60000', gas: '0x43f5d', maxFeePerGas: '0x2ca6ae494', maxPriorityFeePerGas: '0x41cc3c0', input: '0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006643504700000000000000000000000000000000000000000000000000000000000000040b080604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec600000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec60000000000000000000000000000000000000000000000000000019124bb5ae978c000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b80000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b8000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c00000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b800000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000190240001b9872b', r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0', chainId: '0x1', accessList: [], type: '0x2' }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Transaction.fromRpc`](/api/Transaction/fromRpc) | Converts an [`Transaction.Rpc`](/api/Transaction/types#rpc) to an [`Transaction.Transaction`](/api/Transaction/types#transaction). | | [`Transaction.toRpc`](/api/Transaction/toRpc) | Converts an [`Transaction.Transaction`](/api/Transaction/types#transaction) to an [`Transaction.Rpc`](/api/Transaction/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Transaction.Base`](/api/Transaction/types#transactionbase) | Base properties of a Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.BaseRpc`](/api/Transaction/types#transactionbaserpc) | Base properties of an RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip1559`](/api/Transaction/types#transactioneip1559) | An [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip1559Rpc`](/api/Transaction/types#transactioneip1559rpc) | An [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip2930`](/api/Transaction/types#transactioneip2930) | An [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip2930Rpc`](/api/Transaction/types#transactioneip2930rpc) | An RPC [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip4844`](/api/Transaction/types#transactioneip4844) | An [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip4844Rpc`](/api/Transaction/types#transactioneip4844rpc) | An RPC [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip7702`](/api/Transaction/types#transactioneip7702) | An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Eip7702Rpc`](/api/Transaction/types#transactioneip7702rpc) | An RPC [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.FromRpcType`](/api/Transaction/types#transactionfromrpctype) | RPC Type to Type mapping. | | [`Transaction.Legacy`](/api/Transaction/types#transactionlegacy) | An legacy Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.LegacyRpc`](/api/Transaction/types#transactionlegacyrpc) | A legacy RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.Rpc`](/api/Transaction/types#transactionrpc) | An RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | | [`Transaction.ToRpcType`](/api/Transaction/types#transactiontorpctype) | Type to RPC Type mapping. | | [`Transaction.Transaction`](/api/Transaction/types#transactiontransaction) | A Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). | # Transaction.fromRpc Converts an [`Transaction.Rpc`](/api/Transaction/types#rpc) to an [`Transaction.Transaction`](/api/Transaction/types#transaction). ## Imports :::code-group ```ts [Named] import { Transaction } from 'ox' ``` ```ts [Entrypoint] import * as Transaction from 'ox/Transaction' ``` ::: ## Examples ```ts twoslash import { Transaction } from 'ox' const transaction = Transaction.fromRpc({ hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', nonce: '0x357', blockHash: '0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b', blockNumber: '0x12f296f', transactionIndex: '0x2', from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6', to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', value: '0x9b6e64a8ec60000', gas: '0x43f5d', maxFeePerGas: '0x2ca6ae494', maxPriorityFeePerGas: '0x41cc3c0', input: '0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006643504700000000000000000000000000000000000000000000000000000000000000040b080604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec600000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec60000000000000000000000000000000000000000000000000000019124bb5ae978c000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b80000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b8000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c00000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b800000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000190240001b9872b', r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', yParity: '0x0', chainId: '0x1', accessList: [], type: '0x2' }) ``` ## Definition ```ts function fromRpc( transaction: transaction | Rpc | null, _options?: fromRpc.Options, ): transaction extends Rpc ? Transaction : null ``` **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L276) ## Parameters ### transaction * **Type:** `transaction | Rpc | null` The RPC transaction to convert. ## Return Type An instantiated [`Transaction.Transaction`](/api/Transaction/types#transaction). `transaction extends Rpc ? Transaction : null` # Transaction.toRpc Converts an [`Transaction.Transaction`](/api/Transaction/types#transaction) to an [`Transaction.Rpc`](/api/Transaction/types#rpc). ## Imports :::code-group ```ts [Named] import { Transaction } from 'ox' ``` ```ts [Entrypoint] import * as Transaction from 'ox/Transaction' ``` ::: ## Examples ```ts twoslash import { Transaction } from 'ox' const transaction = Transaction.toRpc({ accessList: [], blockHash: '0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b', blockNumber: 19868015n, chainId: 1, from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6', gas: 278365n, hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', input: '0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006643504700000000000000000000000000000000000000000000000000000000000000040b080604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec600000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec60000000000000000000000000000000000000000000000000000019124bb5ae978c000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b80000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b8000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c00000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b800000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000190240001b9872b', maxFeePerGas: 11985937556n, maxPriorityFeePerGas: 68993984n, nonce: 855n, r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d', s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540', to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', transactionIndex: 2, type: 'eip1559', v: 27, value: 700000000000000000n, yParity: 0 }) ``` ## Definition ```ts function toRpc( transaction: toRpc.Input, _options?: toRpc.Options, ): Rpc ``` **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L376) ## Parameters ### transaction * **Type:** `toRpc.Input` The transaction to convert. ## Return Type An RPC-formatted transaction. `Rpc` # Transaction Types ## `Transaction.Base` Base properties of a Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L43) ## `Transaction.BaseRpc` Base properties of an RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L88) ## `Transaction.Eip1559` An [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L94) ## `Transaction.Eip1559Rpc` An [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L113) ## `Transaction.Eip2930` An [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L118) ## `Transaction.Eip2930Rpc` An RPC [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L133) ## `Transaction.Eip4844` An [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L138) ## `Transaction.Eip4844Rpc` An RPC [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L159) ## `Transaction.Eip7702` An [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L164) ## `Transaction.Eip7702Rpc` An RPC [EIP-7702](https://eips.ethereum.org/EIPS/eip-7702) Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L183) ## `Transaction.FromRpcType` RPC Type to Type mapping. **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L238) ## `Transaction.Legacy` An legacy Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L188) ## `Transaction.LegacyRpc` A legacy RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L209) ## `Transaction.Rpc` An RPC Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L31) ## `Transaction.ToRpcType` Type to RPC Type mapping. **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L223) ## `Transaction.Transaction` A Transaction as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.yaml). **Source:** [src/core/Transaction.ts](https://github.com/wevm/ox/blob/main/src/core/Transaction.ts#L13) # TransactionReceipt Utilities & types for working with **Transaction Receipts** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml) ## Examples ### Converting from RPC Format Receipts can be converted from RPC format using [`TransactionReceipt.fromRpc`](/api/TransactionReceipt/fromRpc): ```ts twoslash import 'ox/window' import { TransactionReceipt } from 'ox' const receipt = await window .ethereum!.request({ method: 'eth_getTransactionReceipt', params: [ '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0' ] }) .then(TransactionReceipt.fromRpc) // [!code hl] // @log: { // @log: blobGasPrice: 270441n, // @log: blobGasUsed: 4919n, // @log: blockHash: "0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b", // @log: blockNumber: 19868015n, // @log: contractAddress: null, // @log: cumulativeGasUsed: 533781n, // @log: effectiveGasPrice: 9062804489n, // @log: from: "0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6", // @log: gasUsed: 175034n, // @log: logs: [], // @log: logsBloom: "0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000", // @log: root: undefined, // @log: status: "success", // @log: to: "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", // @log: transactionHash: "0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0", // @log: transactionIndex: 2, // @log: type: "eip1559", // @log: } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionReceipt.fromRpc`](/api/TransactionReceipt/fromRpc) | Converts a [`TransactionReceipt.Rpc`](/api/TransactionReceipt/types#rpc) to an [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt). | | [`TransactionReceipt.toRpc`](/api/TransactionReceipt/toRpc) | Converts a [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt) to a [`TransactionReceipt.Rpc`](/api/TransactionReceipt/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionReceipt.Rpc`](/api/TransactionReceipt/types#transactionreceiptrpc) | An RPC Transaction Receipt as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). | | [`TransactionReceipt.RpcStatus`](/api/TransactionReceipt/types#transactionreceiptrpcstatus) | Union of RPC Transaction Receipt statuses. | | [`TransactionReceipt.RpcType`](/api/TransactionReceipt/types#transactionreceiptrpctype) | Union of RPC Transaction Receipt types. | | [`TransactionReceipt.Status`](/api/TransactionReceipt/types#transactionreceiptstatus) | Union of Transaction Receipt statuses. | | [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipttransactionreceipt) | An Transaction Receipt as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). | | [`TransactionReceipt.Type`](/api/TransactionReceipt/types#transactionreceipttype) | Union of Transaction Receipt types. | # TransactionReceipt.fromRpc Converts a [`TransactionReceipt.Rpc`](/api/TransactionReceipt/types#rpc) to an [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt). ## Imports :::code-group ```ts [Named] import { TransactionReceipt } from 'ox' ``` ```ts [Entrypoint] import * as TransactionReceipt from 'ox/TransactionReceipt' ``` ::: ## Examples ```ts twoslash import { TransactionReceipt } from 'ox' const receipt = TransactionReceipt.fromRpc({ blobGasPrice: '0x42069', blobGasUsed: '0x1337', blockHash: '0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b', blockNumber: '0x12f296f', contractAddress: null, cumulativeGasUsed: '0x82515', effectiveGasPrice: '0x21c2f6c09', from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6', gasUsed: '0x2abba', logs: [], logsBloom: '0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000', status: '0x1', to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', transactionHash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', transactionIndex: '0x2', type: '0x2' }) // @log: { // @log: blobGasPrice: 270441n, // @log: blobGasUsed: 4919n, // @log: blockHash: "0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b", // @log: blockNumber: 19868015n, // @log: contractAddress: null, // @log: cumulativeGasUsed: 533781n, // @log: effectiveGasPrice: 9062804489n, // @log: from: "0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6", // @log: gasUsed: 175034n, // @log: logs: [], // @log: logsBloom: "0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000", // @log: root: undefined, // @log: status: "success", // @log: to: "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", // @log: transactionHash: "0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0", // @log: transactionIndex: 2, // @log: type: "eip1559", // @log: } ``` ### End-to-end Below is an example of how to use the `TransactionReceipt.fromRpc` method to convert an RPC transaction receipt to a [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt) object. ```ts twoslash import 'ox/window' import { TransactionReceipt } from 'ox' const receipt = await window .ethereum!.request({ method: 'eth_getTransactionReceipt', params: [ '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0' ] }) .then(TransactionReceipt.fromRpc) // [!code hl] // @log: { // @log: blobGasPrice: 270441n, // @log: blobGasUsed: 4919n, // @log: blockHash: "0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b", // @log: blockNumber: 19868015n, // @log: contractAddress: null, // @log: cumulativeGasUsed: 533781n, // @log: effectiveGasPrice: 9062804489n, // @log: from: "0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6", // @log: gasUsed: 175034n, // @log: logs: [], // @log: logsBloom: "0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000", // @log: root: undefined, // @log: status: "success", // @log: to: "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", // @log: transactionHash: "0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0", // @log: transactionIndex: 2, // @log: type: "eip1559", // @log: } ``` :::note For simplicity, the above example uses `window.ethereum.request`, but you can use any type of JSON-RPC interface. ::: ## Definition ```ts function fromRpc( receipt: receipt | Rpc | null, ): receipt extends Rpc ? TransactionReceipt : null ``` **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L227) ## Parameters ### receipt * **Type:** `receipt | Rpc | null` The RPC receipt to convert. ## Return Type An instantiated [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt). `receipt extends Rpc ? TransactionReceipt : null` # TransactionReceipt.toRpc Converts a [`TransactionReceipt.TransactionReceipt`](/api/TransactionReceipt/types#transactionreceipt) to a [`TransactionReceipt.Rpc`](/api/TransactionReceipt/types#rpc). ## Imports :::code-group ```ts [Named] import { TransactionReceipt } from 'ox' ``` ```ts [Entrypoint] import * as TransactionReceipt from 'ox/TransactionReceipt' ``` ::: ## Examples ```ts twoslash import { TransactionReceipt } from 'ox' const receipt = TransactionReceipt.toRpc({ blobGasPrice: 270441n, blobGasUsed: 4919n, blockHash: '0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b', blockNumber: 19868015n, contractAddress: null, cumulativeGasUsed: 533781n, effectiveGasPrice: 9062804489n, from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6', gasUsed: 175034n, logs: [], logsBloom: '0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000', root: undefined, status: 'success', to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad', transactionHash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0', transactionIndex: 2, type: 'eip1559' }) // @log: { // @log: blobGasPrice: "0x042069", // @log: blobGasUsed: "0x1337", // @log: blockHash: "0xc350d807505fb835650f0013632c5515592987ba169bbc6626d9fc54d91f0f0b", // @log: blockNumber: "0x012f296f", // @log: contractAddress: null, // @log: cumulativeGasUsed: "0x082515", // @log: effectiveGasPrice: "0x021c2f6c09", // @log: from: "0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6", // @log: gasUsed: "0x02abba", // @log: logs: [], // @log: logsBloom: "0x00200000000000000000008080000000000000000040000000000000000000000000000000000000000000000000000022000000080000000000000000000000000000080000000000000008000000200000000000000000000200008020400000000000000000280000000000100000000000000000000000000010000000000000000000020000000000000020000000000001000000080000004000000000000000000000000000000000000000000000400000000000001000000000000000000002000000000000000020000000000000000000001000000000000000000000200000000000000000000000000000001000000000c00000000000000000", // @log: root: undefined, // @log: status: "0x1", // @log: to: "0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad", // @log: transactionHash: "0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0", // @log: transactionIndex: "0x02", // @log: type: "eip1559", // @log: } ``` ## Definition ```ts function toRpc( receipt: toRpc.Input, ): Rpc ``` **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L306) ## Parameters ### receipt * **Type:** `toRpc.Input` The receipt to convert. ## Return Type An RPC receipt. `Rpc` # TransactionReceipt Types ## `TransactionReceipt.Rpc` An RPC Transaction Receipt as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L52) ## `TransactionReceipt.RpcStatus` Union of RPC Transaction Receipt statuses. * `0x0` - `0x1` **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L68) ## `TransactionReceipt.RpcType` Union of RPC Transaction Receipt types. * `0x0`: legacy transactions - `0x1`: EIP-1559 transactions - `0x2`: EIP-2930 transactions - `0x3`: EIP-4844 transactions - `0x4`: EIP-7702 transactions - any other string **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L98) ## `TransactionReceipt.Status` Union of Transaction Receipt statuses. * `success` - `reverted` **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L60) ## `TransactionReceipt.TransactionReceipt` An Transaction Receipt as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L9) ## `TransactionReceipt.Type` Union of Transaction Receipt types. * `legacy` - `eip1559` - `eip2930` - `eip4844` - `eip7702` - any other string **Source:** [src/core/TransactionReceipt.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionReceipt.ts#L80) # TransactionRequest Utilities & types for working with **Transaction Requests** as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4aca1d7a3e5aab24c8f6437131289ad386944eaa/src/schemas/transaction.yaml#L358-L423) ## Examples ```ts twoslash import 'ox/window' import { Provider, TransactionRequest, Value } from 'ox' const provider = Provider.from(window.ethereum!) const request = TransactionRequest.toRpc({ // [!code focus] to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus] value: Value.fromEther('0.01') // [!code focus] }) // [!code focus] const hash = await provider.request({ method: 'eth_sendTransaction', params: [request] }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionRequest.fromRpc`](/api/TransactionRequest/fromRpc) | Converts a [`TransactionRequest.Rpc`](/api/TransactionRequest/types#rpc) to a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). | | [`TransactionRequest.toEnvelope`](/api/TransactionRequest/toEnvelope) | Converts a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) to a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). | | [`TransactionRequest.toRpc`](/api/TransactionRequest/toRpc) | Converts a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) to a [`TransactionRequest.Rpc`](/api/TransactionRequest/types#rpc). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionRequest.MissingAuthorizationListError`](/api/TransactionRequest/errors#transactionrequestmissingauthorizationlisterror) | Thrown when a 7702 conversion is requested but no `authorizationList` is provided. | | [`TransactionRequest.MissingKzgError`](/api/TransactionRequest/errors#transactionrequestmissingkzgerror) | Thrown when a 4844 conversion is requested but no `kzg` context is provided. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionRequest.Rpc`](/api/TransactionRequest/types#transactionrequestrpc) | RPC representation of a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). | | [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequesttransactionrequest) | A Transaction Request that is generic to all transaction types, as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4aca1d7a3e5aab24c8f6437131289ad386944eaa/src/schemas/transaction.yaml#L358-L423). | # TransactionRequest.fromRpc Converts a [`TransactionRequest.Rpc`](/api/TransactionRequest/types#rpc) to a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). ## Imports :::code-group ```ts [Named] import { TransactionRequest } from 'ox' ``` ```ts [Entrypoint] import * as TransactionRequest from 'ox/TransactionRequest' ``` ::: ## Examples ```ts twoslash import { TransactionRequest } from 'ox' const request = TransactionRequest.fromRpc({ to: '0x0000000000000000000000000000000000000000', value: '0x2386f26fc10000' }) ``` ## Definition ```ts function fromRpc( request: Rpc, ): TransactionRequest ``` **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L85) ## Parameters ### request * **Type:** `Rpc` The RPC request to convert. ## Return Type A transaction request. `TransactionRequest` # TransactionRequest.toEnvelope Converts a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) to a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). Dispatches to the correct concrete envelope type via [`TransactionEnvelope.getType`](/api/TransactionEnvelope/getType) (using `request.type` when present, otherwise inferring from fee/blob/authorization fields), and drops fields that do not belong to the chosen type. For EIP-4844, if `blobs` is provided without `sidecars`, sidecars and `blobVersionedHashes` are derived via the `kzg` option. If `sidecars` is already provided, it is passed through unchanged. Inputs are expected to be in canonical form (`bigint` numerics and `'eip1559'`-style `type` strings). Pass RPC-shaped payloads through [`TransactionRequest.fromRpc`](/api/TransactionRequest/fromRpc) first. ## Imports :::code-group ```ts [Named] import { TransactionRequest } from 'ox' ``` ```ts [Entrypoint] import * as TransactionRequest from 'ox/TransactionRequest' ``` ::: ## Examples ```ts twoslash import { TransactionRequest } from 'ox' const envelope = TransactionRequest.toEnvelope({ chainId: 1, maxFeePerGas: 1n, to: '0x0000000000000000000000000000000000000000', value: 1n }) // @log: { // @log: chainId: 1, // @log: maxFeePerGas: 1n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip1559', // @log: value: 1n, // @log: } ``` ## Definition ```ts function toEnvelope( request: TransactionRequest, options?: toEnvelope.Options, ): TxEnvelope.TxEnvelope ``` **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L277) ## Parameters ### request * **Type:** `TransactionRequest` The transaction request to convert. #### request.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### request.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` * **Optional** EIP-7702 Authorization List. #### request.blobVersionedHashes * **Type:** `readonly 0x${string}[]` * **Optional** Versioned hashes of blobs to be included in the transaction. #### request.blobs * **Type:** `readonly 0x${string}[]` * **Optional** Raw blob data. #### request.chainId * **Type:** `numberType` * **Optional** EIP-155 Chain ID. #### request.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### request.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. #### request.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### request.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### request.input * **Type:** `0x${string}` * **Optional** #### request.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### request.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### request.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### request.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### request.r * **Type:** `0x${string}` * **Optional** ECDSA signature r. #### request.s * **Type:** `0x${string}` * **Optional** ECDSA signature s. #### request.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### request.type * **Type:** `type` * **Optional** Transaction type #### request.v * **Type:** `numberType` * **Optional** #### request.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### request.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `toEnvelope.Options` * **Optional** Options. #### options.kzg * **Type:** `Kzg` * **Optional** KZG context used to derive EIP-4844 `sidecars` and `blobVersionedHashes` from raw `blobs`. Required when `blobs` is provided without `sidecars` or `blobVersionedHashes`. ## Return Type A transaction envelope. `TxEnvelope.TxEnvelope` # TransactionRequest.toRpc Converts a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) to a [`TransactionRequest.Rpc`](/api/TransactionRequest/types#rpc). ## Imports :::code-group ```ts [Named] import { TransactionRequest } from 'ox' ``` ```ts [Entrypoint] import * as TransactionRequest from 'ox/TransactionRequest' ``` ::: ## Examples ```ts twoslash import { TransactionRequest, Value } from 'ox' const request = TransactionRequest.toRpc({ to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('0.01') }) ``` ### Using with a Provider You can use [`Provider.from`](/api/Provider/from) to instantiate an EIP-1193 Provider and send a transaction to the Wallet using the `eth_sendTransaction` method. ```ts twoslash import 'ox/window' import { Provider, TransactionRequest, Value } from 'ox' const provider = Provider.from(window.ethereum!) const request = TransactionRequest.toRpc({ to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('0.01') }) const hash = await provider.request({ // [!code focus] method: 'eth_sendTransaction', // [!code focus] params: [request] // [!code focus] }) // [!code focus] ``` ## Definition ```ts function toRpc( request: toRpc.Input, ): Rpc ``` **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L168) ## Parameters ### request * **Type:** `toRpc.Input` The request to convert. ## Return Type An RPC request. `Rpc` # TransactionRequest Errors ## `TransactionRequest.MissingAuthorizationListError` Thrown when a 7702 conversion is requested but no `authorizationList` is provided. **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L454) ## `TransactionRequest.MissingKzgError` Thrown when a 4844 conversion is requested but no `kzg` context is provided. **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L441) # TransactionRequest Types ## `TransactionRequest.Rpc` RPC representation of a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L67) ## `TransactionRequest.TransactionRequest` A Transaction Request that is generic to all transaction types, as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/4aca1d7a3e5aab24c8f6437131289ad386944eaa/src/schemas/transaction.yaml#L358-L423). **Source:** [src/core/TransactionRequest.ts](https://github.com/wevm/ox/blob/main/src/core/TransactionRequest.ts#L15) # Withdrawal Utilities & types for working with Withdrawals as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Withdrawal.fromRpc`](/api/Withdrawal/fromRpc) | Converts a [`Withdrawal.Rpc`](/api/Withdrawal/types#rpc) to an [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawal). | | [`Withdrawal.toRpc`](/api/Withdrawal/toRpc) | Converts a [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawal) to an [`Withdrawal.Rpc`](/api/Withdrawal/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Withdrawal.Rpc`](/api/Withdrawal/types#withdrawalrpc) | An RPC Withdrawal as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml). | | [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawalwithdrawal) | A Withdrawal as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml). | # Withdrawal.fromRpc Converts a [`Withdrawal.Rpc`](/api/Withdrawal/types#rpc) to an [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawal). ## Imports :::code-group ```ts [Named] import { Withdrawal } from 'ox' ``` ```ts [Entrypoint] import * as Withdrawal from 'ox/Withdrawal' ``` ::: ## Examples ```ts twoslash import { Withdrawal } from 'ox' const withdrawal = Withdrawal.fromRpc({ address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', amount: '0x620323', index: '0x0', validatorIndex: '0x1' }) // @log: { // @log: address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', // @log: amount: 6423331n, // @log: index: 0, // @log: validatorIndex: 1 // @log: } ``` ## Definition ```ts function fromRpc( withdrawal: Rpc, ): Withdrawal ``` **Source:** [src/core/Withdrawal.ts](https://github.com/wevm/ox/blob/main/src/core/Withdrawal.ts#L40) ## Parameters ### withdrawal * **Type:** `Rpc` The RPC withdrawal to convert. ## Return Type An instantiated [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawal). `Withdrawal` # Withdrawal.toRpc Converts a [`Withdrawal.Withdrawal`](/api/Withdrawal/types#withdrawal) to an [`Withdrawal.Rpc`](/api/Withdrawal/types#rpc). ## Imports :::code-group ```ts [Named] import { Withdrawal } from 'ox' ``` ```ts [Entrypoint] import * as Withdrawal from 'ox/Withdrawal' ``` ::: ## Examples ```ts twoslash import { Withdrawal } from 'ox' const withdrawal = Withdrawal.toRpc({ address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', amount: 6423331n, index: 0, validatorIndex: 1 }) // @log: { // @log: address: '0x00000000219ab540356cBB839Cbe05303d7705Fa', // @log: amount: '0x620323', // @log: index: '0x0', // @log: validatorIndex: '0x1', // @log: } ``` ## Definition ```ts function toRpc( withdrawal: toRpc.Input, ): Rpc ``` **Source:** [src/core/Withdrawal.ts](https://github.com/wevm/ox/blob/main/src/core/Withdrawal.ts#L77) ## Parameters ### withdrawal * **Type:** `toRpc.Input` The Withdrawal to convert. ## Return Type An RPC Withdrawal. `Rpc` # Withdrawal Types ## `Withdrawal.Rpc` An RPC Withdrawal as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml). **Source:** [src/core/Withdrawal.ts](https://github.com/wevm/ox/blob/main/src/core/Withdrawal.ts#L14) ## `Withdrawal.Withdrawal` A Withdrawal as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/withdrawal.yaml). **Source:** [src/core/Withdrawal.ts](https://github.com/wevm/ox/blob/main/src/core/Withdrawal.ts#L6) # Json Utility functions for working with JSON (with support for `bigint`). ## Examples Below are some examples demonstrating common usages of the `Json` module: * [Stringifying JSON](#stringifying-json) * [Parsing JSON](#parsing-json) ### Stringifying JSON JSON values can be stringified (with `bigint` support) using [`Json.stringify`](/api/Json/stringify): ```ts twoslash import { Json } from 'ox' const json = Json.stringify({ foo: 'bar', baz: 69420694206942069420694206942069420694206942069420n }) // @log: '{"foo":"bar","baz":69420694206942069420694206942069420694206942069420}' ``` ### Parsing JSON JSON values can be parsed (with `bigint` support) using [`Json.parse`](/api/Json/parse): ```ts twoslash import { Json } from 'ox' const value = Json.parse( '{"foo":"bar","baz":69420694206942069420694206942069420694206942069420}' ) // @log: { foo: 'bar', baz: 69420694206942069420694206942069420694206942069420n } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Json.canonicalize`](/api/Json/canonicalize) | Serializes a value to a canonical JSON string as defined by [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785). | | [`Json.parse`](/api/Json/parse) | Parses a JSON string, with support for `bigint`. | | [`Json.stringify`](/api/Json/stringify) | Stringifies a value to its JSON representation, with support for `bigint`. | # Json.canonicalize Serializes a value to a canonical JSON string as defined by [RFC 8785 (JSON Canonicalization Scheme)](https://www.rfc-editor.org/rfc/rfc8785). * Object keys are sorted recursively by UTF-16 code unit comparison. - Primitives are serialized per ECMAScript rules (no trailing zeros on numbers, etc.). - No whitespace is inserted. ## Imports :::code-group ```ts [Named] import { Json } from 'ox' ``` ```ts [Entrypoint] import * as Json from 'ox/Json' ``` ::: ## Examples ```ts twoslash import { Json } from 'ox' const json = Json.canonicalize({ b: 2, a: 1 }) // @log: '{"a":1,"b":2}' ``` ```ts twoslash import { Json } from 'ox' const json = Json.canonicalize({ z: [3, { y: 1, x: 2 }], a: 'hello' }) // @log: '{"a":"hello","z":[3,{"x":2,"y":1}]}' ``` ## Definition ```ts function canonicalize( value: unknown, ): string ``` **Source:** [src/core/Json.ts](https://github.com/wevm/ox/blob/main/src/core/Json.ts#L35) ## Parameters ### value * **Type:** `unknown` The value to canonicalize. ## Return Type The canonical JSON string. `string` # Json.parse Parses a JSON string, with support for `bigint`. ## Imports :::code-group ```ts [Named] import { Json } from 'ox' ``` ```ts [Entrypoint] import * as Json from 'ox/Json' ``` ::: ## Examples ```ts twoslash import { Json } from 'ox' const json = Json.parse( '{"foo":"bar","baz":"69420694206942069420694206942069420694206942069420#__bigint"}' ) // @log: { // @log: foo: 'bar', // @log: baz: 69420694206942069420694206942069420694206942069420n // @log: } ``` ## Definition ```ts function parse( string: string, reviver?: (this: any, key: string, value: any) => any, ): any ``` **Source:** [src/core/Json.ts](https://github.com/wevm/ox/blob/main/src/core/Json.ts#L100) ## Parameters ### string * **Type:** `string` The value to parse. ### reviver * **Type:** `(this: any, key: string, value: any) => any` * **Optional** A function that transforms the results. ## Return Type The parsed value. `any` # Json.stringify Stringifies a value to its JSON representation, with support for `bigint`. ## Imports :::code-group ```ts [Named] import { Json } from 'ox' ``` ```ts [Entrypoint] import * as Json from 'ox/Json' ``` ::: ## Examples ```ts twoslash import { Json } from 'ox' const json = Json.stringify({ foo: 'bar', baz: 69420694206942069420694206942069420694206942069420n }) // @log: '{"foo":"bar","baz":"69420694206942069420694206942069420694206942069420#__bigint"}' ``` ## Definition ```ts function stringify( value: any, replacer?: ((this: any, key: string, value: any) => any) | null, space?: string | number, ): string ``` **Source:** [src/core/Json.ts](https://github.com/wevm/ox/blob/main/src/core/Json.ts#L142) ## Parameters ### value * **Type:** `any` The value to stringify. ### replacer * **Type:** `((this: any, key: string, value: any) => any) | null` * **Optional** A function that transforms the results. It is passed the key and value of the property, and must return the value to be used in the JSON string. If this function returns `undefined`, the property is not included in the resulting JSON string. ### space * **Type:** `string | number` * **Optional** A string or number that determines the indentation of the JSON string. If it is a number, it indicates the number of spaces to use as indentation; if it is a string (e.g. `'\t'`), it uses the string as the indentation character. ## Return Type The JSON string. `string` # RpcRequest Utility types & functions for working with [JSON-RPC 2.0 Requests](https://www.jsonrpc.org/specification#request_object) and Ethereum JSON-RPC methods as defined on the [Ethereum API specification](https://github.com/ethereum/execution-apis) ## Examples ### Instantiating a Request Store A Request Store can be instantiated using [`RpcRequest.createStore`](/api/RpcRequest/createStore): ```ts twoslash import { RpcRequest } from 'ox' const store = RpcRequest.createStore() const request_1 = store.prepare({ method: 'eth_blockNumber' }) // @log: { id: 0, jsonrpc: '2.0', method: 'eth_blockNumber' } const request_2 = store.prepare({ method: 'eth_call', params: [ { to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef' } ] }) // @log: { id: 1, jsonrpc: '2.0', method: 'eth_call', params: [{ to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef' }] } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`RpcRequest.createStore`](/api/RpcRequest/createStore) | Creates a JSON-RPC request store to build requests with an incrementing `id`. | | [`RpcRequest.from`](/api/RpcRequest/from) | A type-safe interface to build a JSON-RPC request object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`RpcRequest.RpcRequest`](/api/RpcRequest/types#rpcrequestrpcrequest) | A JSON-RPC request object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). | | [`RpcRequest.Store`](/api/RpcRequest/types#rpcrequeststore) | JSON-RPC request store type. | # RpcRequest.createStore Creates a JSON-RPC request store to build requests with an incrementing `id`. Returns a type-safe `prepare` function to build a JSON-RPC request object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). ## Imports :::code-group ```ts [Named] import { RpcRequest } from 'ox' ``` ```ts [Entrypoint] import * as RpcRequest from 'ox/RpcRequest' ``` ::: ## Examples ```ts twoslash import { RpcRequest } from 'ox' const store = RpcRequest.createStore() const request_1 = store.prepare({ method: 'eth_blockNumber' }) // @log: { id: 0, jsonrpc: '2.0', method: 'eth_blockNumber' } const request_2 = store.prepare({ method: 'eth_call', params: [ { to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef' } ] }) // @log: { id: 1, jsonrpc: '2.0', method: 'eth_call', params: [{ to: '0x0000000000000000000000000000000000000000', data: '0xdeadbeef' }] } ``` ### Type-safe Custom Schemas It is possible to define your own type-safe schema by using [`RpcSchema.from`](/api/RpcSchema/from). ```ts twoslash import { RpcSchema, RpcRequest } from 'ox' const schema = RpcSchema.from< | { // [!code focus] Request: { // [!code focus] method: 'eth_foobar' // [!code focus] params: [number] // [!code focus] } // [!code focus] ReturnType: string // [!code focus] } | { // [!code focus] Request: { // [!code focus] method: 'eth_foobaz' // [!code focus] params: [string] // [!code focus] } // [!code focus] ReturnType: string // [!code focus] } >() // [!code focus] const store = RpcRequest.createStore({ schema }) // [!code focus] const request = store.prepare({ method: 'eth_foobar', // [!code focus] // ^? params: [42] }) ``` ## Definition ```ts function createStore( options?: createStore.Options, ): createStore.ReturnType ``` **Source:** [src/core/RpcRequest.ts](https://github.com/wevm/ox/blob/main/src/core/RpcRequest.ts#L111) ## Parameters ### options * **Type:** `createStore.Options` * **Optional** Request store options. #### options.id * **Type:** `number` * **Optional** The initial request ID. #### options.schema * **Type:** `schema | Generic` * **Optional** RPC Schema to use for the request store. ## Return Type The request store `createStore.ReturnType` # RpcRequest.from A type-safe interface to build a JSON-RPC request object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). :::warning You will likely want to use [`RpcRequest.createStore`](/api/RpcRequest/createStore) instead as it will also manage `id`s and uses this function internally. ::: ## Imports :::code-group ```ts [Named] import { RpcRequest } from 'ox' ``` ```ts [Entrypoint] import * as RpcRequest from 'ox/RpcRequest' ``` ::: ## Examples ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' // 1. Build a request object. const request = RpcRequest.from({ // [!code focus] id: 0, // [!code focus] method: 'eth_estimateGas', // [!code focus] params: [ // [!code focus] { // [!code focus] from: '0xd2135CfB216b74109775236E36d4b433F1DF507B', // [!code focus] to: '0x0D44f617435088c947F00B31160f64b074e412B4', // [!code focus] value: '0x69420' // [!code focus] } // [!code focus] ] // [!code focus] }) // [!code focus] // 2. Send the JSON-RPC request via HTTP. const gas = await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), headers: { 'Content-Type': 'application/json' }, method: 'POST' }) .then((response) => response.json()) // 3. Parse the JSON-RPC response into a type-safe result. .then((response) => RpcResponse.parse(response, { request }) ) ``` ## Definition ```ts function from( options: from.Options, ): from.ReturnType ``` **Source:** [src/core/RpcRequest.ts](https://github.com/wevm/ox/blob/main/src/core/RpcRequest.ts#L186) ## Parameters ### options * **Type:** `from.Options` JSON-RPC request options. #### options.Request * **Type:** `{ method: methodName; }` #### options.id * **Type:** `number` #### options.method * **Type:** `methodName` #### options.params * **Type:** `unknown` * **Optional** ## Return Type The fully-formed JSON-RPC request object. `from.ReturnType` # RpcRequest Types ## `RpcRequest.RpcRequest` A JSON-RPC request object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). **Source:** [src/core/RpcRequest.ts](https://github.com/wevm/ox/blob/main/src/core/RpcRequest.ts#L7) ## `RpcRequest.Store` JSON-RPC request store type. **Source:** [src/core/RpcRequest.ts](https://github.com/wevm/ox/blob/main/src/core/RpcRequest.ts#L20) # RpcResponse Utility types & functions for working with [JSON-RPC 2.0 Responses](https://www.jsonrpc.org/specification#response_object) ## Examples Below are some examples demonstrating common usages of the `RpcResponse` module: * [Instantiating an RPC Response](#instantiating-an-rpc-response) * [Parsing an RPC Response](#parsing-an-rpc-response) ### Instantiating an RPC Response RPC Responses can be instantiated using [`RpcResponse.from`](/api/RpcResponse/from): ```ts twoslash import { RpcResponse } from 'ox' const response = RpcResponse.from({ id: 0, jsonrpc: '2.0', result: '0x69420' }) ``` :::note Type-safe instantiation from a `request` object is also supported. If a `request` is provided, then the `id` and `jsonrpc` properties will be overridden with the values from the request. ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' const request = RpcRequest.from({ id: 0, method: 'eth_blockNumber' }) const response = RpcResponse.from( { result: '0x69420' }, { request } ) ``` ::: ### Parsing an RPC Response RPC Responses can be parsed using [`RpcResponse.parse`](/api/RpcResponse/parse): ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' // 1. Create a request store. const store = RpcRequest.createStore() // 2. Get a request object. const request = store.prepare({ method: 'eth_getBlockByNumber', params: ['0x1', false] }) // 3. Send the JSON-RPC request via HTTP. const block = await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), headers: { 'Content-Type': 'application/json' }, method: 'POST' }) .then((response) => response.json()) // 4. Parse the JSON-RPC response into a type-safe result. // [!code focus] .then((response) => RpcResponse.parse(response, { request }) ) // [!code focus] block // [!code focus] // ^? ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`RpcResponse.from`](/api/RpcResponse/from) | A type-safe interface to instantiate a JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#response_object). | | [`RpcResponse.parse`](/api/RpcResponse/parse) | A type-safe interface to parse a JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#response_object), and extract the result. | | [`RpcResponse.parseError`](/api/RpcResponse/parseError) | Parses an error into a RPC Error instance. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`RpcResponse.BaseError`](/api/RpcResponse/errors#rpcresponsebaseerror) | Thrown when a JSON-RPC error has occurred. | | [`RpcResponse.InternalError`](/api/RpcResponse/errors#rpcresponseinternalerror) | Thrown when an internal JSON-RPC error has occurred. | | [`RpcResponse.InvalidInputError`](/api/RpcResponse/errors#rpcresponseinvalidinputerror) | Thrown when the input to a JSON-RPC method is invalid. | | [`RpcResponse.InvalidParamsError`](/api/RpcResponse/errors#rpcresponseinvalidparamserror) | Thrown when the parameters to a JSON-RPC method are invalid. | | [`RpcResponse.InvalidRequestError`](/api/RpcResponse/errors#rpcresponseinvalidrequesterror) | Thrown when a JSON-RPC request is invalid. | | [`RpcResponse.LimitExceededError`](/api/RpcResponse/errors#rpcresponselimitexceedederror) | Thrown when a rate-limit is exceeded. | | [`RpcResponse.MethodNotFoundError`](/api/RpcResponse/errors#rpcresponsemethodnotfounderror) | Thrown when a JSON-RPC method is not found. | | [`RpcResponse.MethodNotSupportedError`](/api/RpcResponse/errors#rpcresponsemethodnotsupportederror) | Thrown when a JSON-RPC method is not supported. | | [`RpcResponse.ParseError`](/api/RpcResponse/errors#rpcresponseparseerror) | Thrown when a JSON-RPC response is invalid. | | [`RpcResponse.ResourceNotFoundError`](/api/RpcResponse/errors#rpcresponseresourcenotfounderror) | Thrown when a JSON-RPC resource is not found. | | [`RpcResponse.ResourceUnavailableError`](/api/RpcResponse/errors#rpcresponseresourceunavailableerror) | Thrown when a JSON-RPC resource is unavailable. | | [`RpcResponse.TransactionRejectedError`](/api/RpcResponse/errors#rpcresponsetransactionrejectederror) | Thrown when a JSON-RPC transaction is rejected. | | [`RpcResponse.VersionNotSupportedError`](/api/RpcResponse/errors#rpcresponseversionnotsupportederror) | Thrown when a JSON-RPC version is not supported. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`RpcResponse.BaseErrorType`](/api/RpcResponse/types#rpcresponsebaseerrortype) | | | [`RpcResponse.ErrorObject`](/api/RpcResponse/types#rpcresponseerrorobject) | JSON-RPC error object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#error_object). | | [`RpcResponse.RpcResponse`](/api/RpcResponse/types#rpcresponserpcresponse) | A JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). | # RpcResponse.from A type-safe interface to instantiate a JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#response_object). ## Imports :::code-group ```ts [Named] import { RpcResponse } from 'ox' ``` ```ts [Entrypoint] import * as RpcResponse from 'ox/RpcResponse' ``` ::: ## Examples ### Instantiating a Response Object ```ts twoslash import { RpcResponse } from 'ox' const response = RpcResponse.from({ id: 0, jsonrpc: '2.0', result: '0x69420' }) ``` ### Type-safe Instantiation If you have a JSON-RPC request object, you can use it to strongly-type the response. If a `request` is provided, then the `id` and `jsonrpc` properties will be overridden with the values from the request. ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' const request = RpcRequest.from({ id: 0, method: 'eth_blockNumber' }) const response = RpcResponse.from( { result: '0x69420' }, { request } ) ``` ## Definition ```ts function from( response: from.Response, options?: from.Options, ): Compute> ``` **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L68) ## Parameters ### response * **Type:** `from.Response` Opaque JSON-RPC response object. ### options * **Type:** `from.Options` * **Optional** Parsing options. #### options.request * **Type:** `request | { method: string; params?: unknown; id: number; jsonrpc: "2.0"; _returnType: unknown; }` * **Optional** ## Return Type Typed JSON-RPC result, or response object (if `raw` is `true`). `Compute>` # RpcResponse.parse A type-safe interface to parse a JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#response_object), and extract the result. ## Imports :::code-group ```ts [Named] import { RpcResponse } from 'ox' ``` ```ts [Entrypoint] import * as RpcResponse from 'ox/RpcResponse' ``` ::: ## Examples ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' // 1. Create a request store. const store = RpcRequest.createStore() // 2. Get a request object. const request = store.prepare({ method: 'eth_getBlockByNumber', params: ['0x1', false] }) // 3. Send the JSON-RPC request via HTTP. const block = await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(request), headers: { 'Content-Type': 'application/json' }, method: 'POST' }) .then((response) => response.json()) // 4. Parse the JSON-RPC response into a type-safe result. // [!code focus] .then((response) => RpcResponse.parse(response, { request }) ) // [!code focus] block // [!code focus] // ^? ``` :::tip If you don't need the return type, you can omit the options entirely. ```ts twoslash // @noErrors import { RpcResponse } from 'ox' const block = await fetch('https://1.rpc.thirdweb.com', {}) .then((response) => response.json()) .then((response) => RpcResponse.parse(response, { request }) ) // [!code --] .then(RpcResponse.parse) // [!code ++] ``` ::: ### Raw Mode If `raw` is `true`, the response will be returned as an object with `result` and `error` properties instead of returning the `result` directly and throwing errors. ```ts twoslash import { RpcRequest, RpcResponse } from 'ox' const store = RpcRequest.createStore() const request = store.prepare({ method: 'eth_blockNumber' }) const response = RpcResponse.parse( {}, { request, raw: true // [!code hl] } ) response.result // ^? response.error // ^? ``` ## Definition ```ts function parse( response: response, options?: parse.Options, ): parse.ReturnType ``` **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L203) ## Parameters ### response * **Type:** `response` Opaque JSON-RPC response object. ### options * **Type:** `parse.Options` * **Optional** Parsing options. #### options.raw * **Type:** `boolean | raw` * **Optional** Enables raw mode – responses will return an object with `result` and `error` properties instead of returning the `result` directly and throwing errors. * `true`: a JSON-RPC response object will be returned with `result` and `error` properties. * `false`: the JSON-RPC response object's `result` property will be returned directly, and JSON-RPC Errors will be thrown. #### options.request * **Type:** `{ method: string; params?: unknown; id: number; jsonrpc: "2.0"; _returnType: unknown; } | { _returnType: returnType; }` * **Optional** JSON-RPC Method that was used to make the request. Used for typing the response. ## Return Type Typed JSON-RPC result, or response object (if `raw` is `true`). `parse.ReturnType` # RpcResponse.parseError Parses an error into a RPC Error instance. ## Imports :::code-group ```ts [Named] import { RpcResponse } from 'ox' ``` ```ts [Entrypoint] import * as RpcResponse from 'ox/RpcResponse' ``` ::: ## Examples ```ts twoslash import { RpcResponse } from 'ox' const error = RpcResponse.parseError({ code: -32000, message: 'unsupported method' }) error // ^? ``` ## Definition ```ts function parseError( error: error | Error | ErrorObject, ): parseError.ReturnType ``` **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L297) ## Parameters ### error * **Type:** `error | Error | ErrorObject` Error. ## Return Type RPC Error instance. `parseError.ReturnType` # RpcResponse Errors ## `RpcResponse.BaseError` Thrown when a JSON-RPC error has occurred. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L411) ## `RpcResponse.InternalError` Thrown when an internal JSON-RPC error has occurred. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L587) ## `RpcResponse.InvalidInputError` Thrown when the input to a JSON-RPC method is invalid. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L437) ## `RpcResponse.InvalidParamsError` Thrown when the parameters to a JSON-RPC method are invalid. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L572) ## `RpcResponse.InvalidRequestError` Thrown when a JSON-RPC request is invalid. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L542) ## `RpcResponse.LimitExceededError` Thrown when a rate-limit is exceeded. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L512) ## `RpcResponse.MethodNotFoundError` Thrown when a JSON-RPC method is not found. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L557) ## `RpcResponse.MethodNotSupportedError` Thrown when a JSON-RPC method is not supported. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L497) ## `RpcResponse.ParseError` Thrown when a JSON-RPC response is invalid. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L609) ## `RpcResponse.ResourceNotFoundError` Thrown when a JSON-RPC resource is not found. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L452) ## `RpcResponse.ResourceUnavailableError` Thrown when a JSON-RPC resource is unavailable. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L467) ## `RpcResponse.TransactionRejectedError` Thrown when a JSON-RPC transaction is rejected. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L482) ## `RpcResponse.VersionNotSupportedError` Thrown when a JSON-RPC version is not supported. **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L527) # RpcResponse Types ## `RpcResponse.BaseErrorType` **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L408) ## `RpcResponse.ErrorObject` JSON-RPC error object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#error_object). **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L22) ## `RpcResponse.RpcResponse` A JSON-RPC response object as per the [JSON-RPC 2.0 specification](https://www.jsonrpc.org/specification#request_object). **Source:** [src/core/RpcResponse.ts](https://github.com/wevm/ox/blob/main/src/core/RpcResponse.ts#L11) # RpcSchema Utility types for working with Ethereum JSON-RPC namespaces & schemas. ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`RpcSchema.from`](/api/RpcSchema/from) | Instantiates a statically typed Schema. This is a runtime-noop function, and is purposed to be used as a type-level tag to be used with [`Provider.from`](/api/Provider/from) or [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`RpcSchema.Default`](/api/RpcSchema/types#rpcschemadefault) | Type-safe union of all JSON-RPC Methods. | | [`RpcSchema.Eth`](/api/RpcSchema/types#rpcschemaeth) | Union of all JSON-RPC Methods for the `eth_` namespace. | | [`RpcSchema.ExtractItem`](/api/RpcSchema/types#rpcschemaextractitem) | Extracts a schema item from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). | | [`RpcSchema.ExtractMethodName`](/api/RpcSchema/types#rpcschemaextractmethodname) | Type-safe union of all JSON-RPC Method Names. | | [`RpcSchema.ExtractParams`](/api/RpcSchema/types#rpcschemaextractparams) | Extracts parameters from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). | | [`RpcSchema.ExtractRequest`](/api/RpcSchema/types#rpcschemaextractrequest) | Extracts request from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). | | [`RpcSchema.ExtractReturnType`](/api/RpcSchema/types#rpcschemaextractreturntype) | Extracts return type from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). | | [`RpcSchema.From`](/api/RpcSchema/types#rpcschemafrom) | Type to define a custom type-safe JSON-RPC Schema. | | [`RpcSchema.FromViem`](/api/RpcSchema/types#rpcschemafromviem) | Converts a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`) to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`). | | [`RpcSchema.FromZod`](/api/RpcSchema/types#rpcschemafromzod) | Converts a record of Zod `params`/`returns` schemas (keyed by method name) to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`). | | [`RpcSchema.Generic`](/api/RpcSchema/types#rpcschemageneric) | Generic type to define a JSON-RPC Method. | | [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#rpcschemamethodnamegeneric) | Generic type to define a JSON-RPC Method Name. | | [`RpcSchema.Schema`](/api/RpcSchema/types#rpcschemaschema) | Schema input accepted by APIs that statically type JSON-RPC requests (e.g. [`Provider.from`](/api/Provider/from), [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp)): either an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or a record of Zod `params`/`returns` schemas (keyed by method name) from `ox/zod`. | | [`RpcSchema.ToGeneric`](/api/RpcSchema/types#rpcschematogeneric) | Resolves a [`RpcSchema.Schema`](/api/RpcSchema/types#schema) input to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic). A Zod namespace is converted via [`RpcSchema.FromZod`](/api/RpcSchema/types#fromzod); a `Generic` is passed through; otherwise falls back to [`RpcSchema.Default`](/api/RpcSchema/types#default). | | [`RpcSchema.ToViem`](/api/RpcSchema/types#rpcschematoviem) | Converts an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`) to a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`). | | [`RpcSchema.Wallet`](/api/RpcSchema/types#rpcschemawallet) | Union of all JSON-RPC Methods for the `wallet_` namespace. | # RpcSchema.from Instantiates a statically typed Schema. This is a runtime-noop function, and is purposed to be used as a type-level tag to be used with [`Provider.from`](/api/Provider/from) or [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp). ## Imports :::code-group ```ts [Named] import { RpcSchema } from 'ox' ``` ```ts [Entrypoint] import * as RpcSchema from 'ox/RpcSchema' ``` ::: ## Examples ### Using with `Provider.from` ```ts twoslash // @noErrors import 'ox/window' import { Provider, RpcSchema } from 'ox' const schema = RpcSchema.from< | RpcSchema.Default | { Request: { method: 'abe_foo' params: [id: number] } ReturnType: string } | { Request: { method: 'abe_bar' params: [id: string] } ReturnType: string } >() const provider = Provider.from(window.ethereum, { schema }) const blockNumber = await provider.request({ method: 'e' }) // ^| ``` ## Definition ```ts function from(): schema ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L45) ## Return Type `schema` # RpcSchema Types ## `RpcSchema.Default` Type-safe union of all JSON-RPC Methods. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Schema = RpcSchema.Default // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L199) ## `RpcSchema.Eth` Union of all JSON-RPC Methods for the `eth_` namespace. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Schema = RpcSchema.Eth // ^? ``` **Source:** [src/core/internal/rpcSchemas/eth.ts](https://github.com/wevm/ox/blob/main/src/core/internal/rpcSchemas/eth.ts#L26) ## `RpcSchema.ExtractItem` Extracts a schema item from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Item = RpcSchema.ExtractItem< RpcSchema.Eth, 'eth_getBlockByNumber' > const item = null as unknown as Item // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L64) ## `RpcSchema.ExtractMethodName` Type-safe union of all JSON-RPC Method Names. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type MethodName = RpcSchema.ExtractMethodName // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L104) ## `RpcSchema.ExtractParams` Extracts parameters from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Eth_GetBlockByNumber = RpcSchema.ExtractParams< RpcSchema.Eth, 'eth_getBlockByNumber' > const parameters = null as unknown as Eth_GetBlockByNumber // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L122) ## `RpcSchema.ExtractRequest` Extracts request from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Request = RpcSchema.ExtractRequest< RpcSchema.Eth, 'eth_getBlockByNumber' > const request = null as unknown as Request // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L87) ## `RpcSchema.ExtractReturnType` Extracts return type from a [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or [`RpcSchema.MethodNameGeneric`](/api/RpcSchema/types#methodnamegeneric). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type ReturnType = RpcSchema.ExtractReturnType< RpcSchema.Eth, 'eth_getBlockByNumber' > const returnType = null as unknown as ReturnType // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L142) ## `RpcSchema.From` Type to define a custom type-safe JSON-RPC Schema. ### Examples ```ts twoslash import { RpcSchema, RpcRequest } from 'ox' type Schema = RpcSchema.From<{ Request: { method: 'eth_foobar' params: [id: number] } ReturnType: string }> ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L167) ## `RpcSchema.FromViem` Converts a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`) to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type OxSchema = RpcSchema.FromViem< [ { Method: 'eth_blockNumber' Parameters?: undefined ReturnType: `0x${string}` }, { Method: 'eth_chainId' Parameters?: undefined ReturnType: `0x${string}` } ] > ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L273) ## `RpcSchema.FromZod` Converts a record of Zod `params`/`returns` schemas (keyed by method name) to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`). Each method's name comes from its key. Both `params` and `ReturnType` are derived from the Zod schema's input (wire) type, since raw JSON-RPC clients send and receive wire values. Decode wire results to their native representation explicitly via `zod.RpcSchema.decodeReturns`. ### Examples ```ts twoslash import { RpcSchema } from 'ox' import { z } from 'ox/zod' type Schema = RpcSchema.FromZod // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L303) ## `RpcSchema.Generic` Generic type to define a JSON-RPC Method. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Schema = RpcSchema.Generic // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L180) ## `RpcSchema.MethodNameGeneric` Generic type to define a JSON-RPC Method Name. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Name = RpcSchema.MethodNameGeneric // ^? ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L212) ## `RpcSchema.Schema` Schema input accepted by APIs that statically type JSON-RPC requests (e.g. [`Provider.from`](/api/Provider/from), [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp)): either an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) or a record of Zod `params`/`returns` schemas (keyed by method name) from `ox/zod`. **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L330) ## `RpcSchema.ToGeneric` Resolves a [`RpcSchema.Schema`](/api/RpcSchema/types#schema) input to an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic). A Zod namespace is converted via [`RpcSchema.FromZod`](/api/RpcSchema/types#fromzod); a `Generic` is passed through; otherwise falls back to [`RpcSchema.Default`](/api/RpcSchema/types#default). **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L337) ## `RpcSchema.ToViem` Converts an Ox [`RpcSchema.Generic`](/api/RpcSchema/types#generic) (union of `{ Request, ReturnType }`) to a [Viem-compatible RPC schema](https://viem.sh) (tuple of `{ Method, Parameters, ReturnType }`). ### Examples ```ts twoslash import { RpcSchema } from 'ox' type ViemSchema = RpcSchema.ToViem< | { Request: { method: 'eth_blockNumber' params?: undefined } ReturnType: `0x${string}` } | { Request: { method: 'eth_chainId'; params?: undefined } ReturnType: `0x${string}` } > ``` **Source:** [src/core/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/core/RpcSchema.ts#L239) ## `RpcSchema.Wallet` Union of all JSON-RPC Methods for the `wallet_` namespace. ### Examples ```ts twoslash import { RpcSchema } from 'ox' type Schema = RpcSchema.Wallet // ^? ``` **Source:** [src/core/internal/rpcSchemas/wallet.ts](https://github.com/wevm/ox/blob/main/src/core/internal/rpcSchemas/wallet.ts#L19) # RpcTransport Utility functions for working with JSON-RPC Transports. :::note This is a convenience module distributed for experimenting with network connectivity on Ox. Consider using networking functionality from a higher-level library such as [Viem's Transports](https://viem.sh/docs/clients/transports/http) if you need more features such as: retry logic, WebSockets/IPC, middleware, batch JSON-RPC, etc. ::: ## Examples ### HTTP Instantiation ```ts twoslash import { RpcTransport } from 'ox' const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) const blockNumber = await transport.request({ method: 'eth_blockNumber' }) // @log: '0x1a2b3c' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) | Creates a HTTP JSON-RPC Transport from a URL. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`RpcTransport.HttpError`](/api/RpcTransport/errors#rpctransporthttperror) | Thrown when a HTTP request fails. | | [`RpcTransport.MalformedResponseError`](/api/RpcTransport/errors#rpctransportmalformedresponseerror) | Thrown when a HTTP response is malformed. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`RpcTransport.Http`](/api/RpcTransport/types#rpctransporthttp) | HTTP-based RPC Transport. | | [`RpcTransport.HttpOptions`](/api/RpcTransport/types#rpctransporthttpoptions) | | | [`RpcTransport.RequestFn`](/api/RpcTransport/types#rpctransportrequestfn) | | | [`RpcTransport.RpcTransport`](/api/RpcTransport/types#rpctransportrpctransport) | Root type for an RPC Transport. | # RpcTransport.fromHttp Creates a HTTP JSON-RPC Transport from a URL. ## Imports :::code-group ```ts [Named] import { RpcTransport } from 'ox' ``` ```ts [Entrypoint] import * as RpcTransport from 'ox/RpcTransport' ``` ::: ## Examples ```ts twoslash import { RpcTransport } from 'ox' const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) const blockNumber = await transport.request({ method: 'eth_blockNumber' }) // @log: '0x1a2b3c' ``` ## Definition ```ts function fromHttp( url: string, options?: fromHttp.Options, ): Http> ``` **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L82) ## Parameters ### url * **Type:** `string` URL to perform the JSON-RPC requests to. ### options * **Type:** `fromHttp.Options` * **Optional** Transport options. #### options.fetchFn * **Type:** `{ (input: RequestInfo | URL, init?: RequestInit): Promise; }` * **Optional** Function to use to make the request. #### options.fetchOptions * **Type:** `Omit)` * **Optional** Request configuration to pass to `fetch`. #### options.timeout * **Type:** `number` * **Optional** Timeout for the request in milliseconds. ## Return Type HTTP JSON-RPC Transport. `Http>` # RpcTransport Errors ## `RpcTransport.HttpError` Thrown when a HTTP request fails. **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L191) ## `RpcTransport.MalformedResponseError` Thrown when a HTTP response is malformed. **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L217) # RpcTransport Types ## `RpcTransport.Http` HTTP-based RPC Transport. **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L20) ## `RpcTransport.HttpOptions` **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L25) ## `RpcTransport.RequestFn` **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L39) ## `RpcTransport.RpcTransport` Root type for an RPC Transport. **Source:** [src/core/RpcTransport.ts](https://github.com/wevm/ox/blob/main/src/core/RpcTransport.ts#L11) # MlDsa44 Utilities for working with ML-DSA-44 signatures and key pairs, as defined in [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final). ML-DSA is a post-quantum digital signature scheme, standardized by NIST as the primary quantum-resistant replacement for elliptic-curve signatures. The `44` parameter set targets NIST security category 2 (128-bit classical security). Private keys are the 32-byte FIPS 204 seed. Public keys are 1,312 bytes and signatures are 2,420 bytes. ## Examples Below are some examples demonstrating common usages of the `MlDsa44` module: * [Creating Key Pairs](#creating-key-pairs) * [Signing & Verifying](#signing-&-verifying) ### Creating Key Pairs ```ts twoslash import { MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() ``` ### Signing & Verifying ```ts twoslash import { MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() const payload = '0xdeadbeef' const signature = MlDsa44.sign({ payload, privateKey }) const isValid = MlDsa44.verify({ payload, publicKey, signature }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`MlDsa44.createKeyPair`](/api/MlDsa44/createKeyPair) | Creates a new ML-DSA-44 key pair consisting of a private key and its corresponding public key. | | [`MlDsa44.fromPrf`](/api/MlDsa44/fromPrf) | Derives an ML-DSA-44 private key from a 32-byte WebAuthn PRF output. | | [`MlDsa44.getPublicKey`](/api/MlDsa44/getPublicKey) | Computes the ML-DSA-44 public key from a provided private key. | | [`MlDsa44.randomPrivateKey`](/api/MlDsa44/randomPrivateKey) | Generates a random ML-DSA-44 private key (32-byte seed). | | [`MlDsa44.sign`](/api/MlDsa44/sign) | Signs the payload with the provided private key and returns an ML-DSA-44 signature (2,420 bytes). | | [`MlDsa44.verify`](/api/MlDsa44/verify) | Verifies a payload was signed by the provided public key. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`MlDsa44.InvalidContextSizeError`](/api/MlDsa44/errors#mldsa44invalidcontextsizeerror) | Thrown when a context string exceeds the 255-byte FIPS 204 limit. | | [`MlDsa44.InvalidPrfSizeError`](/api/MlDsa44/errors#mldsa44invalidprfsizeerror) | Thrown when a WebAuthn PRF output is not 32 bytes. | # MlDsa44.createKeyPair Creates a new ML-DSA-44 key pair consisting of a private key and its corresponding public key. The private key is the 32-byte seed (`ξ`) from FIPS 204 key generation — the canonical interchange form of an ML-DSA private key. The 1,312-byte public key is deterministically expanded from it. ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() ``` ## Definition ```ts function createKeyPair( options?: createKeyPair.Options, ): createKeyPair.ReturnType ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L39) ## Parameters ### options * **Type:** `createKeyPair.Options` * **Optional** The options to generate the key pair. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private and public keys. ## Return Type The generated key pair containing both private and public keys. `createKeyPair.ReturnType` # MlDsa44.fromPrf Derives an ML-DSA-44 private key from a 32-byte WebAuthn PRF output. The permanent derivation contract uses the PRF output as the HMAC-SHA256 key. Its message is the UTF-8 bytes of `ox.mldsa44.fromPrf.v1` followed by a 32-bit big-endian counter set to zero. ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const privateKey = MlDsa44.fromPrf( '0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' ) ``` ## Definition ```ts function fromPrf( value: Hex.Hex | Bytes.Bytes, options?: fromPrf.Options, ): fromPrf.ReturnType ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L96) ## Parameters ### value * **Type:** `Hex.Hex | Bytes.Bytes` A 32-byte WebAuthn PRF output. ### options * **Type:** `fromPrf.Options` * **Optional** Options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type An ML-DSA-44 private key (32-byte seed). `fromPrf.ReturnType` # MlDsa44.getPublicKey Computes the ML-DSA-44 public key from a provided private key. ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const publicKey = MlDsa44.getPublicKey({ privateKey: '0x...' }) ``` ## Definition ```ts function getPublicKey( options: getPublicKey.Options, ): getPublicKey.ReturnType ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L155) ## Parameters ### options * **Type:** `getPublicKey.Options` The options to compute the public key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned public key. #### options.privateKey * **Type:** `0x${string} | Uint8Array` Private key (32-byte seed) to compute the public key from. ## Return Type The computed 1,312-byte public key. `getPublicKey.ReturnType` # MlDsa44.randomPrivateKey Generates a random ML-DSA-44 private key (32-byte seed). ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const privateKey = MlDsa44.randomPrivateKey() ``` ## Definition ```ts function randomPrivateKey( options?: randomPrivateKey.Options, ): randomPrivateKey.ReturnType ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L201) ## Parameters ### options * **Type:** `randomPrivateKey.Options` * **Optional** The options to generate the private key. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned private key. ## Return Type The generated private key. `randomPrivateKey.ReturnType` # MlDsa44.sign Signs the payload with the provided private key and returns an ML-DSA-44 signature (2,420 bytes). Signing is deterministic by default. Set `extraEntropy` to `true` (or to 32 bytes of entropy) for the hedged variant of FIPS 204, which protects against fault attacks and randomness-reuse pitfalls at the cost of reproducibility. ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const signature = MlDsa44.sign({ // [!code focus] payload: '0xdeadbeef', // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function sign( options: sign.Options, ): sign.ReturnType ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L248) ## Parameters ### options * **Type:** `sign.Options` The signing options. #### options.as * **Type:** `"Bytes" | "Hex" | as` * **Optional** Format of the returned signature. #### options.context * **Type:** `0x${string} | Uint8Array` * **Optional** Context string for domain separation, at most 255 bytes. #### options.extraEntropy * **Type:** `boolean | 0x${string} | Uint8Array` * **Optional** Extra entropy to add to the signing process. Setting to `true` enables hedged signing with 32 fresh random bytes; 32 bytes may be supplied directly instead. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload to sign. #### options.privateKey * **Type:** `0x${string} | Uint8Array` ML-DSA-44 private key (32-byte seed). ## Return Type The ML-DSA-44 signature. `sign.ReturnType` # MlDsa44.verify Verifies a payload was signed by the provided public key. ## Imports :::code-group ```ts [Named] import { MlDsa44 } from 'ox' ``` ```ts [Entrypoint] import * as MlDsa44 from 'ox/MlDsa44' ``` ::: ## Examples ```ts twoslash import { MlDsa44 } from 'ox' const { privateKey, publicKey } = MlDsa44.createKeyPair() const signature = MlDsa44.sign({ payload: '0xdeadbeef', privateKey }) const verified = MlDsa44.verify({ // [!code focus] publicKey, // [!code focus] payload: '0xdeadbeef', // [!code focus] signature // [!code focus] }) // [!code focus] ``` ## Definition ```ts function verify( options: verify.Options, ): boolean ``` **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L337) ## Parameters ### options * **Type:** `verify.Options` The verification options. #### options.context * **Type:** `0x${string} | Uint8Array` * **Optional** Context string for domain separation, at most 255 bytes. #### options.payload * **Type:** `0x${string} | Uint8Array` Payload that was signed. #### options.publicKey * **Type:** `0x${string} | Uint8Array` Public key that signed the payload. #### options.signature * **Type:** `0x${string} | Uint8Array` Signature of the payload. ## Return Type Whether the payload was signed by the provided public key. `boolean` # MlDsa44 Errors ## `MlDsa44.InvalidContextSizeError` Thrown when a context string exceeds the 255-byte FIPS 204 limit. **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L370) ## `MlDsa44.InvalidPrfSizeError` Thrown when a WebAuthn PRF output is not 32 bytes. **Source:** [src/core/MlDsa44.ts](https://github.com/wevm/ox/blob/main/src/core/MlDsa44.ts#L387) # Provider Utilities & types for working with [EIP-1193 Providers](https://eips.ethereum.org/EIPS/eip-1193) ## Examples Below are some examples demonstrating common usages of the `Provider` module: * [Instantiating External Providers](#instantiating-external-providers) * [Instantiating with an RPC Transport](#instantiating-with-an-rpc-transport) * [Instantiating a Provider with Events](#instantiating-a-provider-with-events) ### Instantiating External Providers External EIP-1193 Providers can be instantiated with [`Provider.from`](/api/Provider/from): ```ts twoslash import 'ox/window' import { Provider } from 'ox' const provider = Provider.from(window.ethereum) const blockNumber = await provider.request({ method: 'eth_blockNumber' }) ``` :::tip There are also libraries that distribute EIP-1193 Provider objects that you can use with `Provider.from`: * [`@walletconnect/ethereum-provider`](https://www.npmjs.com/package/@walletconnect/ethereum-provider) * [`@coinbase/wallet-sdk`](https://www.npmjs.com/package/@coinbase/wallet-sdk) * [`@metamask/detect-provider`](https://www.npmjs.com/package/@metamask/detect-provider) * [`@safe-global/safe-apps-provider`](https://github.com/safe-global/safe-apps-sdk/tree/main/packages/safe-apps-provider) * [`mipd`](https://github.com/wevm/mipd): EIP-6963 Multi Injected Providers ::: ### Instantiating with an RPC Transport Ox's [`RpcTransport`](/api/) is also EIP-1193 compliant, and can be used to instantiate an EIP-1193 Provider. This means you can use any HTTP RPC endpoint as an EIP-1193 Provider. ```ts twoslash import { Provider, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) const provider = Provider.from(transport) ``` ### Instantiating a Provider with Events Event emitters for EIP-1193 Providers can be created using [`Provider.createEmitter`](/api/Provider/createEmitter): Useful for Wallets that distribute an EIP-1193 Provider (e.g. webpage injection via `window.ethereum`). ```ts twoslash // @noErrors import { Provider, RpcRequest, RpcResponse } from 'ox' // 1. Instantiate a Provider Emitter. const emitter = Provider.createEmitter() // [!code ++] const store = RpcRequest.createStore() const provider = Provider.from({ // 2. Pass the Emitter to the Provider. ...emitter, // [!code ++] async request(args) { return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(store.prepare(args)), method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then((res) => res.json()) .then(RpcResponse.parse) } }) // 3. Emit Provider Events. emitter.emit('accountsChanged', ['0x...']) // [!code ++] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Provider.createEmitter`](/api/Provider/createEmitter) | Creates an EIP-1193 flavored event emitter to be injected onto a Provider. | | [`Provider.from`](/api/Provider/from) | Instantiates an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) [`Provider.Provider`](/api/Provider/types#provider) from an arbitrary [EIP-1193 Provider](https://eips.ethereum.org/EIPS/eip-1193) interface. | | [`Provider.parseError`](/api/Provider/parseError) | Parses an error into a Provider error instance. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Provider.AtomicityNotSupportedError`](/api/Provider/errors#provideratomicitynotsupportederror) | The wallet does not support atomic execution but the request requires it. | | [`Provider.AtomicReadyWalletRejectedUpgradeError`](/api/Provider/errors#provideratomicreadywalletrejectedupgradeerror) | The Wallet can support atomicity after an upgrade, but the user rejected the upgrade. | | [`Provider.BundleTooLargeError`](/api/Provider/errors#providerbundletoolargeerror) | The call bundle is too large for the Wallet to process. | | [`Provider.ChainDisconnectedError`](/api/Provider/errors#providerchaindisconnectederror) | The provider is not connected to the requested chain. | | [`Provider.DisconnectedError`](/api/Provider/errors#providerdisconnectederror) | The provider is disconnected from all chains. | | [`Provider.DuplicateIdError`](/api/Provider/errors#providerduplicateiderror) | There is already a bundle submitted with this ID. | | [`Provider.IsUndefinedError`](/api/Provider/errors#providerisundefinederror) | Thrown when the provider is undefined. | | [`Provider.ProviderRpcError`](/api/Provider/errors#providerproviderrpcerror) | | | [`Provider.SwitchChainError`](/api/Provider/errors#providerswitchchainerror) | An error occurred when attempting to switch chain. | | [`Provider.UnauthorizedError`](/api/Provider/errors#providerunauthorizederror) | The requested method and/or account has not been authorized by the user. | | [`Provider.UnknownBundleIdError`](/api/Provider/errors#providerunknownbundleiderror) | This bundle id is unknown / has not been submitted. | | [`Provider.UnsupportedChainIdError`](/api/Provider/errors#providerunsupportedchainiderror) | This Wallet does not support the requested chain ID. | | [`Provider.UnsupportedMethodError`](/api/Provider/errors#providerunsupportedmethoderror) | The provider does not support the requested method. | | [`Provider.UnsupportedNonOptionalCapabilityError`](/api/Provider/errors#providerunsupportednonoptionalcapabilityerror) | This Wallet does not support a capability that was not marked as optional. | | [`Provider.UserRejectedRequestError`](/api/Provider/errors#provideruserrejectedrequesterror) | The user rejected the request. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Provider.ConnectInfo`](/api/Provider/types#providerconnectinfo) | | | [`Provider.Emitter`](/api/Provider/types#provideremitter) | Type for an EIP-1193 Provider's event emitter. | | [`Provider.EventMap`](/api/Provider/types#providereventmap) | | | [`Provider.Message`](/api/Provider/types#providermessage) | | | [`Provider.Options`](/api/Provider/types#provideroptions) | Options for a [`Provider.Provider`](/api/Provider/types#provider). | | [`Provider.Provider`](/api/Provider/types#providerprovider) | Root type for an EIP-1193 Provider. | | [`Provider.RequestFn`](/api/Provider/types#providerrequestfn) | EIP-1193 Provider's `request` function. | # Provider.createEmitter Creates an EIP-1193 flavored event emitter to be injected onto a Provider. ## Imports :::code-group ```ts [Named] import { Provider } from 'ox' ``` ```ts [Entrypoint] import * as Provider from 'ox/Provider' ``` ::: ## Examples ```ts twoslash // @noErrors import { Provider, RpcRequest, RpcResponse } from 'ox' // [!code focus] // 1. Instantiate a Provider Emitter. // [!code focus] const emitter = Provider.createEmitter() // [!code focus] const store = RpcRequest.createStore() const provider = Provider.from({ // 2. Pass the Emitter to the Provider. // [!code focus] ...emitter, // [!code focus] async request(args) { return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(store.prepare(args)), method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then((res) => res.json()) .then(RpcResponse.parse) } }) // 3. Emit Provider Events. // [!code focus] emitter.emit('accountsChanged', ['0x...']) // [!code focus] ``` ## Definition ```ts function createEmitter(): Emitter ``` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L288) ## Return Type An event emitter. `Emitter` # Provider.from Instantiates an [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) [`Provider.Provider`](/api/Provider/types#provider) from an arbitrary [EIP-1193 Provider](https://eips.ethereum.org/EIPS/eip-1193) interface. ## Imports :::code-group ```ts [Named] import { Provider } from 'ox' ``` ```ts [Entrypoint] import * as Provider from 'ox/Provider' ``` ::: ## Examples ### Instantiating with RPC Transport Ox's [`RpcTransport`](/api/) is EIP-1193 compliant, and can be used to instantiate an EIP-1193 Provider. This means you can use any HTTP RPC endpoint as an EIP-1193 Provider. ```ts twoslash import { Provider, RpcTransport } from 'ox' const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) const provider = Provider.from(transport) ``` ### Instantiating with External Providers The example below demonstrates how we can instantiate a typed EIP-1193 Provider from an external EIP-1193 Provider like `window.ethereum`. ```ts twoslash import 'ox/window' import { Provider } from 'ox' const provider = Provider.from(window.ethereum) const blockNumber = await provider.request({ method: 'eth_blockNumber' }) ``` :::tip There are also libraries that distribute EIP-1193 Provider objects that you can use with `Provider.from`: * [`@walletconnect/ethereum-provider`](https://www.npmjs.com/package/@walletconnect/ethereum-provider) * [`@coinbase/wallet-sdk`](https://www.npmjs.com/package/@coinbase/wallet-sdk) * [`@metamask/detect-provider`](https://www.npmjs.com/package/@metamask/detect-provider) * [`@safe-global/safe-apps-provider`](https://github.com/safe-global/safe-apps-sdk/tree/main/packages/safe-apps-provider) * [`mipd`](https://github.com/wevm/mipd): EIP-6963 Multi Injected Providers ::: ### Instantiating a Custom Provider The example below demonstrates how we can instantiate a typed EIP-1193 Provider from a HTTP `fetch` JSON-RPC request. You can use this pattern to integrate with any asynchronous JSON-RPC transport, including WebSockets and IPC. ```ts twoslash // @noErrors import { Provider, RpcRequest, RpcResponse } from 'ox' const store = RpcRequest.createStore() const provider = Provider.from({ async request(args) { return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(store.prepare(args)), method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then((res) => res.json()) .then(RpcResponse.parse) } }) const blockNumber = await provider.request({ method: 'eth_blockNumber' }) ``` ### Type-safe Custom Schemas It is possible to define your own type-safe schema by using the [`RpcSchema.from`](/api/RpcSchema/from) type. ```ts twoslash // @noErrors import 'ox/window' import { Provider, RpcSchema } from 'ox' const schema = RpcSchema.from< | RpcSchema.Default | { Request: { method: 'abe_foo' params: [id: number] } ReturnType: string } | { Request: { method: 'abe_bar' params: [id: string] } ReturnType: string } >() const provider = Provider.from(window.ethereum, { schema }) const blockNumber = await provider.request({ method: 'e' }) // ^| ``` ### Instantiating a Provider with Events The example below demonstrates how to instantiate a Provider with your own EIP-1193 flavored event emitter. This example is useful for Wallets that distribute an EIP-1193 Provider (e.g. webpage injection via `window.ethereum`). ```ts twoslash // @noErrors import { Provider, RpcRequest, RpcResponse } from 'ox' // 1. Instantiate a Provider Emitter. const emitter = Provider.createEmitter() // [!code ++] const store = RpcRequest.createStore() const provider = Provider.from({ // 2. Pass the Emitter to the Provider. ...emitter, // [!code ++] async request(args) { return await fetch('https://1.rpc.thirdweb.com', { body: JSON.stringify(store.prepare(args)), method: 'POST', headers: { 'Content-Type': 'application/json' } }) .then((res) => res.json()) .then(RpcResponse.parse) } }) // 3. Emit Provider Events. emitter.emit('accountsChanged', ['0x...']) // [!code ++] ``` ## Definition ```ts function from( provider: provider | from.Value | undefined, options?: options | Options, ): from.ReturnType ``` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L473) ## Parameters ### provider * **Type:** `provider | from.Value | undefined` The EIP-1193 provider to convert. ### options * **Type:** `options | Options` * **Optional** ## Return Type An typed EIP-1193 Provider. `from.ReturnType` # Provider.parseError Parses an error into a Provider error instance. ## Imports :::code-group ```ts [Named] import { Provider } from 'ox' ``` ```ts [Entrypoint] import * as Provider from 'ox/Provider' ``` ::: ## Examples ```ts twoslash import { Provider } from 'ox' const error = Provider.parseError({ code: 4200, message: 'foo' }) error // ^? ``` ## Definition ```ts function parseError( error: error | Error | RpcResponse.ErrorObject, ): parseError.ReturnType ``` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L544) ## Parameters ### error * **Type:** `error | Error | RpcResponse.ErrorObject` The error object to parse. ## Return Type An error instance. `parseError.ReturnType` # Provider Errors ## `Provider.AtomicityNotSupportedError` The wallet does not support atomic execution but the request requires it. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L241) ## `Provider.AtomicReadyWalletRejectedUpgradeError` The Wallet can support atomicity after an upgrade, but the user rejected the upgrade. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L228) ## `Provider.BundleTooLargeError` The call bundle is too large for the Wallet to process. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L215) ## `Provider.ChainDisconnectedError` The provider is not connected to the requested chain. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L137) ## `Provider.DisconnectedError` The provider is disconnected from all chains. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L124) ## `Provider.DuplicateIdError` There is already a bundle submitted with this ID. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L189) ## `Provider.IsUndefinedError` Thrown when the provider is undefined. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L674) ## `Provider.ProviderRpcError` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L61) ## `Provider.SwitchChainError` An error occurred when attempting to switch chain. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L150) ## `Provider.UnauthorizedError` The requested method and/or account has not been authorized by the user. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L98) ## `Provider.UnknownBundleIdError` This bundle id is unknown / has not been submitted. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L202) ## `Provider.UnsupportedChainIdError` This Wallet does not support the requested chain ID. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L176) ## `Provider.UnsupportedMethodError` The provider does not support the requested method. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L111) ## `Provider.UnsupportedNonOptionalCapabilityError` This Wallet does not support a capability that was not marked as optional. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L163) ## `Provider.UserRejectedRequestError` The user rejected the request. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L85) # Provider Types ## `Provider.ConnectInfo` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L52) ## `Provider.Emitter` Type for an EIP-1193 Provider's event emitter. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L41) ## `Provider.EventMap` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L74) ## `Provider.Message` **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L56) ## `Provider.Options` Options for a [`Provider.Provider`](/api/Provider/types#provider). **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L10) ## `Provider.Provider` Root type for an EIP-1193 Provider. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L21) ## `Provider.RequestFn` EIP-1193 Provider's `request` function. **Source:** [src/core/Provider.ts](https://github.com/wevm/ox/blob/main/src/core/Provider.ts#L46) # Siwe Utility functions for working with [EIP-4361: Sign-In with Ethereum](https://eips.ethereum.org/EIPS/eip-4361) ## Examples Below are some examples demonstrating common usages of the `Siwe` module: * [Creating a SIWE Message](#creating-a-siwe-message) * [Generating SIWE Nonces](#generating-siwe-nonces) * [Parsing a SIWE Message](#parsing-a-siwe-message) * [Validating a SIWE Message](#validating-a-siwe-message) ### Creating a SIWE Message SIWE messages can be created using [`Siwe.createMessage`](/api/Siwe/createMessage): ```ts twoslash import { Siwe } from 'ox' Siwe.createMessage({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: 'foobarbaz', uri: 'https://example.com/path', version: '1' }) // @log: "example.com wants you to sign in with your Ethereum account: // @log: 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e // @log: // @log: // @log: URI: https://example.com/path // @log: Version: 1 // @log: Chain ID: 1 // @log: Nonce: foobarbaz // @log: Issued At: 2023-02-01T00:00:00.000Z" ``` ### Generating SIWE Nonces SIWE nonces can be generated using [`Siwe.generateNonce`](/api/Siwe/generateNonce): ```ts twoslash import { Siwe } from 'ox' Siwe.generateNonce() // @log: '65ed4681d4efe0270b923ff5f4b097b1c95974dc33aeebecd5724c42fd86dfd25dc70b27ef836b2aa22e68f19ebcccc1' ``` ### Parsing a SIWE Message SIWE messages can be parsed using [`Siwe.parseMessage`](/api/Siwe/parseMessage): ```ts twoslash import { Siwe } from 'ox' Siwe.parseMessage(`example.com wants you to sign in with your Ethereum account: 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e I accept the ExampleOrg Terms of Service: https://example.com/tos URI: https://example.com/path Version: 1 Chain ID: 1 Nonce: foobarbaz Issued At: 2023-02-01T00:00:00.000Z`) // @log: { // @log: address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // @log: chainId: 1, // @log: domain: 'example.com', // @log: issuedAt: '2023-02-01T00:00:00.000Z', // @log: nonce: 'foobarbaz', // @log: statement: 'I accept the ExampleOrg Terms of Service: https://example.com/tos', // @log: uri: 'https://example.com/path', // @log: version: '1', // @log: } ``` ### Validating a SIWE Message SIWE messages can be validated using [`Siwe.validateMessage`](/api/Siwe/validateMessage): ```ts twoslash import { Siwe } from 'ox' Siwe.validateMessage({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', domain: 'example.com', message: { address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: 'foobarbaz', uri: 'https://example.com/path', version: '1' }, nonce: 'foobarbaz' }) // @log: true ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Siwe.createMessage`](/api/Siwe/createMessage) | Creates [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message. | | [`Siwe.generateNonce`](/api/Siwe/generateNonce) | Generates random [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) nonce. | | [`Siwe.isUri`](/api/Siwe/isUri) | Check if the given URI is a valid [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI. | | [`Siwe.parseMessage`](/api/Siwe/parseMessage) | [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message into message fields object. | | [`Siwe.validateMessage`](/api/Siwe/validateMessage) | Validates [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) message. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Siwe.InvalidMessageFieldError`](/api/Siwe/errors#siweinvalidmessagefielderror) | Thrown when a field in a SIWE Message is invalid. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Siwe.Message`](/api/Siwe/types#siwemessage) | [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) message fields. | # Siwe.createMessage Creates [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message. ## Imports :::code-group ```ts [Named] import { Siwe } from 'ox' ``` ```ts [Entrypoint] import * as Siwe from 'ox/Siwe' ``` ::: ## Examples ```ts twoslash import { Siwe } from 'ox' Siwe.createMessage({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: 'foobarbaz', uri: 'https://example.com/path', version: '1' }) // @log: "example.com wants you to sign in with your Ethereum account: // @log: 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e // @log: // @log: // @log: URI: https://example.com/path // @log: Version: 1 // @log: Chain ID: 1 // @log: Nonce: foobarbaz // @log: Issued At: 2023-02-01T00:00:00.000Z" ``` ## Definition ```ts function createMessage( value: Siwe.Message, ): string ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L111) ## Parameters ### value * **Type:** [`Siwe.Message`](/api/Siwe/types#siwemessage) Values to use when creating EIP-4361 formatted message. #### value.address * **Type:** `abitype_Address` The Ethereum address performing the signing. #### value.chainId * **Type:** `number` The [EIP-155](https://eips.ethereum.org/EIPS/eip-155) Chain ID to which the session is bound, #### value.domain * **Type:** `string` [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) authority that is requesting the signing. #### value.expirationTime * **Type:** `Date` * **Optional** Time when the signed authentication message is no longer valid. #### value.issuedAt * **Type:** `Date` * **Optional** Time when the message was generated, typically the current time. #### value.nonce * **Type:** `string` A random string typically chosen by the relying party and used to prevent replay attacks. #### value.notBefore * **Type:** `Date` * **Optional** Time when the signed authentication message will become valid. #### value.requestId * **Type:** `string` * **Optional** A system-specific identifier that may be used to uniquely refer to the sign-in request. #### value.resources * **Type:** `string[]` * **Optional** A list of information or references to information the user wishes to have resolved as part of authentication by the relying party. #### value.scheme * **Type:** `string` * **Optional** [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986#section-3.1) URI scheme of the origin of the request. #### value.statement * **Type:** `string` * **Optional** A human-readable ASCII assertion that the user will sign. #### value.uri * **Type:** `string` [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI referring to the resource that is the subject of the signing (as in the subject of a claim). #### value.version * **Type:** `"1"` The current version of the SIWE Message. ## Return Type EIP-4361 formatted message. `string` # Siwe.generateNonce Generates random [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) nonce. ## Imports :::code-group ```ts [Named] import { Siwe } from 'ox' ``` ```ts [Entrypoint] import * as Siwe from 'ox/Siwe' ``` ::: ## Examples ```ts twoslash import { Siwe } from 'ox' Siwe.generateNonce() // @log: '65ed4681d4efe0270b923ff5f4b097b1c95974dc33aeebecd5724c42fd86dfd25dc70b27ef836b2aa22e68f19ebcccc1' ``` ## Definition ```ts function generateNonce(): string ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L267) ## Return Type Random nonce. `string` # Siwe.isUri Check if the given URI is a valid [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986) URI. ## Imports :::code-group ```ts [Named] import { Siwe } from 'ox' ``` ```ts [Entrypoint] import * as Siwe from 'ox/Siwe' ``` ::: ## Examples ```ts twoslash import { Siwe } from 'ox' Siwe.isUri('https://example.com/foo') // @log: true ``` ## Definition ```ts function isUri( value: string, ): false | string ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L286) ## Parameters ### value * **Type:** `string` Value to check. ## Return Type `false` if invalid, otherwise the valid URI. `false | string` # Siwe.parseMessage [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message into message fields object. ## Imports :::code-group ```ts [Named] import { Siwe } from 'ox' ``` ```ts [Entrypoint] import * as Siwe from 'ox/Siwe' ``` ::: ## Examples ```ts twoslash import { Siwe } from 'ox' Siwe.parseMessage(`example.com wants you to sign in with your Ethereum account: 0xA0Cf798816D4b9b9866b5330EEa46a18382f251e I accept the ExampleOrg Terms of Service: https://example.com/tos URI: https://example.com/path Version: 1 Chain ID: 1 Nonce: foobarbaz Issued At: 2023-02-01T00:00:00.000Z`) // @log: { // @log: address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', // @log: chainId: 1, // @log: domain: 'example.com', // @log: issuedAt: '2023-02-01T00:00:00.000Z', // @log: nonce: 'foobarbaz', // @log: statement: 'I accept the ExampleOrg Terms of Service: https://example.com/tos', // @log: uri: 'https://example.com/path', // @log: version: '1', // @log: } ``` ## Definition ```ts function parseMessage( message: string, ): ExactPartial ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L367) ## Parameters ### message * **Type:** `string` [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) formatted message. ## Return Type Message fields object. `ExactPartial` # Siwe.validateMessage Validates [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) message. ## Imports :::code-group ```ts [Named] import { Siwe } from 'ox' ``` ```ts [Entrypoint] import * as Siwe from 'ox/Siwe' ``` ::: ## Examples ```ts twoslash import { Siwe } from 'ox' Siwe.validateMessage({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', domain: 'example.com', message: { address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1, domain: 'example.com', nonce: 'foobarbaz', uri: 'https://example.com/path', version: '1' }, nonce: 'foobarbaz' }) // @log: true ``` ## Definition ```ts function validateMessage( value: validateMessage.Value, ): boolean ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L427) ## Parameters ### value * **Type:** `validateMessage.Value` Values to use when validating EIP-4361 formatted message. ## Return Type Whether the message is valid. `boolean` # Siwe Errors ## `Siwe.InvalidMessageFieldError` Thrown when a field in a SIWE Message is invalid. ### Examples ```ts twoslash // @noErrors import { Siwe } from 'ox' Siwe.createMessage({ address: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', chainId: 1.1, domain: 'example.com', nonce: 'foobarbaz', uri: 'https://example.com/path', version: '1' }) // @error: Siwe.InvalidMessageFieldError: Invalid Sign-In with Ethereum message field "chainId". // @error: - Chain ID must be a EIP-155 chain ID. // @error: - See https://eips.ethereum.org/EIPS/eip-155 // @error: Provided value: 1.1 ``` **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L500) # Siwe Types ## `Siwe.Message` [EIP-4361](https://eips.ethereum.org/EIPS/eip-4361) message fields. **Source:** [src/core/Siwe.ts](https://github.com/wevm/ox/blob/main/src/core/Siwe.ts#L27) # PersonalMessage Utilities & types for working with [EIP-191 Personal Messages](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e) ## Examples ### Computing Sign Payload An EIP-191 personal sign payload can be computed using [`PersonalMessage.getSignPayload`](/api/PersonalMessage/getSignPayload): ```ts twoslash import { Hex, PersonalMessage, Secp256k1 } from 'ox' const payload = PersonalMessage.getSignPayload( Hex.fromString('hello world') ) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`PersonalMessage.encode`](/api/PersonalMessage/encode) | Encodes a personal sign message in [ERC-191 format](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e): `0x19 ‖ "Ethereum Signed Message:\n" + message.length ‖ message`. | | [`PersonalMessage.getSignPayload`](/api/PersonalMessage/getSignPayload) | Gets the payload to use for signing an [ERC-191 formatted](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e) personal message. | # PersonalMessage.encode Encodes a personal sign message in [ERC-191 format](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e): `0x19 ‖ "Ethereum Signed Message:\n" + message.length ‖ message`. ## Imports :::code-group ```ts [Named] import { PersonalMessage } from 'ox' ``` ```ts [Entrypoint] import * as PersonalMessage from 'ox/PersonalMessage' ``` ::: ## Examples ```ts twoslash import { Hex, PersonalMessage } from 'ox' const data = PersonalMessage.encode( Hex.fromString('hello world') ) // @log: '0x19457468657265756d205369676e6564204d6573736167653a0a313168656c6c6f20776f726c64' // @log: (0x19 ‖ 'Ethereum Signed Message:\n11' ‖ 'hello world') ``` ## Definition ```ts function encode( data: Hex.Hex | Bytes.Bytes, ): Hex.Hex ``` **Source:** [src/core/PersonalMessage.ts](https://github.com/wevm/ox/blob/main/src/core/PersonalMessage.ts#L33) ## Parameters ### data * **Type:** `Hex.Hex | Bytes.Bytes` The data to encode. ## Return Type The encoded personal sign message. `Hex.Hex` # PersonalMessage.getSignPayload Gets the payload to use for signing an [ERC-191 formatted](https://eips.ethereum.org/EIPS/eip-191#version-0x45-e) personal message. ## Imports :::code-group ```ts [Named] import { PersonalMessage } from 'ox' ``` ```ts [Entrypoint] import * as PersonalMessage from 'ox/PersonalMessage' ``` ::: ## Examples ```ts twoslash import { Hex, PersonalMessage, Secp256k1 } from 'ox' const payload = PersonalMessage.getSignPayload( Hex.fromString('hello world') ) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( data: Hex.Hex | Bytes.Bytes, ): Hex.Hex ``` **Source:** [src/core/PersonalMessage.ts](https://github.com/wevm/ox/blob/main/src/core/PersonalMessage.ts#L71) ## Parameters ### data * **Type:** `Hex.Hex | Bytes.Bytes` The data to get the sign payload for. ## Return Type The payload to use for signing. `Hex.Hex` # TypedData Utility functions for working with [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) ## Examples ### Getting Sign Payloads Typed Data can be converted to a sign payload using [`TypedData.getSignPayload`](/api/TypedData/getSignPayload): ```ts twoslash import { Secp256k1, TypedData, Hash } from 'ox' const payload = TypedData.getSignPayload({ // [!code focus:99] domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000' }, types: { Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' } ], Mail: [ { name: 'from', type: 'Person' }, { name: 'to', type: 'Person' }, { name: 'contents', type: 'string' } ] }, primaryType: 'Mail', message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826' }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB' }, contents: 'Hello, Bob!' } }) const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TypedData.assert`](/api/TypedData/assert) | Asserts that [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. | | [`TypedData.domainSeparator`](/api/TypedData/domainSeparator) | Creates [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) [`domainSeparator`](https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator) for the provided domain. | | [`TypedData.encode`](/api/TypedData/encode) | Encodes typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712): `0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message)`. | | [`TypedData.encodeType`](/api/TypedData/encodeType) | Encodes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for the provided primaryType. | | [`TypedData.extractEip712DomainTypes`](/api/TypedData/extractEip712DomainTypes) | Gets [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for EIP-721 domain. | | [`TypedData.getSignPayload`](/api/TypedData/getSignPayload) | Gets the payload to use for signing typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712). | | [`TypedData.hashDomain`](/api/TypedData/hashDomain) | Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) domain. | | [`TypedData.hashStruct`](/api/TypedData/hashStruct) | Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) struct. | | [`TypedData.serialize`](/api/TypedData/serialize) | Serializes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema into string. | | [`TypedData.validate`](/api/TypedData/validate) | Checks if [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`TypedData.BytesSizeMismatchError`](/api/TypedData/errors#typeddatabytessizemismatcherror) | Thrown when the bytes size of a typed data value does not match the expected size. | | [`TypedData.InvalidArrayError`](/api/TypedData/errors#typeddatainvalidarrayerror) | Thrown when an array-typed value is not an array. | | [`TypedData.InvalidArrayLengthError`](/api/TypedData/errors#typeddatainvalidarraylengtherror) | Thrown when a fixed-length array does not match its declared length. | | [`TypedData.InvalidDomainError`](/api/TypedData/errors#typeddatainvaliddomainerror) | Thrown when the domain is invalid. | | [`TypedData.InvalidPrimaryTypeError`](/api/TypedData/errors#typeddatainvalidprimarytypeerror) | Thrown when the primary type of a typed data value is invalid. | | [`TypedData.InvalidStructTypeError`](/api/TypedData/errors#typeddatainvalidstructtypeerror) | Thrown when the struct type is not a valid type. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TypedData.Definition`](/api/TypedData/types#typeddatadefinition) | | | [`TypedData.Domain`](/api/TypedData/types#typeddatadomain) | | | [`TypedData.EIP712DomainDefinition`](/api/TypedData/types#typeddataeip712domaindefinition) | | | [`TypedData.MessageDefinition`](/api/TypedData/types#typeddatamessagedefinition) | | | [`TypedData.Parameter`](/api/TypedData/types#typeddataparameter) | | | [`TypedData.TypedData`](/api/TypedData/types#typeddatatypeddata) | | # TypedData.assert Asserts that [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.assert({ domain: { name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, primaryType: 'Foo', types: { Foo: [ { name: 'address', type: 'address' }, { name: 'name', type: 'string' }, { name: 'foo', type: 'string' } ] }, message: { address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', name: 'jxom', foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9' } }) ``` ## Definition ```ts function assert( value: assert.Value, ): void ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L103) ## Parameters ### value * **Type:** `assert.Value` The Typed Data to validate. ## Return Type `void` # TypedData.domainSeparator Creates [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) [`domainSeparator`](https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator) for the provided domain. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.domainSeparator({ name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }) // @log: '0x9911ee4f58a7059a8f5385248040e6984d80e2c849500fe6a4d11c4fa98c2af3' ``` ## Definition ```ts function domainSeparator( domain: Domain, ): Hex.Hex ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L232) ## Parameters ### domain * **Type:** `Domain` The domain for which to create the domain separator. ## Return Type The domain separator. `Hex.Hex` # TypedData.encode Encodes typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712): `0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message)`. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData, Hash } from 'ox' const data = TypedData.encode({ // [!code focus:33] domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000' }, types: { Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' } ], Mail: [ { name: 'from', type: 'Person' }, { name: 'to', type: 'Person' }, { name: 'contents', type: 'string' } ] }, primaryType: 'Mail', message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826' }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB' }, contents: 'Hello, Bob!' } }) // @log: '0x19012fdf3441fcaf4f30c7e16292b258a5d7054a4e2e00dbd7b7d2f467f2b8fb9413c52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e' // @log: (0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message)) const hash = Hash.keccak256(data) ``` ## Definition ```ts function encode( value: encode.Value, ): Hex.Hex ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L291) ## Parameters ### value * **Type:** `encode.Value` The Typed Data to encode. ## Return Type The encoded Typed Data. `Hex.Hex` # TypedData.encodeType Encodes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for the provided primaryType. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.encodeType({ types: { Foo: [ { name: 'address', type: 'address' }, { name: 'name', type: 'string' }, { name: 'foo', type: 'string' } ] }, primaryType: 'Foo' }) // @log: 'Foo(address address,string name,string foo)' ``` ## Definition ```ts function encodeType( value: encodeType.Value, ): string ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L376) ## Parameters ### value * **Type:** `encodeType.Value` The Typed Data schema. #### value.primaryType * **Type:** `string` #### value.types * **Type:** `abitype.TypedData` ## Return Type The encoded type. `string` # TypedData.extractEip712DomainTypes Gets [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for EIP-721 domain. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.extractEip712DomainTypes({ name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }) // @log: [ // @log: { 'name': 'name', 'type': 'string' }, // @log: { 'name': 'version', 'type': 'string' }, // @log: { 'name': 'chainId', 'type': 'uint256' }, // @log: { 'name': 'verifyingContract', 'type': 'address' }, // @log: ] ``` ## Definition ```ts function extractEip712DomainTypes( domain: Domain | undefined, ): Parameter[] ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L427) ## Parameters ### domain * **Type:** `Domain | undefined` The EIP-712 domain. ## Return Type The EIP-712 domain schema. `Parameter[]` # TypedData.getSignPayload Gets the payload to use for signing typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712). ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { Secp256k1, TypedData, Hash } from 'ox' const payload = TypedData.getSignPayload({ // [!code focus:99] domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000' }, types: { Person: [ { name: 'name', type: 'string' }, { name: 'wallet', type: 'address' } ], Mail: [ { name: 'from', type: 'Person' }, { name: 'to', type: 'Person' }, { name: 'contents', type: 'string' } ] }, primaryType: 'Mail', message: { from: { name: 'Cow', wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826' }, to: { name: 'Bob', wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB' }, contents: 'Hello, Bob!' } }) const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( value: encode.Value, ): Hex.Hex ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L500) ## Parameters ### value * **Type:** `encode.Value` The typed data to get the sign payload for. ## Return Type The payload to use for signing. `Hex.Hex` # TypedData.hashDomain Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) domain. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.hashDomain({ domain: { name: 'Ether Mail', version: '1', chainId: 1, verifyingContract: '0x0000000000000000000000000000000000000000' } }) // @log: '0x6192106f129ce05c9075d319c1fa6ea9b3ae37cbd0c1ef92e2be7137bb07baa1' ``` ## Definition ```ts function hashDomain( value: hashDomain.Value, ): Hex.Hex ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L536) ## Parameters ### value * **Type:** `hashDomain.Value` The Typed Data domain and types. #### value.EIP712Domain * **Type:** `readonly abitype.TypedDataParameter[]` * **Optional** #### value.domain * **Type:** `abitype.TypedDataDomain` The Typed Data domain. #### value.typeHashes * **Type:** `Map` * **Optional** Optional cache of `keccak256(encodeType(t))` keyed by type name, shared across nested struct/array calls. #### value.types * **Type:** `{ [key: string]: readonly abitype.TypedDataParameter[]; EIP712Domain?: readonly abitype.TypedDataParameter[]; }` * **Optional** The Typed Data types. ## Return Type The hashed domain. `Hex.Hex` # TypedData.hashStruct Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) struct. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.hashStruct({ types: { Foo: [ { name: 'address', type: 'address' }, { name: 'name', type: 'string' }, { name: 'foo', type: 'string' } ] }, primaryType: 'Foo', data: { address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', name: 'jxom', foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9' } }) // @log: '0x996fb3b6d48c50312d69abdd4c1b6fb02057c85aa86bb8d04c6f023326a168ce' ``` ## Definition ```ts function hashStruct( value: hashStruct.Value, ): Hex.Hex ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L595) ## Parameters ### value * **Type:** `hashStruct.Value` The Typed Data struct to hash. #### value.data * **Type:** `Record` The Typed Data struct to hash. #### value.primaryType * **Type:** `string` The primary type of the Typed Data struct. #### value.typeHashes * **Type:** `Map` * **Optional** Optional cache of `keccak256(encodeType(t))` keyed by type name, shared across nested struct/array calls. #### value.types * **Type:** `abitype.TypedData` The types of the Typed Data struct. ## Return Type The hashed Typed Data struct. `Hex.Hex` # TypedData.serialize Serializes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema into string. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' TypedData.serialize({ domain: { name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, primaryType: 'Foo', types: { Foo: [ { name: 'address', type: 'address' }, { name: 'name', type: 'string' }, { name: 'foo', type: 'string' } ] }, message: { address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', name: 'jxom', foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9' } }) // @log: "{"domain":{},"message":{"address":"0xb9cab4f0e46f7f6b1024b5a7463734fa68e633f9","name":"jxom","foo":"0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9"},"primaryType":"Foo","types":{"Foo":[{"name":"address","type":"address"},{"name":"name","type":"string"},{"name":"foo","type":"string"}]}}" ``` ## Definition ```ts function serialize( value: serialize.Value, ): string ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L659) ## Parameters ### value * **Type:** `serialize.Value` The Typed Data schema to serialize. ## Return Type The serialized Typed Data schema. w `string` # TypedData.validate Checks if [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. ## Imports :::code-group ```ts [Named] import { TypedData } from 'ox' ``` ```ts [Entrypoint] import * as TypedData from 'ox/TypedData' ``` ::: ## Examples ```ts twoslash import { TypedData } from 'ox' const valid = TypedData.validate({ domain: { name: 'Ether!', version: '1', chainId: 1, verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' }, primaryType: 'Foo', types: { Foo: [ { name: 'address', type: 'address' }, { name: 'name', type: 'string' }, { name: 'foo', type: 'string' } ] }, message: { address: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9', name: 'jxom', foo: '0xb9CAB4F0E46F7F6b1024b5A7463734fa68E633f9' } }) // @log: true ``` ## Definition ```ts function validate( value: assert.Value, ): boolean ``` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L743) ## Parameters ### value * **Type:** `assert.Value` The Typed Data to validate. ## Return Type `boolean` # TypedData Errors ## `TypedData.BytesSizeMismatchError` Thrown when the bytes size of a typed data value does not match the expected size. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L760) ## `TypedData.InvalidArrayError` Thrown when an array-typed value is not an array. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L817) ## `TypedData.InvalidArrayLengthError` Thrown when a fixed-length array does not match its declared length. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L836) ## `TypedData.InvalidDomainError` Thrown when the domain is invalid. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L775) ## `TypedData.InvalidPrimaryTypeError` Thrown when the primary type of a typed data value is invalid. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L786) ## `TypedData.InvalidStructTypeError` Thrown when the struct type is not a valid type. **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L806) # TypedData Types ## `TypedData.Definition` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L17) ## `TypedData.Domain` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L13) ## `TypedData.EIP712DomainDefinition` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L26) ## `TypedData.MessageDefinition` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L45) ## `TypedData.Parameter` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L14) ## `TypedData.TypedData` **Source:** [src/core/TypedData.ts](https://github.com/wevm/ox/blob/main/src/core/TypedData.ts#L12) # ValidatorData Utilities & types for working with [EIP-191 Validator Data](https://eips.ethereum.org/EIPS/eip-191#0x00) ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`ValidatorData.encode`](/api/ValidatorData/encode) | Encodes data with a validator in [ERC-191 format](https://eips.ethereum.org/EIPS/eip-191#version-0x00): `0x19 ‖ 0x00 ‖ `. | | [`ValidatorData.getSignPayload`](/api/ValidatorData/getSignPayload) | Gets the payload to use for signing [ERC-191 formatted](https://eips.ethereum.org/EIPS/eip-191#0x00) data with an intended validator. | # ValidatorData.encode Encodes data with a validator in [ERC-191 format](https://eips.ethereum.org/EIPS/eip-191#version-0x00): `0x19 ‖ 0x00 ‖ `. ## Imports :::code-group ```ts [Named] import { ValidatorData } from 'ox' ``` ```ts [Entrypoint] import * as ValidatorData from 'ox/ValidatorData' ``` ::: ## Examples ```ts twoslash import { Hex, ValidatorData } from 'ox' const encoded = ValidatorData.encode({ data: Hex.fromString('hello world'), validator: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045' }) // @log: '0x1900d8da6bf26964af9d7eed9e03e53415d37aa9604568656c6c6f20776f726c64' // @log: '0x19 ‖ 0x00 ‖ 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 ‖ "hello world"' ``` ## Definition ```ts function encode( value: encode.Value, ): Hex.Hex ``` **Source:** [src/core/ValidatorData.ts](https://github.com/wevm/ox/blob/main/src/core/ValidatorData.ts#L25) ## Parameters ### value * **Type:** `encode.Value` The data to encode. #### value.data * **Type:** `0x${string} | Uint8Array` #### value.validator * **Type:** `abitype_Address` ## Return Type The encoded personal sign message. `Hex.Hex` # ValidatorData.getSignPayload Gets the payload to use for signing [ERC-191 formatted](https://eips.ethereum.org/EIPS/eip-191#0x00) data with an intended validator. ## Imports :::code-group ```ts [Named] import { ValidatorData } from 'ox' ``` ```ts [Entrypoint] import * as ValidatorData from 'ox/ValidatorData' ``` ::: ## Examples ```ts twoslash import { Hex, Secp256k1, ValidatorData } from 'ox' const payload = ValidatorData.getSignPayload({ // [!code focus] data: Hex.fromString('hello world'), // [!code focus] validator: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045' // [!code focus] }) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( value: getSignPayload.Value, ): Hex.Hex ``` **Source:** [src/core/ValidatorData.ts](https://github.com/wevm/ox/blob/main/src/core/ValidatorData.ts#L72) ## Parameters ### value * **Type:** `getSignPayload.Value` The data to get the sign payload for. #### value.data * **Type:** `0x${string} | Uint8Array` #### value.validator * **Type:** `abitype_Address` ## Return Type The payload to use for signing. `Hex.Hex` # TransactionEnvelope Errors & Types for working with Transaction Envelopes. :::note Refer to the following modules for specific Transaction Envelope types: * [`TxEnvelopeLegacy`](/api/TxEnvelopeLegacy) * [`TxEnvelopeEip1559`](/api/TxEnvelopeEip1559) * [`TxEnvelopeEip2930`](/api/TxEnvelopeEip2930) * [`TxEnvelopeEip4844`](/api/TxEnvelopeEip4844) * [`TxEnvelopeEip7702`](/api/TxEnvelopeEip7702) ::: ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionEnvelope.assert`](/api/TransactionEnvelope/assert) | Asserts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) is valid. | | [`TransactionEnvelope.deserialize`](/api/TransactionEnvelope/deserialize) | Deserializes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) from its serialized form. | | [`TransactionEnvelope.from`](/api/TransactionEnvelope/from) | Converts an arbitrary transaction object or serialized transaction into a Transaction Envelope. | | [`TransactionEnvelope.getSerializedType`](/api/TransactionEnvelope/getSerializedType) | Returns the type of a serialized Transaction Envelope. | | [`TransactionEnvelope.getSignPayload`](/api/TransactionEnvelope/getSignPayload) | Returns the payload to sign for a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). | | [`TransactionEnvelope.getType`](/api/TransactionEnvelope/getType) | Returns the type of a Transaction Envelope. | | [`TransactionEnvelope.hash`](/api/TransactionEnvelope/hash) | Hashes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). This is the "transaction hash". | | [`TransactionEnvelope.serialize`](/api/TransactionEnvelope/serialize) | Serializes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). | | [`TransactionEnvelope.toRpc`](/api/TransactionEnvelope/toRpc) | Converts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) to an [`TransactionEnvelope.Rpc`](/api/TransactionEnvelope/types#rpc). | | [`TransactionEnvelope.toTransactionRequest`](/api/TransactionEnvelope/toTransactionRequest) | Converts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) to a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). | | [`TransactionEnvelope.validate`](/api/TransactionEnvelope/validate) | Validates a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). Returns `true` if the envelope is valid, `false` otherwise. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionEnvelope.FeeCapTooHighError`](/api/TransactionEnvelope/errors#transactionenvelopefeecaptoohigherror) | Thrown when a fee cap is too high. | | [`TransactionEnvelope.GasPriceTooHighError`](/api/TransactionEnvelope/errors#transactionenvelopegaspricetoohigherror) | Thrown when a gas price is too high. | | [`TransactionEnvelope.InvalidChainIdError`](/api/TransactionEnvelope/errors#transactionenvelopeinvalidchainiderror) | Thrown when a chain ID is invalid. | | [`TransactionEnvelope.InvalidSerializedError`](/api/TransactionEnvelope/errors#transactionenvelopeinvalidserializederror) | Thrown when a serialized transaction is invalid. | | [`TransactionEnvelope.InvalidSerializedTypeError`](/api/TransactionEnvelope/errors#transactionenvelopeinvalidserializedtypeerror) | Thrown when a serialized transaction type cannot be resolved. | | [`TransactionEnvelope.InvalidTypeError`](/api/TransactionEnvelope/errors#transactionenvelopeinvalidtypeerror) | Thrown when a transaction envelope type cannot be resolved. | | [`TransactionEnvelope.TipAboveFeeCapError`](/api/TransactionEnvelope/errors#transactionenvelopetipabovefeecaperror) | Thrown when a tip is higher than a fee cap. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TransactionEnvelope.Base`](/api/TransactionEnvelope/types#transactionenvelopebase) | Base type for a Transaction Envelope. Transaction Envelopes inherit this type. | | [`TransactionEnvelope.BaseRpc`](/api/TransactionEnvelope/types#transactionenvelopebaserpc) | RPC representation of a [`TransactionEnvelope.Base`](/api/TransactionEnvelope/types#base). | | [`TransactionEnvelope.BaseSigned`](/api/TransactionEnvelope/types#transactionenvelopebasesigned) | Signed representation of a [`TransactionEnvelope.Base`](/api/TransactionEnvelope/types#base). | | [`TransactionEnvelope.Rpc`](/api/TransactionEnvelope/types#transactionenveloperpc) | RPC representation of a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). | | [`TransactionEnvelope.Serialized`](/api/TransactionEnvelope/types#transactionenvelopeserialized) | Serialized Transaction Envelope. | | [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#transactionenvelopetxenvelope) | Transaction Envelope. | | [`TransactionEnvelope.Type`](/api/TransactionEnvelope/types#transactionenvelopetype) | Transaction Envelope type. | # TransactionEnvelope.assert Asserts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) is valid. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' TransactionEnvelope.assert({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) ``` ## Definition ```ts function assert( envelope: TransactionEnvelope.TxEnvelope, ): void ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L136) ## Parameters ### envelope * **Type:** [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#transactionenvelopetxenvelope) The transaction envelope to assert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type `void` # TransactionEnvelope.deserialize Deserializes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) from its serialized form. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const envelope = TransactionEnvelope.deserialize( '0x02df0180808080808080c0' as TransactionEnvelope.Serialized ) ``` ## Definition ```ts function deserialize( serialized: serialized | Serialized | Hex.Hex, ): deserialize.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L172) ## Parameters ### serialized * **Type:** `serialized | Serialized | Hex.Hex` The serialized transaction envelope. ## Return Type Deserialized Transaction Envelope. `deserialize.ReturnType` # TransactionEnvelope.from Converts an arbitrary transaction object or serialized transaction into a Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const envelope = TransactionEnvelope.from({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) ``` ## Definition ```ts function from( envelope: envelope | Typeable | Hex.Hex, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L229) ## Parameters ### envelope * **Type:** `envelope | Typeable | Hex.Hex` The transaction envelope. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** Signature to append to the Transaction Envelope. ## Return Type Transaction Envelope. `from.ReturnType` # TransactionEnvelope.getSerializedType Returns the type of a serialized Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const type = TransactionEnvelope.getSerializedType( '0x02df0180808080808080c0' ) // @log: 'eip1559' ``` ## Definition ```ts function getSerializedType( serialized: serialized | Serialized | Hex.Hex, ): getSerializedType.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L454) ## Parameters ### serialized * **Type:** `serialized | Serialized | Hex.Hex` The serialized transaction envelope. ## Return Type Transaction Envelope type. `getSerializedType.ReturnType` # TransactionEnvelope.getSignPayload Returns the payload to sign for a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const payload = TransactionEnvelope.getSignPayload({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelope, ): Hex.Hex ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L323) ## Parameters ### envelope * **Type:** `TxEnvelope` The transaction envelope. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type The sign payload. `Hex.Hex` # TransactionEnvelope.getType Returns the type of a Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const type = TransactionEnvelope.getType({ maxFeePerGas: 1n }) // @log: 'eip1559' ``` ## Definition ```ts function getType( envelope: envelope | Typeable, ): getType.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L367) ## Parameters ### envelope * **Type:** `envelope | Typeable` The transaction envelope. ## Return Type Transaction Envelope type. `getType.ReturnType` # TransactionEnvelope.hash Hashes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const hash = TransactionEnvelope.hash( { chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }, { presign: true } ) ``` ## Definition ```ts function hash( envelope: TxEnvelope, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L518) ## Parameters ### envelope * **Type:** `TxEnvelope` The transaction envelope to hash. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TransactionEnvelope.serialize Serializes a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const serialized = TransactionEnvelope.serialize({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) ``` ## Definition ```ts function serialize( envelope: envelope | Typeable, options?: serialize.Options, ): serialize.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L569) ## Parameters ### envelope * **Type:** `envelope | Typeable` The transaction envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS sidecars to append, producing the 5-element network wrapper. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type Serialized transaction envelope. `serialize.ReturnType` # TransactionEnvelope.toRpc Converts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) to an [`TransactionEnvelope.Rpc`](/api/TransactionEnvelope/types#rpc). ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const envelope_rpc = TransactionEnvelope.toRpc({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) ``` ## Definition ```ts function toRpc( envelope: envelope | toRpc.Input, ): toRpc.ReturnType ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L633) ## Parameters ### envelope * **Type:** `envelope | toRpc.Input` The transaction envelope. ## Return Type RPC representation. `toRpc.ReturnType` # TransactionEnvelope.toTransactionRequest Converts a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope) to a [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest). Flattens any EIP-7594 `sidecars` back into the top-level `blobs` field. Signature fields (`r`, `s`, `yParity`, `v`) are preserved — Ox's `TransactionRequest` extends the Execution API `GenericTransaction` shape to optionally carry signed payloads. Pair with [`TransactionRequest.toRpc`](/api/TransactionRequest/toRpc) to produce an `eth_sendTransaction`-shaped payload. Note: the 4844 round-trip `TxEnvelope → TransactionRequest → TxEnvelope` is **lossy** — `sidecars.commitments` and `sidecars.cellProofs` are not preserved on the `TransactionRequest` shape. Callers that need full round-trip parity must carry sidecars out of band. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope, TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: 1n, to: '0x0000000000000000000000000000000000000000', value: 1n }) const request = TransactionEnvelope.toTransactionRequest(envelope) // @log: { // @log: chainId: 1, // @log: maxFeePerGas: 1n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip1559', // @log: value: 1n, // @log: } ``` ## Definition ```ts function toTransactionRequest( envelope: TransactionEnvelope.TxEnvelope, ): TransactionRequest.TransactionRequest ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L720) ## Parameters ### envelope * **Type:** [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#transactionenvelopetxenvelope) The transaction envelope to convert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type A transaction request. `TransactionRequest.TransactionRequest` # TransactionEnvelope.validate Validates a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TransactionEnvelope } from 'ox' ``` ```ts [Entrypoint] import * as TransactionEnvelope from 'ox/TransactionEnvelope' ``` ::: ## Examples ```ts twoslash import { TransactionEnvelope } from 'ox' const valid = TransactionEnvelope.validate({ chainId: 1, maxFeePerGas: 1n, type: 'eip1559' }) // @log: true ``` ## Definition ```ts function validate( envelope: TransactionEnvelope.TxEnvelope, ): boolean ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L762) ## Parameters ### envelope * **Type:** [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#transactionenvelopetxenvelope) The transaction envelope to validate. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type `boolean` # TransactionEnvelope Errors ## `TransactionEnvelope.FeeCapTooHighError` Thrown when a fee cap is too high. ### Examples ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' TxEnvelopeEip1559.assert({ maxFeePerGas: 2n ** 256n - 1n + 1n, chainId: 1 }) // @error: TransactionEnvelope.FeeCapTooHighError: The fee cap (`maxFeePerGas`/`maxPriorityFeePerGas` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1). ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L789) ## `TransactionEnvelope.GasPriceTooHighError` Thrown when a gas price is too high. ### Examples ```ts twoslash import { TxEnvelopeLegacy } from 'ox' TxEnvelopeLegacy.assert({ gasPrice: 2n ** 256n - 1n + 1n, chainId: 1 }) // @error: TransactionEnvelope.GasPriceTooHighError: The gas price (`gasPrice` = 115792089237316195423570985008687907853269984665640564039457584007913.129639936 gwei) cannot be higher than the maximum allowed value (2^256-1). ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L818) ## `TransactionEnvelope.InvalidChainIdError` Thrown when a chain ID is invalid. ### Examples ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' TxEnvelopeEip1559.assert({ chainId: 0 }) // @error: TransactionEnvelope.InvalidChainIdError: Chain ID "0" is invalid. ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L844) ## `TransactionEnvelope.InvalidSerializedError` Thrown when a serialized transaction is invalid. ### Examples ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' TxEnvelopeEip1559.deserialize('0x02c0') // @error: TransactionEnvelope.InvalidSerializedError: Invalid serialized transaction of type "eip1559" was provided. // @error: Serialized Transaction: "0x02c0" // @error: Missing Attributes: chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gas, to, value, data, accessList ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L868) ## `TransactionEnvelope.InvalidSerializedTypeError` Thrown when a serialized transaction type cannot be resolved. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L892) ## `TransactionEnvelope.InvalidTypeError` Thrown when a transaction envelope type cannot be resolved. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L903) ## `TransactionEnvelope.TipAboveFeeCapError` Thrown when a tip is higher than a fee cap. ### Examples ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' TxEnvelopeEip1559.assert({ chainId: 1, maxFeePerGas: 10n, maxPriorityFeePerGas: 11n }) // @error: TransactionEnvelope.TipAboveFeeCapError: The provided tip (`maxPriorityFeePerGas` = 11 gwei) cannot be higher than the fee cap (`maxFeePerGas` = 10 gwei). ``` **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L930) # TransactionEnvelope Types ## `TransactionEnvelope.Base` Base type for a Transaction Envelope. Transaction Envelopes inherit this type. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L17) ## `TransactionEnvelope.BaseRpc` RPC representation of a [`TransactionEnvelope.Base`](/api/TransactionEnvelope/types#base). **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L59) ## `TransactionEnvelope.BaseSigned` Signed representation of a [`TransactionEnvelope.Base`](/api/TransactionEnvelope/types#base). **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L65) ## `TransactionEnvelope.Rpc` RPC representation of a [`TransactionEnvelope.TxEnvelope`](/api/TransactionEnvelope/types#txenvelope). **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L82) ## `TransactionEnvelope.Serialized` Serialized Transaction Envelope. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L90) ## `TransactionEnvelope.TxEnvelope` Transaction Envelope. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L68) ## `TransactionEnvelope.Type` Transaction Envelope type. **Source:** [src/core/TxEnvelope.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelope.ts#L98) # TxEnvelopeEip1559 Utility functions for working with [EIP-1559 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-1559) ## Examples Below are some examples demonstrating common usages of the `TxEnvelopeEip1559` module: * [Instantiating](#instantiating) * [Signing](#signing) * [Serializing](#serializing) * [Sending](#sending) * [Computing Hashes](#computing-hashes) ### Instantiating Transaction Envelopes can be instantiated using [`TxEnvelopeEip1559.from`](/api/TxEnvelopeEip1559/from): ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: { // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip1559', // @log: value: 1000000000000000000n, // @log: } ``` ### Signing Transaction Envelopes can be signed using [`TxEnvelopeEip1559.getSignPayload`](/api/TxEnvelopeEip1559/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign): ```ts twoslash import { Secp256k1, TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const signature = Secp256k1.sign({ // [!code focus] payload: TxEnvelopeEip1559.getSignPayload(envelope), // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] const envelope_signed = TxEnvelopeEip1559.from(envelope, { signature }) ``` ### Serializing Transaction Envelopes can be serialized using [`TxEnvelopeEip1559.serialize`](/api/TxEnvelopeEip1559/serialize): ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip1559.serialize(envelope) // [!code focus] ``` ### Sending We can send a Transaction Envelope to the network by serializing the signed envelope with `.serialize`, and then broadcasting it over JSON-RPC with `eth_sendRawTransaction`. In this example, we will use [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC. ```ts twoslash import { RpcTransport, TxEnvelopeEip1559, Secp256k1, Value } from 'ox' // Construct the Envelope. const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) // Sign over the Envelope. const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...' }) // Serialize the Envelope with the Signature. // [!code focus] const serialized = TxEnvelopeEip1559.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus] const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) // [!code focus] const hash = await transport.request({ // [!code focus] method: 'eth_sendRawTransaction', // [!code focus] params: [serialized] // [!code focus] }) // [!code focus] ``` If you are interfacing with an RPC that supports `eth_sendTransaction`, you can also use [`TxEnvelopeEip1559.toRpc`](/api/TxEnvelopeEip1559/toRpc) to convert an Envelope to an RPC-compatible format. This means you can skip the ceremony of manually filling & signing the Transaction. ```ts twoslash import 'ox/window' import { Provider, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) const envelope_rpc = TxEnvelopeEip1559.toRpc(envelope) const provider = Provider.from(window.ethereum) const hash = await provider.request({ method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ### Computing Hashes Transaction Hashes can be computed using [`TxEnvelopeEip1559.hash`](/api/TxEnvelopeEip1559/hash): ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, nonce: 0n, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n, data: '0x' }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip1559.from(envelope, { signature }) const hash = TxEnvelopeEip1559.hash(envelope_signed) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip1559.assert`](/api/TxEnvelopeEip1559/assert) | Asserts a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) is valid. | | [`TxEnvelopeEip1559.deserialize`](/api/TxEnvelopeEip1559/deserialize) | Deserializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) from its serialized form. | | [`TxEnvelopeEip1559.from`](/api/TxEnvelopeEip1559/from) | Converts an arbitrary transaction object into an EIP-1559 Transaction Envelope. | | [`TxEnvelopeEip1559.getSignPayload`](/api/TxEnvelopeEip1559/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). | | [`TxEnvelopeEip1559.hash`](/api/TxEnvelopeEip1559/hash) | Hashes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). This is the "transaction hash". | | [`TxEnvelopeEip1559.serialize`](/api/TxEnvelopeEip1559/serialize) | Serializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). | | [`TxEnvelopeEip1559.toRpc`](/api/TxEnvelopeEip1559/toRpc) | Converts an [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) to an [`TxEnvelopeEip1559.Rpc`](/api/TxEnvelopeEip1559/types#rpc). | | [`TxEnvelopeEip1559.validate`](/api/TxEnvelopeEip1559/validate) | Validates a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). Returns `true` if the envelope is valid, `false` otherwise. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip1559.Rpc`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559rpc) | | | [`TxEnvelopeEip1559.Serialized`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559serialized) | | | [`TxEnvelopeEip1559.SerializedType`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559serializedtype) | | | [`TxEnvelopeEip1559.Signed`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559signed) | | | [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559txenvelopeeip1559) | | | [`TxEnvelopeEip1559.Type`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559type) | | # TxEnvelopeEip1559.assert Asserts a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) is valid. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' TxEnvelopeEip1559.assert({ maxFeePerGas: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @error: FeeCapTooHighError: // @error: The fee cap (`masFeePerGas` = 115792089237316195423570985008687907853269984665640564039457584007913 gwei) cannot be // @error: higher than the maximum allowed value (2^256-1). ``` ## Definition ```ts function assert( envelope: PartialBy, ): void ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L72) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to assert. ## Return Type `void` # TxEnvelopeEip1559.deserialize Deserializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) from its serialized form. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.deserialize( '0x02ef0182031184773594008477359400809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0' ) // @log: { // @log: type: 'eip1559', // @log: nonce: 785n, // @log: maxFeePerGas: 2000000000n, // @log: gas: 1000000n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function deserialize( serialized: Serialized, ): Compute ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L122) ## Parameters ### serialized * **Type:** `Serialized` The serialized transaction. ## Return Type Deserialized Transaction Envelope. `Compute` # TxEnvelopeEip1559.from Converts an arbitrary transaction object into an EIP-1559 Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` ### Attaching Signatures It is possible to attach a `signature` to the transaction envelope. ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip1559.from(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // @log: { // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip1559', // @log: value: 1000000000000000000n, // @log: r: 125...n, // @log: s: 642...n, // @log: yParity: 0, // @log: } ``` ### From Serialized It is possible to instantiate an EIP-1559 Transaction Envelope from a [`TxEnvelopeEip1559.Serialized`](/api/TxEnvelopeEip1559/types#serialized) value. ```ts twoslash import { TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.from( '0x02f858018203118502540be4008504a817c800809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c08477359400e1a001627c687261b0e7f8638af1112efa8a77e23656f6e7945275b19e9deed80261' ) // @log: { // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip1559', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function from( envelope: envelope | UnionPartialBy | Serialized, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L292) ## Parameters ### envelope * **Type:** `envelope | UnionPartialBy | Serialized` The transaction object to convert. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type An EIP-1559 Transaction Envelope. `from.ReturnType` # TxEnvelopeEip1559.getSignPayload Returns the payload to sign for a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples The example below demonstrates how to compute the sign payload which can be used with ECDSA signing utilities like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Secp256k1, TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const payload = TxEnvelopeEip1559.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelopeEip1559, ): getSignPayload.ReturnType ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L373) ## Parameters ### envelope * **Type:** `TxEnvelopeEip1559` The transaction envelope to get the sign payload for. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type The sign payload. `getSignPayload.ReturnType` # TxEnvelopeEip1559.hash Hashes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { Secp256k1, TxEnvelopeEip1559 } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip1559.from(envelope, { signature }) const hash = TxEnvelopeEip1559.hash(envelope_signed) // [!code focus] ``` ## Definition ```ts function hash( envelope: TxEnvelopeEip1559, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L417) ## Parameters ### envelope * **Type:** `TxEnvelopeEip1559` The EIP-1559 Transaction Envelope to hash. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TxEnvelopeEip1559.serialize Serializes a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip1559.serialize(envelope) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the serialized Transaction Envelope. ```ts twoslash import { Secp256k1, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip1559.getSignPayload(envelope), privateKey: '0x...' }) const serialized = TxEnvelopeEip1559.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // ... send `serialized` transaction to JSON-RPC `eth_sendRawTransaction` ``` ## Definition ```ts function serialize( envelope: PartialBy, options?: serialize.Options, ): Serialized ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L502) ## Parameters ### envelope * **Type:** `PartialBy` The Transaction Envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type The serialized Transaction Envelope. `Serialized` # TxEnvelopeEip1559.toRpc Converts an [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559) to an [`TxEnvelopeEip1559.Rpc`](/api/TxEnvelopeEip1559/types#rpc). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { RpcRequest, TxEnvelopeEip1559, Value } from 'ox' const envelope = TxEnvelopeEip1559.from({ chainId: 1, nonce: 0n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1') }) const envelope_rpc = TxEnvelopeEip1559.toRpc(envelope) // [!code focus] const request = RpcRequest.from({ id: 0, method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ## Definition ```ts function toRpc( envelope: toRpc.Input, ): Rpc ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L583) ## Parameters ### envelope * **Type:** `toRpc.Input` The EIP-1559 transaction envelope to convert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type An RPC-formatted EIP-1559 transaction envelope. `Rpc` # TxEnvelopeEip1559.validate Validates a [`TxEnvelopeEip1559.TxEnvelopeEip1559`](/api/TxEnvelopeEip1559/types#txenvelopeeip1559). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip1559 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip1559 from 'ox/TxEnvelopeEip1559' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip1559, Value } from 'ox' const valid = TxEnvelopeEip1559.assert({ maxFeePerGas: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: false ``` ## Definition ```ts function validate( envelope: PartialBy, ): boolean ``` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L642) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to validate. ## Return Type `boolean` # TxEnvelopeEip1559 Types ## `TxEnvelopeEip1559.Rpc` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L35) ## `TxEnvelopeEip1559.Serialized` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L42) ## `TxEnvelopeEip1559.SerializedType` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L45) ## `TxEnvelopeEip1559.Signed` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L47) ## `TxEnvelopeEip1559.TxEnvelopeEip1559` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L19) ## `TxEnvelopeEip1559.Type` **Source:** [src/core/TxEnvelopeEip1559.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip1559.ts#L50) # TxEnvelopeEip2930 Utility functions for working with [EIP-2930 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-2930) ## Examples Below are some examples demonstrating common usages of the `TxEnvelopeEip2930` module: * [Instantiating](#instantiating) * [Signing](#signing) * [Serializing](#serializing) * [Sending](#sending) * [Computing Hashes](#computing-hashes) ### Instantiating Transaction Envelopes can be instantiated using [`TxEnvelopeEip2930.from`](/api/TxEnvelopeEip2930/from): ```ts twoslash // @noErrors import { TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, accessList: [...], gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1'), }) ``` ### Signing Transaction Envelopes can be signed using [`TxEnvelopeEip2930.getSignPayload`](/api/TxEnvelopeEip2930/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign): ```ts twoslash import { Secp256k1, TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const payload = TxEnvelopeEip2930.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ### Serializing Transaction Envelopes can be serialized using [`TxEnvelopeEip2930.serialize`](/api/TxEnvelopeEip2930/serialize): ```ts twoslash import { Secp256k1, TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const serialized = TxEnvelopeEip2930.serialize(envelope) // [!code focus] ``` ### Sending We can send a Transaction Envelope to the network by serializing the signed envelope with `.serialize`, and then broadcasting it over JSON-RPC with `eth_sendRawTransaction`. In this example, we will use [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC. ```ts twoslash import { RpcTransport, TxEnvelopeEip2930, Secp256k1, Value } from 'ox' // Construct the Envelope. const envelope = TxEnvelopeEip2930.from({ accessList: [], chainId: 1, gasPrice: Value.fromGwei('10'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) // Sign over the Envelope. const signature = Secp256k1.sign({ payload: TxEnvelopeEip2930.getSignPayload(envelope), privateKey: '0x...' }) // Serialize the Envelope with the Signature. // [!code focus] const serialized = TxEnvelopeEip2930.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus] const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) // [!code focus] const hash = await transport.request({ // [!code focus] method: 'eth_sendRawTransaction', // [!code focus] params: [serialized] // [!code focus] }) // [!code focus] ``` If you are interfacing with an RPC that supports `eth_sendTransaction`, you can also use [`TxEnvelopeEip2930.toRpc`](/api/TxEnvelopeEip2930/toRpc) to convert an Envelope to an RPC-compatible format. This means you can skip the ceremony of manually filling & signing the Transaction. ```ts twoslash import 'ox/window' import { Provider, TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ accessList: [], chainId: 1, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) const envelope_rpc = TxEnvelopeEip2930.toRpc(envelope) const provider = Provider.from(window.ethereum) const hash = await provider.request({ method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ### Computing Hashes Transaction Hashes can be computed using [`TxEnvelopeEip2930.hash`](/api/TxEnvelopeEip2930/hash): ```ts twoslash import { Secp256k1, TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n, data: '0x' }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip2930.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip2930.from(envelope, { signature }) const hash = TxEnvelopeEip2930.hash(envelope_signed) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip2930.assert`](/api/TxEnvelopeEip2930/assert) | Asserts a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) is valid. | | [`TxEnvelopeEip2930.deserialize`](/api/TxEnvelopeEip2930/deserialize) | Deserializes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) from its serialized form. | | [`TxEnvelopeEip2930.from`](/api/TxEnvelopeEip2930/from) | Converts an arbitrary transaction object into an EIP-2930 Transaction Envelope. | | [`TxEnvelopeEip2930.getSignPayload`](/api/TxEnvelopeEip2930/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). | | [`TxEnvelopeEip2930.hash`](/api/TxEnvelopeEip2930/hash) | Hashes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). This is the "transaction hash". | | [`TxEnvelopeEip2930.serialize`](/api/TxEnvelopeEip2930/serialize) | Serializes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). | | [`TxEnvelopeEip2930.toRpc`](/api/TxEnvelopeEip2930/toRpc) | Converts an [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) to an [`TxEnvelopeEip2930.Rpc`](/api/TxEnvelopeEip2930/types#rpc). | | [`TxEnvelopeEip2930.validate`](/api/TxEnvelopeEip2930/validate) | Validates a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). Returns `true` if the envelope is valid, `false` otherwise. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip2930.Rpc`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930rpc) | | | [`TxEnvelopeEip2930.Serialized`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930serialized) | | | [`TxEnvelopeEip2930.SerializedType`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930serializedtype) | | | [`TxEnvelopeEip2930.Signed`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930signed) | | | [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930txenvelopeeip2930) | | | [`TxEnvelopeEip2930.Type`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930type) | | # TxEnvelopeEip2930.assert Asserts a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) is valid. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip2930, Value } from 'ox' TxEnvelopeEip2930.assert({ gasPrice: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @error: GasPriceTooHighError: // @error: The gas price (`gasPrice` = 115792089237316195423570985008687907853269984665640564039457584007913 gwei) cannot be // @error: higher than the maximum allowed value (2^256-1). ``` ## Definition ```ts function assert( envelope: PartialBy, ): void ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L70) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to assert. ## Return Type `void` # TxEnvelopeEip2930.deserialize Deserializes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) from its serialized form. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.deserialize( '0x01ef0182031184773594008477359400809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0' ) // @log: { // @log: type: 'eip2930', // @log: nonce: 785n, // @log: gasPrice: 2000000000n, // @log: gas: 1000000n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function deserialize( serialized: TxEnvelopeEip2930.Serialized, ): TxEnvelopeEip2930 ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L110) ## Parameters ### serialized * **Type:** [`TxEnvelopeEip2930.Serialized`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930serialized) The serialized transaction. ## Return Type Deserialized Transaction Envelope. `TxEnvelopeEip2930` # TxEnvelopeEip2930.from Converts an arbitrary transaction object into an EIP-2930 Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash // @noErrors import { TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, accessList: [...], gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1'), }) ``` ### Attaching Signatures It is possible to attach a `signature` to the transaction envelope. ```ts twoslash import { Secp256k1, TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip2930.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip2930.from(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // @log: { // @log: chainId: 1, // @log: gasPrice: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip2930', // @log: value: 1000000000000000000n, // @log: r: 125...n, // @log: s: 642...n, // @log: yParity: 0, // @log: } ``` ### From Serialized It is possible to instantiate an EIP-2930 Transaction Envelope from a [`TxEnvelopeEip2930.Serialized`](/api/TxEnvelopeEip2930/types#serialized) value. ```ts twoslash import { TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.from( '0x01f858018203118502540be4008504a817c800809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c08477359400e1a001627c687261b0e7f8638af1112efa8a77e23656f6e7945275b19e9deed80261' ) // @log: { // @log: chainId: 1, // @log: gasPrice: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip2930', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function from( envelope: envelope | UnionPartialBy | Serialized, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L270) ## Parameters ### envelope * **Type:** `envelope | UnionPartialBy | Serialized` The transaction object to convert. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type A [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) `from.ReturnType` # TxEnvelopeEip2930.getSignPayload Returns the payload to sign for a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples The example below demonstrates how to compute the sign payload which can be used with ECDSA signing utilities like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash import { Secp256k1, TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const payload = TxEnvelopeEip2930.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelopeEip2930, ): getSignPayload.ReturnType ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L351) ## Parameters ### envelope * **Type:** `TxEnvelopeEip2930` The transaction envelope to get the sign payload for. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type The sign payload. `getSignPayload.ReturnType` # TxEnvelopeEip2930.hash Hashes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { Secp256k1, TxEnvelopeEip2930 } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip2930.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip2930.from(envelope, { signature }) const hash = TxEnvelopeEip2930.hash(envelope_signed) // [!code focus] ``` ## Definition ```ts function hash( envelope: TxEnvelopeEip2930, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L395) ## Parameters ### envelope * **Type:** `TxEnvelopeEip2930` The EIP-2930 Transaction Envelope to hash. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TxEnvelopeEip2930.serialize Serializes a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip2930.serialize(envelope) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the serialized Transaction Envelope. ```ts twoslash import { Secp256k1, TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip2930.getSignPayload(envelope), privateKey: '0x...' }) const serialized = TxEnvelopeEip2930.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // ... send `serialized` transaction to JSON-RPC `eth_sendRawTransaction` ``` ## Definition ```ts function serialize( envelope: PartialBy, options?: serialize.Options, ): Serialized ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L478) ## Parameters ### envelope * **Type:** `PartialBy` The Transaction Envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type The serialized Transaction Envelope. [`TxEnvelopeEip2930.Serialized`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930serialized) # TxEnvelopeEip2930.toRpc Converts an [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930) to an [`TxEnvelopeEip2930.Rpc`](/api/TxEnvelopeEip2930/types#rpc). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { RpcRequest, TxEnvelopeEip2930, Value } from 'ox' const envelope = TxEnvelopeEip2930.from({ chainId: 1, nonce: 0n, gas: 21000n, maxFeePerGas: Value.fromGwei('20'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1') }) const envelope_rpc = TxEnvelopeEip2930.toRpc(envelope) // [!code focus] const request = RpcRequest.from({ id: 0, method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ## Definition ```ts function toRpc( envelope: toRpc.Input, ): Rpc ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L549) ## Parameters ### envelope * **Type:** `toRpc.Input` The EIP-2930 transaction envelope to convert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type An RPC-formatted EIP-2930 transaction envelope. `Rpc` # TxEnvelopeEip2930.validate Validates a [`TxEnvelopeEip2930.TxEnvelopeEip2930`](/api/TxEnvelopeEip2930/types#txenvelopeeip2930). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip2930 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip2930 from 'ox/TxEnvelopeEip2930' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip2930, Value } from 'ox' const valid = TxEnvelopeEip2930.assert({ gasPrice: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: false ``` ## Definition ```ts function validate( envelope: PartialBy, ): boolean ``` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L601) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to validate. ## Return Type `boolean` # TxEnvelopeEip2930 Types ## `TxEnvelopeEip2930.Rpc` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L33) ## `TxEnvelopeEip2930.Serialized` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L40) ## `TxEnvelopeEip2930.SerializedType` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L43) ## `TxEnvelopeEip2930.Signed` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L45) ## `TxEnvelopeEip2930.TxEnvelopeEip2930` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L19) ## `TxEnvelopeEip2930.Type` **Source:** [src/core/TxEnvelopeEip2930.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip2930.ts#L48) # TxEnvelopeEip4844 Utility functions for working with [EIP-4844 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-4844) ## Examples Below are some examples demonstrating common usages of the `TxEnvelopeEip4844` module: * [Instantiating Blobs](#instantiating-blobs) * [Instantiating](#instantiating) * [Signing](#signing) * [Serializing](#serializing) * [Sending](#sending) * [Computing Hashes](#computing-hashes) ### Instantiating Blobs Blobs can be instantiated using [`Blobs.from`](/api/Blobs/from): ```ts twoslash import { Blobs, Hex } from 'ox' const blobs = Blobs.from(Hex.fromString('Hello World!')) ``` ### Instantiating Transaction Envelopes can be instantiated using [`TxEnvelopeEip4844.from`](/api/TxEnvelopeEip4844/from): ```ts twoslash // @noErrors import { Blobs, Hex, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from(Hex.fromString('Hello World!')) const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ chainId: 1, blobVersionedHashes, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` ### Signing Transaction Envelopes can be signed using [`TxEnvelopeEip4844.getSignPayload`](/api/TxEnvelopeEip4844/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign): ```ts twoslash // @noErrors import { Blobs, Secp256k1, TxEnvelopeEip4844 } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, nonce: 0n, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ // [!code focus] payload: TxEnvelopeEip4844.getSignPayload(envelope), // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] const envelope_signed = TxEnvelopeEip4844.from(envelope, { signature }) ``` ### Serializing Transaction Envelopes can be serialized using [`TxEnvelopeEip4844.serialize`](/api/TxEnvelopeEip4844/serialize): ```ts twoslash // @noErrors import { Blobs, TxEnvelopeEip4844 } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip4844.serialize(envelope) // [!code focus] ``` ### Sending We can send a Transaction Envelope to the network by serializing the signed envelope with `.serialize`, and then broadcasting it over JSON-RPC with `eth_sendRawTransaction`. In this example, we will use [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC. ```ts twoslash // @noErrors import { Blobs, RpcTransport, TxEnvelopeEip4844, Secp256k1, Value } from 'ox' import { kzg } from './kzg' // Compute the Blob Versioned Hashes. const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) // Construct the Envelope. const envelope = TxEnvelopeEip4844.from({ chainId: 1, blobVersionedHashes, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 0n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) // Sign over the Envelope. const signature = Secp256k1.sign({ payload: TxEnvelopeEip4844.getSignPayload(envelope), privateKey: '0x...' }) // Serialize the Envelope with the Signature. // [!code focus] const serialized = TxEnvelopeEip4844.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus] const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) // [!code focus] const hash = await transport.request({ // [!code focus] method: 'eth_sendRawTransaction', // [!code focus] params: [serialized] // [!code focus] }) // [!code focus] ``` ### Computing Hashes Transaction Hashes can be computed using [`TxEnvelopeEip4844.hash`](/api/TxEnvelopeEip4844/hash): ```ts twoslash // @noErrors import { Blobs, Secp256k1, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip4844.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip4844.from(envelope, { signature }) const hash = TxEnvelopeEip4844.hash(envelope_signed) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip4844.assert`](/api/TxEnvelopeEip4844/assert) | Asserts a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) is valid. | | [`TxEnvelopeEip4844.deserialize`](/api/TxEnvelopeEip4844/deserialize) | Deserializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) from its serialized form. | | [`TxEnvelopeEip4844.from`](/api/TxEnvelopeEip4844/from) | Converts an arbitrary transaction object into an EIP-4844 Transaction Envelope. | | [`TxEnvelopeEip4844.getSignPayload`](/api/TxEnvelopeEip4844/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). | | [`TxEnvelopeEip4844.hash`](/api/TxEnvelopeEip4844/hash) | Hashes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). This is the "transaction hash". | | [`TxEnvelopeEip4844.serialize`](/api/TxEnvelopeEip4844/serialize) | Serializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). | | [`TxEnvelopeEip4844.toRpc`](/api/TxEnvelopeEip4844/toRpc) | Converts an [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) to an [`TxEnvelopeEip4844.Rpc`](/api/TxEnvelopeEip4844/types#rpc). | | [`TxEnvelopeEip4844.validate`](/api/TxEnvelopeEip4844/validate) | Validates a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). Returns `true` if the envelope is valid, `false` otherwise. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip4844.LegacyBlobSidecarWrapperError`](/api/TxEnvelopeEip4844/errors#txenvelopeeip4844legacyblobsidecarwrappererror) | Thrown when attempting to deserialize a legacy 4-element EIP-4844 network wrapper. Only the PeerDAS (EIP-7594) 5-element wrapper with `wrapper_version = 0x01` is accepted. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip4844.Rpc`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844rpc) | | | [`TxEnvelopeEip4844.Serialized`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844serialized) | | | [`TxEnvelopeEip4844.SerializedType`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844serializedtype) | | | [`TxEnvelopeEip4844.Sidecars`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844sidecars) | PeerDAS (EIP-7594) sidecars carried alongside a type-3 transaction in the `PooledTransactions` p2p message. | | [`TxEnvelopeEip4844.Signed`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844signed) | | | [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844txenvelopeeip4844) | | | [`TxEnvelopeEip4844.Type`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844type) | | # TxEnvelopeEip4844.assert Asserts a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) is valid. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip4844, Value } from 'ox' TxEnvelopeEip4844.assert({ blobVersionedHashes: [], chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @error: EmptyBlobVersionedHashesError: Blob versioned hashes must not be empty. ``` ## Definition ```ts function assert( envelope: PartialBy, ): void ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L104) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to assert. ## Return Type `void` # TxEnvelopeEip4844.deserialize Deserializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) from its serialized form. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip4844 } from 'ox' const envelope = TxEnvelopeEip4844.deserialize( '0x03ef0182031184773594008477359400809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0' ) // @log: { // @log: blobVersionedHashes: [...], // @log: type: 'eip4844', // @log: nonce: 785n, // @log: maxFeePerGas: 2000000000n, // @log: gas: 1000000n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function deserialize( serialized: Serialized, ): Compute ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L163) ## Parameters ### serialized * **Type:** `Serialized` The serialized transaction. ## Return Type Deserialized Transaction Envelope. `Compute` # TxEnvelopeEip4844.from Converts an arbitrary transaction object into an EIP-4844 Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ chainId: 1, blobVersionedHashes, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` ### Attaching Signatures It is possible to attach a `signature` to the transaction envelope. ```ts twoslash // @noErrors import { Blobs, Secp256k1, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip4844.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip4844.from(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // @log: { // @log: blobVersionedHashes: [...], // @log: chainId: 1, // @log: maxFeePerBlobGas: 3000000000n, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip4844', // @log: value: 1000000000000000000n, // @log: r: 125...n, // @log: s: 642...n, // @log: yParity: 0, // @log: } ``` ### From Serialized It is possible to instantiate an EIP-4844 Transaction Envelope from a [`TxEnvelopeEip4844.Serialized`](/api/TxEnvelopeEip4844/types#serialized) value. ```ts twoslash import { TxEnvelopeEip4844 } from 'ox' const envelope = TxEnvelopeEip4844.from( '0x03f858018203118502540be4008504a817c800809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c08477359400e1a001627c687261b0e7f8638af1112efa8a77e23656f6e7945275b19e9deed80261' ) // @log: { // @log: blobVersionedHashes: [...], // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip4844', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function from( envelope: envelope | UnionPartialBy | Serialized, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L437) ## Parameters ### envelope * **Type:** `envelope | UnionPartialBy | Serialized` The transaction object to convert. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type An EIP-4844 Transaction Envelope. `from.ReturnType` # TxEnvelopeEip4844.getSignPayload Returns the payload to sign for a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples The example below demonstrates how to compute the sign payload which can be used with ECDSA signing utilities like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash // @noErrors import { Blobs, Secp256k1, TxEnvelopeEip4844 } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const payload = TxEnvelopeEip4844.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelopeEip4844, ): getSignPayload.ReturnType ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L526) ## Parameters ### envelope * **Type:** `TxEnvelopeEip4844` The transaction envelope to get the sign payload for. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type The sign payload. `getSignPayload.ReturnType` # TxEnvelopeEip4844.hash Hashes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs, TxEnvelopeEip4844 } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const hash = TxEnvelopeEip4844.hash(envelope) // [!code focus] ``` ## Definition ```ts function hash( envelope: TxEnvelopeEip4844, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L569) ## Parameters ### envelope * **Type:** `TxEnvelopeEip4844` The EIP-4844 Transaction Envelope to hash. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TxEnvelopeEip4844.serialize Serializes a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs, TxEnvelopeEip4844 } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip4844.serialize(envelope) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the serialized Transaction Envelope. ```ts twoslash // @noErrors import { Blobs, Secp256k1, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, maxFeePerBlobGas: Value.fromGwei('3'), maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip4844.getSignPayload(envelope), privateKey: '0x...' }) const serialized = TxEnvelopeEip4844.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // ... send `serialized` transaction to JSON-RPC `eth_sendRawTransaction` ``` ## Definition ```ts function serialize( envelope: PartialBy, options?: serialize.Options, ): Serialized ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L678) ## Parameters ### envelope * **Type:** `PartialBy` The Transaction Envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS sidecars to append, producing the 5-element network wrapper. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type The serialized Transaction Envelope. `Serialized` # TxEnvelopeEip4844.toRpc Converts an [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844) to an [`TxEnvelopeEip4844.Rpc`](/api/TxEnvelopeEip4844/types#rpc). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash // @noErrors import { Blobs, RpcRequest, TxEnvelopeEip4844, Value } from 'ox' import { kzg } from './kzg' const blobs = Blobs.from('0xdeadbeef') const blobVersionedHashes = Blobs.toVersionedHashes(blobs, { kzg }) const envelope = TxEnvelopeEip4844.from({ blobVersionedHashes, chainId: 1, nonce: 0n, gas: 21000n, maxFeePerBlobGas: Value.fromGwei('20'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1') }) const envelope_rpc = TxEnvelopeEip4844.toRpc(envelope) // [!code focus] const request = RpcRequest.from({ id: 0, method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ## Definition ```ts function toRpc( envelope: toRpc.Input, ): Rpc ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L795) ## Parameters ### envelope * **Type:** `toRpc.Input` The EIP-4844 transaction envelope to convert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.blobVersionedHashes * **Type:** `readonly 0x${string}[]` Versioned hashes of blobs to be included in the transaction. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerBlobGas * **Type:** `bigintType` * **Optional** Maximum total fee per gas sender is willing to pay for blob gas (in wei). #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.sidecars * **Type:** `Sidecars` * **Optional** PeerDAS (EIP-7594) sidecars associated with this transaction. When defined, the envelope serializes into the 5-element "PooledTransactions" network wrapper (`rlp([tx_body, wrapper_version, blobs, commitments, cell_proofs])`). #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type An RPC-formatted EIP-4844 transaction envelope. `Rpc` # TxEnvelopeEip4844.validate Validates a [`TxEnvelopeEip4844.TxEnvelopeEip4844`](/api/TxEnvelopeEip4844/types#txenvelopeeip4844). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip4844 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip4844 from 'ox/TxEnvelopeEip4844' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip4844, Value } from 'ox' const valid = TxEnvelopeEip4844.assert({ blobVersionedHashes: [], chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: false ``` ## Definition ```ts function validate( envelope: PartialBy, ): boolean ``` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L857) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to validate. ## Return Type `boolean` # TxEnvelopeEip4844 Errors ## `TxEnvelopeEip4844.LegacyBlobSidecarWrapperError` Thrown when attempting to deserialize a legacy 4-element EIP-4844 network wrapper. Only the PeerDAS (EIP-7594) 5-element wrapper with `wrapper_version = 0x01` is accepted. **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L875) # TxEnvelopeEip4844 Types ## `TxEnvelopeEip4844.Rpc` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L69) ## `TxEnvelopeEip4844.Serialized` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L76) ## `TxEnvelopeEip4844.SerializedType` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L79) ## `TxEnvelopeEip4844.Sidecars` PeerDAS (EIP-7594) sidecars carried alongside a type-3 transaction in the `PooledTransactions` p2p message. * `blobs` — one 131,072-byte payload per blob (same shape as 4844). - `commitments` — one KZG commitment per blob. - `cellProofs` — flat list of `CELLS_PER_EXT_BLOB * blobs.length` cell KZG proofs (128 per blob). `cellProofs[i * 128 + j]` is the proof for cell `j` of `blobs[i]`'s extended form. **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L58) ## `TxEnvelopeEip4844.Signed` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L81) ## `TxEnvelopeEip4844.TxEnvelopeEip4844` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L21) ## `TxEnvelopeEip4844.Type` **Source:** [src/core/TxEnvelopeEip4844.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip4844.ts#L84) # TxEnvelopeEip7702 Utility functions for working with [EIP-7702 Typed Transaction Envelopes](https://eips.ethereum.org/EIPS/eip-7702) ## Examples Below are some examples demonstrating common usages of the `TxEnvelopeEip7702` module: * [Instantiating](#instantiating) * [Signing](#signing) * [Sending](#sending) ### Instantiating Transaction Envelopes can be instantiated using [`TxEnvelopeEip7702.from`](/api/TxEnvelopeEip7702/from): ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 0n }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationList = [ Authorization.from(authorization, { signature }) ] const envelope = TxEnvelopeEip7702.from({ // [!code focus] authorizationList, // [!code focus] chainId: 1, // [!code focus] maxFeePerGas: Value.fromGwei('10'), // [!code focus] maxPriorityFeePerGas: Value.fromGwei('1'), // [!code focus] to: '0x0000000000000000000000000000000000000000', // [!code focus] value: Value.fromEther('1') // [!code focus] }) // [!code focus] ``` :::tip See [`Authorization`](/api/) for more details on instantiating and signing EIP-7702 Authorizations. ::: ### Signing Transaction Envelopes can be signed using [`TxEnvelopeEip7702.getSignPayload`](/api/TxEnvelopeEip7702/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign): ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 0n }) const signature_auth = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationList = [ Authorization.from(authorization, { signature: signature_auth }) ] const envelope = TxEnvelopeEip7702.from({ authorizationList, chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ // [!code focus] payload: TxEnvelopeEip7702.getSignPayload(envelope), // [!code focus] privateKey: '0x...' // [!code focus] }) const envelope_signed = TxEnvelopeEip7702.from(envelope, { signature }) ``` ### Sending We can send a Transaction Envelope to the network by serializing the signed envelope with `.serialize`, and then broadcasting it over JSON-RPC with `eth_sendRawTransaction`. In this example, we will use [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC. ```ts twoslash import { Authorization, RpcTransport, TxEnvelopeEip7702, Secp256k1, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 0n }) const signature_auth = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationList = [ Authorization.from(authorization, { signature: signature_auth }) ] const envelope = TxEnvelopeEip7702.from({ authorizationList, chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip7702.getSignPayload(envelope), privateKey: '0x...' }) // Serialize the Envelope with the Signature. // [!code focus] const serialized = TxEnvelopeEip7702.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus] const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) // [!code focus] const hash = await transport.request({ // [!code focus] method: 'eth_sendRawTransaction', // [!code focus] params: [serialized] // [!code focus] }) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip7702.assert`](/api/TxEnvelopeEip7702/assert) | Asserts a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) is valid. | | [`TxEnvelopeEip7702.deserialize`](/api/TxEnvelopeEip7702/deserialize) | Deserializes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) from its serialized form. | | [`TxEnvelopeEip7702.from`](/api/TxEnvelopeEip7702/from) | Converts an arbitrary transaction object into an EIP-7702 Transaction Envelope. | | [`TxEnvelopeEip7702.getSignPayload`](/api/TxEnvelopeEip7702/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). | | [`TxEnvelopeEip7702.hash`](/api/TxEnvelopeEip7702/hash) | Hashes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). This is the "transaction hash". | | [`TxEnvelopeEip7702.serialize`](/api/TxEnvelopeEip7702/serialize) | Serializes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). | | [`TxEnvelopeEip7702.toRpc`](/api/TxEnvelopeEip7702/toRpc) | Converts an [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) to an [`TxEnvelopeEip7702.Rpc`](/api/TxEnvelopeEip7702/types#rpc). | | [`TxEnvelopeEip7702.validate`](/api/TxEnvelopeEip7702/validate) | Validates a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). Returns `true` if the envelope is valid, `false` otherwise. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeEip7702.Rpc`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702rpc) | | | [`TxEnvelopeEip7702.Serialized`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702serialized) | | | [`TxEnvelopeEip7702.SerializedType`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702serializedtype) | | | [`TxEnvelopeEip7702.Signed`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702signed) | | | [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702txenvelopeeip7702) | | | [`TxEnvelopeEip7702.Type`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702type) | | # TxEnvelopeEip7702.assert Asserts a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) is valid. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip7702, Value } from 'ox' TxEnvelopeEip7702.assert({ authorizationList: [], maxFeePerGas: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @error: FeeCapTooHighError: // @error: The fee cap (`masFeePerGas` = 115792089237316195423570985008687907853269984665640564039457584007913 gwei) cannot be // @error: higher than the maximum allowed value (2^256-1). ``` ## Definition ```ts function assert( envelope: PartialBy, ): void ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L77) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to assert. ## Return Type `void` # TxEnvelopeEip7702.deserialize Deserializes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) from its serialized form. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip7702 } from 'ox' const envelope = TxEnvelopeEip7702.deserialize( '0x04ef0182031184773594008477359400809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0' ) // @log: { // @log: authorizationList: [...], // @log: type: 'eip7702', // @log: nonce: 785n, // @log: maxFeePerGas: 2000000000n, // @log: gas: 1000000n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function deserialize( serialized: Serialized, ): Compute ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L123) ## Parameters ### serialized * **Type:** `Serialized` The serialized transaction. ## Return Type Deserialized Transaction Envelope. `Compute` # TxEnvelopeEip7702.from Converts an arbitrary transaction object into an EIP-7702 Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 0n }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationList = [ Authorization.from(authorization, { signature }) ] const envelope = TxEnvelopeEip7702.from({ // [!code focus] authorizationList, // [!code focus] chainId: 1, // [!code focus] maxFeePerGas: Value.fromGwei('10'), // [!code focus] maxPriorityFeePerGas: Value.fromGwei('1'), // [!code focus] to: '0x0000000000000000000000000000000000000000', // [!code focus] value: Value.fromEther('1') // [!code focus] }) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the transaction envelope. ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const envelope = TxEnvelopeEip7702.from({ authorizationList: [...], chainId: 1, maxFeePerGas: Value.fromGwei('10'), maxPriorityFeePerGas: Value.fromGwei('1'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip7702.getSignPayload(envelope), privateKey: '0x...', }) const envelope_signed = TxEnvelopeEip7702.from(envelope, { // [!code focus] signature, // [!code focus] }) // [!code focus] // @log: { // @log: authorizationList: [...], // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: maxPriorityFeePerGas: 1000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip7702', // @log: value: 1000000000000000000n, // @log: r: 125...n, // @log: s: 642...n, // @log: yParity: 0, // @log: } ``` ### From Serialized It is possible to instantiate an EIP-7702 Transaction Envelope from a [`TxEnvelopeEip7702.Serialized`](/api/TxEnvelopeEip7702/types#serialized) value. ```ts twoslash import { TxEnvelopeEip7702 } from 'ox' const envelope = TxEnvelopeEip7702.from( '0x04f858018203118502540be4008504a817c800809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c08477359400e1a001627c687261b0e7f8638af1112efa8a77e23656f6e7945275b19e9deed80261' ) // @log: { // @log: authorizationList: [...], // @log: chainId: 1, // @log: maxFeePerGas: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip7702', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function from( envelope: envelope | UnionPartialBy | Serialized, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L325) ## Parameters ### envelope * **Type:** `envelope | UnionPartialBy | Serialized` The transaction object to convert. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type An EIP-7702 Transaction Envelope. `from.ReturnType` # TxEnvelopeEip7702.getSignPayload Returns the payload to sign for a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples The example below demonstrates how to compute the sign payload which can be used with ECDSA signing utilities like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeEip7702 } from 'ox' const envelope = TxEnvelopeEip7702.from({ authorizationList: [...], chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n, }) const payload = TxEnvelopeEip7702.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelopeEip7702, ): getSignPayload.ReturnType ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L405) ## Parameters ### envelope * **Type:** `TxEnvelopeEip7702` The transaction envelope to get the sign payload for. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type The sign payload. `getSignPayload.ReturnType` # TxEnvelopeEip7702.hash Hashes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeEip7702 } from 'ox' const envelope = TxEnvelopeEip7702.from({ authorizationList: [...], chainId: 1, nonce: 0n, maxFeePerGas: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n, }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip7702.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeEip7702.from(envelope, { signature }) const hash = TxEnvelopeEip7702.hash(envelope_signed) // [!code focus] ``` ## Definition ```ts function hash( envelope: TxEnvelopeEip7702, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L449) ## Parameters ### envelope * **Type:** `TxEnvelopeEip7702` The EIP-7702 Transaction Envelope to hash. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TxEnvelopeEip7702.serialize Serializes a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash // @noErrors import { Authorization, Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const authorization = Authorization.from({ address: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', chainId: 1, nonce: 0n }) const signature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationList = [ Authorization.from(authorization, { signature }) ] const envelope = TxEnvelopeEip7702.from({ authorizationList, chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeEip7702.serialize(envelope) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the serialized Transaction Envelope. ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeEip7702, Value } from 'ox' const envelope = TxEnvelopeEip7702.from({ authorizationList: [...], chainId: 1, maxFeePerGas: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1'), }) const signature = Secp256k1.sign({ payload: TxEnvelopeEip7702.getSignPayload(envelope), privateKey: '0x...', }) const serialized = TxEnvelopeEip7702.serialize(envelope, { // [!code focus] signature, // [!code focus] }) // [!code focus] // ... send `serialized` transaction to JSON-RPC `eth_sendRawTransaction` ``` ## Definition ```ts function serialize( envelope: PartialBy, options?: serialize.Options, ): Serialized ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L554) ## Parameters ### envelope * **Type:** `PartialBy` The Transaction Envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type The serialized Transaction Envelope. `Serialized` # TxEnvelopeEip7702.toRpc Converts an [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702) to an [`TxEnvelopeEip7702.Rpc`](/api/TxEnvelopeEip7702/types#rpc). ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash // @noErrors import { Authorization, RpcRequest, TxEnvelopeEip7702, Value } from 'ox' const envelope = TxEnvelopeEip7702.from({ authorizationList: [...], chainId: 1, nonce: 0n, gas: 21000n, maxFeePerGas: Value.fromGwei('10'), to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1'), }) const envelope_rpc = TxEnvelopeEip7702.toRpc(envelope) // [!code focus] const request = RpcRequest.from({ id: 0, method: 'eth_sendTransaction', params: [envelope_rpc], }) ``` ## Definition ```ts function toRpc( envelope: toRpc.Input, ): Rpc ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L641) ## Parameters ### envelope * **Type:** `toRpc.Input` The EIP-7702 transaction envelope to convert. #### envelope.accessList * **Type:** `readonly { address: abitype_Address; storageKeys: readonly 0x${string}[]; }[]` * **Optional** EIP-2930 Access List. #### envelope.authorizationList * **Type:** `readonly { address: abitype_Address; chainId: numberType; nonce: bigintType; r: 0x${string}; s: 0x${string}; yParity: numberType; }[]` EIP-7702 Authorization List. #### envelope.chainId * **Type:** `numberType` EIP-155 Chain ID. #### envelope.data * **Type:** `0x${string}` * **Optional** Contract code or a hashed method call with encoded args #### envelope.from * **Type:** `Address.Address | undefined` * **Optional** Sender of the transaction. RPC-only metadata; not part of the serialized envelope. Carried here for parity with [`TransactionRequest.TransactionRequest`](/api/TransactionRequest/types#transactionrequest) and [`Transaction.Transaction`](/api/Transaction/types#transaction). #### envelope.gas * **Type:** `bigintType` * **Optional** Gas provided for transaction execution #### envelope.input * **Type:** `0x${string}` * **Optional** #### envelope.maxFeePerGas * **Type:** `bigintType` * **Optional** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). #### envelope.maxPriorityFeePerGas * **Type:** `bigintType` * **Optional** Max priority fee per gas (in wei). #### envelope.nonce * **Type:** `bigintType` * **Optional** Unique number identifying this transaction #### envelope.r * **Type:** `0x${string}` #### envelope.s * **Type:** `0x${string}` #### envelope.to * **Type:** `Address.Address | null | undefined` * **Optional** Transaction recipient #### envelope.type * **Type:** `type` Transaction type #### envelope.v * **Type:** `numberType` * **Optional** #### envelope.value * **Type:** `bigintType` * **Optional** Value in wei sent with this transaction #### envelope.yParity * **Type:** `numberType` * **Optional** ECDSA signature yParity. ## Return Type An RPC-formatted EIP-7702 transaction envelope. `Rpc` # TxEnvelopeEip7702.validate Validates a [`TxEnvelopeEip7702.TxEnvelopeEip7702`](/api/TxEnvelopeEip7702/types#txenvelopeeip7702). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TxEnvelopeEip7702 } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeEip7702 from 'ox/TxEnvelopeEip7702' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeEip7702, Value } from 'ox' const valid = TxEnvelopeEip7702.validate({ authorizationList: [], maxFeePerGas: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: false ``` ## Definition ```ts function validate( envelope: PartialBy, ): boolean ``` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L702) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to validate. ## Return Type `boolean` # TxEnvelopeEip7702 Types ## `TxEnvelopeEip7702.Rpc` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L39) ## `TxEnvelopeEip7702.Serialized` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L46) ## `TxEnvelopeEip7702.SerializedType` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L51) ## `TxEnvelopeEip7702.Signed` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L48) ## `TxEnvelopeEip7702.TxEnvelopeEip7702` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L21) ## `TxEnvelopeEip7702.Type` **Source:** [src/core/TxEnvelopeEip7702.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeEip7702.ts#L54) # TxEnvelopeLegacy Utility functions for working with **Legacy Transaction Envelopes**. ## Examples Below are some examples demonstrating common usages of the `TxEnvelopeLegacy` module: * [Instantiating](#instantiating) * [Signing](#signing) * [Serializing](#serializing) * [Sending](#sending) * [Computing Hashes](#computing-hashes) ### Instantiating Transaction Envelopes can be instantiated using [`TxEnvelopeLegacy.from`](/api/TxEnvelopeLegacy/from): ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` * ### Signing Transaction Envelopes can be signed using [`TxEnvelopeLegacy.getSignPayload`](/api/TxEnvelopeLegacy/getSignPayload) and a signing function such as [`Secp256k1.sign`](/api/Secp256k1/sign) or [`P256.sign`](/api/P256/sign): ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from({ nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const signature = Secp256k1.sign({ // [!code focus] payload: TxEnvelopeLegacy.getSignPayload(envelope), // [!code focus] privateKey: '0x...' // [!code focus] }) // [!code focus] const envelope_signed = TxEnvelopeLegacy.from(envelope, { signature }) ``` ### Serializing Transaction Envelopes can be serialized using [`TxEnvelopeLegacy.serialize`](/api/TxEnvelopeLegacy/serialize): ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeLegacy.serialize(envelope) // [!code focus] ``` ### Sending We can send a Transaction Envelope to the network by serializing the signed envelope with `.serialize`, and then broadcasting it over JSON-RPC with `eth_sendRawTransaction`. In this example, we will use [`RpcTransport.fromHttp`](/api/RpcTransport/fromHttp) to broadcast a `eth_sendRawTransaction` request over HTTP JSON-RPC. ```ts twoslash import { RpcTransport, TxEnvelopeLegacy, Secp256k1, Value } from 'ox' // Construct the Envelope. const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), nonce: 69n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) // Sign over the Envelope. const signature = Secp256k1.sign({ payload: TxEnvelopeLegacy.getSignPayload(envelope), privateKey: '0x...' }) // Serialize the Envelope with the Signature. // [!code focus] const serialized = TxEnvelopeLegacy.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // Broadcast the Envelope with `eth_sendRawTransaction`. // [!code focus] const transport = RpcTransport.fromHttp( 'https://1.rpc.thirdweb.com' ) // [!code focus] const hash = await transport.request({ // [!code focus] method: 'eth_sendRawTransaction', // [!code focus] params: [serialized] // [!code focus] }) // [!code focus] ``` If you are interfacing with an RPC that supports `eth_sendTransaction`, you can also use [`TxEnvelopeLegacy.toRpc`](/api/TxEnvelopeLegacy/toRpc) to convert an Envelope to an RPC-compatible format. This means you can skip the ceremony of manually filling & signing the Transaction. ```ts twoslash import 'ox/window' import { Provider, TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1.5') }) const envelope_rpc = TxEnvelopeLegacy.toRpc(envelope) const provider = Provider.from(window.ethereum) const hash = await provider.request({ method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ### Computing Hashes Transaction Hashes can be computed using [`TxEnvelopeLegacy.hash`](/api/TxEnvelopeLegacy/hash): ```ts twoslash import { Secp256k1, TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n, data: '0x' }) const signature = Secp256k1.sign({ payload: TxEnvelopeLegacy.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeLegacy.from(envelope, { signature }) const hash = TxEnvelopeLegacy.hash(envelope_signed) // [!code focus] ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeLegacy.assert`](/api/TxEnvelopeLegacy/assert) | Asserts a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) is valid. | | [`TxEnvelopeLegacy.deserialize`](/api/TxEnvelopeLegacy/deserialize) | Deserializes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) from its serialized form. | | [`TxEnvelopeLegacy.from`](/api/TxEnvelopeLegacy/from) | Converts an arbitrary transaction object into a legacy Transaction Envelope. | | [`TxEnvelopeLegacy.getSignPayload`](/api/TxEnvelopeLegacy/getSignPayload) | Returns the payload to sign for a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). | | [`TxEnvelopeLegacy.hash`](/api/TxEnvelopeLegacy/hash) | Hashes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). This is the "transaction hash". | | [`TxEnvelopeLegacy.serialize`](/api/TxEnvelopeLegacy/serialize) | Serializes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). | | [`TxEnvelopeLegacy.toRpc`](/api/TxEnvelopeLegacy/toRpc) | Converts an [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) to an [`TxEnvelopeLegacy.Rpc`](/api/TxEnvelopeLegacy/types#rpc). | | [`TxEnvelopeLegacy.validate`](/api/TxEnvelopeLegacy/validate) | Validates a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). Returns `true` if the envelope is valid, `false` otherwise. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`TxEnvelopeLegacy.Rpc`](/api/TxEnvelopeLegacy/types#txenvelopelegacyrpc) | | | [`TxEnvelopeLegacy.Serialized`](/api/TxEnvelopeLegacy/types#txenvelopelegacyserialized) | | | [`TxEnvelopeLegacy.Signed`](/api/TxEnvelopeLegacy/types#txenvelopelegacysigned) | | | [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacytxenvelopelegacy) | | | [`TxEnvelopeLegacy.Type`](/api/TxEnvelopeLegacy/types#txenvelopelegacytype) | | # TxEnvelopeLegacy.assert Asserts a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) is valid. ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' TxEnvelopeLegacy.assert({ gasPrice: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @error: GasPriceTooHighError: // @error: The gas price (`gasPrice` = 115792089237316195423570985008687907853269984665640564039457584007913 gwei) cannot be // @error: higher than the maximum allowed value (2^256-1). ``` ## Definition ```ts function assert( envelope: PartialBy, ): void ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L68) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to assert. ## Return Type `void` # TxEnvelopeLegacy.deserialize Deserializes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) from its serialized form. ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.deserialize( '0x01ef0182031184773594008477359400809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c0' ) // @log: { // @log: type: 'legacy', // @log: nonce: 785n, // @log: gasPrice: 2000000000n, // @log: gas: 1000000n, // @log: to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function deserialize( serialized: Hex.Hex, ): Compute ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L108) ## Parameters ### serialized * **Type:** `Hex.Hex` The serialized transaction. ## Return Type Deserialized Transaction Envelope. `Compute` # TxEnvelopeLegacy.from Converts an arbitrary transaction object into a legacy Transaction Envelope. ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) ``` ### Attaching Signatures It is possible to attach a `signature` to the transaction envelope. ```ts twoslash import { Secp256k1, TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeLegacy.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeLegacy.from(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // @log: { // @log: authorizationList: [...], // @log: chainId: 1, // @log: gasPrice: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'eip7702', // @log: value: 1000000000000000000n, // @log: r: 125...n, // @log: s: 642...n, // @log: yParity: 0, // @log: } ``` ### From Serialized It is possible to instantiate an legacy Transaction Envelope from a [`TxEnvelopeLegacy.Serialized`](/api/TxEnvelopeLegacy/types#serialized) value. ```ts twoslash import { TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from( '0xf858018203118502540be4008504a817c800809470997970c51812dc3a010c7d01b50e0d17dc79c8880de0b6b3a764000080c08477359400e1a001627c687261b0e7f8638af1112efa8a77e23656f6e7945275b19e9deed80261' ) // @log: { // @log: chainId: 1, // @log: gasPrice: 10000000000n, // @log: to: '0x0000000000000000000000000000000000000000', // @log: type: 'legacy', // @log: value: 1000000000000000000n, // @log: } ``` ## Definition ```ts function from( envelope: envelope | UnionPartialBy | Hex.Hex, options?: from.Options, ): from.ReturnType ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L253) ## Parameters ### envelope * **Type:** `envelope | UnionPartialBy | Hex.Hex` The transaction object to convert. ### options * **Type:** `from.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type A legacy Transaction Envelope. `from.ReturnType` # TxEnvelopeLegacy.getSignPayload Returns the payload to sign for a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples The example below demonstrates how to compute the sign payload which can be used with ECDSA signing utilities like [`Secp256k1.sign`](/api/Secp256k1/sign). ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from({ nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const payload = TxEnvelopeLegacy.getSignPayload(envelope) // [!code focus] // @log: '0x...' const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( envelope: TxEnvelopeLegacy, ): getSignPayload.ReturnType ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L347) ## Parameters ### envelope * **Type:** `TxEnvelopeLegacy` The transaction envelope to get the sign payload for. #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. ## Return Type The sign payload. `getSignPayload.ReturnType` # TxEnvelopeLegacy.hash Hashes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). This is the "transaction hash". ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { Secp256k1, TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, nonce: 0n, gasPrice: 1000000000n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: 1000000000000000000n }) const signature = Secp256k1.sign({ payload: TxEnvelopeLegacy.getSignPayload(envelope), privateKey: '0x...' }) const envelope_signed = TxEnvelopeLegacy.from(envelope, { signature }) const hash = TxEnvelopeLegacy.hash(envelope_signed) // [!code focus] ``` ## Definition ```ts function hash( envelope: TxEnvelopeLegacy, options?: hash.Options, ): hash.ReturnType ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L391) ## Parameters ### envelope * **Type:** `TxEnvelopeLegacy` The Legacy Transaction Envelope to hash. #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. ### options * **Type:** `hash.Options` * **Optional** Options. #### options.presign * **Type:** `boolean | presign` * **Optional** Whether to hash this transaction for signing. ## Return Type The hash of the transaction envelope. `hash.ReturnType` # TxEnvelopeLegacy.serialize Serializes a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash // @noErrors import { TxEnvelopeLegacy } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const serialized = TxEnvelopeLegacy.serialize(envelope) // [!code focus] ``` ### Attaching Signatures It is possible to attach a `signature` to the serialized Transaction Envelope. ```ts twoslash // @noErrors import { Secp256k1, TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, gasPrice: Value.fromGwei('10'), to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) const signature = Secp256k1.sign({ payload: TxEnvelopeLegacy.getSignPayload(envelope), privateKey: '0x...' }) const serialized = TxEnvelopeLegacy.serialize(envelope, { // [!code focus] signature // [!code focus] }) // [!code focus] // ... send `serialized` transaction to JSON-RPC `eth_sendRawTransaction` ``` ## Definition ```ts function serialize( envelope: PartialBy, options?: serialize.Options, ): Serialized ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L476) ## Parameters ### envelope * **Type:** `PartialBy` The Transaction Envelope to serialize. ### options * **Type:** `serialize.Options` * **Optional** Options. #### options.signature * **Type:** `{ r: 0x${string}; s: 0x${string}; yParity: number; }` * **Optional** Signature to append to the serialized Transaction Envelope. ## Return Type The serialized Transaction Envelope. [`Serialized`](/api/TxEnvelopeLegacy/types#txenvelopelegacyserialized) # TxEnvelopeLegacy.toRpc Converts an [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy) to an [`TxEnvelopeLegacy.Rpc`](/api/TxEnvelopeLegacy/types#rpc). ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { RpcRequest, TxEnvelopeLegacy, Value } from 'ox' const envelope = TxEnvelopeLegacy.from({ chainId: 1, nonce: 0n, gas: 21000n, to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', value: Value.fromEther('1') }) const envelope_rpc = TxEnvelopeLegacy.toRpc(envelope) // [!code focus] const request = RpcRequest.from({ id: 0, method: 'eth_sendTransaction', params: [envelope_rpc] }) ``` ## Definition ```ts function toRpc( envelope: toRpc.Input, ): Rpc ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L583) ## Parameters ### envelope * **Type:** `toRpc.Input` The legacy transaction envelope to convert. #### envelope.gasPrice * **Type:** `bigintType` * **Optional** Base fee per gas. ## Return Type An RPC-formatted legacy transaction envelope. `Rpc` # TxEnvelopeLegacy.validate Validates a [`TxEnvelopeLegacy.TxEnvelopeLegacy`](/api/TxEnvelopeLegacy/types#txenvelopelegacy). Returns `true` if the envelope is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { TxEnvelopeLegacy } from 'ox' ``` ```ts [Entrypoint] import * as TxEnvelopeLegacy from 'ox/TxEnvelopeLegacy' ``` ::: ## Examples ```ts twoslash import { TxEnvelopeLegacy, Value } from 'ox' const valid = TxEnvelopeLegacy.assert({ gasPrice: 2n ** 256n - 1n + 1n, chainId: 1, to: '0x0000000000000000000000000000000000000000', value: Value.fromEther('1') }) // @log: false ``` ## Definition ```ts function validate( envelope: PartialBy, ): boolean ``` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L659) ## Parameters ### envelope * **Type:** `PartialBy` The transaction envelope to validate. ## Return Type `boolean` # TxEnvelopeLegacy Types ## `TxEnvelopeLegacy.Rpc` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L34) ## `TxEnvelopeLegacy.Serialized` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L41) ## `TxEnvelopeLegacy.Signed` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L43) ## `TxEnvelopeLegacy.TxEnvelopeLegacy` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L19) ## `TxEnvelopeLegacy.Type` **Source:** [src/core/TxEnvelopeLegacy.ts](https://github.com/wevm/ox/blob/main/src/core/TxEnvelopeLegacy.ts#L46) # EntryPoint Utility functions and types for working with [ERC-4337 EntryPoints](https://eips.ethereum.org/EIPS/eip-4337). ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`EntryPoint.Version`](/ercs/erc4337/EntryPoint/types#entrypointversion) | EntryPoint version. | # EntryPoint Types ## `EntryPoint.Version` EntryPoint version. **Source:** [src/erc4337/EntryPoint.ts](https://github.com/wevm/ox/blob/main/src/erc4337/EntryPoint.ts#L6) # RpcSchema Utility types for working with ERC-4337 JSON-RPC schemas. ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`RpcSchema.Bundler`](/ercs/erc4337/RpcSchema/types#rpcschemabundler) | Union of all JSON-RPC Methods for ERC-4337 Bundlers. | | [`RpcSchema.BundlerDebug`](/ercs/erc4337/RpcSchema/types#rpcschemabundlerdebug) | Union of all JSON-RPC Methods for the debug methods of ERC-4337 Bundlers. | # RpcSchema Types ## `RpcSchema.Bundler` Union of all JSON-RPC Methods for ERC-4337 Bundlers. ### Examples ```ts twoslash // @noErrors import { RpcSchema } from 'ox' type Schema = RpcSchema.Bundler // ^? ``` **Source:** [src/erc4337/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/erc4337/RpcSchema.ts#L22) ## `RpcSchema.BundlerDebug` Union of all JSON-RPC Methods for the debug methods of ERC-4337 Bundlers. ### Examples ```ts twoslash // @noErrors import { RpcSchema } from 'ox' type Schema = RpcSchema.BundlerDebug // ^? ``` **Source:** [src/erc4337/RpcSchema.ts](https://github.com/wevm/ox/blob/main/src/erc4337/RpcSchema.ts#L93) # UserOperation Utility functions and types for working with [ERC-4337 User Operations](https://eips.ethereum.org/EIPS/eip-4337). ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperation.from`](/ercs/erc4337/UserOperation/from) | Instantiates a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) from a provided input. | | [`UserOperation.fromPacked`](/ercs/erc4337/UserOperation/fromPacked) | Transforms a "packed" User Operation into a structured [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). | | [`UserOperation.fromRpc`](/ercs/erc4337/UserOperation/fromRpc) | Converts an [`UserOperation.Rpc`](/ercs/erc4337/UserOperation/types#rpc) to an [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). | | [`UserOperation.getSignPayload`](/ercs/erc4337/UserOperation/getSignPayload) | Obtains the signing payload for a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). | | [`UserOperation.hash`](/ercs/erc4337/UserOperation/hash) | Hashes a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). This is the "user operation hash". | | [`UserOperation.toInitCode`](/ercs/erc4337/UserOperation/toInitCode) | Converts a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to `initCode`. | | [`UserOperation.toPacked`](/ercs/erc4337/UserOperation/toPacked) | Transforms a User Operation into "packed" format. | | [`UserOperation.toRpc`](/ercs/erc4337/UserOperation/toRpc) | Converts a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to a [`UserOperation.Rpc`](/ercs/erc4337/UserOperation/types#rpc). | | [`UserOperation.toTypedData`](/ercs/erc4337/UserOperation/toTypedData) | Converts a signed [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to a [`TypedData.Definition`](/api/TypedData/types#definition). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperation.Packed`](/ercs/erc4337/UserOperation/types#useroperationpacked) | Packed User Operation. | | [`UserOperation.Rpc`](/ercs/erc4337/UserOperation/types#useroperationrpc) | RPC User Operation type. | | [`UserOperation.RpcTransactionInfo`](/ercs/erc4337/UserOperation/types#useroperationrpctransactioninfo) | RPC Transaction Info. | | [`UserOperation.RpcV06`](/ercs/erc4337/UserOperation/types#useroperationrpcv06) | RPC User Operation on EntryPoint 0.6 | | [`UserOperation.RpcV07`](/ercs/erc4337/UserOperation/types#useroperationrpcv07) | RPC User Operation on EntryPoint 0.7 | | [`UserOperation.RpcV08`](/ercs/erc4337/UserOperation/types#useroperationrpcv08) | RPC User Operation on EntryPoint 0.8 | | [`UserOperation.RpcV09`](/ercs/erc4337/UserOperation/types#useroperationrpcv09) | RPC User Operation on EntryPoint 0.9 | | [`UserOperation.TransactionInfo`](/ercs/erc4337/UserOperation/types#useroperationtransactioninfo) | Transaction Info. | | [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperationuseroperation) | User Operation. | | [`UserOperation.V06`](/ercs/erc4337/UserOperation/types#useroperationv06) | Type for User Operation on EntryPoint 0.6 | | [`UserOperation.V07`](/ercs/erc4337/UserOperation/types#useroperationv07) | Type for User Operation on EntryPoint 0.7 | | [`UserOperation.V08`](/ercs/erc4337/UserOperation/types#useroperationv08) | Type for User Operation on EntryPoint 0.8 | | [`UserOperation.V09`](/ercs/erc4337/UserOperation/types#useroperationv09) | Type for User Operation on EntryPoint 0.9 | # UserOperation.from Instantiates a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) from a provided input. ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.from({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n }) ``` ### From Packed User Operation ```ts twoslash import { UserOperation } from 'ox/erc4337' const packed: UserOperation.Packed = { accountGasLimits: '0x...', callData: '0xdeadbeef', initCode: '0x', gasFees: '0x...', nonce: 69n, paymasterAndData: '0x', preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', signature: '0x' } const userOperation = UserOperation.from(packed) ``` ### Attaching Signatures ```ts twoslash import { Secp256k1, Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.from({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n }) const payload = UserOperation.getSignPayload( userOperation, { chainId: 1, entryPointAddress: '0x1234567890123456789012345678901234567890', entryPointVersion: '0.7' } ) const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) const userOperation_signed = UserOperation.from( userOperation, { signature } ) // [!code focus] ``` ## Definition ```ts function from( userOperation: userOperation | UserOperation | Packed, options?: from.Options, ): from.ReturnType ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L367) ## Parameters ### userOperation * **Type:** `userOperation | UserOperation | Packed` The user operation to instantiate (structured or packed format). ### options * **Type:** `from.Options` * **Optional** #### options.signature * **Type:** `0x${string} | { r: 0x${string}; s: 0x${string}; yParity: number; } | signature` * **Optional** ## Return Type User Operation. `from.ReturnType` # UserOperation.fromPacked Transforms a "packed" User Operation into a structured [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { UserOperation } from 'ox/erc4337' const packed: UserOperation.Packed = { accountGasLimits: '0x...', callData: '0xdeadbeef', initCode: '0x...', gasFees: '0x...', nonce: 69n, paymasterAndData: '0x', preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', signature: '0x...' } const userOperation = UserOperation.fromPacked(packed) ``` ## Definition ```ts function fromPacked( packed: Packed, ): UserOperation<'0.7', true> ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L841) ## Parameters ### packed * **Type:** [`Packed`](/ercs/erc4337/UserOperation/types#useroperationpacked) The packed user operation to transform. #### packed.accountGasLimits * **Type:** `0x${string}` Concatenation of `verificationGasLimit` (16 bytes) and `callGasLimit` (16 bytes) #### packed.callData * **Type:** `0x${string}` The data to pass to the `sender` during the main execution call. #### packed.gasFees * **Type:** `0x${string}` Concatenation of `maxPriorityFee` (16 bytes) and `maxFeePerGas` (16 bytes) #### packed.initCode * **Type:** `0x${string}` Concatenation of `factory` and `factoryData`. #### packed.nonce * **Type:** `bigint` Anti-replay parameter. #### packed.paymasterAndData * **Type:** `0x${string}` Concatenation of paymaster fields (or empty). #### packed.preVerificationGas * **Type:** `bigint` Extra gas to pay the Bundler. #### packed.sender * **Type:** `abitype_Address` The account making the operation. #### packed.signature * **Type:** `0x${string}` Data passed into the account to verify authorization. ## Return Type The structured user operation. `UserOperation<'0.7', true>` # UserOperation.fromRpc Converts an [`UserOperation.Rpc`](/ercs/erc4337/UserOperation/types#rpc) to an [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.fromRpc({ callData: '0xdeadbeef', callGasLimit: '0x69420', maxFeePerGas: '0x2ca6ae494', maxPriorityFeePerGas: '0x41cc3c0', nonce: '0x357', preVerificationGas: '0x69420', signature: '0x', sender: '0x1234567890123456789012345678901234567890', verificationGasLimit: '0x69420' }) ``` ## Definition ```ts function fromRpc( rpc: Rpc, ): UserOperation ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L434) ## Parameters ### rpc * **Type:** `Rpc` The RPC user operation to convert. ## Return Type An instantiated [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). `UserOperation` # UserOperation.getSignPayload Obtains the signing payload for a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Secp256k1, Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.from({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n }) const payload = UserOperation.getSignPayload( userOperation, { // [!code focus] chainId: 1, // [!code focus] entryPointAddress: '0x1234567890123456789012345678901234567890', // [!code focus] entryPointVersion: '0.6' // [!code focus] } ) // [!code focus] const signature = Secp256k1.sign({ payload, privateKey: '0x...' }) ``` ## Definition ```ts function getSignPayload( userOperation: UserOperation, options: getSignPayload.Options, ): Hex.Hex ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L504) ## Parameters ### userOperation * **Type:** `UserOperation` The user operation to get the sign payload for. #### userOperation.callData * **Type:** `0x${string}` The data to pass to the `sender` during the main execution call. #### userOperation.callGasLimit * **Type:** `bigintType` The amount of gas to allocate the main execution call #### userOperation.initCode * **Type:** `0x${string}` * **Optional** Account init code. Only for new accounts. #### userOperation.maxFeePerGas * **Type:** `bigintType` Maximum fee per gas. #### userOperation.maxPriorityFeePerGas * **Type:** `bigintType` Maximum priority fee per gas. #### userOperation.nonce * **Type:** `bigintType` Anti-replay parameter. #### userOperation.paymasterAndData * **Type:** `0x${string}` * **Optional** Paymaster address with calldata. #### userOperation.preVerificationGas * **Type:** `bigintType` Extra gas to pay the Bundler. #### userOperation.sender * **Type:** `abitype_Address` The account making the operation. #### userOperation.signature * **Type:** `0x${string}` #### userOperation.verificationGasLimit * **Type:** `bigintType` The amount of gas to allocate for the verification step. ### options * **Type:** `getSignPayload.Options` ## Return Type The signing payload for the user operation. `Hex.Hex` # UserOperation.hash Hashes a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation). This is the "user operation hash". ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.hash( { callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n }, { chainId: 1, entryPointAddress: '0x1234567890123456789012345678901234567890', entryPointVersion: '0.6' } ) ``` ## Definition ```ts function hash( userOperation: UserOperation, options: hash.Options, ): Hex.Hex ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L552) ## Parameters ### userOperation * **Type:** `UserOperation` The user operation to hash. #### userOperation.callData * **Type:** `0x${string}` The data to pass to the `sender` during the main execution call. #### userOperation.callGasLimit * **Type:** `bigintType` The amount of gas to allocate the main execution call #### userOperation.initCode * **Type:** `0x${string}` * **Optional** Account init code. Only for new accounts. #### userOperation.maxFeePerGas * **Type:** `bigintType` Maximum fee per gas. #### userOperation.maxPriorityFeePerGas * **Type:** `bigintType` Maximum priority fee per gas. #### userOperation.nonce * **Type:** `bigintType` Anti-replay parameter. #### userOperation.paymasterAndData * **Type:** `0x${string}` * **Optional** Paymaster address with calldata. #### userOperation.preVerificationGas * **Type:** `bigintType` Extra gas to pay the Bundler. #### userOperation.sender * **Type:** `abitype_Address` The account making the operation. #### userOperation.signature * **Type:** `0x${string}` #### userOperation.verificationGasLimit * **Type:** `bigintType` The amount of gas to allocate for the verification step. ### options * **Type:** `hash.Options` #### options.chainId * **Type:** `number` #### options.entryPointAddress * **Type:** `abitype_Address` #### options.entryPointVersion * **Type:** `entrypointVersion | Version` ## Return Type The hash of the user operation. `Hex.Hex` # UserOperation.toInitCode Converts a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to `initCode`. ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const initCode = UserOperation.toInitCode({ authorization: { address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', chainId: 1, nonce: 69n, yParity: 0, r: '0x0000000000000000000000000000000000000000000000000000000000000001', s: '0x0000000000000000000000000000000000000000000000000000000000000002' }, callData: '0xdeadbeef', callGasLimit: 300_000n, factory: '0x7702', factoryData: '0xdeadbeef', maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357' }) ``` ## Definition ```ts function toInitCode( userOperation: UserOperation.UserOperation, ): Hex.Hex ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L707) ## Parameters ### userOperation * **Type:** [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperationuseroperation) The user operation to convert. ## Return Type The init code. `Hex.Hex` # UserOperation.toPacked Transforms a User Operation into "packed" format. ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const packed = UserOperation.toPacked({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', signature: '0x...', verificationGasLimit: 100_000n }) ``` ## Definition ```ts function toPacked( userOperation: UserOperation<'0.7' | '0.8' | '0.9', true>, options?: toPacked.Options, ): Packed ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L745) ## Parameters ### userOperation * **Type:** `UserOperation<'0.7' | '0.8' | '0.9', true>` The user operation to transform. #### userOperation.callData * **Type:** `0x${string}` The data to pass to the `sender` during the main execution call. #### userOperation.callGasLimit * **Type:** `bigintType` The amount of gas to allocate the main execution call #### userOperation.initCode * **Type:** `0x${string}` * **Optional** Account init code. Only for new accounts. #### userOperation.maxFeePerGas * **Type:** `bigintType` Maximum fee per gas. #### userOperation.maxPriorityFeePerGas * **Type:** `bigintType` Maximum priority fee per gas. #### userOperation.nonce * **Type:** `bigintType` Anti-replay parameter. #### userOperation.paymasterAndData * **Type:** `0x${string}` * **Optional** Paymaster address with calldata. #### userOperation.preVerificationGas * **Type:** `bigintType` Extra gas to pay the Bundler. #### userOperation.sender * **Type:** `abitype_Address` The account making the operation. #### userOperation.signature * **Type:** `0x${string}` #### userOperation.verificationGasLimit * **Type:** `bigintType` The amount of gas to allocate for the verification step. ### options * **Type:** `toPacked.Options` * **Optional** #### options.forHash * **Type:** `boolean` * **Optional** Omits the paymaster signature while retaining its marker. ## Return Type The packed user operation. [`Packed`](/ercs/erc4337/UserOperation/types#useroperationpacked) # UserOperation.toRpc Converts a [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to a [`UserOperation.Rpc`](/ercs/erc4337/UserOperation/types#rpc). ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const userOperation = UserOperation.toRpc({ callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', verificationGasLimit: 100_000n }) ``` ## Definition ```ts function toRpc( userOperation: toRpc.Input, ): Rpc ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L952) ## Parameters ### userOperation * **Type:** `toRpc.Input` The user operation to convert. ## Return Type An RPC-formatted user operation. `Rpc` # UserOperation.toTypedData Converts a signed [`UserOperation.UserOperation`](/ercs/erc4337/UserOperation/types#useroperation) to a [`TypedData.Definition`](/api/TypedData/types#definition). ## Imports :::code-group ```ts [Named] import { UserOperation } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperation from 'ox/erc4337/UserOperation' ``` ::: ## Examples ```ts twoslash import { Value } from 'ox' import { UserOperation } from 'ox/erc4337' const typedData = UserOperation.toTypedData( { authorization: { chainId: 1, address: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', nonce: 69n, yParity: 0, r: '0x0000000000000000000000000000000000000000000000000000000000000001', s: '0x0000000000000000000000000000000000000000000000000000000000000002' }, callData: '0xdeadbeef', callGasLimit: 300_000n, maxFeePerGas: Value.fromGwei('20'), maxPriorityFeePerGas: Value.fromGwei('2'), nonce: 69n, preVerificationGas: 100_000n, sender: '0x9f1fdab6458c5fc642fa0f4c5af7473c46837357', signature: '0x...', verificationGasLimit: 100_000n }, { chainId: 1, entryPointAddress: '0x1234567890123456789012345678901234567890' } ) ``` ## Definition ```ts function toTypedData( userOperation: UserOperation<'0.8' | '0.9', true>, options: toTypedData.Options, ): TypedData.Definition ``` **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L1056) ## Parameters ### userOperation * **Type:** `UserOperation<'0.8' | '0.9', true>` The user operation to convert. #### userOperation.callData * **Type:** `0x${string}` The data to pass to the `sender` during the main execution call. #### userOperation.callGasLimit * **Type:** `bigintType` The amount of gas to allocate the main execution call #### userOperation.initCode * **Type:** `0x${string}` * **Optional** Account init code. Only for new accounts. #### userOperation.maxFeePerGas * **Type:** `bigintType` Maximum fee per gas. #### userOperation.maxPriorityFeePerGas * **Type:** `bigintType` Maximum priority fee per gas. #### userOperation.nonce * **Type:** `bigintType` Anti-replay parameter. #### userOperation.paymasterAndData * **Type:** `0x${string}` * **Optional** Paymaster address with calldata. #### userOperation.preVerificationGas * **Type:** `bigintType` Extra gas to pay the Bundler. #### userOperation.sender * **Type:** `abitype_Address` The account making the operation. #### userOperation.signature * **Type:** `0x${string}` #### userOperation.verificationGasLimit * **Type:** `bigintType` The amount of gas to allocate for the verification step. ### options * **Type:** `toTypedData.Options` #### options.chainId * **Type:** `number` #### options.entryPointAddress * **Type:** `abitype_Address` ## Return Type A Typed Data definition. `TypedData.Definition` # UserOperation Types ## `UserOperation.Packed` Packed User Operation. **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L90) ## `UserOperation.Rpc` RPC User Operation type. **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L112) ## `UserOperation.RpcTransactionInfo` RPC Transaction Info. **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L135) ## `UserOperation.RpcV06` RPC User Operation on EntryPoint 0.6 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L177) ## `UserOperation.RpcV07` RPC User Operation on EntryPoint 0.7 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L214) ## `UserOperation.RpcV08` RPC User Operation on EntryPoint 0.8 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L257) ## `UserOperation.RpcV09` RPC User Operation on EntryPoint 0.9 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L276) ## `UserOperation.TransactionInfo` Transaction Info. **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L123) ## `UserOperation.UserOperation` User Operation. **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L69) ## `UserOperation.V06` Type for User Operation on EntryPoint 0.6 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L151) ## `UserOperation.V07` Type for User Operation on EntryPoint 0.7 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L180) ## `UserOperation.V08` Type for User Operation on EntryPoint 0.8 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L217) ## `UserOperation.V09` Type for User Operation on EntryPoint 0.9 **Source:** [src/erc4337/UserOperation.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperation.ts#L266) # UserOperationGas Utility functions and types for working with [ERC-4337 User Operation Gas](https://eips.ethereum.org/EIPS/eip-4337). ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperationGas.fromRpc`](/ercs/erc4337/UserOperationGas/fromRpc) | Converts an [`UserOperationGas.Rpc`](/ercs/erc4337/UserOperationGas/types#rpc) to an [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgas). | | [`UserOperationGas.toRpc`](/ercs/erc4337/UserOperationGas/toRpc) | Converts a [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgas) to a [`UserOperationGas.Rpc`](/ercs/erc4337/UserOperationGas/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperationGas.Rpc`](/ercs/erc4337/UserOperationGas/types#useroperationgasrpc) | RPC User Operation Gas. | | [`UserOperationGas.RpcV06`](/ercs/erc4337/UserOperationGas/types#useroperationgasrpcv06) | RPC User Operation Gas on EntryPoint 0.6 | | [`UserOperationGas.RpcV07`](/ercs/erc4337/UserOperationGas/types#useroperationgasrpcv07) | RPC User Operation Gas on EntryPoint 0.7 | | [`UserOperationGas.RpcV08`](/ercs/erc4337/UserOperationGas/types#useroperationgasrpcv08) | RPC User Operation Gas on EntryPoint 0.8 | | [`UserOperationGas.RpcV09`](/ercs/erc4337/UserOperationGas/types#useroperationgasrpcv09) | RPC User Operation Gas on EntryPoint 0.9 | | [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgasuseroperationgas) | User Operation Gas type. | | [`UserOperationGas.V06`](/ercs/erc4337/UserOperationGas/types#useroperationgasv06) | Type for User Operation Gas on EntryPoint 0.6 | | [`UserOperationGas.V07`](/ercs/erc4337/UserOperationGas/types#useroperationgasv07) | Type for User Operation Gas on EntryPoint 0.7 | | [`UserOperationGas.V08`](/ercs/erc4337/UserOperationGas/types#useroperationgasv08) | Type for User Operation Gas on EntryPoint 0.8 | | [`UserOperationGas.V09`](/ercs/erc4337/UserOperationGas/types#useroperationgasv09) | Type for User Operation Gas on EntryPoint 0.9 | # UserOperationGas.fromRpc Converts an [`UserOperationGas.Rpc`](/ercs/erc4337/UserOperationGas/types#rpc) to an [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgas). ## Imports :::code-group ```ts [Named] import { UserOperationGas } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperationGas from 'ox/erc4337/UserOperationGas' ``` ::: ## Examples ```ts twoslash import { UserOperationGas } from 'ox/erc4337' const userOperationGas = UserOperationGas.fromRpc({ callGasLimit: '0x69420', preVerificationGas: '0x69420', verificationGasLimit: '0x69420' }) ``` ## Definition ```ts function fromRpc( rpc: Rpc, ): UserOperationGas ``` **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L73) ## Parameters ### rpc * **Type:** `Rpc` The RPC user operation gas to convert. ## Return Type An instantiated [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgas). `UserOperationGas` # UserOperationGas.toRpc Converts a [`UserOperationGas.UserOperationGas`](/ercs/erc4337/UserOperationGas/types#useroperationgas) to a [`UserOperationGas.Rpc`](/ercs/erc4337/UserOperationGas/types#rpc). ## Imports :::code-group ```ts [Named] import { UserOperationGas } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperationGas from 'ox/erc4337/UserOperationGas' ``` ::: ## Examples ```ts twoslash import { UserOperationGas } from 'ox/erc4337' const userOperationGas = UserOperationGas.toRpc({ callGasLimit: 300_000n, preVerificationGas: 100_000n, verificationGasLimit: 100_000n }) ``` ## Definition ```ts function toRpc( userOperationGas: toRpc.Input, ): Rpc ``` **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L108) ## Parameters ### userOperationGas * **Type:** `toRpc.Input` The user operation gas to convert. ## Return Type An RPC-formatted user operation gas. `Rpc` # UserOperationGas Types ## `UserOperationGas.Rpc` RPC User Operation Gas. **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L18) ## `UserOperationGas.RpcV06` RPC User Operation Gas on EntryPoint 0.6 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L30) ## `UserOperationGas.RpcV07` RPC User Operation Gas on EntryPoint 0.7 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L42) ## `UserOperationGas.RpcV08` RPC User Operation Gas on EntryPoint 0.8 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L48) ## `UserOperationGas.RpcV09` RPC User Operation Gas on EntryPoint 0.9 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L54) ## `UserOperationGas.UserOperationGas` User Operation Gas type. **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L7) ## `UserOperationGas.V06` Type for User Operation Gas on EntryPoint 0.6 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L23) ## `UserOperationGas.V07` Type for User Operation Gas on EntryPoint 0.7 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L33) ## `UserOperationGas.V08` Type for User Operation Gas on EntryPoint 0.8 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L45) ## `UserOperationGas.V09` Type for User Operation Gas on EntryPoint 0.9 **Source:** [src/erc4337/UserOperationGas.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationGas.ts#L51) # UserOperationReceipt Utility functions and types for working with [ERC-4337 User Operation Receipts](https://eips.ethereum.org/EIPS/eip-4337). ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperationReceipt.fromRpc`](/ercs/erc4337/UserOperationReceipt/fromRpc) | Converts an [`UserOperationReceipt.Rpc`](/ercs/erc4337/UserOperationReceipt/types#rpc) to an [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceipt). | | [`UserOperationReceipt.toRpc`](/ercs/erc4337/UserOperationReceipt/toRpc) | Converts a [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceipt) to a [`UserOperationReceipt.Rpc`](/ercs/erc4337/UserOperationReceipt/types#rpc). | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`UserOperationReceipt.Rpc`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceiptrpc) | RPC User Operation Receipt. | | [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceiptuseroperationreceipt) | User Operation Receipt type. | # UserOperationReceipt.fromRpc Converts an [`UserOperationReceipt.Rpc`](/ercs/erc4337/UserOperationReceipt/types#rpc) to an [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceipt). ## Imports :::code-group ```ts [Named] import { UserOperationReceipt } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperationReceipt from 'ox/erc4337/UserOperationReceipt' ``` ::: ## Examples ```ts twoslash // @noErrors import { UserOperationReceipt } from 'ox/erc4337' const userOperationReceipt = UserOperationReceipt.fromRpc({ actualGasCost: '0x1', actualGasUsed: '0x2', entryPoint: '0x0000000071727de22e5e9d8baf0edac6f37da032', logs: [], nonce: '0x1', receipt: { ... }, sender: '0xE911628bF8428C23f179a07b081325cAe376DE1f', success: true, userOpHash: '0x5ab163e9b2f30549274c7c567ca0696edf9ef1aa476d9784d22974468fdb24d8', }) ``` ## Definition ```ts function fromRpc( rpc: Rpc, ): UserOperationReceipt ``` **Source:** [src/erc4337/UserOperationReceipt.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationReceipt.ts#L87) ## Parameters ### rpc * **Type:** `Rpc` The RPC user operation receipt to convert. #### rpc.blobGasPrice * **Type:** `bigintType` * **Optional** The actual value per gas deducted from the sender's account for blob gas. Only specified for blob transactions as defined by EIP-4844. #### rpc.blobGasUsed * **Type:** `bigintType` * **Optional** The amount of blob gas used. Only specified for blob transactions as defined by EIP-4844. #### rpc.blockHash * **Type:** `0x${string}` Hash of block containing this transaction #### rpc.blockNumber * **Type:** `bigintType` Number of block containing this transaction #### rpc.contractAddress * **Type:** `Address.Address | null | undefined` * **Optional** Address of new contract or `null` if no contract was created #### rpc.cumulativeGasUsed * **Type:** `bigintType` Gas used by this and all preceding transactions in this block #### rpc.effectiveGasPrice * **Type:** `bigintType` Pre-London, it is equal to the transaction's gasPrice. Post-London, it is equal to the actual gas price paid for inclusion. #### rpc.from * **Type:** `abitype_Address` Transaction sender #### rpc.gasUsed * **Type:** `bigintType` Gas used by this transaction #### rpc.logs * **Type:** `{ address: abitype_Address; blockHash: 0x${string}; blockNumber: bigintType; blockTimestamp?: bigintType; data: 0x${string}; logIndex: numberType; topics: 0x${string}[]; transactionHash: 0x${string}; transactionIndex: numberType; removed: boolean; }[]` List of log objects generated by this transaction #### rpc.logsBloom * **Type:** `0x${string}` Logs bloom filter #### rpc.root * **Type:** `0x${string}` * **Optional** The post-transaction state root. Only specified for transactions included before the Byzantium upgrade. #### rpc.status * **Type:** `status` `success` if this transaction was successful or `reverted` if it failed #### rpc.to * **Type:** `Address.Address | null` Transaction recipient or `null` if deploying a contract #### rpc.transactionHash * **Type:** `0x${string}` Hash of this transaction #### rpc.transactionIndex * **Type:** `numberType` Index of this transaction in the block #### rpc.type * **Type:** `type` Transaction type ## Return Type An instantiated [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceipt). `UserOperationReceipt` # UserOperationReceipt.toRpc Converts a [`UserOperationReceipt.UserOperationReceipt`](/ercs/erc4337/UserOperationReceipt/types#useroperationreceipt) to a [`UserOperationReceipt.Rpc`](/ercs/erc4337/UserOperationReceipt/types#rpc). ## Imports :::code-group ```ts [Named] import { UserOperationReceipt } from 'ox/erc4337' ``` ```ts [Entrypoint] import * as UserOperationReceipt from 'ox/erc4337/UserOperationReceipt' ``` ::: ## Examples ```ts twoslash // @noErrors import { UserOperationReceipt } from 'ox/erc4337' const userOperationReceipt = UserOperationReceipt.toRpc({ actualGasCost: 1n, actualGasUsed: 2n, entryPoint: '0x0000000071727de22e5e9d8baf0edac6f37da032', logs: [], nonce: 1n, receipt: { ... }, sender: '0xE911628bF8428C23f179a07b081325cAe376DE1f', success: true, userOpHash: '0x5ab163e9b2f30549274c7c567ca0696edf9ef1aa476d9784d22974468fdb24d8', }) ``` ## Definition ```ts function toRpc( userOperationReceipt: toRpc.Input, ): Rpc ``` **Source:** [src/erc4337/UserOperationReceipt.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationReceipt.ts#L124) ## Parameters ### userOperationReceipt * **Type:** `toRpc.Input` The user operation receipt to convert. ## Return Type An RPC-formatted user operation receipt. `Rpc` # UserOperationReceipt Types ## `UserOperationReceipt.Rpc` RPC User Operation Receipt. **Source:** [src/erc4337/UserOperationReceipt.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationReceipt.ts#L49) ## `UserOperationReceipt.UserOperationReceipt` User Operation Receipt type. **Source:** [src/erc4337/UserOperationReceipt.ts](https://github.com/wevm/ox/blob/main/src/erc4337/UserOperationReceipt.ts#L13) # SignatureErc6492 Utility functions for working with [ERC-6492 wrapped signatures](https://eips.ethereum.org/EIPS/eip-6492#specification). ## Examples ```ts twoslash import { PersonalMessage, Secp256k1, Signature } from 'ox' import { SignatureErc6492 } from 'ox/erc6492' // [!code focus] const signature = Secp256k1.sign({ payload: PersonalMessage.getSignPayload('0xdeadbeef'), privateKey: '0x...' }) const wrapped = SignatureErc6492.wrap({ // [!code focus] data: '0xcafebabe', // [!code focus] signature: Signature.toHex(signature), // [!code focus] to: '0xcafebabecafebabecafebabecafebabecafebabe' // [!code focus] }) // [!code focus] // @log: '0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041fa78c5905fb0b9d6066ef531f962a62bc6ef0d5eb59ecb134056d206f75aaed7780926ff2601a935c2c79707d9e1799948c9f19dcdde1e090e903b19a07923d01c000000000000000000000000000000000000000000000000000000000000006492649264926492649264926492649264926492649264926492649264926492' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc6492.assert`](/ercs/erc6492/SignatureErc6492/assert) | Asserts that the wrapped signature is valid. | | [`SignatureErc6492.from`](/ercs/erc6492/SignatureErc6492/from) | Parses an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification) into its constituent parts. | | [`SignatureErc6492.unwrap`](/ercs/erc6492/SignatureErc6492/unwrap) | Parses an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification) into its constituent parts. | | [`SignatureErc6492.validate`](/ercs/erc6492/SignatureErc6492/validate) | Validates a wrapped signature. Returns `true` if the wrapped signature is valid, `false` otherwise. | | [`SignatureErc6492.wrap`](/ercs/erc6492/SignatureErc6492/wrap) | Serializes an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc6492.InvalidUnwrappedSignatureError`](/ercs/erc6492/SignatureErc6492/errors#signatureerc6492invalidunwrappedsignatureerror) | Thrown when an ERC-6492 unwrapped signature object is malformed. | | [`SignatureErc6492.InvalidWrappedSignatureError`](/ercs/erc6492/SignatureErc6492/errors#signatureerc6492invalidwrappedsignatureerror) | Thrown when the ERC-6492 wrapped signature is invalid. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc6492.Unwrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492unwrapped) | Unwrapped ERC-6492 signature. | | [`SignatureErc6492.Wrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492wrapped) | Wrapped ERC-6492 signature. | # SignatureErc6492.assert Asserts that the wrapped signature is valid. ## Imports :::code-group ```ts [Named] import { SignatureErc6492 } from 'ox/erc6492' ``` ```ts [Entrypoint] import * as SignatureErc6492 from 'ox/erc6492/SignatureErc6492' ``` ::: ## Examples ```ts twoslash import { SignatureErc6492 } from 'ox/erc6492' SignatureErc6492.assert('0xdeadbeef') // @error: InvalidWrappedSignatureError: Value `0xdeadbeef` is an invalid ERC-6492 wrapped signature. ``` ## Definition ```ts function assert( value: Unwrapped | Wrapped, ): void ``` **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L101) ## Parameters ### value * **Type:** `Unwrapped | Wrapped` The value to assert. #### value.data * **Type:** `0x${string}` Calldata to pass to the target address for counterfactual verification. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `abitype_Address` The target address to use for counterfactual verification. ## Return Type `void` # SignatureErc6492.from Parses an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification) into its constituent parts. ## Imports :::code-group ```ts [Named] import { SignatureErc6492 } from 'ox/erc6492' ``` ```ts [Entrypoint] import * as SignatureErc6492 from 'ox/erc6492/SignatureErc6492' ``` ::: ## Examples ```ts twoslash // @noErrors import { Secp256k1 } from 'ox' import { SignatureErc6492 } from 'ox/erc6492' // [!code focus] const signature = Secp256k1.sign({ payload: '0x...', privateKey: '0x...' }) // Instantiate from serialized format. // [!code focus] const wrapped = SignatureErc6492.from('0x...') // [!code focus] // @log: { data: '0x...', signature: { ... }, to: '0x...', } // [!code focus] // Instantiate from constituent parts. // [!code focus] const wrapped = SignatureErc6492.from({ // [!code focus] data: '0x...', // [!code focus] signature, // [!code focus] to: '0x...' // [!code focus] }) // @log: { data: '0x...', signature: { ... }, to: '0x...', } ``` ## Definition ```ts function from( wrapped: Unwrapped | Wrapped, ): Unwrapped ``` **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L157) ## Parameters ### wrapped * **Type:** `Unwrapped | Wrapped` Wrapped signature to parse. #### wrapped.data * **Type:** `0x${string}` Calldata to pass to the target address for counterfactual verification. #### wrapped.signature * **Type:** `0x${string}` The original signature. #### wrapped.to * **Type:** `abitype_Address` The target address to use for counterfactual verification. ## Return Type Wrapped signature. [`Unwrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492unwrapped) # SignatureErc6492.unwrap Parses an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification) into its constituent parts. ## Imports :::code-group ```ts [Named] import { SignatureErc6492 } from 'ox/erc6492' ``` ```ts [Entrypoint] import * as SignatureErc6492 from 'ox/erc6492/SignatureErc6492' ``` ::: ## Examples ```ts twoslash import { SignatureErc6492 } from 'ox/erc6492' const { data, signature, to } = SignatureErc6492.unwrap('0x...') ``` ## Definition ```ts function unwrap( wrapped: Wrapped, ): Unwrapped ``` **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L187) ## Parameters ### wrapped * **Type:** [`Wrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492wrapped) Wrapped signature to parse. ## Return Type Wrapped signature. [`Unwrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492unwrapped) # SignatureErc6492.validate Validates a wrapped signature. Returns `true` if the wrapped signature is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { SignatureErc6492 } from 'ox/erc6492' ``` ```ts [Entrypoint] import * as SignatureErc6492 from 'ox/erc6492/SignatureErc6492' ``` ::: ## Examples ```ts twoslash import { SignatureErc6492 } from 'ox/erc6492' const valid = SignatureErc6492.validate('0xdeadbeef') // @log: false ``` ## Definition ```ts function validate( wrapped: Wrapped, ): boolean ``` **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L259) ## Parameters ### wrapped * **Type:** [`Wrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492wrapped) The wrapped signature to validate. ## Return Type `true` if the wrapped signature is valid, `false` otherwise. `boolean` # SignatureErc6492.wrap Serializes an [ERC-6492 wrapped signature](https://eips.ethereum.org/EIPS/eip-6492#specification). ## Imports :::code-group ```ts [Named] import { SignatureErc6492 } from 'ox/erc6492' ``` ```ts [Entrypoint] import * as SignatureErc6492 from 'ox/erc6492/SignatureErc6492' ``` ::: ## Examples ```ts twoslash import { Secp256k1, Signature } from 'ox' import { SignatureErc6492 } from 'ox/erc6492' // [!code focus] const signature = Secp256k1.sign({ payload: '0x...', privateKey: '0x...' }) const wrapped = SignatureErc6492.wrap({ // [!code focus] data: '0xdeadbeef', // [!code focus] signature: Signature.toHex(signature), // [!code focus] to: '0x00000000219ab540356cBB839Cbe05303d7705Fa' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function wrap( value: Unwrapped, ): Wrapped ``` **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L228) ## Parameters ### value * **Type:** [`Unwrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492unwrapped) Wrapped signature to serialize. #### value.data * **Type:** `0x${string}` Calldata to pass to the target address for counterfactual verification. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `abitype_Address` The target address to use for counterfactual verification. ## Return Type Serialized wrapped signature. [`Wrapped`](/ercs/erc6492/SignatureErc6492/types#signatureerc6492wrapped) # SignatureErc6492 Errors ## `SignatureErc6492.InvalidUnwrappedSignatureError` Thrown when an ERC-6492 unwrapped signature object is malformed. **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L282) ## `SignatureErc6492.InvalidWrappedSignatureError` Thrown when the ERC-6492 wrapped signature is invalid. **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L273) # SignatureErc6492 Types ## `SignatureErc6492.Unwrapped` Unwrapped ERC-6492 signature. **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L9) ## `SignatureErc6492.Wrapped` Wrapped ERC-6492 signature. **Source:** [src/erc6492/SignatureErc6492.ts](https://github.com/wevm/ox/blob/main/src/erc6492/SignatureErc6492.ts#L19) # Calls Utility functions for encoding and decoding [ERC-7821](https://eips.ethereum.org/EIPS/eip-7821) calls. ## Examples Below are some examples demonstrating common usages of the `Calls` module: * [Encoding calls](#encoding-calls) * [Decoding calls](#decoding-calls) ### Encoding calls Calls can be encoded using `Calls.encode`. ```ts twoslash import { Calls } from 'ox/erc7821' const calls = Calls.encode([ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n } ]) ``` ### Decoding calls Calls can be decoded using `Calls.decode`. ```ts twoslash import { Calls } from 'ox/erc7821' const { calls } = Calls.decode('0x...') ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Calls.decode`](/ercs/erc7821/Calls/decode) | Decodes a set of ERC-7821 calls from encoded data. | | [`Calls.encode`](/ercs/erc7821/Calls/encode) | Encodes a set of ERC-7821 calls. | | [`Calls.getAbiParameters`](/ercs/erc7821/Calls/getAbiParameters) | Gets the ABI parameters for the ERC-7821 calls. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Calls.Call`](/ercs/erc7821/Calls/types#callscall) | | # Calls.decode Decodes a set of ERC-7821 calls from encoded data. ## Imports :::code-group ```ts [Named] import { Calls } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Calls from 'ox/erc7821/Calls' ``` ::: ## Examples ```ts twoslash import { Calls } from 'ox/erc7821' const data = Calls.decode( '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000004deadbeef00000000000000000000000000000000000000000000000000000000000000000000000000000000deadbeefdeadbeefdeadbeefdeadbeefdeadbeef000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000004cafebabe00000000000000000000000000000000000000000000000000000000' ) // @log: { // @log: calls: [ // @log: { // @log: data: '0xdeadbeef', // @log: to: '0xcafebabecafebabecafebabecafebabecafebabe', // @log: value: 1n, // @log: }, // @log: { // @log: data: '0xcafebabe', // @log: to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', // @log: value: 2n, // @log: }, // @log: ] // @log: } ``` ## Definition ```ts function decode( data: Hex.Hex, options?: decode.Options, ): decode.ReturnType ``` **Source:** [src/erc7821/Calls.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Calls.ts#L125) ## Parameters ### data * **Type:** `Hex.Hex` The encoded calls data. ### options * **Type:** `decode.Options` * **Optional** Options for decoding. #### options.opData * **Type:** `boolean` * **Optional** Whether to decode opData if present. ## Return Type The decoded calls and optional opData. `decode.ReturnType` # Calls.encode Encodes a set of ERC-7821 calls. ## Imports :::code-group ```ts [Named] import { Calls } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Calls from 'ox/erc7821/Calls' ``` ::: ## Examples ```ts twoslash import { Calls } from 'ox/erc7821' const calls = Calls.encode([ { data: '0xdeadbeef', to: '0xcafebabecafebabecafebabecafebabecafebabe', value: 1n }, { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 2n } ]) // @log: '0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000cafebabecafebabecafebabecafebabecafebabe0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000deadbeef000000000000000000000000deadbeefdeadbeefdeadbeefdeadbeefdeadbeef0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cafebabe' ``` ## Definition ```ts function encode( calls: readonly Call[], options?: encode.Options, ): `0x${string}` ``` **Source:** [src/erc7821/Calls.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Calls.ts#L50) ## Parameters ### calls * **Type:** `readonly Call[]` Calls to encode. #### calls.data * **Type:** `0x${string}` * **Optional** #### calls.to * **Type:** `abitype_Address` #### calls.value * **Type:** `bigintType` * **Optional** ### options * **Type:** `encode.Options` * **Optional** Options for the encoding. #### options.opData * **Type:** `0x${string}` * **Optional** Additional data to include for execution. ## Return Type The encoded calls. `0x${string}` # Calls.getAbiParameters Gets the ABI parameters for the ERC-7821 calls. ## Imports :::code-group ```ts [Named] import { Calls } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Calls from 'ox/erc7821/Calls' ``` ::: ## Examples ```ts twoslash import { Calls } from 'ox/erc7821' const abiParameters = Calls.getAbiParameters({ opData: true }) ``` ## Definition ```ts function getAbiParameters( options?: getAbiParameters.Options, ): readonly [{ readonly type: "tuple[]"; readonly components: readonly [{ readonly type: "address"; readonly name: "target"; }, { readonly type: "uint256"; readonly name: "value"; }, { readonly type: "bytes"; readonly name: "data"; }]; readonly name: "calls"; }] | readonly [{ readonly type: "tuple[]"; readonly components: readonly [{ readonly type: "address"; readonly name: "target"; }, { readonly type: "uint256"; readonly name: "value"; }, { readonly type: "bytes"; readonly name: "data"; }]; readonly name: "calls"; }, { readonly type: "bytes"; readonly name: "opData"; }] ``` **Source:** [src/erc7821/Calls.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Calls.ts#L84) ## Parameters ### options * **Type:** `getAbiParameters.Options` * **Optional** Options. #### options.opData * **Type:** `boolean` * **Optional** ## Return Type The ABI parameters. `readonly [{ readonly type: "tuple[]"; readonly components: readonly [{ readonly type: "address"; readonly name: "target"; }, { readonly type: "uint256"; readonly name: "value"; }, { readonly type: "bytes"; readonly name: "data"; }]; readonly name: "calls"; }] | readonly [{ readonly type: "tuple[]"; readonly components: readonly [{ readonly type: "address"; readonly name: "target"; }, { readonly type: "uint256"; readonly name: "value"; }, { readonly type: "bytes"; readonly name: "data"; }]; readonly name: "calls"; }, { readonly type: "bytes"; readonly name: "opData"; }]` # Calls Types ## `Calls.Call` **Source:** [src/erc7821/Calls.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Calls.ts#L5) # Execute Utility functions for encoding and decoding [ERC-7821](https://eips.ethereum.org/EIPS/eip-7821) `execute` function data. ## Examples Below are some examples demonstrating common usages of the `Execute` module: * [Encoding `execute` Function Data](#encoding-`execute`-function-data) * [Decoding `execute` Function Data](#decoding-`execute`-function-data) ### Encoding `execute` Function Data The `execute` function data can be encoded using `Execute.encodeData`. ```ts twoslash import { Execute } from 'ox/erc7821' const data = Execute.encodeData([ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n } ]) ``` ### Decoding `execute` Function Data The `execute` function data can be decoded using `Execute.decodeData`. ```ts twoslash import { Execute } from 'ox/erc7821' const { calls } = Execute.decodeData('0xe9ae5c53...') ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Execute.decodeBatchOfBatchesData`](/ercs/erc7821/Execute/decodeBatchOfBatchesData) | Decodes batches from ERC-7821 `execute` function data in "batch of batches" mode. | | [`Execute.decodeData`](/ercs/erc7821/Execute/decodeData) | Decodes calls from ERC-7821 `execute` function data. | | [`Execute.encodeBatchOfBatchesData`](/ercs/erc7821/Execute/encodeBatchOfBatchesData) | Encodes calls for the ERC-7821 `execute` function with "batch of batches" mode. | | [`Execute.encodeData`](/ercs/erc7821/Execute/encodeData) | Encodes calls for the ERC-7821 `execute` function. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Execute.Batch`](/ercs/erc7821/Execute/types#executebatch) | | | [`Execute.Call`](/ercs/erc7821/Execute/types#executecall) | | # Execute.decodeBatchOfBatchesData Decodes batches from ERC-7821 `execute` function data in "batch of batches" mode. ## Imports :::code-group ```ts [Named] import { Execute } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Execute from 'ox/erc7821/Execute' ``` ::: ## Examples ```ts twoslash import { Execute } from 'ox/erc7821' const batches = Execute.decodeBatchOfBatchesData('0x...') ``` ## Definition ```ts function decodeBatchOfBatchesData( data: Hex.Hex, ): decodeBatchOfBatchesData.ReturnType ``` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L83) ## Parameters ### data * **Type:** `Hex.Hex` The encoded data. ## Return Type The decoded batches. `decodeBatchOfBatchesData.ReturnType` # Execute.decodeData Decodes calls from ERC-7821 `execute` function data. ## Imports :::code-group ```ts [Named] import { Execute } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Execute from 'ox/erc7821/Execute' ``` ::: ## Examples ```ts twoslash import { Execute } from 'ox/erc7821' const { calls } = Execute.decodeData('0x...') ``` ## Definition ```ts function decodeData( data: Hex.Hex, ): decodeData.ReturnType ``` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L55) ## Parameters ### data * **Type:** `Hex.Hex` The encoded data. ## Return Type The decoded calls and optional opData. `decodeData.ReturnType` # Execute.encodeBatchOfBatchesData Encodes calls for the ERC-7821 `execute` function with "batch of batches" mode. ## Imports :::code-group ```ts [Named] import { Execute } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Execute from 'ox/erc7821/Execute' ``` ::: ## Examples ```ts twoslash import { Execute } from 'ox/erc7821' const data = Execute.encodeBatchOfBatchesData([ { calls: [ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n } ] }, { calls: [ { data: '0xdeadbeef', to: '0xcafebabecafebabecafebabecafebabecafebabe', value: 2n } ], opData: '0xcafebabe' } ]) ``` ## Definition ```ts function encodeBatchOfBatchesData( batches: readonly Batch[], ): `0x${string}` ``` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L151) ## Parameters ### batches * **Type:** `readonly Batch[]` #### batches.calls * **Type:** `readonly Call[]` #### batches.opData * **Type:** `0x${string}` * **Optional** ## Return Type The encoded data. `0x${string}` # Execute.encodeData Encodes calls for the ERC-7821 `execute` function. ## Imports :::code-group ```ts [Named] import { Execute } from 'ox/erc7821' ``` ```ts [Entrypoint] import * as Execute from 'ox/erc7821/Execute' ``` ::: ## Examples ```ts twoslash import { Execute } from 'ox/erc7821' const data = Execute.encodeData([ { data: '0xcafebabe', to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef', value: 1n } ]) ``` ## Definition ```ts function encodeData( calls: readonly Call[], options?: encodeData.Options, ): `0x${string}` ``` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L183) ## Parameters ### calls * **Type:** `readonly Call[]` The calls to encode. ### options * **Type:** `encodeData.Options` * **Optional** The options. #### options.opData * **Type:** `0x${string}` * **Optional** ## Return Type The encoded data. `0x${string}` # Execute Types ## `Execute.Batch` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L6) ## `Execute.Call` **Source:** [src/erc7821/Execute.ts](https://github.com/wevm/ox/blob/main/src/erc7821/Execute.ts#L11) # SignatureErc8010 Utility functions for working with [ERC-8010 wrapped signatures](https://eips.ethereum.org/EIPS/eip-8010#specification). ## Examples ```ts twoslash import { Authorization, PersonalMessage, Secp256k1, Signature } from 'ox' import { SignatureErc8010 } from 'ox/erc8010' // [!code focus] const authorization = Authorization.from({ address: '0x1234567890abcdef1234567890abcdef12345678', chainId: 1, nonce: 69n }) const authorizationSignature = Secp256k1.sign({ payload: Authorization.getSignPayload(authorization), privateKey: '0x...' }) const authorizationSigned = Authorization.from( authorization, { signature: authorizationSignature } ) const signature = Secp256k1.sign({ payload: PersonalMessage.getSignPayload('0xdeadbeef'), privateKey: '0x...' }) const wrapped = SignatureErc8010.wrap({ // [!code focus] authorization: authorizationSigned, // [!code focus] data: '0xcafebabe', // [!code focus] signature: Signature.toHex(signature) // [!code focus] }) // [!code focus] // @log: '0x000000000000000000000000cafebabecafebabecafebabecafebabecafebabe000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000004deadbeef000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000041fa78c5905fb0b9d6066ef531f962a62bc6ef0d5eb59ecb134056d206f75aaed7780926ff2601a935c2c79707d9e1799948c9f19dcdde1e090e903b19a07923d01c000000000000000000000000000000000000000000000000000000000000008010801080108010801080108010801080108010801080108010801080108010' ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc8010.assert`](/ercs/erc8010/SignatureErc8010/assert) | Asserts that the wrapped signature is valid. | | [`SignatureErc8010.from`](/ercs/erc8010/SignatureErc8010/from) | Parses an [ERC-8010 wrapped signature](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md) into its constituent parts. | | [`SignatureErc8010.unwrap`](/ercs/erc8010/SignatureErc8010/unwrap) | Unwraps an [ERC-8010 wrapped signature](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md) into its constituent parts. | | [`SignatureErc8010.validate`](/ercs/erc8010/SignatureErc8010/validate) | Validates a wrapped signature. Returns `true` if the wrapped signature is valid, `false` otherwise. | | [`SignatureErc8010.wrap`](/ercs/erc8010/SignatureErc8010/wrap) | Wraps a signature into [ERC-8010 format](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md). | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc8010.InvalidUnwrappedSignatureError`](/ercs/erc8010/SignatureErc8010/errors#signatureerc8010invalidunwrappedsignatureerror) | Thrown when an ERC-8010 unwrapped signature object is malformed. | | [`SignatureErc8010.InvalidWrappedSignatureError`](/ercs/erc8010/SignatureErc8010/errors#signatureerc8010invalidwrappedsignatureerror) | Thrown when the ERC-8010 wrapped signature is invalid. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`SignatureErc8010.Unwrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010unwrapped) | Unwrapped ERC-8010 signature. | | [`SignatureErc8010.Wrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010wrapped) | Wrapped ERC-8010 signature. | # SignatureErc8010.assert Asserts that the wrapped signature is valid. ## Imports :::code-group ```ts [Named] import { SignatureErc8010 } from 'ox/erc8010' ``` ```ts [Entrypoint] import * as SignatureErc8010 from 'ox/erc8010/SignatureErc8010' ``` ::: ## Examples ```ts twoslash import { SignatureErc8010 } from 'ox/erc8010' SignatureErc8010.assert('0xdeadbeef') // @error: InvalidWrappedSignatureError: Value `0xdeadbeef` is an invalid ERC-8010 wrapped signature. ``` ## Definition ```ts function assert( value: Unwrapped | Wrapped, ): void ``` **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L48) ## Parameters ### value * **Type:** `Unwrapped | Wrapped` The value to assert. #### value.authorization * **Type:** `{ address: abitype_Address; chainId: number; nonce: bigint; r: 0x${string}; s: 0x${string}; yParity: number; }` Authorization signed by the delegatee. #### value.data * **Type:** `0x${string}` * **Optional** Data to initialize the delegation. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `Address.Address | undefined` * **Optional** Address of the initializer. ## Return Type `void` # SignatureErc8010.from Parses an [ERC-8010 wrapped signature](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md) into its constituent parts. ## Imports :::code-group ```ts [Named] import { SignatureErc8010 } from 'ox/erc8010' ``` ```ts [Entrypoint] import * as SignatureErc8010 from 'ox/erc8010/SignatureErc8010' ``` ::: ## Examples ```ts twoslash // @noErrors import { Secp256k1 } from 'ox' import { SignatureErc8010 } from 'ox/erc8010' // [!code focus] const signature = Secp256k1.sign({ payload: '0x...', privateKey: '0x...', }) // Instantiate from serialized format. // [!code focus] const wrapped = SignatureErc8010.from('0x...') // [!code focus] // @log: { authorization: { ... }, data: '0x...', signature: { ... } } // [!code focus] // Instantiate from constituent parts. // [!code focus] const wrapped = SignatureErc8010.from({ // [!code focus] authorization: { ... }, // [!code focus] data: '0x...', // [!code focus] signature, // [!code focus] }) // @log: { authorization: { ... }, data: '0x...', signature: { ... } } ``` ## Definition ```ts function from( value: Unwrapped | Wrapped, ): Unwrapped ``` **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L106) ## Parameters ### value * **Type:** `Unwrapped | Wrapped` Value to parse. #### value.authorization * **Type:** `{ address: abitype_Address; chainId: number; nonce: bigint; r: 0x${string}; s: 0x${string}; yParity: number; }` Authorization signed by the delegatee. #### value.data * **Type:** `0x${string}` * **Optional** Data to initialize the delegation. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `Address.Address | undefined` * **Optional** Address of the initializer. ## Return Type Parsed value. [`SignatureErc8010.Unwrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010unwrapped) # SignatureErc8010.unwrap Unwraps an [ERC-8010 wrapped signature](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md) into its constituent parts. ## Imports :::code-group ```ts [Named] import { SignatureErc8010 } from 'ox/erc8010' ``` ```ts [Entrypoint] import * as SignatureErc8010 from 'ox/erc8010/SignatureErc8010' ``` ::: ## Examples ```ts twoslash import { SignatureErc8010 } from 'ox/erc8010' const { authorization, data, signature } = SignatureErc8010.unwrap('0x...') ``` ## Definition ```ts function unwrap( wrapped: SignatureErc8010.Wrapped, ): Unwrapped ``` **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L130) ## Parameters ### wrapped * **Type:** [`SignatureErc8010.Wrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010wrapped) Wrapped signature to unwrap. ## Return Type Unwrapped signature. [`SignatureErc8010.Unwrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010unwrapped) # SignatureErc8010.validate Validates a wrapped signature. Returns `true` if the wrapped signature is valid, `false` otherwise. ## Imports :::code-group ```ts [Named] import { SignatureErc8010 } from 'ox/erc8010' ``` ```ts [Entrypoint] import * as SignatureErc8010 from 'ox/erc8010/SignatureErc8010' ``` ::: ## Examples ```ts twoslash import { SignatureErc8010 } from 'ox/erc8010' const valid = SignatureErc8010.validate('0xdeadbeef') // @log: false ``` ## Definition ```ts function validate( value: Unwrapped | Wrapped, ): boolean ``` **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L233) ## Parameters ### value * **Type:** `Unwrapped | Wrapped` The value to validate. #### value.authorization * **Type:** `{ address: abitype_Address; chainId: number; nonce: bigint; r: 0x${string}; s: 0x${string}; yParity: number; }` Authorization signed by the delegatee. #### value.data * **Type:** `0x${string}` * **Optional** Data to initialize the delegation. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `Address.Address | undefined` * **Optional** Address of the initializer. ## Return Type `true` if the value is valid, `false` otherwise. `boolean` # SignatureErc8010.wrap Wraps a signature into [ERC-8010 format](https://github.com/jxom/ERCs/blob/16f7e3891fff2e1e9c25dea0485497739db8a816/ERCS/erc-8010.md). ## Imports :::code-group ```ts [Named] import { SignatureErc8010 } from 'ox/erc8010' ``` ```ts [Entrypoint] import * as SignatureErc8010 from 'ox/erc8010/SignatureErc8010' ``` ::: ## Examples ```ts twoslash // @noErrors import { Secp256k1, Signature } from 'ox' import { SignatureErc8010 } from 'ox/erc8010' // [!code focus] const signature = Secp256k1.sign({ payload: '0x...', privateKey: '0x...', }) const wrapped = SignatureErc8010.wrap({ // [!code focus] authorization: { ... }, // [!code focus] data: '0xdeadbeef', // [!code focus] signature: Signature.toHex(signature), // [!code focus] }) // [!code focus] ``` ## Definition ```ts function wrap( value: SignatureErc8010.Unwrapped, ): Wrapped ``` **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L188) ## Parameters ### value * **Type:** [`SignatureErc8010.Unwrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010unwrapped) Values to wrap. #### value.authorization * **Type:** `{ address: abitype_Address; chainId: number; nonce: bigint; r: 0x${string}; s: 0x${string}; yParity: number; }` Authorization signed by the delegatee. #### value.data * **Type:** `0x${string}` * **Optional** Data to initialize the delegation. #### value.signature * **Type:** `0x${string}` The original signature. #### value.to * **Type:** `Address.Address | undefined` * **Optional** Address of the initializer. ## Return Type Wrapped signature. [`SignatureErc8010.Wrapped`](/ercs/erc8010/SignatureErc8010/types#signatureerc8010wrapped) # SignatureErc8010 Errors ## `SignatureErc8010.InvalidUnwrappedSignatureError` Thrown when an ERC-8010 unwrapped signature object is malformed. **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L256) ## `SignatureErc8010.InvalidWrappedSignatureError` Thrown when the ERC-8010 wrapped signature is invalid. **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L247) # SignatureErc8010 Types ## `SignatureErc8010.Unwrapped` Unwrapped ERC-8010 signature. **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L10) ## `SignatureErc8010.Wrapped` Wrapped ERC-8010 signature. **Source:** [src/erc8010/SignatureErc8010.ts](https://github.com/wevm/ox/blob/main/src/erc8010/SignatureErc8010.ts#L22) # Attribution Utility functions for working with [ERC-8021 Transaction Attribution](https://eip.tools/eip/8021). ## Examples Below are some examples demonstrating common usages of the `Attribution` module: * [Converting an Attribution to Data Suffix](#converting-an-attribution-to-data-suffix) * [Extracting an Attribution from Calldata](#extracting-an-attribution-from-calldata) ### Converting an Attribution to Data Suffix ```ts twoslash import { Attribution } from 'ox/erc8021' const dataSuffix1 = Attribution.toDataSuffix({ codes: ['baseapp'] }) const dataSuffix2 = Attribution.toDataSuffix({ codes: ['baseapp', 'morpho'], codeRegistry: { address: '0x0000000000000000000000000000000000000000', chainId: 1 } }) const dataSuffix3 = Attribution.toDataSuffix({ appCode: 'baseapp', walletCode: 'privy' }) ``` ### Extracting an Attribution from Calldata ```ts twoslash import { Attribution } from 'ox/erc8021' const attribution = Attribution.fromData('0x...') console.log(attribution) // @log: { codes: ['baseapp', 'morpho'], codeRegistry: { address: '0x...', chainId: 1 } } ``` ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Attribution.fromData`](/ercs/erc8021/Attribution/fromData) | Extracts an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution) from transaction calldata. | | [`Attribution.getSchemaId`](/ercs/erc8021/Attribution/getSchemaId) | Determines the schema ID for an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution). | | [`Attribution.toDataSuffix`](/ercs/erc8021/Attribution/toDataSuffix) | Converts an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution) to a data suffix that can be appended to transaction calldata. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attributionattribution) | ERC-8021 Transaction Attribution. | | [`Attribution.AttributionSchemaId0`](/ercs/erc8021/Attribution/types#attributionattributionschemaid0) | Schema 0: Canonical Registry Attribution. | | [`Attribution.AttributionSchemaId1`](/ercs/erc8021/Attribution/types#attributionattributionschemaid1) | Schema 1: Custom Registry Attribution. | | [`Attribution.AttributionSchemaId1Registry`](/ercs/erc8021/Attribution/types#attributionattributionschemaid1registry) | | | [`Attribution.AttributionSchemaId2`](/ercs/erc8021/Attribution/types#attributionattributionschemaid2) | Schema 2: CBOR-Encoded Attribution. | | [`Attribution.AttributionSchemaId2Registries`](/ercs/erc8021/Attribution/types#attributionattributionschemaid2registries) | | | [`Attribution.AttributionSchemaId2Registry`](/ercs/erc8021/Attribution/types#attributionattributionschemaid2registry) | | | [`Attribution.SchemaId`](/ercs/erc8021/Attribution/types#attributionschemaid) | Attribution schema identifier. | # Attribution.fromData Extracts an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution) from transaction calldata. ## Imports :::code-group ```ts [Named] import { Attribution } from 'ox/erc8021' ``` ```ts [Entrypoint] import * as Attribution from 'ox/erc8021/Attribution' ``` ::: ## Examples ### Schema 0 (Canonical Registry) ```ts twoslash import { Attribution } from 'ox/erc8021' const attribution = Attribution.fromData( '0xdddddddd62617365617070070080218021802180218021802180218021' ) // @log: { codes: ['baseapp'], id: 0 } ``` ### Schema 1 (Custom Registry) ```ts twoslash import { Attribution } from 'ox/erc8021' const attribution = Attribution.fromData( '0xddddddddcccccccccccccccccccccccccccccccccccccccc210502626173656170702C6D6F7270686F0E0180218021802180218021802180218021' ) // @log: { // @log: codes: ['baseapp', 'morpho'], // @log: codeRegistry: { // @log: address: '0xcccccccccccccccccccccccccccccccccccccccc', // @log: chainId: 8453, // @log: }, // @log: id: 1 // @log: } ``` ### Schema 2 (CBOR-Encoded) ```ts twoslash import { Attribution } from 'ox/erc8021' const attribution = Attribution.fromData( '0xdddddddda161616762617365617070000b0280218021802180218021802180218021' ) // @log: { appCode: 'baseapp', id: 2 } ``` ## Definition ```ts function fromData( data: Hex.Hex, ): Attribution | undefined ``` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L296) ## Parameters ### data * **Type:** `Hex.Hex` The transaction calldata containing the attribution suffix. ## Return Type The extracted attribution, or undefined if no valid attribution is found. [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attributionattribution) # Attribution.getSchemaId Determines the schema ID for an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution). ## Imports :::code-group ```ts [Named] import { Attribution } from 'ox/erc8021' ``` ```ts [Entrypoint] import * as Attribution from 'ox/erc8021/Attribution' ``` ::: ## Examples ```ts twoslash import { Attribution } from 'ox/erc8021' const schemaId = Attribution.getSchemaId({ codes: ['baseapp'] }) // @log: 0 const schemaId2 = Attribution.getSchemaId({ codes: ['baseapp'], codeRegistry: { address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', chainId: 8453 } }) // @log: 1 const schemaId3 = Attribution.getSchemaId({ appCode: 'baseapp' }) // @log: 2 ``` ## Definition ```ts function getSchemaId( attribution: Attribution.Attribution, ): SchemaId ``` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L138) ## Parameters ### attribution * **Type:** [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attributionattribution) The attribution object. #### attribution.appCode * **Type:** `string` * **Optional** Application attribution code. #### attribution.codeRegistry * **Type:** `AttributionSchemaId1Registry` #### attribution.codes * **Type:** `readonly string[]` Attribution codes identifying entities involved in the transaction. #### attribution.id * **Type:** `2` * **Optional** Schema identifier (2 for CBOR-encoded). #### attribution.metadata * **Type:** `Record` * **Optional** Arbitrary metadata key-value pairs. #### attribution.registries * **Type:** `AttributionSchemaId2Registries` * **Optional** Custom code registries keyed by entity type. #### attribution.serviceCodes * **Type:** `readonly string[]` * **Optional** Service codes identifying additional service providers (e.g., block builders, relayers, solvers). #### attribution.walletCode * **Type:** `string` * **Optional** Wallet attribution code. ## Return Type The schema ID (0 for canonical registry, 1 for custom registry, 2 for CBOR-encoded). [`SchemaId`](/ercs/erc8021/Attribution/types#attributionschemaid) # Attribution.toDataSuffix Converts an [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attribution) to a data suffix that can be appended to transaction calldata. ## Imports :::code-group ```ts [Named] import { Attribution } from 'ox/erc8021' ``` ```ts [Entrypoint] import * as Attribution from 'ox/erc8021/Attribution' ``` ::: ## Examples ### Schema 0 (Canonical Registry) ```ts twoslash import { Attribution } from 'ox/erc8021' const suffix = Attribution.toDataSuffix({ codes: ['baseapp', 'morpho'] }) // @log: '0x626173656170702c6d6f7270686f0e0080218021802180218021802180218021' ``` ### Schema 1 (Custom Registry) ```ts twoslash import { Attribution } from 'ox/erc8021' const suffix = Attribution.toDataSuffix({ codes: ['baseapp'], codeRegistry: { address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', chainId: 8453 } }) ``` ### Schema 2 (CBOR-Encoded) ```ts twoslash import { Attribution } from 'ox/erc8021' const suffix = Attribution.toDataSuffix({ appCode: 'baseapp', walletCode: 'privy', metadata: { source: 'webapp' } }) ``` ## Definition ```ts function toDataSuffix( attribution: Attribution.Attribution, ): Hex.Hex ``` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L199) ## Parameters ### attribution * **Type:** [`Attribution.Attribution`](/ercs/erc8021/Attribution/types#attributionattribution) The attribution to convert. #### attribution.appCode * **Type:** `string` * **Optional** Application attribution code. #### attribution.codeRegistry * **Type:** `AttributionSchemaId1Registry` #### attribution.codes * **Type:** `readonly string[]` Attribution codes identifying entities involved in the transaction. #### attribution.id * **Type:** `2` * **Optional** Schema identifier (2 for CBOR-encoded). #### attribution.metadata * **Type:** `Record` * **Optional** Arbitrary metadata key-value pairs. #### attribution.registries * **Type:** `AttributionSchemaId2Registries` * **Optional** Custom code registries keyed by entity type. #### attribution.serviceCodes * **Type:** `readonly string[]` * **Optional** Service codes identifying additional service providers (e.g., block builders, relayers, solvers). #### attribution.walletCode * **Type:** `string` * **Optional** Wallet attribution code. ## Return Type The data suffix as a [`Hex.Hex`](/api/Hex/types#hex) value. `Hex.Hex` # Attribution Types ## `Attribution.Attribution` ERC-8021 Transaction Attribution. Represents attribution metadata that can be appended to transaction calldata to track entities involved in facilitating a transaction. **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L13) ## `Attribution.AttributionSchemaId0` Schema 0: Canonical Registry Attribution. Uses the canonical attribution code registry for resolving entity identities. **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L22) ## `Attribution.AttributionSchemaId1` Schema 1: Custom Registry Attribution. Uses a custom registry contract for resolving attribution codes. **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L34) ## `Attribution.AttributionSchemaId1Registry` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L43) ## `Attribution.AttributionSchemaId2` Schema 2: CBOR-Encoded Attribution. Uses CBOR encoding for extensible transaction annotation with optional fields, support for arbitrary metadata, and coexistence with other suffix-based systems. **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L56) ## `Attribution.AttributionSchemaId2Registries` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L71) ## `Attribution.AttributionSchemaId2Registry` **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L78) ## `Attribution.SchemaId` Attribution schema identifier. * `0`: Canonical registry - `1`: Custom registry - `2`: CBOR-encoded **Source:** [src/erc8021/Attribution.ts](https://github.com/wevm/ox/blob/main/src/erc8021/Attribution.ts#L92) # Authentication Utility functions and types for WebAuthn authentication ceremonies (signing and verification). ## Functions | Name | Description | | ------------------- | ----------------------------------- | | [`Authentication.deserializeOptions`](/webauthn/webauthn/Authentication/deserializeOptions) | Deserializes credential request options that can be passed to `navigator.credentials.get()`. | | [`Authentication.deserializeResponse`](/webauthn/webauthn/Authentication/deserializeResponse) | Deserializes a serialized authentication response. | | [`Authentication.getOptions`](/webauthn/webauthn/Authentication/getOptions) | Returns the request options to sign a challenge with the Web Authentication API. | | [`Authentication.getSignPayload`](/webauthn/webauthn/Authentication/getSignPayload) | Constructs the final digest that was signed and computed by the authenticator. This payload includes the cryptographic `challenge`, as well as authenticator metadata (`authenticatorData` + `clientDataJSON`). This value can be also used with raw P256 verification (such as `P256.verify` or `WebCryptoP256.verify`). | | [`Authentication.serializeOptions`](/webauthn/webauthn/Authentication/serializeOptions) | Serializes credential request options into a JSON-serializable format, converting `BufferSource` fields to base64url strings. | | [`Authentication.serializeResponse`](/webauthn/webauthn/Authentication/serializeResponse) | Serializes an authentication response into a JSON-serializable format, converting `BufferSource` fields to base64url strings and the signature to a hex string. | | [`Authentication.sign`](/webauthn/webauthn/Authentication/sign) | Signs a challenge using a stored WebAuthn P256 Credential. If no Credential is provided, a prompt will be displayed for the user to select an existing Credential that was previously registered. | | [`Authentication.verify`](/webauthn/webauthn/Authentication/verify) | Verifies a signature using the Credential's public key and the challenge which was signed. | ## Errors | Name | Description | | ------------------- | ----------------------------------- | | [`Authentication.SignFailedError`](/webauthn/webauthn/Authentication/errors#authenticationsignfailederror) | Thrown when a WebAuthn P256 credential request fails. | ## Types | Name | Description | | ------------------- | ----------------------------------- | | [`Authentication.Response`](/webauthn/webauthn/Authentication/types#authenticationresponse) | Response from a WebAuthn authentication ceremony. | # Authentication.deserializeOptions Deserializes credential request options that can be passed to `navigator.credentials.get()`. ## Imports :::code-group ```ts [Named] import { Authentication } from 'ox/webauthn' ``` ```ts [Entrypoint] import * as Authentication from 'ox/webauthn/Authentication' ``` ::: ## Examples ```ts twoslash import { Authentication } from 'ox/webauthn' const options = Authentication.getOptions({ challenge: '0xdeadbeef' }) const serialized = Authentication.serializeOptions(options) // ... send to server and back ... const deserialized = Authentication.deserializeOptions(serialized) // [!code focus] const credential = await window.navigator.credentials.get(deserialized) ``` ## Definition ```ts function deserializeOptions( options: Types.CredentialRequestOptions, ): Types.CredentialRequestOptions ``` **Source:** [src/webauthn/Authentication.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Authentication.ts#L57) ## Parameters ### options * **Type:** `Types.CredentialRequestOptions` The serialized credential request options. ## Return Type The deserialized credential request options. `Types.CredentialRequestOptions` # Authentication.deserializeResponse Deserializes a serialized authentication response. ## Imports :::code-group ```ts [Named] import { Authentication } from 'ox/webauthn' ``` ```ts [Entrypoint] import * as Authentication from 'ox/webauthn/Authentication' ``` ::: ## Examples ```ts twoslash import { Authentication } from 'ox/webauthn' const response = Authentication.deserializeResponse({ // [!code focus] id: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus] metadata: { // [!code focus] authenticatorData: '0x49960de5...', // [!code focus] clientDataJSON: '{"type":"webauthn.get",...}', // [!code focus] challengeIndex: 23, // [!code focus] typeIndex: 1, // [!code focus] userVerificationRequired: true // [!code focus] }, // [!code focus] raw: { // [!code focus] id: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus] type: 'public-key', // [!code focus] authenticatorAttachment: 'platform', // [!code focus] rawId: 'm1-bMPuAqpWhCxHZQZTT6e-lSPntQbh3opIoGe7g4Qs', // [!code focus] response: { clientDataJSON: 'eyJ0eXBlIjoid2ViYXV0aG4uZ2V0In0' } // [!code focus] }, // [!code focus] signature: '0x...' // [!code focus] }) // [!code focus] ``` ## Definition ```ts function deserializeResponse( response: Response, ): Response ``` **Source:** [src/webauthn/Authentication.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Authentication.ts#L123) ## Parameters ### response * **Type:** `Response` The serialized authentication response. #### response.id * **Type:** `string` #### response.metadata * **Type:** `{ authenticatorData: 0x${string}; challengeIndex?: number; clientDataJSON: string; typeIndex?: number; userVerificationRequired?: boolean; }` #### response.raw * **Type:** `PublicKeyCredential` #### response.signature * **Type:** `serialized extends true ? 0x${string} : { r: 0x${string}; s: 0x${string}; yParity?: number; }` ## Return Type The deserialized authentication response. `Response` # Authentication.getOptions Returns the request options to sign a challenge with the Web Authentication API. ## Imports :::code-group ```ts [Named] import { Authentication } from 'ox/webauthn' ``` ```ts [Entrypoint] import * as Authentication from 'ox/webauthn/Authentication' ``` ::: ## Examples ```ts twoslash import { Authentication } from 'ox/webauthn' const options = Authentication.getOptions({ challenge: '0xdeadbeef' }) const credential = await window.navigator.credentials.get(options) ``` ## Definition ```ts function getOptions( options: getOptions.Options, ): Types.CredentialRequestOptions ``` **Source:** [src/webauthn/Authentication.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Authentication.ts#L170) ## Parameters ### options * **Type:** `getOptions.Options` Options. #### options.challenge * **Type:** `0x${string}` The challenge to sign. #### options.credentialId * **Type:** `string | string[]` * **Optional** The credential ID to use. #### options.extensions * **Type:** `AuthenticationExtensionsClientInputs` * **Optional** List of Web Authentication API credentials to use during creation or authentication. #### options.rpId * **Type:** `string` * **Optional** The relying party identifier to use. #### options.timeout * **Type:** `number` * **Optional** Time, in milliseconds, that the caller is willing to wait for the operation. #### options.userVerification * **Type:** `UserVerificationRequirement` * **Optional** The user verification requirement. ## Return Type The credential request options. `Types.CredentialRequestOptions` # Authentication.getSignPayload Constructs the final digest that was signed and computed by the authenticator. This payload includes the cryptographic `challenge`, as well as authenticator metadata (`authenticatorData` + `clientDataJSON`). This value can be also used with raw P256 verification (such as `P256.verify` or `WebCryptoP256.verify`). :::warning This function is mainly for testing purposes or for manually constructing signing payloads. In most cases you will not need this function and instead use `Authentication.sign`. ::: ## Imports :::code-group ```ts [Named] import { Authentication } from 'ox/webauthn' ``` ```ts [Entrypoint] import * as Authentication from 'ox/webauthn/Authentication' ``` ::: ## Examples ```ts twoslash import { Authentication } from 'ox/webauthn' import { WebCryptoP256 } from 'ox' const { metadata, payload } = Authentication.getSignPayload( { // [!code focus] challenge: '0xdeadbeef' // [!code focus] } ) // [!code focus] const { publicKey, privateKey } = await WebCryptoP256.createKeyPair() const signature = await WebCryptoP256.sign({ payload, privateKey }) ``` ## Definition ```ts function getSignPayload( options: getSignPayload.Options, ): getSignPayload.ReturnType ``` **Source:** [src/webauthn/Authentication.ts](https://github.com/wevm/ox/blob/main/src/webauthn/Authentication.ts#L271) ## Parameters ### options * **Type:** `getSignPayload.Options` Options to construct the signing payload. #### options.challenge * **Type:** `0x${string}` The challenge to sign. #### options.crossOrigin * **Type:** `boolean` * **Optional** If set to `true`, it means that the calling context is an `