Jump to content

Help with Memo field [RippleAPI]


hotpocket

Recommended Posts

Hi folks,

I'm using RippleAPI (the NodeJS library) to write a quick proof-of-concept for an idea I have. Basically what I'm trying to do is store some data in the Memo field. I can do it easily enough with some plain text (e.g., "Here is text" below):

  const preparedTx = await api.prepareTransaction(
    {
      TransactionType: "Payment",
      Account: ADDRESS,
      Amount: api.xrpToDrops("22"), // Same as "Amount": "22000000"
      Destination: "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM",
      Memos: [
          Memo: {
            MemoData: Buffer.from("Here is text", "utf8")
              .toString("hex")
              .toUpperCase()
          }
        }
      ]
    },
    {
      // Expire this transaction if it doesn't execute within ~5 minutes:
      maxLedgerVersionOffset: 5
    }
  );

After submitting the tx I just do this to get the text:

let data = tx.specification.memos[0].data;
console.log(Buffer.from(data, "hex").toString("utf-8")); // => "Here is text"

What I'm having difficulty doing is submitting encrypted data in the memo field, e.g.:

const preparedTx = await api.prepareTransaction(
    {
      TransactionType: "Payment",
      Account: ADDRESS,
      Amount: api.xrpToDrops("22"), // Same as "Amount": "22000000"
      Destination: "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM",
      Memos: [
        {
          Memo: {
            MemoData: encrypted.toUpperCase()
          }
        }
      ]
    },
    {
      // Expire this transaction if it doesn't execute within ~5 minutes:
      maxLedgerVersionOffset: 5
    }
  );

console.log(preparedTx);
// => {"TransactionType":"Payment","Account":"rHVQntm23skDRzDrrjtFjr7iWkZvwTPvYd","Amount":"22000000","Destination":"rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM","Memos":[{"Memo":{"MemoData":"EB8C1317FBD61EBF271022486402753A4373E80A6ED498DEB437B27E44E07DB9AC4A8B5BF635E10232A236CE784DCBFC"}}],"Flags":2147483648,"LastLedgerSequence":2617395,"Fee":"12","Sequence":2614128}

It looks like the memo field is properly in hex format, but when I print `tx.specification.memos` after submitting the tx, the memo data field looks unrecognizable:

[
  {
    data: `�\u0013\u0017��\u001e�'\u0010"Hd\u0002u:Cs�\nnԘ޴7�~D�}��J�[�5�\u00022�6�xM��`
  }
]

How do I fix this to show the hex memo data? (I'm not good with encodings, please forgive me if I'm not using the right terms)

Without going too much into the specifics, here is the encryption function:

function encrypt(text, secret) {
  const IV_LENGTH = 16;
  let iv = crypto.randomBytes(IV_LENGTH);
  console.log("SECRET_LENGTH", secret.length * 4);
  let cipher = crypto.createCipheriv(
    "aes-" + secret.length * 4 + "-cbc",
    Buffer.from(secret, "hex"),
    iv
  );
  let encrypted = cipher.update(text);
  encrypted = Buffer.concat([encrypted, cipher.final()]);
  return iv.toString("hex") + encrypted.toString("hex");
}

 

Edited by hotpocket
Add encryption function
Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Sorry that I can't help you with your specific problem, but I just noticed that you've set    maxLedgerVersionOffset: 5    to represent ~5 minutes but according to https://xrpl.org/send-xrp.html#2-prepare-transaction it should be set to 75.

It probably makes no difference if you're immediately submitting the transaction, but if you're moving a prepared transaction to an offline device and then back to a online device for submission you're probably going to have the transaction fail. This is because, while you're transferring transactions around, the 'Ledger Version Number' would've already progressed past the 'Current Ledger Version Number ' PLUS the maxLedgerVersionOffset.

If you use 5 for the maxLedgerVersionOffset it only gives you ~20 seconds to submit the transaction from the time you prepared it.

Admittedly, this isn't very helpful advice right now, but it might prevent some confusion later on. Good luck.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...