{"version":3,"file":"serializeRecordContent.cjs","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":["record","UTF8_ENCODED_RECORDS","Record","utf8Codec","addressCodec","EVM_RECORDS","_check","InvalidEvmAddressError","uint8ArrayFromHex","bech32","InvalidInjectiveAddressError","InvalidARecordError","InvalidAAAARecordError","InvalidRecordInputError"],"mappings":";;;;;;;;;;;;;;AAkBA,MAAM,iBAAiB,GAAG,qBAAqB;AAoB/C;;;;;;;;;;;;;;;;;;;AAmBG;AACU,MAAA,sBAAsB,GAAG,CAAC,EACrC,OAAO,UACPA,QAAM,GACuB,KAAwB;IACrD,MAAM,WAAW,GAAGC,4BAAoB,CAAC,GAAG,CAACD,QAAM,CAAC;IACpD,IAAI,WAAW,EAAE;AACf,QAAA,IAAIA,QAAM,KAAKE,aAAM,CAAC,KAAK,IAAIF,QAAM,KAAKE,aAAM,CAAC,GAAG,EAAE;AACpD,YAAA,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC;;AAEpC,QAAA,OAAOC,gBAAS,CAAC,MAAM,CAAC,OAAO,CAAC;;AAC3B,SAAA,IAAIH,QAAM,KAAKE,aAAM,CAAC,GAAG,EAAE;AAChC,QAAA,OAAOE,mBAAY,CAAC,MAAM,CAAC,OAAkB,CAAC;;AACzC,SAAA,IAAIC,mBAAW,CAAC,GAAG,CAACL,QAAM,CAAC,EAAE;AAClC,QAAAM,YAAM,CACJ,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,EAC/B,IAAIC,6BAAsB,CACxB,mEAAmE,CACpE,CACF;QACD,OAAOC,mCAAiB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;;AACrC,SAAA,IAAIR,QAAM,KAAKE,aAAM,CAAC,SAAS,EAAE;AACtC,QAAA,IAAI,OAAuC;AAE3C,QAAA,IAAI;AACF,YAAA,OAAO,GAAGO,WAAM,CAAC,aAAa,CAAC,OAAO,CA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