{"version":3,"file":"record.cjs","sources":["../../../src/states/record.ts"],"sourcesContent":["import {\r\n  Address,\r\n  GetAccountInfoApi,\r\n  GetMultipleAccountsApi,\r\n  Rpc,\r\n  fetchEncodedAccount,\r\n  fetchEncodedAccounts,\r\n} from \"@solana/kit\";\r\nimport { Schema, deserialize } from \"borsh\";\r\n\r\nimport { NoRecordDataError } from \"../errors\";\r\nimport { Validation } from \"../types/validation\";\r\n\r\nexport const NAME_REGISTRY_LEN = 96;\r\n\r\nexport const getValidationLength = (validation: Validation) => {\r\n  switch (validation) {\r\n    case Validation.None:\r\n      return 0;\r\n    case Validation.Ethereum:\r\n      return 20;\r\n    case Validation.Solana:\r\n      return 32;\r\n    case Validation.UnverifiedSolana:\r\n      return 32;\r\n    default:\r\n      throw new Error(\"Invalid validation enum\");\r\n  }\r\n};\r\n\r\nexport class RecordHeaderState {\r\n  stalenessValidation: number;\r\n  rightOfAssociationValidation: number;\r\n  contentLength: number;\r\n\r\n  static schema: Schema = {\r\n    struct: {\r\n      stalenessValidation: \"u16\",\r\n      rightOfAssociationValidation: \"u16\",\r\n      contentLength: \"u32\",\r\n    },\r\n  };\r\n\r\n  // The total length of the struct is calculated as the sum of:\r\n  // - `stalenessValidation`: 2 bytes (`u16`)\r\n  // - `rightOfAssociationValidation`: 2 bytes (`u16`)\r\n  // - `contentLength`: 4 bytes (`u32`)\r\n  static LEN = 8;\r\n\r\n  constructor(obj: {\r\n    stalenessValidation: number;\r\n    rightOfAssociationValidation: number;\r\n    contentLength: number;\r\n  }) {\r\n    this.stalenessValidation = obj.stalenessValidation;\r\n    this.rightOfAssociationValidation = obj.rightOfAssociationValidation;\r\n    this.contentLength = obj.contentLength;\r\n  }\r\n\r\n  static deserialize(data: Uint8Array): RecordHeaderState {\r\n    return new RecordHeaderState(deserialize(this.schema, data, true) as any);\r\n  }\r\n\r\n  static async retrieve(\r\n    rpc: Rpc<GetAccountInfoApi>,\r\n    address: Address\r\n  ): Promise<RecordHeaderState> {\r\n    const recordHeaderAccount = await fetchEncodedAccount(rpc, address);\r\n\r\n    if (!recordHeaderAccount.exists) {\r\n      throw new Error(\"Record header account not found\");\r\n    }\r\n\r\n    return this.deserialize(\r\n      recordHeaderAccount.data.slice(\r\n        NAME_REGISTRY_LEN,\r\n        NAME_REGISTRY_LEN + this.LEN\r\n      )\r\n    );\r\n  }\r\n}\r\n\r\nexport class RecordState {\r\n  header: RecordHeaderState;\r\n  data: Uint8Array;\r\n\r\n  constructor(header: RecordHeaderState, data: Uint8Array) {\r\n    this.data = data;\r\n    this.header = header;\r\n  }\r\n\r\n  static deserialize(data: Uint8Array): RecordState {\r\n    const offset = NAME_REGISTRY_LEN;\r\n    const header = RecordHeaderState.deserialize(\r\n      data.slice(offset, offset + RecordHeaderState.LEN)\r\n    );\r\n\r\n    return new RecordState(header, data.slice(offset + RecordHeaderState.LEN));\r\n  }\r\n\r\n  static async retrieve(\r\n    rpc: Rpc<GetAccountInfoApi>,\r\n    address: Address\r\n  ): Promise<RecordState> {\r\n    const recordAccount = await fetchEncodedAccount(rpc, address);\r\n    if (!recordAccount.exists) {\r\n      throw new NoRecordDataError(\"Record account not found\");\r\n    }\r\n\r\n    return this.deserialize(recordAccount.data);\r\n  }\r\n\r\n  static async retrieveBatch(\r\n    rpc: Rpc<GetMultipleAccountsApi>,\r\n    addresses: Address[]\r\n  ): Promise<(RecordState | undefined)[]> {\r\n    const recordAccounts = await fetchEncodedAccounts(rpc, addresses);\r\n\r\n    return recordAccounts.map((account) =>\r\n      account.exists ? this.deserialize(account.data) : undefined\r\n    );\r\n  }\r\n\r\n  getContent(): Uint8Array {\r\n    const startOffset =\r\n      getValidationLength(this.header.stalenessValidation) +\r\n      getValidationLength(this.header.rightOfAssociationValidation);\r\n\r\n    return this.data.slice(startOffset);\r\n  }\r\n\r\n  getStalenessId(): Uint8Array {\r\n    const endOffset = getValidationLength(this.header.stalenessValidation);\r\n\r\n    return this.data.slice(0, endOffset);\r\n  }\r\n\r\n  getRoAId(): Uint8Array {\r\n    const startOffset = getValidationLength(this.header.stalenessValidation);\r\n    const endOffset =\r\n      startOffset +\r\n      getValidationLength(this.header.rightOfAssociationValidation);\r\n\r\n    return this.data.slice(startOffset, endOffset);\r\n  }\r\n}\r\n"],"names":["getValidationLength","validation","Validation","None","Ethereum","Solana","UnverifiedSolana","Error","RecordHeaderState","stalenessValidation","rightOfAssociationValidation","contentLength","static","struct","constructor","obj","this","deserialize","data","schema","retrieve","rpc","address","recordHeaderAccount","fetchEncodedAccount","exists","slice","LEN","RecordState","header","recordAccount","NoRecordDataError","retrieveBatch","addresses","fetchEncodedAccounts","map","account","undefined","getContent","startOffset","getStalenessId","endOffset","getRoAId"],"mappings":"+JAaO,MAEMA,EAAuBC,IAClC,OAAQA,GACN,KAAKC,EAAUA,WAACC,KACd,OAAO,EACT,KAAKD,EAAUA,WAACE,SACd,OAAO,GACT,KAAKF,EAAUA,WAACG,OAEhB,KAAKH,EAAUA,WAACI,iBACd,OAAO,GACT,QACE,MAAM,IAAIC,MAAM,mCAITC,EACXC,oBACAC,6BACAC,cAEAC,cAAwB,CACtBC,OAAQ,CACNJ,oBAAqB,MACrBC,6BAA8B,MAC9BC,cAAe,QAQnBC,WAAa,EAEb,WAAAE,CAAYC,GAKVC,KAAKP,oBAAsBM,EAAIN,oBAC/BO,KAAKN,6BAA+BK,EAAIL,6BACxCM,KAAKL,cAAgBI,EAAIJ,cAG3B,kBAAOM,CAAYC,GACjB,OAAO,IAAIV,EAAkBS,cAAYD,KAAKG,OAAQD,GAAM,IAG9D,qBAAaE,CACXC,EACAC,GAEA,MAAMC,QAA4BC,sBAAoBH,EAAKC,GAE3D,IAAKC,EAAoBE,OACvB,MAAM,IAAIlB,MAAM,mCAGlB,OAAOS,KAAKC,YACVM,EAAoBL,KAAKQ,MA7DE,MA+DLV,KAAKW,aAMpBC,EACXC,OACAX,KAEA,WAAAJ,CAAYe,EAA2BX,GACrCF,KAAKE,KAAOA,EACZF,KAAKa,OAASA,EAGhB,kBA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