hash.test.js 3.6 KB

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