What is @stellar/js-xdr?
@stellar/js-xdr is a JavaScript library for working with XDR (External Data Representation) data structures, specifically tailored for the Stellar network. It allows developers to encode and decode XDR data, which is used extensively in the Stellar protocol for defining data structures and messages.
What are @stellar/js-xdr's main functionalities?
Encoding XDR Data
This feature allows you to encode data into XDR format. The code sample demonstrates creating a data structure and encoding it into XDR, which is then output as a base64 string.
const xdr = require('@stellar/js-xdr');
const myData = new xdr.MyDataStructure({
field1: 'value1',
field2: 1234
});
const encoded = myData.toXDR();
console.log(encoded.toString('base64'));
Decoding XDR Data
This feature allows you to decode XDR data back into a JavaScript object. The code sample shows how to take a base64 encoded XDR string, decode it, and access the resulting data structure.
const xdr = require('@stellar/js-xdr');
const encodedData = Buffer.from('base64encodedstring', 'base64');
const myData = xdr.MyDataStructure.fromXDR(encodedData);
console.log(myData);
Defining Custom XDR Types
This feature allows you to define custom XDR types using the library's API. The code sample demonstrates defining a custom structure with specific fields and encoding it into XDR.
const xdr = require('@stellar/js-xdr');
const MyCustomType = xdr.struct([['field1', xdr.string()], ['field2', xdr.int()]]);
const myData = new MyCustomType({
field1: 'example',
field2: 42
});
console.log(myData.toXDR().toString('base64'));
Other packages similar to @stellar/js-xdr
xdr-js-serialize
xdr-js-serialize is a library for serializing and deserializing XDR data structures in JavaScript. It provides similar functionality to @stellar/js-xdr but is more general-purpose and not specifically tailored for the Stellar network.
XDR, for Javascript
Read/write XDR encoded data structures (RFC 4506)

XDR is an open data format, specified in
RFC 4506. This library provides a way
to read and write XDR data from javascript. It can read/write all of the
primitive XDR types and also provides facilities to define readers for the
compound XDR types (enums, structs and unions)
Installation
via npm:
npm install --save @stellar/js-xdr
Usage
You can find some examples here.
First, let's import the library:
var xdr = require('@stellar/js-xdr');
import xdr from '@stellar/js-xdr';
Now, let's look at how to decode some primitive types:
xdr.Bool.fromXDR([0, 0, 0, 0]);
xdr.Bool.fromXDR([0, 0, 0, 1]);
xdr.Bool.toXDR(true);
xdr.Int.fromXDR([0xff, 0xff, 0xff, 0xff]);
xdr.UnsignedInt.fromXDR([0xff, 0xff, 0xff, 0xff]);
var result = xdr.Hyper.fromXDR([0, 0, 0, 0, 0, 0, 0, 0]);
result = new xdr.Hyper(0);
result.toString();
var ten = result.toBigInt() + 10;
var minusone = result.toBigInt() - 1;
var big = xdr.Hyper.fromString('1099511627776');
big.toXDR();
Caveats
There are a couple of caveats to be aware of with this library:
- We do not support quadruple precision floating point values. Attempting to
read or write these values will throw errors.
- NaN is not handled perfectly for floats and doubles. There are several forms
of NaN as defined by IEEE754 and the browser polyfill for node's Buffer
class seems to handle them poorly.
Code generation
js-xdr
by itself does not have any ability to parse XDR IDL files and produce
a parser for your custom data types. Instead, that is the responsibility of
xdrgen
. xdrgen will take your .x files
and produce a javascript file that target this library to allow for your own
custom types.
See stellar-base
for an example
(check out the src/generated directory)
Contributing
Please see CONTRIBUTING.md for details.
To develop and test js-xdr itself
- Clone the repo
git clone https://github.com/stellar/js-xdr.git
- Install dependencies inside js-xdr folder
cd js-xdr
npm i
- Install Node 14
Because we support the oldest maintenance version of Node, please install and
develop on Node 14 so you don't get surprised when your code works locally but
breaks in CI.
Here's out to install nvm
if you haven't: https://github.com/creationix/nvm
nvm install
# if you've never installed 14.x before you'll want to re-install yarn
npm install -g yarn
If you work on several projects that use different Node versions, you might it
helpful to install this automatic version manager:
https://github.com/wbyoung/avn
- Observe the project's code style
While you're making changes, make sure to run the linter periodically to catch any linting errors (in addition to making sure your text editor supports ESLint)
yarn fmt
If you're working on a file not in src
, limit your code to Node 14! See what's
supported here: https://node.green/ (The reason is that our npm library must
support earlier versions of Node, so the tests need to run on those versions.)