Class XDR Implementation#1422
Merged
Merged
Conversation
* Replace __USE_AXIOS__ dynamic require with static fetch default * Add babel and webpack aliasing to emit axios variant from shared source * Flip package.json exports: fetch default, /axios opt-in, drop /no-axios * Update vitest configs and test harness for TRANSPORT=axios variant * Update CI, README, and stale references for new fetch-default build matrix
* update eventsource to v4.1.0 and remove conditional import of eventsource * remove no-eventsource lib bundle as the eventsource dependency runs in the workerd runtime * remove the dom-monkeypatch as its now included in the tsconfig lib field * add additional tests for eventsource behavior
* add type: module in pkg.json and update js config files to esm style * build and export cjs and esm build varients, remove export of umd bundles * remove unused dependency (causing issue with hook files being cjs format)
* replace webpack + babel dependencies for rollup * migrate build tools to rollup
* move stellar-base src under src/base * add unit tests from stellar-base * port xdr and makefile for generating xdr * port manual type declarations for js only dependencies * inline js-xdr for node esm compatibility * disable bundle_size workflow
* migrate from classic yarn to pnpm * add pnpm setup action to git workflows
* remove randomBytes for universal crypto.getRandomValues * replace sha.js with noble/hashes and update to version 2.2.0 * update BigNumber to v11.0.0 * replace noble/curves with noble/ed25519 for reduced bundle size * move tsconfig project root * replace toml with smol-toml
* refactor: replace URI usage for native URL + URLSearchParam objects * remove usage of non-null assertion * allow expandUriTemplate to handle relative templated links
* test src code directly instead of the built lib
…p nyc (#1408) * Update husky config + remove nyc * Root .nvmrc + bump Node to v22 * pnpm minimumReleaseAge config * Cleanup
…ndly XdrString wrapper class
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
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-xdrv4 runtime 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. TheJavaScript-standard
toJSON()hook delegates totoJson(), soJSON.stringifyemits SEP-0051 too — including for XDR values nestedinside plain objects (loggers,
res.json, snapshots), where it wouldotherwise throw on bigint fields.
toXdrObject/fromXdrObjectbridging methods (legacy types heldwire shape directly, so this distinction wasn't meaningful).
XdrStringwrapper for byte-faithfulstring<N>handling.decodeStream(Type, bytes)for decoding several concatenated valuesof one type (contract-spec entry streams, etc.).
expectUnionVariant/isUnionVariant.What
The XDR layer is rebuilt on
@stellar/js-xdrv5, which now providesthe generic RFC 4506 runtime (
Reader/Writerbyte I/O, theBaseType<T>schema interface, and the schema primitives —struct,union,enumType,opaque,varOpaque,string, ints,array,option,lazy, …). Everything Stellar-specific lives in this repounder
src/xdr/:values/— consumer-facing bases.XdrValue(universal base withtoXdr/fromXdr/toJson/fromJson),EnumValue,BytesValue,BigIntValue,XdrString. The SEP-0051 JSON walker lives here(
to-json.ts), with per-schema-name overrides for StrKey forms,wide-int decimal strings, and asset-code trimming.
generated/— 449 generated classes produced bytools/xdrgen/generate.mjsreadingxdr/xdr.json(regeneratedagainst protocol 23, including the CAP-71
SOROBAN_CREDENTIALS_ADDRESS_V2credentials). TSDoc on each class carries the original
.xsource sohovers show the upstream XDR declaration.
dx/— hand-written DX overlays (Int128/Uint128/Int256/Uint256exposing a singlebigint) plusInt64/Uint64/Int32/Uint32shims returning native primitives from the call form(
xdr.Int64(v)); calling them withnewthrows a descriptiveTypeErrorat the call site.index.ts— the single public entry, exported as thexdrnamespace. The js-xdr
Reader/Writerand schema-builder factoriesare deliberately not re-exported; streaming callers use
xdr.decodeStreaminstead.Plus:
toXDR→toXdr,fromXDR→fromXdron every type.
toXdrObject/fromXdrObject/fromJsonare newmethods with no legacy analogue.
LargeIntclasses insrc/base/numbers/—Int128,Uint128,Int256,Uint256now hold a singlebigint.XdrLargeIntkeeps the legacy multi-slice constructor contract(little-endian,
parts[0]= least significant) and now range-checkseach slice at construction.
XdrStringwrapper forstring<N>fields — bytes-faithful, withexplicit
.toString()/.toStringStrict()/.asStringOrBytes()/.bytes/.toJson()access patterns.value.toJson()andType.fromJson(json)onevery XDR class.
authorizeEntry/authorizeInvocationgain opt-inADDRESS_V2credentials (
authV2: true) and low-level delegate-signing primitives(
buildAuthorizationEntryPreimage,buildWithDelegatesEntry).test/unit/xdr/validating wirecompatibility with the legacy SDK: hand-written smoke tests, a
real-traffic mainnet corpus, schema-driven exhaustive coverage
(~2000 generated cases), and JSON walker round-trips.
docs/XDR_MIGRATION.md— caller-facing migration guide.src/xdr/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: