base-x-3.0.5.1.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. (function(){
  9. 'use strict';
  10. this.baseX = function base (ALPHABET) {
  11. if (ALPHABET.length >= 255) throw new TypeError('Alphabet too long')
  12. const BASE_MAP = new Uint8Array(256)
  13. BASE_MAP.fill(255)
  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. if (source.length === 0) return ''
  26. // Skip & count leading zeroes.
  27. let zeroes = 0
  28. let length = 0
  29. let pbegin = 0
  30. const pend = source.length
  31. while (pbegin !== pend && source[pbegin] === 0) {
  32. pbegin++
  33. zeroes++
  34. }
  35. // Allocate enough space in big-endian base58 representation.
  36. const size = ((pend - pbegin) * iFACTOR + 1) >>> 0
  37. const b58 = new Uint8Array(size)
  38. // Process the bytes.
  39. while (pbegin !== pend) {
  40. let carry = source[pbegin]
  41. // Apply "b58 = b58 * 256 + ch".
  42. let i = 0
  43. for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
  44. carry += (256 * b58[it]) >>> 0
  45. b58[it] = (carry % BASE) >>> 0
  46. carry = (carry / BASE) >>> 0
  47. }
  48. if (carry !== 0) throw new Error('Non-zero carry')
  49. length = i
  50. pbegin++
  51. }
  52. // Skip leading zeroes in base58 result.
  53. let it = size - length
  54. while (it !== size && b58[it] === 0) {
  55. it++
  56. }
  57. // Translate the result into a string.
  58. let str = LEADER.repeat(zeroes)
  59. for (; it < size; ++it) str += ALPHABET.charAt(b58[it])
  60. return str
  61. }
  62. function decodeUnsafe (source) {
  63. if (typeof source !== 'string') throw new TypeError('Expected String')
  64. if (source.length === 0) return ''
  65. let psz = 0
  66. // Skip leading spaces.
  67. if (source[psz] === ' ') return
  68. // Skip and count leading '1's.
  69. let zeroes = 0
  70. let length = 0
  71. while (source[psz] === LEADER) {
  72. zeroes++
  73. psz++
  74. }
  75. // Allocate enough space in big-endian base256 representation.
  76. const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.
  77. const b256 = new Uint8Array(size)
  78. // Process the characters.
  79. while (source[psz]) {
  80. // Decode character
  81. let carry = BASE_MAP[source.charCodeAt(psz)]
  82. // Invalid character
  83. if (carry === 255) return
  84. let i = 0
  85. for (let it = size - 1; (carry !== 0 || i < length) && (it !== -1); it--, i++) {
  86. carry += (BASE * b256[it]) >>> 0
  87. b256[it] = (carry % 256) >>> 0
  88. carry = (carry / 256) >>> 0
  89. }
  90. if (carry !== 0) throw new Error('Non-zero carry')
  91. length = i
  92. psz++
  93. }
  94. // Skip trailing spaces.
  95. if (source[psz] === ' ') return
  96. // Skip leading zeroes in b256.
  97. let it = size - length
  98. while (it !== size && b256[it] === 0) {
  99. it++
  100. }
  101. var vch = [];
  102. let j = zeroes
  103. while (it !== size) {
  104. vch[j++] = b256[it++]
  105. }
  106. return vch
  107. }
  108. function decode (string) {
  109. const buffer = decodeUnsafe(string)
  110. if (buffer) return buffer
  111. throw new Error('Non-base' + BASE + ' character')
  112. }
  113. return {
  114. encode: encode,
  115. decodeUnsafe: decodeUnsafe,
  116. decode: decode
  117. }
  118. }
  119. }).call(this);