{"version":3,"file":"bcs.mjs","names":[],"sources":["../../src/utils/bcs.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsType } from '@mysten/sui/bcs';\nimport { bcs } from '@mysten/sui/bcs';\n\nconst MerkleNode = bcs.enum('MerkleNode', {\n\tEmpty: null,\n\tDigest: bcs.bytes(32),\n});\n\nconst SliverPairMetadata = bcs.struct('SliverPairMetadata', {\n\tprimary_hash: MerkleNode,\n\tsecondary_hash: MerkleNode,\n});\n\nexport const EncodingType = bcs\n\t.enum('EncodingType', {\n\t\tRedStuff: null,\n\t\tRS2: null,\n\t})\n\t.transform({\n\t\tinput: (\n\t\t\tencodingType:\n\t\t\t\t| { RedStuff: boolean | object | null }\n\t\t\t\t| { RS2: boolean | object | null }\n\t\t\t\t| 'RedStuff'\n\t\t\t\t| 'RS2',\n\t\t) =>\n\t\t\ttypeof encodingType === 'string'\n\t\t\t\t? ({ [encodingType]: null } as Exclude<typeof encodingType, string>)\n\t\t\t\t: encodingType,\n\t\toutput: (encodingType) => encodingType,\n\t});\n\nexport const BlobMetadataV1 = bcs.struct('BlobMetadataV1', {\n\tencoding_type: EncodingType,\n\tunencoded_length: bcs.u64(),\n\thashes: bcs.vector(SliverPairMetadata),\n});\n\nexport const BlobMetadata = bcs.enum('BlobMetadata', {\n\tV1: BlobMetadataV1,\n});\n\nexport const BlobId = bcs.u256().transform({\n\tinput: (blobId: string | bigint) => (typeof blobId === 'string' ? blobIdToInt(blobId) : blobId),\n\toutput: (id: string) => blobIdFromInt(id),\n});\n\nexport function blobIdFromInt(blobId: bigint | string): string {\n\treturn bcs\n\t\t.u256()\n\t\t.serialize(blobId)\n\t\t.toBase64()\n\t\t.replace(/=*$/, '')\n\t\t.replaceAll('+', '-')\n\t\t.replaceAll('/', '_');\n}\n\nexport function blobIdFromBytes(blobId: Uint8Array): string {\n\treturn blobIdFromInt(bcs.u256().parse(blobId));\n}\n\nexport function blobIdToInt(blobId: string): bigint {\n\treturn BigInt(bcs.u256().fromBase64(blobId.replaceAll('-', '+').replaceAll('_', '/')));\n}\n\nexport const BlobMetadataWithId = bcs.struct('BlobMetadataWithId', {\n\tblobId: BlobId,\n\tmetadata: BlobMetadata,\n});\n\nconst Symbols = bcs.struct('Symbols', {\n\tdata: bcs.byteVector(),\n\tsymbol_size: bcs.u16(),\n});\n\nexport const SliverData = bcs.struct('SliverData', {\n\tsymbols: Symbols,\n\tindex: bcs.u16(),\n});\n\nexport const Sliver = bcs.enum('Sliver', {\n\tPrimary: SliverData,\n\tSecondary: SliverData,\n});\n\nexport const SliverPair = bcs.struct('SliverPair', {\n\tprimary: SliverData,\n\tsecondary: SliverData,\n});\n\nexport enum IntentType {\n\tPROOF_OF_POSSESSION_MSG = 0,\n\tBLOB_CERT_MSG = 1,\n\tINVALID_BLOB_ID_MSG = 2,\n\tSYNC_SHARD_MSG = 3,\n}\n\nexport const Intent = bcs\n\t.struct('Intent', {\n\t\ttype: bcs.u8().transform({\n\t\t\tinput: (type: IntentType) => type,\n\t\t\toutput: (type: number) => type as IntentType,\n\t\t}),\n\t\tversion: bcs.u8(),\n\t\tappId: bcs.u8(),\n\t})\n\t.transform({\n\t\tinput: (intent: IntentType) => ({\n\t\t\ttype: intent,\n\t\t\tversion: 0,\n\t\t\tappId: 3,\n\t\t}),\n\t\toutput: (intent) => intent.type,\n\t});\n\nexport function ProtocolMessage<T extends BcsType<any>>(messageContents: T) {\n\treturn bcs.struct(`ProtocolMessage<${messageContents.name}>`, {\n\t\tintent: Intent,\n\t\tepoch: bcs.u32(),\n\t\tmessageContents,\n\t});\n}\n\nexport const BlobPersistenceType = bcs.enum('BlobPersistenceType', {\n\tPermanent: null,\n\tDeletable: bcs.struct('Deletable', {\n\t\tobjectId: bcs.Address,\n\t}),\n});\nexport const StorageConfirmationBody = bcs.struct('StorageConfirmationBody', {\n\tblobId: BlobId,\n\tblobType: BlobPersistenceType,\n});\n\nexport const StorageConfirmation = ProtocolMessage(StorageConfirmationBody);\n\nexport function Field<T0 extends BcsType<any>, T1 extends BcsType<any>>(\n\t...typeParameters: [T0, T1]\n) {\n\treturn bcs.struct('Field', {\n\t\tid: bcs.Address,\n\t\tname: typeParameters[0],\n\t\tvalue: typeParameters[1],\n\t});\n}\n\nexport const QuiltPatchTags = bcs.map(bcs.string(), bcs.string()).transform({\n\t// Accept both Record and Map as input, bcs.map() handles sorting automatically\n\tinput: (tags: Record<string, string> | Map<string, string>) =>\n\t\ttags instanceof Map ? tags : new Map(Object.entries(tags)),\n\toutput: (tags: Map<string, string>) => Object.fromEntries(tags),\n});\n\nexport const QuiltPatchV1 = bcs.struct('QuiltPatchV1', {\n\tendIndex: bcs.u16(),\n\tidentifier: bcs.string(),\n\ttags: QuiltPatchTags,\n});\n\nexport const QuiltIndexV1 = bcs.struct('QuiltIndexV1', {\n\tpatches: bcs.vector(QuiltPatchV1),\n});\n\nexport const QuiltPatchId = bcs.struct('QuiltPatchId', {\n\tquiltId: BlobId,\n\tpatchId: bcs.struct('InternalQuiltPatchId', {\n\t\tversion: bcs.u8(),\n\t\tstartIndex: bcs.u16(),\n\t\tendIndex: bcs.u16(),\n\t}),\n});\n\nexport const QuiltPatchBlobHeader = bcs.struct('QuiltPatchBlobHeader', {\n\tversion: bcs.u8(),\n\tlength: bcs.u32(),\n\tmask: bcs.u8(),\n});\n\nexport const CertificateBcs = bcs.struct('Certificate', {\n\tsigners: bcs.vector(bcs.u16()),\n\tserializedMessage: bcs.byteVector(),\n\tsignature: bcs.byteVector(),\n});\n\nexport function parseCertificateFromBase64(base64: string) {\n\tconst cert = CertificateBcs.fromBase64(base64);\n\treturn {\n\t\tsigners: cert.signers,\n\t\tserializedMessage: new Uint8Array(cert.serializedMessage),\n\t\tsignature: new Uint8Array(cert.signature),\n\t};\n}\n"],"mappings":";;;AAMA,MAAM,aAAa,IAAI,KAAK,cAAc;CACzC,OAAO;CACP,QAAQ,IAAI,MAAM,GAAG;CACrB,CAAC;AAEF,MA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