{"version":3,"file":"serializeRecordContent.cjs","sources":["../../../../src/utils/serializers/serializeRecordContent.ts"],"sourcesContent":["import { bech32 } from \"@scure/base\";\r\nimport { Address, ReadonlyUint8Array } from \"@solana/kit\";\r\nimport { parse as parseIp } from \"ipaddr.js\";\r\nimport { encode as encodePunycode } from \"punycode/\";\r\n\r\nimport { addressCodec, utf8Codec } from \"../../codecs\";\r\nimport { EVM_RECORDS, UTF8_ENCODED_RECORDS } from \"../../constants/records\";\r\nimport {\r\n  InvalidAAAARecordError,\r\n  InvalidARecordError,\r\n  InvalidEvmAddressError,\r\n  InvalidInjectiveAddressError,\r\n  InvalidRecordInputError,\r\n} from \"../../errors\";\r\nimport { Record } from \"../../types/record\";\r\nimport { _check } from \"../check\";\r\nimport { uint8ArrayFromHex } from \"../uint8Array/uint8ArrayFromHex\";\r\n\r\ninterface SerializeRecordContentParams {\r\n  content: string;\r\n  record: Record;\r\n}\r\n\r\n/**\r\n * Serializes record content based on its type.\r\n *\r\n * @param params - An object containing the following properties:\r\n *   - `content`: The record content to serialize.\r\n *   - `record`: The type of record for which the content is being serialized.\r\n * @returns The serialized content as a ReadonlyUint8Array.\r\n */\r\nexport const serializeRecordContent = ({\r\n  content,\r\n  record,\r\n}: SerializeRecordContentParams): ReadonlyUint8Array => {\r\n  const utf8Encoded = UTF8_ENCODED_RECORDS.has(record);\r\n  if (utf8Encoded) {\r\n    if (record === Record.CNAME || record === Record.TXT) {\r\n      content = encodePunycode(content);\r\n    }\r\n    return utf8Codec.encode(content);\r\n  } else if (record === Record.SOL) {\r\n    return addressCodec.encode(content as Address);\r\n  } else if (EVM_RECORDS.has(record)) {\r\n    _check(\r\n      content.slice(0, 2) === \"0x\",\r\n      new InvalidEvmAddressError(\"The record content must start with `0x`\")\r\n    );\r\n    _check(\r\n      content.length === 42,\r\n      new InvalidEvmAddressError(\r\n        \"The record content must be 42 characters long\"\r\n      )\r\n    );\r\n    return uint8ArrayFromHex(content.slice(2));\r\n  } else if (record === Record.Injective) {\r\n    const decoded = bech32.decodeToBytes(content);\r\n    _check(\r\n      decoded.prefix === \"inj\" && content.length === 42,\r\n      new InvalidInjectiveAddressError(\r\n        \"The record content must start with `inj`\"\r\n      )\r\n    );\r\n    _check(\r\n      decoded.bytes.length === 20,\r\n      new InvalidInjectiveAddressError(\"The record data must be 20 bytes long\")\r\n    );\r\n    return decoded.bytes;\r\n  } else if (record === Record.A) {\r\n    const array = parseIp(content).toByteArray();\r\n    _check(\r\n      array.length === 4,\r\n      new InvalidARecordError(\"The record content must be 4 bytes long\")\r\n    );\r\n    return new Uint8Array(array);\r\n  } else if (record === Record.AAAA) {\r\n    const array = parseIp(content).toByteArray();\r\n    _check(\r\n      array.length === 16,\r\n      new InvalidAAAARecordError(\"The record content must be 16 bytes long\")\r\n    );\r\n    return new Uint8Array(array);\r\n  } else {\r\n    throw new InvalidRecordInputError(\"The record content is malformed\");\r\n  }\r\n};\r\n"],"names":["content","record","UTF8_ENCODED_RECORDS","has","Record","CNAME","TXT","encodePunycode","encode","utf8Codec","SOL","addressCodec","EVM_RECORDS","_check","slice","InvalidEvmAddressError","length","uint8ArrayFromHex","Injective","decoded","bech32","decodeToBytes","prefix","InvalidInjectiveAddressError","bytes","A","array","parseIp","toByteArray","InvalidARecordError","Uint8Array","AAAA","InvalidAAAARecordError","InvalidRecordInputError"],"mappings":"maA+BsC,EACpCA,UACAC,OAAAA,MAGA,GADoBC,EAAAA,qBAAqBC,IAAIF,GAK3C,OAHIA,IAAWG,EAAAA,OAAOC,OAASJ,IAAWG,EAAAA,OAAOE,MAC/CN,EAAUO,EAAcC,OAACR,IAEpBS,EAASA,UAACD,OAAOR,GACnB,GAAIC,IAAWG,EAAMA,OAACM,IAC3B,OAAOC,EAAYA,aAACH,OAAOR,GACtB,GAAIY,EAAWA,YAACT,IAAIF,GAWzB,OAVAY,SAC0B,OAAxBb,EAAQc,MAAM,EAAG,GACjB,IAAIC,yBAAuB,4CAE7BF,EAAMA,OACe,KAAnBb,EAAQgB,OACR,IAAID,EAAAA,uBACF,kDAGGE,oBAAkBjB,EAAQc,MAAM,IAClC,GAAIb,IAAWG,EAAMA,OAACc,UAAW,CACtC,MAAMC,EAAUC,EAAAA,OAAOC,cAAcrB,GAWrC,OAVAa,SACqB,QAAnBM,EAAQG,QAAuC,KAAnBtB,EAAQgB,OACpC,IAAIO,+BACF,6CAGJV,EAAMA,OACqB,KAAzBM,EAAQK,MAAMR,OACd,IAAIO,EAAAA,6BAA6B,0CAE5BJ,EAAQK,MACV,GAAIvB,IAAWG,EAAMA,OAACqB,EAAG,CAC9B,MAAMC,EAAQC,EAAAA,cAAAA,MAAQ3B,GAAS4B,cAK/B,OAJAf,EAAMA,OACa,IAAjBa,EAAMV,OACN,IAAIa,EAAAA,oBAAoB,4CAEnB,IAAIC,WAAWJ,GACjB,GAAIzB,IAAWG,EAAMA,OAAC2B,KAAM,CACjC,MAAML,EAAQC,EAAAA,cAAAA,MAAQ3B,GAAS4B,cAK/B,OAJAf,EAAMA,OACa,KAAjBa,EAAMV,OACN,IAAIgB,EAAAA,uBAAuB,6CAEtB,IAAIF,WAAWJ,GAEtB,MAAM,IAAIO,EAAuBA,wBAAC"}