UNPKG

lotus-sdk

Version:

Central repository for several classes of tools for integrating with, and building for, the Lotusia ecosystem

59 lines (58 loc) 1.4 kB
import { Preconditions } from './preconditions.js'; const CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l'; const CHARSET_INVERSE_INDEX = { 'q': 0, 'p': 1, 'z': 2, 'r': 3, 'y': 4, '9': 5, 'x': 6, '8': 7, 'g': 8, 'f': 9, '2': 10, 't': 11, 'v': 12, 'd': 13, 'w': 14, '0': 15, 's': 16, '3': 17, 'j': 18, 'n': 19, '5': 20, '4': 21, 'k': 22, 'h': 23, 'c': 24, 'e': 25, '6': 26, 'm': 27, 'u': 28, 'a': 29, '7': 30, 'l': 31, }; export class Base32 { static encode(data) { Preconditions.checkArgument(Array.isArray(data), 'data', 'Must be Array'); let base32 = ''; for (let i = 0; i < data.length; i++) { const value = data[i]; Preconditions.checkArgument(0 <= value && value < 32, 'value', `value ${value}`); base32 += CHARSET[value]; } return base32; } static decode(base32) { Preconditions.checkArgument(typeof base32 === 'string', 'base32', 'Must be base32-encoded string'); const data = []; for (let i = 0; i < base32.length; i++) { const value = base32[i]; Preconditions.checkArgument(value in CHARSET_INVERSE_INDEX, 'value', `value ${value}`); data.push(CHARSET_INVERSE_INDEX[value]); } return data; } }