This page was exported from David Olliver [ https://www.davidolliver.com ]
Export date: Wed Mar 20 1:38:41 2019 / +0000 GMT

C#: Create Escrow Transactions + Memos on XRP Ledger (Part II)




In my previous article, I wrote about "Transaction Basics: Sending a transaction to the XRP Ledger". This time let me give you some of the rudimentary information about "ESCROW". As you might know, it's a feature to send conditional XRP payments. The idea of this feature is to set aside XRP and release it later when specific conditions are met.

The XRP set aside in an escrow is locked up. No one can use or destroy the XRP until the escrow has been successfully finished or canceled. Before the expiration time, only the intended receiver can get the XRP. After the expiration time, the XRP can only be returned to the sender.

That's what we read on XRP Ledger Dev Portal. When we send an escrow, we usually use an EscrowCreate transaction to lock up some XRP.

The following is an example of what a simple Escrow transaction looks like:

[cc lang="javascript"]

{

"Account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",

"TransactionType": "EscrowCreate",

"Amount": "10000",

"Destination": "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW",

"CancelAfter": 533257958,

"FinishAfter": 533171558,

"Condition": "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855810100",

"DestinationTag": 23480,

"SourceTag": 11747

}

[/cc]

Pay attention to the fields used for this transaction. You'll notice EscrowCreate transaction uses some additional fields, such as:

CancelAfterNumberUInt32(Optional) The time, in seconds since the Ripple Epoch, when this escrow expires. This value is immutable; the funds can only be returned the sender after this time.
FinishAfterNumberUInt32(Optional) The time, in seconds since the Ripple Epoch, when the escrowed XRP can be released to the recipient. This value is immutable; the funds cannot move until this time is reached.
ConditionStringBlob(Optional) Hex value representing a PREIMAGE-SHA-256 crypto-condition. The funds can only be delivered to the recipient if this condition is fulfilled.
DestinationTagNumberUInt32(Optional) Arbitrary tag to further specify the destination for this escrowed payment, such as a hosted recipient at the destination address.

Either CancelAfter or FinishAfter must be specified. If both are included, the FinishAfter time must be before the CancelAfter time.

Now that we already have the transaction structure we need to make changes to an existing one.Add the following where necessary

[cc lang="csharp"]

public long? FinishAfter { get; set; }

[/cc]

And you'll have something like this:

[cc lang="csharp"]

public class RootObject

{

public string TransactionType { get; set; }

public long Flags { get; set; }

public int Sequence { get; set; }

public string Amount { get; set; }

public string Fee { get; set; }

public long? FinishAfter { get; set; }

public string Account { get; set; }

public string Destination { get; set; }

public List Memos { get; set; }

}

[/cc]

What we know about Specifying Time?

FinishAfter - The time, in seconds since the Ripple Epoch, when the escrowed XRP can be released to the recipient. This value is immutable; the funds cannot move until this time is reached.

And if we follow the provided information:

The rippled server and its APIs represent time as an unsigned integer. This number measures the number of seconds since the "Ripple Epoch" of January 1, 2000 (00:00 UTC). This is like the way the Unix epoch works, except the Ripple Epoch is 946684800 seconds after the Unix Epoch.

we have to build the function "ConvertToUnixTime based on Ripple time" - 2000, 1, 1, 0, 0, 0, 0. Here is what the function looks like which I've coded for my solution:

[cc lang="csharp"]

public static long ConvertToUnixTime(DateTime date)

{

var ripplepoch = new DateTime(2000, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

return Convert.ToInt64((date.ToUniversalTime() - ripplepoch).TotalSeconds);

}

[/cc]

Below you'll find the usage:

[cc lang="csharp"]

obj.FinishAfter = ToUnixTime(DateTime.Now.AddMinutes(1));

[/cc]

Now let's see the complete code for Escrow Transaction + Memos:

[cc lang="csharp"]

IRippleClient client = new RippleClient("wss://s.altnet.rippletest.net:51233");

client.Connect();

string msg = "testing";

byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(msg);

string hex = BitConverter.ToString(utfBytes).Replace("-", "");

// List rootObject = new List();

var secret = "XXXXXXXXXX";

RootObject obj = new RootObject();

List memos = new List();

Memos mm = new Memos();

obj.TransactionType = "EscrowCreate";

obj.Flags = 2147483648;

obj.Sequence = 90;

obj.Amount = "18000000";

obj.Fee = "10";

obj.Account = "rp5EciKXsqUb6ZWkFQBGzeGACTsgfkLp6E";

obj.Destination = "rJAGYcpj2TGePnEmKfqmNAfjQFhrBZdzRr";

obj.FinishAfter = ToUnixTime(DateTime.Now.AddMinutes(1));

mm.Memo.MemoData = hex;

mm.Memo.MemoType = hex;

memos.Add(mm);

obj.Memos = memos;

string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

var signed = TxSigner.SignJson(JObject.Parse(json), secret);

textBox1.Text = signed.Hash + "rn" + signed.TxJson + "rn" + signed.TxBlob;

SubmitBlobRequest request = new SubmitBlobRequest();

request.TransactionBlob = signed.TxBlob;

Submit result = await client.SubmitTransactionBlob(request);

textBox3.Text = result.Transaction.Hash + "rn";

client.Disconnect();

[/cc]

The "using" directives are the same we've talked about in the previous article. Please, read more about DateTime.AddMinutes(Double) Method

 

 


Post date: 2018-12-21 13:08:35
Post date GMT: 2018-12-21 13:08:35
Post modified date: 2018-12-21 14:13:36
Post modified date GMT: 2018-12-21 14:13:36

Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team www.gVectors.com