Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

hash.test.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. const { coreTypes } = require('../src/types')
  2. const { Hash128, Hash160, Hash256, AccountID, Currency } = coreTypes
  3. const { Buffer } = require('buffer/')
  4. describe('Hash128', function () {
  5. test('has a static width member', function () {
  6. expect(Hash128.width).toBe(16)
  7. })
  8. test('can be unset', function () {
  9. const h1 = Hash128.from('')
  10. expect(h1.toJSON()).toBe('')
  11. })
  12. test('can be compared against another', function () {
  13. const h1 = Hash128.from('100000000000000000000000000000000')
  14. const h2 = Hash128.from('200000000000000000000000000000000')
  15. const h3 = Hash128.from('000000000000000000000000000000003')
  16. expect(h1.lt(h2)).toBe(true)
  17. expect(h3.lt(h2)).toBe(true)
  18. expect(h2.gt(h1)).toBe(true)
  19. expect(h1.gt(h3)).toBe(true)
  20. })
  21. test('throws when constructed from invalid hash length', () => {
  22. expect(() => Hash128.from('1000000000000000000000000000000')).toThrow(
  23. 'Invalid Hash length 15',
  24. )
  25. expect(() => Hash128.from('10000000000000000000000000000000000')).toThrow(
  26. 'Invalid Hash length 17',
  27. )
  28. })
  29. })
  30. describe('Hash160', function () {
  31. test('has a static width member', function () {
  32. expect(Hash160.width).toBe(20)
  33. })
  34. test('inherited by subclasses', function () {
  35. expect(AccountID.width).toBe(20)
  36. expect(Currency.width).toBe(20)
  37. })
  38. test('can be compared against another', function () {
  39. const h1 = Hash160.from('1000000000000000000000000000000000000000')
  40. const h2 = Hash160.from('2000000000000000000000000000000000000000')
  41. const h3 = Hash160.from('0000000000000000000000000000000000000003')
  42. expect(h1.lt(h2)).toBe(true)
  43. expect(h3.lt(h2)).toBe(true)
  44. })
  45. test('throws when constructed from invalid hash length', () => {
  46. expect(() =>
  47. Hash160.from('10000000000000000000000000000000000000'),
  48. ).toThrow('Invalid Hash length 19')
  49. expect(() =>
  50. Hash160.from('100000000000000000000000000000000000000000'),
  51. ).toThrow('Invalid Hash length 21')
  52. })
  53. })
  54. describe('Hash256', function () {
  55. test('has a static width member', function () {
  56. expect(Hash256.width).toBe(32)
  57. })
  58. test('has a ZERO_256 member', function () {
  59. expect(Hash256.ZERO_256.toJSON()).toBe(
  60. '0000000000000000000000000000000000000000000000000000000000000000',
  61. )
  62. })
  63. test('supports getting the nibblet values at given positions', function () {
  64. const h = Hash256.from(
  65. '1359BD0000000000000000000000000000000000000000000000000000000000',
  66. )
  67. expect(h.nibblet(0)).toBe(0x1)
  68. expect(h.nibblet(1)).toBe(0x3)
  69. expect(h.nibblet(2)).toBe(0x5)
  70. expect(h.nibblet(3)).toBe(0x9)
  71. expect(h.nibblet(4)).toBe(0x0b)
  72. expect(h.nibblet(5)).toBe(0xd)
  73. })
  74. })
  75. describe('Currency', function () {
  76. test('Decoding allows dodgy XRP without throwing', function () {
  77. const currencyCode = '0000000000000000000000005852500000000000'
  78. expect(Currency.from(currencyCode).toJSON()).toBe(currencyCode)
  79. })
  80. test('Currency code with lowercase letters decodes to ISO code', () => {
  81. expect(Currency.from('xRp').toJSON()).toBe('xRp')
  82. })
  83. test('Currency codes with symbols decodes to ISO code', () => {
  84. expect(Currency.from('x|p').toJSON()).toBe('x|p')
  85. })
  86. test('Currency code with non-standard symbols decodes to hex', () => {
  87. expect(Currency.from(':::').toJSON()).toBe(
  88. '0000000000000000000000003A3A3A0000000000',
  89. )
  90. })
  91. test('Currency codes can be exclusively standard symbols', () => {
  92. expect(Currency.from('![]').toJSON()).toBe('![]')
  93. })
  94. test('Currency codes with uppercase and 0-9 decode to ISO codes', () => {
  95. expect(Currency.from('X8P').toJSON()).toBe('X8P')
  96. expect(Currency.from('USD').toJSON()).toBe('USD')
  97. })
  98. test('Currency codes with no contiguous zeroes in first 96 type code & reserved bits', function () {
  99. expect(
  100. Currency.from('0000000023410000000000005852520000000000').iso(),
  101. ).toBe(null)
  102. })
  103. test('Currency codes with no contiguous zeroes in last 40 reserved bits', function () {
  104. expect(
  105. Currency.from('0000000000000000000000005852527570656500').iso(),
  106. ).toBe(null)
  107. })
  108. test('can be constructed from a Buffer', function () {
  109. const xrp = new Currency(Buffer.alloc(20))
  110. expect(xrp.iso()).toBe('XRP')
  111. })
  112. test('Can handle non-standard currency codes', () => {
  113. const currency = '015841551A748AD2C1F76FF6ECB0CCCD00000000'
  114. expect(Currency.from(currency).toJSON()).toBe(currency)
  115. })
  116. test('Can handle other non-standard currency codes', () => {
  117. const currency = '0000000000414C6F676F30330000000000000000'
  118. expect(Currency.from(currency).toJSON()).toBe(currency)
  119. })
  120. test('throws on invalid reprs', function () {
  121. expect(() => Currency.from(Buffer.alloc(19))).toThrow()
  122. expect(() => Currency.from(1)).toThrow()
  123. expect(() =>
  124. Currency.from('00000000000000000000000000000000000000m'),
  125. ).toThrow()
  126. })
  127. })