[DRAFT] Class XDR Implementation#1422
Draft
Ryang-21 wants to merge 27 commits into
Draft
Conversation
…ndly XdrString wrapper class
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The legacy
@stellar/js-xdrruntime had several rough edges thatconstrained the SDK from inside:
value.switch(),value.value(),value.arm()) with no TypeScript narrowing — everyunion access needed an
ascast.AssetType.assetTypeNative())even though the members are cached singletons under the hood. The
function-call ergonomics broke pattern-matching on enum identity and
made
switchstatements over enums verbose.new Int128(lo, hi))and exposed
slice()for accessing 32- or 64-bit chunks. The bigintvalue was only reachable via
.toBigInt().Int64/Uint64were object-boxed (Hyper/UnsignedHyperextending
LargeInt) rather than nativebigint.stellar-xdr-jsonwas a separate process.toXDR,fromXDR) where theSDK's broader convention is single-initial-cap (
toString, etc.).Additionally, this PR introduces several capabilities the legacy runtime
didn't have at all (not fixes, additions):
toJson/fromJsonon every XDR value, SEP-0051-compliant.toXdrObject/fromXdrObjectbridging methods (legacy types heldwire shape directly, so this distinction wasn't meaningful).
XdrStringwrapper for byte-faithfulstring<N>handling.What
A full in-tree replacement of the XDR layer (this will be moved to js-xdr but for testing purposes it lives here for now):
src/xdr/core/—Reader,Writer,BaseType<T>. Buffer I/Oprimitives.
src/xdr/types/— schema primitives (struct,union,enum,opaque,varOpaque,string,int32/int64/etc.,array,fixedArray,option,lazy). Charset-agnostic; no dependency onconsumer value classes. Designed to be liftable into a standalone XDR
runtime.
src/xdr/values/— consumer-facing bases.XdrValue(universalbase with
toXdr/fromXdr/toJson/fromJson),EnumValue,BytesValue,BigIntValue,XdrString. The SEP-0051 JSON walkerlives here (
to-json.ts).src/xdr/generated/— 447 generated classes produced bytools/xdrgen/generate.mjsreadingxdr/xdr.json. TSDoc on each classcarries the original
.xsource so hovers show the upstream XDRdeclaration.
src/xdr/dx/— hand-written DX overlays (Int128/Uint128/Int256/Uint256exposing a singlebigint) and primitive shims(
Int64/Uint64/Int32/Uint32returning native primitives via aProxyconstructtrap).Plus:
toXDR→toXdr,fromXDR→fromXdron every type.
toXDRObject/fromXDRObject/fromJSONlistedelsewhere are new methods with no legacy analogue.
LargeIntclasses insrc/base/numbers/—Int128,Uint128,Int256,Uint256now hold a singlebigint.XdrStringwrapper forstring<N>fields — bytes-faithful, withexplicit
.toString()/.toStringStrict()/.asStringOrBytes()/.bytes/.toJson()access patterns.value.toJson()andType.fromJson(json)onevery XDR class.
hand-written smoke tests (~15), real-traffic mainnet corpus (~15),
schema-driven exhaustive coverage (~2000), JSON walker round-trips
(~120).
docs/XDR_MIGRATION.md— caller-facing migration guide.docs/ARCHITECTURE.md— contributor-facing internals.Example changes
Union access —
.typeliteral narrowing replaces method-call accessors:Enum access — drop the parens; the singleton was already cached:
Wide ints — single bigint instead of parts arithmetic:
XDR strings —
XdrStringwrapper with explicit decoding:SEP-0051 JSON output (new — no legacy equivalent):
Method renames:
Bytes — explicit wrappers required:
Typedef-opaque aliases became distinct classes: