base58check.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { typedView } from '@exodus/bytes/array.js'
  2. import { toBase58, fromBase58 } from '@exodus/bytes/base58.js'
  3. import { assertU8 } from './_utils.js'
  4. const E_CHECKSUM = 'Invalid checksum'
  5. // checksum length is 4, i.e. only the first 4 bytes of the hash are used
  6. function encodeWithChecksum(arr, checksum) {
  7. // arr type in already validated in input
  8. const res = new Uint8Array(arr.length + 4)
  9. res.set(arr, 0)
  10. res.set(checksum.subarray(0, 4), arr.length)
  11. return toBase58(res)
  12. }
  13. function decodeWithChecksum(str) {
  14. const arr = fromBase58(str) // checks input
  15. const payloadSize = arr.length - 4
  16. if (payloadSize < 0) throw new Error(E_CHECKSUM)
  17. return [arr.subarray(0, payloadSize), arr.subarray(payloadSize)]
  18. }
  19. function assertChecksum(c, r) {
  20. if ((c[0] ^ r[0]) | (c[1] ^ r[1]) | (c[2] ^ r[2]) | (c[3] ^ r[3])) throw new Error(E_CHECKSUM)
  21. }
  22. export const makeBase58check = (hashAlgo, hashAlgoSync) => {
  23. const apis = {
  24. async encode(arr) {
  25. assertU8(arr)
  26. return encodeWithChecksum(arr, await hashAlgo(arr))
  27. },
  28. async decode(str, format = 'uint8') {
  29. const [payload, checksum] = decodeWithChecksum(str)
  30. assertChecksum(checksum, await hashAlgo(payload))
  31. return typedView(payload, format)
  32. },
  33. }
  34. if (!hashAlgoSync) return apis
  35. return {
  36. ...apis,
  37. encodeSync(arr) {
  38. assertU8(arr)
  39. return encodeWithChecksum(arr, hashAlgoSync(arr))
  40. },
  41. decodeSync(str, format = 'uint8') {
  42. const [payload, checksum] = decodeWithChecksum(str)
  43. assertChecksum(checksum, hashAlgoSync(payload))
  44. return typedView(payload, format)
  45. },
  46. }
  47. }