x-address.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const { encode, decode } = require('./../dist/index')
  2. const fixtures = require('./fixtures/x-codec-fixtures.json')
  3. let json_x1 = {
  4. OwnerCount: 0,
  5. Account: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
  6. PreviousTxnLgrSeq: 7,
  7. LedgerEntryType: 'AccountRoot',
  8. PreviousTxnID:
  9. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  10. Flags: 0,
  11. Sequence: 1,
  12. Balance: '10000000000',
  13. }
  14. let json_r1 = {
  15. OwnerCount: 0,
  16. Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  17. PreviousTxnLgrSeq: 7,
  18. LedgerEntryType: 'AccountRoot',
  19. PreviousTxnID:
  20. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  21. Flags: 0,
  22. Sequence: 1,
  23. Balance: '10000000000',
  24. SourceTag: 12345,
  25. }
  26. let json_null_x = {
  27. OwnerCount: 0,
  28. Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  29. Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  30. Issuer: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P4GETfNyyXGaoqBj71',
  31. PreviousTxnLgrSeq: 7,
  32. LedgerEntryType: 'AccountRoot',
  33. PreviousTxnID:
  34. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  35. Flags: 0,
  36. Sequence: 1,
  37. Balance: '10000000000',
  38. }
  39. let json_invalid_x = {
  40. OwnerCount: 0,
  41. Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  42. Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  43. Issuer: 'XVXdn5wEVm5g4UhEHWDPqjvdeH361P4GETfNyyXGaoqBj71',
  44. PreviousTxnLgrSeq: 7,
  45. LedgerEntryType: 'AccountRoot',
  46. PreviousTxnID:
  47. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  48. Flags: 0,
  49. Sequence: 1,
  50. Balance: '10000000000',
  51. }
  52. let json_null_r = {
  53. OwnerCount: 0,
  54. Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  55. Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  56. Issuer: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  57. PreviousTxnLgrSeq: 7,
  58. LedgerEntryType: 'AccountRoot',
  59. PreviousTxnID:
  60. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  61. Flags: 0,
  62. Sequence: 1,
  63. Balance: '10000000000',
  64. }
  65. let invalid_json_issuer_tagged = {
  66. OwnerCount: 0,
  67. Account: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  68. Destination: 'rLs1MzkFWCxTbuAHgjeTZK4fcCDDnf2KRv',
  69. Issuer: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
  70. PreviousTxnLgrSeq: 7,
  71. LedgerEntryType: 'AccountRoot',
  72. PreviousTxnID:
  73. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  74. Flags: 0,
  75. Sequence: 1,
  76. Balance: '10000000000',
  77. }
  78. let invalid_json_x_and_tagged = {
  79. OwnerCount: 0,
  80. Account: 'XVXdn5wEVm5G4UhEHWDPqjvdeH361P7BsapL4m2D2XnPSwT',
  81. PreviousTxnLgrSeq: 7,
  82. LedgerEntryType: 'AccountRoot',
  83. PreviousTxnID:
  84. 'DF530FB14C5304852F20080B0A8EEF3A6BDD044F41F4EBBD68B8B321145FE4FF',
  85. Flags: 0,
  86. Sequence: 1,
  87. Balance: '10000000000',
  88. SourceTag: 12345,
  89. }
  90. let json_issued_x = {
  91. TakerPays: {
  92. currency: 'USD',
  93. issuer: 'X7WZKEeNVS2p9Tire9DtNFkzWBZbFtJHWxDjN9fCrBGqVA4',
  94. value: '7072.8',
  95. },
  96. }
  97. let json_issued_r = {
  98. TakerPays: {
  99. currency: 'USD',
  100. issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
  101. value: '7072.8',
  102. },
  103. }
  104. let json_issued_with_tag = {
  105. TakerPays: {
  106. currency: 'USD',
  107. issuer: 'X7WZKEeNVS2p9Tire9DtNFkzWBZbFtSiS2eDBib7svZXuc2',
  108. value: '7072.8',
  109. },
  110. }
  111. describe('X-Address Account is equivalent to a classic address w/ SourceTag', () => {
  112. let encoded_x = encode(json_x1)
  113. let encoded_r = encode(json_r1)
  114. test('Can encode with x-Address', () => {
  115. expect(encoded_x).toEqual(encoded_r)
  116. })
  117. test('decoded X-address is object w/ source and tag', () => {
  118. let decoded_x = decode(encoded_x)
  119. expect(decoded_x).toEqual(json_r1)
  120. })
  121. test('Encoding issuer X-Address w/ undefined destination tag', () => {
  122. expect(encode(json_null_x)).toEqual(encode(json_null_r))
  123. })
  124. test('Throws when X-Address is invalid', () => {
  125. expect(() => encode(json_invalid_x)).toThrow('checksum_invalid')
  126. })
  127. test('Encodes issued currency w/ x-address', () => {
  128. expect(encode(json_issued_x)).toEqual(encode(json_issued_r))
  129. })
  130. })
  131. describe('Invalid X-Address behavior', () => {
  132. test('X-Address with tag throws value for invalid field', () => {
  133. expect(() => encode(invalid_json_issuer_tagged)).toThrow(
  134. new Error('Issuer cannot have an associated tag'),
  135. )
  136. })
  137. test('Throws when Account has both X-Addr and Destination Tag', () => {
  138. expect(() => encode(invalid_json_x_and_tagged)).toThrow(
  139. new Error('Cannot have Account X-Address and SourceTag'),
  140. )
  141. })
  142. test('Throws when issued currency has tag', () => {
  143. expect(() => encode(json_issued_with_tag)).toThrow(
  144. 'Only allowed to have tag on Account or Destination',
  145. )
  146. })
  147. })
  148. describe('ripple-binary-codec x-address test', function () {
  149. function makeSuite(name, entries) {
  150. describe(name, function () {
  151. entries.forEach((t, testN) => {
  152. test(`${name}[${testN}] encodes X-address json equivalent to classic address json`, () => {
  153. expect(encode(t.rjson)).toEqual(encode(t.xjson))
  154. })
  155. test(`${name}[${testN}] decodes X-address json equivalent to classic address json`, () => {
  156. expect(decode(encode(t.xjson))).toEqual(t.rjson)
  157. })
  158. })
  159. })
  160. }
  161. makeSuite('transactions', fixtures.transactions)
  162. })