Skip to content

Transia-RnD/XrplCSharp

Repository files navigation

NuGet Badge

XrplCSharp

  • This library would not be possible without Chris Williams.

A pure C# implementation for interacting with the XRP Ledger, the XrplCSharp library simplifies the hardest parts of XRP Ledger interaction, like serialization and transaction signing, by providing native C# methods and models for XRP Ledger transactions and core server API (rippled) objects.

// create a network client
using System.Diagnostics;
using Xrpl.Client;
var client = new XrplClient("wss://s.altnet.rippletest.net:51233");
client.OnConnected += async () =>
{
    Debug.WriteLine("CONNECTED");
};
await client.Connect();

// create a wallet on the testnet
XrplWallet testWallet = XrplWallet.Generate();
await WalletSugar.FundWallet(client, testWallet);
Debug.WriteLine(testWallet);
// public_key: ED3CC1BBD0952A60088E89FA502921895FC81FBD79CAE9109A8FE2D23659AD5D56
// private_key: -HIDDEN -
// classic_address: rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo

// look up account info
//using System.Diagnostics;
//using Xrpl.Model.Account;
string account = "rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo";
AccountInfoRequest request = new AccountInfoRequest(account);
AccountInfo accountInfo = await client.AccountInfo(request);
Debug.WriteLine(accountInfo);
// {
//     "Account": "rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo",
//     "Balance": "1000000000",
//     "Flags": 0,
//     "LedgerEntryType": "AccountRoot",
//     "OwnerCount": 0,
//     "PreviousTxnID": "73CD4A37537A992270AAC8472F6681F44E400CBDE04EC8983C34B519F56AB107",
//     "PreviousTxnLgrSeq": 16233962,
//     "Sequence": 16233962,
//     "index": "FD66EC588B52712DCE74831DCB08B24157DC3198C29A0116AA64D310A58512D7"
// }

Installation and supported versions

The XrplCSharp library is available on DotNet. Install with dotnet:

dotnet add package XrplCSharp --version 1.0.0

The library supports Dotnet 6 and later.

Features

Use XrplCSharp to build C# applications that leverage the XRP Ledger. The library helps with all aspects of interacting with the XRP Ledger, including:

  • Key and wallet management
  • Serialization
  • Transaction Signing

XrplCSharp also provides:

  • A network client — See xrpl.clients for more information.
  • Methods for inspecting accounts — See XRPL Account Methods for more information.
  • Codecs for encoding and decoding addresses and other objects — See Core Codecs for more information.

See the complete XrplCSharp reference documentation on Read the Docs.

Usage

The following sections describe some of the most commonly used modules in the XrplCSharp library and provide sample code.

Network client

Use the Xrpl.Client library to create a network client for connecting to the XRP Ledger.

using System.Diagnostics;
using Xrpl.Client;
var client = new XrplClient("wss://s.altnet.rippletest.net:51233");
client.OnConnected += async () =>
{
    Debug.WriteLine("CONNECTED");
};
await client.Connect();

Manage keys and wallets

Xrpl.Wallet

Use the Xrpl.Wallet module to create a wallet from a given seed or or via a Testnet faucet.

To create a wallet from a seed (in this case, the value generated using Xrpl.Keypairs):

using System.Diagnostics;
using Xrpl.Wallet;
// ...
string seed = "s";
XrplWallet wallet = XrplWallet.FromSeed(seed);
Debug.WriteLine(wallet);
// pub_key: ED46949E414A3D6D758D347BAEC9340DC78F7397FEE893132AAF5D56E4D7DE77B0
// priv_key: -HIDDEN-
// classic_address: rG5ZvYsK5BPi9f1Nb8mhFGDTNMJhEhufn6

To create a wallet from a Testnet faucet:

using System.Diagnostics;
using Xrpl.Wallet;
XrplWallet testWallet = XrplWallet.Generate();
await WalletSugar.FundWallet(client, testWallet);
Debug.WriteLine(testWallet.ClassicAddress);
# Classic address: rEQB2hhp3rg7sHj6L8YyR4GG47Cb7pfcuw

Xrpl.Keypairs

Use the Xrpl.Keypairs module to generate seeds and derive keypairs and addresses from those seed values.

Here's an example of how to generate a seed value and derive an XRP Ledger "classic" address from that seed.

using System.Diagnostics;
using Xrpl.Wallet;
// ...
XrplWallet wallet = XrplWallet.Generate();
string publicKey = wallet.PublicKey;
string privateKey = wallet.PrivateKey;
Debug.WriteLine("Here's the public key:");
Debug.WriteLine(publicKey);
Debug.WriteLine("Here's the private key:");
Debug.WriteLine(privateKey);
Debug.WriteLine("Store this in a secure place!");
// Here's the public key:
// ED3CC1BBD0952A60088E89FA502921895FC81FBD79CAE9109A8FE2D23659AD5D56
// Here's the private key:
// EDE65EE7882847EF5345A43BFB8E6F5EEC60F45461696C384639B99B26AAA7A5CD
// Store this in a secure place!

Note: You can use Keypairs to sign transactions but XrplCSharp also provides explicit methods for safely signing and submitting transactions. See Transaction Signing and XRPL Transaction Methods for more information.

Serialize and sign transactions

To securely submit transactions to the XRP Ledger, you need to first serialize data from JSON and other formats into the XRP Ledger's canonical format, then to authorize the transaction by digitally signing it with the account's private key. The XrplCSharp library provides several methods to simplify this process.

Use the xrpl.transaction module to sign and submit transactions. The module offers three ways to do this:

using System.Diagnostics;
using Xrpl.Models.Transactions;
using Xrpl.Models.Methods;
using Newtonsoft.Json;
// ...
string classicAddress = "rBtXmAdEYcno9LWRnAGfT9qBxCeDvuVRZo";
AccountInfoRequest request = new AccountInfoRequest(wallet.ClassicAddress);
AccountInfo accountInfo = await client.AccountInfo(request);

// prepare the transaction
// the amount is expressed in drops, not XRP
// see https://xrpl.org/basic-data-types.html#specifying-currency-amounts
IPayment tx = new Payment()
{
    Account = wallet.ClassicAddress,
    Destination = "rEqtEHKbinqm18wQSQGstmqg9SFpUELasT",
    Amount = new Xrpl.Models.Common.Currency { ValueAsXrp = 1 },
    Sequence = accountInfo.AccountData.Sequence
};

// submit the transaction
Dictionary<string, dynamic> txJson = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(tx.ToJson());
Submit response = await client.Submit(txJson, wallet);
Debug.WriteLine(response);

Get fee from the XRP Ledger

In most cases, you can specify the minimum transaction cost of "10" for the fee field unless you have a strong reason not to. But if you want to get the current load-balanced transaction cost from the network, you can use the Fees function:

using System.Diagnostics;
using Xrpl.Models.Transactions;
FeeRequest feeRequest = new FeeRequest();
Fee fee = await client.Fee(feeRequest);
Debug.WriteLine(fee);
# 10

Contributing

If you want to contribute to this project, see CONTRIBUTING.md.

Mailing Lists

We have a low-traffic mailing list for announcements of new XrplCSharp releases. (About 1 email per week)

If you're using the XRP Ledger in production, you should run a rippled server and subscribe to the ripple-server mailing list as well.

License

The XrplCSharp library is licensed under the ISC License. See LICENSE for more information.

Repository Credit

The credit of this repository goes to Chris Williams.

https://github.com/chriswill

Thank you Chris.