Skip to content

Instantly share code, notes, and snippets.

@sunny-g
Forked from justmoon/gist:9462822
Created August 5, 2014 14:44
Show Gist options
  • Save sunny-g/578d9db4fccbd1c26fab to your computer and use it in GitHub Desktop.
Save sunny-g/578d9db4fccbd1c26fab to your computer and use it in GitHub Desktop.

Take this example SWIFT message:

{1:F01MIDLGB22AXXX0548034693}{2:I103BKTRUS33XBRDN3}{3:{108:MT103}}{4:
:20:8861198-0706
:23B:CRED
:32A:000612USD5443,99
:33B:USD5443,99
:50K:GIAN ANGELO IMPORTS
NAPLES
:52A:BCITITMM500
:53A:BCITUS33
:54A:IRVTUS3N
:57A:BNPAFRPPGRE
:59:/20041010050500001M02606
KILLY S.A.
GRENOBLE
:70:/RFB/INVOICE 559661
:71A:SHA
-}

Using the MemoType swift we can embed that into a Ripple transaction:

{
  "Flags":0,
  "TransactionType":"Payment",
  "Account":"rNb721TdNHN37yoURrMYDiQFmvXmENCZW6",
  "Amount":"100",
  "Destination":"r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
  "Sequence":1,
  "Fee":"12",
  "SigningPubKey":"02398E2CC7E72DC3BE5AC05EF20D85C8E450AD7A2044F7ACAA58B2B9844F252C35",
  "Memos":[
    {
      "Memo":{
        "MemoType":"swift",
        "MemoData":"{1:F01MIDLGB22AXXX0548034693}{2:I103BKTRUS33XBRDN3}{3:{108:MT103}}{4:\n:20:8861198-0706\n:23B:CRED\n:32A:000612USD5443,99\n:33B:USD5443,99\n:50K:GIAN ANGELO IMPORTS\nNAPLES\n:52A:BCITITMM500\n:53A:BCITUS33\n:54A:IRVTUS3N\n:57A:BNPAFRPPGRE\n:59:/20041010050500001M02606\nKILLY S.A.\nGRENOBLE\n:70:/RFB/INVOICE 559661\n:71A:SHA\n-}"
      }
    }
  ],
  "TxnSignature":"3045022100A5B1384EB8C97B529CDB1898B5D2DE01B2EEDFE0A959ABBE4A940D298848F3EE02202FACCAF7AD90C4C43045F66C7826F79A5C0426284CB0027CB88B95F577F20F2B"
}

Finally, we'll serialize it into the binary format:

"1200002200000000240000000161400000000000006468400000000000000C732102398E2CC7E72D
C3BE5AC05EF20D85C8E450AD7A2044F7ACAA58B2B9844F252C35744630440220128EB80710D61F36
148CD257D1C8B7E316E81ED12FB19CFA58FE5F8EB66B91E602201A80A1B4D335BEF3318ADD6B342D
B52AE879DB544D4F3BBB2855939024DEB9258114951ECDC771FCB9AB66FF0447AFB90A3CE27598CB
83145E7B112523F68D2F5E879DB4EAC51C6698A69304F9EA7C0573776966747DC1787B313A463031
4D49444C4742323241585858303534383033343639337D7B323A49313033424B5452555333335842
52444E337D7B333A7B3130383A4D543130337D7D7B343A0A3A32303A383836313139382D30373036
0A3A3233423A435245440A3A3332413A303030363132555344353434332C39390A3A3333423A5553
44353434332C39390A3A35304B3A4749414E20414E47454C4F20494D504F5254530A4E41504C4553
0A3A3532413A4243495449544D4D3530300A3A3533413A42434954555333330A3A3534413A495256
545553334E0A3A3537413A424E5041465250504752450A3A35393A2F323030343130313030353035
30303030314D30323630360A4B494C4C5920532E412E0A4752454E4F424C450A3A37303A2F524642
2F494E564F494345203535393636310A3A3731413A5348410A2D7DE1F1"

And that's it! Everything below is just the small print.


For reference, here are the commands used to generate the transaction:

var tx = rippleclient.net.remote.transaction()
tx.payment(rippleclient.$scope.address,'r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59', "100")
tx._secret = "{secret}";
tx.tx_json.Sequence = 1;
function ascii2hex(text) { return sjcl.codec.hex.fromBits(sjcl.codec.utf8String.toBits(text)); }
tx.tx_json.Memos = [{Memo:{MemoType: ascii2hex("swift"), MemoData: ascii2hex("{1:F01MIDLGB22AXXX0548034693}{2:I103BKTRUS33XBRDN3}{3:{108:MT103}}{4:\n:20:8861198-0706\n:23B:CRED\n:32A:000612USD5443,99\n:33B:USD5443,99\n:50K:GIAN ANGELO IMPORTS\nNAPLES\n:52A:BCITITMM500\n:53A:BCITUS33\n:54A:IRVTUS3N\n:57A:BNPAFRPPGRE\n:59:/20041010050500001M02606\nKILLY S.A.\nGRENOBLE\n:70:/RFB/INVOICE 559661\n:71A:SHA\n-}")}}]
tx.complete()
tx.sign()

// JSON output
console.log(JSON.stringify(tx.tx_json));

// Hex output
console.log(tx.serialize().to_hex().match(/.{1,80}/g).join('\n'));

Disclaimers:

  • This example transaction makes no sense. The data in the SWIFT message does not match the data in the Ripple transaction.
  • The API for ripple-lib could (and soon will) be improved.
  • I did this using the latest develop version of ripple-lib.
  • The JSON above is bogus - in real life it would actually contain the hex of the MemoType and MemoData, not very readable. In the future, ripple-lib's serializer should accept UTF8 data for binary fields somehow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment