{"version":3,"file":"getDomainAddress.cjs","sources":["../../../src/domain/getDomainAddress.ts"],"sourcesContent":["import {\r\n  CENTRAL_STATE_DOMAIN_RECORDS,\r\n  ROOT_DOMAIN_ADDRESS,\r\n} from \"../constants/addresses\";\r\nimport { InvalidInputError } from \"../errors\";\r\nimport { RecordVersion } from \"../types/record\";\r\nimport { deriveAddress } from \"../utils/deriveAddress\";\r\n\r\ninterface GetDomainAddressParams {\r\n  domain: string;\r\n  record?: RecordVersion;\r\n}\r\n\r\n/**\r\n * Derives the address of a domain, a subdomain, or a record.\r\n *\r\n * @param params - An object containing the following properties:\r\n *   - `domain`: The (sub)domain to process, with or without the .sol suffix.\r\n *   - `record`: (Optional) The record version. Only provide if the domain being resolved is a record.\r\n * @returns A promise that resolves to an object containing the derived address and additional metadata.\r\n */\r\nexport const getDomainAddress = async ({\r\n  domain,\r\n  record,\r\n}: GetDomainAddressParams) => {\r\n  if (domain.endsWith(\".sol\")) {\r\n    domain = domain.slice(0, -4);\r\n  }\r\n\r\n  const recordClass =\r\n    record === RecordVersion.V2 ? CENTRAL_STATE_DOMAIN_RECORDS : undefined;\r\n  const recordPrefix =\r\n    {\r\n      [RecordVersion.V2]: \"\\x02\",\r\n      [RecordVersion.V1]: \"\\x01\",\r\n    }[record as RecordVersion] || \"\\x00\";\r\n  const splitted = domain.split(\".\");\r\n\r\n  if (splitted.length === 2) {\r\n    const parentAddress = await deriveAddress(splitted[1], ROOT_DOMAIN_ADDRESS);\r\n    const domainAddress = await deriveAddress(\r\n      recordPrefix + splitted[0],\r\n      parentAddress,\r\n      recordClass\r\n    );\r\n\r\n    return { domainAddress, parentAddress, isSub: true };\r\n  } else if (splitted.length === 3 && !!record) {\r\n    // Parent domain\r\n    const parentAddress = await deriveAddress(splitted[2], ROOT_DOMAIN_ADDRESS);\r\n\r\n    // Sub domain\r\n    const subAddress = await deriveAddress(\"\\0\" + splitted[1], parentAddress);\r\n\r\n    // Sub record\r\n    const domainAddress = await deriveAddress(\r\n      recordPrefix + splitted[0],\r\n      subAddress,\r\n      recordClass\r\n    );\r\n\r\n    return { domainAddress, parentAddress, isSub: true, isSubRecord: true };\r\n  } else if (splitted.length >= 3) {\r\n    throw new InvalidInputError(\"The domain is malformed\");\r\n  }\r\n\r\n  const domainAddress = await deriveAddress(domain, ROOT_DOMAIN_ADDRESS);\r\n\r\n  return { domainAddress, isSub: false };\r\n};\r\n"],"names":["async","domain","record","endsWith","slice","recordClass","RecordVersion","V2","CENTRAL_STATE_DOMAIN_RECORDS","undefined","recordPrefix","V1","splitted","split","length","parentAddress","deriveAddress","ROOT_DOMAIN_ADDRESS","domainAddress","isSub","subAddress","isSubRecord","InvalidInputError"],"mappings":"sLAqBgCA,OAC9BC,SACAC,OAAAA,MAEID,EAAOE,SAAS,UAClBF,EAASA,EAAOG,MAAM,GAAG,IAG3B,MAAMC,EACJH,IAAWI,EAAaA,cAACC,GAAKC,EAA4BA,kCAAGC,EACzDC,EACJ,CACE,CAACJ,EAAAA,cAAcC,IAAK,IACpB,CAACD,EAAAA,cAAcK,IAAK,KACpBT,IAA4B,KAC1BU,EAAWX,EAAOY,MAAM,KAE9B,GAAwB,IAApBD,EAASE,OAAc,CACzB,MAAMC,QAAsBC,EAAaA,cAACJ,EAAS,GAAIK,EAAAA,qBAOvD,MAAO,CAAEC,oBANmBF,EAAAA,cAC1BN,EAAeE,EAAS,GACxBG,EACAV,GAGsBU,gBAAeI,OAAO,GACzC,GAAwB,IAApBP,EAASE,QAAkBZ,EAAQ,CAE5C,MAAMa,QAAsBC,EAAaA,cAACJ,EAAS,GAAIK,EAAAA,qBAGjDG,QAAmBJ,gBAAc,KAAOJ,EAAS,GAAIG,GAS3D,MAAO,CAAEG,oBANmBF,EAAAA,cAC1BN,EAAeE,EAAS,GACxBQ,EACAf,GAGsBU,gBAAeI,OAAO,EAAME,aAAa,GAC5D,GAAIT,EAASE,QAAU,EAC5B,MAAM,IAAIQ,EAAiBA,kBAAC,2BAK9B,MAAO,CAAEJ,oBAFmBF,gBAAcf,EAAQgB,EAAAA,qBAE1BE,OAAO,EAAO"}