{"version":3,"file":"serializeRecordContent.mjs","sources":["../../../../src/utils/serializers/serializeRecordContent.ts"],"sourcesContent":["import { type Bech32DecodedWithArray, bech32 } from \"@scure/base\";\r\nimport type { Address, ReadonlyUint8Array } from \"@solana/kit\";\r\nimport ipaddr from \"ipaddr.js\";\r\nimport punycode from \"punycode/punycode.js\";\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\nconst EVM_ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;\r\n\r\n/**\r\n * Parameters for serializing record content.\r\n *\r\n * @example\r\n * ```ts\r\n * const params: SerializeRecordContentParams = {\r\n *   content: \"https://example.com\",\r\n *   record: Record.Url,\r\n * };\r\n * ```\r\n */\r\nexport interface SerializeRecordContentParams {\r\n  /** Record content. */\r\n  content: string;\r\n  /** Record type. */\r\n  record: Record;\r\n}\r\n\r\n/**\r\n * Serializes record content according to SNS-IP 1.\r\n *\r\n * `CNAME` and `TXT` content is punycode-encoded before UTF-8 serialization.\r\n *\r\n * @param params Record serialization parameters\r\n * @param params.content Record content to serialize\r\n * @param params.record Record type\r\n * @returns Serialized record content.\r\n * @throws InvalidEvmAddressError, InvalidInjectiveAddressError, InvalidARecordError,\r\n * InvalidAAAARecordError, or InvalidRecordInputError when the record content is invalid or unsupported.\r\n *\r\n * @example\r\n * ```ts\r\n * const content = serializeRecordContent({\r\n *   content: \"https://example.com\",\r\n *   record: Record.Url,\r\n * });\r\n * ```\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 = punycode.encode(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      EVM_ADDRESS_REGEX.test(content),\r\n      new InvalidEvmAddressError(\r\n        \"The record content must be a valid EVM address starting with `0x`\"\r\n      )\r\n    );\r\n    return uint8ArrayFromHex(content.slice(2));\r\n  } else if (record === Record.Injective) {\r\n    let decoded: Bech32DecodedWithArray<string>;\r\n\r\n    try {\r\n      decoded = bech32.decodeToBytes(content);\r\n    } catch {\r\n      throw new InvalidInjectiveAddressError(\r\n        \"The record content must be a valid Injective address\"\r\n      );\r\n    }\r\n\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\r\n    return decoded.bytes;\r\n  } else if (record === Record.A) {\r\n    let array: number[];\r\n\r\n    try {\r\n      array = ipaddr.parse(content).toByteArray();\r\n    } catch {\r\n      throw new InvalidARecordError(\r\n        \"The record content must be a valid IPv4 address\"\r\n      );\r\n    }\r\n    _check(\r\n      array.length === 4,\r\n      new InvalidARecordError(\"The record content must be 4 bytes long\")\r\n    );\r\n\r\n    return new Uint8Array(array);\r\n  } else if (record === Record.AAAA) {\r\n    let array: number[];\r\n\r\n    try {\r\n      array = ipaddr.parse(content).toByteArray();\r\n    } catch {\r\n      throw new InvalidAAAARecordError(\r\n        \"The record content must be a valid IPv6 address\"\r\n      );\r\n    }\r\n    _check(\r\n      array.length === 16,\r\n      new InvalidAAAARecordError(\"The record content must be 16 bytes long\")\r\n    );\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":[],"mappings":";;;;;;;;;;AAkBA,MAAM,iBAAiB,GAAG,qBAAqB;AAoB/C;;;;;;;;;;;;;;;;;;;AAmBG;AACU,MAAA,sBAAsB,GAAG,CAAC,EACrC,OAAO,EACP,MAAM,GACuB,KAAwB;IACrD,MAAM,WAAW,GAAG,oBAAoB,CAAC,GAAG,CAAC,MAAM,CAAC;IACpD,IAAI,WAAW,EAAE;AACf,QAAA,IAAI,MAAM,KAAK,MAAM,CAAC,KAAK,IAAI,MAAM,KAAK,MAAM,CAAC,GAAG,EAAE;AACpD,YAAA,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;;AAEpC,QAAA,OAAO,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC;;AAC3B,SAAA,IAAI,MAAM,KAAK,MAAM,CAAC,GAAG,EAAE;AAChC,QAAA,OAAO,YAAY,CAAC,MAAM,CAAC,OAAkB,CAAC;;AACzC,SAAA,IAAI,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;AAClC,QAAA,MAAM,CACJ,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,EAC/B,IAAI,sBAAsB,CACxB,mEAAmE,CACpE,CACF;QACD,OAAO,iBAAiB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;;AACrC,SAAA,IAAI,MAAM,KAAK,MAAM,CAAC,SAAS,EAAE;AACtC,QAAA,IAAI,OAAuC;AAE3C,QAAA,IAAI;AACF,YAAA,OAAO,GAAG,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC;;AACvC,QAAA,MAAM;AACN,YAAA,MAAM,IAAI,4BAA4B,CACpC,sDAAsD,CACvD;;AAGH,QAAA,MAAM,CACJ,OAAO,CAAC,MAAM,KAAK,KAAK,IAAI,OAAO,CAAC,MAAM,KAAK,EAAE,EACjD,IAA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