base-x-3.0.5.1.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // base-x encoding / decoding
  2. // based on https://github.com/cryptocoinjs/base-x 3.0.5
  3. // modification: removed Buffer dependency and node.modules entry
  4. // Copyright (c) 2018 base-x contributors
  5. // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
  6. // Distributed under the MIT software license, see the accompanying
  7. // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
  8. var baseX = function base (ALPHABET) {
  9. if (ALPHABET.length >= 255) throw new TypeError('Alphabet too long')
  10. const BASE_MAP = new Uint8Array(256)
  11. BASE_MAP.fill(255)
  12. for (let i = 0; i < ALPHABET.length; i++) {
  13. const x = ALPHABET.charAt(i)
  14. const xc = x.charCodeAt(0)
  15. if (BASE_MAP[xc] !== 255) throw new TypeError(x + ' is ambiguous')
  16. BASE_MAP[xc] = i
  17. }
  18. const BASE = ALPHABET.length
  19. const LEADER = ALPHABET.charAt(0)
  20. const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
  21. const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
  22. function encode (source) {
  23. if (source.length === 0) return ''
  24. // Skip & count leading zeroes.
  25. let zeroes = 0
  26. let length = 0
  27. let pbegin = 0
  28. const pend = source.length
  29. while (pbegin !== pend && source[pbegin] === 0) {
  30. pbegin++
  31. zeroes++
  32. }
  33. // Allocate enough space in big-endian base58 representation.
  34. const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
  35. const b58 = new Uint8Array(size)
  36. // Process the bytes.
  37. while (pbegin !== pend) {
  38. let carry = source[pbegin]
  39. // Apply "b58 = b58 * 256 + ch".
  40. let i = 0
  41. for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
  42. carry += (256 * b58[it]) >>> 0
  43. b58[it] = (carry % BASE) >>> 0
  44. carry = (carry / BASE) >>> 0
  45. }
  46. if (carry !== 0) throw new Error('Non-zero carry')
  47. length = i
  48. pbegin++
  49. }
  50. // Skip leading zeroes in base58 result.
  51. let it = size - length
  52. while (it !== size && b58[it] === 0) {
  53. it++
  54. }
  55. // Translate the result into a string.
  56. let str = LEADER.repeat(zeroes)
  57. for (; it < size; ++it) str += ALPHABET.charAt(b58[it])
  58. return str
  59. }
  60. function decodeUnsafe (source) {
  61. if (typeof source !== 'string') throw new TypeError('Expected String')
  62. if (source.length === 0) return ''
  63. let psz = 0
  64. // Skip leading spaces.
  65. if (source[psz] === ' ') return
  66. // Skip and count leading '1's.
  67. let zeroes = 0
  68. let length = 0
  69. while (source[psz] === LEADER) {
  70. zeroes++
  71. psz++
  72. }
  73. // Allocate enough space in big-endian base256 representation.
  74. const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
  75. const b256 = new Uint8Array(size)
  76. // Process the characters.
  77. while (source[psz]) {
  78. // Decode character
  79. let carry = BASE_MAP[source.charCodeAt(psz)]
  80. // Invalid character
  81. if (carry === 255) return
  82. let i = 0
  83. for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
  84. carry += (BASE * b256[it]) >>> 0
  85. b256[it] = (carry % 256) >>> 0
  86. carry = (carry / 256) >>> 0
  87. }
  88. if (carry !== 0) throw new Error('Non-zero carry')
  89. length = i
  90. psz++
  91. }
  92. // Skip trailing spaces.
  93. if (source[psz] === ' ') return
  94. // Skip leading zeroes in b256.
  95. let it = size - length
  96. while (it !== size && b256[it] === 0) {
  97. it++
  98. }
  99. var vch = [];
  100. let j = zeroes
  101. while (it !== size) {
  102. vch[j++] = b256[it++]
  103. }
  104. return vch
  105. }
  106. function decode (string) {
  107. const buffer = decodeUnsafe(string)
  108. if (buffer) return buffer
  109. throw new Error('Non-base' + BASE + ' character')
  110. }
  111. return {
  112. encode: encode,
  113. decodeUnsafe: decodeUnsafe,
  114. decode: decode
  115. }
  116. }