text: import { CID } from 'multiformats/cid';
  import { Key } from 'interface-datastore/key';
  import debug from 'debug';
  import length from 'it-length';
  import { base32 } from 'multiformats/bases/base32';
  import * as raw from 'multiformats/codecs/raw';
  import * as mhd from 'multiformats/hashes/digest';
  const log = debug('ipfs:repo:migrator:migration-8');
  function unwrap(blockstore) {
    if (blockstore.child) {
      return unwrap(blockstore.child);
    }
    return blockstore;
  }
  function keyToMultihash(key) {
    try {
      const buf = base32.decode(`b${ key.toString().toLowerCase().slice(1) }`);
      const multihash = CID.decode(buf).multihash.bytes;
      const multihashStr = base32.encode(multihash).slice(1).toUpperCase();
      return new Key(`/${ multihashStr }`, false);
    } catch (err) {
      return key;
    }
  }
  function keyToCid(key) {
    try {
      const buf = base32.decode(`b${ key.toString().toLowerCase().slice(1) }`);
      const digest = mhd.decode(buf);
      const multihash = base32.encode(CID.createV1(raw.code, digest).bytes).slice(1);
      return new Key(`/${ multihash.toUpperCase() }`, false);
    } catch {
      return key;
    }
  }
  async function process(backends, onProgress, keyFunction) {
    const blockstore = backends.blocks;
    await blockstore.open();
    const unwrapped = unwrap(blockstore);
    const blockCount = await length(unwrapped.queryKeys({
      filters: [key => {
          const newKey = keyFunction(key);
          return newKey.toString() !== key.toString();
        }]
    }));
    try {
      let counter = 0;
      for await (const block of unwrapped.query({})) {
        const newKey = keyFunction(block.key);
        if (newKey.toString() !== block.key.toString()) {
          counter += 1;
          log(`Migrating Block from ${ block.key } to ${ newKey }`, await unwrapped.has(block.key));
          await unwrapped.delete(block.key);
          await unwrapped.put(newKey, block.value);
          onProgress(counter / blockCount * 100, `Migrated Block from ${ block.key } to ${ newKey }`);
        }
      }
    } finally {
      await blockstore.close();
    }
  }
  export const migration = {
    version: 8,
    description: 'Transforms key names into base32 encoding and converts Block store to use bare multihashes encoded as base32',
    migrate: (backends, onProgress = () => {
    }) => {
      return process(backends, onProgress, keyToMultihash);
    },
    revert: (backends, onProgress = () => {
    }) => {
      return process(backends, onProgress, keyToCid);
    }
  };