import { fromHex, toHex } from '@pezkuwi/papi-utils';
import { Bytes, createCodec, createDecoder } from 'scale-ts';
import '@noble/hashes/blake2.js';
import '@noble/hashes/blake3.js';
import { Keccak256 } from '../../hashes/keccak.mjs';
import { textEncoder } from './fixed-str.mjs';

const getFormattedAddress = (hexAddress) => {
  const nonChecksum = hexAddress.slice(2);
  const hashedAddress = toHex(Keccak256(textEncoder.encode(nonChecksum))).slice(
    2
  );
  const result = new Array(40);
  for (let i = 0; i < 40; i++) {
    const checksumVal = parseInt(hashedAddress[i], 16);
    const char = nonChecksum[i];
    result[i] = checksumVal > 7 ? char.toUpperCase() : char;
  }
  return `0x${result.join("")}`;
};
const bytes20Dec = Bytes(20)[1];
const ethAccount = createCodec(
  (input) => {
    const bytes = fromHex(input);
    if (bytes.length !== 20)
      throw new Error(`Invalid length found on EthAddress(${input})`);
    const hexAddress = toHex(bytes);
    if (input === hexAddress || input === hexAddress.toUpperCase()) return bytes;
    if (getFormattedAddress(hexAddress) !== input)
      throw new Error(`Invalid checksum found on EthAddress(${input})`);
    return bytes;
  },
  createDecoder((bytes) => getFormattedAddress(toHex(bytes20Dec(bytes))))
);

export { ethAccount };
//# sourceMappingURL=ethAccount.mjs.map
