{"version":3,"file":"write-files.mjs","names":[],"sources":["../../src/flows/write-files.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { Signer } from '@mysten/sui/cryptography';\nimport type { Transaction } from '@mysten/sui/transactions';\n\nimport type {\n\tWriteBlobFlowOptions,\n\tWriteBlobStep,\n\tWriteBlobStepEncoded,\n\tWriteFilesFlow,\n\tWriteFilesFlowOptions,\n\tWriteFilesFlowRegisterOptions,\n\tWriteFilesFlowRunOptions,\n} from '../types.js';\nimport { encodeQuiltPatchId } from '../utils/quilts.js';\nimport type { WalrusClient } from '../client.js';\n\nexport function createWriteFilesFlow(\n\tclient: WalrusClient,\n\t{ files, resume }: WriteFilesFlowOptions,\n): WriteFilesFlow {\n\tlet quiltBytes: Uint8Array | undefined;\n\tlet quiltIndex: Awaited<ReturnType<typeof client.encodeQuilt>>['index'] | undefined;\n\n\tconst blobFlow = client.writeBlobFlow({\n\t\tget blob(): Uint8Array {\n\t\t\tif (!quiltBytes) {\n\t\t\t\tthrow new Error('encode must be executed before accessing blob');\n\t\t\t}\n\t\t\treturn quiltBytes;\n\t\t},\n\t\tresume,\n\t} as WriteBlobFlowOptions); // Cast needed: blob is a lazy getter populated by encode()\n\n\tconst encode = async (): Promise<WriteBlobStepEncoded> => {\n\t\tif (!quiltBytes) {\n\t\t\tconst { quilt, index } = await client.encodeQuilt({\n\t\t\t\tblobs: await Promise.all(\n\t\t\t\t\tfiles.map(async (file, i) => ({\n\t\t\t\t\t\tcontents: await file.bytes(),\n\t\t\t\t\t\tidentifier: (await file.getIdentifier()) ?? `file-${i}`,\n\t\t\t\t\t\ttags: (await file.getTags()) ?? {},\n\t\t\t\t\t})),\n\t\t\t\t),\n\t\t\t});\n\t\t\tquiltBytes = quilt;\n\t\t\tquiltIndex = index;\n\t\t}\n\n\t\treturn blobFlow.encode();\n\t};\n\n\tconst register = (options: WriteFilesFlowRegisterOptions): Transaction => {\n\t\tif (!quiltBytes) {\n\t\t\tthrow new Error('encode must be executed before calling register');\n\t\t}\n\n\t\treturn blobFlow.register({\n\t\t\t...options,\n\t\t\tattributes: {\n\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t...options.attributes,\n\t\t\t},\n\t\t});\n\t};\n\n\tconst listFiles = async () => {\n\t\tif (!quiltIndex) {\n\t\t\tthrow new Error('encode must be executed before calling listFiles');\n\t\t}\n\n\t\tconst certResult = await blobFlow.getBlob();\n\t\treturn quiltIndex.patches.map((patch) => ({\n\t\t\tid: encodeQuiltPatchId({\n\t\t\t\tquiltId: certResult.blobId,\n\t\t\t\tpatchId: {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tstartIndex: patch.startIndex,\n\t\t\t\t\tendIndex: patch.endIndex,\n\t\t\t\t},\n\t\t\t}),\n\t\t\tblobId: certResult.blobId,\n\t\t\tblobObject: certResult.blobObject,\n\t\t}));\n\t};\n\n\t/** @yields {WriteBlobStep} */\n\tasync function* run(options: WriteFilesFlowRunOptions): AsyncGenerator<WriteBlobStep> {\n\t\tconst resumeStep = resume?.step;\n\t\tconst stepOrder = ['encoded', 'registered', 'uploaded', 'certified'] as const;\n\t\tconst resumeIndex = resumeStep ? stepOrder.indexOf(resumeStep) : -1;\n\n\t\tif (resumeIndex >= stepOrder.indexOf('certified')) {\n\t\t\treturn;\n\t\t}\n\n\t\tif (resumeIndex < stepOrder.indexOf('encoded')) {\n\t\t\tyield await encode();\n\t\t} else if (resumeIndex < stepOrder.indexOf('uploaded')) {\n\t\t\tawait encode();\n\t\t}\n\n\t\tconst resumeBlobObjectId = resume && 'blobObjectId' in resume ? resume.blobObjectId : undefined;\n\t\tlet registerDigest: string | undefined;\n\t\tif (!resumeBlobObjectId) {\n\t\t\tconst regResult = await blobFlow.executeRegister({\n\t\t\t\tsigner: options.signer,\n\t\t\t\tepochs: options.epochs,\n\t\t\t\tdeletable: options.deletable,\n\t\t\t\towner: options.owner ?? options.signer.toSuiAddress(),\n\t\t\t\tattributes: {\n\t\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t\t...options.attributes,\n\t\t\t\t},\n\t\t\t});\n\t\t\tregisterDigest = regResult.txDigest;\n\t\t\tyield regResult;\n\t\t}\n\n\t\tif (resumeIndex < stepOrder.indexOf('uploaded')) {\n\t\t\tyield await blobFlow.upload({\n\t\t\t\tdigest: registerDigest,\n\t\t\t\tdeletable: options.deletable,\n\t\t\t\tsignal: options.signal,\n\t\t\t});\n\t\t}\n\n\t\tif (resumeIndex < stepOrder.indexOf('certified')) {\n\t\t\tyield await blobFlow.executeCertify({ signer: options.signer });\n\t\t}\n\t}\n\n\treturn {\n\t\tencode: encode,\n\t\tregister: register,\n\t\tupload: blobFlow.upload,\n\t\tcertify: blobFlow.certify,\n\t\tlistFiles: listFiles,\n\t\texecuteRegister: async (options: WriteFilesFlowRegisterOptions & { signer: Signer }) => {\n\t\t\tconst { signer, ...rest } = options;\n\t\t\treturn blobFlow.executeRegister({\n\t\t\t\tsigner,\n\t\t\t\t...rest,\n\t\t\t\tattributes: {\n\t\t\t\t\t_walrusBlobType: 'quilt',\n\t\t\t\t\t...rest.attributes,\n\t\t\t\t},\n\t\t\t});\n\t\t},\n\t\texecuteCertify: blobFlow.executeCertify,\n\t\trun,\n\t};\n}\n"],"mappings":";;;AAkBA,SAAgB,qBACf,QACA,EAAE,OAAO,UACQ;CACjB,IAAI;CACJ,IAAI;CAEJ,MAAM,WAAW,OAAO,cAAc;EACrC,IAAI,OAAmB;AACtB,OAAI,CAAC,WACJ,OAAM,IAAI,MAAM,gDAAgD;AAEjE,UAAO;;EAER;EACA,CAAyB;CAE1B,MAAM,SAAS,YAA2C;AACzD,MAAI,CAAC,YAAY;GAChB,MAAM,EAAE,OAAO,UAAU,MAAM,OAAO,YAAY,EACjD,OAAO,MAAM,QAAQ,IACpB,MAAM,IAAI,OAAO,MAAM,OAAO;IAC7B,UAAU,MAAM,KAAK,OAAO;IAC5B,YAAa,MAAM,KAAK,eAAe,IAAK,QAAQ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