tx-encode-decode.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { encode, decode } = require('../dist')
  2. // Notice: no Amount or Fee
  3. const tx_json = {
  4. Account: 'r9LqNeG6qHxjeUocjvVki2XR35weJ9mZgQ',
  5. // Amount: '1000',
  6. Destination: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
  7. // Fee: '10',
  8. // JavaScript converts operands to 32-bit signed ints after doing bitwise
  9. // operations. We need to convert it back to an unsigned int with >>> 0.
  10. Flags: (1 << 31) >>> 0, // tfFullyCanonicalSig
  11. Sequence: 1,
  12. TransactionType: 'Payment',
  13. // TxnSignature,
  14. // Signature,
  15. // SigningPubKey
  16. }
  17. describe('encoding and decoding tx_json', function () {
  18. test('can encode tx_json without Amount or Fee', function () {
  19. const encoded = encode(tx_json)
  20. const decoded = decode(encoded)
  21. expect(tx_json).toEqual(decoded)
  22. })
  23. test('can encode tx_json with Amount and Fee', function () {
  24. const my_tx = Object.assign({}, tx_json, {
  25. Amount: '1000',
  26. Fee: '10',
  27. })
  28. const encoded = encode(my_tx)
  29. const decoded = decode(encoded)
  30. expect(my_tx).toEqual(decoded)
  31. })
  32. test('can encode tx_json with TicketCount', function () {
  33. const my_tx = Object.assign({}, tx_json, {
  34. TicketCount: 2,
  35. })
  36. const encoded = encode(my_tx)
  37. const decoded = decode(encoded)
  38. expect(my_tx).toEqual(decoded)
  39. })
  40. test('can encode tx_json with TicketSequence', function () {
  41. const my_tx = Object.assign({}, tx_json, {
  42. Sequence: 0,
  43. TicketSequence: 2,
  44. })
  45. const encoded = encode(my_tx)
  46. const decoded = decode(encoded)
  47. expect(my_tx).toEqual(decoded)
  48. })
  49. test('can decode a transaction with an issued currency that evaluates to XRP', function () {
  50. // Encoding is done prior, because this is disallowed during encoding with client libraries to avoid scam XRP tokens.
  51. const expectedTx = {
  52. TransactionType: 'TrustSet',
  53. Flags: 0,
  54. Sequence: 19,
  55. LimitAmount: {
  56. value: '200',
  57. currency: '0000000000000000000000005852500000000000',
  58. issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
  59. },
  60. Fee: '10',
  61. SigningPubKey:
  62. '023076CBB7A61837F1A23D4A3DD7CE810B694992EB0959AB9D6F4BB6FED6F8CC26',
  63. TxnSignature:
  64. '304502202D0CD77D8E765E3783C309CD663723B18406B7950A348A6F301492916A990FC70221008A76D586111205304F10ADEFDFDDAF804EF202D8CD1E492DC6E1AA8030EA1844',
  65. Account: 'rPtfQWdcdhuL9eNeNv5YfmekSX3K7vJHbG',
  66. }
  67. const encoded = encode(expectedTx)
  68. const decoded = decode(encoded)
  69. expect(expectedTx).toEqual(decoded)
  70. })
  71. test('throws when Amount is invalid', function () {
  72. const my_tx = Object.assign({}, tx_json, {
  73. Amount: '1000.001',
  74. Fee: '10',
  75. })
  76. expect(() => {
  77. encode(my_tx)
  78. }).toThrow()
  79. })
  80. test('throws when Fee is invalid', function () {
  81. const my_tx = Object.assign({}, tx_json, {
  82. Amount: '1000',
  83. Fee: '10.123',
  84. })
  85. expect(() => {
  86. encode(my_tx)
  87. }).toThrow()
  88. })
  89. test('throws when Amount and Fee are invalid', function () {
  90. const my_tx = Object.assign({}, tx_json, {
  91. Amount: '1000.789',
  92. Fee: '10.123',
  93. })
  94. expect(() => {
  95. encode(my_tx)
  96. }).toThrow()
  97. })
  98. test('throws when Amount is a number instead of a string-encoded integer', function () {
  99. const my_tx = Object.assign({}, tx_json, {
  100. Amount: 1000.789,
  101. })
  102. expect(() => {
  103. encode(my_tx)
  104. }).toThrow()
  105. })
  106. test('throws when Fee is a number instead of a string-encoded integer', function () {
  107. const my_tx = Object.assign({}, tx_json, {
  108. Amount: 1234.56,
  109. })
  110. expect(() => {
  111. encode(my_tx)
  112. }).toThrow()
  113. })
  114. })