INNER CODE UNIT · TypeScript

key

blueshift-gg/doppler · packages/core/index.ts:100

function key(text: unknown, what: string): Uint8Array {
  if (typeof text !== 'string') throw new TypeError(`${what}: a key is 32 bytes in base58`);
  let zeros = 0;
  while (zeros < text.length && text[zeros] === '1') zeros++;

  let n = 0n;
  for (const c of text.slice(zeros)) {
    const digit = ALPHABET.indexOf(c);
    if (digit < 0) throw new TypeError(`${what}: a key is 32 bytes in base58`);
    n = n * 58n + BigInt(digit);
  }
  const bytes = new Uint8Array(32);
  let valueBytes = 0;
  for (let i = 31; n > 0n; i--, n >>= 8n) {
    bytes[i] = Number(n & 0xffn);
    valueBytes++;
  }
  if (zeros + valueBytes !== 32) throw new TypeError(`${what}: a key is 32 bytes in base58`);

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…