parseAttestationBytes.ts 850 Bytes
Newer Older
Will Cory's avatar
Will Cory committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import { BigNumber } from 'ethers'
import { toUtf8String } from 'ethers/lib/utils.js'

import type { DataTypeOption } from '../types/DataTypeOption'
import type { WagmiBytes } from '../types/WagmiBytes'

export const parseAttestationBytes = (
  attestationBytes: WagmiBytes,
  dataType: DataTypeOption
) => {
  if (dataType === 'bytes') {
    return attestationBytes
  }
  if (dataType === 'number') {
    return BigNumber.from(attestationBytes).toString()
  }
  if (dataType === 'address') {
    return BigNumber.from(attestationBytes).toHexString()
  }
  if (dataType === 'bool') {
    return BigNumber.from(attestationBytes).gt(0) ? 'true' : 'false'
  }
  if (dataType === 'string') {
    return attestationBytes && toUtf8String(attestationBytes)
  }
Will Cory's avatar
Will Cory committed
26
  console.warn(`unrecognized dataType ${dataType satisfies never}`)
Will Cory's avatar
Will Cory committed
27 28
  return attestationBytes
}