{"version":3,"file":"wasm.mjs","names":[],"sources":["../src/wasm.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { fromBase64 } from '@mysten/bcs';\nimport init, {\n\tBlobEncoder,\n\tbls12381_min_pk_aggregate,\n\tbls12381_min_pk_verify,\n} from '@mysten/walrus-wasm';\n\nimport type { StorageConfirmation } from './storage-node/types.js';\nimport type { EncodingType, ProtocolMessageCertificate } from './types.js';\nimport type { BlobMetadata } from './utils/bcs.js';\nimport { BlobId, blobIdFromBytes } from './utils/bcs.js';\nimport { getSourceSymbols } from './utils/index.js';\n\nexport interface EncodedBlob {\n\tprimarySlivers: Uint8Array[];\n\tsecondarySlivers: Uint8Array[];\n\tblobId: string;\n\tmetadata: typeof BlobMetadata.$inferInput;\n\trootHash: Uint8Array;\n}\n\nexport async function getWasmBindings(url?: string) {\n\tawait init({ module_or_path: url });\n\n\tfunction encodeBlob(\n\t\tnShards: number,\n\t\tbytes: Uint8Array,\n\t\tencodingType: EncodingType = 'RS2',\n\t): EncodedBlob {\n\t\tconst encoder = new BlobEncoder(nShards);\n\n\t\tif (encodingType !== 'RS2') {\n\t\t\tthrow new Error(`Unsupported encoding type: ${encodingType}`);\n\t\t}\n\n\t\t// Pre-allocate BCS buffers\n\t\tconst bufferSizes = computeBcsBufferSizes(bytes.length, nShards);\n\t\tconst primaryBuffers = Array.from({ length: nShards }).map(\n\t\t\t() => new Uint8Array(bufferSizes.primary),\n\t\t);\n\t\tconst secondaryBuffers = Array.from({ length: nShards }).map(\n\t\t\t() => new Uint8Array(bufferSizes.secondary),\n\t\t);\n\n\t\tconst [metadata, rootHash] = encoder.encode(bytes, primaryBuffers, secondaryBuffers);\n\n\t\treturn {\n\t\t\tprimarySlivers: primaryBuffers,\n\t\t\tsecondarySlivers: secondaryBuffers,\n\t\t\tblobId: blobIdFromBytes(new Uint8Array(metadata.blob_id)),\n\t\t\tmetadata: metadata.metadata,\n\t\t\trootHash: new Uint8Array(rootHash.Digest),\n\t\t};\n\t}\n\n\tfunction combineSignatures(\n\t\tconfirmations: StorageConfirmation[],\n\t\tsignerIndices: number[],\n\t): ProtocolMessageCertificate {\n\t\tconst signature = bls12381_min_pk_aggregate(\n\t\t\tconfirmations.map((confirmation) => fromBase64(confirmation.signature)),\n\t\t);\n\n\t\treturn {\n\t\t\tsigners: signerIndices,\n\t\t\tserializedMessage: fromBase64(confirmations[0].serializedMessage),\n\t\t\tsignature,\n\t\t};\n\t}\n\n\tfunction decodePrimarySlivers(\n\t\tblobId: string,\n\t\tnShards: number,\n\t\tsize: number | bigint | string,\n\t\tslivers: Uint8Array[],\n\t\tencodingType: EncodingType = 'RS2',\n\t): Uint8Array {\n\t\tconst encoder = new BlobEncoder(nShards);\n\n\t\tif (encodingType !== 'RS2') {\n\t\t\tthrow new Error(`Unsupported encoding type: ${encodingType}`);\n\t\t}\n\n\t\tconst blobSize = BigInt(size);\n\t\tconst outputBuffer = new Uint8Array(Number(blobSize));\n\t\tencoder.decode(BlobId.serialize(blobId).toBytes(), blobSize, slivers, outputBuffer);\n\t\treturn outputBuffer;\n\t}\n\n\tfunction getVerifySignature() {\n\t\treturn (confirmation: StorageConfirmation, publicKey: Uint8Array) =>\n\t\t\tbls12381_min_pk_verify(\n\t\t\t\tfromBase64(confirmation.signature),\n\t\t\t\tpublicKey,\n\t\t\t\tfromBase64(confirmation.serializedMessage),\n\t\t\t);\n\t}\n\n\tfunction computeMetadata(\n\t\tnShards: number,\n\t\tbytes: Uint8Array,\n\t\tencodingType: EncodingType = 'RS2',\n\t): { blobId: string; rootHash: Uint8Array; unencodedLength: bigint; encodingType: EncodingType } {\n\t\tconst encoder = new BlobEncoder(nShards);\n\t\tconst [blobId, rootHash, unencodedLength, encType] = encoder.compute_metadata(bytes);\n\n\t\tif (encodingType !== 'RS2') {\n\t\t\tthrow new Error(`Unsupported encoding type: ${encodingType}`);\n\t\t}\n\n\t\treturn {\n\t\t\tblobId: blobIdFromBytes(new Uint8Array(blobId)),\n\t\t\trootHash: new Uint8Array(rootHash.Digest),\n\t\t\tunencodedLength: BigInt(unencodedLength),\n\t\t\tencodingType: encType as EncodingType,\n\t\t};\n\t}\n\n\treturn {\n\t\tencodeBlob,\n\t\tcombineSignatures,\n\t\tdecodePrimarySlivers,\n\t\tgetVerifySignature,\n\t\tcomputeMetadata,\n\t};\n}\n\nfunction uleb128Size(value: number): number {\n\tlet size = 1;\n\tvalue >>= 7;\n\twhile (value !== 0) {\n\t\tsize++;\n\t\tvalue >>= 7;\n\t}\n\treturn size;\n}\n\nfunction computeBcsBufferSize(dataLength: number): number {\n\tconst ulebSize = uleb128Size(dataLength);\n\treturn ulebSize + dataLength + 2 + 2; // ULEB128 + data + symbol_size + index\n}\n\nfunction computeBcsBufferSizes(blobSize: number, nShards: number) {\n\tconst { primarySymbols, secondarySymbols } = getSourceSymbols(nShards);\n\n\tlet symbolSize =\n\t\tMath.floor((Math.max(blobSize, 1) - 1) / (primarySymbols * secondarySymbols)) + 1;\n\tif (symbolSize % 2 === 1) {\n\t\tsymbolSize = symbolSize + 1;\n\t}\n\n\tconst primarySliverSize = secondarySymbols * symbolSize;\n\tconst secondarySliverSize = primarySymbols * symbolSize;\n\n\tconst primaryBcsSize = computeBcsBufferSize(primarySliverSize);\n\tconst secondaryBcsSize = computeBcsBufferSize(secondarySliverSize);\n\n\treturn {\n\t\tnShards,\n\t\tprimary: primaryBcsSize,\n\t\tsecondary: secondaryBcsSize,\n\t};\n}\n"],"mappings":";;;;;;AAwBA,eAAsB,gBAAgB,KAAc;AACnD,OAAM,KAAK,EAAE,gBAAgB,KAAK,CAAC;CAEnC,SAAS,WACR,SACA,OACA,eAA6B,OACf;EACd,MAAM,UAAU,IAAI,YAAY,QAAQ;AAExC,MAAI,iBAAiB,MACpB,OAAM,IAAI,MAAM,8BAA8B,eAAe;EAI9D,MAAM,cAAc,sBAAsB,MAAM,QAAQ,QAAQ;EAChE,MAAM,iBAAiB,MAAM,KAAK,EAAE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