Create XRPL transaction with memo

WATCH DEMO: In my previous article I talked about Sending a (XRP) payment using offline signing . This time let’s discuss Transaction Memos. In addition to this, you will also learn how to create JSON in C#. According to Ripple : Every transaction can optionally have an array of memos for user applications. The memos field in each transaction specification is an array of objects with the following structure:
Name Type Description
data string Optional Arbitrary string, conventionally containing the content of the memo.
format string Optional Conventionally containing information on how the memo is encoded, for example as a MIME type. Only characters allowed in URLs are permitted.
type string Optional Conventionally, a unique relation (according to RFC 5988) that defines the format of this memo. Only characters allowed in URLs are permitted.
That being said, let’s have a look at the JSON structure of MEMO:
[{"Memo": {"MemoData": "MemoData","MemoFormat": "MemoType","MemoType": "MemoType"}}]
We use JSON for real-time data communication between a Server and a Web Application which means we can send and receive JSON data and the approach we are going to use seems obvious: we will discuss the ways in which a user can create JSON object using C#. JSON has become very popular as a data exchange format thanks to its pretty simple syntax. Now, let’s get started. TOOLS WE NEED:– Json.NET- RippleDotNet library ( RippleNetCore library (download it from NuGet)- Visual Studio 2017 (for better performance)

How to Create JSON Objects Using C# Code

We need to generate c# classes from json so we do the following:
public class Memos{public Memos(){Memo = new Memo();}[JsonProperty("Memo")]public Memo Memo { get; set; }}
public class Memo{public string MemoData { get; set; }public string MemoFormat { get; set; }public string MemoType { get; set; }
Ok, now, we need to use JSON.Net – a popular high-performance JSON framework for .NET to serialize the object:
List Memos = new List();var myMemos = new Memos();myMemos.Memo.MemoData = "Example";myMemos.Memo.MemoType = "Example";Memos.Add(myMemos);
string myJson = JsonConvert.SerializeObject(Memos, Formatting.Indented);
Actually, you will have to convert your string to HEX. You can try this basic approach:
string yourMemo = "testing";byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(yourMemo);string ToHexString = BitConverter.ToString(utfBytes).Replace("-", "");
and then when do the following:
myMemos.Memo.MemoData = ToHexString;myMemos.Memo.MemoType = ToHexString;
Now that you have prepared your MEMO you have to decide what are you going to do with it. Let’s suppose you are using RippleNetCore library which you download from NuGet. In this case, use the following statement:
using Ripple.TxSigning;
and don’t forget to have a look at my previous article where you will find the necessary code to build your Transaction:
var unsignedTxJson = @"{'Account': 'rp5EciKXsqUb6ZWkFQBGzeGACTsgfkLp6E','Amount': '18000000','Destination': 'rJAGYcpj2TGePnEmKfqmNAfjQFhrBZdzRr','Fee': '10','Sequence': " + paymentTransaction.Sequence.ToString() + @",'TransactionType' : 'Payment'}";
While playing with it I’ve found that you can use Anonymous Types as well and do something like this:
var anonArray = new{Memos = new[]{new{Memo = new{MemoType = ToHexString,MemoData = ToHexString}}}};
string myjson = JsonConvert.SerializeObject(anonArray, Formatting.Indented);
You will have to make changes accordingly though.  And finally, let’s have a look what a simple memo Transaction looks like:
var unsignedTxJson = @"{'Account': 'rXXXXXXXXXX','Amount': '18000000','Destination': 'rXXXXXXXXXX','Fee': '10','Sequence': '62','TransactionType': 'Payment','Memos': [{'Memo': {'MemoType': '" + ToHexString + @"','MemoData': '" + ToHexString + @"'}}]}";

 made with love