base-x-5.0.1.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. // base-x encoding / decoding
  3. // Copyright (c) 2018 base-x contributors
  4. // Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
  5. // Distributed under the MIT software license, see the accompanying
  6. // file LICENSE or http://www.opensource.org/licenses/mit-license.php.
  7. (function(){
  8. this.baseX = function base (ALPHABET) {
  9. if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
  10. const BASE_MAP = new Uint8Array(256)
  11. for (let j = 0; j < BASE_MAP.length; j++) {
  12. BASE_MAP[j] = 255
  13. }
  14. for (let i = 0; i < ALPHABET.length; i++) {
  15. const x = ALPHABET.charAt(i)
  16. const xc = x.charCodeAt(0)
  17. if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
  18. BASE_MAP[xc] = i
  19. }
  20. const BASE = ALPHABET.length
  21. const LEADER = ALPHABET.charAt(0)
  22. const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up
  23. const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up
  24. function encode (source) {
  25. // eslint-disable-next-line no-empty
  26. if (source instanceof Uint8Array) { } else if (ArrayBuffer.isView(source)) {
  27. source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
  28. } else if (Array.isArray(source)) {
  29. source = Uint8Array.from(source)
  30. }
  31. if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
  32. if (source.length === 0) { return '' }
  33. // Skip & count leading zeroes.
  34. let zeroes = 0
  35. let length = 0
  36. let pbegin = 0
  37. const pend = source.length
  38. while (pbegin !== pend && source[pbegin] === 0) {
  39. pbegin++
  40. zeroes++
  41. }
  42. // Allocate enough space in big-endian base58 representation.
  43. const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
  44. const b58 = new Uint8Array(size)
  45. // Process the bytes.
  46. while (pbegin !== pend) {
  47. let carry = source[pbegin]
  48. // Apply "b58 = b58 * 256 + ch".
  49. let i = 0
  50. for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
  51. carry += (256 * b58[it1]) >>> 0
  52. b58[it1] = (carry % BASE) >>> 0
  53. carry = (carry / BASE) >>> 0
  54. }
  55. if (carry !== 0) { throw new Error('Non-zero carry') }
  56. length = i
  57. pbegin++
  58. }
  59. // Skip leading zeroes in base58 result.
  60. let it2 = size - length
  61. while (it2 !== size && b58[it2] === 0) {
  62. it2++
  63. }
  64. // Translate the result into a string.
  65. let str = LEADER.repeat(zeroes)
  66. for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }
  67. return str
  68. }
  69. function decodeUnsafe (source) {
  70. if (typeof source !== 'string') { throw new TypeError('Expected String') }
  71. if (source.length === 0) { return new Uint8Array() }
  72. let psz = 0
  73. // Skip and count leading '1's.
  74. let zeroes = 0
  75. let length = 0
  76. while (source[psz] === LEADER) {
  77. zeroes++
  78. psz++
  79. }
  80. // Allocate enough space in big-endian base256 representation.
  81. const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
  82. const b256 = new Uint8Array(size)
  83. // Process the characters.
  84. while (psz < source.length) {
  85. // Find code of next character
  86. const charCode = source.charCodeAt(psz)
  87. // Base map can not be indexed using char code
  88. if (charCode > 255) { return }
  89. // Decode character
  90. let carry = BASE_MAP[charCode]
  91. // Invalid character
  92. if (carry === 255) { return }
  93. let i = 0
  94. for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
  95. carry += (BASE * b256[it3]) >>> 0
  96. b256[it3] = (carry % 256) >>> 0
  97. carry = (carry / 256) >>> 0
  98. }
  99. if (carry !== 0) { throw new Error('Non-zero carry') }
  100. length = i
  101. psz++
  102. }
  103. // Skip leading zeroes in b256.
  104. let it4 = size - length
  105. while (it4 !== size && b256[it4] === 0) {
  106. it4++
  107. }
  108. const vch = new Uint8Array(zeroes + (size - it4))
  109. let j = zeroes
  110. while (it4 !== size) {
  111. vch[j++] = b256[it4++]
  112. }
  113. return vch
  114. }
  115. function decode (string) {
  116. const buffer = decodeUnsafe(string)
  117. if (buffer) { return buffer }
  118. throw new Error('Non-base' + BASE + ' character')
  119. }
  120. return {
  121. encode,
  122. decodeUnsafe,
  123. decode
  124. }
  125. }
  126. }).call(this);