Skip to content

💡 [SDKv3] Simplify RLP encoding #2737

@claytonneal

Description

@claytonneal

Summary

At the moment we have built a "generic rlp encoding solution" and then applied it to a vechain transaction
And we have made use of dependency @ethereumjs/rlp to do that
We also expose publicly RLP classes that really users won't need

The objective is to simplify this, we do not need a general rlp encoding solution, only something that is specific to encode vechain transaction / decode vechain transaction. We can make use of viem dependency for this and remove the @ethereumjs/rlp dependency

Rough design (for encoding):

Helpers:

import { toHex, type Hex } from 'viem'

type RlpPrimitive =
  | Hex
  | number
  | bigint
  | null
  | undefined

export function toRlpValue(value: RlpPrimitive): Hex {
  if (value === null || value === undefined) return '0x'

  if (typeof value === 'number' || typeof value === 'bigint') {
    const v = BigInt(value)
    return v === 0n ? '0x' : toHex(v)
  }

  // Hex string → raw bytes
  return value
}

Usage:

const clause = [
  toRlpValue(c.to),
  toRlpValue(c.value),
  toRlpValue(c.data),
]

const body = [
  toRlpValue(tx.chainTag),
  toRlpValue(tx.blockRef),
  toRlpValue(tx.expiration),
  clauses,
  toRlpValue(tx.gasPriceCoef),
  toRlpValue(tx.gas),
  toRlpValue(tx.dependsOn),
  toRlpValue(tx.nonce),
  tx.reserved ?? [],
]

const encoded = toRlp(body)

Basic Example

a

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions