import { Cbor, CborByte } from "."; import { AddressUtils } from "../address"; import { Multiasset } from "../assets"; import { CborInteger } from "./integer"; import { CborSet } from "./set"; const decode = (value: string, headerLength = 76) => { const address = CborByte.read(value.substring(headerLength)); const utxoValue = value.substring(headerLength + 4 + address.length); const utxoValueType = Cbor.identifyType(utxoValue); if (utxoValueType === "array") { const coin = CborInteger.read(utxoValue.substring(2)); const coinValue = utxoValue.substring(2); const coinLength = Cbor.readNextItemStringLength(coinValue); const multiassetValue = coinValue.substring(coinLength); return { address: AddressUtils.convertFromHexAddressToBech32(address), amount: { coin, multiasset: CborSet.read(multiassetValue), }, }; } else if (utxoValueType === "integer" || utxoValueType === "unsigned") { return { address: AddressUtils.convertFromHexAddressToBech32(address), amount: { coin: CborInteger.read(utxoValue), }, }; } else { throw `Unknown utxo value`; } }; export const CborUtxo = { decode, };